//  [python]
def calculate_focus_scores(self, image_group, workspace):
        """Calculate a local blur measurement and a image-wide one"""

        result = []
        for image_name in self.images_to_process(image_group, workspace):

            image = workspace.image_set.get_image(image_name, must_be_grayscale=True)
            pixel_data = image.pixel_data
            shape = image.pixel_data.shape
            if image.has_mask:
                pixel_data = pixel_data[image.mask]

            local_focus_score = []
            for scale_group in image_group.scale_groups:
                scale = scale_group.scale.value

                focus_score = 0
                if len(pixel_data):
                    mean_image_value = numpy.mean(pixel_data)
                    squared_normalized_image = (pixel_data - mean_image_value) ** 2
                    if mean_image_value > 0:
                        focus_score = numpy.sum(squared_normalized_image) / (
                            numpy.prod(pixel_data.shape) * mean_image_value
                        )
                #
                # Create a labels matrix that grids the image to the dimensions
                # of the window size
                #
                if image.dimensions == 2:
                    i, j = numpy.mgrid[0 : shape[0], 0 : shape[1]].astype(float)
                    m, n = (numpy.array(shape) + scale - 1) // scale
                    i = (i * float(m) / float(shape[0])).astype(int)
                    j = (j * float(n) / float(shape[1])).astype(int)
                    grid = i * n + j + 1
                    grid_range = numpy.arange(0, m * n + 1, dtype=numpy.int32)
                else:
                    k, i, j = numpy.mgrid[
                        0 : shape[0], 0 : shape[1], 0 : shape[2]
                    ].astype(float)
                    o, m, n = (numpy.array(shape) + scale - 1) // scale
                    k = (k * float(o) / float(shape[0])).astype(int)
                    i = (i * float(m) / float(shape[1])).astype(int)
                    j = (j * float(n) / float(shape[2])).astype(int)
                    grid = k * o + i * n + j + 1  # hmm
                    grid_range = numpy.arange(0, m * n * o + 1, dtype=numpy.int32)

                if image.has_mask:
                    grid[numpy.logical_not(image.mask)] = 0
                
                #
                # Do the math per label
                #
                local_means = centrosome.cpmorphology.fixup_scipy_ndimage_result(
                    scipy.ndimage.mean(image.pixel_data, grid, grid_range)
                )
                local_squared_normalized_image = (
                    image.pixel_data - local_means[grid]
                ) ** 2
                #
                # Compute the sum of local_squared_normalized_image values for each
                # grid for means > 0. Exclude grid label = 0 because that's masked
                #
                grid_mask = (local_means != 0) & ~numpy.isnan(local_means)
                nz_grid_range = grid_range[grid_mask]
                if len(nz_grid_range) and nz_grid_range[0] == 0:
                    nz_grid_range = nz_grid_range[1:]
                    local_means = local_means[1:]
                    grid_mask = grid_mask[1:]
                local_focus_score += [
                    0
                ]  # assume the worst - that we can't calculate it
                if len(nz_grid_range):
                    sums = centrosome.cpmorphology.fixup_scipy_ndimage_result(
                        scipy.ndimage.sum(
                            local_squared_normalized_image, grid, nz_grid_range
                        )
                    )
                    pixel_counts = centrosome.cpmorphology.fixup_scipy_ndimage_result(
                        scipy.ndimage.sum(numpy.ones(shape), grid, nz_grid_range)
                    )
                    local_norm_var = sums / (pixel_counts * local_means[grid_mask])
                    local_norm_median = numpy.median(local_norm_var)
                    if numpy.isfinite(local_norm_median) and local_norm_median > 0:
                        local_focus_score[-1] = (
                            numpy.var(local_norm_var) / local_norm_median
                        )

            #
            # Add the measurements
            #
            focus_score_name = "{}_{}_{}".format(
                C_IMAGE_QUALITY, F_FOCUS_SCORE, image_name
            )
            workspace.add_measurement("Image", focus_score_name, focus_score)
            result += [["{} focus score @{:d}".format(image_name, scale), focus_score]]

            for idx, scale_group in enumerate(image_group.scale_groups):
                scale = scale_group.scale.value
                local_focus_score_name = "{}_{}_{}_{:d}".format(
                    C_IMAGE_QUALITY, F_LOCAL_FOCUS_SCORE, image_name, scale
                )
                workspace.add_measurement(
                    "Image", local_focus_score_name, local_focus_score[idx],
                )
                result += [
                    [
                        "{} local focus score @{:d}".format(image_name, scale),
                        local_focus_score[idx],
                    ]
                ]

        return result