{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":99552,"databundleVersionId":13190393,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import kaggle_evaluation.rsna_inference_server\nfrom collections import defaultdict\nimport pydicom, shutil, os\nimport pandas as pd\nimport polars as pl\nimport numpy as np\nfrom sklearn import *\n\np = '/kaggle/input/rsna-intracranial-aneurysm-detection/'\ntrain = pd.read_csv(p+'train.csv')\ntrainl = pd.read_csv(p+'train_localizers.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-28T22:56:41.397656Z","iopub.execute_input":"2025-07-28T22:56:41.398071Z","iopub.status.idle":"2025-07-28T22:56:41.435335Z","shell.execute_reply.started":"2025-07-28T22:56:41.398041Z","shell.execute_reply":"2025-07-28T22:56:41.434408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ID_COL = 'SeriesInstanceUID'\n\nLABEL_COLS = [\n    'Left Infraclinoid Internal Carotid Artery',\n    'Right Infraclinoid Internal Carotid Artery',\n    'Left Supraclinoid Internal Carotid Artery',\n    'Right Supraclinoid Internal Carotid Artery',\n    'Left Middle Cerebral Artery',\n    'Right Middle Cerebral Artery',\n    'Anterior Communicating Artery',\n    'Left Anterior Cerebral Artery',\n    'Right Anterior Cerebral Artery',\n    'Left Posterior Communicating Artery',\n    'Right Posterior Communicating Artery',\n    'Basilar Tip',\n    'Other Posterior Circulation',\n    'Aneurysm Present',\n]\n\nDICOM_TAG_ALLOWLIST = [\n    'BitsAllocated',\n    'BitsStored',\n    'Columns',\n    'FrameOfReferenceUID',\n    'HighBit',\n    'ImageOrientationPatient',\n    'ImagePositionPatient',\n    'InstanceNumber',\n    'Modality',\n    'PatientID',\n    'PhotometricInterpretation',\n    'PixelRepresentation',\n    'PixelSpacing',\n    'PlanarConfiguration',\n    'RescaleIntercept',\n    'RescaleSlope',\n    'RescaleType',\n    'Rows',\n    'SOPClassUID',\n    'SOPInstanceUID',\n    'SamplesPerPixel',\n    'SliceThickness',\n    'SpacingBetweenSlices',\n    'StudyInstanceUID',\n    'TransferSyntaxUID',\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-28T22:56:41.436773Z","iopub.execute_input":"2025-07-28T22:56:41.437093Z","iopub.status.idle":"2025-07-28T22:56:41.4432Z","shell.execute_reply.started":"2025-07-28T22:56:41.437062Z","shell.execute_reply":"2025-07-28T22:56:41.442321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"means = train[LABEL_COLS].mean().to_dict()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-28T22:56:41.444236Z","iopub.execute_input":"2025-07-28T22:56:41.444591Z","iopub.status.idle":"2025-07-28T22:56:41.465755Z","shell.execute_reply.started":"2025-07-28T22:56:41.444545Z","shell.execute_reply":"2025-07-28T22:56:41.464792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(series_path: str):\n    series_id = os.path.basename(series_path)\n    #all_filepaths = []\n    #for root, _, files in os.walk(series_path):\n        #for file in files:\n            #if file.endswith('.dcm'):\n                #all_filepaths.append(os.path.join(root, file))\n    #all_filepaths.sort()\n    #tags = defaultdict(list)\n    #tags['SeriesInstanceUID'] = series_id\n    #global dcms\n    #for filepath in all_filepaths:\n        #ds = pydicom.dcmread(filepath, force=True)\n        #tags['filepath'].append(filepath)\n        #for tag in DICOM_TAG_ALLOWLIST:\n            #tags[tag].append(getattr(ds, tag, None))\n    predictions = pl.DataFrame(data=[[series_id] + [means[k] for k in LABEL_COLS]], schema=[ID_COL, *LABEL_COLS], orient='row',)\n    if isinstance(predictions, pl.DataFrame):\n        assert predictions.columns == [ID_COL, *LABEL_COLS]\n    elif isinstance(predictions, pd.DataFrame):\n        assert (predictions.columns == [ID_COL, *LABEL_COLS]).all()\n    else:\n        raise TypeError('The predict function must return a DataFrame')\n    shutil.rmtree('/kaggle/shared', ignore_errors=True)\n    return predictions.drop(ID_COL)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-28T22:56:41.467412Z","iopub.execute_input":"2025-07-28T22:56:41.467728Z","iopub.status.idle":"2025-07-28T22:56:41.483991Z","shell.execute_reply.started":"2025-07-28T22:56:41.467698Z","shell.execute_reply":"2025-07-28T22:56:41.482966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.rsna_inference_server.RSNAInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway()\n    display(pl.read_parquet('/kaggle/working/submission.parquet'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-28T22:56:41.484982Z","iopub.execute_input":"2025-07-28T22:56:41.485223Z","iopub.status.idle":"2025-07-28T22:56:44.974402Z","shell.execute_reply.started":"2025-07-28T22:56:41.485204Z","shell.execute_reply":"2025-07-28T22:56:44.973608Z"}},"outputs":[],"execution_count":null}]}