{"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 download -q \"python-gdcm\" pydicom pylibjpeg \"opencv-python-headless\" --dest frozen_packages --prefer-binary\n!rm frozen_packages/torch-*\n!ls -lh frozen_packages","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:05:28.971253Z","iopub.execute_input":"2022-10-13T08:05:28.971755Z","iopub.status.idle":"2022-10-13T08:05:37.670485Z","shell.execute_reply.started":"2022-10-13T08:05:28.971644Z","shell.execute_reply":"2022-10-13T08:05:37.668984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qU \"python-gdcm\" pydicom pylibjpeg --find-links frozen_packages --no-index","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:05:37.673204Z","iopub.execute_input":"2022-10-13T08:05:37.673709Z","iopub.status.idle":"2022-10-13T08:05:50.385477Z","shell.execute_reply.started":"2022-10-13T08:05:37.673668Z","shell.execute_reply":"2022-10-13T08:05:50.384409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom tqdm import tqdm\nimport pandas as pd\n\nfrom glob import glob\n\nimport numpy as np\nimport pandas as pd\nimport nibabel as nib\n\nfrom pathlib import Path\n\nfrom sklearn.model_selection import GroupKFold\n","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:05:50.38741Z","iopub.execute_input":"2022-10-13T08:05:50.387838Z","iopub.status.idle":"2022-10-13T08:05:51.297676Z","shell.execute_reply.started":"2022-10-13T08:05:50.387799Z","shell.execute_reply":"2022-10-13T08:05:51.296377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:05:51.300465Z","iopub.execute_input":"2022-10-13T08:05:51.300856Z","iopub.status.idle":"2022-10-13T08:05:52.390554Z","shell.execute_reply.started":"2022-10-13T08:05:51.30082Z","shell.execute_reply":"2022-10-13T08:05:52.389036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/kagglersna2022code/train_vertebrae_kfold.csv\")\nprint(\"df shape is \", df.shape)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-13T08:05:52.393291Z","iopub.execute_input":"2022-10-13T08:05:52.393652Z","iopub.status.idle":"2022-10-13T08:05:52.461338Z","shell.execute_reply.started":"2022-10-13T08:05:52.393619Z","shell.execute_reply":"2022-10-13T08:05:52.459997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = Path(f\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/\")\n\noutput_dir = Path(f\"vertebrae\")\noutput_dir.mkdir(exist_ok=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:05:52.463341Z","iopub.execute_input":"2022-10-13T08:05:52.464116Z","iopub.status.idle":"2022-10-13T08:05:52.469621Z","shell.execute_reply.started":"2022-10-13T08:05:52.464074Z","shell.execute_reply":"2022-10-13T08:05:52.468486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndicoms = {}\n\nfor p in data_dir.glob(\"*\"):\n    assert p not in dicoms.keys()\n    dicoms[p.name] = list(sorted(list(p.glob(\"*.dcm\")), key=lambda x:int(x.stem)))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:05:52.470888Z","iopub.execute_input":"2022-10-13T08:05:52.471467Z","iopub.status.idle":"2022-10-13T08:08:02.40982Z","shell.execute_reply.started":"2022-10-13T08:05:52.471432Z","shell.execute_reply":"2022-10-13T08:08:02.408766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dicoms[\"1.2.826.0.1.3680043.10014\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:08:02.411417Z","iopub.execute_input":"2022-10-13T08:08:02.411892Z","iopub.status.idle":"2022-10-13T08:08:02.417567Z","shell.execute_reply.started":"2022-10-13T08:08:02.411848Z","shell.execute_reply":"2022-10-13T08:08:02.416315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(df.shape[0])):\n    row = df.iloc[i]\n    idx = row[\"StudyInstanceUID\"]\n    slice_ind = row[\"slice\"]\n\n    dicom = pydicom.dcmread(str(dicoms[idx][slice_ind]))\n    img = dicom.pixel_array\n\n    img = (img - img.min()) / (img.max() - img.min())\n    img = np.clip(img*255, 0., 255.).astype(np.uint8)\n\n    np.save(str(output_dir / f\"{idx}_{slice_ind}\"), img)\n\nprint(\"finished\")","metadata":{"execution":{"iopub.status.busy":"2022-10-13T08:08:02.419424Z","iopub.execute_input":"2022-10-13T08:08:02.42029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf frozen_packages","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -q -r data.zip ./vertebrae/*","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf vertebrae","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}