{"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":"gpu","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-30T18:00:23.098569Z","iopub.execute_input":"2024-09-30T18:00:23.099695Z","iopub.status.idle":"2024-09-30T18:00:23.105010Z","shell.execute_reply.started":"2024-09-30T18:00:23.099649Z","shell.execute_reply":"2024-09-30T18:00:23.104067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nimport matplotlib.pyplot as plt\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:23.106933Z","iopub.execute_input":"2024-09-30T18:00:23.107416Z","iopub.status.idle":"2024-09-30T18:00:36.569364Z","shell.execute_reply.started":"2024-09-30T18:00:23.107371Z","shell.execute_reply":"2024-09-30T18:00:36.568489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification'\nurl_train = base_dir + '/train.csv'\nurl_coor = base_dir + '/train_label_coordinates.csv'\nurl_desc = base_dir + '/train_series_descriptions.csv'\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.570546Z","iopub.execute_input":"2024-09-30T18:00:36.571097Z","iopub.status.idle":"2024-09-30T18:00:36.575874Z","shell.execute_reply.started":"2024-09-30T18:00:36.571056Z","shell.execute_reply":"2024-09-30T18:00:36.574925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"url = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv'\ndf = pl.read_csv(url_train)\ndf_coor = pl.read_csv(url_coor)\ndf_desc = pl.read_csv(url_desc)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.577957Z","iopub.execute_input":"2024-09-30T18:00:36.578272Z","iopub.status.idle":"2024-09-30T18:00:36.794140Z","shell.execute_reply.started":"2024-09-30T18:00:36.578240Z","shell.execute_reply":"2024-09-30T18:00:36.793293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.795542Z","iopub.execute_input":"2024-09-30T18:00:36.796403Z","iopub.status.idle":"2024-09-30T18:00:36.818204Z","shell.execute_reply.started":"2024-09-30T18:00:36.796355Z","shell.execute_reply":"2024-09-30T18:00:36.817119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_columns = df.columns[1:]\ndf_columns","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.819489Z","iopub.execute_input":"2024-09-30T18:00:36.819791Z","iopub.status.idle":"2024-09-30T18:00:36.827526Z","shell.execute_reply.started":"2024-09-30T18:00:36.819760Z","shell.execute_reply":"2024-09-30T18:00:36.826723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_melted = df.melt(\n    id_vars=[\"study_id\"],  \n    value_vars=df_columns,\n    variable_name=\"level\",    \n    value_name=\"severity\"     \n)\ndf_melted_2 = df_melted.with_columns(\n    pl.concat_str(pl.col(\"study_id\"), pl.col(\"level\"), separator=\"_\").alias(\"row_id\")\n)\n\ndf_melted_2.filter(\n    pl.col(\"study_id\") == 4003253\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.828678Z","iopub.execute_input":"2024-09-30T18:00:36.829056Z","iopub.status.idle":"2024-09-30T18:00:36.910935Z","shell.execute_reply.started":"2024-09-30T18:00:36.829010Z","shell.execute_reply":"2024-09-30T18:00:36.909904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\ntext = df_columns\npattern = r'(_l\\d+_l\\d)|(_l\\d+_s\\d+)'\n\nfor t in text:\n    cleaned_text = re.sub(pattern, '', t)\n    print(cleaned_text)\n    \n    \npattern_2 =  r'(l\\d+_l\\d)|(l\\d+_s\\d)'\n\nfor t in text:\n    cleaned_text = re.search(pattern_2, t)\n    cleaned_text = cleaned_text[0].upper()\n    cleaned_text = cleaned_text.replace(\"_\",\"/\")\n    print(cleaned_text)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.912399Z","iopub.execute_input":"2024-09-30T18:00:36.913119Z","iopub.status.idle":"2024-09-30T18:00:36.921325Z","shell.execute_reply.started":"2024-09-30T18:00:36.913072Z","shell.execute_reply":"2024-09-30T18:00:36.920395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pattern_2 =  r'(l\\d+_(l|s)\\d)'\n\ndf_melted_proc = df_melted_2.with_columns(\n    pl.col(\"level\").str.replace(pattern, \"\").alias(\"condition\"),\n    pl.col(\"level\").str.extract(pattern_2).str.to_uppercase().str.replace(\"_\",\"/\").alias(\"level\")\n) \ndf_melted_proc.filter(\n    (pl.col(\"study_id\") == 4003253) & (pl.col(\"condition\") == 'right_neural_foraminal_narrowing')\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.922729Z","iopub.execute_input":"2024-09-30T18:00:36.923066Z","iopub.status.idle":"2024-09-30T18:00:36.971713Z","shell.execute_reply.started":"2024-09-30T18:00:36.923018Z","shell.execute_reply":"2024-09-30T18:00:36.970812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_coor.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.975583Z","iopub.execute_input":"2024-09-30T18:00:36.975887Z","iopub.status.idle":"2024-09-30T18:00:36.985192Z","shell.execute_reply.started":"2024-09-30T18:00:36.975855Z","shell.execute_reply":"2024-09-30T18:00:36.984208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_desc.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.986488Z","iopub.execute_input":"2024-09-30T18:00:36.986805Z","iopub.status.idle":"2024-09-30T18:00:36.996343Z","shell.execute_reply.started":"2024-09-30T18:00:36.986773Z","shell.execute_reply":"2024-09-30T18:00:36.995486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mergin tables\nmerged_df = df_melted_proc.join(df_coor, on=\"study_id\", how=\"inner\")\nmerged_df = merged_df.drop(\"level\").drop(\"condition\")\nmerged_df = merged_df.join(df_desc, on=\"series_id\", how=\"inner\").drop(\"study_id_right\")\n\nmerged_df.head(26)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:36.997584Z","iopub.execute_input":"2024-09-30T18:00:36.997958Z","iopub.status.idle":"2024-09-30T18:00:37.153198Z","shell.execute_reply.started":"2024-09-30T18:00:36.997917Z","shell.execute_reply":"2024-09-30T18:00:37.152208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PROCESSING IMGS","metadata":{}},{"cell_type":"code","source":"import glob\nimg_path = glob.glob(base_dir+'/train_images/*/*/*')\nimg_path[1]","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:00:37.154638Z","iopub.execute_input":"2024-09-30T18:00:37.154946Z","iopub.status.idle":"2024-09-30T18:01:13.010407Z","shell.execute_reply.started":"2024-09-30T18:00:37.154912Z","shell.execute_reply":"2024-09-30T18:01:13.009478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_merged_df = merged_df.with_columns(\n    pl.concat_str([\n        pl.lit(base_dir+'/train_images'), \n        pl.col(\"study_id\"), \n        pl.col(\"series_id\"), \n        pl.concat_str([pl.col(\"instance_number\"), pl.lit(\".dcm\")])\n    ], separator=\"/\").alias(\"img_path\")\n)\nimg_merged_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:13.011462Z","iopub.execute_input":"2024-09-30T18:01:13.011747Z","iopub.status.idle":"2024-09-30T18:01:13.324003Z","shell.execute_reply.started":"2024-09-30T18:01:13.011716Z","shell.execute_reply":"2024-09-30T18:01:13.323078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_merged_df.row(0)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:13.325231Z","iopub.execute_input":"2024-09-30T18:01:13.325512Z","iopub.status.idle":"2024-09-30T18:01:13.333395Z","shell.execute_reply.started":"2024-09-30T18:01:13.325481Z","shell.execute_reply":"2024-09-30T18:01:13.332485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\n\ndef process_dcm(path):\n    \n    dicom = pydicom.dcmread(path)\n    print(dicom)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:13.334777Z","iopub.execute_input":"2024-09-30T18:01:13.335227Z","iopub.status.idle":"2024-09-30T18:01:13.342120Z","shell.execute_reply.started":"2024-09-30T18:01:13.335181Z","shell.execute_reply":"2024-09-30T18:01:13.341292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_dcm(img_path[0])","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:13.343230Z","iopub.execute_input":"2024-09-30T18:01:13.343558Z","iopub.status.idle":"2024-09-30T18:01:13.365510Z","shell.execute_reply.started":"2024-09-30T18:01:13.343494Z","shell.execute_reply":"2024-09-30T18:01:13.364546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\n\ndef plot_dicom_images(df, n, nrows=1):\n\n    ncols = (n + nrows - 1) // nrows  \n\n   \n    fig, axes = plt.subplots(nrows, ncols, figsize=(15, 5 * nrows))\n    axes = axes.flatten()  \n\n    for i in range(n):\n\n        dcm_path = df.row(i)[-1] \n        \n      \n        dicom_data = pydicom.dcmread(dcm_path)\n        \n      \n        axes[i].imshow(dicom_data.pixel_array, cmap=plt.cm.gray)\n        axes[i].set_title(f\"ID: {df.row(i)[0]} |{df.row(i)[2]}\")\n        axes[i].axis('off')\n    \n  \n    for j in range(i + 1, nrows * ncols):\n        axes[j].axis('off')\n    \n   \n    plt.tight_layout()\n    plt.show()\n\nplot_dicom_images(img_merged_df, n=12, nrows=6)  \n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:13.366854Z","iopub.execute_input":"2024-09-30T18:01:13.367135Z","iopub.status.idle":"2024-09-30T18:01:15.604173Z","shell.execute_reply.started":"2024-09-30T18:01:13.367105Z","shell.execute_reply":"2024-09-30T18:01:15.603097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_merged_df_pd = img_merged_df.to_pandas()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:15.605449Z","iopub.execute_input":"2024-09-30T18:01:15.605777Z","iopub.status.idle":"2024-09-30T18:01:16.204282Z","shell.execute_reply.started":"2024-09-30T18:01:15.605743Z","shell.execute_reply":"2024-09-30T18:01:16.203212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntest_path = base_dir + '/test_series_descriptions.csv'\ntest_df = pd.read_csv(test_path)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.205707Z","iopub.execute_input":"2024-09-30T18:01:16.206034Z","iopub.status.idle":"2024-09-30T18:01:16.218259Z","shell.execute_reply.started":"2024-09-30T18:01:16.206000Z","shell.execute_reply":"2024-09-30T18:01:16.217345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.219404Z","iopub.execute_input":"2024-09-30T18:01:16.219732Z","iopub.status.idle":"2024-09-30T18:01:16.236061Z","shell.execute_reply.started":"2024-09-30T18:01:16.219698Z","shell.execute_reply":"2024-09-30T18:01:16.235221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_merged_df_pd.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.237226Z","iopub.execute_input":"2024-09-30T18:01:16.238063Z","iopub.status.idle":"2024-09-30T18:01:16.252723Z","shell.execute_reply.started":"2024-09-30T18:01:16.238028Z","shell.execute_reply":"2024-09-30T18:01:16.251798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_merged_df_pd_2 = img_merged_df_pd.drop(['row_id','instance_number', 'condition_right', 'level_right', 'x', 'y'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.254082Z","iopub.execute_input":"2024-09-30T18:01:16.254534Z","iopub.status.idle":"2024-09-30T18:01:16.311346Z","shell.execute_reply.started":"2024-09-30T18:01:16.254489Z","shell.execute_reply":"2024-09-30T18:01:16.310485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_merged_df_pd_2.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.312620Z","iopub.execute_input":"2024-09-30T18:01:16.313010Z","iopub.status.idle":"2024-09-30T18:01:16.325151Z","shell.execute_reply.started":"2024-09-30T18:01:16.312967Z","shell.execute_reply":"2024-09-30T18:01:16.323967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_merged_df_pd_2['class'] = train_merged_df_pd_2['severity'].map({'Normal/Mild': 0, 'Moderate': 1, 'Severe': 2})","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.326448Z","iopub.execute_input":"2024-09-30T18:01:16.326735Z","iopub.status.idle":"2024-09-30T18:01:16.463474Z","shell.execute_reply.started":"2024-09-30T18:01:16.326704Z","shell.execute_reply":"2024-09-30T18:01:16.462537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_merged_df_pd_2.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.464632Z","iopub.execute_input":"2024-09-30T18:01:16.465006Z","iopub.status.idle":"2024-09-30T18:01:16.480560Z","shell.execute_reply.started":"2024-09-30T18:01:16.464964Z","shell.execute_reply":"2024-09-30T18:01:16.479466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_merged_df_pd_2['severity'] = train_merged_df_pd_2['severity'].astype(str)\ntrain_merged_df_pd_2.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.482041Z","iopub.execute_input":"2024-09-30T18:01:16.482487Z","iopub.status.idle":"2024-09-30T18:01:16.515811Z","shell.execute_reply.started":"2024-09-30T18:01:16.482443Z","shell.execute_reply":"2024-09-30T18:01:16.514863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_merged_df_pd_2.dropna(inplace=True)\ntrain_merged_df_pd_2.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.522075Z","iopub.execute_input":"2024-09-30T18:01:16.522381Z","iopub.status.idle":"2024-09-30T18:01:16.948080Z","shell.execute_reply.started":"2024-09-30T18:01:16.522349Z","shell.execute_reply":"2024-09-30T18:01:16.947167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## IMG PROC","metadata":{}},{"cell_type":"code","source":"def dicom_to_tensor(dicom_path, target_size=(224, 224)):\n    # Leer la imagen DICOM\n    dicom = pydicom.dcmread(dicom_path)\n    \n    # Extraer el array de píxeles de la imagen\n    img_array = dicom.pixel_array.astype(float)\n    \n    # Normalizar la imagen a valores entre 0 y 1\n    img_array = (img_array - np.min(img_array)) / (np.max(img_array) - np.min(img_array))\n    \n    # Escalar la imagen a valores entre 0 y 255\n    img_array = (img_array * 255).astype(np.uint8)\n    \n    # Si la imagen es en escala de grises, la convertimos a RGB (replicando el canal)\n    if len(img_array.shape) == 2:\n        img_array = np.stack([img_array] * 3, axis=-1)\n    \n    # Redimensionar la imagen al tamaño objetivo\n    img_resized = tf.image.resize(img_array, target_size)  # O usa cv2 si prefieres\n    \n    # Convertir a tensor\n    tensor_img = tf.convert_to_tensor(img_resized, dtype=tf.float32)  # o usa torch.tensor si usas PyTorch\n    \n    return tensor_img","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.949204Z","iopub.execute_input":"2024-09-30T18:01:16.949504Z","iopub.status.idle":"2024-09-30T18:01:16.957432Z","shell.execute_reply.started":"2024-09-30T18:01:16.949472Z","shell.execute_reply":"2024-09-30T18:01:16.956438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = train_merged_df_pd_2.sample(frac=0.009, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:16.958563Z","iopub.execute_input":"2024-09-30T18:01:16.958900Z","iopub.status.idle":"2024-09-30T18:01:17.007177Z","shell.execute_reply.started":"2024-09-30T18:01:16.958867Z","shell.execute_reply":"2024-09-30T18:01:17.006210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:17.008309Z","iopub.execute_input":"2024-09-30T18:01:17.008603Z","iopub.status.idle":"2024-09-30T18:01:17.022281Z","shell.execute_reply.started":"2024-09-30T18:01:17.008571Z","shell.execute_reply":"2024-09-30T18:01:17.021214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:17.023701Z","iopub.execute_input":"2024-09-30T18:01:17.024024Z","iopub.status.idle":"2024-09-30T18:01:17.031987Z","shell.execute_reply.started":"2024-09-30T18:01:17.023989Z","shell.execute_reply":"2024-09-30T18:01:17.031085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:17.033368Z","iopub.execute_input":"2024-09-30T18:01:17.034210Z","iopub.status.idle":"2024-09-30T18:01:17.047309Z","shell.execute_reply.started":"2024-09-30T18:01:17.034139Z","shell.execute_reply":"2024-09-30T18:01:17.046441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor_array=np.array([dicom_to_tensor(img) for img in sample_df['img_path']])\n# np.save('tensor_array.npy', tensor_array)\n# tensor_array = np.load('/kaggle/working/tensor_array.npy')","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:01:17.048724Z","iopub.execute_input":"2024-09-30T18:01:17.049141Z","iopub.status.idle":"2024-09-30T18:05:29.088848Z","shell.execute_reply.started":"2024-09-30T18:01:17.049054Z","shell.execute_reply":"2024-09-30T18:05:29.087961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" from tensorflow.keras.utils import to_categorical\n Y = np.array(sample_df['class']).astype(int)\n# Y =  np.load('/kaggle/working/Y.npy')","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.090136Z","iopub.execute_input":"2024-09-30T18:05:29.090469Z","iopub.status.idle":"2024-09-30T18:05:29.097729Z","shell.execute_reply.started":"2024-09-30T18:05:29.090436Z","shell.execute_reply":"2024-09-30T18:05:29.096874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('Y.npy', Y)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.098807Z","iopub.execute_input":"2024-09-30T18:05:29.099096Z","iopub.status.idle":"2024-09-30T18:05:29.115454Z","shell.execute_reply.started":"2024-09-30T18:05:29.099065Z","shell.execute_reply":"2024-09-30T18:05:29.114481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# To one-hot encoding\nY_one = to_categorical(Y, num_classes=3)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.116962Z","iopub.execute_input":"2024-09-30T18:05:29.117619Z","iopub.status.idle":"2024-09-30T18:05:29.126831Z","shell.execute_reply.started":"2024-09-30T18:05:29.117571Z","shell.execute_reply":"2024-09-30T18:05:29.126017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_one.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.128027Z","iopub.execute_input":"2024-09-30T18:05:29.128436Z","iopub.status.idle":"2024-09-30T18:05:29.139209Z","shell.execute_reply.started":"2024-09-30T18:05:29.128392Z","shell.execute_reply":"2024-09-30T18:05:29.138404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor_array.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.140326Z","iopub.execute_input":"2024-09-30T18:05:29.140594Z","iopub.status.idle":"2024-09-30T18:05:29.150811Z","shell.execute_reply.started":"2024-09-30T18:05:29.140564Z","shell.execute_reply":"2024-09-30T18:05:29.149875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(3, activation='softmax')\n])\n\n# Compilar el modelo\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.151903Z","iopub.execute_input":"2024-09-30T18:05:29.152214Z","iopub.status.idle":"2024-09-30T18:05:29.255535Z","shell.execute_reply.started":"2024-09-30T18:05:29.152156Z","shell.execute_reply":"2024-09-30T18:05:29.254434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(tensor_array, Y_one, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:29.256874Z","iopub.execute_input":"2024-09-30T18:05:29.257315Z","iopub.status.idle":"2024-09-30T18:05:30.712733Z","shell.execute_reply.started":"2024-09-30T18:05:29.257266Z","shell.execute_reply":"2024-09-30T18:05:30.711824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" hist = model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=10, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:05:30.713807Z","iopub.execute_input":"2024-09-30T18:05:30.714088Z","iopub.status.idle":"2024-09-30T18:07:15.995006Z","shell.execute_reply.started":"2024-09-30T18:05:30.714059Z","shell.execute_reply":"2024-09-30T18:07:15.994098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:15.996378Z","iopub.execute_input":"2024-09-30T18:07:15.996693Z","iopub.status.idle":"2024-09-30T18:07:16.215784Z","shell.execute_reply.started":"2024-09-30T18:07:15.996660Z","shell.execute_reply":"2024-09-30T18:07:16.214733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TEST","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ntest_path = base_dir + '/test_series_descriptions.csv'\ntest_df = pd.read_csv(test_path)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:16.216999Z","iopub.execute_input":"2024-09-30T18:07:16.217342Z","iopub.status.idle":"2024-09-30T18:07:16.232917Z","shell.execute_reply.started":"2024-09-30T18:07:16.217308Z","shell.execute_reply":"2024-09-30T18:07:16.231976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conditions = {'Sagittal T1': ['left_neural_foraminal_narrowing', 'right_neural_foraminal_narrowing'],'Axial T2': ['left_subarticular_stenosis' 'right_subarticular_stenosis'],'Sagittal T2/STIR': 'spinal_canal_stenosis'}\n   \n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:16.234266Z","iopub.execute_input":"2024-09-30T18:07:16.235016Z","iopub.status.idle":"2024-09-30T18:07:16.239684Z","shell.execute_reply.started":"2024-09-30T18:07:16.234970Z","shell.execute_reply":"2024-09-30T18:07:16.238614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ntest_img_path = glob.glob(base_dir+'/test_images/*/*/*')\ntets_tensor_arr = np.array([dicom_to_tensor(img) for img in test_img_path])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:16.240810Z","iopub.execute_input":"2024-09-30T18:07:16.241123Z","iopub.status.idle":"2024-09-30T18:07:19.013476Z","shell.execute_reply.started":"2024-09-30T18:07:16.241091Z","shell.execute_reply":"2024-09-30T18:07:19.012440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img_path_1 = os.listdir(base_dir+'/test_images/44036939/3481971518/')","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:19.014876Z","iopub.execute_input":"2024-09-30T18:07:19.015303Z","iopub.status.idle":"2024-09-30T18:07:19.019891Z","shell.execute_reply.started":"2024-09-30T18:07:19.015253Z","shell.execute_reply":"2024-09-30T18:07:19.018985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_img_path_1)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:19.021113Z","iopub.execute_input":"2024-09-30T18:07:19.021505Z","iopub.status.idle":"2024-09-30T18:07:19.031596Z","shell.execute_reply.started":"2024-09-30T18:07:19.021461Z","shell.execute_reply":"2024-09-30T18:07:19.030710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conditions = ['left_neural_foraminal_narrowing',\n             'left_subarticular_stenosis',\n             'right_neural_foraminal_narrowing',\n             'right_subarticular_stenosis',\n             'spinal_canal_stenosis']\n\nlevels = ['l1_l2',\n         'l2_l3',\n         'l3_l4',\n         'l4_l5',\n         'l5_s1']","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:19.032693Z","iopub.execute_input":"2024-09-30T18:07:19.032994Z","iopub.status.idle":"2024-09-30T18:07:19.042255Z","shell.execute_reply.started":"2024-09-30T18:07:19.032948Z","shell.execute_reply":"2024-09-30T18:07:19.041356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row_id = []\n\nfor cond in conditions:\n    print(cond)\n    for l in levels:\n        row_id.append('44036939' + \"_\" + cond + \"_\" + l)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:19.043399Z","iopub.execute_input":"2024-09-30T18:07:19.043675Z","iopub.status.idle":"2024-09-30T18:07:19.052274Z","shell.execute_reply.started":"2024-09-30T18:07:19.043638Z","shell.execute_reply":"2024-09-30T18:07:19.051409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row_id","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:19.053378Z","iopub.execute_input":"2024-09-30T18:07:19.053732Z","iopub.status.idle":"2024-09-30T18:07:19.063841Z","shell.execute_reply.started":"2024-09-30T18:07:19.053686Z","shell.execute_reply":"2024-09-30T18:07:19.063002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_id_arr = []\nconds = []\npaths = []\nrows = []\n\nfor _,row in test_df.iloc[:,1:].iterrows():\n        \n        s_id = row[0]\n        cond = row[1]\n        img_path = os.listdir(base_dir+'/test_images/44036939/'+str(s_id)+'/')\n        for img in img_path:\n            s_id_arr.append(s_id)\n            conds.append(cond)\n            paths.append(base_dir + '/test_images/' + str(s_id) + '/' + str(cond) + '/' + img)\n        break\n    ","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:40.007925Z","iopub.execute_input":"2024-09-30T18:07:40.008350Z","iopub.status.idle":"2024-09-30T18:07:40.017606Z","shell.execute_reply.started":"2024-09-30T18:07:40.008309Z","shell.execute_reply":"2024-09-30T18:07:40.016659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(paths)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:47.317419Z","iopub.execute_input":"2024-09-30T18:07:47.318560Z","iopub.status.idle":"2024-09-30T18:07:47.324632Z","shell.execute_reply.started":"2024-09-30T18:07:47.318516Z","shell.execute_reply":"2024-09-30T18:07:47.323628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tets_tensor_arr[0]","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:07:51.164701Z","iopub.execute_input":"2024-09-30T18:07:51.165096Z","iopub.status.idle":"2024-09-30T18:07:51.173298Z","shell.execute_reply.started":"2024-09-30T18:07:51.165057Z","shell.execute_reply":"2024-09-30T18:07:51.172407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(tets_tensor_arr)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:08:22.203749Z","iopub.execute_input":"2024-09-30T18:08:22.204230Z","iopub.status.idle":"2024-09-30T18:08:23.041800Z","shell.execute_reply.started":"2024-09-30T18:08:22.204181Z","shell.execute_reply":"2024-09-30T18:08:23.040918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in predictions:\n    print(i)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:08:26.628302Z","iopub.execute_input":"2024-09-30T18:08:26.628713Z","iopub.status.idle":"2024-09-30T18:08:26.646228Z","shell.execute_reply.started":"2024-09-30T18:08:26.628675Z","shell.execute_reply":"2024-09-30T18:08:26.645204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = ['normal/mild', 'moderate', 'severe']\nvalues_df = pd.DataFrame(predictions, columns=columns)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:10:16.604985Z","iopub.execute_input":"2024-09-30T18:10:16.605905Z","iopub.status.idle":"2024-09-30T18:10:16.610617Z","shell.execute_reply.started":"2024-09-30T18:10:16.605862Z","shell.execute_reply":"2024-09-30T18:10:16.609658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids_df = pd.DataFrame(row_id, columns=['row_id'])","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:10:18.276904Z","iopub.execute_input":"2024-09-30T18:10:18.277270Z","iopub.status.idle":"2024-09-30T18:10:18.282407Z","shell.execute_reply.started":"2024-09-30T18:10:18.277237Z","shell.execute_reply":"2024-09-30T18:10:18.281314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.concat([ids_df, values_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:10:19.931033Z","iopub.execute_input":"2024-09-30T18:10:19.931906Z","iopub.status.idle":"2024-09-30T18:10:19.944068Z","shell.execute_reply.started":"2024-09-30T18:10:19.931862Z","shell.execute_reply":"2024-09-30T18:10:19.942929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[0:25]","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:11:03.314283Z","iopub.execute_input":"2024-09-30T18:11:03.314989Z","iopub.status.idle":"2024-09-30T18:11:03.329642Z","shell.execute_reply.started":"2024-09-30T18:11:03.314943Z","shell.execute_reply":"2024-09-30T18:11:03.328657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[0:25].to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-30T18:11:14.248723Z","iopub.execute_input":"2024-09-30T18:11:14.249382Z","iopub.status.idle":"2024-09-30T18:11:14.257566Z","shell.execute_reply.started":"2024-09-30T18:11:14.249339Z","shell.execute_reply":"2024-09-30T18:11:14.256549Z"},"trusted":true},"execution_count":null,"outputs":[]}]}