{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport cv2\nimport itertools\nimport numpy as np \nimport pandas as pd \nfrom matplotlib import pyplot as plt\n\nimport pydicom\nimport nibabel as nib\n\npd.set_option('display.max_columns', 100)\npd.set_option('display.max_rows', 100)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:56:01.808724Z","iopub.execute_input":"2023-09-05T04:56:01.809296Z","iopub.status.idle":"2023-09-05T04:56:02.999575Z","shell.execute_reply.started":"2023-09-05T04:56:01.809253Z","shell.execute_reply":"2023-09-05T04:56:02.998282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/'\ntrain_path = input_path+'train_png/'","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:56:03.921957Z","iopub.execute_input":"2023-09-05T04:56:03.92268Z","iopub.status.idle":"2023-09-05T04:56:03.930052Z","shell.execute_reply.started":"2023-09-05T04:56:03.922624Z","shell.execute_reply":"2023-09-05T04:56:03.928219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_files = os.listdir(input_path+'segmentations')\nseg_files = [tok[:-4] for tok in seg_files]\nlen(seg_files)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:56:04.798605Z","iopub.execute_input":"2023-09-05T04:56:04.799069Z","iopub.status.idle":"2023-09-05T04:56:04.855418Z","shell.execute_reply.started":"2023-09-05T04:56:04.79903Z","shell.execute_reply":"2023-09-05T04:56:04.853942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(input_path+'train.csv')\ntrain_se = pd.read_csv(input_path+'train_series_meta.csv')\nimage_label = pd.read_csv(input_path+'image_level_labels.csv')\ntrain_dicom_tag = pd.read_parquet(input_path+'train_dicom_tags.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:56:05.810932Z","iopub.execute_input":"2023-09-05T04:56:05.812085Z","iopub.status.idle":"2023-09-05T04:56:13.641276Z","shell.execute_reply.started":"2023-09-05T04:56:05.81203Z","shell.execute_reply":"2023-09-05T04:56:13.639956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dicom_tag['series_id'] = train_dicom_tag['path'].apply(lambda x: x.split('/')[-2:][0])\ntrain_dicom_tag['file_path'] = train_dicom_tag['path'].apply(lambda x: x.split('/')[-1:][0])","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:56:13.643439Z","iopub.execute_input":"2023-09-05T04:56:13.644211Z","iopub.status.idle":"2023-09-05T04:56:16.425768Z","shell.execute_reply.started":"2023-09-05T04:56:13.644174Z","shell.execute_reply":"2023-09-05T04:56:16.424321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# NIFTI orientation","metadata":{}},{"cell_type":"code","source":"def check_patient_position(nifti_file_path):\n    try:\n        # Load NIFTI\n        img = nib.load(nifti_file_path)\n        \n        # Retrieve patient location from metadata\n        affine = img.affine\n        orientation = nib.aff2axcodes(affine)\n        \n        # Determine patient orientation\n        if orientation == ('R', 'A', 'S'):\n            return \"The patient is in a supine position.\"\n        else:\n            return f\"The patient is not in a supine position. The orientation is: {orientation}\"\n    except Exception as e:\n        return str(e)\n\nresult_list = []\nfor p in seg_files:\n    result = check_patient_position(input_path+'segmentations/'+p+'.nii')\n    result_list.append(result)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:57:38.110425Z","iopub.execute_input":"2023-09-05T04:57:38.110921Z","iopub.status.idle":"2023-09-05T04:57:40.74922Z","shell.execute_reply.started":"2023-09-05T04:57:38.110887Z","shell.execute_reply":"2023-09-05T04:57:40.748261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(result_list)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T04:59:21.924132Z","iopub.execute_input":"2023-09-05T04:59:21.924551Z","iopub.status.idle":"2023-09-05T04:59:21.931517Z","shell.execute_reply.started":"2023-09-05T04:59:21.924521Z","shell.execute_reply":"2023-09-05T04:59:21.930637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The orientation is unified and faces left. So, rotate it 90 degrees to the right and use it.","metadata":{}},{"cell_type":"markdown","source":"# NIFTI and DICOM slice","metadata":{}},{"cell_type":"code","source":"def plot_nifti_dicom_slice(p):\n    fig = plt.figure(figsize=(18,4))\n    file_path = input_path+\"segmentations/\"+p+\".nii\"\n\n    nifti_img = nib.load(file_path)\n    image_data = nifti_img.get_fdata()\n\n    image_data = np.transpose(image_data, [2, 1, 0])\n    image_data = np.rot90(image_data, -1, (1,2))\n    image_data = image_data[::-1,:,:]\n    image_data = np.transpose(image_data, [2, 1, 0])\n    image_data = image_data[::1, ::1, ::1]\n\n    ax1 = fig.add_subplot(1, 3, 1)\n    # Choose a slice index (for example, the middle slice along the z-axis)\n    slice_index = image_data.shape[2] // 2\n    # Plot the selected slice\n    niiimg = image_data[:, :, slice_index]\n    sc = ax1.imshow(niiimg)\n    plt.colorbar(sc)\n\n    dcm_p = input_path+train_dicom_tag[train_dicom_tag['path'].str.contains(p)].sort_values('InstanceNumber').reset_index(drop=True).iloc[slice_index]['path']\n\n    ax2 = fig.add_subplot(1, 3, 2)\n    img = pydicom.dcmread(dcm_p).pixel_array\n    ax2.imshow(img, cmap=plt.cm.bone)\n    ax2.set_title(\"series ID : \" + p)\n\n    niiimg = niiimg.astype('float32')\n\n    img = img.astype('float32')\n    # Normalize img to a range of 0 to 1\n    min_val = img.min()\n    max_val = img.max()\n    img = (img - min_val) / (max_val - min_val)\n\n    ax3 = fig.add_subplot(1, 3, 3)\n    blend=cv2.addWeighted(img,0.7,niiimg,0.3,0)\n    ax3.imshow(blend)\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T05:00:43.962215Z","iopub.execute_input":"2023-09-05T05:00:43.962778Z","iopub.status.idle":"2023-09-05T05:00:43.979363Z","shell.execute_reply.started":"2023-09-05T05:00:43.962736Z","shell.execute_reply":"2023-09-05T05:00:43.978394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for p in seg_files[:10]:\n    plot_nifti_dicom_slice(p)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T05:00:44.859397Z","iopub.execute_input":"2023-09-05T05:00:44.859738Z","iopub.status.idle":"2023-09-05T05:01:18.786324Z","shell.execute_reply.started":"2023-09-05T05:00:44.859713Z","shell.execute_reply":"2023-09-05T05:01:18.785185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}