def make_workspace(self, labels, mode, distance=0, neighbors_labels=None): '''Make a workspace for testing MeasureObjectNeighbors''' module = M.MeasureObjectNeighbors() module.module_num = 1 module.object_name.value = OBJECTS_NAME module.distance_method.value = mode module.distance.value = distance pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) measurements = cpmeas.Measurements() measurements.group_index = 1 measurements.group_number = 1 workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) if neighbors_labels is None: module.neighbors_name.value = OBJECTS_NAME else: module.neighbors_name.value = NEIGHBORS_NAME objects = cpo.Objects() objects.segmented = neighbors_labels object_set.add_objects(objects, NEIGHBORS_NAME) return workspace, module
def test_relate_children_volume(self): parent_labels = numpy.zeros((30, 30, 30), dtype=numpy.uint8) k, i, j = numpy.mgrid[-15:15, -15:15, -15:15] parent_labels[k**2 + i**2 + j**2 <= 196] = 1 parent_object = cpo.Objects() parent_object.segmented = parent_labels labels = numpy.zeros((30, 30, 30), dtype=numpy.uint8) k, i, j = numpy.mgrid[-15:15, -15:15, -7:23] labels[k**2 + i**2 + j**2 <= 25] = 1 k, i, j = numpy.mgrid[-15:15, -15:15, -22:8] labels[k**2 + i**2 + j**2 <= 16] = 2 labels[ 0, 10:20, 10: 20] = 3 # not touching a parent, should not be counted as a child object = cpo.Objects() object.segmented = labels actual_children, actual_parents = parent_object.relate_children(object) expected_children = [2] expected_parents = [1, 1, 0] numpy.testing.assert_array_equal(actual_children, expected_children) numpy.testing.assert_array_equal(actual_parents, expected_parents)
def test_05_01_overlapping(self): '''Test object measurement with two overlapping objects in ijv format''' i, j = np.mgrid[0:10, 0:20] m = (i > 1) & (i < 9) & (j > 1) & (j < 19) m1 = m & (i < j) m2 = m & (i < 9 - j) mlist = [] olist = [] for m in (m1, m2): objects = cpo.Objects() objects.segmented = m.astype(int) olist.append(objects) ijv = np.column_stack(( np.hstack([np.argwhere(m)[:, 0] for m in (m1, m2)]), np.hstack([np.argwhere(m)[:, 1] for m in (m1, m2)]), np.array([1] * np.sum(m1) + [2] * np.sum(m2)))) objects = cpo.Objects() objects.ijv = ijv olist.append(objects) for objects in olist: module = cpmoas.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True object_set = cpo.ObjectSet() object_set.add_objects(objects, "SomeObjects") module.module_num = 1 image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() mlist.append(measurements) pipeline = cpp.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) for c in module.get_measurement_columns(None): oname, feature = c[:2] if oname != "SomeObjects": continue measurements = mlist[0] self.assertTrue(isinstance(measurements, cpmeas.Measurements)) v1 = measurements.get_current_measurement(oname, feature) self.assertEqual(len(v1), 1) v1 = v1[0] measurements = mlist[1] v2 = measurements.get_current_measurement(oname, feature) self.assertEqual(len(v2), 1) v2 = v2[0] expected = (v1, v2) v = mlist[2].get_current_measurement(oname, feature) self.assertEqual(tuple(v), expected)
def test_05_01_relate_zero_parents_and_children(self): """Test the relate method if both parent and child label matrices are zeros""" x = cpo.Objects() x.segmented = np.zeros((10, 10), int) y = cpo.Objects() y.segmented = np.zeros((10, 10), int) children_per_parent, parents_of_children = x.relate_children(y) self.assertEqual(np.product(children_per_parent.shape), 0) self.assertEqual(np.product(parents_of_children.shape), 0)
def test_05_03_relate_one_parent_no_children(self): x = cpo.Objects() labels = np.zeros((10, 10), int) labels[3:6, 3:6] = 1 x.segmented = labels y = cpo.Objects() y.segmented = np.zeros((10, 10), int) children_per_parent, parents_of_children = x.relate_children(y) self.assertEqual(np.product(children_per_parent.shape), 1) self.assertEqual(children_per_parent[0], 0) self.assertEqual(np.product(parents_of_children.shape), 0)
def make_workspace( self, labels, overlap_choice, masking_objects=None, masking_image=None, renumber=True, ): module = M.MaskObjects() module.module_num = 1 module.object_name.value = INPUT_OBJECTS module.remaining_objects.value = OUTPUT_OBJECTS module.mask_choice.value = (M.MC_OBJECTS if masking_objects is not None else M.MC_IMAGE) module.masking_image.value = MASKING_IMAGE module.masking_objects.value = MASKING_OBJECTS module.retain_or_renumber.value = M.R_RENUMBER if renumber else M.R_RETAIN module.overlap_choice.value = overlap_choice pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) object_set = cpo.ObjectSet() io = cpo.Objects() io.segmented = labels object_set.add_objects(io, INPUT_OBJECTS) if masking_objects is not None: oo = cpo.Objects() oo.segmented = masking_objects object_set.add_objects(oo, MASKING_OBJECTS) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) if masking_image is not None: mi = cpi.Image(masking_image) image_set.add(MASKING_IMAGE, mi) workspace = cpw.Workspace( pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list, ) return workspace, module
def test_06_05_last_object_masked(self): # Regression test of issue #1553 # MeasureColocalization was truncating the measurements # if the last had no pixels or all pixels masked. # r = np.random.RandomState() r.seed(65) image1 = r.uniform(size=(20, 20)) image2 = r.uniform(size=(20, 20)) labels = np.zeros((20, 20), int) labels[3:8, 3:8] = 1 labels[13:18, 13:18] = 2 mask = labels != 2 objects = cpo.Objects() objects.segmented = labels for mask1, mask2 in ((mask, None), (None, mask), (mask, mask)): workspace, module = self.make_workspace( cpi.Image(image1, mask=mask1), cpi.Image(image2, mask=mask2), objects) module.run(workspace) m = workspace.measurements feature = M.F_CORRELATION_FORMAT % (IMAGE1_NAME, IMAGE2_NAME) values = m[OBJECTS_NAME, feature] self.assertEqual(len(values), 2) self.assertTrue(np.isnan(values[1]))
def test_06_03_no_objects(self): '''Test images with no objects''' labels = np.zeros((10, 10), int) i, j = np.mgrid[0:10, 0:10] image1 = ((i + j) % 2).astype(float) image2 = image1.copy() i1 = cpi.Image(image1) i2 = cpi.Image(image2) o = cpo.Objects() o.segmented = labels workspace, module = self.make_workspace(i1, i2, o) module.run(workspace) m = workspace.measurements mi = module.get_measurement_images(None, OBJECTS_NAME, "Correlation", "Correlation") corr = m.get_current_measurement(OBJECTS_NAME, "Correlation_Correlation_%s" % mi[0]) self.assertEqual(len(corr), 0) self.assertEqual(len(m.get_object_names()), 2) self.assertTrue(OBJECTS_NAME in m.get_object_names()) columns = module.get_measurement_columns(None) image_features = m.get_feature_names(cpmeas.IMAGE) object_features = m.get_feature_names(OBJECTS_NAME) self.assertEqual(len(columns), len(image_features) + len(object_features)) for column in columns: if column[0] == cpmeas.IMAGE: self.assertTrue(column[1] in image_features) else: self.assertEqual(column[0], OBJECTS_NAME) self.assertTrue(column[1] in object_features)
def make_workspace(self, labels, operation, iterations=1, wants_outlines=False, wants_fill_holes=False): object_set = cpo.ObjectSet() objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, INPUT_NAME) module = E.ExpandOrShrink() module.object_name.value = INPUT_NAME module.output_object_name.value = OUTPUT_NAME module.outlines_name.value = OUTLINES_NAME module.operation.value = operation module.iterations.value = iterations module.wants_outlines.value = wants_outlines module.wants_fill_holes.value = wants_fill_holes module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) image_set_list = cpi.ImageSetList() workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, cpmeas.Measurements(), image_set_list) return workspace, module
def test_04_02_masked_edge(self): # Regression test of issue #1115 labels = np.zeros((20, 50), int) labels[15:25, 15:25] = 1 image = np.random.uniform(size=labels.shape).astype(np.float32) # # Mask the edge of the object # mask = ~cpmo.outline(labels).astype(bool) m = cpmeas.Measurements() m.add(IMAGE_NAME, cpi.Image(image, mask=mask)) object_set = cpo.ObjectSet() o = cpo.Objects() o.segmented = labels object_set.add_objects(o, OBJECT_NAME) pipeline = P.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, P.RunExceptionEvent)) pipeline.add_listener(callback) module = MOI.MeasureObjectIntensity() module.module_num = 1 module.images[0].name.value = IMAGE_NAME module.objects[0].name.value = OBJECT_NAME pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, m, object_set, m, None) module.run(workspace)
def run(self, workspace): image_name = self.image_name.value objects_name = self.objects_name.value image = workspace.image_set.get_image(image_name) pixel_data = image.pixel_data labels = workspace.interaction_request( self, pixel_data, workspace.measurements.image_set_number) if labels is None: # User cancelled. Soldier on as best we can. workspace.cancel_request() labels = np.zeros(pixel_data.shape[:2], int) objects = cpo.Objects() objects.segmented = labels workspace.object_set.add_objects(objects, objects_name) ################## # # Add measurements # m = workspace.measurements # # The object count # object_count = np.max(labels) I.add_object_count_measurements(m, objects_name, object_count) # # The object locations # I.add_object_location_measurements(m, objects_name, labels) workspace.display_data.labels = labels workspace.display_data.pixel_data = pixel_data
def test_06_05_ijv_three_overlapping(self): # # This is a regression test of a bug where a segmentation consists # of only one point, labeled three times yielding two planes instead # of three. # ijv = np.array([[4, 5, 1], [4, 5, 2], [4, 5, 3]]) x = cpo.Objects() x.set_ijv(ijv, (8, 9)) labels = [] indices = np.zeros(3, bool) for l, i in x.get_labels(): labels.append(l) self.assertEqual(len(i), 1) self.assertTrue(i[0] in (1, 2, 3)) indices[i[0] - 1] = True self.assertTrue(np.all(indices)) self.assertEqual(len(labels), 3) lstacked = np.dstack(labels) i, j, k = np.mgrid[0:lstacked.shape[0], 0:lstacked.shape[1], 0:lstacked.shape[2]] self.assertTrue(np.all(lstacked[(i != 4) | (j != 5)] == 0)) self.assertEqual((1, 2, 3), tuple(sorted(lstacked[4, 5, :])))
def test_non_overlapping_object_intensity(self): # https://github.com/CellProfiler/CellProfiler/issues/2764 image1 = np.random.rand(10, 10) image1[:5, :] = 0 image2 = np.random.rand(10, 10) image2[5:, :] = 0 objects = cpo.Objects() objects.segmented = np.ones_like(image1, dtype=np.uint8) workspace, module = self.make_workspace(cpi.Image(image1), cpi.Image(image2), objects) module.run(workspace) m = workspace.measurements feature = M.F_OVERLAP_FORMAT % (IMAGE1_NAME, IMAGE2_NAME) values = m[OBJECTS_NAME, feature] assert len(values) == 1 assert values[0] == 0.0
def make_workspace(self, primary_labels, secondary_labels): """Make a workspace that has objects for the input labels returns a workspace with the following object_set - has object with name "primary" containing the primary labels has object with name "secondary" containing the secondary labels """ isl = cpi.ImageSetList() module = cpmit.IdentifyTertiarySubregion() module.module_num = 1 module.primary_objects_name.value = PRIMARY module.secondary_objects_name.value = SECONDARY module.subregion_objects_name.value = TERTIARY workspace = cpw.Workspace(cpp.Pipeline(), module, isl.get_image_set(0), cpo.ObjectSet(), cpm.Measurements(), isl) workspace.pipeline.add_module(module) for labels, name in ((primary_labels, PRIMARY), (secondary_labels, SECONDARY)): objects = cpo.Objects() objects.segmented = labels workspace.object_set.add_objects(objects, name) return workspace
def run(self, workspace): '''Find the outlines on the current image set workspace - The workspace contains pipeline - instance of cpp for this run image_set - the images in the image set being processed object_set - the objects (labeled masks) in this image set measurements - the measurements for this run frame - the parent frame to whatever frame is created. None means don't draw. ''' gridding = workspace.get_grid(self.grid_name.value) if self.shape_choice == SHAPE_RECTANGLE: labels = self.run_rectangle(workspace, gridding) elif self.shape_choice == SHAPE_CIRCLE_FORCED: labels = self.run_forced_circle(workspace, gridding) elif self.shape_choice == SHAPE_CIRCLE_NATURAL: labels = self.run_natural_circle(workspace, gridding) elif self.shape_choice == SHAPE_NATURAL: labels = self.run_natural(workspace, gridding) objects = cpo.Objects() objects.segmented = labels object_count = gridding.rows * gridding.columns workspace.object_set.add_objects(objects, self.output_objects_name.value) add_object_location_measurements(workspace.measurements, self.output_objects_name.value, labels, object_count) add_object_count_measurements(workspace.measurements, self.output_objects_name.value, object_count) if self.show_window: workspace.display_data.gridding = gridding workspace.display_data.labels = labels
def make_workspace(self, gridding, labels=None): module = I.IdentifyObjectsInGrid() module.module_num = 1 module.grid_name.value = GRID_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.guiding_object_name.value = GUIDING_OBJECTS_NAME image_set_list = cpi.ImageSetList() object_set = cpo.ObjectSet() if labels is not None: my_objects = cpo.Objects() my_objects.segmented = labels object_set.add_objects(my_objects, GUIDING_OBJECTS_NAME) pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = cpw.Workspace( pipeline, module, image_set_list.get_image_set(0), object_set, cpmeas.Measurements(), image_set_list, ) workspace.set_grid(GRID_NAME, gridding) return workspace, module
def rruunn(self, input_labels, relabel_option, merge_option=cellprofiler.modules.splitormergeobjects. UNIFY_DISTANCE, unify_method=cellprofiler.modules.splitormergeobjects. UM_DISCONNECTED, distance_threshold=5, minimum_intensity_fraction=.9, where_algorithm=cellprofiler.modules.splitormergeobjects. CA_CLOSEST_POINT, image=None, parent_object="Parent_object", parents_of=None): '''Run the SplitOrMergeObjects module returns the labels matrix and the workspace. ''' module = cellprofiler.modules.splitormergeobjects.SplitOrMergeObjects() module.module_num = 1 module.objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.relabel_option.value = relabel_option module.merge_option.value = merge_option module.merging_method.value = unify_method module.parent_object.value = parent_object module.distance_threshold.value = distance_threshold module.minimum_intensity_fraction.value = minimum_intensity_fraction module.wants_image.value = (image is not None) module.where_algorithm.value = where_algorithm pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) if image is not None: img = cpi.Image(image) image_set.add(IMAGE_NAME, img) module.image_name.value = IMAGE_NAME object_set = cpo.ObjectSet() o = cpo.Objects() o.segmented = input_labels object_set.add_objects(o, INPUT_OBJECTS_NAME) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) if parents_of is not None: m = workspace.measurements ftr = cellprofiler.measurement.FF_PARENT % parent_object m[INPUT_OBJECTS_NAME, ftr] = parents_of module.run(workspace) output_objects = workspace.object_set.get_objects(OUTPUT_OBJECTS_NAME) return output_objects.segmented, workspace
def test_zero_valued_intensity(self): # https://github.com/CellProfiler/CellProfiler/issues/2680 image1 = np.zeros((10, 10), dtype=np.float32) image2 = np.random.rand(10, 10).astype(np.float32) labels = np.zeros_like(image1, dtype=np.uint8) labels[5, 5] = 1 objects = cpo.Objects() objects.segmented = labels workspace, module = self.make_workspace(cpi.Image(image1), cpi.Image(image2), objects) module.run(workspace) m = workspace.measurements feature = M.F_CORRELATION_FORMAT % (IMAGE1_NAME, IMAGE2_NAME) values = m[OBJECTS_NAME, feature] assert len(values) == 1 assert np.isnan(values[0])
def test_06_04_ijv_to_labels_overlapping(self): '''Convert an ijv representation with overlap to labels''' ijv = np.array([[1, 1, 1], [1, 2, 1], [2, 1, 1], [2, 2, 1], [1, 3, 2], [2, 3, 2], [2, 3, 3], [4, 4, 4], [4, 5, 4], [4, 5, 5], [5, 5, 5]]) x = cpo.Objects() x.ijv = ijv labels = x.get_labels() self.assertEqual(len(labels), 2) unique_a = np.unique(labels[0][0])[1:] unique_b = np.unique(labels[1][0])[1:] for a in unique_a: self.assertTrue(a not in unique_b) for b in unique_b: self.assertTrue(b not in unique_a) for i, j, v in ijv: mylabels = labels[0][0] if v in unique_a else labels[1][0] self.assertEqual(mylabels[i, j], v)
def test_06_02_cropped_objects(self): '''Test images and objects with a cropping mask''' np.random.seed(0) image1 = np.random.uniform(size=(20, 20)) i1 = cpi.Image(image1) crop_mask = np.zeros((20, 20), bool) crop_mask[5:15, 5:15] = True i2 = cpi.Image(image1[5:15, 5:15], crop_mask=crop_mask) labels = np.zeros((10, 10), int) labels[:4, :4] = 1 labels[6:, 6:] = 2 o = cpo.Objects() o.segmented = labels # # Make the objects have the cropped image as a parent # o.parent_image = i2 workspace, module = self.make_workspace(i1, i2, o) module.run(workspace) m = workspace.measurements mi = module.get_measurement_images(None, OBJECTS_NAME, "Correlation", "Correlation") corr = m.get_current_measurement(OBJECTS_NAME, "Correlation_Correlation_%s" % mi[0]) self.assertAlmostEqual(corr[0], 1) self.assertAlmostEqual(corr[1], 1)
def test_07_01_make_ijv_outlines(self): np.random.seed(70) x = cpo.Objects() ii, jj = np.mgrid[0:10, 0:20] masks = [(ii - ic) ** 2 + (jj - jc) ** 2 < r ** 2 for ic, jc, r in ((4, 5, 5), (4, 12, 5), (6, 8, 5))] i = np.hstack([ii[mask] for mask in masks]) j = np.hstack([jj[mask] for mask in masks]) v = np.hstack([[k + 1] * np.sum(mask) for k, mask in enumerate(masks)]) x.set_ijv(np.column_stack((i, j, v)), ii.shape) x.parent_image = cpi.Image(np.zeros((10, 20))) colors = np.random.uniform(size=(3, 3)).astype(np.float32) image = x.make_ijv_outlines(colors) i1 = [i for i, color in enumerate(colors) if np.all(color == image[0, 5, :])] self.assertEqual(len(i1), 1) i2 = [i for i, color in enumerate(colors) if np.all(color == image[0, 12, :])] self.assertEqual(len(i2), 1) i3 = [i for i, color in enumerate(colors) if np.all(color == image[-1, 8, :])] self.assertEqual(len(i3), 1) self.assertNotEqual(i1[0], i2[0]) self.assertNotEqual(i2[0], i3[0]) colors = colors[np.array([i1[0], i2[0], i3[0]])] outlines = np.zeros((10, 20, 3), np.float32) alpha = np.zeros((10, 20)) for i, (color, mask) in enumerate(zip(colors, masks)): my_outline = outline(mask) outlines[my_outline] += color alpha[my_outline] += 1 alpha[alpha == 0] = 1 outlines /= alpha[:, :, np.newaxis] np.testing.assert_almost_equal(outlines, image)
def test_03_01_non_contiguous(self): '''make sure MeasureObjectAreaShape doesn't crash if fed non-contiguous objects''' module = cpmoas.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True object_set = cpo.ObjectSet() labels = np.zeros((10, 20), int) labels[1:9, 1:5] = 1 labels[4:6, 6:19] = 1 objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") module.module_num = 1 image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() pipeline = cpp.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) values = measurements.get_current_measurement("SomeObjects", "AreaShape_Perimeter") self.assertEqual(len(values), 1) self.assertEqual(values[0], 54)
def test_04_01_wrong_image_size(self): '''Regression test of IMG-961 - object and image size differ''' np.random.seed(41) labels = np.ones((20, 50), int) image = np.random.uniform(size=(30, 40)).astype(np.float32) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add('MyImage', cpi.Image(image)) object_set = cpo.ObjectSet() o = cpo.Objects() o.segmented = labels object_set.add_objects(o, "MyObjects") pipeline = P.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, P.RunExceptionEvent)) pipeline.add_listener(callback) module = MOI.MeasureObjectIntensity() module.module_num = 1 module.images[0].name.value = "MyImage" module.objects[0].name.value = "MyObjects" pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) module.run(workspace) feature_name = '%s_%s_%s' % (MOI.INTENSITY, MOI.INTEGRATED_INTENSITY, "MyImage") m = workspace.measurements.get_current_measurement( "MyObjects", feature_name) self.assertEqual(len(m), 1) self.assertAlmostEqual(m[0], np.sum(image[:20, :40]), 4)
def test_04_01_extent(self): module = cpmoas.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True object_set = cpo.ObjectSet() labels = np.zeros((10, 20), int) # 3/4 of a square is covered labels[5:7, 5:10] = 1 labels[7:9, 5:15] = 1 objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") module.module_num = 1 image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() pipeline = cpp.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) values = measurements.get_current_measurement( "SomeObjects", "_".join((cpmoas.AREA_SHAPE, cpmoas.F_EXTENT))) self.assertEqual(len(values), 1) self.assertAlmostEqual(values[0], .75)
def make_workspace(self, image, labels): module = D.DefineGrid() module.module_num = 1 module.grid_image.value = GRID_NAME module.manual_image.value = INPUT_IMAGE_NAME module.display_image_name.value = INPUT_IMAGE_NAME module.object_name.value = OBJECTS_NAME module.save_image_name.value = OUTPUT_IMAGE_NAME image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image)) object_set = cpo.ObjectSet() objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, OBJECTS_NAME) pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.LoadExceptionEvent)) self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) return workspace, module
def test_05_06_relate_one_parent_two_children(self): x = cpo.Objects() labels = np.zeros((10, 10), int) labels[3:6, 3:9] = 1 x.segmented = labels y = cpo.Objects() labels = np.zeros((10, 10), int) labels[3:6, 3:6] = 1 labels[3:6, 7:9] = 2 y.segmented = labels children_per_parent, parents_of_children = x.relate_children(y) self.assertEqual(np.product(children_per_parent.shape), 1) self.assertEqual(children_per_parent[0], 2) self.assertEqual(np.product(parents_of_children.shape), 2) self.assertEqual(parents_of_children[0], 1) self.assertEqual(parents_of_children[1], 1)
def run(self, workspace: cpw.Workspace) -> None: # Prepare inputs image_name: str = self.image_name.value image: cpi.Image = workspace.image_set.get_image(image_name) input_pixels: np.ndarray = image.pixel_data params = self.get_mask_params(workspace) self.save_mask_params(workspace, params) # Transform from microns to pixels before processing mpp = params.pop("MPP") params["RadialOffset"] /= mpp params["Thickness"] /= mpp # Construct wedge object and place in workspace mask = make_wedge_mask(params, shape=input_pixels.shape[:2]) mask_obj = cpo.Objects() mask_obj.segmented = mask workspace.object_set.add_objects(mask_obj, self.wedge_mask_name.value) if self.show_window: # Construct merged image image_rgb = (np.copy(input_pixels) if image.multichannel else skimage.color.gray2rgb(input_pixels)) color = tuple(c / 255.0 for c in self.mask_color.to_rgb()) blended_image = blend_image_and_mask(image_rgb, mask, color) workspace.display_data.blended_image = blended_image
def make_workspace(self, image, outline=None, labels=None): '''Make a workspace for testing ApplyThreshold''' m = cpmeas.Measurements() object_set = cpo.ObjectSet() module = O.OverlayOutlines() module.blank_image.value = False module.image_name.value = INPUT_IMAGE_NAME module.output_image_name.value = OUTPUT_IMAGE_NAME if outline is not None: module.outlines[0].outline_name.value = OUTLINE_NAME m.add(OUTLINE_NAME, cpi.Image(outline)) module.outlines[0].outline_choice.value = O.FROM_IMAGES if labels is not None: objects = cpo.Objects() if len(labels) > 1: ijv = np.vstack([ np.column_stack(list(np.where(l > 0)) + [l[l > 0]]) for l in labels ]) objects.set_ijv(ijv, shape=labels[0].shape) else: objects.segmented = labels[0] object_set.add_objects(objects, OBJECTS_NAME) module.outlines[0].outline_choice.value = O.FROM_OBJECTS module.outlines[0].objects_name.value = OBJECTS_NAME pipeline = cpp.Pipeline() workspace = cpw.Workspace(pipeline, module, m, object_set, m, None) m.add(INPUT_IMAGE_NAME, cpi.Image(image)) return workspace, module
def test_01_00_zeros(self): """Run on an empty labels matrix""" object_set = cpo.ObjectSet() labels = np.zeros((10, 20), int) objects = cpo.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") module = cpmoas.MeasureObjectAreaShape() settings = ["SomeObjects", "Yes"] module.set_settings_from_values(settings, 1, module.module_class()) module.module_num = 1 image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() pipeline = cpp.Pipeline() pipeline.add_module(module) workspace = cpw.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) for f in (cpmoas.F_AREA, cpmoas.F_CENTER_X, cpmoas.F_CENTER_Y, cpmoas.F_ECCENTRICITY, cpmoas.F_EULER_NUMBER, cpmoas.F_EXTENT, cpmoas.F_FORM_FACTOR, cpmoas.F_MAJOR_AXIS_LENGTH, cpmoas.F_MINOR_AXIS_LENGTH, cpmoas.F_ORIENTATION, cpmoas.F_PERIMETER, cpmoas.F_SOLIDITY, cpmoas.F_COMPACTNESS, cpmoas.F_MAXIMUM_RADIUS, cpmoas.F_MEAN_RADIUS, cpmoas.F_MEDIAN_RADIUS, cpmoas.F_MIN_FERET_DIAMETER, cpmoas.F_MAX_FERET_DIAMETER): m = cpmoas.AREA_SHAPE + "_" + f a = measurements.get_current_measurement('SomeObjects', m) self.assertEqual(len(a), 0)
def make_workspace(self, image, mask=None, labels=None, binary_image=None): '''Make a workspace and IdentifyPrimaryObjects module image - the intensity image for thresholding mask - if present, the "don't analyze" mask of the intensity image labels - if thresholding per-object, the labels matrix needed binary_image - if thresholding using a binary image, the image ''' module = YS.IdentifyYeastCells() module.module_num = 1 module.input_image_name.value = IMAGE_NAME module.object_name.value = OBJECTS_NAME module.binary_image.value = BINARY_IMAGE_NAME module.masking_objects.value = MASKING_OBJECTS_NAME pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) m = cpmeas.Measurements() cpimage = cpi.Image(image, mask=mask) m.add(IMAGE_NAME, cpimage) if binary_image is not None: m.add(BINARY_IMAGE_NAME, cpi.Image(binary_image)) object_set = cpo.ObjectSet() if labels is not None: o = cpo.Objects() o.segmented = labels object_set.add_objects(o, MASKING_OBJECTS_NAME) workspace = cellprofiler.workspace.Workspace(pipeline, module, m, object_set, m, None) return workspace, module