{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# RSNA 2024 Lumbar Spine Degenerative Classification","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Description\n\nLow back pain is the leading cause of disability worldwide, according to the World Health Organization, affecting 619 million people in 2020. Most people experience low back pain at some point in their lives, with the frequency increasing with age. Pain and restricted mobility are often symptoms of spondylosis, a set of degenerative spine conditions including degeneration of intervertebral discs and subsequent narrowing of the spinal canal (spinal stenosis), subarticular recesses, or neural foramen with associated compression or irritations of the nerves in the low back.\n\nMagnetic resonance imaging (MRI) provides a detailed view of the lumbar spine vertebra, discs and nerves, enabling radiologists to assess the presence and severity of these conditions. Proper diagnosis and grading of these conditions help guide treatment and potential surgery to help alleviate back pain and improve overall health and quality of life for patients.\n\nRSNA has teamed with the American Society of Neuroradiology (ASNR) to conduct this competition exploring whether artificial intelligence can be used to aid in the detection and classification of degenerative spine conditions using lumbar spine MR images.\n\nThe challenge will focus on the classification of five lumbar spine degenerative conditions: Left Neural Foraminal Narrowing, Right Neural Foraminal Narrowing, Left Subarticular Stenosis, Right Subarticular Stenosis, and Spinal Canal Stenosis. For each imaging study in the dataset, we’ve provided severity scores (Normal/Mild, Moderate, or Severe) for each of the five conditions across the intervertebral disc levels L1/L2, L2/L3, L3/L4, L4/L5, and L5/S1.\n\nTo create the ground truth dataset, the RSNA challenge planning task force collected imaging data sourced from eight sites on five continents. This multi-institutional, expertly curated dataset promises to improve standardized classification of degenerative lumbar spine conditions and enable development of tools to automate accurate and rapid disease classification.","metadata":{"execution":{"iopub.status.busy":"2024-05-22T13:34:02.414659Z","iopub.execute_input":"2024-05-22T13:34:02.415035Z","iopub.status.idle":"2024-05-22T13:34:02.453727Z","shell.execute_reply.started":"2024-05-22T13:34:02.415006Z","shell.execute_reply":"2024-05-22T13:34:02.452240Z"}}},{"cell_type":"markdown","source":"# Data Understanding and Exploratory Data Analysis (EDA)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nsns.set_style('darkgrid')\nsns.set()\nimport matplotlib.pyplot as plt\n%config InlineBackend.figure_format = 'retina'\n\nimport os\nfrom tqdm import tqdm\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:19:05.438532Z","iopub.execute_input":"2024-05-27T18:19:05.439227Z","iopub.status.idle":"2024-05-27T18:19:05.449108Z","shell.execute_reply.started":"2024-05-27T18:19:05.439195Z","shell.execute_reply":"2024-05-27T18:19:05.447959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\ntrain_label_coordinates = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:55.753580Z","iopub.execute_input":"2024-05-27T18:17:55.754487Z","iopub.status.idle":"2024-05-27T18:17:55.894801Z","shell.execute_reply.started":"2024-05-27T18:17:55.754452Z","shell.execute_reply":"2024-05-27T18:17:55.893991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:55.896049Z","iopub.execute_input":"2024-05-27T18:17:55.896430Z","iopub.status.idle":"2024-05-27T18:17:55.934376Z","shell.execute_reply.started":"2024-05-27T18:17:55.896381Z","shell.execute_reply":"2024-05-27T18:17:55.933480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"COLUMNS - %i:\" % len(train_data.columns))\nprint(train_data.columns)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:55.936315Z","iopub.execute_input":"2024-05-27T18:17:55.936713Z","iopub.status.idle":"2024-05-27T18:17:55.942399Z","shell.execute_reply.started":"2024-05-27T18:17:55.936678Z","shell.execute_reply":"2024-05-27T18:17:55.941486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of train_data: (%i, %i)\" % train_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:55.943718Z","iopub.execute_input":"2024-05-27T18:17:55.944072Z","iopub.status.idle":"2024-05-27T18:17:55.951387Z","shell.execute_reply.started":"2024-05-27T18:17:55.944041Z","shell.execute_reply":"2024-05-27T18:17:55.950478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:55.952697Z","iopub.execute_input":"2024-05-27T18:17:55.953336Z","iopub.status.idle":"2024-05-27T18:17:55.986961Z","shell.execute_reply.started":"2024-05-27T18:17:55.953301Z","shell.execute_reply":"2024-05-27T18:17:55.986031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:55.988721Z","iopub.execute_input":"2024-05-27T18:17:55.988993Z","iopub.status.idle":"2024-05-27T18:17:56.060543Z","shell.execute_reply.started":"2024-05-27T18:17:55.988970Z","shell.execute_reply":"2024-05-27T18:17:56.059588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Unique value of all columns:\")\nprint(train_data.iloc[:, 1].unique())","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:56.354278Z","iopub.execute_input":"2024-05-27T18:17:56.355026Z","iopub.status.idle":"2024-05-27T18:17:56.360231Z","shell.execute_reply.started":"2024-05-27T18:17:56.354998Z","shell.execute_reply":"2024-05-27T18:17:56.359267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:56.820044Z","iopub.execute_input":"2024-05-27T18:17:56.820393Z","iopub.status.idle":"2024-05-27T18:17:56.833146Z","shell.execute_reply.started":"2024-05-27T18:17:56.820365Z","shell.execute_reply":"2024-05-27T18:17:56.832057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_coordinates.at[0, 'x']","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:17:57.186201Z","iopub.execute_input":"2024-05-27T18:17:57.186505Z","iopub.status.idle":"2024-05-27T18:17:57.192538Z","shell.execute_reply.started":"2024-05-27T18:17:57.186481Z","shell.execute_reply":"2024-05-27T18:17:57.191626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(data: pd.DataFrame, base_path: str):\n    images = []\n    labels = []\n    \n    for index, row in tqdm(data.iterrows(), desc='Loading images', total=len(data)):\n        study_id = str(row['study_id'])\n        series_id = row['series_id']\n        instance_number = int(row['instance_number'])\n        condition = row['condition']\n        level = row['level']\n\n        study_path = os.path.join(base_path, study_id)\n\n        found = False\n        for root, _, files in os.walk(study_path):\n            for file in files:\n                if file.endswith('.dcm') and int(file.split('.')[0]) == instance_number:\n                    file_path = os.path.join(root, file)\n                    image = pydicom.dcmread(file_path)\n                    images.append(image.pixel_array)\n                    labels.append((condition, level))\n                    found = True\n                    break\n\n\n            if found:\n                break\n\n    return images, labels","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:28:58.208348Z","iopub.execute_input":"2024-05-27T18:28:58.209077Z","iopub.status.idle":"2024-05-27T18:28:58.219865Z","shell.execute_reply.started":"2024-05-27T18:28:58.209046Z","shell.execute_reply":"2024-05-27T18:28:58.218812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features, labels = load_data(train_label_coordinates, '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:29:05.633321Z","iopub.execute_input":"2024-05-27T18:29:05.633687Z","iopub.status.idle":"2024-05-27T18:44:08.608523Z","shell.execute_reply.started":"2024-05-27T18:29:05.633659Z","shell.execute_reply":"2024-05-27T18:44:08.607538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:51:34.097704Z","iopub.execute_input":"2024-05-27T18:51:34.098051Z","iopub.status.idle":"2024-05-27T18:51:34.103999Z","shell.execute_reply.started":"2024-05-27T18:51:34.098028Z","shell.execute_reply":"2024-05-27T18:51:34.103016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[0]","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:51:41.884160Z","iopub.execute_input":"2024-05-27T18:51:41.884528Z","iopub.status.idle":"2024-05-27T18:51:41.890232Z","shell.execute_reply.started":"2024-05-27T18:51:41.884499Z","shell.execute_reply":"2024-05-27T18:51:41.889292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = ds = pydicom.read_file('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/4003253/702807833/8.dcm')","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:56:35.082638Z","iopub.execute_input":"2024-05-27T18:56:35.083010Z","iopub.status.idle":"2024-05-27T18:56:35.089960Z","shell.execute_reply.started":"2024-05-27T18:56:35.082980Z","shell.execute_reply":"2024-05-27T18:56:35.088626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(ds.pixel_array, cmap='gray')\n\nplt.axis(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:56:36.996230Z","iopub.execute_input":"2024-05-27T18:56:36.997087Z","iopub.status.idle":"2024-05-27T18:56:37.370887Z","shell.execute_reply.started":"2024-05-27T18:56:36.997054Z","shell.execute_reply":"2024-05-27T18:56:37.369941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(features[0], cmap='gray')\n\nplt.axis(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-27T18:56:45.382292Z","iopub.execute_input":"2024-05-27T18:56:45.383151Z","iopub.status.idle":"2024-05-27T18:56:45.728548Z","shell.execute_reply.started":"2024-05-27T18:56:45.383120Z","shell.execute_reply":"2024-05-27T18:56:45.727609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}