{"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":"import pandas as pd\nimport numpy as np\nimport seaborn as sb\nimport matplotlib.pyplot as plt\nimport nibabel as nib\n\nimport os\nimport pydicom\nfrom glob import glob\nfrom tqdm import tqdm, trange","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-17T15:54:54.025452Z","iopub.execute_input":"2023-08-17T15:54:54.025946Z","iopub.status.idle":"2023-08-17T15:54:55.133473Z","shell.execute_reply.started":"2023-08-17T15:54:54.025906Z","shell.execute_reply":"2023-08-17T15:54:55.132411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" This notebook converts DICOM CT image into 3D numpy image.","metadata":{}},{"cell_type":"code","source":"PATH = '/kaggle/input/rsna-2023-abdominal-trauma-detection'\ntrain = pd.read_csv(f'{PATH}/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:54:55.135624Z","iopub.execute_input":"2023-08-17T15:54:55.136186Z","iopub.status.idle":"2023-08-17T15:54:55.186186Z","shell.execute_reply.started":"2023-08-17T15:54:55.136141Z","shell.execute_reply":"2023-08-17T15:54:55.185121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta = pd.read_csv(f'{PATH}/train_series_meta.csv')\ntrain_series_meta.head()           ","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:54:56.407767Z","iopub.execute_input":"2023-08-17T15:54:56.408187Z","iopub.status.idle":"2023-08-17T15:54:56.432573Z","shell.execute_reply.started":"2023-08-17T15:54:56.408152Z","shell.execute_reply":"2023-08-17T15:54:56.431511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(train_series_meta, train,\n              how='inner', on='patient_id')","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:55:03.122818Z","iopub.execute_input":"2023-08-17T15:55:03.12331Z","iopub.status.idle":"2023-08-17T15:55:03.14957Z","shell.execute_reply.started":"2023-08-17T15:55:03.123271Z","shell.execute_reply":"2023-08-17T15:55:03.148671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# making dataset","metadata":{}},{"cell_type":"code","source":"\"\"\"\nif os.path.isdir('/kaggle/working/rsna/') == False:\n    os.mkdir('/kaggle/working/rsna/')\n\nfor idx in range(len(df)):\n    temp = df.iloc[idx].astype('int')\n    img_paths = glob(f'{PATH}/train_images/{temp.patient_id}/{temp.series_id}/*.dcm')\n    ct_scan_all = []\n    for i in range(len(img_paths)):\n        dicom_file = pydicom.dcmread(img_paths[i])\n        ct_scan = dicom_file.pixel_array\n\n        # Bringing the image in scale of 0 to 255.\n        ct_scan = np.interp(ct_scan, [np.min(ct_scan), np.max(ct_scan)], [0,255])\n        ct_scan = ct_scan.tolist()\n        ct_scan_all.append(ct_scan)\n    ct_scan_all = np.array(ct_scan_all)\n    ct_scan_all = ct_scan_all.transpose(1, 2, 0)\n\n    #making dataset code\n    \n    if os.path.isdir('/kaggle/working/rsna/{0}'.format(temp.patient_id)) == False:\n        os.mkdir('/kaggle/working/rsna/{0}'.format(temp.patient_id))\n    np.save('/kaggle/working/rsna/{0}/{1}'.format(temp.patient_id, temp.series_id),ct_scan_all)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:55:31.285889Z","iopub.execute_input":"2023-08-17T15:55:31.286307Z","iopub.status.idle":"2023-08-17T15:57:03.739041Z","shell.execute_reply.started":"2023-08-17T15:55:31.286277Z","shell.execute_reply":"2023-08-17T15:57:03.737972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# making 3D numpy","metadata":{}},{"cell_type":"code","source":"idx = 0 #specifing a variable\n\ntemp = df.iloc[idx].astype('int')\nimg_paths = glob(f'{PATH}/train_images/{temp.patient_id}/{temp.series_id}/*.dcm')\nct_scan_all = []\nfor i in range(len(img_paths)):\n    dicom_file = pydicom.dcmread(img_paths[i])\n    ct_scan = dicom_file.pixel_array\n\n    # Bringing the image in scale of 0 to 255.\n    ct_scan = np.interp(ct_scan, [np.min(ct_scan), np.max(ct_scan)], [0,255])\n    ct_scan = ct_scan.tolist()\n    ct_scan_all.append(ct_scan)\nct_scan_all = np.array(ct_scan_all)\nct_scan_all = ct_scan_all.transpose(1, 2, 0)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:57:59.556302Z","iopub.execute_input":"2023-08-17T15:57:59.556685Z","iopub.status.idle":"2023-08-17T15:59:18.252098Z","shell.execute_reply.started":"2023-08-17T15:57:59.556656Z","shell.execute_reply":"2023-08-17T15:59:18.250865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ct_scan_all.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:59:18.254307Z","iopub.execute_input":"2023-08-17T15:59:18.255096Z","iopub.status.idle":"2023-08-17T15:59:18.262607Z","shell.execute_reply.started":"2023-08-17T15:59:18.25506Z","shell.execute_reply":"2023-08-17T15:59:18.261522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z = 250 #slice number\nplt.imshow(ct_scan_all[:,:,z], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-08-17T15:59:18.264157Z","iopub.execute_input":"2023-08-17T15:59:18.264589Z","iopub.status.idle":"2023-08-17T15:59:18.61847Z","shell.execute_reply.started":"2023-08-17T15:59:18.264556Z","shell.execute_reply":"2023-08-17T15:59:18.617489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}