{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This kernel uses the model to predict the test set and write results to submission.csv\n\nThis is part of a larger solution found at: https://www.kaggle.com/ohbewise/a-rsna-mri-solution-from-dicom-to-submission","metadata":{}},{"cell_type":"code","source":"!pip install --quiet --no-index --find-links ../input/pip-download-torchio/ --requirement ../input/pip-download-torchio/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2021-09-28T01:35:22.504164Z","iopub.execute_input":"2021-09-28T01:35:22.504861Z","iopub.status.idle":"2021-09-28T01:35:35.801811Z","shell.execute_reply.started":"2021-09-28T01:35:22.504735Z","shell.execute_reply":"2021-09-28T01:35:35.800715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport csv\nimport nibabel as nib\nimport torchio as tio\nimport tensorflow as tf\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2021-09-28T01:35:35.803672Z","iopub.execute_input":"2021-09-28T01:35:35.803968Z","iopub.status.idle":"2021-09-28T01:35:43.316393Z","shell.execute_reply.started":"2021-09-28T01:35:35.803935Z","shell.execute_reply":"2021-09-28T01:35:43.315196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# functions\ndef read_nifti_file(filepath):\n    \"\"\"Read and load volume\"\"\"\n    # Read file\n    scan = nib.load(filepath)\n    # Get raw data\n    scan = scan.get_fdata()\n    return scan\n\ndef add_batch_channel(volume):\n    \"\"\"Process validation data by adding a channel.\"\"\"\n    volume = tf.expand_dims(volume, axis=-1)\n    volume = tf.expand_dims(volume, axis=0)\n    return volume\n\ndef process_scan(filepath):\n    scan = read_nifti_file(filepath)\n    volume = add_batch_channel(scan)\n    return volume","metadata":{"execution":{"iopub.status.busy":"2021-09-28T01:35:43.318128Z","iopub.execute_input":"2021-09-28T01:35:43.318479Z","iopub.status.idle":"2021-09-28T01:35:43.325860Z","shell.execute_reply.started":"2021-09-28T01:35:43.318447Z","shell.execute_reply":"2021-09-28T01:35:43.324803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set up directories\ndata_dir   = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/'\ntest_dir   = f'{data_dir}test'\npatients = os.listdir(test_dir)\nprint(f'Total patients: {len(patients)}\\n\\n')\n\nout_dir  = '/kaggle/working/processed'\n\nscan_types = ['FLAIR', 'T1w', 'T1wCE', 'T2w']\nscan_types = ['T1wCE']\n\nfor scan_type in scan_types:\n    f = open(f'/kaggle/working/submission.csv', 'w')\n    writer = csv.writer(f)\n    writer.writerow(['BraTS21ID','MGMT_value'])\n    for patient in patients:\n        # dicom to nifiti\n        scan_src  = f'{test_dir}/{patient}/{scan_type}/'\n        scan_dest = f'{out_dir}/test/{patient}/{scan_type}/'\n        Path(scan_dest).mkdir(parents=True, exist_ok=True)\n        image = tio.ScalarImage(scan_src)  # subclass of Image\n        transforms = [\n            tio.ToCanonical(),\n            tio.Resample(1),\n            tio.ZNormalization(masking_method=tio.ZNormalization.mean),\n            tio.CropOrPad((128,128,64)),\n            tio.RescaleIntensity((-1, 1)),\n        ]\n        transform = tio.Compose(transforms)\n        preprocessed = transform(image)\n        filepath = f'{scan_dest}/{scan_type}.nii.gz'\n        preprocessed.save(filepath)\n        \n        # process_scan\n        case = process_scan(filepath)\n\n        # tf model\n        model = tf.keras.models.load_model(f'../input/dataset-to-model-with-tensorflow/models/{scan_type}/')\n\n        # get prediction\n        prediction = model.predict(case)\n        \n        # write prediction\n        print(f'{patient},{prediction[0][0]}')\n        writer.writerow([patient, prediction[0][0]])\n\n    f.close()   ","metadata":{"execution":{"iopub.status.busy":"2021-09-28T01:35:43.327422Z","iopub.execute_input":"2021-09-28T01:35:43.327726Z","iopub.status.idle":"2021-09-28T01:36:13.546490Z","shell.execute_reply.started":"2021-09-28T01:35:43.327697Z","shell.execute_reply":"2021-09-28T01:36:13.544575Z"},"trusted":true},"execution_count":null,"outputs":[]}]}