{"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":"!pip install -qU ../input/for-pydicom/python_gdcm-3.0.14-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl ../input/for-pydicom/pylibjpeg-1.4.0-py3-none-any.whl --find-links frozen_packages --no-index","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:35:23.667349Z","iopub.execute_input":"2022-08-24T13:35:23.668132Z","iopub.status.idle":"2022-08-24T13:35:37.904657Z","shell.execute_reply.started":"2022-08-24T13:35:23.668002Z","shell.execute_reply":"2022-08-24T13:35:37.903569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport time\nimport nibabel as nib\nimport pandas as pd\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\n\nfrom pydicom import dcmread","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:35:48.504452Z","iopub.execute_input":"2022-08-24T13:35:48.504876Z","iopub.status.idle":"2022-08-24T13:35:50.756253Z","shell.execute_reply.started":"2022-08-24T13:35:48.504839Z","shell.execute_reply":"2022-08-24T13:35:50.755221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basedir = '../input/rsna-2022-cervical-spine-fracture-detection'\nimage_dir = os.path.join(basedir, 'train_images')\nmask_dir = os.path.join(basedir, 'segmentations')","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:35:50.757845Z","iopub.execute_input":"2022-08-24T13:35:50.758441Z","iopub.status.idle":"2022-08-24T13:35:50.763764Z","shell.execute_reply.started":"2022-08-24T13:35:50.758409Z","shell.execute_reply":"2022-08-24T13:35:50.7625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_nii(fname: str):\n    scan = nib.load(fname)\n    hu_img = scan.get_fdata()\n    return hu_img\n\ndef read_nii_nslices(fname: str):\n    scan = nib.load(fname)\n    return scan.header.get_data_shape()[-1]","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:35:50.765372Z","iopub.execute_input":"2022-08-24T13:35:50.765787Z","iopub.status.idle":"2022-08-24T13:35:50.777075Z","shell.execute_reply.started":"2022-08-24T13:35:50.765755Z","shell.execute_reply":"2022-08-24T13:35:50.775977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"volume_data = list()\nfor root, _, files in os.walk(image_dir):\n    if root == image_dir:\n        continue\n    volume_data.append({\n        'sid': os.path.basename(root),\n        'num_slices': len(files),\n    })\ndf_volume = pd.DataFrame(volume_data).sort_values('sid').set_index('sid')\ndf_volume","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:35:50.779055Z","iopub.execute_input":"2022-08-24T13:35:50.780022Z","iopub.status.idle":"2022-08-24T13:36:51.691344Z","shell.execute_reply.started":"2022-08-24T13:35:50.779985Z","shell.execute_reply":"2022-08-24T13:36:51.690151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(df_volume['num_slices'], bins=100)\nplt.xlabel('Number of Slices')\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:36:51.692977Z","iopub.execute_input":"2022-08-24T13:36:51.693581Z","iopub.status.idle":"2022-08-24T13:36:52.069151Z","shell.execute_reply.started":"2022-08-24T13:36:51.693512Z","shell.execute_reply":"2022-08-24T13:36:52.067925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nseries_info = dict()\nfor uid in tqdm(df_volume.index):\n    d = os.path.join(image_dir, uid)\n    idxs = [int(f.split('.')[0]) for f in os.listdir(d)]\n    first = min(idxs)\n    obj0 = dcmread(os.path.join(d, f'{first}.dcm'))\n    obj1 = dcmread(os.path.join(d, f'{first+1}.dcm'))\n    series_info[uid] = {\n        'rows': int(obj0.Rows),\n        'cols': int(obj0.Columns),\n        'pixel_spacing': [float(v) for v in obj0.PixelSpacing],\n        'slice_spacing': np.sqrt(np.sum(\n            (np.array(obj1.ImagePositionPatient) - np.array(obj0.ImagePositionPatient))**2\n        )),\n    }","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-08-24T13:36:52.070903Z","iopub.execute_input":"2022-08-24T13:36:52.071351Z","iopub.status.idle":"2022-08-24T13:37:42.94223Z","shell.execute_reply.started":"2022-08-24T13:36:52.071306Z","shell.execute_reply":"2022-08-24T13:37:42.940641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_volume['rows'] = [series_info[uid]['rows'] for uid in df_volume.index]\ndf_volume['cols'] = [series_info[uid]['cols'] for uid in df_volume.index]\ndf_volume['pixel_spacing_x'] = [series_info[uid]['pixel_spacing'][0] for uid in df_volume.index]\ndf_volume['pixel_spacing_y'] = [series_info[uid]['pixel_spacing'][1] for uid in df_volume.index]\ndf_volume['slice_spacing'] = [series_info[uid]['slice_spacing'] for uid in df_volume.index]\ndf_volume['pixel_spacing_same'] = df_volume['pixel_spacing_x'] == df_volume['pixel_spacing_y']\ndf_volume['height'] = df_volume['num_slices']*df_volume['slice_spacing']\ndf_volume['width'] = df_volume['rows']*df_volume['pixel_spacing_x']\ndf_volume","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:42.946026Z","iopub.execute_input":"2022-08-24T13:37:42.946457Z","iopub.status.idle":"2022-08-24T13:37:42.996832Z","shell.execute_reply.started":"2022-08-24T13:37:42.946416Z","shell.execute_reply":"2022-08-24T13:37:42.995672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_volume[~df_volume['pixel_spacing_same']]","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:42.998294Z","iopub.execute_input":"2022-08-24T13:37:42.999212Z","iopub.status.idle":"2022-08-24T13:37:43.020336Z","shell.execute_reply.started":"2022-08-24T13:37:42.999176Z","shell.execute_reply":"2022-08-24T13:37:43.019506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(df_volume['pixel_spacing_x'], df_volume['slice_spacing'])\nplt.xlabel('pixel spacing (mm)')\nplt.ylabel('slice spacing (mm)')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:43.022226Z","iopub.execute_input":"2022-08-24T13:37:43.023068Z","iopub.status.idle":"2022-08-24T13:37:43.252306Z","shell.execute_reply.started":"2022-08-24T13:37:43.023019Z","shell.execute_reply":"2022-08-24T13:37:43.251188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(df_volume['cols'], df_volume['rows'])\nplt.xlabel('# cols')\nplt.ylabel('# rows')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:43.25379Z","iopub.execute_input":"2022-08-24T13:37:43.254143Z","iopub.status.idle":"2022-08-24T13:37:43.465689Z","shell.execute_reply.started":"2022-08-24T13:37:43.25411Z","shell.execute_reply":"2022-08-24T13:37:43.464831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(df_volume['num_slices'], df_volume['slice_spacing'], marker='.')\nplt.xlabel('num_slices')\nplt.ylabel('slice_spacing')\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:43.4671Z","iopub.execute_input":"2022-08-24T13:37:43.468122Z","iopub.status.idle":"2022-08-24T13:37:43.66913Z","shell.execute_reply.started":"2022-08-24T13:37:43.468087Z","shell.execute_reply":"2022-08-24T13:37:43.668071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(df_volume['height'], bins=80)\nplt.xlabel('volume height (mm)')\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:43.670244Z","iopub.execute_input":"2022-08-24T13:37:43.670569Z","iopub.status.idle":"2022-08-24T13:37:44.235848Z","shell.execute_reply.started":"2022-08-24T13:37:43.670525Z","shell.execute_reply":"2022-08-24T13:37:44.234675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(df_volume['width'], bins=80)\nplt.xlabel('volume width (mm)')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:44.237615Z","iopub.execute_input":"2022-08-24T13:37:44.238428Z","iopub.status.idle":"2022-08-24T13:37:44.559341Z","shell.execute_reply.started":"2022-08-24T13:37:44.238381Z","shell.execute_reply":"2022-08-24T13:37:44.558223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(df_volume['width'], df_volume['height'], marker='.')\nplt.xlabel('volume width (mm)')\nplt.ylabel('volume height (mm)')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:44.560982Z","iopub.execute_input":"2022-08-24T13:37:44.561602Z","iopub.status.idle":"2022-08-24T13:37:44.785941Z","shell.execute_reply.started":"2022-08-24T13:37:44.561561Z","shell.execute_reply":"2022-08-24T13:37:44.783138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nseries_info = dict()\nfor uid in tqdm(df_volume.index):\n    st = time.time()\n    d = os.path.join(image_dir, uid)\n    vol = np.stack([\n        dcmread(os.path.join(d, f)).pixel_array\n        for f in os.listdir(d)\n    ])\n    et = time.time()\n    series_info[uid] = {\n        'read_array_cost': et-st,\n    }","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:37:44.789465Z","iopub.execute_input":"2022-08-24T13:37:44.790164Z","iopub.status.idle":"2022-08-24T13:38:38.372459Z","shell.execute_reply.started":"2022-08-24T13:37:44.790114Z","shell.execute_reply":"2022-08-24T13:38:38.371205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_volume['read_array_cost'] = [series_info[uid]['read_array_cost'] for uid in df_volume.index]\ndf_volume","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:38:38.374134Z","iopub.execute_input":"2022-08-24T13:38:38.374625Z","iopub.status.idle":"2022-08-24T13:38:38.407502Z","shell.execute_reply.started":"2022-08-24T13:38:38.374578Z","shell.execute_reply":"2022-08-24T13:38:38.405873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_volume.to_csv('df_volume.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-24T13:38:38.408822Z","iopub.status.idle":"2022-08-24T13:38:38.40959Z","shell.execute_reply.started":"2022-08-24T13:38:38.409342Z","shell.execute_reply":"2022-08-24T13:38:38.409365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}