{"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":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"},{"sourceId":89324,"sourceType":"modelInstanceVersion","modelInstanceId":74932,"modelId":99654}],"dockerImageVersionId":30761,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport glob\nimport os\nimport polars as pl\n# import duckdb as dd\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport cv2\nfrom pydicom import dcmread\nimport warnings\nfrom sklearn.preprocessing import LabelEncoder\nimport pickle\nimport gc\nimport ctypes\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport tensorflow_io as tfio\nfrom tensorflow import keras\nfrom tensorflow.python.keras import backend as K\nfrom joblib import Parallel, delayed","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-23T12:07:35.841033Z","iopub.execute_input":"2024-08-23T12:07:35.841487Z","iopub.status.idle":"2024-08-23T12:07:53.766717Z","shell.execute_reply.started":"2024-08-23T12:07:35.841431Z","shell.execute_reply":"2024-08-23T12:07:53.765698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Test = False\nconfig = {}\n\nif Test:\n    config['root_file_path'] = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\n    config['start'] = 400\n    config['end'] = 800\n    studies = os.listdir(config['root_file_path'])[config['start']:config['end']]\n    test_dict = {}\nelse:\n    config['root_file_path'] = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/test_images/'\n    studies = os.listdir(config['root_file_path'])\n    test_dict = {}\n    \nfor study in studies:\n    image_files = []\n    for dirname, _, filenames in os.walk(config['root_file_path']+'/'+study):\n        for filename in filenames:\n            test_dict[os.path.join(dirname, filename).split('/')[-3]] = image_files\n            image_files.append(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:07:58.569351Z","iopub.execute_input":"2024-08-23T12:07:58.570111Z","iopub.status.idle":"2024-08-23T12:07:58.620068Z","shell.execute_reply.started":"2024-08-23T12:07:58.570065Z","shell.execute_reply":"2024-08-23T12:07:58.618690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from multiprocessing import cpu_count\nn_cores = cpu_count()\nprint(f'Number of Logical CPU cores: {n_cores}')","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:00.728827Z","iopub.execute_input":"2024-08-23T12:08:00.729264Z","iopub.status.idle":"2024-08-23T12:08:00.736192Z","shell.execute_reply.started":"2024-08-23T12:08:00.729223Z","shell.execute_reply":"2024-08-23T12:08:00.734662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.\\\nload_model(\"/kaggle/input/keras_base_lnfn_l1_l2/keras/default/1/keras_base_left_neural_foraminal_narrowing_l1_l2.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:01.828946Z","iopub.execute_input":"2024-08-23T12:08:01.829419Z","iopub.status.idle":"2024-08-23T12:08:02.017124Z","shell.execute_reply.started":"2024-08-23T12:08:01.829375Z","shell.execute_reply":"2024-08-23T12:08:02.015823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/sample_submission.csv')\nsubmission['row_id'] = 'samples'","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:03.239046Z","iopub.execute_input":"2024-08-23T12:08:03.239486Z","iopub.status.idle":"2024-08-23T12:08:03.261879Z","shell.execute_reply.started":"2024-08-23T12:08:03.239443Z","shell.execute_reply":"2024-08-23T12:08:03.260661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_feature_array(img_file_list):\n    dicom_ds = dcmread(img_file_list)\n    img_array = dicom_ds.pixel_array\n    return np.resize(np.mean(img_array.T, axis=0),(128,))\n\nvfunc = np.vectorize(get_feature_array, otypes=[object])\n\ndef get_predictions(key, model_to_use):\n    final_feature_list = vfunc(test_dict[key]).tolist()\n    final = np.array(final_feature_list)\n    return model_to_use.predict(final)\n\ny_proba = (Parallel(n_jobs=4)(delayed(get_predictions)(st, model) for st in tqdm(test_dict.keys())))","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:17.859251Z","iopub.execute_input":"2024-08-23T12:08:17.859704Z","iopub.status.idle":"2024-08-23T12:08:26.577485Z","shell.execute_reply.started":"2024-08-23T12:08:17.859665Z","shell.execute_reply":"2024-08-23T12:08:26.575757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = {}\n\nfor i in range(len(y_proba)):\n    rows[list(test_dict.keys())[i]+'_left_neural_foraminal_narrowing_l1_l2'] = np.max(y_proba[i], axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:37.392136Z","iopub.execute_input":"2024-08-23T12:08:37.392606Z","iopub.status.idle":"2024-08-23T12:08:37.401350Z","shell.execute_reply.started":"2024-08-23T12:08:37.392561Z","shell.execute_reply":"2024-08-23T12:08:37.400025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#rows = {}\n# weight_dict = {'normal_mild':1, 'moderate':2, 'severe':4}\nconditions = ['spinal_canal_stenosis', 'neural_foraminal_narrowing', 'subarticular_stenosis']\nsides = ['left', 'right']\nvertebrae_levels = ['l1_l2', 'l2_l3', 'l3_l4', 'l4_l5', 'l5_s1']\nseverity_levels = ['normal_mild', 'moderate', 'severe']\n\nfor c in conditions:\n    for v in vertebrae_levels:\n        if c != 'spinal_canal_stenosis':\n            for s in sides:\n                for st in test_dict.keys():\n                    if s+'_'+c+'_'+v == 'left_neural_foraminal_narrowing_l1_l2':\n                        #print(\"going for model \", st+'_'+s+'_'+c+'_'+v)\n                        pass\n                    else:\n                        #print(st+'_'+s+'_'+c+'_'+v)\n                        rows[st+'_'+s+'_'+c+'_'+v] = np.array([0.333333, 0.333333, 0.333333])\n        else:\n            for st in test_dict.keys():\n                #print(st+'_'+c+'_'+v)\n                rows[st+'_'+c+'_'+v] = np.array([0.333333, 0.333333, 0.333333])","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:41.636202Z","iopub.execute_input":"2024-08-23T12:08:41.636683Z","iopub.status.idle":"2024-08-23T12:08:41.654872Z","shell.execute_reply.started":"2024-08-23T12:08:41.636641Z","shell.execute_reply":"2024-08-23T12:08:41.653014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for row_id, feature in tqdm(rows.items()):\n    feature_set_reshaped = feature.reshape(1, -1)\n    predictions = np.ascontiguousarray(feature_set_reshaped)\n    df = pd.DataFrame(predictions, columns=severity_levels)\n    df.insert(loc=0, column='row_id', value=row_id)\n    submission = pd.concat([submission,df]).reset_index(drop=True)\n    \ni = submission[(submission.row_id == 'samples')].index\nsubmission = submission.drop(i).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:08:44.068264Z","iopub.execute_input":"2024-08-23T12:08:44.069046Z","iopub.status.idle":"2024-08-23T12:08:44.116729Z","shell.execute_reply.started":"2024-08-23T12:08:44.068998Z","shell.execute_reply":"2024-08-23T12:08:44.115356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T12:09:06.323685Z","iopub.execute_input":"2024-08-23T12:09:06.324126Z","iopub.status.idle":"2024-08-23T12:09:06.335188Z","shell.execute_reply.started":"2024-08-23T12:09:06.324086Z","shell.execute_reply":"2024-08-23T12:09:06.333820Z"},"trusted":true},"execution_count":null,"outputs":[]}]}