# Buggy code
def get_probabilities_from_label_map(
    label_map: torch.Tensor,
    label_probabilities_dict: Dict[int, float],
) -> torch.Tensor:
    """Create probability map according to label map probabilities."""
    multichannel = label_map.shape[0] > 1
    probability_map = torch.zeros_like(label_map)
    label_probs = torch.Tensor(list(label_probabilities_dict.values()))
    normalized_probs = label_probs / label_probs.sum()
    iterable = zip(label_probabilities_dict, normalized_probs)
    for label, label_probability in iterable:
        if multichannel:
            mask = label_map[label]
        else:
            mask = label_map == label
        label_size = mask.sum()
        if not label_size:
            continue
        prob_voxels = label_probability / label_size
        if multichannel:
            probability_map[label] = prob_voxels * mask
        else:
            probability_map[mask] = prob_voxels
    if multichannel:
        probability_map = probability_map.sum(dim=0, keepdim=True)
    return probability_map


# Traceback (AssertionError)
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Test label sampler.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyNrlDa49HLQ/jzNDBnNeFDL",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/gist/fepegar/9e029c85827f48360ecc1b62b2530fa7/test-label-sampler.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KrbGyxQCb62F"
      },
      "source": [
        "!pip install --quiet torchio"
      ],
      "execution_count": 85,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ETNalyapb8t3",
        "outputId": "3cb5991a-7d4f-41a8-a1e5-52391274dcbc"
      },
      "source": [
        "import torch\n",
        "import torchio as tio\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "size = 5\n",
        "tensor = torch.zeros(1, size, size, 1)\n",
        "tensor[0, 2, 2] = 1\n",
        "label = tio.LabelMap(tensor=tensor)\n",
        "\n",
        "subject = tio.Subject(label=label)\n",
        "patch_size = 2, 2, 1\n",
        "sampler = tio.LabelSampler(patch_size, label_probabilities={0: 1, 1:1})\n",
        "values = torch.as_tensor([patch.label.data[0, 1, 1, 0] for patch in sampler(subject, 10000)])\n",
        "print(values.mean())"
      ],
      "execution_count": 86,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(0.6166)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 287
        },
        "id": "qWNBeroSctgu",
        "outputId": "f06fa6be-b3b0-46b0-e32b-c73505a50ee9"
      },
      "source": [
        "ax = plt.imshow(tensor[0, ..., 0])\n",
        "plt.colorbar(ax)"
      ],
      "execution_count": 87,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.colorbar.Colorbar at 0x7f6b2e792630>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 87
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAScAAAD8CAYAAAA11GIZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAQvklEQVR4nO3dbYxc1X3H8e+PxbDhwUHUVCXYiZHqRrVoC5FlU/lFKA+NIZH9olWFI9KmQvWbUJGGJgK1Iil9lValUSUr7TZYpEkKpSSqVtSpSxIjlAqIl4da2A6t5bbBBMk1T4FGYHv31xd3jIbV7sxddu7Omb2/j3Slebhz5m8Lfj733DPnyDYREaU5Y9gFRETMJeEUEUVKOEVEkRJOEVGkhFNEFCnhFBFFSjhFxKJJ2iXpmKRn53lfkv5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EFCnhFBFFSjhFRJESThFRpIRTRBQp4RQRRfp/9slH0Kzqm7cAAAAASUVORK5CYII=\n",
            "text/plain": [
              "<Figure size 432x288 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 323
        },
        "id": "foNGhpRikpXN",
        "outputId": "50c8769b-f836-45d5-d0e6-45b75e85d54c"
      },
      "source": [
        "pmap = sampler.get_probability_map(subject)\n",
        "print(pmap.shape)\n",
        "ax = plt.imshow(pmap[0, ..., 0])\n",
        "plt.colorbar(ax)\n",
        "print(pmap.min())\n",
        "print(pmap.max())\n",
        "p0, p1 = pmap[tensor == 0].sum(), pmap[tensor == 1].sum()\n",
        "assert p0 == p1"
      ],
      "execution_count": 88,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([1, 5, 5, 1])\n",
            "tensor(0.0208)\n",
            "tensor(0.5000)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAScAAAD8CAYAAAA11GIZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAPp0lEQVR4nO3df8idZ33H8fenWdrY2iJSYTXJbP8oQnBbu2VRKOjWKaYq6R9u0IqygiMbWFanQ1o2Otb9NQfiP2EYtCj4o3PqH5mLhqItImhNWrPONHaG4tZ0QqydUzeaNs/z3R/nVE+z58m5nz3nfs51nvv9Kjec+5zT63xbmk+v+7qv+7pSVUhSay6adwGStBLDSVKTDCdJTTKcJDXJcJLUJMNJUpMMJ0nrlmRvkseTnEpy5wqf35bkh0mOj48/nNbmL/VTqqShSLIFOAC8CTgNHE1yqKoeO++rf19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r2LVCssVr2LVGtTFmbMSdKwLFLPSdKAGE6SmrQQ4ZRkb5LHk5xKcue867mQJPcmOZPkO/OuZZokO5M8kOSxJCeS3DHvmlaTZFuSbyX553GtfzXvmrpIsiXJt5N8cd61LJrmwynJFuAAcBOwC7g1ya75VnVBHwc2dLLaOpwD3l9Vu4DXAe9p+N/tWeDGqvp14Dpgb5LXzbmmLu4ATs67iEXUfDgBe4BTVfVEVT0H3AfcPOeaVlVVXwOemXcdXVTVD6rqkfHrnzL6Q7R9vlWtrEZ+Nj7dOj6avpuTZAfwVuCj865lES1COG0Hnpw4P02jf4AWWZKrgeuBh+ZbyerGl0jHgTPA/VXVbK1jHwY+ALT7dG3DFiGc1LMkLwU+D7y3qn4y73pWU1VLVXUdsAPYk+Q1865pNUneBpypqofnXcuiWoRwegrYOXG+Y/yeZiDJVkbB9Kmq+sK86+miqn4MPEDbY3s3APuSfJ/RUMSNST4535IWyyKE01Hg2iTXJLkYuAU4NOeaNoUkAT4GnKyqD827ngtJ8ookLxu/fgnwJuC7861qdVV1V1XtqKqrGf03+9Wqeuecy1oozYdTVZ0DbgeOMBqw/WxVnZhvVatL8hngG8Crk5xO8u5513QBNwDvYvR/9ePj4y3zLmoVVwEPJHmU0f+w7q8qb89vYj6+IqlJzfecJA2T4SSpSYaTpCYZTpKaZDhJapLhJKlJhpOkJv0vOse+S26N7BcAAAAASUVORK5CYII=\n",
            "text/plain": [
              "<Figure size 432x288 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 496
        },
        "id": "T91VpRClktEP",
        "outputId": "c6a43c3c-9ec3-4feb-9e25-e33a7bff4c30"
      },
      "source": [
        "noborder = pmap.clone()[0]\n",
        "sampler.clear_probability_borders(noborder, torch.as_tensor(patch_size))\n",
        "ax = plt.imshow(noborder[..., 0])\n",
        "plt.colorbar(ax)\n",
        "print(noborder.min())\n",
        "print(noborder.max())\n",
        "p0, p1 = noborder[tensor[0] == 0].sum(), noborder[tensor[0] == 1].sum()\n",
        "assert p0 == p1, (p0, p1)"
      ],
      "execution_count": 89,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(0.)\n",
            "tensor(0.5000)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "error",
          "ename": "AssertionError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-89-7d8d55210a52>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnoborder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0mp0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mp1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnoborder\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnoborder\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mp0\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mp1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mp0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mp1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mAssertionError\u001b[0m: (tensor(0.3125), tensor(0.5000))"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAScAAAD8CAYAAAA11GIZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAQZ0lEQVR4nO3db4xc1X3G8e/DxsbhjxtRIxVhNyDVSmTRFlrXjoREWgdUQyK7UlIJIqIgUbmVYpU0aSNQK6q6r5JKNG/8IiuwgpoEJyV5saWOLJIYIaTE8QIujTE0FkpjIyTXQAi0wvbuPn0xAx22u3vvsnP3ntn7fNCV5s5cn/mB4OHcc8+cI9tERJTmgrYLiIiYS8IpIoqUcIqIIiWcIqJICaeIKFLCKSKKlHCKiCWTtF3S85JOSLp7js/vkPRfko72jz+pavM9zZQaEV0haQzYC9wEnAKOSJqw/eysS79pe3fddtNzioil2gKcsP2C7XPAfmDnUhttpOe0Whd6DRc30XREAG/y35zzWS2ljT/8g4v98ivTta598pmzx4A3B94atz3ef30lcHLgs1PA1jma+bikG4D/AP7C9sk5rnlbI+G0hovZqo800XRE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fm3r7E9Jek14FdZ4J9NI+Fk+/Jhtylp0vbmYbfblFGqd5RqhdGqt+RabW9vu4aF5LYuIpbqRWDDwPn6/ntzXiPpPcCvAC8v1GjCKSKW6giwUdLVklbT26VpYtY1E8Cn+68/QW8XpwUfA47SPKfx6kuKMkr1jlKtMFr1jlKt70p/DGk3cBAYA/bZPiZpDzBpewJ4APgnSSeAV+gF2IJUEV4REa3IbV1EFCnhFBFFGolwqpoaXxJJ+ySdlvSTtmupImmDpEOSnpV0TNJdbdc0H0lrJP1Y0r/1a/27tmuqQ9KYpKclPdJ2LaOm+HCqOTW+JF8Fip4/MmAK+LztTcCHgM8U/M/2LLDN9m8D1wLbJX2o5ZrquAs43nYRo6j4cKLe1Phi2H6c3tOI4tl+yfZT/dev0/uP6Mp2q5qbe97on67qH0U/zZG0HvgocH/btYyiUQinuabGF/kf0Cjr/0r8OuBwu5XMr3+LdBQ4DTxqu9ha+74MfAEod12Sgo1COEXDJF0CfBv4rO1ftl3PfGxP276W3gzkLZKuabum+Uj6GHDa9pNt1zKqRiGc6kyNj3dJ0ip6wfR1299pu546bP8COETZY3vXAzsk/YzeUMQ2SV9rt6TRMgrhVGdqfLwL/SUrHgCO276v7XoWIulySe/rv34vcBPwXLtVzc/2PbbX276K3r+zP7B9e8tljZTiw8n2FPDW1PjjwLdsH2u3qvlJegj4IfABSack3dl2TQu4HvgUvf+rH+0ft7Rd1DyuAA5Jeobe/7AetZ3H8ytYfr4SEUUqvucUEd2UcIqIIiWcIqJICaeIKFLCKSKKlHCKiCIlnCKiSP8LWXxR538JAYkAAAAASUVORK5CYII=\n",
            "text/plain": [
              "<Figure size 432x288 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    }
  ]
}

# Fixed code
def get_probabilities_from_label_map(
    label_map: torch.Tensor,
    label_probabilities_dict: Dict[int, float],
    patch_size: np.ndarray,
) -> torch.Tensor:
    """Create probability map according to label map probabilities."""
    patch_size = patch_size.astype(int)
    ini_i, ini_j, ini_k = patch_size // 2
    spatial_shape = np.array(label_map.shape[1:])
    if np.any(patch_size > spatial_shape):
        message = f"Patch size {patch_size}larger than label map {spatial_shape}"
        raise RuntimeError(message)
    crop_fin_i, crop_fin_j, crop_fin_k = crop_fin = (patch_size - 1) // 2
    fin_i, fin_j, fin_k = spatial_shape - crop_fin
    # See https://github.com/fepegar/torchio/issues/458
    label_map = label_map[:, ini_i:fin_i, ini_j:fin_j, ini_k:fin_k]

    multichannel = label_map.shape[0] > 1
    probability_map = torch.zeros_like(label_map)
    label_probs = torch.Tensor(list(label_probabilities_dict.values()))
    normalized_probs = label_probs / label_probs.sum()
    iterable = zip(label_probabilities_dict, normalized_probs)
    for label, label_probability in iterable:
        if multichannel:
            mask = label_map[label]
        else:
            mask = label_map == label
        label_size = mask.sum()
        if not label_size:
            continue
        prob_voxels = label_probability / label_size
        if multichannel:
            probability_map[label] = prob_voxels * mask
        else:
            probability_map[mask] = prob_voxels
    if multichannel:
        probability_map = probability_map.sum(dim=0, keepdim=True)

    # See https://github.com/fepegar/torchio/issues/458
    padding = ini_k, crop_fin_k, ini_j, crop_fin_j, ini_i, crop_fin_i
    probability_map = torch.nn.functional.pad(
        probability_map,
        padding,
    )
    return probability_map