{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"},{"sourceId":9127652,"sourceType":"datasetVersion","datasetId":5494026}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm.auto import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:13:01.124024Z","iopub.execute_input":"2024-09-12T20:13:01.124514Z","iopub.status.idle":"2024-09-12T20:13:01.776223Z","shell.execute_reply.started":"2024-09-12T20:13:01.124464Z","shell.execute_reply":"2024-09-12T20:13:01.774792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df = pd.read_parquet('/kaggle/input/rsna-newmeta/meta.parquet')\ncoor_df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:13:04.218683Z","iopub.execute_input":"2024-09-12T20:13:04.219254Z","iopub.status.idle":"2024-09-12T20:13:04.766102Z","shell.execute_reply.started":"2024-09-12T20:13:04.219208Z","shell.execute_reply":"2024-09-12T20:13:04.764641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coor_df","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:13:06.308594Z","iopub.execute_input":"2024-09-12T20:13:06.309068Z","iopub.status.idle":"2024-09-12T20:13:06.344780Z","shell.execute_reply.started":"2024-09-12T20:13:06.309023Z","shell.execute_reply":"2024-09-12T20:13:06.343592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\nscs_coor = coor_df.loc[coor_df.condition=='Spinal Canal Stenosis']\nleft_nfn_coor = coor_df.loc[coor_df.condition=='Left Neural Foraminal Narrowing']\nright_nfn_coor = coor_df.loc[coor_df.condition=='Right Neural Foraminal Narrowing']\nmerged_nfn_coor = pd.merge(left_nfn_coor, right_nfn_coor, on=['study_id', 'level'], how='inner')\nmerged_nfn_coor['x'] = (merged_nfn_coor.x_x + merged_nfn_coor.x_y)/2\nmerged_nfn_coor['y'] = (merged_nfn_coor.y_x + merged_nfn_coor.y_y)/2\n\nscs_to_nfn_coor_dict = {'study_id': [], 'series_id': [], 'instance_number': [], \n                        'condition': [], 'level': [], 'x': [], 'y': []}\nfor _, row in tqdm(scs_coor.iterrows(), total=len(scs_coor)): \n    try: \n        study, series, ins, level = row.study_id, row.series_id, row.instance_number, row.level\n        meta_row = meta_df.loc[(meta_df.study_id==study) & (meta_df.series_id==series) & (meta_df.instance_number==ins)].squeeze(axis=0)\n        #print(meta_row)\n        ipp_x = meta_row.ipp_x\n        sagt1_meta = meta_df.loc[(meta_df.study_id==study) & (meta_df.series_description=='Sagittal T1')]\n        sagt1_meta['diff_ipp_x'] = (sagt1_meta.ipp_x - ipp_x).abs()\n        sagt1_row = sagt1_meta.iloc[sagt1_meta.diff_ipp_x.argmin()].squeeze(axis=0)\n        sagt1_study_id, sagt1_series_id, sagt1_ins = sagt1_row.study_id, sagt1_row.series_id, sagt1_row.instance_number\n        merged_nfn_coor_row = merged_nfn_coor.loc[(merged_nfn_coor.study_id==sagt1_study_id) & (merged_nfn_coor.level==level)].squeeze(axis=0)\n        sagt1_x, sagt1_y = merged_nfn_coor_row.x, merged_nfn_coor_row.y\n        scs_to_nfn_coor_dict['study_id'].append(sagt1_study_id)\n        scs_to_nfn_coor_dict['series_id'].append(sagt1_series_id)\n        scs_to_nfn_coor_dict['instance_number'].append(sagt1_ins)\n        scs_to_nfn_coor_dict['condition'].append('LR Neural Foraminal Narrowing')\n        scs_to_nfn_coor_dict['level'].append(level)\n        scs_to_nfn_coor_dict['x'].append(sagt1_x)\n        scs_to_nfn_coor_dict['y'].append(sagt1_y)\n    except: \n        pass","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:13:09.195495Z","iopub.execute_input":"2024-09-12T20:13:09.195980Z","iopub.status.idle":"2024-09-12T20:18:53.953273Z","shell.execute_reply.started":"2024-09-12T20:13:09.195935Z","shell.execute_reply":"2024-09-12T20:18:53.951916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nfn_coor_df = pd.DataFrame(scs_to_nfn_coor_dict)","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:18:54.228336Z","iopub.execute_input":"2024-09-12T20:18:54.228802Z","iopub.status.idle":"2024-09-12T20:18:54.362360Z","shell.execute_reply.started":"2024-09-12T20:18:54.228756Z","shell.execute_reply":"2024-09-12T20:18:54.361169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#coor_df_wo_nfn = coor_df.loc[(coor_df.condition != 'Left Neural Foraminal Narrowing') | (coor_df.condition != 'Right Neural Foraminal Narrowing')]\nexpanded_train_label_coordinates = pd.concat([coor_df, nfn_coor_df])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:19:35.295333Z","iopub.execute_input":"2024-09-12T20:19:35.296171Z","iopub.status.idle":"2024-09-12T20:19:35.317696Z","shell.execute_reply.started":"2024-09-12T20:19:35.296094Z","shell.execute_reply":"2024-09-12T20:19:35.316280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"expanded_train_label_coordinates = expanded_train_label_coordinates.loc[(~expanded_train_label_coordinates.x.isna()) & (~expanded_train_label_coordinates.y.isna())]","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:20:51.728970Z","iopub.execute_input":"2024-09-12T20:20:51.729437Z","iopub.status.idle":"2024-09-12T20:20:51.748246Z","shell.execute_reply.started":"2024-09-12T20:20:51.729393Z","shell.execute_reply":"2024-09-12T20:20:51.746684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"expanded_train_label_coordinates.to_csv('expanded_train_label_coordinates.csv', index=False)\nexpanded_train_label_coordinates","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:21:25.878502Z","iopub.execute_input":"2024-09-12T20:21:25.878982Z","iopub.status.idle":"2024-09-12T20:21:26.414066Z","shell.execute_reply.started":"2024-09-12T20:21:25.878939Z","shell.execute_reply":"2024-09-12T20:21:26.412688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"expanded_train_label_coordinates.loc[expanded_train_label_coordinates.y.isna()]","metadata":{"execution":{"iopub.status.busy":"2024-09-12T20:21:19.778526Z","iopub.execute_input":"2024-09-12T20:21:19.778997Z","iopub.status.idle":"2024-09-12T20:21:19.793641Z","shell.execute_reply.started":"2024-09-12T20:21:19.778952Z","shell.execute_reply":"2024-09-12T20:21:19.792438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}