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 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_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_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)
Beispiel #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 make_workspace(self, labels, pixel_data, mask=None):
     image_set_list = cpi.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     image_set.add(IMAGE_NAME, cpi.Image(pixel_data, mask))
     object_set = cpo.ObjectSet()
     o = cpo.Objects()
     o.segmented = labels
     object_set.add_objects(o, OBJECT_NAME)
     pipeline = P.Pipeline()
     module = MOI.MeasureObjectIntensity()
     module.images[0].name.value = IMAGE_NAME
     module.objects[0].name.value = OBJECT_NAME
     module.module_num = 1
     pipeline.add_listener(self.error_callback)
     pipeline.add_module(module)
     workspace = cpw.Workspace(pipeline, module, image_set,
                               object_set, cpmeas.Measurements(),
                               image_set_list)
     return workspace, module
 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]))
Beispiel #10
0
 def test_02_02_get_measurement_columns(self):
     '''test the get_measurement_columns method'''
     moi = MOI.MeasureObjectIntensity()
     moi.images[0].name.value = 'MyImage'
     moi.add_object()
     moi.objects[0].name.value = 'MyObjects1'
     moi.objects[1].name.value = 'MyObjects2'
     columns = moi.get_measurement_columns(None)
     self.assertEqual(len(columns), 2 * len(MOI.ALL_MEASUREMENTS) + 2 * len(MOI.ALL_LOCATION_MEASUREMENTS))
     for column in columns:
         self.assertTrue(column[0] in ('MyObjects1', 'MyObjects2'))
         self.assertEqual(column[2], cpmeas.COLTYPE_FLOAT)
         category = column[1].split('_')[0]
         self.assertTrue(category in (MOI.INTENSITY, MOI.C_LOCATION))
         if category == MOI.INTENSITY:
             self.assertTrue(column[1][column[1].find('_') + 1:] in
                             [m + '_MyImage' for m in MOI.ALL_MEASUREMENTS])
         else:
             self.assertTrue(column[1][column[1].find('_') + 1:] in
                             [m + '_MyImage' for m in MOI.ALL_LOCATION_MEASUREMENTS])
 def test_02_01_supplied_measurements(self):
     """Test the get_category / get_measurements, get_measurement_images functions"""
     
     moi = MOI.MeasureObjectIntensity()
     moi.images[0].name.value='MyImage'
     moi.add_object()
     moi.objects[0].name.value = 'MyObjects1'
     moi.objects[1].name.value = 'MyObjects2'
     
     self.assertEqual(tuple(sorted(moi.get_categories(None, 'MyObjects1'))), tuple(sorted([MOI.INTENSITY, MOI.C_LOCATION])))
     self.assertEqual(moi.get_categories(None, 'Foo'),[])
     measurements = moi.get_measurements(None,'MyObjects1',MOI.INTENSITY)
     self.assertEqual(len(measurements),len(MOI.ALL_MEASUREMENTS))
     measurements = moi.get_measurements(None,'MyObjects1',MOI.C_LOCATION)
     self.assertEqual(len(measurements),len(MOI.ALL_LOCATION_MEASUREMENTS))
     self.assertTrue(all([m in MOI.ALL_LOCATION_MEASUREMENTS for m in measurements]))
     self.assertTrue(moi.get_measurement_images(None,'MyObjects1',
                                                MOI.INTENSITY,
                                                MOI.MAX_INTENSITY),
                     ['MyImage'])
 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()