def test_03_04_quartiles(self):
     """test quartile values on a 250x250 square filled with uniform values"""
     labels = np.ones((250, 250), int)
     np.random.seed(0)
     image = np.random.uniform(size=(250, 250))
     ii = II.InjectImage('MyImage', image)
     ii.module_num = 1
     io = II.InjectObjects('MyObjects', labels)
     io.module_num = 2
     moi = MOI.MeasureObjectIntensity()
     moi.images[0].name.value = 'MyImage'
     moi.objects[0].name.value = 'MyObjects'
     moi.module_num = 3
     pipeline = P.Pipeline()
     pipeline.add_listener(self.error_callback)
     pipeline.add_module(ii)
     pipeline.add_module(io)
     pipeline.add_module(moi)
     m = pipeline.run()
     feature_name = '%s_%s_%s' % (MOI.INTENSITY,
                                  MOI.LOWER_QUARTILE_INTENSITY, 'MyImage')
     data = m.get_current_measurement('MyObjects', feature_name)
     self.assertAlmostEqual(data[0], .25, 2)
     feature_name = '%s_%s_%s' % (MOI.INTENSITY, MOI.MEDIAN_INTENSITY,
                                  'MyImage')
     data = m.get_current_measurement('MyObjects', feature_name)
     self.assertAlmostEqual(data[0], .50, 2)
     feature_name = '%s_%s_%s' % (MOI.INTENSITY, MOI.MAD_INTENSITY,
                                  'MyImage')
     data = m.get_current_measurement('MyObjects', feature_name)
     self.assertAlmostEqual(data[0], 0.25, 2)
     feature_name = '%s_%s_%s' % (MOI.INTENSITY,
                                  MOI.UPPER_QUARTILE_INTENSITY, 'MyImage')
     data = m.get_current_measurement('MyObjects', feature_name)
     self.assertAlmostEqual(data[0], .75, 2)
    def test_03_01_01_masked(self):
        """Make sure we can process a completely masked image

        Regression test of IMG-971
        """
        ii = II.InjectImage('MyImage', np.zeros((10, 10)),
                            np.zeros((10, 10), bool))
        ii.module_num = 1
        io = II.InjectObjects('MyObjects', np.ones((10, 10), int))
        io.module_num = 2
        moi = MOI.MeasureObjectIntensity()
        moi.images[0].name.value = 'MyImage'
        moi.objects[0].name.value = 'MyObjects'
        moi.module_num = 3
        pipeline = P.Pipeline()
        pipeline.add_listener(self.error_callback)
        pipeline.add_module(ii)
        pipeline.add_module(io)
        pipeline.add_module(moi)
        m = pipeline.run()
        for meas_name in MOI.ALL_MEASUREMENTS:
            feature_name = "%s_%s_%s" % (MOI.INTENSITY, meas_name, 'MyImage')
            data = m.get_current_measurement('MyObjects', feature_name)
            self.assertEqual(np.product(data.shape), 1)
            self.assertTrue(np.all(np.isnan(data) | (data == 0)))
        self.features_and_columns_match(m, moi)
 def test_03_01_00_zero(self):
     """Make sure we can process a blank image"""
     ii = II.InjectImage('MyImage', np.zeros((10, 10)))
     ii.module_num = 1
     io = II.InjectObjects('MyObjects', np.zeros((10, 10), int))
     io.module_num = 2
     moi = MOI.MeasureObjectIntensity()
     moi.images[0].name.value = 'MyImage'
     moi.objects[0].name.value = 'MyObjects'
     moi.module_num = 3
     pipeline = P.Pipeline()
     pipeline.add_listener(self.error_callback)
     pipeline.add_module(ii)
     pipeline.add_module(io)
     pipeline.add_module(moi)
     m = pipeline.run()
     for category, features in ((MOI.INTENSITY, MOI.ALL_MEASUREMENTS),
                                (MOI.C_LOCATION,
                                 MOI.ALL_LOCATION_MEASUREMENTS)):
         for meas_name in features:
             feature_name = "%s_%s_%s" % (category, meas_name, 'MyImage')
             data = m.get_current_measurement('MyObjects', feature_name)
             self.assertEqual(np.product(data.shape), 0,
                              "Got data for feature %s" % feature_name)
         self.features_and_columns_match(m, moi)
    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_02_02_color_by_color(self):
     '''Correct a color image with a black & white illumination fn'''
     np.random.seed(0)
     image = np.random.uniform(size=(10,10,3)).astype(np.float32)
     illum = np.random.uniform(size=(10,10,3)).astype(np.float32)
     expected = image - illum
     expected[expected < 0] = 0
     pipeline = cpp.Pipeline()
     pipeline.add_listener(self.error_callback)
     input_module = inj.InjectImage("InputImage",image)
     input_module.module_num = 1
     pipeline.add_module(input_module)
     illum_module = inj.InjectImage("IllumImage", illum)
     illum_module.module_num = 2
     pipeline.add_module(illum_module)
     module = cpmcia.CorrectIlluminationApply()
     module.module_num = 3
     pipeline.add_module(module)
     image = module.images[0]
     image.image_name.value = "InputImage"
     image.illum_correct_function_image_name.value = "IllumImage"
     image.corrected_image_name.value = "OutputImage"
     image.divide_or_subtract.value = cpmcia.DOS_SUBTRACT
     image.rescale_option = cpmcia.RE_NONE
     measurements = cpm.Measurements()
     image_set_list = cpi.ImageSetList()
     measurements = cpm.Measurements()
     workspace = cpw.Workspace(pipeline, None, None, None,
                               measurements, image_set_list)
     pipeline.prepare_run(workspace)
     input_module.prepare_group(workspace, {}, [1])
     illum_module.prepare_group(workspace, {}, [1])
     module.prepare_group(workspace, {}, [1])
     image_set = image_set_list.get_image_set(0)
     object_set = cpo.ObjectSet()
     workspace = cpw.Workspace(pipeline,
                               input_module,
                               image_set,
                               object_set,
                               measurements,
                               image_set_list)
     input_module.run(workspace)
     illum_module.run(workspace)
     module.run(workspace)
     output_image = workspace.image_set.get_image("OutputImage")
     self.assertTrue(np.all(output_image.pixel_data == expected))
Exemple #6
0
    def test_03_03_01_mass_displacement_masked(self):
        """Regression test IMG-766 - mass displacement of a masked image"""

        labels = np.array([[0, 0, 0, 0, 0, 0, 0],
                           [0, 1, 1, 1, 1, 1, 0],
                           [0, 1, 1, 1, 1, 1, 0],
                           [0, 1, 1, 1, 1, 1, 0],
                           [0, 1, 1, 1, 1, 1, 0],
                           [0, 1, 1, 1, 1, 1, 0],
                           [0, 0, 0, 0, 0, 0, 0],
                           [0, 2, 2, 2, 2, 2, 0],
                           [0, 2, 2, 2, 2, 2, 0],
                           [0, 2, 2, 2, 2, 2, 0],
                           [0, 2, 2, 2, 2, 2, 0],
                           [0, 2, 2, 2, 2, 2, 0],
                           [0, 0, 0, 0, 0, 0, 0],
                           [0, 3, 3, 3, 3, 3, 0],
                           [0, 3, 3, 3, 3, 3, 0],
                           [0, 3, 3, 3, 3, 3, 0],
                           [0, 3, 3, 3, 3, 3, 0],
                           [0, 3, 3, 3, 3, 3, 0],
                           [0, 0, 0, 0, 0, 0, 0]])
        image = np.zeros(labels.shape, dtype=float)
        #
        # image # 1 has a single value in one of the corners
        # whose distance is sqrt(8) from the center
        #
        image[1, 1] = 1
        # image # 2 has a single value on the top edge
        # and should have distance 2
        #
        image[7, 3] = 1
        # image # 3 has a single value on the left edge
        # and should have distance 2
        image[15, 1] = 1
        mask = np.zeros(image.shape, bool)
        mask[labels > 0] = True
        ii = II.InjectImage('MyImage', image, mask)
        ii.module_num = 1
        io = II.InjectObjects('MyObjects', labels)
        io.module_num = 2
        moi = MOI.MeasureObjectIntensity()
        moi.images[0].name.value = 'MyImage'
        moi.objects[0].name.value = 'MyObjects'
        moi.module_num = 3
        pipeline = P.Pipeline()
        pipeline.add_listener(self.error_callback)
        pipeline.add_module(ii)
        pipeline.add_module(io)
        pipeline.add_module(moi)
        m = pipeline.run()
        feature_name = '%s_%s_%s' % (MOI.INTENSITY, MOI.MASS_DISPLACEMENT, 'MyImage')
        mass_displacement = m.get_current_measurement('MyObjects', feature_name)
        self.assertEqual(np.product(mass_displacement.shape), 3)
        self.assertAlmostEqual(mass_displacement[0], math.sqrt(8.0))
        self.assertAlmostEqual(mass_displacement[1], 2.0)
        self.assertAlmostEqual(mass_displacement[2], 2.0)
    def test_03_06_quartiles(self):
        """test quartile values on a 250x250 square with 4 objects"""
        labels = np.ones((250, 250), int)
        labels[125:, :] += 1
        labels[:, 125:] += 2
        np.random.seed(0)
        image = np.random.uniform(size=(250, 250))
        #
        # Make the distributions center around .5, .25, 1/6 and .125
        #
        image /= labels.astype(float)
        ii = II.InjectImage('MyImage', image)
        ii.module_num = 1
        io = II.InjectObjects('MyObjects', labels)
        io.module_num = 2
        moi = MOI.MeasureObjectIntensity()
        moi.images[0].name.value = 'MyImage'
        moi.objects[0].name.value = 'MyObjects'
        moi.module_num = 3
        pipeline = P.Pipeline()
        pipeline.add_listener(self.error_callback)
        pipeline.add_module(ii)
        pipeline.add_module(io)
        pipeline.add_module(moi)
        m = pipeline.run()
        feature_name = '%s_%s_%s' % (MOI.INTENSITY,
                                     MOI.LOWER_QUARTILE_INTENSITY, 'MyImage')
        data = m.get_current_measurement('MyObjects', feature_name)
        self.assertAlmostEqual(data[0], 1.0 / 4.0, 2)
        self.assertAlmostEqual(data[1], 1.0 / 8.0, 2)
        self.assertAlmostEqual(data[2], 1.0 / 12.0, 2)
        self.assertAlmostEqual(data[3], 1.0 / 16.0, 2)
        feature_name = '%s_%s_%s' % (MOI.INTENSITY, MOI.MEDIAN_INTENSITY,
                                     'MyImage')
        data = m.get_current_measurement('MyObjects', feature_name)
        self.assertAlmostEqual(data[0], 1.0 / 2.0, 2)
        self.assertAlmostEqual(data[1], 1.0 / 4.0, 2)
        self.assertAlmostEqual(data[2], 1.0 / 6.0, 2)
        self.assertAlmostEqual(data[3], 1.0 / 8.0, 2)
        feature_name = '%s_%s_%s' % (MOI.INTENSITY,
                                     MOI.UPPER_QUARTILE_INTENSITY, 'MyImage')
        data = m.get_current_measurement('MyObjects', feature_name)
        self.assertAlmostEqual(data[0], 3.0 / 4.0, 2)
        self.assertAlmostEqual(data[1], 3.0 / 8.0, 2)
        self.assertAlmostEqual(data[2], 3.0 / 12.0, 2)
        self.assertAlmostEqual(data[3], 3.0 / 16.0, 2)
        feature_name = '%s_%s_%s' % (MOI.INTENSITY, MOI.MAD_INTENSITY,
                                     'MyImage')
        data = m.get_current_measurement('MyObjects', feature_name)

        self.assertAlmostEqual(data[0], 1.0 / 4.0, 2)
        self.assertAlmostEqual(data[1], 1.0 / 8.0, 2)
        self.assertAlmostEqual(data[2], 1.0 / 12.0, 2)
        self.assertAlmostEqual(data[3], 1.0 / 16.0, 2)
    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_03_02_00_one(self):
     """Check measurements on a 3x3 square of 1's"""
     img = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0],
                     [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0]])
     ii = II.InjectImage('MyImage', img.astype(float))
     ii.module_num = 1
     io = II.InjectObjects('MyObjects', img.astype(int))
     io.module_num = 2
     moi = MOI.MeasureObjectIntensity()
     moi.images[0].name.value = 'MyImage'
     moi.objects[0].name.value = 'MyObjects'
     moi.module_num = 3
     pipeline = P.Pipeline()
     pipeline.add_listener(self.error_callback)
     pipeline.add_module(ii)
     pipeline.add_module(io)
     pipeline.add_module(moi)
     m = pipeline.run()
     for category, meas_name, value in (
         (MOI.INTENSITY, MOI.INTEGRATED_INTENSITY,
          9), (MOI.INTENSITY, MOI.MEAN_INTENSITY,
               1), (MOI.INTENSITY, MOI.STD_INTENSITY,
                    0), (MOI.INTENSITY, MOI.MIN_INTENSITY,
                         1), (MOI.INTENSITY, MOI.MAX_INTENSITY, 1),
         (MOI.INTENSITY, MOI.INTEGRATED_INTENSITY_EDGE,
          8), (MOI.INTENSITY, MOI.MEAN_INTENSITY_EDGE,
               1), (MOI.INTENSITY, MOI.STD_INTENSITY_EDGE,
                    0), (MOI.INTENSITY, MOI.MIN_INTENSITY_EDGE,
                         1), (MOI.INTENSITY, MOI.MAX_INTENSITY_EDGE,
                              1), (MOI.INTENSITY, MOI.MASS_DISPLACEMENT, 0),
         (MOI.INTENSITY, MOI.LOWER_QUARTILE_INTENSITY,
          1), (MOI.INTENSITY, MOI.MEDIAN_INTENSITY,
               1), (MOI.INTENSITY, MOI.UPPER_QUARTILE_INTENSITY,
                    1), (MOI.C_LOCATION, MOI.LOC_CMI_X,
                         3), (MOI.C_LOCATION, MOI.LOC_CMI_Y, 2)):
         feature_name = "%s_%s_%s" % (category, meas_name, 'MyImage')
         data = m.get_current_measurement('MyObjects', feature_name)
         self.assertEqual(np.product(data.shape), 1)
         self.assertEqual(
             data[0], value,
             "%s expected %f != actual %f" % (meas_name, value, data[0]))
 def test_03_05_quartiles(self):
     """Regression test a bug that occurs in an image with one pixel"""
     labels = np.zeros((10, 20))
     labels[2:7, 3:8] = 1
     labels[5, 15] = 2
     np.random.seed(0)
     image = np.random.uniform(size=(10, 20))
     ii = II.InjectImage('MyImage', image)
     ii.module_num = 1
     io = II.InjectObjects('MyObjects', labels)
     io.module_num = 2
     moi = MOI.MeasureObjectIntensity()
     moi.images[0].name.value = 'MyImage'
     moi.objects[0].name.value = 'MyObjects'
     moi.module_num = 3
     pipeline = P.Pipeline()
     pipeline.add_listener(self.error_callback)
     pipeline.add_module(ii)
     pipeline.add_module(io)
     pipeline.add_module(moi)
     # Crashes when pipeline runs in measureobjectintensity.py revision 7146
     m = pipeline.run()