def test_masked_errors(): numpy.random.seed(38) ground_truth = numpy.random.uniform(size=(20, 10)) > 0.5 test = ground_truth.copy() mask = numpy.random.uniform(size=(20, 10)) > 0.5 test[~mask] = numpy.random.uniform(size=numpy.sum(~mask)) > 0.5 workspace, module = make_workspace(dict(image=ground_truth), dict(image=test, mask=mask)) module.run(workspace) measurements = workspace.measurements for feature, expected in ( (cellprofiler.modules.measureimageoverlap.FTR_FALSE_POS_RATE, 0), (cellprofiler.modules.measureimageoverlap.FTR_FALSE_NEG_RATE, 0), (cellprofiler.modules.measureimageoverlap.FTR_TRUE_POS_RATE, 1), (cellprofiler.modules.measureimageoverlap.FTR_TRUE_NEG_RATE, 1), (cellprofiler.modules.measureimageoverlap.FTR_RECALL, 1), (cellprofiler.modules.measureimageoverlap.FTR_PRECISION, 1), (cellprofiler.modules.measureimageoverlap.FTR_F_FACTOR, 1), ): mname = "_".join(( cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME, )) value = measurements.get_current_image_measurement(mname) assert round(abs(expected - value), 7) == 0, "%s is wrong" % feature
def test_enhance_circles_volume(image, module, workspace): k, i, j = numpy.mgrid[-15:16, -15:16, -15:16] data = numpy.abs(numpy.sqrt(k * k + i * i + j * j) - 6) <= 1.5 data = data.astype(float) image.pixel_data = data image.dimensions = 3 module.method.value = "Enhance" module.enhance_method.value = "Circles" module.object_size.value = 12 module.run(workspace) actual = workspace.image_set.get_image("output").pixel_data expected = numpy.zeros_like(data) for index, plane in enumerate(data): expected[index] = skimage.transform.hough_circle(plane, 6)[0] numpy.testing.assert_array_almost_equal(expected, actual)
def test_enhance(accuracy, image, module, workspace): data = numpy.zeros((20, 30)) expected = numpy.zeros((20, 30)) i, j = numpy.mgrid[-10:10, -10:20] data[i ** 2 + j ** 2 <= 7 ** 2] = 1 i, j = numpy.mgrid[-10:10, -25:5] data[i ** 2 + j ** 2 <= 9] = 1 expected[i ** 2 + j ** 2 <= 9] = 1 image.pixel_data = data module.method.value = "Enhance" module.enhance_method.value = "Speckles" module.speckle_accuracy.value = accuracy module.object_size.value = 14 module.run(workspace) output = workspace.image_set.get_image("output") actual = output.pixel_data numpy.testing.assert_array_equal(expected, actual)
def test_suppress_masked_volume(image, module, workspace): data = numpy.zeros((20, 20, 30)) mask = numpy.ones_like(data, dtype=numpy.bool) k, i, j = numpy.mgrid[-10:10, -10:10, -10:20] data[k ** 2 + i ** 2 + j ** 2 <= 7 ** 2] = 1 k, i, j = numpy.mgrid[-10:10, -10:10, -25:5] data[k ** 2 + i ** 2 + j ** 2 <= 9] = 1 mask[k ** 2 + i ** 2 + j ** 2 <= 9] = False expected = data image.pixel_data = data image.mask = mask image.dimensions = 3 module.method.value = "Suppress" module.object_size.value = 14 module.run(workspace) output = workspace.image_set.get_image("output") actual = output.pixel_data numpy.testing.assert_array_equal(expected, actual)
def test_enhance_neurites_tubeness_negative(image, module, workspace): data = numpy.ones((20, 30)) data[5:15, 10:20] -= numpy.identity(10) image.pixel_data = data module.method.value = "Enhance" module.enhance_method.value = "Neurites" module.neurite_choice.value = "Tubeness" module.smoothing.value = 1.0 module.wants_rescale.value = True module.run(workspace) output = workspace.image_set.get_image("output") actual = output.pixel_data expected = centrosome.filter.hessian( scipy.ndimage.gaussian_filter(data, 1.0), return_hessian=False, return_eigenvectors=False, ) expected = -expected[:, :, 0] * (expected[:, :, 0] < 0) expected = skimage.exposure.rescale_intensity(expected) numpy.testing.assert_array_almost_equal(expected, actual, decimal=5)
def test_calculate_centroid_distances_volume(self): parents = numpy.zeros((9, 11, 11), dtype=numpy.uint8) children = numpy.zeros_like(parents) k, i, j = numpy.mgrid[0:9, 0:11, 0:11] parents[(k - 4)**2 + (i - 5)**2 + (j - 5)**2 <= 16] = 1 children[(k - 3)**2 + (i - 3)**2 + (j - 3)**2 <= 4] = 1 children[(k - 4)**2 + (i - 7)**2 + (j - 7)**2 <= 4] = 2 workspace, module = self.make_workspace(parents, children) module.find_parent_child_distances.value = ( cellprofiler.modules.relateobjects.D_CENTROID) module.run(workspace) expected = [3, numpy.sqrt(8)] actual = workspace.measurements.get_current_measurement( CHILD_OBJECTS, cellprofiler.modules.relateobjects.FF_CENTROID % PARENT_OBJECTS, ) numpy.testing.assert_array_equal(actual, expected)
def test_relate_zeros_with_step_parent(self): # https://github.com/CellProfiler/CellProfiler/issues/2441 parents = numpy.zeros((10, 10), dtype=numpy.uint8) children = numpy.zeros_like(parents) step_parents = numpy.zeros_like(parents) step_parents_object = cellprofiler_core.object.Objects() step_parents_object.segmented = step_parents workspace, module = self.make_workspace(parents, children) workspace.measurements.add_measurement( "Step", cellprofiler_core.measurement.FF_PARENT % PARENT_OBJECTS, []) module.step_parent_names[0].step_parent_name.value = "Step" workspace.object_set.add_objects(step_parents_object, "Step") module.wants_step_parent_distances.value = True module.find_parent_child_distances.value = ( cellprofiler.modules.relateobjects.D_MINIMUM) module.run(workspace) expected = [] actual = workspace.measurements.get_current_measurement( CHILD_OBJECTS, cellprofiler.modules.relateobjects.FF_MINIMUM % "Step") numpy.testing.assert_array_equal(actual, expected)
def test_volume_and_objects(image, measurements, module, objects, workspace): object_data = skimage.morphology.ball(3, dtype=numpy.uint8) image.set_image(numpy.ones_like(object_data, dtype=numpy.uint8), convert=False) image.dimensions = 3 objects.segmented = object_data module.wants_objects.value = True module.run(workspace) expected = { "Intensity_TotalIntensity_image_objects": 123.0, "Intensity_MeanIntensity_image_objects": 1.0, "Intensity_MedianIntensity_image_objects": 1.0, "Intensity_StdIntensity_image_objects": 0.0, "Intensity_MADIntensity_image_objects": 0.0, "Intensity_MaxIntensity_image_objects": 1.0, "Intensity_MinIntensity_image_objects": 1.0, "Intensity_TotalArea_image_objects": 123.0, "Intensity_PercentMaximal_image_objects": 100.0, "Intensity_UpperQuartileIntensity_image_objects": 1.0, "Intensity_LowerQuartileIntensity_image_objects": 1.0, } for feature, value in list(expected.items()): actual = measurements.get_current_measurement( cellprofiler_core.measurement.IMAGE, feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
def test_zeros(image, measurements, module, workspace): """Test operation on a completely-masked image""" image.pixel_data = numpy.zeros((10, 10)) image.mask = numpy.zeros((10, 10), bool) module.run(workspace) assert (measurements.get_current_measurement( cellprofiler_core.measurement.IMAGE, "Intensity_TotalArea_image") == 0) assert len(measurements.get_object_names()) == 1 assert measurements.get_object_names( )[0] == cellprofiler_core.measurement.IMAGE columns = module.get_measurement_columns(workspace.pipeline) features = measurements.get_feature_names( cellprofiler_core.measurement.IMAGE) assert len(columns) == len(features) for column in columns: assert column[1] in features
def test_color_outlines_on_color_volume(): numpy.random.seed(0) image = numpy.random.uniform(size=(9, 9, 9, 3)).astype(numpy.float32) labels = numpy.zeros((9, 9, 9)) k, i, j = numpy.mgrid[-4:5, -4:5, -4:5] labels[k**2 + i**2 + j**2 <= 9] = 1 workspace, module = make_workspace(image, labels=[labels.astype(int)], dimensions=3) module.blank_image.value = False module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_COLOR module.outlines[0].color.value = "Red" module.run(workspace) output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) expected = numpy.zeros_like(image) for index, plane in enumerate(labels): expected[index] = skimage.segmentation.mark_boundaries(image[index], plane, color=(1, 0, 0), mode="inner") numpy.testing.assert_array_equal(output_image.pixel_data, expected)
def test_gray_outlines_on_blank_volume(): image = numpy.zeros((9, 9, 9)) labels = numpy.zeros_like(image) k, i, j = numpy.mgrid[-4:5, -4:5, -4:5] labels[k**2 + i**2 + j**2 <= 9] = 1 workspace, module = make_workspace(image, labels=[labels.astype(int)], dimensions=3) module.blank_image.value = True module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_GRAYSCALE module.run(workspace) output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) expected = numpy.zeros(labels.shape + (3, )) for index, plane in enumerate(labels): expected[index] = skimage.segmentation.mark_boundaries(image[index], plane, color=1.0, mode="inner") expected = skimage.color.rgb2gray(expected) numpy.testing.assert_array_equal(output_image.pixel_data, expected)
def test_ijv(): numpy.random.seed(0) image = numpy.random.uniform(size=(50, 50, 3)).astype(numpy.float32) image[0, 0] = 1 labels0 = numpy.zeros(image.shape[:2], int) labels0[20:30, 20:30] = 1 labels1 = numpy.zeros(image.shape[:2], int) labels1[25:35, 25:35] = 2 labels = [labels0, labels1] expected = image.copy() mask = numpy.zeros(image.shape[:2], bool) mask[20:30, 20] = True mask[20:30, 29] = True mask[20, 20:30] = True mask[29, 20:30] = True mask[25:35, 25] = True mask[25:35, 34] = True mask[25, 25:35] = True mask[34, 25:35] = True expected[mask, 0] = 1 expected[mask, 1:] = 0 workspace, module = make_workspace(image, labels=labels) module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_COLOR module.outlines[0].color.value = "Red" module.line_mode.value = "Inner" module.run(workspace) output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) numpy.testing.assert_array_equal(output_image.pixel_data, expected)
def test_test_objects_rand_index(): r = numpy.random.RandomState() r.seed(52) base = numpy.zeros((100, 100), bool) base[r.randint(0, 100, size=10), r.randint(0, 100, size=10)] = True gt = base.copy() gt[r.randint(0, 100, size=5), r.randint(0, 100, size=5)] = True test = base.copy() test[r.randint(0, 100, size=5), r.randint(0, 100, size=5)] = True gt = scipy.ndimage.binary_dilation(gt, numpy.ones((5, 5), bool)) test = scipy.ndimage.binary_dilation(test, numpy.ones((5, 5), bool)) workspace, module = make_workspace(dict(image=gt), dict(image=test)) module.wants_emd.value = False module.run(workspace) measurements = workspace.measurements mname = "_".join(( cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, cellprofiler.modules.measureimageoverlap.FTR_RAND_INDEX, TEST_IMAGE_NAME, )) expected_rand_index = measurements.get_current_image_measurement(mname) mname = "_".join(( cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, cellprofiler.modules.measureimageoverlap.FTR_ADJUSTED_RAND_INDEX, TEST_IMAGE_NAME, )) expected_adjusted_rand_index = measurements.get_current_image_measurement( mname)
def test_cropped(): numpy.random.seed(39) i, j = numpy.mgrid[0:10, 0:20] ground_truth = ((i + j) % 2) == 0 test = ground_truth.copy() test[0, 1] = True cropping = numpy.zeros((20, 40), bool) cropping[10:20, 10:30] = True big_ground_truth = numpy.random.uniform(size=(20, 40)) > 0.5 big_ground_truth[10:20, 10:30] = ground_truth workspace, module = make_workspace(dict(image=big_ground_truth), dict(image=test, crop_mask=cropping)) module.run(workspace) measurements = workspace.measurements precision = 100.0 / 101.0 f_factor = 2 * precision / (1 + precision) for feature, expected in ( (cellprofiler.modules.measureimageoverlap.FTR_FALSE_POS_RATE, 0.01), (cellprofiler.modules.measureimageoverlap.FTR_FALSE_NEG_RATE, 0), (cellprofiler.modules.measureimageoverlap.FTR_TRUE_POS_RATE, 1), (cellprofiler.modules.measureimageoverlap.FTR_TRUE_NEG_RATE, 0.99), (cellprofiler.modules.measureimageoverlap.FTR_RECALL, 1), (cellprofiler.modules.measureimageoverlap.FTR_PRECISION, precision), (cellprofiler.modules.measureimageoverlap.FTR_F_FACTOR, f_factor), ): mname = "_".join(( cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME, )) value = measurements.get_current_image_measurement(mname) assert (round( abs(expected - value), 7) == 0), "%s is wrong. Expected %f, got %f" % (feature, expected, value)
def test_04_00_distance_empty(self): """Make sure we can handle labels matrices that are all zero""" empty_labels = numpy.zeros((10, 20), int) some_labels = numpy.zeros((10, 20), int) some_labels[2:7, 3:8] = 1 some_labels[3:8, 12:17] = 2 for parent_labels, child_labels, n in ( (empty_labels, empty_labels, 0), (some_labels, empty_labels, 0), (empty_labels, some_labels, 2), ): workspace, module = self.make_workspace(parent_labels, child_labels) self.assertTrue( isinstance(module, cellprofiler.modules.relateobjects.Relate)) module.find_parent_child_distances.value = ( cellprofiler.modules.relateobjects.D_BOTH) module.run(workspace) self.features_and_columns_match(workspace) meas = workspace.measurements for feature in ( cellprofiler.modules.relateobjects.FF_CENTROID, cellprofiler.modules.relateobjects.FF_MINIMUM, ): m = feature % PARENT_OBJECTS v = meas.get_current_measurement(CHILD_OBJECTS, m) self.assertEqual(len(v), n) if n > 0: self.assertTrue(numpy.all(numpy.isnan(v)))
def test_load_filename(): # # Load a file, only specifying the FileName in the CSV # csv_text = (""""Image_FileName_DNA" "%s" """ % test_filename) pipeline, module, filename = make_pipeline(csv_text) assert isinstance(module, cellprofiler.modules.loaddata.LoadData) module.image_directory.dir_choice = cellprofiler_core.setting.ABSOLUTE_FOLDER_NAME module.image_directory.custom_path = test_path m = cellprofiler_core.measurement.Measurements() workspace = cellprofiler_core.workspace.Workspace( pipeline, module, m, cellprofiler_core.object.ObjectSet(), m, cellprofiler_core.image.ImageSetList(), ) assert module.prepare_run(workspace) assert (m.get_measurement(cellprofiler_core.measurement.IMAGE, "FileName_DNA", 1) == test_filename) path = m.get_measurement(cellprofiler_core.measurement.IMAGE, "PathName_DNA", 1) assert path == test_path assert m.get_measurement( cellprofiler_core.measurement.IMAGE, "URL_DNA", 1) == cellprofiler_core.modules.loadimages.pathname2url( os.path.join(test_path, test_filename)) module.prepare_group(workspace, {}, [1]) module.run(workspace) img = workspace.image_set.get_image("DNA", must_be_grayscale=True) assert tuple(img.pixel_data.shape) == test_shape
def test_04_01_distance_centroids(self): """Check centroid-centroid distance calculation""" i, j = numpy.mgrid[0:14, 0:30] parent_labels = (i >= 7) * 1 + (j >= 15) * 2 + 1 # Centers should be at i=3 and j=7 parent_centers = numpy.array([[3, 7], [10, 7], [3, 22], [10, 22]], float) child_labels = numpy.zeros(i.shape) numpy.random.seed(0) # Take 12 random points and label them child_centers = numpy.random.permutation(numpy.prod(i.shape))[:12] child_centers = numpy.vstack( (i.flatten()[child_centers], j.flatten()[child_centers])) child_labels[child_centers[0], child_centers[1]] = numpy.arange(1, 13) parent_indexes = parent_labels[child_centers[0], child_centers[1]] - 1 expected = numpy.sqrt( numpy.sum((parent_centers[parent_indexes, :] - child_centers.transpose())**2, 1)) workspace, module = self.make_workspace(parent_labels, child_labels) self.assertTrue( isinstance(module, cellprofiler.modules.relateobjects.Relate)) module.find_parent_child_distances.value = ( cellprofiler.modules.relateobjects.D_BOTH) module.run(workspace) self.features_and_columns_match(workspace) meas = workspace.measurements v = meas.get_current_measurement( CHILD_OBJECTS, cellprofiler.modules.relateobjects.FF_CENTROID % PARENT_OBJECTS, ) assert v.shape[0] == 12 assert numpy.all(numpy.abs(v - expected) < 0.0001)
def test_volume(image, measurements, module, workspace): image.set_image(skimage.morphology.ball(3), convert=False) image.dimensions = 3 module.run(workspace) expected = { "Intensity_TotalIntensity_image": 123.0, "Intensity_MeanIntensity_image": 0.358600583090379, "Intensity_MedianIntensity_image": 0.0, "Intensity_StdIntensity_image": 0.47958962134059907, "Intensity_MADIntensity_image": 0.0, "Intensity_MaxIntensity_image": 1.0, "Intensity_MinIntensity_image": 0.0, "Intensity_TotalArea_image": 343.0, "Intensity_PercentMaximal_image": 35.8600583090379, "Intensity_UpperQuartileIntensity_image": 1.0, "Intensity_LowerQuartileIntensity_image": 0.0, "Intensity_Percentile_10_image": 0.0, "Intensity_Percentile_50_image": 0.0, "Intensity_Percentile_90_image": 1.0, } for feature, value in list(expected.items()): actual = measurements.get_current_measurement("Image", feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
def test_calculate_minimum_distances_volume(self): parents = numpy.zeros((9, 11, 11), dtype=numpy.uint8) children = numpy.zeros_like(parents) parents[1:8, 1:10, 1:10] = 1 children[3:6, 2:3, 2:3] = 1 children[4:7, 3:8, 3:8] = 2 workspace, module = self.make_workspace(parents, children) module.find_parent_child_distances.value = ( cellprofiler.modules.relateobjects.D_MINIMUM) module.run(workspace) expected = [1, 2] actual = workspace.measurements.get_current_measurement( CHILD_OBJECTS, cellprofiler.modules.relateobjects.FF_MINIMUM % PARENT_OBJECTS, ) numpy.testing.assert_array_equal(actual, expected)
def test_volume_and_mask(image, measurements, module, workspace): mask = skimage.morphology.ball(3, dtype=numpy.bool) image.set_image(numpy.ones_like(mask, dtype=numpy.uint8), convert=False) image.mask = mask image.dimensions = 3 module.run(workspace) expected = { "Intensity_TotalIntensity_image": 123.0, "Intensity_MeanIntensity_image": 1.0, "Intensity_MedianIntensity_image": 1.0, "Intensity_StdIntensity_image": 0.0, "Intensity_MADIntensity_image": 0.0, "Intensity_MaxIntensity_image": 1.0, "Intensity_MinIntensity_image": 1.0, "Intensity_TotalArea_image": 123.0, "Intensity_PercentMaximal_image": 100.0, "Intensity_UpperQuartileIntensity_image": 1.0, "Intensity_LowerQuartileIntensity_image": 1.0, "Intensity_Percentile_10_image": 1.0, "Intensity_Percentile_50_image": 1.0, "Intensity_Percentile_90_image": 1.0, } for feature, value in list(expected.items()): actual = measurements.get_current_measurement("Image", feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
def test_otsu3_high(): """Test the three-class otsu, weighted variance middle = foreground""" numpy.random.seed(0) image = numpy.hstack(( numpy.random.exponential(1.5, size=300), numpy.random.poisson(15, size=300), numpy.random.poisson(30, size=300), )) image.shape = (30, 30) image = centrosome.filter.stretch(image) limage, d = centrosome.threshold.log_transform(image) t1, t2 = centrosome.otsu.otsu3(limage) threshold = centrosome.threshold.inverse_log_transform(t1, d) workspace, module = make_workspace(image) module.threshold_scope.value = cellprofiler.modules.threshold.TS_GLOBAL module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_THREE_CLASS module.assign_middle_to_foreground.value = ( cellprofiler.modules.threshold.O_FOREGROUND) module.run(workspace) m = workspace.measurements m_threshold = m[cellprofiler_core.measurement.IMAGE, cellprofiler_core.measurement.FF_ORIG_THRESHOLD % module.y_name.value, ] assert round(abs(m_threshold - threshold), 7) == 0
def test_image(image, measurements, module, workspace): """Test operation on a single unmasked image""" numpy.random.seed(0) pixels = numpy.random.uniform(size=(10, 10)).astype(numpy.float32) * 0.99 pixels[0:2, 0:2] = 1 image.pixel_data = pixels module.run(workspace) assert (measurements.get_current_measurement( "Image", "Intensity_TotalArea_image") == 100) assert measurements.get_current_measurement( "Image", "Intensity_TotalIntensity_image") == numpy.sum(pixels) assert (measurements.get_current_measurement( "Image", "Intensity_MeanIntensity_image") == numpy.sum(pixels) / 100.0) assert measurements.get_current_image_measurement( "Intensity_MinIntensity_image") == numpy.min(pixels) assert measurements.get_current_image_measurement( "Intensity_MaxIntensity_image") == numpy.max(pixels) assert (measurements.get_current_image_measurement( "Intensity_PercentMaximal_image") == 4.0)
def test_enhance_neurites_gradient_volume(image, module, workspace): resources = os.path.realpath( os.path.join(os.path.dirname(__file__), "..", "resources") ) data = numpy.load(os.path.join(resources, "neurite.npy")) data = skimage.exposure.rescale_intensity(1.0 * data) data = numpy.tile(data, (3, 1)).reshape(3, *data.shape) image.pixel_data = data image.dimensions = 3 module.method.value = "Enhance" module.enhance_method.value = "Neurites" module.neurite_choice.value = "Line structures" module.object_size.value = 8 module.run(workspace) output = workspace.image_set.get_image("output") actual = output.pixel_data expected = numpy.load(os.path.join(resources, "enhanced_neurite.npy")) expected = numpy.tile(expected, (3, 1)).reshape(3, *expected.shape) numpy.testing.assert_array_almost_equal(expected, actual)
def test_image_and_mask(image, measurements, module, workspace): """Test operation on a masked image""" numpy.random.seed(0) pixels = numpy.random.uniform(size=(10, 10)).astype(numpy.float32) * 0.99 pixels[1:3, 1:3] = 1 mask = numpy.zeros((10, 10), bool) mask[1:9, 1:9] = True image.pixel_data = pixels image.mask = mask module.run(workspace) assert (measurements.get_current_measurement( "Image", "Intensity_TotalArea_image") == 64) assert measurements.get_current_measurement( "Image", "Intensity_TotalIntensity_image") == numpy.sum(pixels[1:9, 1:9]) assert (measurements.get_current_measurement( "Image", "Intensity_MeanIntensity_image") == numpy.sum(pixels[1:9, 1:9]) / 64.0) assert (measurements.get_current_measurement( "Image", "Intensity_PercentMaximal_image") == 400.0 / 64.0)
def test_enhance_circles_masked(image, module, workspace): data = numpy.zeros((31, 62)) mask = numpy.ones_like(data, dtype=numpy.bool) i, j = numpy.mgrid[-15:16, -15:16] circle = numpy.abs(numpy.sqrt(i * i + j * j) - 6) <= 1.5 data[:, :31] = circle data[:, 31:] = circle mask[:, 31:][circle] = False image.pixel_data = data image.mask = mask module.method.value = "Enhance" module.enhance_method.value = "Circles" module.object_size.value = 12 module.run(workspace) actual = workspace.image_set.get_image("output").pixel_data assert actual[15, 15] == 1 assert actual[15, 15 + 31] == 0
def test_volume_zeros(image, measurements, module, workspace): image.pixel_data = numpy.zeros((10, 10, 10)) image.dimensions = 3 module.run(workspace) expected = { "Intensity_TotalIntensity_image": 0.0, "Intensity_MeanIntensity_image": 0.0, "Intensity_MedianIntensity_image": 0.0, "Intensity_StdIntensity_image": 0.0, "Intensity_MADIntensity_image": 0.0, "Intensity_MaxIntensity_image": 0.0, "Intensity_MinIntensity_image": 0.0, "Intensity_TotalArea_image": 1000.0, "Intensity_PercentMaximal_image": 100.0, "Intensity_UpperQuartileIntensity_image": 0.0, "Intensity_LowerQuartileIntensity_image": 0.0, "Intensity_Percentile_10_image": 0.0, "Intensity_Percentile_50_image": 0.0, "Intensity_Percentile_90_image": 0.0, } for feature, value in list(expected.items()): actual = measurements.get_current_measurement("Image", feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
def test_enhance_texture(image, module, workspace): r = numpy.random.RandomState() r.seed(81) data = r.uniform(size=(19, 24)) image.pixel_data = data sigma = 2.1 module.method.value = "Enhance" module.enhance_method.value = "Texture" module.smoothing.value = sigma module.run(workspace) gaussian_mask = skimage.filters.gaussian( numpy.ones_like(data), sigma, mode="constant" ) gaussian = skimage.filters.gaussian(data, sigma, mode="constant") / gaussian_mask squared_gaussian = ( skimage.filters.gaussian(data ** 2, sigma, mode="constant") / gaussian_mask ) expected = squared_gaussian - gaussian ** 2 actual = workspace.image_set.get_image("output").pixel_data numpy.testing.assert_almost_equal(expected, actual)
def test_03_01_mean(self): """Compute the mean for two parents and four children""" i, j = numpy.mgrid[0:20, 0:20] parent_labels = (i / 10 + 1).astype(int) child_labels = (i / 10).astype(int) + (j / 10).astype(int) * 2 + 1 workspace, module = self.make_workspace(parent_labels, child_labels, fake_measurement=True) module.wants_per_parent_means.value = True m = workspace.measurements self.assertTrue( isinstance(m, cellprofiler_core.measurement.Measurements)) m.add_measurement(CHILD_OBJECTS, MEASUREMENT, numpy.array([1.0, 2.0, 3.0, 4.0])) m.add_measurement(CHILD_OBJECTS, IGNORED_MEASUREMENT, numpy.array([1, 2, 3, 4])) expected = numpy.array([2.0, 3.0]) module.run(workspace) name = "Mean_%s_%s" % (CHILD_OBJECTS, MEASUREMENT) self.assertTrue(name in m.get_feature_names(PARENT_OBJECTS)) data = m.get_current_measurement(PARENT_OBJECTS, name) self.assertTrue(numpy.all(data == expected)) self.features_and_columns_match(workspace) name = "Mean_%s_%s" % (CHILD_OBJECTS, IGNORED_MEASUREMENT) self.assertFalse(name in m.get_feature_names(PARENT_OBJECTS))
def test_invert_binary_invert(): # # Regression for issue #1329 # r = numpy.random.RandomState() r.seed(1102) m = cellprofiler_core.measurement.Measurements() pixel_data = r.uniform(size=(20, 20)) > 0.5 m.add("inputimage", cellprofiler_core.image.Image(pixel_data)) module = cellprofiler.modules.imagemath.ImageMath() module.images[0].image_name.value = "inputimage" module.output_image_name.value = "intermediateimage" module.operation.value = cellprofiler.modules.imagemath.O_INVERT module.set_module_num(1) pipeline = cellprofiler_core.pipeline.Pipeline() pipeline.add_module(module) module = cellprofiler.modules.imagemath.ImageMath() module.images[0].image_name.value = "intermediateimage" module.output_image_name.value = "outputimage" module.operation.value = cellprofiler.modules.imagemath.O_INVERT module.set_module_num(2) pipeline = cellprofiler_core.pipeline.Pipeline() workspace = cellprofiler_core.workspace.Workspace(pipeline, module, m, None, m, None) for module in pipeline.modules(): module.run(workspace) numpy.testing.assert_array_equal( pixel_data, m.get_image("inputimage").pixel_data > 0.5)
def test_one_false_negative_and_mask(): i, j = numpy.mgrid[0:10, 0:20] ground_truth = ((i + j) % 2) == 0 test = ground_truth.copy() test[0, 0] = False mask = j < 10 workspace, module = make_workspace(dict(image=ground_truth), dict(image=test, mask=mask)) module.run(workspace) measurements = workspace.measurements recall = 0.98 f_factor = 2 * recall / (1 + recall) for feature, expected in ( (cellprofiler.modules.measureimageoverlap.FTR_FALSE_POS_RATE, 0), (cellprofiler.modules.measureimageoverlap.FTR_FALSE_NEG_RATE, 0.02), (cellprofiler.modules.measureimageoverlap.FTR_TRUE_POS_RATE, 0.98), (cellprofiler.modules.measureimageoverlap.FTR_TRUE_NEG_RATE, 1), (cellprofiler.modules.measureimageoverlap.FTR_RECALL, recall), (cellprofiler.modules.measureimageoverlap.FTR_PRECISION, 1), (cellprofiler.modules.measureimageoverlap.FTR_F_FACTOR, f_factor), ): mname = "_".join(( cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME, )) value = measurements.get_current_image_measurement(mname) assert round(abs(expected - value), 7) == 0, "%s is wrong" % feature