{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nROOT = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-22T02:38:51.437685Z","iopub.execute_input":"2024-06-22T02:38:51.438523Z","iopub.status.idle":"2024-06-22T02:38:51.444066Z","shell.execute_reply.started":"2024-06-22T02:38:51.438480Z","shell.execute_reply":"2024-06-22T02:38:51.442697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport matplotlib.pyplot as plt\nimport re","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:38:51.736202Z","iopub.execute_input":"2024-06-22T02:38:51.736584Z","iopub.status.idle":"2024-06-22T02:38:51.742675Z","shell.execute_reply.started":"2024-06-22T02:38:51.736554Z","shell.execute_reply":"2024-06-22T02:38:51.741239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### I was trying to make use of the coordinate to train the model, but i later found the coordinate is not provided in the test set.  \n### Then i make need to train a machine learning model to have a coordinate of different levels.\n### Then i ask the question, are those right and left condition has different orientation.","metadata":{}},{"cell_type":"code","source":"def extract_number(filename):\n    # Use regular expression to find the numeric part\n    match = re.search(r'(\\d+)', filename)\n    return int(match.group(1)) if match else 0","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:38:52.049358Z","iopub.execute_input":"2024-06-22T02:38:52.049786Z","iopub.status.idle":"2024-06-22T02:38:52.056095Z","shell.execute_reply.started":"2024-06-22T02:38:52.049748Z","shell.execute_reply":"2024-06-22T02:38:52.054811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc = pd.read_csv(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv\")\ndesc.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:38:54.368255Z","iopub.execute_input":"2024-06-22T02:38:54.368681Z","iopub.status.idle":"2024-06-22T02:38:54.454907Z","shell.execute_reply.started":"2024-06-22T02:38:54.368647Z","shell.execute_reply":"2024-06-22T02:38:54.453588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_grouped_image_path(df, study_id = None):\n    path_dict = {}\n    if study_id == None:\n        study_id = df.sample(n=1).to_dict(orient='records')[0]['study_id']\n    df = df[df['study_id']==study_id]\n    conditions = df['condition'].unique()\n    for cond in conditions:\n        path_dict[cond] = []\n        sub_df = df[df['condition']==cond]\n        for idx, row in sub_df.iterrows():\n            path_dict[cond].append(f\"{study_id}/{row['series_id']}/{row['instance_number']}.dcm\")\n    return path_dict","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:38:58.895267Z","iopub.execute_input":"2024-06-22T02:38:58.895652Z","iopub.status.idle":"2024-06-22T02:38:58.903174Z","shell.execute_reply.started":"2024-06-22T02:38:58.895621Z","shell.execute_reply":"2024-06-22T02:38:58.901749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting a random path dict\npath_dict = get_grouped_image_path(desc)\npath_dict","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:38:59.358186Z","iopub.execute_input":"2024-06-22T02:38:59.358582Z","iopub.status.idle":"2024-06-22T02:38:59.377355Z","shell.execute_reply.started":"2024-06-22T02:38:59.358550Z","shell.execute_reply":"2024-06-22T02:38:59.376223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_dicom_case(path_dict, folder_path):\n    for cond, path_list in path_dict.items():\n        path_list = sorted(path_list, key=extract_number)\n        \n        # Determine the grid size\n        nrows = 2\n        ncols = 3\n        fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(15, 10))\n        fig.suptitle(cond, fontsize=16)\n        \n        for idx, path in enumerate(path_list):\n            if idx >= nrows * ncols:\n                break\n\n            # Construct the full file path\n            file_path = os.path.join(folder_path, path)\n\n            # Read the DICOM file\n            ds = pydicom.dcmread(file_path)\n\n            # Get the pixel data\n            pixel_array = ds.pixel_array\n\n            # Determine the position in the grid\n            ax = axes[idx // ncols, idx % ncols]\n            ax.imshow(pixel_array, cmap=plt.cm.gray)\n            ax.set_title(f'File: {os.path.basename(path)}')\n            ax.axis('off')  # Hide the axis\n\n        # Hide any remaining empty subplots\n        for idx in range(len(path_list), nrows * ncols):\n            fig.delaxes(axes.flatten()[idx])\n        \n        plt.tight_layout()\n        plt.subplots_adjust(top=0.9)  # Adjust to make room for the suptitle\n        plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:39:02.498485Z","iopub.execute_input":"2024-06-22T02:39:02.498919Z","iopub.status.idle":"2024-06-22T02:39:02.508774Z","shell.execute_reply.started":"2024-06-22T02:39:02.498885Z","shell.execute_reply":"2024-06-22T02:39:02.507408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_dicom_case(path_dict, ROOT+\"train_images\")","metadata":{"execution":{"iopub.status.busy":"2024-06-22T02:39:33.206653Z","iopub.execute_input":"2024-06-22T02:39:33.207658Z","iopub.status.idle":"2024-06-22T02:39:38.702970Z","shell.execute_reply.started":"2024-06-22T02:39:33.207622Z","shell.execute_reply":"2024-06-22T02:39:38.701680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## As we can see, the left neural narrowing and right nerual narrowing are in the same orientation.","metadata":{}}]}