def test_01_01_combine(self): img = self.get_my_image() inj = cpm_inject.InjectImage("my_image", img) inj.module_num = 1 ctg = cpm_ctg.ColorToGray() ctg.module_num = 2 ctg.image_name.value = "my_image" ctg.combine_or_split.value = cpm_ctg.COMBINE ctg.red_contribution.value = 1 ctg.green_contribution.value = 2 ctg.blue_contribution.value = 3 ctg.grayscale_name.value = "my_grayscale" pipeline = cpp.Pipeline() pipeline.add_module(inj) pipeline.add_module(ctg) pipeline.test_valid() measurements = cpm.Measurements() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() workspace = Workspace(pipeline, inj, None, None, measurements, image_set_list, None) inj.prepare_run(workspace) inj.prepare_group(workspace, {}, [1]) image_set = image_set_list.get_image_set(0) inj.run(Workspace(pipeline, inj, image_set, object_set, measurements, None)) ctg.run(Workspace(pipeline, ctg, image_set, object_set, measurements, None)) grayscale = image_set.get_image("my_grayscale") self.assertTrue(grayscale) img = grayscale.image self.assertAlmostEqual(img[0, 0], 1.0 / 6.0) self.assertAlmostEqual(img[0, 25], 1.0 / 3.0) self.assertAlmostEqual(img[25, 0], 1.0 / 2.0) self.assertAlmostEqual(img[25, 25], 0)
def test_01_02_split_all(self): img = self.get_my_image() inj = cpm_inject.InjectImage("my_image", img) inj.module_num = 1 ctg = cpm_ctg.ColorToGray() ctg.module_num = 2 ctg.image_name.value = "my_image" ctg.combine_or_split.value = cpm_ctg.SPLIT ctg.use_red.value = True ctg.use_blue.value = True ctg.use_green.value = True ctg.red_name.value = "my_red" ctg.green_name.value = "my_green" ctg.blue_name.value = "my_blue" pipeline = cpp.Pipeline() pipeline.add_module(inj) pipeline.add_module(ctg) pipeline.test_valid() measurements = cpm.Measurements() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() workspace = Workspace(pipeline, inj, None, None, measurements, image_set_list, None) inj.prepare_run(workspace) inj.prepare_group(workspace, {}, [1]) image_set = image_set_list.get_image_set(0) inj.run( Workspace(pipeline, inj, image_set, object_set, measurements, None)) ctg.run( Workspace(pipeline, ctg, image_set, object_set, measurements, None)) red = image_set.get_image("my_red") self.assertTrue(red) img = red.image self.assertAlmostEqual(img[0, 0], 1) self.assertAlmostEqual(img[0, 25], 0) self.assertAlmostEqual(img[25, 0], 0) self.assertAlmostEqual(img[25, 25], 0) green = image_set.get_image("my_green") self.assertTrue(green) img = green.image self.assertAlmostEqual(img[0, 0], 0) self.assertAlmostEqual(img[0, 25], 1) self.assertAlmostEqual(img[25, 0], 0) self.assertAlmostEqual(img[25, 25], 0) blue = image_set.get_image("my_blue") self.assertTrue(blue) img = blue.image self.assertAlmostEqual(img[0, 0], 0) self.assertAlmostEqual(img[0, 25], 0) self.assertAlmostEqual(img[25, 0], 1) self.assertAlmostEqual(img[25, 25], 0)
def test_01_02_split_all(): img = get_my_image() inj = cellprofiler.modules.injectimage.InjectImage("my_image", img) inj.module_num = 1 ctg = cellprofiler.modules.colortogray.ColorToGray() ctg.module_num = 2 ctg.image_name.value = "my_image" ctg.combine_or_split.value = cellprofiler.modules.colortogray.SPLIT ctg.use_red.value = True ctg.use_blue.value = True ctg.use_green.value = True ctg.red_name.value = "my_red" ctg.green_name.value = "my_green" ctg.blue_name.value = "my_blue" pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(inj) pipeline.add_module(ctg) pipeline.test_valid() measurements = cellprofiler.measurement.Measurements() object_set = cellprofiler.object.ObjectSet() image_set_list = cellprofiler.image.ImageSetList() workspace = Workspace(pipeline, inj, None, None, measurements, image_set_list, None) inj.prepare_run(workspace) inj.prepare_group(workspace, {}, [1]) image_set = image_set_list.get_image_set(0) inj.run(Workspace(pipeline, inj, image_set, object_set, measurements, None)) ctg.run(Workspace(pipeline, ctg, image_set, object_set, measurements, None)) red = image_set.get_image("my_red") assert red img = red.image numpy.testing.assert_almost_equal(img[0, 0], 1) numpy.testing.assert_almost_equal(img[0, 25], 0) numpy.testing.assert_almost_equal(img[25, 0], 0) numpy.testing.assert_almost_equal(img[25, 25], 0) green = image_set.get_image("my_green") assert green img = green.image numpy.testing.assert_almost_equal(img[0, 0], 0) numpy.testing.assert_almost_equal(img[0, 25], 1) numpy.testing.assert_almost_equal(img[25, 0], 0) numpy.testing.assert_almost_equal(img[25, 25], 0) blue = image_set.get_image("my_blue") assert blue img = blue.image numpy.testing.assert_almost_equal(img[0, 0], 0) numpy.testing.assert_almost_equal(img[0, 25], 0) numpy.testing.assert_almost_equal(img[25, 0], 1) numpy.testing.assert_almost_equal(img[25, 25], 0)
def test_02_04_mask_input_image(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.input_image_name.value = IMAGE_NAME x.segmentation_precision.value = 11 x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 30 img = np.ones((200, 200)) * 0.5 draw_brightfield_cell(img, 100, 100, 20, False) draw_brightfield_cell(img, 25, 25, 10, False) img[0:10, 0:10] = 1 img[180:200, 180:200] = 0 msk = np.zeros((200, 200)) msk[0:10, 0:10] = 1 msk[180:200, 180:200] = 1 image = cpi.Image(img, file_name="test_02_04_mask_input_image") mask = cpi.Image(msk) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) image_set.providers.append( cpi.VanillaImageProvider(MASK_IMAGE_NAME, mask)) # first try without masking object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) self.assertEqual(0, objects.segmented.max(), "Cells should not be found due to the distractors") # now if we use masking option we should find these cells object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.ignore_mask_image_name.value = MASK_IMAGE_NAME x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) self.assertEqual(objects.segmented[25, 25] > 0, 1, "The small object was not there") self.assertEqual(objects.segmented[100, 100] > 0, 1, "The large object was not there")
def test_02_02_discard_small(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.segmentation_precision.value = 11 x.input_image_name.value = IMAGE_NAME x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 30 x.min_cell_area.value = 500 x.max_cell_area.value = 5000 x.advanced_cell_filtering.value = True img = np.ones((200, 200)) * 0.5 draw_brightfield_cell(img, 100, 100, 20, False) draw_brightfield_cell(img, 25, 25, 10, False) draw_brightfield_cell(img, 150, 150, 15, False) image = cpi.Image(img, file_name="test_02_02_discard_small") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) self.assertEqual(objects.segmented[25, 25] > 0, 0, "The small object was not filtered out") self.assertEqual(objects.segmented[150, 150] > 0, 1, "The medium object was not there") self.assertEqual(objects.segmented[100, 100] > 0, 1, "The large object was not there") location_center_x = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_X") self.assertTrue(isinstance(location_center_x, np.ndarray)) self.assertEqual(np.product(location_center_x.shape), 2)
def test_01_07_extreme_params(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.segmentation_precision.value = 14 x.input_image_name.value = IMAGE_NAME x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 77 img = get_two_cell_mask() draw_disc(img, (5, 5), 2, 0.7) draw_disc(img, (35, 11), 3, 0.2) img = convert_to_brightfield(img, False) image = cpi.Image(img, file_name="test_01_07_extreme_params") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) segmented = objects.segmented self.assertTrue(np.all( segmented == 0)) # no found because of parameters (no foreground) self.assertEqual(0, objects.count)
def test_01_06_fill_holes(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.segmentation_precision.value = 11 x.input_image_name.value = IMAGE_NAME x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 5 img = np.zeros((40, 40)) draw_disc(img, (10, 10), 7, .5) draw_disc(img, (30, 30), 7, .5) img[10, 10] = 0 img[30, 30] = 0 image = cpi.Image(img, file_name="test_01_06_fill_holes") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) self.assertTrue(objects.segmented[10, 10] > 0) self.assertTrue(objects.segmented[30, 30] > 0)
def test_01_04_split_channels(self): np.random.seed(13) image = np.random.uniform(size=(20, 10, 5)) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cpi.Image(image)) module = cpm_ctg.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cpm_ctg.SPLIT module.rgb_or_channels.value = cpm_ctg.CH_CHANNELS module.add_channel() module.add_channel() channel_indexes = np.array([1, 4, 2]) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = module.channel_names[channel_index] module.channels[i].image_name.value = OUTPUT_IMAGE_F % i pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpm.Measurements(), image_set_list) module.run(workspace) for i, channel_index in enumerate(channel_indexes): pixels = image_set.get_image(module.channels[i].image_name.value).pixel_data self.assertEqual(pixels.ndim, 2) self.assertEqual(tuple(pixels.shape), (20, 10)) np.testing.assert_almost_equal(image[:, :, channel_index], pixels)
def test_01_01_test_one_object(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.segmentation_precision.value = 11 x.input_image_name.value = IMAGE_NAME x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 10 img = convert_to_brightfield(get_one_cell_mask(), False) image = cpi.Image(img, file_name="test_01_01_test_one_object") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) self.assertEqual(len(object_set.object_names), 1) self.assertTrue(OBJECTS_NAME in object_set.object_names) objects = object_set.get_objects(OBJECTS_NAME) segmented = objects.segmented self.assertTrue(is_segmentation_correct(get_one_cell_mask(), segmented)) self.assertTrue("Image" in measurements.get_object_names()) self.assertTrue(OBJECTS_NAME in measurements.get_object_names()) self.assertTrue( "Features_Quality" in measurements.get_feature_names(OBJECTS_NAME)) quality = measurements.get_current_measurement(OBJECTS_NAME, "Features_Quality") self.assertTrue(quality[0] > 0) self.assertTrue("Location_Center_Y" in measurements.get_feature_names( OBJECTS_NAME)) location_center_y = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_Y") self.assertTrue(isinstance(location_center_y, np.ndarray)) self.assertEqual(np.product(location_center_y.shape), 1) self.assertTrue(location_center_y[0] > 8) self.assertTrue(location_center_y[0] < 12) self.assertTrue("Location_Center_X" in measurements.get_feature_names( OBJECTS_NAME)) location_center_x = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_X") self.assertTrue(isinstance(location_center_x, np.ndarray)) self.assertEqual(np.product(location_center_x.shape), 1) self.assertTrue(location_center_x[0] > 13) self.assertTrue(location_center_x[0] < 16) columns = x.get_measurement_columns(pipeline) for object_name in (cpmeas.IMAGE, OBJECTS_NAME): ocolumns = [x for x in columns if x[0] == object_name] features = measurements.get_feature_names(object_name) self.assertEqual(len(ocolumns), len(features)) self.assertTrue(all([column[1] in features for column in ocolumns]))
def run(self, workspace: cpw.Workspace) -> None: input_mask_name: str = self.input_mask_name.value input_mask: np.ndarray = workspace.get_objects( input_mask_name).segmented input_mask = input_mask.astype(dtype=bool) output = make_padded_array(input_mask, padding=self.padding.value) output_mask = output["array"] output_mask_obj = cpo.Objects() output_mask_obj.segmented = output_mask workspace.object_set.add_objects(output_mask_obj, self.output_mask_name.value) workspace.measurements.add_measurement( object_name=self.output_mask_name.value, feature_name="Metadata_Wedge_Origin_X", data=[output["origin"][0] ], # not sure why this has to be a list... ) workspace.measurements.add_measurement( object_name=self.output_mask_name.value, feature_name="Metadata_Wedge_Origin_Y", data=[output["origin"][1]], ) if self.show_window: # Initialize image to overlay on if self.image_name.value == cps.LEAVE_BLANK: image = np.zeros(shape=input_mask.shape[:2] + (3, ), dtype=float) else: image: np.ndarray = workspace.image_set.get_image( self.image_name.value).pixel_data # Construct merged image input_mask_color: Tuple[float, ...] = tuple( c / 255.0 for c in self.input_mask_color.to_rgb()) output_mask_color: Tuple[float, ...] = tuple( c / 255.0 for c in self.output_mask_color.to_rgb()) blended_image: np.ndarray = blend_arrays( image=image, arrays=[ (input_mask, input_mask_color), (output_mask, output_mask_color), ], ) workspace.display_data.blended_image = blended_image
def test_01_01_combine(): img = get_my_image() inj = cellprofiler.modules.injectimage.InjectImage("my_image", img) inj.module_num = 1 ctg = cellprofiler.modules.colortogray.ColorToGray() ctg.module_num = 2 ctg.image_name.value = "my_image" ctg.combine_or_split.value = cellprofiler.modules.colortogray.COMBINE ctg.red_contribution.value = 1 ctg.green_contribution.value = 2 ctg.blue_contribution.value = 3 ctg.grayscale_name.value = "my_grayscale" pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(inj) pipeline.add_module(ctg) pipeline.test_valid() measurements = cellprofiler.measurement.Measurements() object_set = cellprofiler.object.ObjectSet() image_set_list = cellprofiler.image.ImageSetList() workspace = Workspace(pipeline, inj, None, None, measurements, image_set_list, None) inj.prepare_run(workspace) inj.prepare_group(workspace, {}, [1]) image_set = image_set_list.get_image_set(0) inj.run(Workspace(pipeline, inj, image_set, object_set, measurements, None)) ctg.run(Workspace(pipeline, ctg, image_set, object_set, measurements, None)) grayscale = image_set.get_image("my_grayscale") assert grayscale img = grayscale.image numpy.testing.assert_almost_equal(img[0, 0], 1.0 / 6.0) numpy.testing.assert_almost_equal(img[0, 25], 1.0 / 3.0) numpy.testing.assert_almost_equal(img[25, 0], 1.0 / 2.0) numpy.testing.assert_almost_equal(img[25, 25], 0)
def test_01_03_combine_channels(): numpy.random.seed(13) image = numpy.random.uniform(size=(20, 10, 5)) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cellprofiler.image.Image(image)) module = cellprofiler.modules.colortogray.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cellprofiler.modules.colortogray.COMBINE module.grayscale_name.value = OUTPUT_IMAGE_F % 1 module.rgb_or_channels.value = cellprofiler.modules.colortogray.CH_CHANNELS module.add_channel() module.add_channel() channel_indexes = numpy.array([2, 0, 3]) factors = numpy.random.uniform(size=3) divisor = numpy.sum(factors) expected = numpy.zeros((20, 10)) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = channel_index + 1 module.channels[i].contribution.value_text = "%.10f" % factors[i] expected += image[:, :, channel_index] * factors[i] / divisor pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(), image_set_list, ) module.run(workspace) pixels = image_set.get_image(module.grayscale_name.value).pixel_data assert pixels.ndim == 2 assert tuple(pixels.shape) == (20, 10) numpy.testing.assert_almost_equal(expected, pixels)
def test_01_05_test_two_flu_dark_objects(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.segmentation_precision.value = 11 x.input_image_name.value = IMAGE_NAME x.background_brighter_then_cell_inside.value = True x.bright_field_image.value = False x.average_cell_diameter.value = 10 img = convert_to_fluorescent(get_two_cell_mask(), True) image = cpi.Image(img, file_name="test_01_05_test_two_flu_dark_objects") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) self.assertEqual(len(object_set.object_names), 1) self.assertTrue(OBJECTS_NAME in object_set.object_names) objects = object_set.get_objects(OBJECTS_NAME) self.assertTrue( is_segmentation_correct(get_two_cell_mask(), objects.segmented)) self.assertTrue("Image" in measurements.get_object_names()) self.assertTrue(OBJECTS_NAME in measurements.get_object_names()) self.assertTrue( "Features_Quality" in measurements.get_feature_names(OBJECTS_NAME)) quality = measurements.get_current_measurement(OBJECTS_NAME, "Features_Quality") self.assertTrue(len(quality) == 2) self.assertTrue("Location_Center_X" in measurements.get_feature_names( OBJECTS_NAME)) location_center_y = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_Y") location_center_x = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_X") positions = sorted(zip(location_center_x, location_center_y)) self.assertEqual(2, len(positions)) self.assertRange(3, 18, positions[0][0]) self.assertRange(25, 45, positions[0][1]) self.assertRange(20, 40, positions[1][0]) self.assertRange(5, 25, positions[1][1])
def test_01_04_split_channels(): numpy.random.seed(13) image = numpy.random.uniform(size=(20, 10, 5)) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cellprofiler.image.Image(image)) module = cellprofiler.modules.colortogray.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cellprofiler.modules.colortogray.SPLIT module.rgb_or_channels.value = cellprofiler.modules.colortogray.CH_CHANNELS module.add_channel() module.add_channel() module.add_channel() module.add_channel() channel_indexes = numpy.array([1, 4, 2]) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = channel_index + 1 module.channels[i].image_name.value = OUTPUT_IMAGE_F % i pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(), image_set_list, ) module.run(workspace) for i, channel_index in enumerate(channel_indexes): pixels = image_set.get_image( module.channels[i].image_name.value).pixel_data assert pixels.ndim == 2 assert tuple(pixels.shape) == (20, 10) numpy.testing.assert_almost_equal(image[:, :, channel_index], pixels)
def test_01_00_test_zero_objects(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.segmentation_precision.value = 11 x.input_image_name.value = IMAGE_NAME x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 5 img = np.zeros((25, 25)) image = cpi.Image(img, file_name="test_01_00_test_zero_objects") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) self.assertEqual(len(object_set.object_names), 1) self.assertTrue(OBJECTS_NAME in object_set.object_names) objects = object_set.get_objects(OBJECTS_NAME) segmented = objects.segmented self.assertTrue(np.all(segmented == 0)) self.assertTrue("Image" in measurements.get_object_names()) self.assertTrue(OBJECTS_NAME in measurements.get_object_names()) self.assertTrue( "Count_" + OBJECTS_NAME in measurements.get_feature_names("Image")) count = measurements.get_current_measurement("Image", "Count_" + OBJECTS_NAME) self.assertEqual(count, 0) self.assertTrue("Location_Center_X" in measurements.get_feature_names( OBJECTS_NAME)) location_center_x = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_X") self.assertTrue(isinstance(location_center_x, np.ndarray)) self.assertEqual(np.product(location_center_x.shape), 0) self.assertTrue("Location_Center_Y" in measurements.get_feature_names( OBJECTS_NAME)) location_center_y = measurements.get_current_measurement( OBJECTS_NAME, "Location_Center_Y") self.assertTrue(isinstance(location_center_y, np.ndarray)) self.assertEqual(np.product(location_center_y.shape), 0)
def test_01_03_combine_channels(self): np.random.seed(13) image = np.random.uniform(size=(20, 10, 5)) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cpi.Image(image)) module = cpm_ctg.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cpm_ctg.COMBINE module.grayscale_name.value = OUTPUT_IMAGE_F % 1 module.rgb_or_channels.value = cpm_ctg.CH_CHANNELS module.add_channel() module.add_channel() channel_indexes = np.array([2, 0, 3]) factors = np.random.uniform(size=3) divisor = np.sum(factors) expected = np.zeros((20, 10)) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = module.channel_names[ channel_index] module.channels[i].contribution.value_text = "%.10f" % factors[i] expected += image[:, :, channel_index] * factors[i] / divisor pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace(pipeline, module, image_set, cpo.ObjectSet(), cpm.Measurements(), image_set_list) module.run(workspace) pixels = image_set.get_image(module.grayscale_name.value).pixel_data self.assertEqual(pixels.ndim, 2) self.assertEqual(tuple(pixels.shape), (20, 10)) np.testing.assert_almost_equal(expected, pixels)
def test_02_03_use_background_image(self): x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.input_image_name.value = IMAGE_NAME x.segmentation_precision.value = 11 x.background_image_name.value = BACKGROUND_IMAGE_NAME x.background_elimination_strategy.value = YS.BKG_FILE x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 30 img = np.ones((200, 200)) * 0.5 draw_brightfield_cell(img, 100, 100, 20, False) draw_brightfield_cell(img, 25, 25, 10, False) draw_brightfield_cell(img, 150, 150, 15, False) # background blob bkg = np.ones((200, 200)) * 0.5 draw_brightfield_cell(bkg, 150, 150, 15, False) # background blob image = cpi.Image(img, file_name="test_02_03_use_background_image") background = cpi.Image(bkg) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) image_set.providers.append( cpi.VanillaImageProvider(BACKGROUND_IMAGE_NAME, background)) object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) self.assertEqual(objects.segmented[25, 25] > 0, 1, "The small object was not there") self.assertEqual(objects.segmented[100, 100] > 0, 1, "The large object was not there") self.assertEqual(objects.segmented[150, 150] > 0, 0, "The background blob was not filtered out")
def run(self, workspace: cpw.Workspace) -> None: if self.crop_mask_name.value == "Leave blank": origin_x = 0 origin_y = 0 else: crop_mask: np.ndarray = workspace.object_set.get_objects( self.crop_mask_name.value).segmented origin_x = np.logical_or.reduce(crop_mask, axis=0).argmax() origin_y = np.logical_or.reduce(crop_mask, axis=1).argmax() center_x = workspace.measurements.get_current_image_measurement( "Metadata_Bow_Center_X") center_y = workspace.measurements.get_current_image_measurement( "Metadata_Bow_Center_Y") well_x = workspace.measurements.get_current_image_measurement( "Metadata_Bow_Well_X") well_y = workspace.measurements.get_current_image_measurement( "Metadata_Bow_Well_Y") mpp = workspace.measurements.get_current_image_measurement( "Metadata_MPP") # translate if cropped center_x -= origin_x center_y -= origin_y well_x -= origin_x well_y -= origin_y # Compute polar origin radius = math.sqrt((well_y - center_y)**2 + (well_x - center_x)**2) radius *= mpp angle = math.atan2((well_y - center_y), (well_x - center_x)) for object_name in [obj.name.value for obj in self.object_groups]: objects: cpo.Objects = workspace.object_set.get_objects( object_name) # Compute delta in polar coordinates centroids: np.ndarray = objects.center_of_mass() # y, x if centroids.size == 0: continue dy = centroids[:, 0] - center_y dx = centroids[:, 1] - center_x radial_dist = np.sqrt(dy**2 + dx**2) # everything in pixels up until now radial_dist *= mpp angular_dist = (np.arctan2(dy, dx) - angle + math.pi) % (2 * math.pi) - math.pi workspace.measurements.add_image_measurement( feature_name="Metadata_Radius", data=radius) workspace.add_measurement( object_name=object_name, feature_name="WellDistance_RadialDistance", data=radial_dist, ) workspace.add_measurement( object_name=object_name, feature_name="WellDistance_AngularDistance", data=angular_dist, )
def test_03_01_simple_fitting(self): # reduce depth of fitting to speed up testing import cellstar.parameter_fitting.pf_process as process import cellstar.parameter_fitting.pf_runner as runner process.SEARCH_LENGTH_NORMAL = 20 # test multicore but only two cores process.get_max_workers = lambda: 2 runner.get_max_workers = lambda: 2 # make it deterministic np.random.seed(1) x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.input_image_name.value = IMAGE_NAME x.segmentation_precision.value = 9 # so that it is faster for fitting x.maximal_cell_overlap.value = 0.4 x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 30 x.autoadaptation_steps.value = 1 img = np.ones((200, 200)) * 0.5 draw_brightfield_cell(img, 100, 100, 15, False) draw_brightfield_cell(img, 120, 120, 15, False) draw_brightfield_cell(img, 110, 70, 15, False) draw_brightfield_cell(img, 160, 160, 10, False) draw_disc(img, (100, 100), 15, .65) draw_disc(img, (120, 120), 15, .65) draw_disc(img, (110, 70), 15, .65) draw_disc(img, (160, 160), 10, .65) img = img + np.random.normal(3., 0.01, img.shape) img = scipy.ndimage.gaussian_filter(img, 3) label = np.zeros((200, 200), dtype=int) draw_disc(label, (100, 100), 15, 1) draw_disc(label, (110, 70), 15, 2) image = cpi.Image(img, file_name="test_03_01_simple_fitting") image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) old_params = ast.literal_eval(x.autoadapted_params.value) input_processed, background_processed, ignore_mask_processed = x.preprocess_images( img, None, None) x.fit_parameters(input_processed, background_processed, ignore_mask_processed, label, x.autoadaptation_steps.value * 2, lambda x: True, lambda secs: time.sleep(secs)) new_params = ast.literal_eval(x.autoadapted_params.value) self.assertNotEqual(old_params[0], new_params[0]) self.assertNotEqual(old_params[1], new_params[1]) # now if we use new parameters option we should find these cells object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.segmentation_precision.value = 11 x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) self.assertEqual(4, objects.segmented.max()) colours = sorted([ objects.segmented[100, 100], objects.segmented[120, 120], objects.segmented[110, 70], objects.segmented[160, 160] ]) self.assertEqual(colours[0], 1) self.assertEqual(colours[1], 2) self.assertEqual(colours[2], 3) self.assertEqual(colours[3], 4)
def test_03_02_fitting_background_masked(self): # reduce depth of fitting to speed up testing import cellstar.parameter_fitting.pf_process as process import cellstar.parameter_fitting.pf_runner as runner process.SEARCH_LENGTH_NORMAL = 20 # test one core process.get_max_workers = lambda: 1 runner.get_max_workers = lambda: 1 # make it deterministic np.random.seed(1) x = YS.IdentifyYeastCells() x.object_name.value = OBJECTS_NAME x.input_image_name.value = IMAGE_NAME x.segmentation_precision.value = 9 # so that it is faster for fitting x.maximal_cell_overlap.value = 0.4 x.background_brighter_then_cell_inside.value = False x.average_cell_diameter.value = 12 x.autoadaptation_steps.value = 1 x.background_image_name.value = BACKGROUND_IMAGE_NAME x.background_elimination_strategy.value = YS.BKG_FILE x.ignore_mask_image_name.value = MASK_IMAGE_NAME img = np.ones((50, 50)) * 0.5 draw_brightfield_cell(img, 7, 7, 5, False) draw_brightfield_cell(img, 25, 28, 5, False) draw_brightfield_cell(img, 15, 16, 5, False) draw_brightfield_cell(img, 40, 40, 4, False) draw_disc(img, (7, 7), 5, .65) draw_disc(img, (25, 28), 5, .65) draw_disc(img, (15, 16), 5, .65) draw_disc(img, (40, 40), 4, .65) img = img + np.random.normal(0.5, 0.01, img.shape) img = scipy.ndimage.gaussian_filter(img, 2) # bright flares draw_disc(img, (40, 10), 10, 1.5) ignore_mask = np.zeros((50, 50), dtype=bool) draw_disc(ignore_mask, (40, 10), 10, 1) # dark areas draw_disc(img, (50, 25), 10, 0.0) background_mask = np.ones((50, 50)) * 0.5 draw_disc(background_mask, (50, 25), 10, 0.0) label = np.zeros((50, 50), dtype=int) draw_disc(label, (10, 10), 7, 1) draw_disc(label, (25, 20), 7, 2) image = cpi.Image(img, file_name="test_03_02_fitting_background_masked") mask = cpi.Image(ignore_mask) background = cpi.Image(background_mask) image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image)) image_set.providers.append( cpi.VanillaImageProvider(MASK_IMAGE_NAME, mask)) image_set.providers.append( cpi.VanillaImageProvider(BACKGROUND_IMAGE_NAME, background)) old_params = ast.literal_eval(x.autoadapted_params.value) input_processed, background_processed, ignore_mask_processed = x.preprocess_images( img, background_mask, ignore_mask) x.fit_parameters(input_processed, background_processed, ignore_mask_processed, label, x.autoadaptation_steps.value * 2, lambda x: True, lambda secs: time.sleep(secs)) new_params = ast.literal_eval(x.autoadapted_params.value) self.assertNotEqual(old_params[0], new_params[0]) self.assertNotEqual(old_params[1], new_params[1]) # now if we use new parameters option we should find these cells object_set = cpo.ObjectSet() measurements = cpmeas.Measurements() pipeline = cellprofiler.pipeline.Pipeline() x.segmentation_precision.value = 11 x.run(Workspace(pipeline, x, image_set, object_set, measurements, None)) objects = object_set.get_objects(OBJECTS_NAME) # 3 or four objects are acceptable self.assertLessEqual(3, objects.segmented.max()) self.assertLessEqual(objects.segmented.max(), 4) colours = sorted([ objects.segmented[10, 10], objects.segmented[25, 25], objects.segmented[40, 40] ]) self.assertEqual(colours[0], 1) self.assertEqual(colours[1], 2) self.assertEqual(colours[2], 3)