コード例 #1
0
ファイル: runner.py プロジェクト: jni/cecog
    def _load_classdef(self, region):

        with cellh5.ch5open(self.ch5file, "r", cached=True) as ch5:
            cld = ch5.class_definition(region)
            classdef = ClassDefinition(cld)

        return classdef
コード例 #2
0
 def test_03_01_export_objects(self):
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     labels = np.zeros((21, 17), int)
     labels[1:4, 2:5] = 1
     labels[11:14, 12:15] = 2
     centers = np.array([[2, 3], [12, 13]])
     minima = np.array([[1, 2], [11, 12]])
     maxima = np.array([[3, 4], [13, 14]])
     objects = cpo.Objects()
     objects.segmented = labels
     workspace.object_set.add_objects(objects, OBJECTS_NAME)
     module.add_objects()
     module.objects_to_export[0].objects_name.value = OBJECTS_NAME
     module.run(workspace)
     with cellh5.ch5open(
             os.path.join(self.temp_dir, module.file_name.value),
             "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         image_defs = ch5.image_definition
         object_channels = image_defs[cellh5.CH5Const.REGION]
         self.assertEqual(len(object_channels), 1)
         self.assertEqual(object_channels[0, "region_name"], OBJECTS_NAME)
         self.assertEqual(object_channels[0, "channel_idx"], 0)
         centers_out = pos.get_center([0, 1], OBJECTS_NAME)
         np.testing.assert_array_equal(centers_out[:]["x"], centers[:, 1])
         np.testing.assert_array_equal(centers_out[:]["y"], centers[:, 0])
         self.assertEqual(len(pos.get_object_table(OBJECTS_NAME)), 2)
         labels_out = \
             pos[cellh5.CH5Const.IMAGE][cellh5.CH5Const.REGION][0, 0, 0]
         np.testing.assert_array_equal(labels, labels_out)
コード例 #3
0
 def test_02_02_export_rgb_image(self):
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     r = np.random.RandomState()
     r.seed(202)
     image = cpi.Image(r.uniform(size=(20, 35, 3)), scale=255)
     workspace.image_set.add(IMAGE_NAME, image)
     module.add_image()
     module.images_to_export[0].image_name.value = IMAGE_NAME
     module.run(workspace)
     with cellh5.ch5open(
             os.path.join(self.temp_dir, module.file_name.value),
             "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         image_defs = ch5.image_definition
         image_channels = image_defs[cellh5.CH5Const.RAW_IMAGE]
         self.assertEqual(len(image_channels), 3)
         for i, image_channel in enumerate(image_channels):
             self.assertEqual(image_channel["channel_name"], "_".join(
                 (IMAGE_NAME, E.COLORS[i][0])))
             image_out = pos.get_image(0, i)
             np.testing.assert_array_equal(
                 (image.pixel_data[:, :, i] * 255).astype(int), image_out)
コード例 #4
0
 def test_02_03_export_int16_image(self):
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     r = np.random.RandomState()
     r.seed(203)
     image = cpi.Image(r.uniform(size=(20, 35)), scale=4095)
     workspace.image_set.add(IMAGE_NAME, image)
     module.add_image()
     module.images_to_export[0].image_name.value = IMAGE_NAME
     module.run(workspace)
     with cellh5.ch5open(
             os.path.join(self.temp_dir, module.file_name.value),
             "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         image_defs = ch5.image_definition
         image_channels = image_defs[cellh5.CH5Const.RAW_IMAGE]
         self.assertEqual(len(image_channels), 1)
         self.assertEqual(image_channels[0, "channel_name"], IMAGE_NAME)
         image_out = pos.get_image(0, 0)
         self.assertTrue(np.issubdtype(image_out.dtype, np.uint16))
         np.testing.assert_array_equal(
             (image.pixel_data * 4095).astype(int), image_out)
コード例 #5
0
    def _load_classdef(self, region):

        with cellh5.ch5open(self.ch5file, "r", cached=True) as ch5:
            cld = ch5.class_definition(region)
            classdef = ClassDefinition(cld)

        return classdef
コード例 #6
0
 def test_03_01_export_objects(self):
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     labels = np.zeros((21, 17), int)
     labels[1:4, 2:5] = 1
     labels[11:14, 12:15] = 2
     centers = np.array([[2, 3], [12, 13]])
     minima = np.array([[1, 2], [11, 12]])
     maxima = np.array([[3, 4], [13, 14]])
     objects = cpo.Objects()
     objects.segmented = labels
     workspace.object_set.add_objects(objects, OBJECTS_NAME)
     module.add_objects()
     module.objects_to_export[0].objects_name.value = OBJECTS_NAME
     module.run(workspace)
     with cellh5.ch5open(os.path.join(self.temp_dir, module.file_name.value), "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         image_defs = ch5.image_definition
         object_channels = image_defs[cellh5.CH5Const.REGION]
         self.assertEqual(len(object_channels), 1)
         self.assertEqual(object_channels[0, "region_name"], OBJECTS_NAME)
         self.assertEqual(object_channels[0, "channel_idx"], 0)
         centers_out = pos.get_center([0, 1], OBJECTS_NAME)
         np.testing.assert_array_equal(centers_out[:]["x"], centers[:, 1])
         np.testing.assert_array_equal(centers_out[:]["y"], centers[:, 0])
         self.assertEqual(len(pos.get_object_table(OBJECTS_NAME)), 2)
         labels_out = pos[cellh5.CH5Const.IMAGE][cellh5.CH5Const.REGION][0, 0, 0]
         np.testing.assert_array_equal(labels, labels_out)
コード例 #7
0
 def test_04_02_some_features(self):
     r = np.random.RandomState()
     r.seed(402)
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     labels = np.zeros((21, 17), int)
     labels[1:4, 2:5] = 1
     labels[11:14, 12:15] = 2
     centers = np.array([[2, 3], [12, 13]])
     minima = np.array([[1, 2], [11, 12]])
     maxima = np.array([[3, 4], [13, 14]])
     objects = cpo.Objects()
     objects.segmented = labels
     workspace.object_set.add_objects(objects, OBJECTS_NAME)
     module.add_objects()
     module.objects_to_export[0].objects_name.value = OBJECTS_NAME
     module.wants_to_choose_measurements.value = True
     module.measurements.set_value([
         module.measurements.make_measurement_choice(
             OBJECTS_NAME, FEATURE1)
     ])
     m = workspace.measurements
     m[OBJECTS_NAME, FEATURE1] = r.uniform(size=2)
     m[OBJECTS_NAME, FEATURE2] = r.uniform(size=2)
     workspace.pipeline.extra_measurement_columns += [
         (OBJECTS_NAME, FEATURE1, cpmeas.COLTYPE_FLOAT),
         (OBJECTS_NAME, FEATURE2, cpmeas.COLTYPE_FLOAT)
     ]
     module.run(workspace)
     with cellh5.ch5open(
             os.path.join(self.temp_dir, module.file_name.value),
             "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         defs = pos.object_feature_def(OBJECTS_NAME)
         self.assertTrue(FEATURE1 in defs)
         self.assertFalse(FEATURE2 in defs)
         feature1 = pos.get_object_features(OBJECTS_NAME)\
             [:, defs.index(FEATURE1)]
         np.testing.assert_almost_equal(feature1, m[OBJECTS_NAME, FEATURE1])
コード例 #8
0
 def test_02_01_export_image(self):
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     r = np.random.RandomState()
     r.seed(201)
     image = cpi.Image(r.uniform(size=(20, 35)), scale=255)
     workspace.image_set.add(IMAGE_NAME, image)
     module.add_image()
     module.images_to_export[0].image_name.value = IMAGE_NAME
     module.run(workspace)
     with cellh5.ch5open(os.path.join(self.temp_dir, module.file_name.value), "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         image_defs = ch5.image_definition
         image_channels = image_defs[cellh5.CH5Const.RAW_IMAGE]
         self.assertEqual(len(image_channels), 1)
         self.assertEqual(image_channels[0, "channel_name"], IMAGE_NAME)
         image_out = pos.get_image(0, 0)
         np.testing.assert_array_equal((image.pixel_data * 255).astype(int), image_out)
コード例 #9
0
 def test_04_01_all_features(self):
     r = np.random.RandomState()
     r.seed(401)
     module, workspace = self.prepare_workspace()
     assert isinstance(module, E.ExportToCellH5)
     labels = np.zeros((21, 17), int)
     labels[1:4, 2:5] = 1
     labels[11:14, 12:15] = 2
     centers = np.array([[2, 3], [12, 13]])
     minima = np.array([[1, 2], [11, 12]])
     maxima = np.array([[3, 4], [13, 14]])
     objects = cpo.Objects()
     objects.segmented = labels
     workspace.object_set.add_objects(objects, OBJECTS_NAME)
     module.add_objects()
     module.objects_to_export[0].objects_name.value = OBJECTS_NAME
     m = workspace.measurements
     m[OBJECTS_NAME, FEATURE1] = r.uniform(size=2)
     m[OBJECTS_NAME, FEATURE2] = r.uniform(size=2)
     workspace.pipeline.extra_measurement_columns += [
         (OBJECTS_NAME, FEATURE1, cpmeas.COLTYPE_FLOAT),
         (OBJECTS_NAME, FEATURE2, cpmeas.COLTYPE_FLOAT),
     ]
     module.run(workspace)
     with cellh5.ch5open(os.path.join(self.temp_dir, module.file_name.value), "r") as ch5:
         well = self.get_well_name(0)
         site = self.get_site_name(0)
         self.assertTrue(ch5.has_position(well, site))
         pos = ch5.get_position(well, site)
         defs = pos.object_feature_def(OBJECTS_NAME)
         self.assertTrue(all([x in defs for x in (FEATURE1, FEATURE2)]))
         feature1_idx, feature2_idx = [defs.index(ftr) for ftr in (FEATURE1, FEATURE2)]
         ftr_out = pos.get_object_features(OBJECTS_NAME)
         feature1 = ftr_out[:, feature1_idx]
         np.testing.assert_almost_equal(feature1, m[OBJECTS_NAME, FEATURE1])
         feature2 = ftr_out[:, feature2_idx]
         np.testing.assert_almost_equal(feature2, m[OBJECTS_NAME, FEATURE2])
コード例 #10
0
ファイル: runner.py プロジェクト: jni/cecog
    def __call__(self):
        self._makedirs()

        mappings = PlateMapping([splitext(basename(f))[0] for f in self.files])

        if self.ecopts.position_labels:
            mpfile = join(self.ecopts.mapping_dir, "%s.txt" %self.plate)
            mappings.read(mpfile)

        for channel in self.ecopts.regionnames.keys():
            dtable, cld = self._load_data(mappings, channel)
            msg = 'performing error correction on channel %s' %channel
            self.interruption_point(msg)

            # error correction
            if self.ecopts.hmm_algorithm == self.ecopts.HMM_BAUMWELCH:
                hmm = HmmSklearn(dtable, channel, cld, self.ecopts)
            else:
                hmm = HmmTde(dtable, channel, cld, self.ecopts)

            data = hmm()

            # plots and export
            report = HmmReport(data, self.ecopts, cld, self._hmm_dir)
            prefix = "%s_%s" %(channel.title(), self.ecopts.regionnames[channel])
            sby = self.ecopts.sortby.replace(" ", "_")

            self.interruption_point("plotting overview")
            report.overview(join(self._hmm_dir, '%s-%s.pdf' %(prefix, sby)))
            report.close_figures()

            self.interruption_point("plotting bar- and boxplots")
            report.bars_and_boxes(join(self._hmm_dir, '%s-%s_boxbars.pdf'
                                       %(prefix, sby)))
            report.close_figures()

            self.interruption_point("plotting hmm model")
            report.hmm_model(join(self._hmm_dir, "%s-%s_model.pdf")
                             %(prefix, sby))


            if self.ecopts.write_gallery:
                self.interruption_point("plotting image gallery")
                try:
                    # replace image_gallery_png with image_gallery_pdf
                    fn = join(self._gallery_dir,
                              '%s-%s_gallery.png' %(prefix, sby))

                    with cellh5.ch5open(self.ch5file, 'r') as ch5:
                        report.image_gallery_png(ch5, fn, self.ecopts.n_galleries,
                                                 self.ecopts.resampling_factor,
                                                 self.ecopts.size_gallery_image)
                        report.close_figures()
                except Exception as e: # don't stop error corection
                    with open(join(self._gallery_dir, '%s-%s_error_readme.txt'
                                   %(prefix, sby)), 'w') as fp:
                        traceback.print_exc(file=fp)
                        fp.write("Check if gallery images exist!")

            report.export_hmm(join(self._hmm_dir, "%s-%s_hmm.csv"
                                   %(prefix, sby)),
                              self.ecopts.sortby)
コード例 #11
0
    def __call__(self):
        self._makedirs()

        mappings = PlateMapping([splitext(basename(f))[0] for f in self.files])

        if self.ecopts.position_labels:
            mpfile = join(self.ecopts.mapping_dir, "%s.txt" % self.plate)
            mappings.read(mpfile)

        for channel in self.ecopts.regionnames.keys():
            dtable, cld = self._load_data(mappings, channel)
            msg = 'performing error correction on channel %s' % channel
            self.interruption_point(msg)

            # error correction
            if self.ecopts.hmm_algorithm == self.ecopts.HMM_BAUMWELCH:
                hmm = HmmSklearn(dtable, channel, cld, self.ecopts)
            else:
                hmm = HmmTde(dtable, channel, cld, self.ecopts)

            data = hmm()

            # plots and export
            report = HmmReport(data, self.ecopts, cld, self._hmm_dir)
            prefix = "%s_%s" % (channel.title(),
                                self.ecopts.regionnames[channel])
            sby = self.ecopts.sortby.replace(" ", "_")

            self.interruption_point("plotting overview")
            report.overview(join(self._hmm_dir, '%s-%s.pdf' % (prefix, sby)))
            report.close_figures()

            self.interruption_point("plotting bar- and boxplots")
            report.bars_and_boxes(
                join(self._hmm_dir, '%s-%s_boxbars.pdf' % (prefix, sby)))
            report.close_figures()

            self.interruption_point("plotting hmm model")
            report.hmm_model(
                join(self._hmm_dir, "%s-%s_model.pdf") % (prefix, sby))

            if self.ecopts.write_gallery:
                self.interruption_point("plotting image gallery")
                try:
                    # replace image_gallery_png with image_gallery_pdf
                    fn = join(self._gallery_dir,
                              '%s-%s_gallery.png' % (prefix, sby))

                    with cellh5.ch5open(self.ch5file, 'r') as ch5:
                        report.image_gallery_png(
                            ch5, fn, self.ecopts.n_galleries,
                            self.ecopts.resampling_factor,
                            self.ecopts.size_gallery_image)
                        report.close_figures()
                except Exception as e:  # don't stop error corection
                    with open(
                            join(self._gallery_dir,
                                 '%s-%s_error_readme.txt' % (prefix, sby)),
                            'w') as fp:
                        traceback.print_exc(file=fp)
                        fp.write("Check if gallery images exist!")

            report.export_hmm(
                join(self._hmm_dir, "%s-%s_hmm.csv" % (prefix, sby)),
                self.ecopts.sortby)