{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Libraries import","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport pydicom as dicom\nfrom pydicom.pixel_data_handlers import pillow_handler\npillow_handler.PillowJPEGTransferSyntaxes.append('1.2.840.10008.1.2.4.70')\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import imshow, show \n","metadata":{"execution":{"iopub.status.busy":"2022-12-27T11:34:59.860869Z","iopub.execute_input":"2022-12-27T11:34:59.86173Z","iopub.status.idle":"2022-12-27T11:34:59.868247Z","shell.execute_reply.started":"2022-12-27T11:34:59.861689Z","shell.execute_reply":"2022-12-27T11:34:59.866663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reading sample image**\n\nI,m going to read some sample image. It's my first time witch .dcm files so i need some warm up.","metadata":{}},{"cell_type":"code","source":"#sample image path\nimagePath = '/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm'\n#getting pixel array\nds = dicom.dcmread(imagePath)\n#image shape\nprint(ds.pixel_array.shape)\n#printing image out\nplt.imshow(ds.pixel_array)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-27T11:35:01.641246Z","iopub.execute_input":"2022-12-27T11:35:01.64169Z","iopub.status.idle":"2022-12-27T11:35:04.965948Z","shell.execute_reply.started":"2022-12-27T11:35:01.641651Z","shell.execute_reply":"2022-12-27T11:35:04.964625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Import Csv**\n\nI'm going to use some of the columns for image processing purpouse. I will iterate through folders and convert all photos to one row.","metadata":{}},{"cell_type":"code","source":"#import csv\ntrain = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\nprint(train.head(10))\n\n#creating patient id list\npatientId = train.patient_id.values.tolist()\n\n#deleting repetitions in patient id list\npatientId = list(dict.fromkeys(patientId))","metadata":{"execution":{"iopub.status.busy":"2022-12-27T11:35:07.897315Z","iopub.execute_input":"2022-12-27T11:35:07.897766Z","iopub.status.idle":"2022-12-27T11:35:07.973837Z","shell.execute_reply.started":"2022-12-27T11:35:07.897728Z","shell.execute_reply":"2022-12-27T11:35:07.972674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image to pixel data**\nIm going to crate a .csv file where i will save pixel data and information about photo for further analisys.","metadata":{}},{"cell_type":"code","source":"#x is just for testing. I wanted to avoid to much calculations\nx = 0\n\n#iterating throughout patinet id list-----------------------------------------------------------------------------------\nfor a,i in enumerate(patientId):\n    \n    #printing out current id        !!just for testing !!\n    print('\\n'+str(i))\n    #getting image id that belong to patient with id=i\n    imgIdForPatientId = train[train['patient_id']==i].image_id.values.tolist()\n    #pprinting those images          !!just for testing!!\n    print(imgIdForPatientId)\n    \n    #iterating through image id for i -th patient-----------------------------------------------------------------------\n    for b,j in enumerate(imgIdForPatientId):\n\n        #creating file path to photo\n        imagePath = '/kaggle/input/rsna-breast-cancer-detection/train_images/'+str(i)+'/'+str(j)+'.dcm'\n        #getting pixel array\n        ds = dicom.dcmread(imagePath)\n        #reshaping the photo to get one row\n        dsReshaped = ds.pixel_array.reshape(1,-1)\n        npArrToExport = np.vstack([npArrToExport, dsReshaped])\n        \n\ndfToExport = pd.DataFrame(npArrToExport)\n#just testing, checking if conversion went all right\nprint('a')\nrawPixels = dfToExport.iloc[2].values\nprint('b')\nrawPixels = rawPixels.reshape(5355,4915)\nprint('c')\nimshow(rawPixels)\nshow()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T11:51:50.57922Z","iopub.execute_input":"2022-12-27T11:51:50.579683Z","iopub.status.idle":"2022-12-27T11:52:00.71514Z","shell.execute_reply.started":"2022-12-27T11:51:50.579645Z","shell.execute_reply":"2022-12-27T11:52:00.713311Z"},"trusted":true},"execution_count":null,"outputs":[]}]}