{"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":"tpu1vmV38","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30748,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from joblib import Parallel, delayed","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pydicom","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def is_interactive():\n    return 'runtime' in get_ipython().config.IPKernelApp.connection_file\n\nprint('Interactive?', is_interactive())","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:03:44.478104Z","iopub.execute_input":"2024-09-15T12:03:44.478598Z","iopub.status.idle":"2024-09-15T12:03:44.486416Z","shell.execute_reply.started":"2024-09-15T12:03:44.478544Z","shell.execute_reply":"2024-09-15T12:03:44.485075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    EPOCHS = 100\n    BATCH_SIZE = 32\n    IMAGE_SIZE = 512\n    WD = 1e-6\n    LR = 1e-3\n    SEED = 42\n    AUG_PROB = 1","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:20:11.861876Z","iopub.execute_input":"2024-09-15T12:20:11.862394Z","iopub.status.idle":"2024-09-15T12:20:11.869375Z","shell.execute_reply.started":"2024-09-15T12:20:11.862347Z","shell.execute_reply":"2024-09-15T12:20:11.868280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport glob","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:15:57.676642Z","iopub.execute_input":"2024-09-15T12:15:57.677073Z","iopub.status.idle":"2024-09-15T12:15:57.682867Z","shell.execute_reply.started":"2024-09-15T12:15:57.677038Z","shell.execute_reply":"2024-09-15T12:15:57.681527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, random\nimport numpy as np\ndef set_seeds(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n\nset_seeds(seed=CFG.SEED)\nrng = np.random.default_rng(seed=CFG.SEED)","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:15:57.816022Z","iopub.execute_input":"2024-09-15T12:15:57.816411Z","iopub.status.idle":"2024-09-15T12:15:57.825070Z","shell.execute_reply.started":"2024-09-15T12:15:57.816380Z","shell.execute_reply":"2024-09-15T12:15:57.823842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:15:58.094764Z","iopub.execute_input":"2024-09-15T12:15:58.095157Z","iopub.status.idle":"2024-09-15T12:15:58.101433Z","shell.execute_reply.started":"2024-09-15T12:15:58.095125Z","shell.execute_reply":"2024-09-15T12:15:58.100183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = pd.read_csv(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv\").dropna()","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:15:58.582455Z","iopub.execute_input":"2024-09-15T12:15:58.583400Z","iopub.status.idle":"2024-09-15T12:15:58.631061Z","shell.execute_reply.started":"2024-09-15T12:15:58.583363Z","shell.execute_reply":"2024-09-15T12:15:58.629819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:15:58.752241Z","iopub.execute_input":"2024-09-15T12:15:58.752683Z","iopub.status.idle":"2024-09-15T12:15:58.769110Z","shell.execute_reply.started":"2024-09-15T12:15:58.752647Z","shell.execute_reply":"2024-09-15T12:15:58.767966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"axial_t2 = df[df[\"series_description\"] == \"Sagittal T2/STIR\"]\naxial_t2 = axial_t2[axial_t2[\"study_id\"].isin(target[\"study_id\"])] \npath = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\"\nunique = np.unique(axial_t2[\"study_id\"])\naxial_t2 = [list(map(lambda x: path + \"/\" + str(i) + \"/\" + str(x), axial_t2[axial_t2[\"study_id\"] == i][\"series_id\"])) for i in unique]\naxial_t2 = [list(map(lambda x: [x + \"/\" + z for z in os.listdir(x)], i)) for i in axial_t2]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:17:46.420636Z","iopub.execute_input":"2024-09-15T12:17:46.421892Z","iopub.status.idle":"2024-09-15T12:17:49.594075Z","shell.execute_reply.started":"2024-09-15T12:17:46.421850Z","shell.execute_reply":"2024-09-15T12:17:49.592657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_empty(pad):\n    return [\"\" for _ in range(pad)]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:17:49.596057Z","iopub.execute_input":"2024-09-15T12:17:49.596441Z","iopub.status.idle":"2024-09-15T12:17:49.601926Z","shell.execute_reply.started":"2024-09-15T12:17:49.596409Z","shell.execute_reply":"2024-09-15T12:17:49.600670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_and_split(x):\n    x = rng.choice(x, 40) if len(x) > 40 else x + return_empty(40-len(x))\n    assert len(x) == 40\n    return x ","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:02.561201Z","iopub.execute_input":"2024-09-15T12:18:02.561722Z","iopub.status.idle":"2024-09-15T12:18:02.571212Z","shell.execute_reply.started":"2024-09-15T12:18:02.561675Z","shell.execute_reply":"2024-09-15T12:18:02.569985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"axial_t2_i = [[iii for ii in i for iii in ii] for i in axial_t2]\naxial_t2_i = [resize_and_split(i) for i in axial_t2_i]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:03.420144Z","iopub.execute_input":"2024-09-15T12:18:03.420580Z","iopub.status.idle":"2024-09-15T12:18:03.441376Z","shell.execute_reply.started":"2024-09-15T12:18:03.420531Z","shell.execute_reply":"2024-09-15T12:18:03.439963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgt1 = df[df[\"series_description\"] == \"Sagittal T1\"]\nsgt1 = sgt1[sgt1[\"study_id\"].isin(target[\"study_id\"])] \npath = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\"\nunique = np.unique(sgt1[\"study_id\"])\nsgt1 = [list(map(lambda x: path + \"/\" + str(i) + \"/\" + str(x), sgt1[sgt1[\"study_id\"] == i][\"series_id\"])) for i in unique]\nsgt1 = [list(map(lambda x: [x + \"/\" + z for z in os.listdir(x)], i)) for i in sgt1]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:07.354520Z","iopub.execute_input":"2024-09-15T12:18:07.354970Z","iopub.status.idle":"2024-09-15T12:18:09.124212Z","shell.execute_reply.started":"2024-09-15T12:18:07.354934Z","shell.execute_reply":"2024-09-15T12:18:09.122951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgt1_i = [[iii for ii in i for iii in ii] for i in sgt1]\nsgt1_i = [resize_and_split(i) for i in sgt1_i]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:09.126124Z","iopub.execute_input":"2024-09-15T12:18:09.127090Z","iopub.status.idle":"2024-09-15T12:18:09.148691Z","shell.execute_reply.started":"2024-09-15T12:18:09.127054Z","shell.execute_reply":"2024-09-15T12:18:09.147165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgt2 = df[df[\"series_description\"] == \"Axial T2\"]\nsgt2 = sgt2[sgt2[\"study_id\"].isin(target[\"study_id\"])] \npath = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\"\nunique = np.unique(sgt2[\"study_id\"])\nsgt2 = [list(map(lambda x: path + \"/\" + str(i) + \"/\" + str(x), sgt2[sgt2[\"study_id\"] == i][\"series_id\"])) for i in unique]\nsgt2 = [list(map(lambda x: [x + \"/\" + z for z in os.listdir(x)], i)) for i in sgt2]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:10.287352Z","iopub.execute_input":"2024-09-15T12:18:10.287773Z","iopub.status.idle":"2024-09-15T12:18:12.413330Z","shell.execute_reply.started":"2024-09-15T12:18:10.287740Z","shell.execute_reply":"2024-09-15T12:18:12.412130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgt2_i = [[iii for ii in i for iii in ii] for i in sgt2]\nsgt2_i = [resize_and_split(i) for i in sgt2_i]","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:12.415497Z","iopub.execute_input":"2024-09-15T12:18:12.415894Z","iopub.status.idle":"2024-09-15T12:18:12.499599Z","shell.execute_reply.started":"2024-09-15T12:18:12.415861Z","shell.execute_reply":"2024-09-15T12:18:12.498410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value = [[axial_t2_i[i], sgt1_i[i], sgt2_i[i]] for i in range(len(sgt2_i))]\nvalue = [iii for i in value for ii in i for iii in ii]\nvalue = np.reshape(value, (-1, 120))","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:13.018128Z","iopub.execute_input":"2024-09-15T12:18:13.018544Z","iopub.status.idle":"2024-09-15T12:18:13.229809Z","shell.execute_reply.started":"2024-09-15T12:18:13.018509Z","shell.execute_reply":"2024-09-15T12:18:13.228586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:18:14.456689Z","iopub.execute_input":"2024-09-15T12:18:14.457064Z","iopub.status.idle":"2024-09-15T12:18:14.465371Z","shell.execute_reply.started":"2024-09-15T12:18:14.457034Z","shell.execute_reply":"2024-09-15T12:18:14.464174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport numpy as np\ndef convert_png(x):\n    ds = pydicom.dcmread(x)\n    out = ds.pixel_array\n    out = apply_voi_lut(out, ds, index=0)\n    out = cv2.resize(out, (CFG.IMAGE_SIZE, CFG.IMAGE_SIZE), interpolation = cv2.INTER_CUBIC)\n    out = ((out - out.min()) / (out.max() - out.min() + 1e-6) * 255).astype(np.uint8)\n    return out","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:20:15.534768Z","iopub.execute_input":"2024-09-15T12:20:15.535296Z","iopub.status.idle":"2024-09-15T12:20:15.545847Z","shell.execute_reply.started":"2024-09-15T12:20:15.535247Z","shell.execute_reply":"2024-09-15T12:20:15.544089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-09-15T12:20:15.665145Z","iopub.execute_input":"2024-09-15T12:20:15.666879Z","iopub.status.idle":"2024-09-15T12:20:15.673150Z","shell.execute_reply.started":"2024-09-15T12:20:15.666809Z","shell.execute_reply":"2024-09-15T12:20:15.671757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compute(idx, directory):\n    value = []\n    for i in directory:\n        if i == \"\":\n            value.append(np.zeros((CFG.IMAGE_SIZE, CFG.IMAGE_SIZE)))\n            continue\n        else:\n            value.append(convert_png(i))\n    value = np.transpose(np.array(value), (2, 1, 0))\n    assert value.shape[2] == 120\n    np.savez_compressed(f\"{idx}.npz\", value)\nParallel(n_jobs=-1)(delayed(compute)(idx, directory) for idx, directory in enumerate(tqdm(value[0:1790//2])))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-09-15T12:22:05.752394Z","iopub.execute_input":"2024-09-15T12:22:05.752827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"yes_sir\")","metadata":{},"execution_count":null,"outputs":[]}]}