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_power(): image = numpy.ones((20, 10)) images_and_masks = [(image.copy(), None) for i in range(9)] for i, (img, _) in enumerate(images_and_masks): img[5, 5] *= numpy.sin(2 * numpy.pi * float(i) / 9.0) image_out = run_image_set( cellprofiler.modules.makeprojection.P_POWER, images_and_masks, frequency=9 ) i, j = numpy.mgrid[: image.shape[0], : image.shape[1]] numpy.testing.assert_almost_equal(image_out.pixel_data[(i != 5) & (j != 5)], 0) assert image_out.pixel_data[5, 5] > 1
def test_brightfield(): image = numpy.ones((20, 10)) images_and_masks = [(image.copy(), None) for i in range(9)] for i, (img, _) in enumerate(images_and_masks): if i < 5: img[:5, :5] = 0 else: img[:5, 5:] = 0 image_out = run_image_set( cellprofiler.modules.makeprojection.P_BRIGHTFIELD, images_and_masks ) i, j = numpy.mgrid[: image.shape[0], : image.shape[1]] numpy.testing.assert_almost_equal(image_out.pixel_data[(i > 5) | (j < 5)], 0) numpy.testing.assert_almost_equal(image_out.pixel_data[(i < 5) & (j >= 5)], 1)
def test_gray_max_possible(): numpy.random.seed(0) image = numpy.random.uniform(size=(50, 50)).astype(numpy.float32) * 0.5 outline = numpy.zeros((50, 50), bool) outline[20:31, 20:31] = 1 outline[21:30, 21:30] = 0 expected = image.copy() expected[outline.astype(bool)] = 1 workspace, module = make_workspace(image, labels=[outline.astype(int)]) module.blank_image.value = False module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_GRAYSCALE module.max_type.value = cellprofiler.modules.overlayoutlines.MAX_POSSIBLE module.line_mode.value = "Inner" module.run(workspace) output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) numpy.testing.assert_almost_equal(output_image.pixel_data, expected)
def test_wrong_size_gray(): """Regression test of IMG-961 - image and outline size differ""" numpy.random.seed(41) image = numpy.random.uniform(size=(50, 50)).astype(numpy.float32) * 0.5 outline = numpy.zeros((60, 40), bool) outline[20:31, 20:31] = True outline[21:30, 21:30] = False expected = image.copy() expected[:50, :40][outline[:50, :40]] = 1 workspace, module = make_workspace(image, labels=[outline.astype(int)]) module.blank_image.value = False module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_GRAYSCALE module.max_type.value = cellprofiler.modules.overlayoutlines.MAX_POSSIBLE module.line_mode.value = "Inner" module.run(workspace) output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) numpy.testing.assert_almost_equal(output_image.pixel_data, expected)
def test_color_to_color_outlines(): numpy.random.seed(0) image = numpy.random.uniform(size=(50, 50, 3)).astype(numpy.float32) image[0, 0] = 1 outline = numpy.zeros((50, 50), bool) outline[20:31, 20:31] = 1 outline[21:30, 21:30] = 0 expected = image.copy() expected[:, :, 0][outline.astype(bool)] = 1 expected[:, :, 1][outline.astype(bool)] = 0 expected[:, :, 2][outline.astype(bool)] = 0 workspace, module = make_workspace(image, labels=[outline.astype(int)]) 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_wrong_size_color_to_color(): numpy.random.seed(25) image = numpy.random.uniform(size=(50, 50, 3)).astype(numpy.float32) image[0, 0] = 1 outline = numpy.zeros((60, 40), bool) outline[20:31, 20:31] = 1 outline[21:30, 21:30] = 0 expected = image.copy() sub_expected = expected[:50, :40] sub_expected[:, :, 0][outline[:50, :40].astype(bool)] = 1 sub_expected[:, :, 1][outline[:50, :40].astype(bool)] = 0 sub_expected[:, :, 2][outline[:50, :40].astype(bool)] = 0 workspace, module = make_workspace(image, labels=[outline.astype(int)]) 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) assert numpy.all(output_image.pixel_data == expected)
def test_flip_top_to_bottom(): numpy.random.seed(0) image = numpy.random.uniform(size=(3, 3)).astype(numpy.float32) mask = numpy.array([[True, True, True], [False, True, True], [True, False, True]]) expected_mask = numpy.array( [[True, False, True], [False, True, True], [True, True, True]] ) expected = image.copy() expected[2, :] = image[0, :] expected[0, :] = image[2, :] def fn(module): assert isinstance(module, cellprofiler.modules.flipandrotate.FlipAndRotate) module.flip_choice.value = cellprofiler.modules.flipandrotate.FLIP_TOP_TO_BOTTOM module.rotate_choice.value = cellprofiler.modules.flipandrotate.ROTATE_NONE output_image, angle = run_module(image, mask=mask, fn=fn) assert angle == 0 assert numpy.all(output_image.mask == expected_mask) assert numpy.all( numpy.abs(output_image.pixel_data - expected) <= numpy.finfo(float).eps )
def test_flip_left_to_right(): numpy.random.seed(0) image = numpy.random.uniform(size=(3, 3)) mask = numpy.array([[True, True, True], [False, True, True], [True, False, True]]) expected_mask = numpy.array( [[True, True, True], [True, True, False], [True, False, True]] ) expected = image.copy() expected[:, 2] = image[:, 0] expected[:, 0] = image[:, 2] def fn(module): assert isinstance(module, cellprofiler.modules.flipandrotate.FlipAndRotate) module.flip_choice.value = cellprofiler.modules.flipandrotate.FLIP_LEFT_TO_RIGHT module.rotate_choice.value = cellprofiler.modules.flipandrotate.ROTATE_NONE output_image, angle = run_module(image, mask=mask, fn=fn) assert angle == 0 assert numpy.all(output_image.mask == expected_mask) assert numpy.all( numpy.abs(output_image.pixel_data - expected) <= numpy.finfo(numpy.float32).eps )