# Buggy code
def calculate_minimum_distances(self, workspace, parent_name):
    """Calculate the distance from child center to parent perimeter"""
    meas = workspace.measurements

    sub_object_name = self.sub_object_name.value

    parents = workspace.object_set.get_objects(parent_name)

    children = workspace.object_set.get_objects(sub_object_name)

    parents_of = self.get_parents_of(workspace, parent_name)

    if len(parents_of) == 0:
        dist = numpy.zeros((0,))
    elif numpy.all(parents_of == 0):
        dist = numpy.array([numpy.NaN] * len(parents_of))
    else:
        mask = parents_of > 0

        ccenters = centrosome.cpmorphology.centers_of_labels(
            children.segmented
        ).transpose()

        ccenters = ccenters[mask, :]

        parents_of_masked = parents_of[mask] - 1

        pperim = centrosome.outline.outline(parents.segmented)

        # Get a list of all points on the perimeter
        perim_loc = numpy.argwhere(pperim != 0)

        # Get the label # for each point
        perim_idx = pperim[perim_loc[:, 0], perim_loc[:, 1]]

        # Sort the points by label #
        idx = numpy.lexsort((perim_loc[:, 1], perim_loc[:, 0], perim_idx))

        perim_loc = perim_loc[idx, :]

        perim_idx = perim_idx[idx]

        # Get counts and indexes to each run of perimeter points
        counts = centrosome.cpmorphology.fixup_scipy_ndimage_result(
            scipy.ndimage.sum(
                numpy.ones(len(perim_idx)),
                perim_idx,
                numpy.arange(1, perim_idx[-1] + 1),
            )
        ).astype(numpy.int32)

        indexes = numpy.cumsum(counts) - counts

        # For the children, get the index and count of the parent
        ccounts = counts[parents_of_masked]

        cindexes = indexes[parents_of_masked]

        # Now make an array that has an element for each of that child's perimeter points
        clabel = numpy.zeros(numpy.sum(ccounts), int)

        # cfirst is the eventual first index of each child in the clabel array
        cfirst = numpy.cumsum(ccounts) - ccounts

        clabel[cfirst[1:]] += 1

        clabel = numpy.cumsum(clabel)

        # Make an index that runs from 0 to ccounts for each child label.
        cp_index = numpy.arange(len(clabel)) - cfirst[clabel]

        # then add cindexes to get an index to the perimeter point
        cp_index += cindexes[clabel]

        # Now, calculate the distance from the centroid of each label to each perimeter point in the parent.
        dist = numpy.sqrt(
            numpy.sum((perim_loc[cp_index, :] - ccenters[clabel, :]) ** 2, 1)
        )

        # Finally, find the minimum distance per child
        min_dist = centrosome.cpmorphology.fixup_scipy_ndimage_result(
            scipy.ndimage.minimum(dist, clabel, numpy.arange(len(ccounts)))
        )

        # Account for unparented children
        dist = numpy.array([numpy.NaN] * len(mask))

        dist[mask] = min_dist

    meas.add_measurement(sub_object_name, FF_MINIMUM % parent_name, dist)


# Traceback (ValueError)
Traceback (most recent call last):
File "C:\Users\brayma2\Desktop\GitHub\CellProfiler\cellprofiler\gui\pipelinecontroller.py", line 2844, in do_step
self.__pipeline.run_module(module, workspace)
File "C:\Users\brayma2\Desktop\GitHub\CellProfiler\cellprofiler\pipeline.py", line 2108, in run_module
module.run(workspace)
File "C:\Users\brayma2\Desktop\GitHub\CellProfiler\cellprofiler\modules\relateobjects.py", line 252, in run
self.calculate_centroid_distances(workspace, parent_name)
File "C:\Users\brayma2\Desktop\GitHub\CellProfiler\cellprofiler\modules\relateobjects.py", line 336, in calculate_centroid_distances
parents_of = self.get_parents_of(workspace, parent_name)
File "C:\Users\brayma2\Desktop\GitHub\CellProfiler\cellprofiler\modules\relateobjects.py", line 475, in get_parents_of
reverse_lookup_len = max(np.max(primary_parents_of) + 1,
File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 2263, in amax
out=out, keepdims=keepdims)
File "C:\Python27\lib\site-packages\numpy\core\_methods.py", line 26, in _amax
return umr_maximum(a, axis, None, out, keepdims)
ValueError: zero-size array to reduction operation maximum which has no identity

# Fixed code
def calculate_minimum_distances(self, workspace, parent_name):
    """Calculate the distance from child center to parent perimeter"""
    meas = workspace.measurements

    sub_object_name = self.y_name.value

    parents = workspace.object_set.get_objects(parent_name)

    children = workspace.object_set.get_objects(sub_object_name)

    parents_of = self.get_parents_of(workspace, parent_name)

    if len(parents_of) == 0:
        dist = numpy.zeros((0,))
    elif numpy.all(parents_of == 0):
        dist = numpy.array([numpy.NaN] * len(parents_of))
    else:
        mask = parents_of > 0

        ccenters = children.center_of_mass()

        ccenters = ccenters[mask, :]

        parents_of_masked = parents_of[mask] - 1

        pperim = (
            skimage.segmentation.find_boundaries(parents.segmented, mode="inner")
            * parents.segmented
        )

        # Get a list of all points on the perimeter
        perim_loc = numpy.argwhere(pperim != 0)

        # Get the label # for each point
        perim_idx = pperim[perim_loc.transpose().tolist()]

        # Sort the points by label #
        reverse_column_order = range(children.dimensions)[::-1]

        coordinates = perim_loc[:, reverse_column_order].transpose().tolist()

        coordinates.append(perim_idx)

        idx = numpy.lexsort(coordinates)

        perim_loc = perim_loc[idx, :]

        perim_idx = perim_idx[idx]

        # Get counts and indexes to each run of perimeter points
        counts = scipy.ndimage.sum(
            numpy.ones(len(perim_idx)), perim_idx, numpy.arange(1, perim_idx[-1] + 1)
        ).astype(numpy.int32)

        indexes = numpy.cumsum(counts) - counts

        # For the children, get the index and count of the parent
        ccounts = counts[parents_of_masked]

        cindexes = indexes[parents_of_masked]

        # Now make an array that has an element for each of that child's perimeter points
        clabel = numpy.zeros(numpy.sum(ccounts), int)

        # cfirst is the eventual first index of each child in the clabel array
        cfirst = numpy.cumsum(ccounts) - ccounts

        clabel[cfirst[1:]] += 1

        clabel = numpy.cumsum(clabel)

        # Make an index that runs from 0 to ccounts for each child label.
        cp_index = numpy.arange(len(clabel)) - cfirst[clabel]

        # then add cindexes to get an index to the perimeter point
        cp_index += cindexes[clabel]

        # Now, calculate the distance from the centroid of each label to each perimeter point in the parent.
        dist = numpy.sqrt(
            numpy.sum((perim_loc[cp_index, :] - ccenters[clabel, :]) ** 2, 1)
        )

        # Finally, find the minimum distance per child
        min_dist = scipy.ndimage.minimum(dist, clabel, numpy.arange(len(ccounts)))

        # Account for unparented children
        dist = numpy.array([numpy.NaN] * len(mask))

        dist[mask] = min_dist

    meas.add_measurement(sub_object_name, FF_MINIMUM % parent_name, dist)