{"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":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## 1. コンペ概要\n- **タスク**<br>\n腰椎MRI画像を用いた5つの腰椎変性疾患の検出と重症度の分類。<br>\n5つの腰椎変性疾患の分類に焦点を当てる： 左神経孔狭窄、右神経孔狭窄、左関節下狭窄、右関節下狭窄、脊柱管狭窄。<br>\n椎間板レベルL1/L2、L2/L3、L3/L4、L4/L5、L5/S1 にわたる5つの状態それぞれについて、重症度スコア（正常/軽度、中等度、重度）が提供される。\n\n- **モチベーション**<br>\n正確で迅速な重症度の等級付けが、腰痛を緩和し、患者の全体的な健康と生活の質を向上させるための治療と手術の可能性を導くのに役立つ。\n\n- **評価関数**<br>\nサンプルで重み付けされたログロスの平均と、メトリックによって生成されたany_severe_spinal予測を用いて評価される。<br>\nサンプルの重みは以下の通り：<br>\n    - 1：Normal/Mild（正常/軽度）<br>\n    - 2：Moderate（中程度）<br>\n    - 4：Severe（重度）<br>\n評価指標：https://www.kaggle.com/code/metric/rsna-lumbar-metric-71549","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport polars as pl\npl.Config.set_tbl_rows(40)\npl.Config.set_fmt_str_lengths(n=40)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-01T13:36:04.766302Z","iopub.execute_input":"2024-06-01T13:36:04.766686Z","iopub.status.idle":"2024-06-01T13:36:04.772822Z","shell.execute_reply.started":"2024-06-01T13:36:04.766658Z","shell.execute_reply":"2024-06-01T13:36:04.771552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read data\nINPUT_DIR = '../input/rsna-2024-lumbar-spine-degenerative-classification'\n\ntrain = pl.read_csv(f'{INPUT_DIR}/train.csv')\ntrain_label = pl.read_csv(f'{INPUT_DIR}/train_label_coordinates.csv')\ntrain_desc = pl.read_csv(f'{INPUT_DIR}/train_series_descriptions.csv')\ntest_desc = pl.read_csv(f'{INPUT_DIR}/test_series_descriptions.csv')\nsample_submission = pl.read_csv(f'{INPUT_DIR}/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:52:13.056767Z","iopub.execute_input":"2024-06-01T13:52:13.057221Z","iopub.status.idle":"2024-06-01T13:52:13.096104Z","shell.execute_reply.started":"2024-06-01T13:52:13.057165Z","shell.execute_reply":"2024-06-01T13:52:13.095221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. データ\n一連のMRI画像とラベルが配布される。学習データ(study_id)は1,975件。","metadata":{}},{"cell_type":"markdown","source":"## 2-1. train.csv\n学習データセットの正解ラベル。\n- `study_id` - 検査ID。各検査は複数の一連の画像を含む。\n- `[condition]_[level]` - spinal_canal_stenosis_l1_l2のような正解ラベル、重症度レベルはNormal/Mild(正常/軽度)、Moderate(中程度)、Severe(重度)の3種類。ラベルが不完全な項目もある。","metadata":{"execution":{"iopub.status.busy":"2024-06-01T14:06:06.225431Z","iopub.execute_input":"2024-06-01T14:06:06.225799Z","iopub.status.idle":"2024-06-01T14:06:06.233391Z","shell.execute_reply.started":"2024-06-01T14:06:06.225771Z","shell.execute_reply":"2024-06-01T14:06:06.231911Z"}}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:24:59.629949Z","iopub.execute_input":"2024-06-01T13:24:59.630365Z","iopub.status.idle":"2024-06-01T13:24:59.668018Z","shell.execute_reply.started":"2024-06-01T13:24:59.630335Z","shell.execute_reply":"2024-06-01T13:24:59.666861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- `condition` - 症状の種類。3種類あり、左右ある症状を含めると5種類ある。\n    - `spinal_canal_stenosis` - 脊柱管狭窄症\n    - `left_neural_foraminal_narrowing` - 左神経孔狭窄症\n    - `right_neural_foraminal_narrowing` - 右神経孔狭窄症\n    - `left_subarticular_stenosis` - 左亜節関節性狭窄症\n    - `right_subarticular_stenosis` - 右亜節関節性狭窄症\n- `level` - 椎間板にある腰神経のレベル。<br>参考：https://jtca2020.or.jp/news/cat3/dermatome/","metadata":{}},{"cell_type":"markdown","source":"## 2-2. train_label.csv\n- `study_id` - 検査ID。\n- `series_id` - MRI画像のシリーズID。同じIDの画像は、同じ1回のMRI撮影で得られた一枚のスライス画像である。\n- `instance_number` - MRI画像のオーダー番号。conditionのラベルが付いたオーダー番号のみ存在。\n- `condition` - 中心的な症状。\n- `level` - 関連する腰神経のレベル。\n- `[x/y]` - ラベルを定義した領域の中心のx/y座標。","metadata":{}},{"cell_type":"code","source":"train_label.filter(pl.col('study_id')==100206310)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:12:09.287118Z","iopub.execute_input":"2024-06-01T13:12:09.287530Z","iopub.status.idle":"2024-06-01T13:12:09.303066Z","shell.execute_reply.started":"2024-06-01T13:12:09.287499Z","shell.execute_reply":"2024-06-01T13:12:09.301848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-3. [train/test]_series_descriptions.csv\n- `study_id` - 検査ID。\n- `series_id` - MRI画像のシリーズID。\n- `series_decription` - スキャンの方向と撮影方法。撮影方法の種類によって、信号の強さの箇所が異なる。\n    - `Sagittal T1` - Sagital(矢状断)方向で、T1画像。\n    - `Sagittal T2/STIR` - sagital(矢状断)方向で、T2またはSTIR(脂肪抑制)画像\n    - `Axial T2` - axial(水平断) でT2画像。<br>\n    参考:https://ilclinic.or.jp/column/mri%E7%94%BB%E5%83%8F%E3%81%AE%E8%A6%8B%E6%96%B9","metadata":{}},{"cell_type":"code","source":"train_desc","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:24:52.702878Z","iopub.execute_input":"2024-06-01T13:24:52.703333Z","iopub.status.idle":"2024-06-01T13:24:52.715249Z","shell.execute_reply.started":"2024-06-01T13:24:52.703297Z","shell.execute_reply":"2024-06-01T13:24:52.714057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_desc.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:24:46.909939Z","iopub.execute_input":"2024-06-01T13:24:46.910401Z","iopub.status.idle":"2024-06-01T13:24:46.920594Z","shell.execute_reply.started":"2024-06-01T13:24:46.910367Z","shell.execute_reply":"2024-06-01T13:24:46.919083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='series_description', data=train_desc.to_pandas())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:16:26.058921Z","iopub.execute_input":"2024-06-01T13:16:26.059506Z","iopub.status.idle":"2024-06-01T13:16:26.369564Z","shell.execute_reply.started":"2024-06-01T13:16:26.059471Z","shell.execute_reply":"2024-06-01T13:16:26.368742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-4. [train/test]_images/[study_id]/[series_id]/[instance_number].dcm\n画像データ。形式は医療画像によく用いられるDICOM画像で、メタ情報が含まれる。<br>\n読込にはpydicomなどのライブラリを用いる。<br>\n症状のある画像を見てみる。","metadata":{}},{"cell_type":"code","source":"# ラベルの存在するidを指定\nstudy_id = 100206310\nseries_id = 1012284084\ninstance_number = 20\n\ntrain_label.filter(\n    (pl.col('study_id')==study_id)\n    &(pl.col('series_id')==series_id)\n    &(pl.col('instance_number')==instance_number)\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:40:18.042471Z","iopub.execute_input":"2024-06-01T13:40:18.042839Z","iopub.status.idle":"2024-06-01T13:40:18.053432Z","shell.execute_reply.started":"2024-06-01T13:40:18.042810Z","shell.execute_reply":"2024-06-01T13:40:18.052280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dcmファイル読込\nds = pydicom.read_file(f'{INPUT_DIR}/train_images/{study_id}/{series_id}/{instance_number}.dcm')","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:40:42.150972Z","iopub.execute_input":"2024-06-01T13:40:42.151392Z","iopub.status.idle":"2024-06-01T13:40:42.159125Z","shell.execute_reply.started":"2024-06-01T13:40:42.151359Z","shell.execute_reply":"2024-06-01T13:40:42.157721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# メタ情報の確認\nds","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:40:49.190911Z","iopub.execute_input":"2024-06-01T13:40:49.191322Z","iopub.status.idle":"2024-06-01T13:40:49.201848Z","shell.execute_reply.started":"2024-06-01T13:40:49.191290Z","shell.execute_reply":"2024-06-01T13:40:49.200739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds.pixel_array","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:41:12.665920Z","iopub.execute_input":"2024-06-01T13:41:12.666392Z","iopub.status.idle":"2024-06-01T13:41:12.674782Z","shell.execute_reply.started":"2024-06-01T13:41:12.666344Z","shell.execute_reply":"2024-06-01T13:41:12.673466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# matplotlibで描画\nplt.imshow(ds.pixel_array, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:41:28.809049Z","iopub.execute_input":"2024-06-01T13:41:28.809490Z","iopub.status.idle":"2024-06-01T13:41:29.061847Z","shell.execute_reply.started":"2024-06-01T13:41:28.809456Z","shell.execute_reply":"2024-06-01T13:41:29.060535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# boneの方が見やすいかも。ここは好みで\nplt.imshow(ds.pixel_array, cmap='bone')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:42:52.876509Z","iopub.execute_input":"2024-06-01T13:42:52.877618Z","iopub.status.idle":"2024-06-01T13:42:53.217758Z","shell.execute_reply.started":"2024-06-01T13:42:52.877566Z","shell.execute_reply":"2024-06-01T13:42:53.216506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ラベルの箇所（症状の中心部分）を可視化、この例では左右で2か所存在\ndf_plt = train_label.filter(\n    (pl.col('study_id')==study_id)\n    &(pl.col('series_id')==series_id)\n    &(pl.col('instance_number')==instance_number)\n)\nplt.imshow(ds.pixel_array, cmap='bone')\nfor row in df_plt.iter_rows():\n    plt.scatter(row[-2], row[-1], color='red')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:49:33.925350Z","iopub.execute_input":"2024-06-01T13:49:33.925770Z","iopub.status.idle":"2024-06-01T13:49:34.225835Z","shell.execute_reply.started":"2024-06-01T13:49:33.925735Z","shell.execute_reply":"2024-06-01T13:49:34.224772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-5. sample_submission.csv\n- `row_id` - 12345_spinal_canal_stenosis_l3_l4のような、study_id,condition,levelによるid。\n- `[normal_mild/moderate/severe]` - 各症状レベルの予測結果を入力する。合計が1となる確率分布で予測する。","metadata":{}},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:52:19.549868Z","iopub.execute_input":"2024-06-01T13:52:19.550285Z","iopub.status.idle":"2024-06-01T13:52:19.561068Z","shell.execute_reply.started":"2024-06-01T13:52:19.550243Z","shell.execute_reply":"2024-06-01T13:52:19.559931Z"},"trusted":true},"execution_count":null,"outputs":[]}]}