{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# What's a DICOM file?","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# I like this explanation from Wikipedia: \"DICOM groups information into data sets. For example, a file of a chest x-ray image may contain the patient ID within the file, so that the image can never be separated from this information by mistake. This is similar to the way that image formats such as JPEG can also have embedded tags to identify and otherwise describe the image.\"","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Let's use pydicom to read the files","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pydicom","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# methods in pydicom?\ndir(pydicom)\n# I see a read_file and dcmread","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Version check\npydicom.__version__","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Read dcm file\nds = pydicom.read_file(\"/kaggle/input/osic-pulmonary-fibrosis-progression/test/ID00421637202311550012437/32.dcm\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Explore the dcm file\nds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's try dcmread\nds2 = pydicom.dcmread(\"/kaggle/input/osic-pulmonary-fibrosis-progression/test/ID00421637202311550012437/32.dcm\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Is ds2 same as ds?\nds2\n# Looks like it's the same. I think read_file will be removed in future. See this - https://github.com/pydicom/pydicom/issues/475\n# Below we can see the information for patient id - 'ID00421637202311550012437' and the CT scan of chest and details stored in pixel data","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot the data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Credits: https://pydicom.github.io/pydicom/stable/auto_examples/input_output/plot_read_dicom.html#sphx-glr-auto-examples-input-output-plot-read-dicom-py\nif 'PixelData' in ds2:\n    rows = int(ds2.Rows)\n    cols = int(ds2.Columns)\n    print(\"Image size.......: {rows:d} x {cols:d}, {size:d} bytes\".format(\n        rows=rows, cols=cols, size=len(ds2.PixelData)))\n    if 'PixelSpacing' in ds2:\n        print(\"Pixel spacing....:\", ds2.PixelSpacing)\n        \n# Pixel data can be used to plot the CT scan       ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"help(plt.imshow) # read more about imshow here: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot the image using matplotlib\nplt.imshow(ds2.pixel_array, cmap=plt.cm.bone) \nplt.show()\n# We see the chest CT scan of the patient.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's try dcmread another file\nds3 = pydicom.dcmread(\"/kaggle/input/osic-pulmonary-fibrosis-progression/test/ID00421637202311550012437/18.dcm\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if 'PixelData' in ds3:\n    rows = int(ds3.Rows)\n    cols = int(ds3.Columns)\n    print(\"Image size.......: {rows:d} x {cols:d}, {size:d} bytes\".format(\n        rows=rows, cols=cols, size=len(ds3.PixelData)))\n    if 'PixelSpacing' in ds3:\n        print(\"Pixel spacing....:\", ds3.PixelSpacing)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(ds3.pixel_array, cmap=plt.cm.bone)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define a function to read all CT scans of a patient","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\ndef read_all_dcm(PatientID):\n    Path = \"/kaggle/input/osic-pulmonary-fibrosis-progression/test/\" + PatientID + \"/\"\n    path, dirs, files = next(os.walk(Path))\n    file_count = len(files)\n    PathList = [0]*file_count\n    for i in range(len(PathList)):   \n        try:\n            PathList[i+1] = Path + str(i+1) + \".dcm\"\n            plt.imshow(pydicom.dcmread(PathList[i+1]).pixel_array, cmap=plt.cm.bone)\n            plt.show()\n            print(PathList[i+1])\n        except:\n            pass\n        \n    return","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"read_all_dcm(\"ID00421637202311550012437\") # Below you can see all CT scans of the patient selected.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# That's it Folks! Discussion upvotes: Please suggest how to display 4 images per row in order? Can we calculate FVC from the images?  ","execution_count":null}],"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":4,"nbformat_minor":4}