{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7365193,"sourceType":"datasetVersion","datasetId":4278708}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Testing the Scoring System\n\nA minimal example with all the functionality necessary to receive a score for the blood vessel segmentation challenge","metadata":{}},{"cell_type":"markdown","source":"## Step 1: Configure the Script","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport os\nfrom skimage.filters import threshold_otsu\nfrom skimage.measure import label\nfrom skimage import morphology\nfrom scipy import ndimage\nfrom tqdm import tqdm\nimport gc\nimport pandas as pd\nfrom glob import glob","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-09T15:25:15.914304Z","iopub.execute_input":"2024-01-09T15:25:15.914817Z","iopub.status.idle":"2024-01-09T15:25:15.922194Z","shell.execute_reply.started":"2024-01-09T15:25:15.914787Z","shell.execute_reply":"2024-01-09T15:25:15.920908Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_submission = len(glob(\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/*.tif\"))!=3\n# is_submission = True","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2024-01-09T15:25:15.924473Z","iopub.execute_input":"2024-01-09T15:25:15.925096Z","iopub.status.idle":"2024-01-09T15:25:15.944663Z","shell.execute_reply.started":"2024-01-09T15:25:15.925060Z","shell.execute_reply":"2024-01-09T15:25:15.944036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 2: Define Helpful Subroutines\n\nWe define the following subroutines here:\n* timeit: a decorator function that measures the execution time of a method.\n* garbage_collect: a decorator that performs garbage collection before and after the decorated function to try saving precious RAM\n* load_images_to_3d_array: a function that creates a numpy.ndarray object with shape `(num_images, height, width)` with all the images from a folder loaded.\n* rle_encode: run length encoding a labelled image in the form of a numpy.array ([source](https://www.kaggle.com/stainsby/fast-tested-rle))","metadata":{}},{"cell_type":"code","source":"# A timing function decorator\ndef timeit(method):\n    \"\"\"\n    Decorator function that measures the execution time of a method.\n\n    Args:\n        method: The method to be timed.\n\n    Returns:\n        The timed method.\n    \"\"\"\n    import time\n\n    def timed(*args, **kwargs):\n        start = time.time()\n        result = method(*args, **kwargs)\n        end = time.time()\n\n        print(f'{method.__name__} took {end-start:.2} seconds.', flush=True)\n        return result\n\n    return timed\n\n# A decorator that performs garbage collection before and after the decorated function\ndef garbage_collect(method):\n    \"\"\"\n    Decorator function that performs garbage collection before and after the decorated function.\n\n    Args:\n        method: The method to be decorated.\n\n    Returns:\n        The decorated method.\n    \"\"\"\n    def wrapper(*args, **kwargs):\n        gc.collect()\n        result = method(*args, **kwargs)\n        gc.collect()\n        return result\n\n    return wrapper\n\n@garbage_collect\ndef load_images_to_3d_array(folder_path: str) -> np.ndarray:\n    \"\"\"\n    Assuming the folder contains images named as follows:\n    0000.tif\n    0001.tif\n    ...\n    NNNN.tif\n\n    Parameters:\n    image_folder (str): The path to the folder containing the images.\n\n    Returns:\n    list: A numpy.ndarray object with shape (num_images, height, width) with all the images loaded.\n    \"\"\"\n\n    # List all tif files in the folder\n    file_names = sorted([f for f in os.listdir(folder_path) if f.endswith('.tif')])\n    num_images = len(file_names)\n\n    # Load the first image to get dimensions\n    image_path = os.path.join(folder_path, file_names[0])\n    image = Image.open(image_path)\n    width, height = image.size\n\n    # Preallocate 3D numpy array\n    images = np.zeros((num_images, height, width), dtype=np.array(image).dtype)\n    print(f\"Image type: {images.dtype}\")\n    print(f\"Image shape: {height} rows x {width} columns\")\n\n    # Load images into the array\n    for i, file_name in enumerate(tqdm(file_names, desc=\"Loading images\", dynamic_ncols=True, miniters=1)):\n        image_path = os.path.join(folder_path, file_name)\n        # Load the image\n        image = Image.open(image_path)\n        images[i] = np.array(image)\n        del image\n        \n    \n    print(f\"Volume type: {images.dtype}\")\n    print(f\"Volume shape: {images.shape}\")\n\n    if images.dtype == np.uint16:\n        np.right_shift(images, 8, out=images)\n        images_reduced_size = images.astype(np.uint8)\n        del images\n        return images_reduced_size\n\n    return images\n\n# Same as rle_encode from the Surface Dice Metric script (in Step 5)\n# with an addition to satisfy a competition requirement\n# \n# Adapted from source: https://www.kaggle.com/code/paulorzp/run-length-encode-and-decode\ndef rle_encode(mask):\n    pixel = mask.flatten()\n    pixel = np.concatenate([[0], pixel, [0]])\n    run = np.where(pixel[1:] != pixel[:-1])[0] + 1\n    run[1::2] -= run[::2]\n    rle = ' '.join(str(r) for r in run)\n    # Below is a custom addition because the competition overview states:\n    # Represent the RLE for an empty mask as 1 0.\n    # (www.kaggle.com/competitions/blood-vessel-segmentation/overview/evaluation)\n    if rle == '':\n        rle = '1 0'\n    return rle","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-09T15:25:15.946200Z","iopub.execute_input":"2024-01-09T15:25:15.946748Z","iopub.status.idle":"2024-01-09T15:25:15.963322Z","shell.execute_reply.started":"2024-01-09T15:25:15.946716Z","shell.execute_reply":"2024-01-09T15:25:15.962231Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 3: Define the Process for Processing Images\nWe create two methods in this section:\n* `process_folder_dummy`: creates a set of labeled images in the form of a `numpy.ndarray` object with shape `(num_images, height, width)`\n* `main`: applies `process_folder_dummy` to a set of folders and saves the rle encoded result into `submission.csv`","metadata":{}},{"cell_type":"code","source":"@garbage_collect\n@timeit\ndef process_folder_dummy(folder_path: str, save_results: bool = True) -> np.ndarray:\n    \"\"\"\n    process_folder_dummy: creates a set of labeled images in the form of a numpy.ndarray object with shape (num_images, height, width)\n    \"\"\"\n    \n    images = load_images_to_3d_array(os.path.join(folder_path, 'images', ''))\n    processed_images = np.ones_like(images, dtype=bool)\n    del images\n#     processed_images[:,0,0] = True\n    return processed_images\n\n\n@timeit\ndef main(dataset_folder: str, datasets: str):\n    \"\"\"\n    For each id in the test set, you must predict rle, \n    a run-length encoded instance segmentation mask, \n    where id represents {dataset}_{slice} for an image \n    with path test/{dataset}/images/{slice}.tif. \n    \n    Represent the RLE for an empty mask as 1 0.\n\n    Your submission should contain a header and have the following format:\n\n    id,rle\n    kidney_5_0,1 1 100 10\n    kidney_5_1,1 1 100 10\n    kidney_6_0,1 0\n    kidney_6_1,1 0\n    ...\n    \"\"\"\n\n    \n    dataset_folders = [os.path.join(dataset_folder, dataset) for dataset in datasets]\n\n    \n    submission_df=[]\n    \n    # For each dataset\n    for dataset_folder in dataset_folders:\n        print(f\"\\n{dataset_folder}\")\n        # Process the folder\n        generated_labels = process_folder_dummy(dataset_folder, save_results=False)\n        print(f\"generated_labels type: {generated_labels.dtype}\")\n        print(f\"generated_labels shape: {generated_labels.shape}\")\n        # For each image\n        for i in tqdm(range(generated_labels.shape[0]), desc=\"Preparing save\"):\n            # Encode the mask\n            submission_df.append(\n                pd.DataFrame(data={\n                    'id'  : f\"{os.path.basename(dataset_folder)}_{i:04}\",\n                    'rle' : rle_encode(generated_labels[i]),\n                },index=[0])\n            )\n            \n        # We're done\n        del generated_labels\n        gc.collect()\n        \n    # End of dataset folder\n    \n    \n    print(f\"\\n\\n\\nNo more folders to process.\")\n    print(f\"Saving processed data to submission.csv...\")\n    submission_df_pd = pd.concat(submission_df)\n    del submission_df\n    submission_df_pd.to_csv('submission.csv', index=False, mode = 'w')\n    del submission_df_pd\n    print(f\"Saved processed data\")\n# end of main()","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2024-01-09T15:25:15.964991Z","iopub.execute_input":"2024-01-09T15:25:15.965403Z","iopub.status.idle":"2024-01-09T15:25:15.982276Z","shell.execute_reply.started":"2024-01-09T15:25:15.965366Z","shell.execute_reply":"2024-01-09T15:25:15.981157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 4: Process the images","metadata":{}},{"cell_type":"code","source":"if is_submission:\n    # Path to the main directory\n    main_directory = '/kaggle/input/blood-vessel-segmentation/test'\n\n    # List all subdirectories in the main directory\n    subfolders = [name for name in os.listdir(main_directory) \n                  if os.path.isdir(os.path.join(main_directory, name))]\n    \n    main(main_directory, subfolders)\n\n    \nelse:\n    main('/kaggle/input/blood-vessel-segmentation/train', ['kidney_1_dense', 'kidney_1_voi', 'kidney_2'])","metadata":{"execution":{"iopub.status.busy":"2024-01-09T15:25:15.985777Z","iopub.execute_input":"2024-01-09T15:25:15.986929Z","iopub.status.idle":"2024-01-09T15:30:48.766561Z","shell.execute_reply.started":"2024-01-09T15:25:15.986887Z","shell.execute_reply":"2024-01-09T15:30:48.765710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 5 (optional): Testing","metadata":{}},{"cell_type":"markdown","source":"Here is the code for the Surface Dice Metric used in the Blood Vessel Segmentation project. ([source](https://www.kaggle.com/code/metric/surface-dice-metric/notebook)) ([proof](www.kaggle.com/competitions/blood-vessel-segmentation/overview/evaluation))","metadata":{}},{"cell_type":"code","source":"\"\"\"Surface Dice metric for HuBMAP 4.\"\"\"\n\nimport numpy as np\nimport pandas as pd\nimport pandas.api.types\nfrom numba import jit\nfrom scipy.ndimage import label, generate_binary_structure\nfrom skimage.transform import resize\nfrom typing import Optional, Tuple, Union\n\n\nclass ParticipantVisibleError(Exception):\n    pass\n\n\ndef score(\n    solution: pd.DataFrame,\n    submission: pd.DataFrame,\n    row_id_column_name: str,\n    rle_column_name: str,\n    tolerance: float = 1.0,\n    image_id_column_name: Optional[str] = None,\n    slice_id_column_name: Optional[str] = None,\n    resize_fraction: float = 1.0,\n) -> float:\n    \"\"\"Mean Surface Dice over collections of 2D or 3D data.\n\n    This metric is adapted from the Google DeepMind surface dice metric as found here:\n    https://github.com/google-deepmind/surface-distance/tree/master.\n\n    Can be used with either 2D or 3D data. When used with 3D data, each row in the solution and\n    submission files should contain run-length encoded masks of a width x height slice.\n\n    Parameters\n    ----------\n    solution : Pandas dataframe, the ground truth values.\n\n    submission : Pandas dataframe, the predicted values.\n\n    row_id_column_name : str, the name of the ID column used by Kaggle's preprocessing code to\n        align the solution and submission dataframes.\n\n    rle_column_name : str, the name of column containing run-length encoded masks.\n\n    tolerance : float, the distance, in millimeters, the predicted mask surfaces are allowed to\n        vary from the ground-truth masks.\n\n    image_id_column_name : str (optional), for 3D data, the name of the column identifying\n        the image a slice belongs to.\n\n    slice_id_column_name : str (optional), for 3D data, the name of the column enumerating\n        the slices within each image.\n\n    resize_fraction : float, the fraction by which to resize the decoded masks. Useful for memory\n        efficiency in the case of large images.\n\n    Returns\n    -------\n    mean_surface_dice : float\n\n    Examples\n    --------\n    No groups (2D images).\n    >>> solution = pd.DataFrame({\n    ...     'id': [0, 1],\n    ...     'rle': ['1 12 20 2', '1 6'],\n    ...     'width': [5, 5],\n    ...     'height': [5, 5],\n    ... })\n\n    Perfect submission.\n    >>> submission = pd.DataFrame({\n    ...     'id': [0, 1],\n    ...     'rle': ['1 12 20 2', '1 6'],\n    ... })\n    >>> score(solution, submission, 'id', 'rle', 0.0)\n    1.0\n\n    One group with two slices.\n    >>> solution = pd.DataFrame({\n    ...     'id': [0, 1],\n    ...     'rle': ['1 12 20 2', '1 6'],\n    ...     'width': [5, 5],\n    ...     'height': [5, 5],\n    ...     'group': ['a', 'a'],\n    ...     'slice': [0, 1],\n    ... })\n\n    Perfect submission.\n    >>> submission = pd.DataFrame({\n    ...     'id': [0, 1],\n    ...     'rle': ['1 12 20 2', '1 6'],\n    ... })\n    >>> score(solution, submission, 'id', 'rle', 0.0, 'group', 'slice')\n    1.0\n\n    Null submission.\n    >>> submission = pd.DataFrame({\n    ...     'id': [0, 1],\n    ...     'rle': ['1 0', '1 0'],\n    ... })\n    >>> score(solution, submission, 'id', 'rle', 0.0, 'group', 'slice')\n    0.0\n\n    Two groups with multiple slices.\n    >>> solution = pd.DataFrame({\n    ...     'id': [0, 1, 2, 3, 4],\n    ...     'rle': ['1 12', '1 12 ', '1 12', '20 5', '22 2'],\n    ...     'width': [5, 5, 5, 8, 8],\n    ...     'height': [5, 5, 5, 4, 4],\n    ...     'group': ['a', 'a', 'a', 'b', 'b'],\n    ...     'slice': [0, 1, 2, 0, 1],\n    ... })\n    >>> submission = pd.DataFrame({\n    ...     'id': [0, 1, 2, 3, 4],\n    ...     'rle': ['1 12', '1 12 ', '1 12', '20 5', '22 2'],\n    ... })\n    >>> score(solution, submission, 'id', 'rle', 0.0, 'group', 'slice')\n    1.0\n\n    >>> submission = pd.DataFrame({\n    ...     'id': [0, 1, 2, 3, 4],\n    ...     'rle': ['1 11', '1 12 ', '1 13', '20 4', '22 3'],\n    ... })\n\n    With non-zero tolerance.\n    >>> score(solution, submission, 'id', 'rle', 5.0, 'group', 'slice')\n    1.0\n\n    With empty masks.\n    >>> submission = pd.DataFrame({\n    ...     'id': [0, 1, 2, 3, 4],\n    ...     'rle': ['1 0', '1 0 ', '1 0', '1 0', '1 0'],\n    ... })\n    >>> score(solution, submission, 'id', 'rle', 0.0, 'group', 'slice')\n    0.0\n    \n    Cubes, adapted from https://github.com/google-deepmind/surface-distance/blob/master/surface_distance_test.py#L307\n    >>> solution = pd.DataFrame({\n    ...     'id': np.arange(100),\n    ...     'rle': ['1 10000' if k <= 49 else '' for k in np.arange(100)],\n    ...     'width': [100] * 100,\n    ...     'height': [100] * 100,\n    ...     'group': ['a'] * 100,\n    ...     'slice': np.arange(100),\n    ... })\n    >>> submission = pd.DataFrame({\n    ...     'id': np.arange(100),\n    ...     'rle': ['1 10000' if k <= 50 else '' for k in np.arange(100)],\n    ... })\n    >>> score(solution, submission, 'id', 'rle', 0.0, 'group', 'slice')\n    0.750877...\n\n    \"\"\"\n    solution = solution.set_index(row_id_column_name)\n    submission = submission.set_index(row_id_column_name)\n\n    # Check that both are defined or neither are\n    if (image_id_column_name is None) != (slice_id_column_name is None):\n        raise ValueError(\"If one of `image_id_column_name` or `slice_id_column_name` is given, the other must be given also.\")\n\n    if tolerance < 0.0:\n        raise ValueError(\"`tolerance` must be non-negative.\")\n\n    # Setup spacing_mm\n    if image_id_column_name is None:\n        spacing_mm = (1, 1)  # (height, width)\n    else:\n        spacing_mm = (1, 1, 1)  # (height, width, depth)\n\n    # Create joined dataframe to iterate over\n    joined = solution.join(submission,\n                           lsuffix='_sol',\n                           rsuffix='_sub')\n\n    del submission\n\n    # Compute surface dice for each group of slices and total\n    total_dice = 0.0\n    group_col = row_id_column_name if image_id_column_name is None else image_id_column_name\n    for group, df in joined.groupby(group_col):\n        # Make indexing easier\n        df = df.reset_index(drop=True)\n\n        # Check that we're stacking slices in order\n        if image_id_column_name is not None:\n            assert df.loc[:, slice_id_column_name].is_monotonic_increasing\n\n        group_mask_sol, group_mask_sub = [], []\n        height, width = df.loc[0, 'height'], df.loc[0, 'width']\n        assert (df.loc[:, 'height'].nunique() == 1) and (df.loc[:, 'width'].nunique() == 1),\\\n            \"Height and width must be constant within each group.\"\n\n        # Decode slice RLEs to arrays\n        for row in df.itertuples():\n            # Mask transformation\n            shape = (height, width)\n            group_mask_sol.append(make_mask(row.rle_sol, shape, resize_fraction))\n            group_mask_sub.append(make_mask(row.rle_sub, shape, resize_fraction))\n\n        # Stack slices to create 3D arrays\n        group_mask_sol = np.stack(group_mask_sol, axis=-1)\n        assert group_mask_sol.ndim == 3\n        group_mask_sub = np.stack(group_mask_sub, axis=-1)\n        assert group_mask_sub.ndim == 3\n\n        # Minimize dims: 2D or 3D\n        group_mask_sol = group_mask_sol.squeeze()\n        group_mask_sub = group_mask_sub.squeeze()\n\n        # Compute surface distance\n        surface_dist = compute_surface_distances(\n            mask_gt=group_mask_sol,\n            mask_pred=group_mask_sub,\n            spacing_mm=spacing_mm,\n        )\n        # Compute surface dice and add to running total\n        total_dice += compute_surface_dice_at_tolerance(\n            surface_dist,\n            tolerance_mm=tolerance,\n        )\n\n    # Compute mean from running total\n    if image_id_column_name is None:\n        ngroups = len(joined)\n    else:\n        ngroups = joined.loc[:, image_id_column_name].nunique()\n    mean_surface_dice = total_dice / ngroups\n    return mean_surface_dice\n\n\ndef make_mask(rle, shape, resize_fraction):\n    if resize_fraction < 0.0 or resize_fraction > 1.0:\n        raise ValueError(\"`resize_fraction` must be between 0.0 and 1.0, inclusive.\")\n\n    mask = rle_decode(rle, shape)\n    if resize_fraction < 1.0:\n        new_shape = int(shape[0] * resize_fraction), int(shape[1] * resize_fraction)\n        mask = voting_resize(mask, new_shape)\n    mask = mask.astype(bool)\n    return mask\n\n\ndef voting_resize(mask, new_shape):\n    interpolated_mask = resize(mask, new_shape, order=1, preserve_range=True)  # Using bilinear interpolation\n    voting_mask = (interpolated_mask > 0.5).astype(np.uint8)\n    return voting_mask\n\n\n# Below is code from https://www.kaggle.com/code/paulorzp/run-length-encode-and-decode\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef rle_decode(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return\n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [\n        np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])\n    ]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)\n\n\n# Below is adapted from https://github.com/google-deepmind/surface-distance\n\n# Copyright 2018 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS-IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n# surface_distance/lookup_tables.py\n\"\"\"Lookup tables used by surface distance metrics.\"\"\"\n\nimport math\nimport numpy as np\n\nENCODE_NEIGHBOURHOOD_3D_KERNEL = np.array([[[128, 64], [32, 16]],\n                                           [[8, 4], [2, 1]]])\n\n# _NEIGHBOUR_CODE_TO_NORMALS is a lookup table.\n# For every binary neighbour code\n# (2x2x2 neighbourhood = 8 neighbours = 8 bits = 256 codes)\n# it contains the surface normals of the triangles (called \"surfel\" for\n# \"surface element\" in the following). The length of the normal\n# vector encodes the surfel area.\n#\n# created using the marching_cube algorithm\n# see e.g. https://en.wikipedia.org/wiki/Marching_cubes\n# pylint: disable=line-too-long\n_NEIGHBOUR_CODE_TO_NORMALS = [[[0, 0, 0]], [[0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125]],\n                              [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]],\n                              [[0.125, -0.125, 0.125]],\n                              [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25]],\n                              [[0.125, -0.125, 0.125], [-0.125, -0.125,\n                                                        0.125]],\n                              [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25],\n                               [0.125, 0.125, 0.125]], [[-0.125, 0.125,\n                                                         0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, 0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25]],\n                              [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.25, -0.25, 0.0], [0.25, -0.25, 0.0]],\n                              [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25],\n                               [-0.125, 0.125, -0.125]],\n                              [[-0.5, 0.0, 0.0], [-0.25, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[0.5, 0.0, 0.0], [0.5, 0.0, 0.0]],\n                              [[0.125, -0.125, -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25]],\n                              [[-0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, -0.5, 0.0], [0.25, 0.25, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [0.125, -0.125,\n                                                        -0.125]],\n                              [[0.0, 0.0, -0.5], [0.25, 0.25, 0.25],\n                               [-0.125, -0.125, -0.125]],\n                              [[-0.125, -0.125, 0.125], [0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25],\n                               [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125,\n                                                        -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[0.125, 0.125, 0.125], [0.375, 0.375, 0.375],\n                               [0.0, -0.25, 0.25], [-0.25, 0.0, 0.25]],\n                              [[0.125, -0.125, -0.125], [0.25, -0.25, 0.0],\n                               [0.25, -0.25, 0.0]],\n                              [[0.375, 0.375, 0.375], [0.0, 0.25, -0.25],\n                               [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]],\n                              [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125],\n                               [-0.25, -0.25, -0.25], [0.125, 0.125, 0.125]],\n                              [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125],\n                               [-0.25, -0.25, -0.25]], [[0.125, -0.125,\n                                                         0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25]],\n                              [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125],\n                               [0.25, 0.25, -0.25]],\n                              [[0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [-0.25, -0.0, -0.25],\n                               [0.25, 0.0, 0.25]],\n                              [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.375, -0.375, 0.375], [-0.0, 0.25, 0.25],\n                               [0.125, 0.125, -0.125], [-0.25, -0.0, -0.25]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.25, 0.25, -0.25], [0.25, 0.25, -0.25],\n                               [0.125, 0.125, -0.125], [-0.125, -0.125,\n                                                        0.125]],\n                              [[0.125, -0.125, 0.125], [0.25, -0.25, 0.0],\n                               [0.25, -0.25, 0.0]],\n                              [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25],\n                               [-0.125, 0.125, -0.125], [0.125, -0.125,\n                                                         0.125]],\n                              [[0.0, 0.25, -0.25], [0.375, -0.375, -0.375],\n                               [-0.125, 0.125, 0.125], [0.25, 0.25, 0.0]],\n                              [[-0.5, 0.0, 0.0], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0]],\n                              [[0.0, 0.5, 0.0], [-0.25, 0.25, 0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, 0.5, 0.0], [0.125, -0.125, 0.125],\n                               [-0.25, 0.25, -0.25]],\n                              [[0.0, 0.5, 0.0], [0.0, -0.5, 0.0]],\n                              [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.375, -0.375, -0.375], [-0.25, 0.0, 0.25],\n                               [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]],\n                              [[0.125, 0.125, 0.125], [0.0, -0.5, 0.0],\n                               [-0.25, -0.25, -0.25], [-0.125, -0.125,\n                                                       -0.125]],\n                              [[0.0, -0.5, 0.0], [-0.25, -0.25, -0.25],\n                               [-0.125, -0.125, -0.125]],\n                              [[-0.125, 0.125, 0.125], [0.25, -0.25, 0.0],\n                               [-0.25, 0.25, 0.0]],\n                              [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25],\n                               [-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.375, 0.375, -0.375], [-0.25, -0.25, 0.0],\n                               [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]],\n                              [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0],\n                               [0.25, -0.25, 0.0], [0.25, -0.25, 0.0]],\n                              [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.25, -0.25, 0.0],\n                               [-0.25, -0.25, 0.0]],\n                              [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0]],\n                              [[-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [-0.25, -0.25, 0.0],\n                               [0.25, 0.25, -0.0]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25]],\n                              [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.375, -0.375, 0.375], [0.0, -0.25, -0.25],\n                               [-0.125, 0.125, -0.125], [0.25, 0.25, 0.0]],\n                              [[-0.125, -0.125, 0.125], [-0.125, 0.125,\n                                                         0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25],\n                               [-0.25, 0.0, 0.25]],\n                              [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.25, 0.25, -0.25], [-0.25, 0.25, -0.25],\n                               [-0.125, 0.125, -0.125],\n                               [-0.125, 0.125, -0.125]],\n                              [[-0.25, 0.0, -0.25], [0.375, -0.375, -0.375],\n                               [0.0, 0.25, -0.25], [-0.125, 0.125, 0.125]],\n                              [[0.5, 0.0, 0.0], [-0.25, 0.25, -0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]],\n                              [[-0.0, 0.0, 0.5], [-0.25, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25],\n                               [0.25, 0.0, -0.25]],\n                              [[-0.25, -0.0, -0.25], [-0.375, 0.375, 0.375],\n                               [-0.25, -0.25, 0.0], [-0.125, 0.125, 0.125]],\n                              [[0.0, 0.0, -0.5], [0.25, 0.25, -0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.0, 0.0, 0.5], [0.0, 0.0, 0.5]],\n                              [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125],\n                               [0.25, 0.25, 0.25], [0.0, 0.0, 0.5]],\n                              [[0.125, 0.125, 0.125], [0.25, 0.25, 0.25],\n                               [0.0, 0.0, 0.5]],\n                              [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25],\n                               [-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]],\n                              [[0.125, -0.125, 0.125], [0.25, 0.0, 0.25],\n                               [0.25, 0.0, 0.25]],\n                              [[0.25, 0.0, 0.25], [-0.375, -0.375, 0.375],\n                               [-0.25, 0.25, 0.0], [-0.125, -0.125, 0.125]],\n                              [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.25, 0.0, 0.25],\n                               [0.25, 0.0, 0.25]],\n                              [[0.25, 0.0, 0.25], [0.25, 0.0, 0.25]],\n                              [[-0.125, -0.125, 0.125], [0.125, -0.125,\n                                                         0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [0.0, -0.25, 0.25],\n                               [0.0, 0.25, -0.25]],\n                              [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125],\n                               [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25],\n                               [0.0, -0.25, 0.25], [0.0, 0.25, -0.25]],\n                              [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125], [0.125, 0.125, 0.125]],\n                              [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25],\n                               [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, -0.125]],\n                              [[0.5, 0.0, -0.0], [0.25, -0.25, -0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125],\n                               [-0.25, 0.25, 0.25], [0.125, -0.125, -0.125]],\n                              [[0.375, -0.375, 0.375], [0.0, 0.25, 0.25],\n                               [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]],\n                              [[0.0, -0.5, 0.0], [-0.25, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.375, -0.375, 0.375], [0.25, -0.25, 0.0],\n                               [0.0, 0.25, 0.25], [-0.125, -0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [-0.25, 0.25, 0.25],\n                               [0.0, 0.0, 0.5]],\n                              [[0.125, 0.125, 0.125], [0.0, 0.25, 0.25],\n                               [0.0, 0.25, 0.25]],\n                              [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25]],\n                              [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25],\n                               [0.125, 0.125, 0.125], [0.125, 0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [0.125, 0.125, 0.125]],\n                              [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125]], [[0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]],\n                              [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [0.125, 0.125, 0.125]],\n                              [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25],\n                               [0.125, 0.125, 0.125], [0.125, 0.125, 0.125]],\n                              [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25]],\n                              [[0.125, 0.125, 0.125], [0.0, 0.25, 0.25],\n                               [0.0, 0.25, 0.25]],\n                              [[-0.125, 0.125, 0.125], [-0.25, 0.25, 0.25],\n                               [0.0, 0.0, 0.5]],\n                              [[-0.375, -0.375, 0.375], [0.25, -0.25, 0.0],\n                               [0.0, 0.25, 0.25], [-0.125, -0.125, 0.125]],\n                              [[0.0, -0.5, 0.0], [-0.25, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[0.375, -0.375, 0.375], [0.0, 0.25, 0.25],\n                               [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]],\n                              [[-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125],\n                               [-0.25, 0.25, 0.25], [0.125, -0.125, -0.125]],\n                              [[0.5, 0.0, -0.0], [0.25, -0.25, -0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25],\n                               [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125], [0.125, 0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25],\n                               [0.0, 0.25, 0.25], [0.0, 0.25, 0.25]],\n                              [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125],\n                               [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [0.0, -0.25, 0.25],\n                               [0.0, 0.25, -0.25]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [0.125, -0.125,\n                                                         0.125]],\n                              [[0.25, 0.0, 0.25], [0.25, 0.0, 0.25]],\n                              [[0.125, 0.125, 0.125], [0.25, 0.0, 0.25],\n                               [0.25, 0.0, 0.25]],\n                              [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.25, 0.0, 0.25], [-0.375, -0.375, 0.375],\n                               [-0.25, 0.25, 0.0], [-0.125, -0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [0.25, 0.0, 0.25],\n                               [0.25, 0.0, 0.25]],\n                              [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25],\n                               [0.25, 0.0, 0.25], [0.25, 0.0, 0.25]],\n                              [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.25, 0.25, 0.25],\n                               [0.0, 0.0, 0.5]],\n                              [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125],\n                               [0.25, 0.25, 0.25], [0.0, 0.0, 0.5]],\n                              [[-0.0, 0.0, 0.5], [0.0, 0.0, 0.5]],\n                              [[0.0, 0.0, -0.5], [0.25, 0.25, -0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.25, -0.0, -0.25], [-0.375, 0.375, 0.375],\n                               [-0.25, -0.25, 0.0], [-0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25],\n                               [0.25, 0.0, -0.25]],\n                              [[-0.0, 0.0, 0.5], [-0.25, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]],\n                              [[0.5, 0.0, 0.0], [-0.25, 0.25, -0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[-0.25, 0.0, -0.25], [0.375, -0.375, -0.375],\n                               [0.0, 0.25, -0.25], [-0.125, 0.125, 0.125]],\n                              [[-0.25, 0.25, -0.25], [-0.25, 0.25, -0.25],\n                               [-0.125, 0.125, -0.125],\n                               [-0.125, 0.125, -0.125]],\n                              [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25],\n                               [-0.25, 0.0, 0.25]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [-0.125, 0.125,\n                                                         0.125]],\n                              [[0.375, -0.375, 0.375], [0.0, -0.25, -0.25],\n                               [-0.125, 0.125, -0.125], [0.25, 0.25, 0.0]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25]],\n                              [[-0.125, -0.125, 0.125], [-0.25, -0.25, 0.0],\n                               [0.25, 0.25, -0.0]],\n                              [[-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125]],\n                              [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0]],\n                              [[0.125, 0.125, 0.125], [-0.25, -0.25, 0.0],\n                               [-0.25, -0.25, 0.0]],\n                              [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0],\n                               [-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]],\n                              [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.375, 0.375, -0.375], [-0.25, -0.25, 0.0],\n                               [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]],\n                              [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25],\n                               [-0.125, -0.125, 0.125],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.25, -0.25, 0.0],\n                               [-0.25, 0.25, 0.0]],\n                              [[0.0, -0.5, 0.0], [-0.25, -0.25, -0.25],\n                               [-0.125, -0.125, -0.125]],\n                              [[0.125, 0.125, 0.125], [0.0, -0.5, 0.0],\n                               [-0.25, -0.25, -0.25], [-0.125, -0.125,\n                                                       -0.125]],\n                              [[-0.375, -0.375, -0.375], [-0.25, 0.0, 0.25],\n                               [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]],\n                              [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0],\n                               [0.125, -0.125, 0.125]],\n                              [[0.0, 0.5, 0.0], [0.0, -0.5, 0.0]],\n                              [[0.0, 0.5, 0.0], [0.125, -0.125, 0.125],\n                               [-0.25, 0.25, -0.25]],\n                              [[0.0, 0.5, 0.0], [-0.25, 0.25, 0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0]],\n                              [[-0.5, 0.0, 0.0], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.0, 0.25, -0.25], [0.375, -0.375, -0.375],\n                               [-0.125, 0.125, 0.125], [0.25, 0.25, 0.0]],\n                              [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25],\n                               [-0.125, 0.125, -0.125], [0.125, -0.125,\n                                                         0.125]],\n                              [[0.125, -0.125, 0.125], [0.25, -0.25, 0.0],\n                               [0.25, -0.25, 0.0]],\n                              [[0.25, 0.25, -0.25], [0.25, 0.25, -0.25],\n                               [0.125, 0.125, -0.125], [-0.125, -0.125,\n                                                        0.125]],\n                              [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[-0.375, -0.375, 0.375], [-0.0, 0.25, 0.25],\n                               [0.125, 0.125, -0.125], [-0.25, -0.0, -0.25]],\n                              [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25],\n                               [0.125, -0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [-0.25, -0.0, -0.25],\n                               [0.25, 0.0, 0.25]],\n                              [[0.125, -0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125],\n                               [0.25, 0.25, -0.25]],\n                              [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25]],\n                              [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]],\n                              [[0.125, -0.125, 0.125]],\n                              [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125],\n                               [-0.25, -0.25, -0.25]],\n                              [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125],\n                               [-0.25, -0.25, -0.25], [0.125, 0.125, 0.125]],\n                              [[0.375, 0.375, 0.375], [0.0, 0.25, -0.25],\n                               [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]],\n                              [[0.125, -0.125, -0.125], [0.25, -0.25, 0.0],\n                               [0.25, -0.25, 0.0]],\n                              [[0.125, 0.125, 0.125], [0.375, 0.375, 0.375],\n                               [0.0, -0.25, 0.25], [-0.25, 0.0, 0.25]],\n                              [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125], [0.125, -0.125,\n                                                        -0.125]],\n                              [[-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25],\n                               [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125], [0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, 0.0, -0.5], [0.25, 0.25, 0.25],\n                               [-0.125, -0.125, -0.125]],\n                              [[0.125, -0.125, 0.125], [0.125, -0.125,\n                                                        -0.125]],\n                              [[0.0, -0.5, 0.0], [0.25, 0.25, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[-0.125, -0.125, 0.125],\n                               [0.125, -0.125, -0.125]],\n                              [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25]],\n                              [[0.125, -0.125, -0.125]],\n                              [[0.5, 0.0, 0.0], [0.5, 0.0, 0.0]],\n                              [[-0.5, 0.0, 0.0], [-0.25, 0.25, 0.25],\n                               [-0.125, 0.125, 0.125]],\n                              [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25],\n                               [-0.125, 0.125, -0.125]],\n                              [[0.25, -0.25, 0.0], [0.25, -0.25, 0.0]],\n                              [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25],\n                               [-0.125, -0.125, 0.125]],\n                              [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25]],\n                              [[0.125, 0.125, 0.125], [-0.125, 0.125, 0.125]],\n                              [[-0.125, 0.125, 0.125]],\n                              [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25],\n                               [0.125, 0.125, 0.125]],\n                              [[0.125, -0.125, 0.125], [-0.125, -0.125,\n                                                        0.125]],\n                              [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25]],\n                              [[0.125, -0.125, 0.125]],\n                              [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]],\n                              [[-0.125, -0.125, 0.125]],\n                              [[0.125, 0.125, 0.125]], [[0, 0, 0]]]\n# pylint: enable=line-too-long\n\n\ndef create_table_neighbour_code_to_surface_area(spacing_mm):\n    \"\"\"Returns an array mapping neighbourhood code to the surface elements area.\n\n  Note that the normals encode the initial surface area. This function computes\n  the area corresponding to the given `spacing_mm`.\n\n  Args:\n    spacing_mm: 3-element list-like structure. Voxel spacing in x0, x1 and x2\n      direction.\n  \"\"\"\n    # compute the area for all 256 possible surface elements\n    # (given a 2x2x2 neighbourhood) according to the spacing_mm\n    neighbour_code_to_surface_area = np.zeros([256])\n    for code in range(256):\n        normals = np.array(_NEIGHBOUR_CODE_TO_NORMALS[code])\n        sum_area = 0\n        for normal_idx in range(normals.shape[0]):\n            # normal vector\n            n = np.zeros([3])\n            n[0] = normals[normal_idx, 0] * spacing_mm[1] * spacing_mm[2]\n            n[1] = normals[normal_idx, 1] * spacing_mm[0] * spacing_mm[2]\n            n[2] = normals[normal_idx, 2] * spacing_mm[0] * spacing_mm[1]\n            area = np.linalg.norm(n)\n            sum_area += area\n        neighbour_code_to_surface_area[code] = sum_area\n\n    return neighbour_code_to_surface_area.astype(np.float32)\n\n\n# In the neighbourhood, points are ordered: top left, top right, bottom left,\n# bottom right.\nENCODE_NEIGHBOURHOOD_2D_KERNEL = np.array([[8, 4], [2, 1]])\n\n\ndef create_table_neighbour_code_to_contour_length(spacing_mm):\n    \"\"\"Returns an array mapping neighbourhood code to the contour length.\n\n  For the list of possible cases and their figures, see page 38 from:\n  https://nccastaff.bournemouth.ac.uk/jmacey/MastersProjects/MSc14/06/thesis.pdf\n\n  In 2D, each point has 4 neighbors. Thus, are 16 configurations. A\n  configuration is encoded with '1' meaning \"inside the object\" and '0' \"outside\n  the object\". The points are ordered: top left, top right, bottom left, bottom\n  right.\n\n  The x0 axis is assumed vertical downward, and the x1 axis is horizontal to the\n  right:\n   (0, 0) --> (0, 1)\n     |\n   (1, 0)\n\n  Args:\n    spacing_mm: 2-element list-like structure. Voxel spacing in x0 and x1\n      directions.\n  \"\"\"\n    neighbour_code_to_contour_length = np.zeros([16])\n\n    vertical = spacing_mm[0]\n    horizontal = spacing_mm[1]\n    diag = 0.5 * math.sqrt(spacing_mm[0]**2 + spacing_mm[1]**2)\n    # pyformat: disable\n    neighbour_code_to_contour_length[int(\"00\" \"01\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"00\" \"10\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"00\" \"11\", 2)] = horizontal\n\n    neighbour_code_to_contour_length[int(\"01\" \"00\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"01\" \"01\", 2)] = vertical\n\n    neighbour_code_to_contour_length[int(\"01\" \"10\", 2)] = 2 * diag\n\n    neighbour_code_to_contour_length[int(\"01\" \"11\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"10\" \"00\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"10\" \"01\", 2)] = 2 * diag\n\n    neighbour_code_to_contour_length[int(\"10\" \"10\", 2)] = vertical\n\n    neighbour_code_to_contour_length[int(\"10\" \"11\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"11\" \"00\", 2)] = horizontal\n\n    neighbour_code_to_contour_length[int(\"11\" \"01\", 2)] = diag\n\n    neighbour_code_to_contour_length[int(\"11\" \"10\", 2)] = diag\n    # pyformat: enable\n\n    return neighbour_code_to_contour_length.astype(np.float32)\n\n\n# surface_distance/metrics.py\n\"\"\"Module exposing surface distance based measures.\"\"\"\n\nimport numpy as np\nfrom scipy import ndimage\n\n\ndef _assert_is_numpy_array(name, array):\n    \"\"\"Raises an exception if `array` is not a numpy array.\"\"\"\n    if not isinstance(array, np.ndarray):\n        raise ValueError(\"The argument {!r} should be a numpy array, not a \"\n                         \"{}\".format(name, type(array)))\n\n\ndef _check_nd_numpy_array(name, array, num_dims):\n    \"\"\"Raises an exception if `array` is not a `num_dims`-D numpy array.\"\"\"\n    if len(array.shape) != num_dims:\n        raise ValueError(\"The argument {!r} should be a {}D array, not of \"\n                         \"shape {}\".format(name, num_dims, array.shape))\n\n\ndef _check_2d_numpy_array(name, array):\n    _check_nd_numpy_array(name, array, num_dims=2)\n\n\ndef _check_3d_numpy_array(name, array):\n    _check_nd_numpy_array(name, array, num_dims=3)\n\n\ndef _assert_is_bool_numpy_array(name, array):\n    _assert_is_numpy_array(name, array)\n    if array.dtype != bool:\n        raise ValueError(\n            \"The argument {!r} should be a numpy array of type bool, \"\n            \"not {}\".format(name, array.dtype))\n\n\ndef _compute_bounding_box(mask):\n    \"\"\"Computes the bounding box of the masks.\n\n  This function generalizes to arbitrary number of dimensions great or equal\n  to 1.\n\n  Args:\n    mask: The 2D or 3D numpy mask, where '0' means background and non-zero means\n      foreground.\n\n  Returns:\n    A tuple:\n     - The coordinates of the first point of the bounding box (smallest on all\n       axes), or `None` if the mask contains only zeros.\n     - The coordinates of the second point of the bounding box (greatest on all\n       axes), or `None` if the mask contains only zeros.\n  \"\"\"\n    num_dims = len(mask.shape)\n    bbox_min = np.zeros(num_dims, np.int64)\n    bbox_max = np.zeros(num_dims, np.int64)\n\n    # max projection to the x0-axis\n    proj_0 = np.amax(mask, axis=tuple(range(num_dims))[1:])\n    idx_nonzero_0 = np.nonzero(proj_0)[0]\n    if len(idx_nonzero_0) == 0:  # pylint: disable=g-explicit-length-test\n        return None, None\n\n    bbox_min[0] = np.min(idx_nonzero_0)\n    bbox_max[0] = np.max(idx_nonzero_0)\n\n    # max projection to the i-th-axis for i in {1, ..., num_dims - 1}\n    for axis in range(1, num_dims):\n        max_over_axes = list(range(num_dims))  # Python 3 compatible\n        max_over_axes.pop(axis)  # Remove the i-th dimension from the max\n        max_over_axes = tuple(max_over_axes)  # numpy expects a tuple of ints\n        proj = np.amax(mask, axis=max_over_axes)\n        idx_nonzero = np.nonzero(proj)[0]\n        bbox_min[axis] = np.min(idx_nonzero)\n        bbox_max[axis] = np.max(idx_nonzero)\n\n    return bbox_min, bbox_max\n\n\ndef _crop_to_bounding_box(mask, bbox_min, bbox_max):\n    \"\"\"Crops a 2D or 3D mask to the bounding box specified by `bbox_{min,max}`.\"\"\"\n    # we need to zeropad the cropped region with 1 voxel at the lower,\n    # the right (and the back on 3D) sides. This is required to obtain the\n    # \"full\" convolution result with the 2x2 (or 2x2x2 in 3D) kernel.\n    # TODO:  This is correct only if the object is interior to the\n    # bounding box.\n    cropmask = np.zeros((bbox_max - bbox_min) + 2, np.uint8)\n\n    num_dims = len(mask.shape)\n    # pyformat: disable\n    if num_dims == 2:\n        cropmask[0:-1, 0:-1] = mask[bbox_min[0]:bbox_max[0] + 1,\n                                    bbox_min[1]:bbox_max[1] + 1]\n    elif num_dims == 3:\n        cropmask[0:-1, 0:-1, 0:-1] = mask[bbox_min[0]:bbox_max[0] + 1,\n                                          bbox_min[1]:bbox_max[1] + 1,\n                                          bbox_min[2]:bbox_max[2] + 1]\n    # pyformat: enable\n    else:\n        assert False\n\n    return cropmask\n\n\ndef _sort_distances_surfels(distances, surfel_areas):\n    \"\"\"Sorts the two list with respect to the tuple of (distance, surfel_area).\n\n  Args:\n    distances: The distances from A to B (e.g. `distances_gt_to_pred`).\n    surfel_areas: The surfel areas for A (e.g. `surfel_areas_gt`).\n\n  Returns:\n    A tuple of the sorted (distances, surfel_areas).\n  \"\"\"\n    sorted_indices = np.argsort(distances)\n    sorted_distances = distances[sorted_indices]\n    sorted_surfel_areas = surfel_areas[sorted_indices]\n\n    return sorted_distances, sorted_surfel_areas\n\n\ndef compute_surface_distances(mask_gt, mask_pred, spacing_mm):\n    \"\"\"Computes closest distances from all surface points to the other surface.\n\n  This function can be applied to 2D or 3D tensors. For 2D, both masks must be\n  2D and `spacing_mm` must be a 2-element list. For 3D, both masks must be 3D\n  and `spacing_mm` must be a 3-element list. The description is done for the 2D\n  case, and the formulation for the 3D case is present is parenthesis,\n  introduced by \"resp.\".\n\n  Finds all contour elements (resp surface elements \"surfels\" in 3D) in the\n  ground truth mask `mask_gt` and the predicted mask `mask_pred`, computes their\n  length in mm (resp. area in mm^2) and the distance to the closest point on the\n  other contour (resp. surface). It returns two sorted lists of distances\n  together with the corresponding contour lengths (resp. surfel areas). If one\n  of the masks is empty, the corresponding lists are empty and all distances in\n  the other list are `inf`.\n\n  Args:\n    mask_gt: 2-dim (resp. 3-dim) bool Numpy array. The ground truth mask.\n    mask_pred: 2-dim (resp. 3-dim) bool Numpy array. The predicted mask.\n    spacing_mm: 2-element (resp. 3-element) list-like structure. Voxel spacing\n      in x0 anx x1 (resp. x0, x1 and x2) directions.\n\n  Returns:\n    A dict with:\n    \"distances_gt_to_pred\": 1-dim numpy array of type float. The distances in mm\n        from all ground truth surface elements to the predicted surface,\n        sorted from smallest to largest.\n    \"distances_pred_to_gt\": 1-dim numpy array of type float. The distances in mm\n        from all predicted surface elements to the ground truth surface,\n        sorted from smallest to largest.\n    \"surfel_areas_gt\": 1-dim numpy array of type float. The length of the\n      of the ground truth contours in mm (resp. the surface elements area in\n      mm^2) in the same order as distances_gt_to_pred.\n    \"surfel_areas_pred\": 1-dim numpy array of type float. The length of the\n      of the predicted contours in mm (resp. the surface elements area in\n      mm^2) in the same order as distances_gt_to_pred.\n\n  Raises:\n    ValueError: If the masks and the `spacing_mm` arguments are of incompatible\n      shape or type. Or if the masks are not 2D or 3D.\n  \"\"\"\n    # The terms used in this function are for the 3D case. In particular, surface\n    # in 2D stands for contours in 3D. The surface elements in 3D correspond to\n    # the line elements in 2D.\n\n    _assert_is_bool_numpy_array(\"mask_gt\", mask_gt)\n    _assert_is_bool_numpy_array(\"mask_pred\", mask_pred)\n\n    if not len(mask_gt.shape) == len(mask_pred.shape) == len(spacing_mm):\n        raise ValueError(\n            \"The arguments must be of compatible shape. Got mask_gt \"\n            \"with {} dimensions ({}) and mask_pred with {} dimensions \"\n            \"({}), while the spacing_mm was {} elements.\".format(\n                len(mask_gt.shape), mask_gt.shape, len(mask_pred.shape),\n                mask_pred.shape, len(spacing_mm)))\n\n    num_dims = len(spacing_mm)\n    if num_dims == 2:\n        _check_2d_numpy_array(\"mask_gt\", mask_gt)\n        _check_2d_numpy_array(\"mask_pred\", mask_pred)\n\n        # compute the area for all 16 possible surface elements\n        # (given a 2x2 neighbourhood) according to the spacing_mm\n        neighbour_code_to_surface_area = (\n            create_table_neighbour_code_to_contour_length(spacing_mm))\n        kernel = ENCODE_NEIGHBOURHOOD_2D_KERNEL\n        full_true_neighbours = 0b1111\n    elif num_dims == 3:\n        _check_3d_numpy_array(\"mask_gt\", mask_gt)\n        _check_3d_numpy_array(\"mask_pred\", mask_pred)\n\n        # compute the area for all 256 possible surface elements\n        # (given a 2x2x2 neighbourhood) according to the spacing_mm\n        neighbour_code_to_surface_area = (\n            create_table_neighbour_code_to_surface_area(spacing_mm))\n        kernel = ENCODE_NEIGHBOURHOOD_3D_KERNEL\n        full_true_neighbours = 0b11111111\n    else:\n        raise ValueError(\"Only 2D and 3D masks are supported, not \"\n                         \"{}D.\".format(num_dims))\n\n    # compute the bounding box of the masks to trim the volume to the smallest\n    # possible processing subvolume\n    bbox_min, bbox_max = _compute_bounding_box(mask_gt | mask_pred)\n    # Both the min/max bbox are None at the same time, so we only check one.\n    if bbox_min is None:\n        return {\n            \"distances_gt_to_pred\": np.array([]),\n            \"distances_pred_to_gt\": np.array([]),\n            \"surfel_areas_gt\": np.array([]),\n            \"surfel_areas_pred\": np.array([]),\n        }\n\n    # crop the processing subvolume.\n    cropmask_gt = _crop_to_bounding_box(mask_gt, bbox_min, bbox_max)\n    cropmask_pred = _crop_to_bounding_box(mask_pred, bbox_min, bbox_max)\n    del mask_gt, mask_pred\n    \n    # compute the neighbour code (local binary pattern) for each voxel\n    # the resulting arrays are spacially shifted by minus half a voxel in each\n    # axis.\n    # i.e. the points are located at the corners of the original voxels\n    neighbour_code_map_gt = ndimage.correlate(cropmask_gt.astype(np.uint8),\n                                              kernel,\n                                              mode=\"constant\",\n                                              cval=0)\n    neighbour_code_map_pred = ndimage.correlate(cropmask_pred.astype(np.uint8),\n                                                kernel,\n                                                mode=\"constant\",\n                                                cval=0)\n\n    # create masks with the surface voxels\n    borders_gt = ((neighbour_code_map_gt != 0) &\n                  (neighbour_code_map_gt != full_true_neighbours))\n    borders_pred = ((neighbour_code_map_pred != 0) &\n                    (neighbour_code_map_pred != full_true_neighbours))\n\n    # compute the distance transform (closest distance of each voxel to the\n    # surface voxels)\n    if borders_gt.any():\n        distmap_gt = distance_transform_edt(~borders_gt)\n\n    else:\n        distmap_gt = np.Inf * np.ones(borders_gt.shape, np.float32)\n\n    distances_pred_to_gt = distmap_gt[borders_pred]\n    del distmap_gt\n\n    if borders_pred.any():\n        distmap_pred = distance_transform_edt(~borders_pred)\n\n    else:\n        distmap_pred = np.Inf * np.ones(borders_pred.shape, np.float32)\n\n    distances_gt_to_pred = distmap_pred[borders_gt]\n    del distmap_pred\n\n    # compute the area of each surface element\n    surface_area_map_gt = neighbour_code_to_surface_area[neighbour_code_map_gt]\n    surfel_areas_gt = surface_area_map_gt[borders_gt]\n    del neighbour_code_map_gt, surface_area_map_gt, borders_gt\n\n    surface_area_map_pred = neighbour_code_to_surface_area[neighbour_code_map_pred]\n    surfel_areas_pred = surface_area_map_pred[borders_pred]\n    del neighbour_code_map_pred, surface_area_map_pred, borders_pred\n\n    # sort them by distance\n    if distances_gt_to_pred.shape != (0, ):\n        distances_gt_to_pred, surfel_areas_gt = _sort_distances_surfels(\n            distances_gt_to_pred, surfel_areas_gt)\n\n    if distances_pred_to_gt.shape != (0, ):\n        distances_pred_to_gt, surfel_areas_pred = _sort_distances_surfels(\n            distances_pred_to_gt, surfel_areas_pred)\n\n    return {\n        \"distances_gt_to_pred\": distances_gt_to_pred,\n        \"distances_pred_to_gt\": distances_pred_to_gt,\n        \"surfel_areas_gt\": surfel_areas_gt,\n        \"surfel_areas_pred\": surfel_areas_pred,\n    }\n\n\ndef compute_surface_dice_at_tolerance(surface_distances, tolerance_mm):\n    \"\"\"Computes the _surface_ DICE coefficient at a specified tolerance.\n\n  Computes the _surface_ DICE coefficient at a specified tolerance. Not to be\n  confused with the standard _volumetric_ DICE coefficient. The surface DICE\n  measures the overlap of two surfaces instead of two volumes. A surface\n  element is counted as overlapping (or touching), when the closest distance to\n  the other surface is less or equal to the specified tolerance. The DICE\n  coefficient is in the range between 0.0 (no overlap) to 1.0 (perfect overlap).\n\n  Args:\n    surface_distances: dict with \"distances_gt_to_pred\", \"distances_pred_to_gt\"\n      \"surfel_areas_gt\", \"surfel_areas_pred\" created by\n      compute_surface_distances()\n    tolerance_mm: a float value. The tolerance in mm\n\n  Returns:\n    A float value. The surface DICE coefficient in [0.0, 1.0].\n  \"\"\"\n    distances_gt_to_pred = surface_distances[\"distances_gt_to_pred\"]\n    distances_pred_to_gt = surface_distances[\"distances_pred_to_gt\"]\n    surfel_areas_gt = surface_distances[\"surfel_areas_gt\"]\n    surfel_areas_pred = surface_distances[\"surfel_areas_pred\"]\n    overlap_gt = np.sum(surfel_areas_gt[distances_gt_to_pred <= tolerance_mm])\n    overlap_pred = np.sum(\n        surfel_areas_pred[distances_pred_to_gt <= tolerance_mm])\n    surface_dice = (overlap_gt + overlap_pred) / (np.sum(surfel_areas_gt) +\n                                                  np.sum(surfel_areas_pred))\n    return surface_dice\n\n\n# Below is from https://github.com/Image-Py/imagepy/blob/master/imagepy/ipyalg/hydrology/edt.py\n# Copyright (c) <2016>, <ImagePy>\n# All rights reserved.\n\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are met:\n# 1. Redistributions of source code must retain the above copyright\n#    notice, this list of conditions and the following disclaimer.\n# 2. Redistributions in binary form must reproduce the above copyright\n#    notice, this list of conditions and the following disclaimer in the\n#    documentation and/or other materials provided with the distribution.\n# 3. All advertising materials mentioning features or use of this software\n#    must display the following acknowledgement:\n#    This product includes software developed by the <organization>.\n# 4. Neither the name of the <organization> nor the\n#    names of its contributors may be used to endorse or promote products\n#    derived from this software without specific prior written permission.\n\n# THIS SOFTWARE IS PROVIDED BY <ImagePy> ''AS IS'' AND ANY\n# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n# DISCLAIMED. IN NO EVENT SHALL <ImagePy> BE LIABLE FOR ANY\n# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\ndef neighbors(shape):\n    dim = len(shape)\n    block = generate_binary_structure(dim, 1)\n    block[tuple([1]*dim)] = 0\n    idx = np.where(block>0)\n    idx = np.array(idx, dtype=np.uint8).T\n    idx = np.array(idx-[1]*dim)\n    acc = np.cumprod((1,)+shape[::-1][:-1])\n    return np.dot(idx, acc[::-1])\n\n\n@jit(nopython=True)\ndef dist(idx1, idx2, acc):\n    dis = 0\n    for i in range(len(acc)):\n        c1 = idx1//acc[i]\n        c2 = idx2//acc[i]\n        dis += (c1-c2)**2\n        idx1 -= c1*acc[i]\n        idx2 -= c2*acc[i]\n    return dis\n\n\n@jit(nopython=True)\ndef step(dis, pts, roots, s, level, nbs, acc, scale):\n    cur = 0\n    while cur<s:\n        p = pts[cur]\n        rp = roots[cur]\n        if dis[p] == 0xffff or dis[p]>level:\n            cur += 1\n            continue\n        for dp in nbs:\n            cp = p+dp\n            if dis[cp]<level*scale+2e-10:continue\n            if dis[cp]==0xffff:continue\n            tdist = dist(cp, rp, acc)\n            if tdist<dis[cp]**2-1e-10:\n                dis[cp] = (tdist**0.5)*scale\n                pts[s] = cp\n                roots[s] = rp\n                if s == len(pts):\n                    s, cur = clear(pts, roots, s, cur)\n                s+=1\n        pts[cur] = -1\n        cur+=1\n    return cur\n\n\n@jit(nopython=True)\ndef clear(pts, roots, s, cur):\n    ns = 0; nc=0;\n    for c in range(s):\n        if pts[c]!=-1:\n            pts[ns] = pts[c]\n            roots[ns] = roots[c]\n            ns += 1\n            if c<cur:nc += 1\n    return ns, nc\n\n        \n@jit(nopython=True)\ndef collect(dis, nbs, pts, root):\n    cur = 0\n    for p in range(len(dis)):\n        if dis[p]>=0xffff-1: continue # edge or back\n        for dp in nbs:\n            if dis[p+dp]==0xffff-1:\n                pts[cur] = p\n                root[cur] = p\n                cur += 1\n                break\n    return cur\n\n\n@jit(nopython=True)\ndef bufjit(line):\n    for i in range(len(line)):\n        line[i] = 1 if line[i]==0 else 0xffff\n\n\ndef buffer(img, dtype):\n    buf = np.ones(tuple(np.array(img.shape)+2), dtype=dtype)\n    buf[tuple([slice(1,-1)]*buf.ndim)] = img\n    bufjit(buf.ravel())\n    buf[tuple([slice(1,-1)]*buf.ndim)] -= 1\n    return buf\n\n\ndef distance_transform_edt(img, output=np.float32, scale=1):\n    dis = buffer(img, output)\n    nbs = neighbors(dis.shape)\n    acc = np.cumprod((1,)+dis.shape[::-1][:-1])[::-1]\n    line = dis.ravel()\n    pts = np.zeros(max(line.size//4, 1024**2), dtype=np.int64)\n    roots = np.zeros(max(line.size//4, 1024**2), dtype=np.int64)\n    s = collect(line, nbs, pts, roots)\n    for level in range(10000):\n        s, c = clear(pts, roots, s, 0)\n        s = step(line, pts, roots, s, level, nbs, acc, scale)\n        if s==0:break\n    return dis[(slice(1,-1),)*img.ndim]","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-09T15:30:48.768567Z","iopub.execute_input":"2024-01-09T15:30:48.768835Z","iopub.status.idle":"2024-01-09T15:30:50.214480Z","shell.execute_reply.started":"2024-01-09T15:30:48.768813Z","shell.execute_reply":"2024-01-09T15:30:50.213084Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we define a subroutine called `score_submission` to score our submission on a subset of our training data using the Surface Dice Metric script defined above.","metadata":{}},{"cell_type":"code","source":"def score_submission():\n    import pandas as pd\n    from IPython.display import display\n    import os\n    from PIL import Image\n    \n    submission = pd.read_csv(\"submission.csv\")\n    print(\"Submission\")\n    display(submission)\n    \n    solution = pd.read_csv(\"/kaggle/input/sennet-truncated-data/train_rles_1_dense_1_voi_2.csv\")\n    print(\"Solution\")\n    display(solution)\n    \n    \n    # Augment the source dataframe with the dimensions of each image\n\n    # Define the base path to the dataset folder\n    base_path = '/kaggle/input/blood-vessel-segmentation/train'\n\n    # Dictionary to store dimensions for each folder\n    folder_dimensions = {}\n\n    # Function to get the width and height of an image\n    def get_image_dimensions(image_id):\n        folder_name = '_'.join(image_id.split('_')[:-1])\n        folder_name = os.path.join(folder_name, \"labels\")\n\n        # Check if dimensions are already known\n        if folder_name in folder_dimensions:\n            return folder_dimensions[folder_name]\n\n        # If not known, find the dimensions from the first image in the folder\n        image_file = os.listdir(os.path.join(base_path, folder_name))[0]\n        image_path = os.path.join(base_path, folder_name, image_file)\n        with Image.open(image_path) as img:\n            dimensions = img.size  # tuple (width, height)\n            folder_dimensions[folder_name] = dimensions\n            return dimensions\n\n    # Apply the function to each row in the DataFrame\n    solution['width'], solution['height'] = zip(*solution['id'].apply(get_image_dimensions))    \n    \n    my_score = score(solution=solution, submission=submission, row_id_column_name='id', rle_column_name='rle')\n    print(f\"\\n\\n\\n\\nScore: {my_score}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-09T15:30:50.216449Z","iopub.execute_input":"2024-01-09T15:30:50.216984Z","iopub.status.idle":"2024-01-09T15:30:50.227859Z","shell.execute_reply.started":"2024-01-09T15:30:50.216920Z","shell.execute_reply":"2024-01-09T15:30:50.226735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not is_submission:\n    score_submission()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T15:30:50.229989Z","iopub.execute_input":"2024-01-09T15:30:50.230455Z","iopub.status.idle":"2024-01-09T15:38:08.827521Z","shell.execute_reply.started":"2024-01-09T15:30:50.230425Z","shell.execute_reply":"2024-01-09T15:38:08.824436Z"},"trusted":true},"execution_count":null,"outputs":[]}]}