def test_03_04_object_measurement_columns(self):
        module = L.LoadSingleImage()
        module.file_settings[0].image_objects_choice.value = L.IO_OBJECTS
        module.file_settings[0].objects_name.value = OBJECTS_NAME
        columns = module.get_measurement_columns(None)
        expected_columns = ((cpmeas.IMAGE,
                             L.C_OBJECTS_FILE_NAME + "_" + OBJECTS_NAME),
                            (cpmeas.IMAGE,
                             L.C_OBJECTS_PATH_NAME + "_" + OBJECTS_NAME),
                            (cpmeas.IMAGE, L.C_COUNT + "_" + OBJECTS_NAME),
                            (OBJECTS_NAME,
                             L.C_LOCATION + "_" + L.FTR_CENTER_X),
                            (OBJECTS_NAME,
                             L.C_LOCATION + "_" + L.FTR_CENTER_Y),
                            (OBJECTS_NAME,
                             L.C_NUMBER + "_" + L.FTR_OBJECT_NUMBER))
        for expected_column in expected_columns:
            self.assertTrue(
                any([
                    column[0] == expected_column[0]
                    and column[1] == expected_column[1] for column in columns
                ]))

        for column in columns:
            self.assertTrue(
                any([
                    column[0] == expected_column[0]
                    and column[1] == expected_column[1]
                    for expected_column in expected_columns
                ]))
    def test_04_01_load_objects(self):
        r = np.random.RandomState()
        r.seed(41)
        labels = np.random.randint(0, 10, size=(30, 40))
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cpprefs.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = L.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = L.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cpmeas.Measurements()
            object_set = cpo.ObjectSet()
            image_set_list = cpi.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cpw.Workspace(pipeline, module, image_set, object_set,
                                      m, image_set_list)
            module.prepare_run(workspace)
            module.run(workspace)

            o = object_set.get_objects(OBJECTS_NAME)
            np.testing.assert_equal(labels, o.segmented)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (L.C_COUNT, OBJECTS_NAME))), 9)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (L.C_OBJECTS_FILE_NAME, OBJECTS_NAME))), filename)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (L.C_OBJECTS_PATH_NAME, OBJECTS_NAME))), directory)
            for feature in (M_LOCATION_CENTER_X, M_LOCATION_CENTER_Y,
                            M_NUMBER_OBJECT_NUMBER):
                values = m.get_current_measurement(OBJECTS_NAME, feature)
                self.assertEqual(len(values), 9)
        finally:
            try:
                os.remove(os.path.join(directory, filename))
                os.rmdir(directory)
            except:
                print "Failed to delete directory " + directory
 def test_03_05_object_categories(self):
     module = L.LoadSingleImage()
     module.file_settings[0].image_objects_choice.value = L.IO_OBJECTS
     module.file_settings[0].objects_name.value = OBJECTS_NAME
     for object_name, expected_categories in (
         (cpmeas.IMAGE, (L.C_COUNT, L.C_OBJECTS_FILE_NAME, L.C_OBJECTS_PATH_NAME)),
         (OBJECTS_NAME, (L.C_NUMBER, L.C_LOCATION))):
         categories = module.get_categories(None, object_name)
         self.assertTrue(all([category in expected_categories
                              for category in categories]))
         self.assertTrue(all([expected_category in categories
                              for expected_category in expected_categories]))
 def test_03_06_object_measurements(self):
     module = L.LoadSingleImage()
     module.file_settings[0].image_objects_choice.value = L.IO_OBJECTS
     module.file_settings[0].objects_name.value = OBJECTS_NAME
     for object_name, category, expected_features in (
         (cpmeas.IMAGE, L.C_COUNT, (OBJECTS_NAME,)),
         (cpmeas.IMAGE, L.C_OBJECTS_FILE_NAME, (OBJECTS_NAME,)),
         (cpmeas.IMAGE, L.C_OBJECTS_PATH_NAME, (OBJECTS_NAME,)),
         (OBJECTS_NAME, L.C_NUMBER, (L.FTR_OBJECT_NUMBER, )),
         (OBJECTS_NAME, L.C_LOCATION, (L.FTR_CENTER_X, L.FTR_CENTER_Y))):
         features = module.get_measurements(None, object_name, category)
         self.assertTrue(all([feature in expected_features
                              for feature in features]))
         self.assertTrue(all([expected_feature in features
                              for expected_feature in  expected_features]))
 def test_02_03_prepare_run(self):
     # regression test for issue #673 and #1161
     #
     # If LoadSingleImage appears first, pathname data does not show
     # up in the measurements.
     #
     maybe_download_sbs()
     folder = "ExampleSBSImages"
     path = os.path.join(example_images_directory(), folder)
     filename = "Channel1-01-A-01.tif"
     pipeline = cpp.Pipeline()
     lsi = L.LoadSingleImage()
     lsi.module_num = 1
     lsi.directory.dir_choice = cps.ABSOLUTE_FOLDER_NAME
     lsi.directory.custom_path = path
     lsi.file_settings[0].image_name.value = self.get_image_name(0)
     lsi.file_settings[0].file_name.value = filename
     pipeline.add_module(lsi)
     li = LI.LoadImages()
     li.module_num = 2
     pipeline.add_module(li)
     li.match_method.value = LI.MS_EXACT_MATCH
     li.location.dir_choice = cps.ABSOLUTE_FOLDER_NAME
     li.location.custom_path = path
     li.images[0].common_text.value = "Channel2-"
     m = cpmeas.Measurements()
     workspace = cpw.Workspace(pipeline, lsi, m, cpo.ObjectSet(), m,
                               cpi.ImageSetList())
     self.assertTrue(pipeline.prepare_run(workspace))
     self.assertGreater(m.image_set_count, 1)
     pipeline.prepare_group(workspace, {}, m.get_image_numbers())
     #
     # Skip to the second image set
     #
     m.next_image_set(2)
     lsi.run(workspace)
     #
     # Are the measurements populated?
     #
     m_file = "_".join((cpmeas.C_FILE_NAME, self.get_image_name(0)))
     self.assertEqual(m[cpmeas.IMAGE, m_file, 2], filename)
     #
     # Can we retrieve the image?
     #
     pixel_data = m.get_image(self.get_image_name(0)).pixel_data
     self.assertFalse(np.isscalar(pixel_data))
    def test_04_02_object_outlines(self):
        labels = np.zeros((30, 40), int)
        labels[10:15, 20:30] = 1
        expected_outlines = labels != 0
        expected_outlines[11:14, 21:29] = False
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cpprefs.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = L.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = L.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            fs.wants_outlines.value = True
            fs.outlines_name.value = OUTLINES_NAME
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cpmeas.Measurements()
            object_set = cpo.ObjectSet()
            image_set_list = cpi.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cpw.Workspace(pipeline, module, image_set, object_set,
                                      m, image_set_list)
            module.prepare_run(workspace)
            module.run(workspace)

            outlines = image_set.get_image(OUTLINES_NAME)
            np.testing.assert_equal(outlines.pixel_data, expected_outlines)
        finally:
            try:
                os.remove(os.path.join(directory, filename))
                os.rmdir(directory)
            except:
                print "Failed to delete directory " + directory
 def make_workspace(self, file_names):
     module = L.LoadSingleImage()
     module.module_num = 1
     for i, file_name in enumerate(file_names):
         if i > 0:
             module.add_file()
         module.file_settings[i].image_name.value = self.get_image_name(i)
         module.file_settings[i].file_name.value = file_name
     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()
     workspace = cpw.Workspace(pipeline, module, 
                               image_set_list.get_image_set(0),
                               cpo.ObjectSet(), cpmeas.Measurements(),
                               image_set_list)
     return workspace, module