import scipy.ndimage as ndim
from skimage.filters import gaussian


def patch_up_roi(roi, sigma=0.5, truncate=2):
    """
    After being non-linearly transformed, ROIs tend to have holes in them.
    We perform a couple of computational geometry operations on the ROI to
    fix that up.

    Parameters
    ----------
    roi : 3D binary array
        The ROI after it has been transformed.

    sigma : float
        The sigma for initial Gaussian smoothing.

    truncate : float
        The truncation for the Gaussian

    Returns
    -------
    ROI after dilation and hole-filling
    """

    return (ndim.binary_fill_holes(
        gaussian(roi, sigma=sigma, truncate=truncate)).astype(float) > 0.1)

import scipy.ndimage as ndim
from skimage.filters import gaussian
from skimage.morphology import convex_hull_image

def patch_up_roi(roi, sigma=0.5, truncate=2):
    """
    After being non-linearly transformed, ROIs tend to have holes in them.
    We perform a couple of computational geometry operations on the ROI to
    fix that up.

    Parameters
    ----------
    roi : 3D binary array
        The ROI after it has been transformed.

    sigma : float
        The sigma for initial Gaussian smoothing.

    truncate : float
        The truncation for the Gaussian

    Returns
    -------
    ROI after dilation and hole-filling
    """

    return convex_hull_image(gaussian(ndim.binary_fill_holes(roi),
                                      sigma=sigma, truncate=truncate) > 0.1)

Add a convex hull operation to really close this up.
Add a convex hull operation to really close this up.

  import scipy.ndimage as ndim
  from skimage.filters import gaussian
- 
+ from skimage.morphology import convex_hull_image
  
  def patch_up_roi(roi, sigma=0.5, truncate=2):
      """
      After being non-linearly transformed, ROIs tend to have holes in them.
      We perform a couple of computational geometry operations on the ROI to
      fix that up.
  
      Parameters
      ----------
      roi : 3D binary array
          The ROI after it has been transformed.
  
      sigma : float
          The sigma for initial Gaussian smoothing.
  
      truncate : float
          The truncation for the Gaussian
  
      Returns
      -------
      ROI after dilation and hole-filling
      """
  
-     return (ndim.binary_fill_holes(
-         gaussian(roi, sigma=sigma, truncate=truncate)).astype(float) > 0.1)
+     return convex_hull_image(gaussian(ndim.binary_fill_holes(roi),
+                                       sigma=sigma, truncate=truncate) > 0.1)
  
Add a convex hull operation to really close this up.
## Code Before:
import scipy.ndimage as ndim
from skimage.filters import gaussian


def patch_up_roi(roi, sigma=0.5, truncate=2):
    """
    After being non-linearly transformed, ROIs tend to have holes in them.
    We perform a couple of computational geometry operations on the ROI to
    fix that up.

    Parameters
    ----------
    roi : 3D binary array
        The ROI after it has been transformed.

    sigma : float
        The sigma for initial Gaussian smoothing.

    truncate : float
        The truncation for the Gaussian

    Returns
    -------
    ROI after dilation and hole-filling
    """

    return (ndim.binary_fill_holes(
        gaussian(roi, sigma=sigma, truncate=truncate)).astype(float) > 0.1)

## Instruction:
Add a convex hull operation to really close this up.
## Code After:
import scipy.ndimage as ndim
from skimage.filters import gaussian
from skimage.morphology import convex_hull_image

def patch_up_roi(roi, sigma=0.5, truncate=2):
    """
    After being non-linearly transformed, ROIs tend to have holes in them.
    We perform a couple of computational geometry operations on the ROI to
    fix that up.

    Parameters
    ----------
    roi : 3D binary array
        The ROI after it has been transformed.

    sigma : float
        The sigma for initial Gaussian smoothing.

    truncate : float
        The truncation for the Gaussian

    Returns
    -------
    ROI after dilation and hole-filling
    """

    return convex_hull_image(gaussian(ndim.binary_fill_holes(roi),
                                      sigma=sigma, truncate=truncate) > 0.1)
