{"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":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport nibabel as nib0\nfrom scipy import ndimage","metadata":{"execution":{"iopub.status.busy":"2021-08-24T06:47:48.455644Z","iopub.execute_input":"2021-08-24T06:47:48.455987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = \"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/\"\ntest_images_dir = input_path + 'stage_2_test/'\ntrain_images_dir = input_path + 'stage_2_train/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_nifti_file(input_path):\n    \"\"\"Read and load volume\"\"\"\n    # Read file\n    scan = nib.load(input_path)\n    # Get raw data\n    scan = scan.get_fdata()\n    return scan","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize(volume):\n    \"\"\"Normalize the volume\"\"\"\n    min = -1000\n    max = 400\n    volume[volume < min] = min\n    volume[volume > max] = max\n    volume = (volume - min) / (max - min)\n    volume = volume.astype(\"float32\")\n    return volume","metadata":{"execution":{"iopub.status.busy":"2021-08-24T06:47:43.113705Z","iopub.status.idle":"2021-08-24T06:47:43.114301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_volume(img):\n    \"\"\"Resize across z-axis\"\"\"\n    # Set the desired depth\n    desired_depth = 64\n    desired_width = 128\n    desired_height = 128\n    # Get current depth\n    current_depth = img.shape[-1]\n    current_width = img.shape[0]\n    current_height = img.shape[1]\n    # Compute depth factor\n    depth = current_depth / desired_depth\n    width = current_width / desired_width\n    height = current_height / desired_height\n    depth_factor = 1 / depth\n    width_factor = 1 / width\n    height_factor = 1 / height\n    # Rotate\n    img = ndimage.rotate(img, 90, reshape=False)\n    # Resize across z-axis\n    img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-08-24T06:47:43.115425Z","iopub.status.idle":"2021-08-24T06:47:43.116036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_scan(input_path):\n    \"\"\"Read and resize volume\"\"\"\n    # Read scan\n    volume = read_nifti_file(input_path)\n    # Normalize\n    volume = normalize(volume)\n    # Resize width, height and depth\n    volume = resize_volume(volume)\n    return volume","metadata":{"execution":{"iopub.status.busy":"2021-08-24T06:47:43.117194Z","iopub.status.idle":"2021-08-24T06:47:43.117745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_scan_paths = [\n    os.path.join(os.getcwd(), \"../input/rsna-intracranial-hemorrhage-detection\", x)\n    for x in os.listdir(\"../input/rsna-intracranial-hemorrhage-detection\")","metadata":{"execution":{"iopub.status.busy":"2021-08-24T06:47:43.118792Z","iopub.status.idle":"2021-08-24T06:47:43.1194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}