{"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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# RSNA 2024 Lumbar Spine Degenerative Classification","metadata":{}},{"cell_type":"markdown","source":"## 1. Load Data","metadata":{}},{"cell_type":"markdown","source":"### Import Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport pydicom\nimport matplotlib.patches as patches\n\nfrom matplotlib import animation, rc\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-24T08:05:44.077860Z","iopub.execute_input":"2024-07-24T08:05:44.078565Z","iopub.status.idle":"2024-07-24T08:05:44.086571Z","shell.execute_reply.started":"2024-07-24T08:05:44.078521Z","shell.execute_reply":"2024-07-24T08:05:44.084789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import Path & Setting Variables","metadata":{}},{"cell_type":"code","source":"path = '../input/rsna-2024-lumbar-spine-degenerative-classification/'\n\ndf_sub         = pd.read_csv(path + 'sample_submission.csv')\ndf_train  = pd.read_csv(path + 'train.csv')\ndf_train_label = pd.read_csv(path + 'train_label_coordinates.csv')\ndf_train_series = pd.read_csv(path + 'train_series_descriptions.csv')\ndf_test_series   = pd.read_csv(path + 'test_series_descriptions.csv')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:05:47.010000Z","iopub.execute_input":"2024-07-24T08:05:47.010495Z","iopub.status.idle":"2024-07-24T08:05:47.119471Z","shell.execute_reply.started":"2024-07-24T08:05:47.010464Z","shell.execute_reply":"2024-07-24T08:05:47.118199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Shape (Row, Col)","metadata":{}},{"cell_type":"code","source":"print(df_sub.shape)\nprint(df_train.shape)\nprint(df_train_label.shape)\nprint(df_train_series.shape)\nprint(df_test_series.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:05:49.227152Z","iopub.execute_input":"2024-07-24T08:05:49.227566Z","iopub.status.idle":"2024-07-24T08:05:49.234168Z","shell.execute_reply.started":"2024-07-24T08:05:49.227535Z","shell.execute_reply":"2024-07-24T08:05:49.233101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Gathering Information","metadata":{}},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:05:52.061221Z","iopub.execute_input":"2024-07-24T08:05:52.061669Z","iopub.status.idle":"2024-07-24T08:05:52.081957Z","shell.execute_reply.started":"2024-07-24T08:05:52.061635Z","shell.execute_reply":"2024-07-24T08:05:52.080581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:05:55.429193Z","iopub.execute_input":"2024-07-24T08:05:55.429639Z","iopub.status.idle":"2024-07-24T08:05:55.454125Z","shell.execute_reply.started":"2024-07-24T08:05:55.429593Z","shell.execute_reply":"2024-07-24T08:05:55.452842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_label.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:05:57.876767Z","iopub.execute_input":"2024-07-24T08:05:57.877183Z","iopub.status.idle":"2024-07-24T08:05:57.892529Z","shell.execute_reply.started":"2024-07-24T08:05:57.877151Z","shell.execute_reply":"2024-07-24T08:05:57.891264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Preprocessing","metadata":{}},{"cell_type":"code","source":"def reshape_row(row):\n    data = {'study_id': [], 'condition': [], 'level': [], 'severity': []}\n    \n    for column, value in row.items():\n        if column not in ['study_id', 'series_id', 'instance_number', 'x', 'y', 'series_description']:\n            parts = column.split('_')\n            condition = ' '.join([word.capitalize() for word in parts[:-2]])\n            level = parts[-2].capitalize() + '/' + parts[-1].capitalize()\n            data['study_id'].append(row['study_id'])\n            data['condition'].append(condition)\n            data['level'].append(level)\n            data['severity'].append(value)\n    \n    return pd.DataFrame(data)\n\n# Reshape the DataFrame for all rows\ndf_new_train = pd.concat([reshape_row(row) for _, row in df_train.iterrows()], ignore_index=True)\n\n# Display the first few rows of the reshaped dataframe\ndf_new_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:41:28.997263Z","iopub.execute_input":"2024-07-24T08:41:28.997693Z","iopub.status.idle":"2024-07-24T08:41:30.948927Z","shell.execute_reply.started":"2024-07-24T08:41:28.997661Z","shell.execute_reply":"2024-07-24T08:41:30.947798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge = pd.merge(df_new_train, df_train_label, on=['study_id', 'condition', 'level'], how='inner')\ndf_trained = pd.merge(df_merge, df_train_series, on=['series_id','study_id'], how='inner')\ndf_trained.head(5)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:41:39.740150Z","iopub.execute_input":"2024-07-24T08:41:39.740543Z","iopub.status.idle":"2024-07-24T08:41:39.842667Z","shell.execute_reply.started":"2024-07-24T08:41:39.740514Z","shell.execute_reply":"2024-07-24T08:41:39.841224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spinal_check = df_trained[df_trained['condition'] == 'Spinal Canal Stenosis']['series_description'].eq('Sagittal T2/STIR').value_counts()\nleft_neural_check = df_trained[df_trained['condition'] == 'Left Neural Foraminal Narrowing']['series_description'].eq('Sagittal T1').all()\nright_neural_check = df_trained[df_trained['condition'] == 'Right Neural Foraminal Narrowing']['series_description'].eq('Sagittal T1').all()\nleft_subarticular_check = df_trained[df_trained['condition'] == 'Left Subarticular Stenosis']['series_description'].eq('Axial T2').all()\nright_subarticular_check = df_trained[df_trained['condition'] == 'Right Subarticular Stenosis']['series_description'].eq('Axial T2').all()\n\nspinal_check, left_neural_check, right_neural_check, left_subarticular_check,right_subarticular_check","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:39:53.830369Z","iopub.execute_input":"2024-07-24T08:39:53.831234Z","iopub.status.idle":"2024-07-24T08:39:53.920875Z","shell.execute_reply.started":"2024-07-24T08:39:53.831194Z","shell.execute_reply":"2024-07-24T08:39:53.919682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trained[df_trained['study_id'] == 100206310].sort_values(['x','y'],ascending = True)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:40:49.930285Z","iopub.execute_input":"2024-07-24T08:40:49.930731Z","iopub.status.idle":"2024-07-24T08:40:49.956763Z","shell.execute_reply.started":"2024-07-24T08:40:49.930697Z","shell.execute_reply":"2024-07-24T08:40:49.955685Z"},"trusted":true},"execution_count":null,"outputs":[]}]}