def test_03_08_masked_errors(self): numpy.random.seed(38) ground_truth = numpy.random.uniform(size=(20, 10)) > .5 test = ground_truth.copy() mask = numpy.random.uniform(size=(20, 10)) > .5 test[~mask] = numpy.random.uniform(size=numpy.sum(~mask)) > .5 workspace, module = self.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) self.assertAlmostEqual(expected, value, msg="%s is wrong" % feature)
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.images[0].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 expected.iteritems(): actual = measurements.get_current_measurement( cellprofiler.measurement.IMAGE, feature ) assert actual == value, "{} expected {}, got {}".format(feature, value, actual)
def test_03_05_one_false_positive(self): i, j = numpy.mgrid[0:10, 0:20] ground_truth = ((i + j) % 2) == 0 test = ground_truth.copy() test[0, 1] = True workspace, module = self.make_workspace(dict(image=ground_truth), dict(image=test)) 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), (cellprofiler.modules.measureimageoverlap. FTR_EARTH_MOVERS_DISTANCE, 0)): mname = '_'.join( (cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME)) value = measurements.get_current_image_measurement(mname) self.assertAlmostEqual(expected, value, msg="%s is wrong" % feature)
def test_gray_outlines_max_possible_on_volume(self): numpy.random.seed(0) image = numpy.random.uniform(size=(9, 9, 9)).astype(numpy.float32) 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 = self.make_workspace(image, labels=[labels.astype(int)], dimensions=3) module.blank_image.value = False module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_GRAYSCALE module.max_type.value = cellprofiler.modules.overlayoutlines.MAX_POSSIBLE 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_almost_equal(output_image.pixel_data, expected)
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_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.object.Objects() step_parents_object.segmented = step_parents workspace, module = self.make_workspace(parents, children) workspace.measurements.add_measurement( "Step", cellprofiler.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_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_13_01_load_filename(self): # # Load a file, only specifying the FileName in the CSV # csv_text = '''"Image_FileName_DNA" "%s" ''' % self.test_filename pipeline, module, filename = self.make_pipeline(csv_text) assert isinstance(module, cellprofiler.modules.loaddata.LoadData) module.image_directory.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME module.image_directory.custom_path = self.test_path m = cellprofiler.measurement.Measurements() workspace = cellprofiler.workspace.Workspace(pipeline, module, m, cellprofiler.object.ObjectSet(), m, cellprofiler.image.ImageSetList()) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(m.get_measurement(cellprofiler.measurement.IMAGE, "FileName_DNA", 1), self.test_filename) path = m.get_measurement(cellprofiler.measurement.IMAGE, "PathName_DNA", 1) self.assertEqual(path, self.test_path) self.assertEqual( m.get_measurement(cellprofiler.measurement.IMAGE, "URL_DNA", 1), cellprofiler.modules.loadimages.pathname2url(os.path.join(self.test_path, self.test_filename))) module.prepare_group(workspace, {}, [1]) module.run(workspace) img = workspace.image_set.get_image("DNA", must_be_grayscale=True) self.assertEqual(tuple(img.pixel_data.shape), self.test_shape)
def test_13_03_extra_fields(self): # # Regression test of issue #853, extra fields # csv_text = '''"Image_URL_DNA" "{cp_logo_url}", "foo" "http:{cp_logo_url_filename}" "bogusurl.png" '''.format(**{ "cp_logo_url": tests.modules.cp_logo_url, "cp_logo_url_filename": tests.modules.cp_logo_url_filename }) pipeline, module, filename = self.make_pipeline(csv_text) assert isinstance(module, cellprofiler.modules.loaddata.LoadData) m = cellprofiler.measurement.Measurements() workspace = cellprofiler.workspace.Workspace(pipeline, module, m, cellprofiler.object.ObjectSet(), m, cellprofiler.image.ImageSetList()) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(m.get_measurement(cellprofiler.measurement.IMAGE, "FileName_DNA", 1), tests.modules.cp_logo_url_filename) path = m.get_measurement(cellprofiler.measurement.IMAGE, "PathName_DNA", 1) self.assertEqual(path, tests.modules.cp_logo_url_folder) self.assertEqual(m.get_measurement(cellprofiler.measurement.IMAGE, "URL_DNA", 1), tests.modules.cp_logo_url) self.assertEqual(m[cellprofiler.measurement.IMAGE, "FileName_DNA", 2], tests.modules.cp_logo_url_filename) self.assertEqual(m[cellprofiler.measurement.IMAGE, "PathName_DNA", 2], "http:") self.assertEqual(m[cellprofiler.measurement.IMAGE, "FileName_DNA", 3], "bogusurl.png") self.assertEqual(m[cellprofiler.measurement.IMAGE, "PathName_DNA", 3], "") module.prepare_group(workspace, {}, [1]) module.run(workspace) img = workspace.image_set.get_image("DNA", must_be_color=True) self.assertEqual(tuple(img.pixel_data.shape), tests.modules.cp_logo_url_shape)
def test_manual_io_range(image, module, workspace): data = image.pixel_data module.rescale_method.value = "Choose specific values to be reset to a custom range" module.wants_automatic_low.value = "Custom" module.wants_automatic_high.value = "Custom" module.source_scale.value = (50, 205) module.dest_scale.value = (0.2, 0.8) module.run(workspace) output = workspace.image_set.get_image("RescaleIntensity") actual = output.pixel_data expected = skimage.exposure.rescale_intensity(1.0 * data, (50, 205), (0.2, 0.8)) numpy.testing.assert_array_equal(expected, actual) assert numpy.any(actual == 0.2) assert numpy.any(actual == 0.8)
def test_13_06_load_default_input_folder(self): # Regression test of issue #1365 - load a file from the default # input folder and check that PathName_xxx is absolute csv_text = '''"Image_FileName_DNA","Image_PathName_DNA"\n"%s","%s"''' \ % (self.test_filename, self.test_path) pipeline, module, filename = self.make_pipeline(csv_text) try: assert isinstance(module, cellprofiler.modules.loaddata.LoadData) module.image_directory.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME module.image_directory.custom_path = self.test_path m = cellprofiler.measurement.Measurements() workspace = cellprofiler.workspace.Workspace(pipeline, module, m, cellprofiler.object.ObjectSet(), m, cellprofiler.image.ImageSetList()) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(m.get_measurement(cellprofiler.measurement.IMAGE, "FileName_DNA", 1), self.test_filename) path_out = m.get_measurement(cellprofiler.measurement.IMAGE, "PathName_DNA", 1) self.assertEqual(self.test_path, path_out) self.assertEqual( m.get_measurement(cellprofiler.measurement.IMAGE, "URL_DNA", 1), cellprofiler.modules.loadimages.pathname2url(os.path.join(self.test_path, self.test_filename))) module.prepare_group(workspace, {}, [1]) module.run(workspace) img = workspace.image_set.get_image("DNA", must_be_grayscale=True) self.assertEqual(tuple(img.pixel_data.shape), self.test_shape) finally: os.remove(filename)
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 } for feature, value in expected.iteritems(): actual = measurements.get_current_measurement( cellprofiler.measurement.IMAGE, feature ) assert actual == value, "{} expected {}, got {}".format(feature, value, actual)
def test_01_04_image_and_objects_and_mask(image, measurements, module, objects, workspace): '''Test operation on an image masked by objects and a mask''' numpy.random.seed(0) pixels = numpy.random.uniform(size=(10, 10)).astype(numpy.float32) mask = numpy.zeros((10, 10), bool) mask[1:9, :9] = True image.pixel_data = pixels image.mask = mask labels = numpy.zeros((10, 10), int) labels[1:9, 1:5] = 1 labels[1:9, 5:] = 2 objects.segmented = labels module.images[0].wants_objects.value = True module.run(workspace) assert measurements.get_current_measurement(cellprofiler.measurement.IMAGE, "Intensity_TotalArea_image_objects") == 64 assert measurements.get_current_measurement(cellprofiler.measurement.IMAGE, "Intensity_TotalIntensity_image_objects") == numpy.sum(pixels[1:9, 1:9]) assert measurements.get_current_measurement(cellprofiler.measurement.IMAGE, "Intensity_MeanIntensity_image_objects") == numpy.sum(pixels[1:9, 1:9]) / 64.0
def test_01_02_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) * .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(cellprofiler.measurement.IMAGE, "Intensity_TotalArea_image") == 64 assert measurements.get_current_measurement(cellprofiler.measurement.IMAGE, "Intensity_TotalIntensity_image") == numpy.sum(pixels[1:9, 1:9]) assert measurements.get_current_measurement(cellprofiler.measurement.IMAGE, "Intensity_MeanIntensity_image") == numpy.sum(pixels[1:9, 1:9]) / 64.0 assert measurements.get_current_measurement(cellprofiler.measurement.IMAGE, "Intensity_PercentMaximal_image") == 400. / 64.
def test_05_02_test_objects_rand_index(self): 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 = self.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_03_03_masked(self): '''Test ground-truth of a masked image''' workspace, module = self.make_workspace( dict(image=numpy.zeros((20, 10), bool)), dict(image=numpy.zeros((20, 10), bool), mask=numpy.zeros((20, 10), bool))) self.assertTrue(isinstance(module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap)) module.run(workspace) measurements = workspace.measurements self.assertTrue(isinstance(measurements, cellprofiler.measurement.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), (cellprofiler.modules.measureimageoverlap.FTR_EARTH_MOVERS_DISTANCE, 0)): mname = '_'.join((cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME)) value = measurements.get_current_image_measurement(mname) self.assertEqual(expected, value) for feature in (cellprofiler.modules.measureimageoverlap.FTR_RAND_INDEX, cellprofiler.modules.measureimageoverlap.FTR_ADJUSTED_RAND_INDEX): mname = '_'.join((cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME)) value = measurements.get_current_image_measurement(mname) self.assertTrue(numpy.isnan(value))
def test_run_uint16(workspace, module): module.object_name.value = "inputobjects" module.image_name.value = "outputimage" module.image_mode.value = "uint16" module.run(workspace) image = workspace.image_set.get_image("outputimage") objects = workspace.get_objects("inputobjects") assert image.dimensions == objects.segmented.ndim pixel_data = image.pixel_data assert pixel_data.shape == objects.shape expected = numpy.reshape(numpy.arange(256), (16, 16)) if objects.segmented.ndim == 3: expected = numpy.tile(expected, (3, 1)) expected = numpy.reshape(expected, (3, 16, 16)) assert numpy.all(pixel_data == expected)
def test_03_09_cropped(self): 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)) > .5 big_ground_truth[10:20, 10:30] = ground_truth workspace, module = self.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) self.assertAlmostEqual(expected, value, msg="%s is wrong. Expected %f, got %f" % (feature, expected, value))
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_CENTROID 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) self.assertEqual(v.shape[0], 12) self.assertTrue(numpy.all(numpy.abs(v - expected) < .0001))
def test_05_02_test_objects_rand_index(self): 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 = self.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_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_04_02_distance_minima(self): '''Check centroid-perimeter distance calculation''' i, j = numpy.mgrid[0:14, 0:30] # # Make the objects different sizes to exercise more code # parent_labels = (i >= 6) * 1 + (j >= 14) * 2 + 1 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) # # Measure the distance from the child to the edge of its parent. # We do this using the distance transform with a background that's # the edges of the labels # background = ((i != 0) & (i != 5) & (i != 6) & (i != 13) & (j != 0) & (j != 13) & (j != 14) & (j != 29)) d = scipy.ndimage.distance_transform_edt(background) expected = d[child_centers[0], child_centers[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_MINIMUM module.run(workspace) self.features_and_columns_match(workspace) meas = workspace.measurements v = meas.get_current_measurement(CHILD_OBJECTS, cellprofiler.modules.relateobjects.FF_MINIMUM % PARENT_OBJECTS) self.assertEqual(v.shape[0], 12) self.assertTrue(numpy.all(numpy.abs(v - expected) < .0001))
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_04_01_ijv(self): 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 = self.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_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_color_outlines_on_color_volume(self): 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 = self.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_03_04_all_right(self): numpy.random.seed(34) image = numpy.random.uniform(size=(10, 20)) > .5 workspace, module = self.make_workspace(dict(image=image), dict(image=image)) module.run(workspace) measurements = workspace.measurements self.assertTrue( isinstance(measurements, cellprofiler.measurement.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), (cellprofiler.modules.measureimageoverlap.FTR_RAND_INDEX, 1), (cellprofiler.modules.measureimageoverlap.FTR_ADJUSTED_RAND_INDEX, 1), (cellprofiler.modules.measureimageoverlap. FTR_EARTH_MOVERS_DISTANCE, 0)): mname = '_'.join( (cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME)) value = measurements.get_current_image_measurement(mname) self.assertEqual(expected, value)
def test_gray_outlines_on_blank_volume(self): 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 = self.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_03_07_one_false_negative_and_mask(self): 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 = self.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) self.assertAlmostEqual(expected, value, msg="%s is wrong" % feature)
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_03_09_cropped(self): 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)) > .5 big_ground_truth[10:20, 10:30] = ground_truth workspace, module = self.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) self.assertAlmostEqual(expected, value, msg="%s is wrong. Expected %f, got %f" % (feature, expected, value))
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_11_02_invert_binary_invert(self): # # Regression for issue #1329 # r = numpy.random.RandomState() r.seed(1102) m = cellprofiler.measurement.Measurements() pixel_data = r.uniform(size=(20, 20)) > .5 m.add("inputimage", cellprofiler.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.module_num = 1 pipeline = cellprofiler.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.module_num = 2 pipeline = cellprofiler.pipeline.Pipeline() workspace = cellprofiler.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 > .5)
def test_03_01_zeros(self): '''Test ground-truth of zeros and image of zeros''' workspace, module = self.make_workspace( dict(image=numpy.ones((20, 10), bool)), dict(image=numpy.ones((20, 10), bool))) self.assertTrue( isinstance( module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap)) module.run(workspace) measurements = workspace.measurements self.assertTrue( isinstance(measurements, cellprofiler.measurement.Measurements)) self.assertEqual( measurements.get_current_image_measurement( "Overlap_FalseNegRate_test"), 0) features = measurements.get_feature_names( cellprofiler.measurement.IMAGE) for feature in cellprofiler.modules.measureimageoverlap.FTR_ALL + [ cellprofiler.modules.measureimageoverlap. FTR_EARTH_MOVERS_DISTANCE ]: field = '_'.join( (cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP, feature, TEST_IMAGE_NAME)) self.assertTrue(field in features, "Missing feature: %s" % feature) ftr_emd = module.measurement_name( cellprofiler.modules.measureimageoverlap.FTR_EARTH_MOVERS_DISTANCE) self.assertEqual(measurements[cellprofiler.measurement.IMAGE, ftr_emd], 0)
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_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_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.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_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_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_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_run_binary(workspace, module): module.object_name.value = "inputobjects" module.image_name.value = "outputimage" module.image_mode.value = "Binary (black & white)" module.run(workspace) image = workspace.image_set.get_image("outputimage") objects = workspace.get_objects("inputobjects") assert image.dimensions == objects.segmented.ndim pixel_data = image.pixel_data assert pixel_data.shape == objects.shape if objects.segmented.ndim == 2: assert not pixel_data[0, 0] assert numpy.all(pixel_data[:, 1:]) assert numpy.all(pixel_data[1:, :]) else: assert not numpy.all(pixel_data[:, 0, 0]) assert numpy.all(pixel_data[:, :, 1:]) assert numpy.all(pixel_data[:, 1:, :])
def test_05_02_test_objects_rand_index(self): 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)) gt_labels, _ = scipy.ndimage.label(gt, numpy.ones((3, 3), bool)) test_labels, _ = scipy.ndimage.label(test, numpy.ones((3, 3), bool)) workspace, module = self.make_obj_workspace( gt_labels, test_labels, dict(image=numpy.ones(gt_labels.shape)), dict(image=numpy.ones(test_labels.shape))) module.run(workspace) measurements = workspace.measurements mname = '_'.join((cellprofiler.modules.measureobjectoverlap.C_IMAGE_OVERLAP, cellprofiler.modules.measureobjectoverlap.FTR_RAND_INDEX, GROUND_TRUTH_OBJ, ID_OBJ)) expected_rand_index = measurements.get_current_image_measurement(mname) rand_index = measurements.get_current_image_measurement(mname) self.assertAlmostEqual(rand_index, expected_rand_index) mname = '_'.join((cellprofiler.modules.measureobjectoverlap.C_IMAGE_OVERLAP, cellprofiler.modules.measureobjectoverlap.FTR_ADJUSTED_RAND_INDEX, GROUND_TRUTH_OBJ, ID_OBJ)) adjusted_rand_index = \ measurements.get_current_image_measurement(mname)
def test_01_02_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) * .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( cellprofiler.measurement.IMAGE, "Intensity_TotalArea_image") == 64 assert measurements.get_current_measurement( cellprofiler.measurement.IMAGE, "Intensity_TotalIntensity_image") == numpy.sum(pixels[1:9, 1:9]) assert measurements.get_current_measurement( cellprofiler.measurement.IMAGE, "Intensity_MeanIntensity_image") == numpy.sum(pixels[1:9, 1:9]) / 64.0 assert measurements.get_current_measurement( cellprofiler.measurement.IMAGE, "Intensity_PercentMaximal_image") == 400. / 64.
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 } for feature, value in expected.iteritems(): actual = measurements.get_current_measurement( cellprofiler.measurement.IMAGE, feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
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.images[0].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 expected.iteritems(): actual = measurements.get_current_measurement( cellprofiler.measurement.IMAGE, feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
def test_01_01_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) * .99 pixels[0:2, 0:2] = 1 image.pixel_data = pixels module.run(workspace) assert measurements.get_current_measurement( cellprofiler.measurement.IMAGE, "Intensity_TotalArea_image") == 100 assert measurements.get_current_measurement( cellprofiler.measurement.IMAGE, "Intensity_TotalIntensity_image") == numpy.sum(pixels) assert measurements.get_current_measurement( cellprofiler.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_run_binary(workspace, module): module.object_name.value = "inputobjects" module.image_name.value = "outputimage" module.image_mode.value = "Binary (black & white)" module.run(workspace) image = workspace.image_set.get_image("outputimage") objects = workspace.get_objects("inputobjects") assert image.dimensions == objects.segmented.ndim pixel_data = image.pixel_data assert pixel_data.shape == objects.shape if objects.segmented.ndim is 2: assert not pixel_data[0, 0] assert numpy.all(pixel_data[:, 1:]) assert numpy.all(pixel_data[1:, :]) else: assert not numpy.all(pixel_data[:, 0, 0]) assert numpy.all(pixel_data[:, :, 1:]) assert numpy.all(pixel_data[:, 1:, :])
def test_run_uint16(workspace, module): module.object_name.value = "inputobjects" module.image_name.value = "outputimage" module.image_mode.value = "uint16" module.run(workspace) image = workspace.image_set.get_image("outputimage") objects = workspace.get_objects("inputobjects") assert image.dimensions == objects.segmented.ndim pixel_data = image.pixel_data assert pixel_data.shape == objects.shape expected = numpy.reshape(numpy.arange(256), (16, 16)) if objects.segmented.ndim is 3: expected = numpy.tile(expected, (3, 1)) expected = numpy.reshape(expected, (3, 16, 16)) assert numpy.all(pixel_data == expected)
def test_run_color(workspace, module): for color in [ "Default", "autumn", "bone", "colorcube", "cool", "copper", "flag", "gray", "hot", "hsv", "jet", "lines", "pink", "prism", "spring", "summer", "white", "winter" ]: module.object_name.value = "inputobjects" module.image_name.value = "outputimage" module.image_mode.value = "Color" module.colormap.value = color module.run(workspace) image = workspace.image_set.get_image("outputimage") objects = workspace.get_objects("inputobjects") assert image.dimensions == objects.segmented.ndim pixel_data = image.pixel_data assert pixel_data.shape == objects.shape + (3, )
def test_enhance_texture_volume(image, module, workspace): r = numpy.random.RandomState() r.seed(81) data = r.uniform(size=(8, 19, 24)) image.pixel_data = data image.dimensions = 3 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_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 } for feature, value in expected.iteritems(): actual = measurements.get_current_measurement( cellprofiler.measurement.IMAGE, feature) assert actual == value, "{} expected {}, got {}".format( feature, value, actual)
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 } for feature, value in expected.iteritems(): actual = measurements.get_current_measurement( cellprofiler.measurement.IMAGE, feature ) assert actual == value, "{} expected {}, got {}".format(feature, value, actual)
def test_03_01_binary_ijv(self): workspace, module, ijv = self.make_workspace_ijv() self.assertTrue(isinstance(module, cellprofiler.modules.convertobjectstoimage.ConvertObjectsToImage)) module.image_mode.value = "Binary (black & white)" module.run(workspace) pixel_data = workspace.image_set.get_image(IMAGE_NAME).pixel_data self.assertEqual(len(numpy.unique(ijv[:, 0] + ijv[:, 1] * pixel_data.shape[0])), numpy.sum(pixel_data)) self.assertTrue(numpy.all(pixel_data[ijv[:, 0], ijv[:, 1]]))