{"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":30715,"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\nimport os\nimport pydicom\nfrom pydicom.data import get_testdata_file\nimport pydicom as dicom","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:12.144676Z","iopub.execute_input":"2024-06-04T17:36:12.145127Z","iopub.status.idle":"2024-06-04T17:36:12.825567Z","shell.execute_reply.started":"2024-06-04T17:36:12.145094Z","shell.execute_reply":"2024-06-04T17:36:12.824230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge\n* train.csv\n* train_label_coordinates\n* train_series_descriptions","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:15.635325Z","iopub.execute_input":"2024-06-04T17:36:15.636562Z","iopub.status.idle":"2024-06-04T17:36:15.715534Z","shell.execute_reply.started":"2024-06-04T17:36:15.636504Z","shell.execute_reply":"2024-06-04T17:36:15.714451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['spinal_canal_stenosis_l1_l2'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:15.945900Z","iopub.execute_input":"2024-06-04T17:36:15.946304Z","iopub.status.idle":"2024-06-04T17:36:15.962044Z","shell.execute_reply.started":"2024-06-04T17:36:15.946278Z","shell.execute_reply":"2024-06-04T17:36:15.960794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:16.217920Z","iopub.execute_input":"2024-06-04T17:36:16.218430Z","iopub.status.idle":"2024-06-04T17:36:16.254006Z","shell.execute_reply.started":"2024-06-04T17:36:16.218394Z","shell.execute_reply":"2024-06-04T17:36:16.252813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:19.488053Z","iopub.execute_input":"2024-06-04T17:36:19.488482Z","iopub.status.idle":"2024-06-04T17:36:19.517872Z","shell.execute_reply.started":"2024-06-04T17:36:19.488449Z","shell.execute_reply":"2024-06-04T17:36:19.516625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates = pd.read_csv(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:21.982887Z","iopub.execute_input":"2024-06-04T17:36:21.983287Z","iopub.status.idle":"2024-06-04T17:36:22.117357Z","shell.execute_reply.started":"2024-06-04T17:36:21.983256Z","shell.execute_reply":"2024-06-04T17:36:22.116141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:22.249755Z","iopub.execute_input":"2024-06-04T17:36:22.250518Z","iopub.status.idle":"2024-06-04T17:36:22.274929Z","shell.execute_reply.started":"2024-06-04T17:36:22.250478Z","shell.execute_reply":"2024-06-04T17:36:22.273322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates.head(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:24.969378Z","iopub.execute_input":"2024-06-04T17:36:24.970423Z","iopub.status.idle":"2024-06-04T17:36:24.985991Z","shell.execute_reply.started":"2024-06-04T17:36:24.970384Z","shell.execute_reply":"2024-06-04T17:36:24.984788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates['condition'][0]","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:25.260904Z","iopub.execute_input":"2024-06-04T17:36:25.261267Z","iopub.status.idle":"2024-06-04T17:36:25.268937Z","shell.execute_reply.started":"2024-06-04T17:36:25.261241Z","shell.execute_reply":"2024-06-04T17:36:25.267776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_names = []\nfor idx, row in train_label_coordinates.iterrows():\n    merged_names.append([name.lower().replace(' ', '_') for name in [train_label_coordinates['condition'][idx], train_label_coordinates['level'][idx].strip(\"/\")]])","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:28.036188Z","iopub.execute_input":"2024-06-04T17:36:28.036570Z","iopub.status.idle":"2024-06-04T17:36:32.217466Z","shell.execute_reply.started":"2024-06-04T17:36:28.036541Z","shell.execute_reply":"2024-06-04T17:36:32.216219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged = []\nfor mer in merged_names:\n    merged.append('_'.join(mer).replace('/', '_'))","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:36:32.219440Z","iopub.execute_input":"2024-06-04T17:36:32.219793Z","iopub.status.idle":"2024-06-04T17:36:32.262633Z","shell.execute_reply.started":"2024-06-04T17:36:32.219765Z","shell.execute_reply":"2024-06-04T17:36:32.261368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates['merged_names'] = merged","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:38:30.003714Z","iopub.execute_input":"2024-06-04T17:38:30.004136Z","iopub.status.idle":"2024-06-04T17:38:30.014218Z","shell.execute_reply.started":"2024-06-04T17:38:30.004103Z","shell.execute_reply":"2024-06-04T17:38:30.012760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:38:31.796187Z","iopub.execute_input":"2024-06-04T17:38:31.796571Z","iopub.status.idle":"2024-06-04T17:38:31.814076Z","shell.execute_reply.started":"2024-06-04T17:38:31.796542Z","shell.execute_reply":"2024-06-04T17:38:31.812764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_label_coordinates['merged_names'][1974]][1974]","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:38:32.013885Z","iopub.execute_input":"2024-06-04T17:38:32.014826Z","iopub.status.idle":"2024-06-04T17:38:32.022245Z","shell.execute_reply.started":"2024-06-04T17:38:32.014781Z","shell.execute_reply":"2024-06-04T17:38:32.021027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Normal_Mild = []\nModerate = []\nSevere = []\nfor ind, rows in train_label_coordinates.iterrows():\n    newdf = train_df[train_df['study_id'] == train_label_coordinates['study_id'][ind]]\n    value = newdf[train_label_coordinates['merged_names'][ind]].values[0]\n    \n    if value == 'Normal/Mild':\n        Normal_Mild.append(1)\n        Moderate.append(0)\n        Severe.append(0)\n    elif value == 'Moderate':\n        Normal_Mild.append(0)\n        Moderate.append(1)\n        Severe.append(0)\n    else:\n        Normal_Mild.append(0)\n        Moderate.append(0)\n        Severe.append(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:09.799173Z","iopub.execute_input":"2024-06-04T17:40:09.799603Z","iopub.status.idle":"2024-06-04T17:40:34.222360Z","shell.execute_reply.started":"2024-06-04T17:40:09.799571Z","shell.execute_reply":"2024-06-04T17:40:34.221236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates['Normal/Mild'] = Normal_Mild\ntrain_label_coordinates['Moderate'] = Moderate\ntrain_label_coordinates['Severe'] = Severe","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:38.948004Z","iopub.execute_input":"2024-06-04T17:40:38.948400Z","iopub.status.idle":"2024-06-04T17:40:39.032376Z","shell.execute_reply.started":"2024-06-04T17:40:38.948369Z","shell.execute_reply":"2024-06-04T17:40:39.031251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:43.184034Z","iopub.execute_input":"2024-06-04T17:40:43.184401Z","iopub.status.idle":"2024-06-04T17:40:43.204178Z","shell.execute_reply.started":"2024-06-04T17:40:43.184373Z","shell.execute_reply":"2024-06-04T17:40:43.202788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_descriptions = pd.read_csv(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:49.379113Z","iopub.execute_input":"2024-06-04T17:40:49.379521Z","iopub.status.idle":"2024-06-04T17:40:49.406124Z","shell.execute_reply.started":"2024-06-04T17:40:49.379486Z","shell.execute_reply":"2024-06-04T17:40:49.404938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_descriptions.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:49.694304Z","iopub.execute_input":"2024-06-04T17:40:49.694714Z","iopub.status.idle":"2024-06-04T17:40:49.708024Z","shell.execute_reply.started":"2024-06-04T17:40:49.694680Z","shell.execute_reply":"2024-06-04T17:40:49.706621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_descriptions.head(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:52.697001Z","iopub.execute_input":"2024-06-04T17:40:52.697394Z","iopub.status.idle":"2024-06-04T17:40:52.709156Z","shell.execute_reply.started":"2024-06-04T17:40:52.697362Z","shell.execute_reply":"2024-06-04T17:40:52.707810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Now merge the description to the original dataframe (train_label_coordinates).\n* That will be done by merging accordin to \"study_id\" and the \"series_id\" columns. ","metadata":{}},{"cell_type":"code","source":"final_df = merged_df = pd.merge(train_label_coordinates, train_series_descriptions[['study_id', 'series_id', 'series_description']], on=['study_id', 'series_id'], how='left')","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:40:55.808681Z","iopub.execute_input":"2024-06-04T17:40:55.809115Z","iopub.status.idle":"2024-06-04T17:40:55.845055Z","shell.execute_reply.started":"2024-06-04T17:40:55.809083Z","shell.execute_reply":"2024-06-04T17:40:55.844007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:41:15.198208Z","iopub.execute_input":"2024-06-04T17:41:15.198596Z","iopub.status.idle":"2024-06-04T17:41:15.238441Z","shell.execute_reply.started":"2024-06-04T17:41:15.198566Z","shell.execute_reply":"2024-06-04T17:41:15.237373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv_file_path = 'train_merged.csv'\nfinal_df.to_csv(csv_file_path, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:42:41.197428Z","iopub.execute_input":"2024-06-04T17:42:41.197873Z","iopub.status.idle":"2024-06-04T17:42:41.925210Z","shell.execute_reply.started":"2024-06-04T17:42:41.197839Z","shell.execute_reply":"2024-06-04T17:42:41.924057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The number of Normal/Mild samples is: {final_df['Normal/Mild'].sum()}\")","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:49:46.751426Z","iopub.execute_input":"2024-06-04T17:49:46.751971Z","iopub.status.idle":"2024-06-04T17:49:46.758305Z","shell.execute_reply.started":"2024-06-04T17:49:46.751932Z","shell.execute_reply":"2024-06-04T17:49:46.757059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The number of Moderate samples is: {final_df['Moderate'].sum()}\")","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:49:47.866152Z","iopub.execute_input":"2024-06-04T17:49:47.867204Z","iopub.status.idle":"2024-06-04T17:49:47.873331Z","shell.execute_reply.started":"2024-06-04T17:49:47.867155Z","shell.execute_reply":"2024-06-04T17:49:47.871989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The number of Severe samples is: {final_df['Severe'].sum()}\")","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:49:48.645881Z","iopub.execute_input":"2024-06-04T17:49:48.646291Z","iopub.status.idle":"2024-06-04T17:49:48.652948Z","shell.execute_reply.started":"2024-06-04T17:49:48.646261Z","shell.execute_reply":"2024-06-04T17:49:48.651789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# distribution of planes\nplt.hist(final_df['series_description'])","metadata":{"execution":{"iopub.status.busy":"2024-06-04T17:50:55.358696Z","iopub.execute_input":"2024-06-04T17:50:55.359279Z","iopub.status.idle":"2024-06-04T17:50:55.998906Z","shell.execute_reply.started":"2024-06-04T17:50:55.359238Z","shell.execute_reply":"2024-06-04T17:50:55.997808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}