{"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":"code","source":"#Loading important libraries\nimport numpy as np\nimport pandas as pd\n\nfrom fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2021-07-08T02:31:04.212153Z","iopub.execute_input":"2021-07-08T02:31:04.212588Z","iopub.status.idle":"2021-07-08T02:31:08.043570Z","shell.execute_reply.started":"2021-07-08T02:31:04.212480Z","shell.execute_reply":"2021-07-08T02:31:08.042156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Get DICOM files into path by using **get_dicom_files** ","metadata":{}},{"cell_type":"code","source":"items = get_dicom_files('../input/siim-covid19-detection/train', recurse = True)\nprint (items)","metadata":{"execution":{"iopub.status.busy":"2021-07-08T02:31:08.045338Z","iopub.execute_input":"2021-07-08T02:31:08.045646Z","iopub.status.idle":"2021-07-08T02:31:46.751961Z","shell.execute_reply.started":"2021-07-08T02:31:08.045622Z","shell.execute_reply":"2021-07-08T02:31:46.751144Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"fastai.medical.imaging uses pydicom.dcmread to read DICOM file. I specify the path from **items** and call **.dcmread**","metadata":{}},{"cell_type":"code","source":"dcm = items[0]\nDCM = dcm.dcmread()\nDCM","metadata":{"execution":{"iopub.status.busy":"2021-07-08T02:31:46.753150Z","iopub.execute_input":"2021-07-08T02:31:46.753347Z","iopub.status.idle":"2021-07-08T02:31:47.333342Z","shell.execute_reply.started":"2021-07-08T02:31:46.753324Z","shell.execute_reply":"2021-07-08T02:31:47.332184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Some key pointers on the tag information above**\n\n**Pixel Data**(7fe0, 0010)- This is where the raw pixel data is stored. Pydicom uses the pixel data (Array of 13249792 elements) to an image.\n\n**Phtometric interpretation**(0028, 0004) - a.k.a colour space. In this file it is 'MONOCHROME1'. It indicates that grayscale ranges from bright to dark with ascending pixel values.\n\n**Samples per Pixel**(0028,0002) - Number of samples (planes) in the image.It is should be 1 for this image because it is monochrome.\n\n**Bits stored**(0028,0101) - Number of bits stored for each pixel sample data.\n\nWe may need this meta-data in future analysis. \n\nhttps://www.dicomlibrary.com/dicom/dicom-tags/ have complete details about DICOM headers.","metadata":{}},{"cell_type":"markdown","source":"The DICOM file consists of image information as **pixel data**. We have to convert the pixel array into a tensor.","metadata":{}},{"cell_type":"code","source":"# Extracting the pixel data as an array\nx =DCM.pixel_array,DCM.pixel_array.shape\nprint (x)\n# Converting the pixel array into tensor using TensorDicom\ntdm = TensorDicom(DCM.pixel_array)\ntdm","metadata":{"execution":{"iopub.status.busy":"2021-07-08T02:31:47.334822Z","iopub.execute_input":"2021-07-08T02:31:47.335145Z","iopub.status.idle":"2021-07-08T02:31:47.433396Z","shell.execute_reply.started":"2021-07-08T02:31:47.335114Z","shell.execute_reply":"2021-07-08T02:31:47.432567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Converting the pixel data into a tensor it is easy to visualise the image by calling **.show** ","metadata":{}},{"cell_type":"code","source":"tdm.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-08T02:31:47.434192Z","iopub.execute_input":"2021-07-08T02:31:47.434385Z","iopub.status.idle":"2021-07-08T02:31:48.024351Z","shell.execute_reply.started":"2021-07-08T02:31:47.434363Z","shell.execute_reply":"2021-07-08T02:31:48.023269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, we are going to create a dataframe to store all the meta-data that we have loaded above","metadata":{}},{"cell_type":"code","source":"%%time\ndicoms_df = pd.DataFrame.from_dicoms(items)","metadata":{"execution":{"iopub.status.busy":"2021-07-08T02:31:48.025437Z","iopub.execute_input":"2021-07-08T02:31:48.025670Z","iopub.status.idle":"2021-07-08T03:08:51.918511Z","shell.execute_reply.started":"2021-07-08T02:31:48.025647Z","shell.execute_reply":"2021-07-08T03:08:51.917104Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicoms_df[:2]","metadata":{"execution":{"iopub.status.busy":"2021-07-08T03:08:51.919899Z","iopub.execute_input":"2021-07-08T03:08:51.920208Z","iopub.status.idle":"2021-07-08T03:08:51.963596Z","shell.execute_reply.started":"2021-07-08T03:08:51.920180Z","shell.execute_reply":"2021-07-08T03:08:51.962687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# It is a time consuming process to load dicoms into a dataframe. \n#So, i pickle the dataframe to load with ease in future.\ndicoms_df.shape\ndicoms_df.to_pickle('./dicoms_df.pkl')","metadata":{"execution":{"iopub.status.busy":"2021-07-08T03:08:51.964719Z","iopub.execute_input":"2021-07-08T03:08:51.965075Z","iopub.status.idle":"2021-07-08T03:08:52.539706Z","shell.execute_reply.started":"2021-07-08T03:08:51.965043Z","shell.execute_reply":"2021-07-08T03:08:52.538689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\ndicom_dataframe = pd.read_pickle('./dicoms_df.pkl')\ndicom_dataframe.shape #Just to demonstrate loading a .pkl into a dataframe","metadata":{"execution":{"iopub.status.busy":"2021-07-08T03:20:41.070961Z","iopub.execute_input":"2021-07-08T03:20:41.071362Z","iopub.status.idle":"2021-07-08T03:20:42.401447Z","shell.execute_reply.started":"2021-07-08T03:20:41.071337Z","shell.execute_reply":"2021-07-08T03:20:42.400604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_dataframe.columns #Attributes that could help with further analysis in the future","metadata":{"execution":{"iopub.status.busy":"2021-07-08T03:11:28.309878Z","iopub.execute_input":"2021-07-08T03:11:28.310203Z","iopub.status.idle":"2021-07-08T03:11:28.316133Z","shell.execute_reply.started":"2021-07-08T03:11:28.310177Z","shell.execute_reply":"2021-07-08T03:11:28.315426Z"},"trusted":true},"execution_count":null,"outputs":[]}]}