{"cells":[{"metadata":{},"cell_type":"markdown","source":"# OSIC Simple Image EDA\n\nJust a very simple notebook to help understand the image data for potential modeling use :)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Imports","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\nimport json\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# View Single Image","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filename = \"/kaggle/input/osic-pulmonary-fibrosis-progression/train/ID00123637202217151272140/137.dcm\"\nds = pydicom.dcmread(filename)\nplt.imshow(ds.pixel_array, cmap=plt.cm.bone) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# View Examples of Image Size","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filename = \"/kaggle/input/osic-pulmonary-fibrosis-progression/train/ID00123637202217151272140/137.dcm\"\npydicom.dcmread(filename).pixel_array.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filename = \"/kaggle/input/osic-pulmonary-fibrosis-progression/train/ID00136637202224951350618/353.dcm\"\npydicom.dcmread(filename).pixel_array.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# View Example of Image Meta-Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filename = \"/kaggle/input/osic-pulmonary-fibrosis-progression/train/ID00123637202217151272140/137.dcm\"\npydicom.dcmread(filename)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Example Extracting Meta-Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"dir(pydicom.dcmread(filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dir(pydicom.dcmread(filename)['ImageOrientationPatient'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pydicom.dcmread(filename)['ImageOrientationPatient'].to_json()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"json.loads(pydicom.dcmread(filename)['ImageOrientationPatient'].to_json())['Value']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# View First Few Images in Order","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# directory for a patient\nimdir = \"/kaggle/input/osic-pulmonary-fibrosis-progression/train/ID00123637202217151272140\"\nprint(\"total images for patient ID00123637202217151272140: \", len(os.listdir(imdir)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"images for patient ID00123637202217151272140 in a rough order:\")\nmylist = os.listdir(imdir)\nmylist.sort()\nprint(mylist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# view first (columns*rows) images in order\nw=10\nh=10\nfig=plt.figure(figsize=(12, 12))\ncolumns = 4\nrows = 5\nimglist = os.listdir(imdir)\nfor i in range(1, columns*rows +1):\n    filename = imdir + \"/\" + str(i) + \".dcm\"\n    ds = pydicom.dcmread(filename)\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(ds.pixel_array, cmap=plt.cm.bone)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Number of Patients and Images in Training Images Folder","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"files = folders = 0\n\npath = \"/kaggle/input/osic-pulmonary-fibrosis-progression/train\"\n\nfor _, dirnames, filenames in os.walk(path):\n  # ^ this idiom means \"we won't be using this value\"\n    files += len(filenames)\n    folders += len(dirnames)\n\nprint(\"{:,} files/images, {:,} folders/patients\".format(files, folders))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"files = []\nfor _, dirnames, filenames in os.walk(path):\n  # ^ this idiom means \"we won't be using this value\"\n    files.append(len(filenames))\n\nprint(\"{:,} average files/images per patient\".format(round(np.mean(files))))\nprint(\"{:,} max files/images per patient\".format(round(np.max(files))))\nprint(\"{:,} min files/images per patient\".format(round(np.min(files))))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Number of Patients and Images in Test Images Folder","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"files = folders = 0\n\npath = \"/kaggle/input/osic-pulmonary-fibrosis-progression/test\"\n\nfor _, dirnames, filenames in os.walk(path):\n  # ^ this idiom means \"we won't be using this value\"\n    files += len(filenames)\n    folders += len(dirnames)\n\nprint(\"{:,} files/images, {:,} folders/patients\".format(files, folders))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"files = []\nfor _, dirnames, filenames in os.walk(path):\n  # ^ this idiom means \"we won't be using this value\"\n    files.append(len(filenames))\n\nprint(\"{:,} average files/images per patient\".format(round(np.mean(files))))\nprint(\"{:,} max files/images per patient\".format(round(np.max(files))))\nprint(\"{:,} min files/images per patient\".format(round(np.min(files))))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Resources\n\nhttps://pydicom.github.io/pydicom/stable/old/viewing_images.html\n\nhttps://stackoverflow.com/questions/46615554/how-to-display-multiple-images-in-one-figure-correctly/46616645\n\nhttps://stackoverflow.com/questions/29769181/count-the-number-of-folders-in-a-directory-and-subdirectories","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}