예제 #1
0
    def run(self, workspace):
        image_set = workspace.image_set
        if self.source_choice == IO_OBJECTS:
            objects = workspace.get_objects(self.object_name.value)
            labels = objects.segmented
            if self.invert_mask.value:
                mask = labels == 0
            else:
                mask = labels > 0
        else:
            objects = None
            try:
                mask = image_set.get_image(self.masking_image_name.value,
                                           must_be_binary=True).pixel_data
            except ValueError:
                mask = image_set.get_image(self.masking_image_name.value,
                                           must_be_grayscale=True).pixel_data
                mask = mask > .5
            if self.invert_mask.value:
                mask = mask == 0
        orig_image = image_set.get_image(self.image_name.value)
        if (orig_image.multichannel
                and mask.shape != orig_image.pixel_data.shape[:-1]
            ) or mask.shape != orig_image.pixel_data.shape:
            tmp = np.zeros(orig_image.pixel_data.shape[:2], mask.dtype)
            tmp[mask] = True
            mask = tmp
        if orig_image.has_mask:
            mask = np.logical_and(mask, orig_image.mask)
        masked_pixels = orig_image.pixel_data.copy()
        masked_pixels[np.logical_not(mask)] = 0
        masked_image = cpi.Image(masked_pixels,
                                 mask=mask,
                                 parent_image=orig_image,
                                 masking_objects=objects,
                                 dimensions=orig_image.dimensions)

        image_set.add(self.masked_image_name.value, masked_image)

        if self.show_window:
            workspace.display_data.dimensions = orig_image.dimensions
            workspace.display_data.orig_image_pixel_data = orig_image.pixel_data
            workspace.display_data.masked_pixels = masked_pixels
            workspace.display_data.multichannel = orig_image.multichannel
예제 #2
0
    def test_01_03_test_two_dark_objects(self):
        x = YS.IdentifyYeastCells()
        x.object_name.value = OBJECTS_NAME
        x.segmentation_precision.value = 11
        x.input_image_name.value = IMAGE_NAME
        x.background_brighter_then_cell_inside.value = True
        x.average_cell_diameter.value = 10
        img = convert_to_brightfield(get_two_cell_mask(), True)
        image = cpi.Image(img, file_name="test_01_03_test_two_dark_objects")
        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image))
        object_set = cpo.ObjectSet()
        measurements = cpmeas.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        x.run(Workspace(pipeline, x, image_set, object_set, measurements,
                        None))
        self.assertEqual(len(object_set.object_names), 1)
        self.assertTrue(OBJECTS_NAME in object_set.object_names)
        objects = object_set.get_objects(OBJECTS_NAME)
        self.assertTrue(
            is_segmentation_correct(get_two_cell_mask(), objects.segmented))
        self.assertTrue("Image" in measurements.get_object_names())
        self.assertTrue(OBJECTS_NAME in measurements.get_object_names())
        self.assertTrue(
            "Features_Quality" in measurements.get_feature_names(OBJECTS_NAME))
        quality = measurements.get_current_measurement(OBJECTS_NAME,
                                                       "Features_Quality")
        self.assertTrue(len(quality) == 2)
        self.assertTrue("Location_Center_Y" in measurements.get_feature_names(
            OBJECTS_NAME))
        location_center_y = measurements.get_current_measurement(
            OBJECTS_NAME, "Location_Center_Y")
        location_center_x = measurements.get_current_measurement(
            OBJECTS_NAME, "Location_Center_X")
        positions = sorted(zip(location_center_x, location_center_y))
        self.assertEqual(2, len(positions))

        self.assertRange(3, 18, positions[0][0])
        self.assertRange(25, 45, positions[0][1])

        self.assertRange(20, 40, positions[1][0])
        self.assertRange(5, 25, positions[1][1])
예제 #3
0
 def make_workspace(self, image, mask):
     """Make a workspace for testing FilterByObjectMeasurement"""
     module = S.Smooth()
     pipeline = cpp.Pipeline()
     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,
         cpmeas.Measurements(),
         image_set_list,
     )
     image_set.add(INPUT_IMAGE_NAME, cpi.Image(image, mask))
     module.image_name.value = INPUT_IMAGE_NAME
     module.filtered_image_name.value = OUTPUT_IMAGE_NAME
     return workspace, module
예제 #4
0
    def prepare_run(self, message, session_id, grouping_allowed=False):
        '''Prepare a pipeline and measurements to run

        message - the run-request or run-groups-request message
        session_id - the session ID for the session
        grouping_allowed - true to allow grouped images
        '''
        pipeline = cpp.Pipeline()
        m = cpmeas.Measurements()
        object_set = cpo.ObjectSet()
        if len(message) < 2:
            self.raise_cellprofiler_exception(session_id,
                                              "Missing run request sections")
            return
        pipeline_txt = message.pop(0).bytes
        image_metadata = message.pop(0).bytes
        try:
            image_metadata = json.loads(image_metadata)
            for channel_name, channel_metadata in image_metadata:
                if len(message) < 1:
                    self.raise_cellprofiler_exception(
                        session_id,
                        "Missing binary data for channel %s" % channel_name)
                    return None, None, None
                pixel_data = self.decode_image(
                    channel_metadata,
                    message.pop(0).bytes,
                    grouping_allowed=grouping_allowed)
                m.add(channel_name, cpi.Image(pixel_data))
        except Exception as e:
            logger.warn("Failed to decode message", exc_info=1)
            self.raise_cellprofiler_exception(session_id, e.message)
            return None, None, None
        try:
            pipeline.loadtxt(StringIO(pipeline_txt))
        except Exception as e:
            logger.warning(
                "Failed to load pipeline: sending pipeline exception",
                exc_info=1)
            self.raise_pipeline_exception(session_id, str(e))
            return None, None, None

        return pipeline, m, object_set
예제 #5
0
 def test_06_04_wrong_size(self):
     '''Regression test of IMG-961 - objects and images of different sizes'''
     np.random.seed(0)
     image1 = np.random.uniform(size=(20, 20))
     i1 = cpi.Image(image1)
     labels = np.zeros((10, 30), int)
     labels[:4, :4] = 1
     labels[6:, 6:] = 2
     o = cpo.Objects()
     o.segmented = labels
     workspace, module = self.make_workspace(i1, i1, o)
     module.run(workspace)
     m = workspace.measurements
     mi = module.get_measurement_images(None, OBJECTS_NAME, "Correlation",
                                        "Correlation")
     corr = m.get_current_measurement(OBJECTS_NAME,
                                      "Correlation_Correlation_%s" % mi[0])
     self.assertAlmostEqual(corr[0], 1)
     self.assertAlmostEqual(corr[1], 1)
예제 #6
0
    def make_workspace(self, image, outlier_percentile):
        '''Make a workspace '''
        module = C.ClipRange()
        pipeline = cpp.Pipeline()
        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,
                                  cpmeas.Measurements(), image_set_list)

        # setup the input images
        image_set.add(INPUT_IMAGE_NAME, cpi.Image(image))

        # setup the input images settings
        module.x_name.value = INPUT_IMAGE_NAME
        module.y_name.value = OUTPUT_IMAGE_NAME
        module.outlier_percentile.value = outlier_percentile

        return workspace, module
예제 #7
0
 def test_03_01_image_casts(self):
     one_target = np.ones((10, 10), dtype=np.float64)
     zero_target = np.zeros((10, 10), dtype=np.float64)
     tests = [(np.float64, 0, 1.0), (np.float32, 0, 1.0),
              (np.uint32, 0, math.pow(2.0, 32.0) - 1),
              (np.uint16, 0, math.pow(2.0, 16.0) - 1),
              (np.uint8, 0, math.pow(2.0, 8.0) - 1),
              (np.int32, -math.pow(2.0, 31.0), math.pow(2.0, 31.0) - 1),
              (np.int16, -math.pow(2.0, 15.0), math.pow(2.0, 15.0) - 1),
              (np.int8, -math.pow(2.0, 7.0), math.pow(2.0, 7.0) - 1)]
     for dtype, zval, oval in tests:
         x = cpi.Image()
         x.set_image((one_target * zval).astype(dtype))
         self.assertTrue((x.image == zero_target).all(),
                         msg="Failed setting %s to min" % (repr(dtype)))
         x.set_image((one_target * oval).astype(dtype))
         y = (x.image == one_target)
         self.assertTrue((x.image == one_target).all(),
                         msg="Failed setting %s to max" % (repr(dtype)))
예제 #8
0
 def run(self, workspace):
     image = workspace.image_set.get_image(self.image_name.value)
     if image.has_mask:
         mask = image.mask
     else:
         mask = None
     pixel_data = image.pixel_data
     if pixel_data.ndim == 3:
         if any([np.any(pixel_data[:, :, 0] != pixel_data[:, :, plane])
                 for plane in range(1, pixel_data.shape[2])]):
             logger.warn("Image is color, converting to grayscale")
         pixel_data = np.sum(pixel_data, 2) / pixel_data.shape[2]
     for function in self.functions:
         pixel_data = self.run_function(function, pixel_data, mask)
     new_image = cpi.Image(pixel_data, parent_image=image)
     workspace.image_set.add(self.output_image_name.value, new_image)
     if self.show_window:
         workspace.display_data.image = image.pixel_data
         workspace.display_data.pixel_data = pixel_data
예제 #9
0
    def test_02_02_mask_invert(self):
        labels = np.zeros((10, 15), int)
        labels[2:5, 3:8] = 1
        labels[5:8, 10:14] = 2
        object_set = cpo.ObjectSet()
        objects = cpo.Objects()
        objects.segmented = labels
        object_set.add_objects(objects, OBJECTS_NAME)

        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        np.random.seed(0)
        pixel_data = np.random.uniform(size=(10, 15)).astype(np.float32)
        image_set.add(IMAGE_NAME, cpi.Image(pixel_data))

        pipeline = cpp.Pipeline()
        module = M.MaskImage()
        module.source_choice.value = M.IO_OBJECTS
        module.object_name.value = OBJECTS_NAME
        module.image_name.value = IMAGE_NAME
        module.masked_image_name.value = MASKED_IMAGE_NAME
        module.invert_mask.value = True
        module.module_num = 1

        workspace = cpw.Workspace(
            pipeline,
            module,
            image_set,
            object_set,
            cpmeas.Measurements(),
            image_set_list,
        )
        module.run(workspace)
        masked_image = workspace.image_set.get_image(MASKED_IMAGE_NAME)
        self.assertTrue(isinstance(masked_image, cpi.Image))
        self.assertTrue(
            np.all(masked_image.pixel_data[labels == 0] == pixel_data[labels ==
                                                                      0]))
        self.assertTrue(np.all(masked_image.pixel_data[labels > 0] == 0))
        self.assertTrue(np.all(masked_image.mask == (labels == 0)))
        self.assertTrue(
            np.all(masked_image.masking_objects.segmented == labels))
    def run(self, workspace):
        image_name = self.image_name.value
        objects_name = self.objects_name.value
        outlines_name = self.outlines_name.value
        image = workspace.image_set.get_image(image_name)
        pixel_data = image.pixel_data

        labels = workspace.interaction_request(
            self, pixel_data, workspace.measurements.image_set_number)
        if labels is None:
            # User cancelled. Soldier on as best we can.
            workspace.cancel_request()
            labels = np.zeros(pixel_data.shape[:2], int)
        objects = cpo.Objects()
        objects.segmented = labels
        workspace.object_set.add_objects(objects, objects_name)

        ##################
        #
        # Add measurements
        #
        m = workspace.measurements
        #
        # The object count
        #
        object_count = np.max(labels)
        I.add_object_count_measurements(m, objects_name, object_count)
        #
        # The object locations
        #
        I.add_object_location_measurements(m, objects_name, labels)
        #
        # Outlines if we want them
        #
        if self.wants_outlines:
            outlines_name = self.outlines_name.value
            outlines = outline(labels)
            outlines_image = cpi.Image(outlines.astype(bool))
            workspace.image_set.add(outlines_name, outlines_image)

        workspace.display_data.labels = labels
        workspace.display_data.pixel_data = pixel_data
    def make_workspace(self, measurement, labels=None, image=None):
        object_set = cpo.ObjectSet()
        module = D.DisplayDataOnImage()
        module.module_num = 1
        module.image_name.value = INPUT_IMAGE_NAME
        module.display_image.value = OUTPUT_IMAGE_NAME
        module.objects_name.value = OBJECTS_NAME
        m = cpmeas.Measurements()

        if labels is None:
            module.objects_or_image.value = D.OI_IMAGE
            m.add_image_measurement(MEASUREMENT_NAME, measurement)
            if image is None:
                image = np.zeros((50, 120))
        else:
            module.objects_or_image.value = D.OI_OBJECTS
            o = cpo.Objects()
            o.segmented = labels
            object_set.add_objects(o, OBJECTS_NAME)
            m.add_measurement(OBJECTS_NAME, MEASUREMENT_NAME,
                              np.array(measurement))
            y, x = centers_of_labels(labels)
            m.add_measurement(OBJECTS_NAME, "Location_Center_X", x)
            m.add_measurement(OBJECTS_NAME, "Location_Center_Y", y)
            if image is None:
                image = np.zeros(labels.shape)
        module.measurement.value = MEASUREMENT_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()
        image_set = image_set_list.get_image_set(0)
        image_set.add(INPUT_IMAGE_NAME, cpi.Image(image))

        workspace = cpw.Workspace(pipeline, module, image_set, object_set, m,
                                  image_set_list)
        return workspace, module
예제 #12
0
 def make_workspace(self, object_dict={}, image_dict={}):
     '''Make a workspace for testing MeasureImageIntensity'''
     module = M.MeasureImageIntensity()
     pipeline = cpp.Pipeline()
     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,
                               cpmeas.Measurements(),
                               image_set_list)
     for key in image_dict.keys():
         image_set.add(key, cpi.Image(image_dict[key]))
     for key in object_dict.keys():
         o = cpo.Objects()
         o.segmented = object_dict[key]
         object_set.add_objects(o, key)
     return workspace, module
예제 #13
0
    def apply_crop(self, workspace, input_image_name, output_image_name,
                   crop_slice):
        image = workspace.image_set.get_image(input_image_name)
        image_pixels = image.pixel_data
        if image_pixels.ndim > 2:
            crop_slice = self.add_slice_dimension(crop_slice)
        output_image = cpi.Image(image_pixels[crop_slice], parent_image=image)
        workspace.image_set.add(output_image_name, output_image)

        if self.show_window:
            if not hasattr(workspace.display_data, 'input_images'):
                workspace.display_data.input_images = [image.pixel_data]
                workspace.display_data.output_images = [output_image.pixel_data]
                workspace.display_data.input_image_names = [input_image_name]
                workspace.display_data.output_image_names = [output_image_name]
            else:
                workspace.display_data.input_images += [image.pixel_data]
                workspace.display_data.output_images += [output_image.pixel_data]
                workspace.display_data.input_image_names += [input_image_name]
                workspace.display_data.output_image_names += [output_image_name]
예제 #14
0
    def make_workspace(self, labels, overlap_choice, masking_objects=None,
                       masking_image=None, renumber=True):
        module = M.MaskObjects()
        module.module_num = 1
        module.object_name.value = INPUT_OBJECTS
        module.remaining_objects.value = OUTPUT_OBJECTS
        module.mask_choice.value = (M.MC_OBJECTS if masking_objects is not None
                                    else M.MC_IMAGE)
        module.masking_image.value = MASKING_IMAGE
        module.masking_objects.value = MASKING_OBJECTS
        module.retain_or_renumber.value = (M.R_RENUMBER if renumber
                                           else M.R_RETAIN)
        module.overlap_choice.value = overlap_choice

        pipeline = cpp.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)

        object_set = cpo.ObjectSet()
        io = cpo.Objects()
        io.segmented = labels
        object_set.add_objects(io, INPUT_OBJECTS)

        if masking_objects is not None:
            oo = cpo.Objects()
            oo.segmented = masking_objects
            object_set.add_objects(oo, MASKING_OBJECTS)

        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        if masking_image is not None:
            mi = cpi.Image(masking_image)
            image_set.add(MASKING_IMAGE, mi)

        workspace = cpw.Workspace(pipeline, module, image_set, object_set,
                                  cpmeas.Measurements(), image_set_list)
        return workspace, module
예제 #15
0
    def test_07_01_make_ijv_outlines(self):
        np.random.seed(70)
        x = cpo.Objects()
        ii, jj = np.mgrid[0:10, 0:20]
        masks = [(ii - ic)**2 + (jj - jc)**2 < r**2
                 for ic, jc, r in ((4, 5, 5), (4, 12, 5), (6, 8, 5))]
        i = np.hstack([ii[mask] for mask in masks])
        j = np.hstack([jj[mask] for mask in masks])
        v = np.hstack([[k + 1] * np.sum(mask) for k, mask in enumerate(masks)])

        x.set_ijv(np.column_stack((i, j, v)), ii.shape)
        x.parent_image = cpi.Image(np.zeros((10, 20)))
        colors = np.random.uniform(size=(3, 3)).astype(np.float32)
        image = x.make_ijv_outlines(colors)
        i1 = [
            i for i, color in enumerate(colors)
            if np.all(color == image[0, 5, :])
        ]
        self.assertEqual(len(i1), 1)
        i2 = [
            i for i, color in enumerate(colors)
            if np.all(color == image[0, 12, :])
        ]
        self.assertEqual(len(i2), 1)
        i3 = [
            i for i, color in enumerate(colors)
            if np.all(color == image[-1, 8, :])
        ]
        self.assertEqual(len(i3), 1)
        self.assertNotEqual(i1[0], i2[0])
        self.assertNotEqual(i2[0], i3[0])
        colors = colors[np.array([i1[0], i2[0], i3[0]])]
        outlines = np.zeros((10, 20, 3), np.float32)
        alpha = np.zeros((10, 20))
        for i, (color, mask) in enumerate(zip(colors, masks)):
            my_outline = outline(mask)
            outlines[my_outline] += color
            alpha[my_outline] += 1
        alpha[alpha == 0] = 1
        outlines /= alpha[:, :, np.newaxis]
        np.testing.assert_almost_equal(outlines, image)
예제 #16
0
 def test_01_00_test_zero_objects(self):
     x = YS.IdentifyYeastCells()
     x.object_name.value = OBJECTS_NAME
     x.segmentation_precision.value = 11
     x.input_image_name.value = IMAGE_NAME
     x.background_brighter_then_cell_inside.value = False
     x.average_cell_diameter.value = 5
     img = np.zeros((25, 25))
     image = cpi.Image(img, file_name="test_01_00_test_zero_objects")
     image_set_list = cpi.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     image_set.providers.append(cpi.VanillaImageProvider(IMAGE_NAME, image))
     object_set = cpo.ObjectSet()
     measurements = cpmeas.Measurements()
     pipeline = cellprofiler.pipeline.Pipeline()
     x.run(Workspace(pipeline, x, image_set, object_set, measurements,
                     None))
     self.assertEqual(len(object_set.object_names), 1)
     self.assertTrue(OBJECTS_NAME in object_set.object_names)
     objects = object_set.get_objects(OBJECTS_NAME)
     segmented = objects.segmented
     self.assertTrue(np.all(segmented == 0))
     self.assertTrue("Image" in measurements.get_object_names())
     self.assertTrue(OBJECTS_NAME in measurements.get_object_names())
     self.assertTrue(
         "Count_" + OBJECTS_NAME in measurements.get_feature_names("Image"))
     count = measurements.get_current_measurement("Image",
                                                  "Count_" + OBJECTS_NAME)
     self.assertEqual(count, 0)
     self.assertTrue("Location_Center_X" in measurements.get_feature_names(
         OBJECTS_NAME))
     location_center_x = measurements.get_current_measurement(
         OBJECTS_NAME, "Location_Center_X")
     self.assertTrue(isinstance(location_center_x, np.ndarray))
     self.assertEqual(np.product(location_center_x.shape), 0)
     self.assertTrue("Location_Center_Y" in measurements.get_feature_names(
         OBJECTS_NAME))
     location_center_y = measurements.get_current_measurement(
         OBJECTS_NAME, "Location_Center_Y")
     self.assertTrue(isinstance(location_center_y, np.ndarray))
     self.assertEqual(np.product(location_center_y.shape), 0)
예제 #17
0
    def make_obj_workspace(self, ground_truth_obj, id_obj, ground_truth, id):
        '''make a workspace to test comparing objects'''
        ''' ground truth object and ID object  are dictionaires w/ the following keys'''
        '''i - i component of pixel coordinates
        j - j component of pixel coordinates
        l - label '''

        module = C.CalculateImageOverlap()
        module.module_num = 1
        module.obj_or_img.value = O_OBJ
        module.object_name_GT.value = GROUND_TRUTH_OBJ
        module.object_name_ID.value = ID_OBJ
        module.wants_emd.value = True
        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()
        image_set = image_set_list.get_image_set(0)

        for name, d in ((GROUND_TRUTH_OBJ_IMAGE_NAME, ground_truth),
                        (ID_OBJ_IMAGE_NAME, id)):
            image = cpi.Image(d["image"],
                              mask=d.get("mask"),
                              crop_mask=d.get("crop_mask"))
            image_set.add(name, image)
        object_set = cpo.ObjectSet()
        for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ,
                                                               id_obj)):
            object = cpo.Objects()
            if d.shape[1] == 3:
                object.ijv = d
            else:
                object.segmented = d
            object_set.add_objects(object, name)
        workspace = cpw.Workspace(pipeline, module, image_set, object_set,
                                  cpmeas.Measurements(), image_set_list)
        return workspace, module
예제 #18
0
    def make_pipeline(self, image, mask, subsample_size, image_sample_size,
                      element_size, granular_spectrum_length,
                      labels=None):
        '''Make a pipeline with a MeasureGranularity module

        image - measure granularity on this image
        mask - exclude / include pixels from measurement. None = no mask
        subsample_size, etc. - values for corresponding settings in the module
        returns tuple of module & workspace
        '''
        module = M.MeasureGranularity()
        module.module_num = 1
        image_setting = module.images[0]
        # assert isinstance(image_setting, M.MeasureGranularity)
        image_setting.image_name.value = IMAGE_NAME
        image_setting.subsample_size.value = subsample_size
        image_setting.image_sample_size.value = image_sample_size
        image_setting.element_size.value = element_size
        image_setting.granular_spectrum_length.value = granular_spectrum_length
        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        img = cpi.Image(image, mask)
        image_set.add(IMAGE_NAME, img)
        pipeline = cpp.Pipeline()

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

        pipeline.add_listener(error_callback)
        pipeline.add_module(module)
        object_set = cpo.ObjectSet()
        if labels is not None:
            objects = cpo.Objects()
            objects.segmented = labels
            object_set.add_objects(objects, OBJECTS_NAME)
            image_setting.add_objects()
            image_setting.objects[0].objects_name.value = OBJECTS_NAME
        workspace = cpw.Workspace(pipeline, module, image_set,
                                  object_set, cpmeas.Measurements(),
                                  image_set_list)
        return module, workspace
예제 #19
0
 def make_workspace(self, image, labels, convert=True, mask=None):
     '''Make a workspace for testing MeasureTexture'''
     module = M.MeasureTexture()
     module.image_groups[0].image_name.value = INPUT_IMAGE_NAME
     module.object_groups[0].object_name.value = INPUT_OBJECTS_NAME
     pipeline = cpp.Pipeline()
     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,
                               cpmeas.Measurements(),
                               image_set_list)
     image_set.add(INPUT_IMAGE_NAME, cpi.Image(image, convert=convert,
                                               mask=mask))
     objects = cpo.Objects()
     objects.segmented = labels
     object_set.add_objects(objects, INPUT_OBJECTS_NAME)
     return workspace, module
예제 #20
0
 def make_workspace(self,
                    image,
                    size_method,
                    interpolation,
                    mask=None,
                    cropping=None):
     module = R.Resize()
     module.image_name.value = INPUT_IMAGE_NAME
     module.resized_image_name.value = OUTPUT_IMAGE_NAME
     module.size_method.value = size_method
     module.interpolation.value = interpolation
     module.module_num = 1
     pipeline = cpp.Pipeline()
     pipeline.add_module(module)
     image_set_list = cpi.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     image = cpi.Image(image, mask, cropping)
     image_set.add(INPUT_IMAGE_NAME, image)
     workspace = cpw.Workspace(pipeline, module, image_set, cpo.ObjectSet(),
                               cpmeas.Measurements(), image_set_list)
     return workspace, module
 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()
     if labels.shape[1] == 3:
         o.ijv = labels
     else:
         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
예제 #22
0
    def test_01_02_object_with_cropping(self):
        labels = np.zeros((10, 10), int)
        labels[0:7, 3:8] = 1
        mask = np.zeros((10, 10), bool)
        mask[1:9, 1:9] = True
        image = cpi.Image(np.zeros((10, 10)), mask=mask)
        area_occupied = np.sum(labels[mask])
        perimeter = np.sum(outline(np.logical_and(labels, mask)))
        total_area = np.sum(mask)
        workspace = self.make_workspace(labels, image)
        module = workspace.module
        module.operands[0].operand_choice.value = "Objects"
        module.run(workspace)
        m = workspace.measurements

        def mn(x):
            return "AreaOccupied_%s_%s" % (x, module.operands[0].operand_objects.value)

        self.assertEqual(m.get_current_measurement("Image", mn("AreaOccupied")), area_occupied)
        self.assertEqual(m.get_current_measurement("Image", mn("Perimeter")), perimeter)
        self.assertEqual(m.get_current_measurement("Image", mn("TotalArea")), total_area)
예제 #23
0
 def execute(self,
             image,
             function,
             mask=None,
             custom_repeats=None,
             scale=None,
             module=None):
     """Run the morph module on an input and return the resulting image"""
     INPUT_IMAGE_NAME = "input"
     OUTPUT_IMAGE_NAME = "output"
     if module is None:
         module = morph.Morph()
     module.functions[0].function.value = function
     module.image_name.value = INPUT_IMAGE_NAME
     module.output_image_name.value = OUTPUT_IMAGE_NAME
     if custom_repeats is None:
         module.functions[0].repeats_choice.value = morph.R_ONCE
     elif custom_repeats == -1:
         module.functions[0].repeats_choice.value = morph.R_FOREVER
     else:
         module.functions[0].repeats_choice.value = morph.R_CUSTOM
         module.functions[0].custom_repeats.value = custom_repeats
     if scale is not None:
         module.functions[0].scale.value = scale
     pipeline = cpp.Pipeline()
     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,
         cpmeas.Measurements(),
         image_set_list,
     )
     image_set.add(INPUT_IMAGE_NAME, cpi.Image(image, mask=mask))
     module.run(workspace)
     output = image_set.get_image(OUTPUT_IMAGE_NAME)
     return output.pixel_data
 def run_classify(self, workspace):
     pixels = self.get_5d_image(workspace)
     #
     # Process the image in chunks
     #
     with self.get_classifier("r") as c:
         assert isinstance(c, PixelClassifier)
         class_names = c.get_class_names()
         prob_idxs = np.array([
             class_names.index(group.class_name.value)
             for group in self.outputs
         ])
         chunk_size = 128
         prob_maps = np.zeros(
             (len(prob_idxs), pixels.shape[3], pixels.shape[4]))
         for i in range(0, pixels.shape[3], chunk_size):
             iend = min(i + chunk_size, pixels.shape[3])
             for j in range(0, pixels.shape[4], chunk_size):
                 jend = min(j + chunk_size, pixels.shape[4])
                 ii, jj = [_.flatten() for _ in np.mgrid[i:iend, j:jend]]
                 coords = np.column_stack(
                     (np.zeros(len(ii), int), np.zeros(len(ii), int),
                      np.zeros(len(ii), int), ii, jj))
                 samples = c.get_samples(pixels, coords)
                 probs = c.run_final_pipeline(samples)
                 prob_maps[:, i:iend, j:jend] = \
                     probs[:, prob_idxs].reshape(
                         iend - i, jend - j, len(prob_maps)).transpose(2, 0, 1)
     for i, group in enumerate(self.outputs):
         image_name = group.output_image.value
         image = cpi.Image(prob_maps[i])
         workspace.image_set.add(image_name, image)
     if self.show_window:
         workspace.display_data.input_images = [
             pixels[i].reshape(*pixels.shape[-2:])
             for i in range(pixels.shape[0])
         ]
         workspace.display_data.output_images = [
             prob_maps[i] for i in range(len(prob_maps))
         ]
예제 #25
0
    def test_07_02_cache(self):
        import h5py
        import os
        import tempfile

        r = np.random.RandomState()
        r.seed(72)
        test_cases = ({
            "image": r.uniform(size=(10, 20))
        }, {
            "image": r.uniform(size=(20, 10, 3))
        }, {
            "image": r.uniform(size=(10, 20)),
            "mask": r.uniform(size=(10, 20)) > .5
        }, {
            "image": r.uniform(size=(10, 20)),
            "crop_mask": np.all(np.mgrid[0:10, 0:20] > 3, 0)
        })
        h, path = tempfile.mkstemp(suffix=".h5")
        hdf_file = h5py.File(path, "w")
        os.close(h)

        for test_case in test_cases:
            image = cpi.Image(**test_case)
            image.cache("foo", hdf_file)
            expected = test_case["image"].astype(np.float32)
            np.testing.assert_array_equal(image.pixel_data, expected)
            if "mask" in test_case:
                np.testing.assert_array_equal(image.mask, test_case["mask"])
            else:
                np.testing.assert_equal(image.mask, True)
                np.testing.assert_equal(tuple(image.mask.shape),
                                        tuple(expected.shape[:2]))
            if "crop_mask" in test_case:
                np.testing.assert_array_equal(image.crop_mask,
                                              test_case["crop_mask"])
            else:
                np.testing.assert_equal(tuple(image.crop_mask.shape),
                                        tuple(expected.shape[:2]))
예제 #26
0
    def make_workspace(self, pixels, choices):
        """Make a workspace for running UnmixColors

        pixels - input image
        choices - a list of choice strings for the images desired
        """
        pipeline = cpp.Pipeline()

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

        pipeline.add_listener(callback)

        module = U.UnmixColors()
        module.input_image_name.value = INPUT_IMAGE
        module.outputs[0].image_name.value = output_image_name(0)
        module.outputs[0].stain_choice.value = choices[0]
        for i, choice in enumerate(choices[1:]):
            module.add_image()
            module.outputs[i + 1].image_name.value = output_image_name(i + 1)
            module.outputs[i + 1].stain_choice.value = choice

        module.module_num = 1
        pipeline.add_module(module)

        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        image = cpi.Image(pixels)
        image_set.add(INPUT_IMAGE, image)

        workspace = cpw.Workspace(
            pipeline,
            module,
            image_set,
            cpo.ObjectSet(),
            cpmeas.Measurements(),
            image_set_list,
        )
        return workspace, module
    def run_image(self, image, workspace):
        '''Perform illumination according to the parameters of one image setting group

        '''
        #
        # Get the image names from the settings
        #
        image_name = image.image_name.value
        spill_correct_name = image.spill_correct_function_image_name.value
        corrected_image_name = image.corrected_image_name.value
        #
        # Get images from the image set
        #
        orig_image = workspace.image_set.get_image(image_name)
        spillover_mat = workspace.image_set.get_image(spill_correct_name)
        #
        # Either divide or subtract the illumination image from the original
        #
        method = image.spill_correct_method.value
        output_pixels = self.compensate_image_ls(orig_image.pixel_data,
                                                 spillover_mat.pixel_data,
                                                 method)
        # Save the output image in the image set and have it inherit
        # mask & cropping from the original image.
        #
        output_image = cpi.Image(output_pixels, parent_image=orig_image)
        workspace.image_set.add(corrected_image_name, output_image)
        #
        # Save images for display
        #
        if self.show_window:
            if not hasattr(workspace.display_data, 'images'):
                workspace.display_data.images = {}
                workspace.display_data.images[
                    image_name] = orig_image.pixel_data
                workspace.display_data.images[
                    corrected_image_name] = output_pixels
                workspace.display_data.images[
                    spill_correct_name] = spillover_mat.pixel_data
예제 #28
0
    def test_01_03_combine_channels(self):
        np.random.seed(13)
        image = np.random.uniform(size=(20, 10, 5))
        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        image_set.add(IMAGE_NAME, cpi.Image(image))

        module = cpm_ctg.ColorToGray()
        module.module_num = 1
        module.image_name.value = IMAGE_NAME
        module.combine_or_split.value = cpm_ctg.COMBINE
        module.grayscale_name.value = OUTPUT_IMAGE_F % 1
        module.rgb_or_channels.value = cpm_ctg.CH_CHANNELS
        module.add_channel()
        module.add_channel()

        channel_indexes = np.array([2, 0, 3])
        factors = np.random.uniform(size=3)
        divisor = np.sum(factors)
        expected = np.zeros((20, 10))
        for i, channel_index in enumerate(channel_indexes):
            module.channels[i].channel_choice.value = module.channel_names[channel_index]
            module.channels[i].contribution.value_text = "%.10f" % factors[i]
            expected += image[:, :, channel_index] * factors[i] / divisor

        pipeline = cpp.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)
        workspace = Workspace(pipeline, module, image_set, cpo.ObjectSet(),
                              cpm.Measurements(), image_set_list)
        module.run(workspace)
        pixels = image_set.get_image(module.grayscale_name.value).pixel_data
        self.assertEqual(pixels.ndim, 2)
        self.assertEqual(tuple(pixels.shape), (20, 10))
        np.testing.assert_almost_equal(expected, pixels)
예제 #29
0
    def run(self, workspace):
        parent_image = None
        parent_image_name = None
        imgset = workspace.image_set
        stack_pixel_data = None
        input_image_names = []
        channel_names = []
        input_image_names = [sc.image_name.value for sc in self.stack_channels]
        channel_names = input_image_names
        source_channels = [
            imgset.get_image(name, must_be_grayscale=False).pixel_data
            for name in input_image_names
        ]
        parent_image = imgset.get_image(input_image_names[0])
        for idx, pd in enumerate(source_channels):
            if pd.shape[:2] != source_channels[0].shape[:2]:
                raise ValueError(
                    "The %s image and %s image have different sizes (%s vs %s)"
                    % (self.stack_channels[0].image_name.value,
                       self.stack_channels[idx].image_name.value,
                       source_channels[0].shape[:2], pd.pixel_data.shape[:2]))
        stack_pixel_data = np.dstack(source_channels)

        ##############
        # Save image #
        ##############
        stack_image = cpi.Image(stack_pixel_data, parent_image=parent_image)
        stack_image.channel_names = channel_names
        imgset.add(self.stack_image_name.value, stack_image)

        ##################
        # Display images #
        ##################
        if self.show_window:
            workspace.display_data.input_image_names = input_image_names
            workspace.display_data.stack_pixel_data = stack_pixel_data
            workspace.display_data.images = \
                [imgset.get_image(name, must_be_grayscale=False).pixel_data
                 for name in input_image_names]
예제 #30
0
    def make_place_workspace(self, images):
        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        module = T.Tile()
        module.module_num = 1
        module.tile_method.value = T.T_WITHIN_CYCLES
        module.output_image.value = OUTPUT_IMAGE_NAME
        module.wants_automatic_rows.value = False
        module.wants_automatic_columns.value = True
        module.rows.value = 1
        for i, image in enumerate(images):
            image_name = input_image_name(i)
            if i == 0:
                module.input_image.value = image_name
            else:
                if len(module.additional_images) <= i:
                    module.add_image()
                module.additional_images[i -
                                         1].input_image_name.value = image_name
            image_set.add(image_name, cpi.Image(image))

        pipeline = cpp.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)

        workspace = cpw.Workspace(
            pipeline,
            module,
            image_set,
            cpo.ObjectSet(),
            cpmeas.Measurements(),
            image_set_list,
        )
        return workspace, module