Exemple #1
0
def test_dont_load_file():
    csv_text = """"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
""" % (
        test_filename,
        test_path,
    )
    pipeline, module, filename = make_pipeline(csv_text)
    c0_ran = [False]

    def callback(workspace):
        imgset = workspace.image_set
        assert len(imgset.names) == 0
        c0_ran[0] = True

    c0 = C0()
    c0.callback = callback
    c0.set_module_num(1)
    pipeline.add_module(c0)
    try:
        module.wants_images.value = False
        pipeline.run()
        assert c0_ran[0]
    finally:
        os.remove(filename)
Exemple #2
0
def make_workspace(image, labels):
    module = cellprofiler.modules.definegrid.DefineGrid()
    module.set_module_num(1)
    module.grid_image.value = GRID_NAME
    module.manual_image.value = INPUT_IMAGE_NAME
    module.display_image_name.value = INPUT_IMAGE_NAME
    module.object_name.value = OBJECTS_NAME
    module.save_image_name.value = OUTPUT_IMAGE_NAME
    image_set_list = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    image_set.add(INPUT_IMAGE_NAME, cellprofiler.image.Image(image))
    object_set = cellprofiler.object.ObjectSet()
    objects = cellprofiler.object.Objects()
    objects.segmented = labels
    object_set.add_objects(objects, OBJECTS_NAME)
    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    measurements = cellprofiler.measurement.Measurements()
    workspace = cellprofiler.workspace.Workspace(
        pipeline, module, image_set, object_set, measurements, image_set_list
    )
    return workspace, module
 def make_workspace(self,
                    image,
                    size_method,
                    interpolation,
                    mask=None,
                    cropping=None,
                    dimensions=2):
     module = cellprofiler.modules.resize.Resize()
     module.x_name.value = INPUT_IMAGE_NAME
     module.y_name.value = OUTPUT_IMAGE_NAME
     module.size_method.value = size_method
     module.interpolation.value = interpolation
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     image_set_list = cellprofiler.image.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     image = cellprofiler.image.Image(image,
                                      mask,
                                      cropping,
                                      dimensions=dimensions)
     image_set.add(INPUT_IMAGE_NAME, image)
     workspace = cellprofiler.workspace.Workspace(
         pipeline, module, image_set, cellprofiler.object.ObjectSet(),
         cellprofiler.measurement.Measurements(), image_set_list)
     return workspace, module
    def test_10_01_scaling(self):
        '''Test loading an image scaled and unscaled'''
        folder = "loaddata"
        file_name = "1-162hrh2ax2.tif"
        path = tests.modules.make_12_bit_image(folder, file_name, (22, 18))
        csv_text = ("Image_PathName_MyFile,Image_FileName_MyFile\n"
                    "%s,%s\n" % os.path.split(path))
        c0_image = []
        for rescale in (False, True):
            pipeline, module, filename = self.make_pipeline(csv_text)
            try:
                module.rescale.value = rescale

                def callback(workspace):
                    imgset = workspace.image_set
                    image = imgset.get_image("MyFile")
                    pixels = image.pixel_data
                    c0_image.append(pixels.copy())

                c0 = C0()
                c0.callback = callback
                c0.module_num = 2
                pipeline.add_module(c0)
                pipeline.run()
            finally:
                os.remove(filename)
        unscaled, scaled = c0_image
        numpy.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
    def test_10_01_scaling(self):
        """Test loading an image scaled and unscaled"""
        folder = "loaddata"
        file_name = "1-162hrh2ax2.tif"
        path = tests.modules.make_12_bit_image(folder, file_name, (22, 18))
        csv_text = (
            "Image_PathName_MyFile,Image_FileName_MyFile\n"
            "%s,%s\n" % os.path.split(path)
        )
        c0_image = []
        for rescale in (False, True):
            pipeline, module, filename = self.make_pipeline(csv_text)
            try:
                module.rescale.value = rescale

                def callback(workspace):
                    imgset = workspace.image_set
                    image = imgset.get_image("MyFile")
                    pixels = image.pixel_data
                    c0_image.append(pixels.copy())

                c0 = C0()
                c0.callback = callback
                c0.module_num = 2
                pipeline.add_module(c0)
                pipeline.run()
            finally:
                os.remove(filename)
        unscaled, scaled = c0_image
        numpy.testing.assert_almost_equal(unscaled * 65535.0 / 4095.0, scaled)
Exemple #6
0
def make_workspace(gridding, labels=None):
    module = cellprofiler.modules.identifyobjectsingrid.IdentifyObjectsInGrid()
    module.set_module_num(1)
    module.grid_name.value = GRID_NAME
    module.output_objects_name.value = OUTPUT_OBJECTS_NAME
    module.guiding_object_name.value = GUIDING_OBJECTS_NAME
    image_set_list = cellprofiler.image.ImageSetList()
    object_set = cellprofiler.object.ObjectSet()
    if labels is not None:
        my_objects = cellprofiler.object.Objects()
        my_objects.segmented = labels
        object_set.add_objects(my_objects, GUIDING_OBJECTS_NAME)
    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set_list.get_image_set(0),
        object_set,
        cellprofiler.measurement.Measurements(),
        image_set_list,
    )
    workspace.set_grid(GRID_NAME, gridding)
    return workspace, module
Exemple #7
0
 def test_11_02_invert_binary_invert(self):
     #
     # Regression for issue #1329
     #
     r = numpy.random.RandomState()
     r.seed(1102)
     m = cellprofiler.measurement.Measurements()
     pixel_data = r.uniform(size=(20, 20)) > .5
     m.add("inputimage", cellprofiler.image.Image(pixel_data))
     module = cellprofiler.modules.imagemath.ImageMath()
     module.images[0].image_name.value = "inputimage"
     module.output_image_name.value = "intermediateimage"
     module.operation.value = cellprofiler.modules.imagemath.O_INVERT
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     module = cellprofiler.modules.imagemath.ImageMath()
     module.images[0].image_name.value = "intermediateimage"
     module.output_image_name.value = "outputimage"
     module.operation.value = cellprofiler.modules.imagemath.O_INVERT
     module.module_num = 2
     pipeline = cellprofiler.pipeline.Pipeline()
     workspace = cellprofiler.workspace.Workspace(pipeline, module, m, None,
                                                  m, None)
     for module in pipeline.modules():
         module.run(workspace)
     numpy.testing.assert_array_equal(
         pixel_data,
         m.get_image("inputimage").pixel_data > .5)
    def make_workspace(file_names):
        module = cellprofiler.modules.loadsingleimage.LoadSingleImage()
        module.set_module_num(1)
        module.directory.set_dir_choice(
            cellprofiler.preferences.DEFAULT_INPUT_FOLDER_NAME)
        for i, file_name in enumerate(file_names):
            if i > 0:
                module.add_file()
            module.file_settings[i].image_name.value = get_image_name(i)
            module.file_settings[i].file_name.value = file_name
        pipeline = cellprofiler.pipeline.Pipeline()

        def callback(caller, event):
            assert not isinstance(event,
                                  cellprofiler.pipeline.RunExceptionEvent)

        pipeline.add_listener(callback)
        pipeline.add_module(module)
        image_set_list = cellprofiler.image.ImageSetList()
        workspace = cellprofiler.workspace.Workspace(
            pipeline,
            module,
            image_set_list.get_image_set(0),
            cellprofiler.object.ObjectSet(),
            cellprofiler.measurement.Measurements(),
            image_set_list,
        )
        return workspace, module
def test_01_01_combine():
    img = get_my_image()
    inj = cellprofiler.modules.injectimage.InjectImage("my_image", img)
    inj.module_num = 1
    ctg = cellprofiler.modules.colortogray.ColorToGray()
    ctg.module_num = 2
    ctg.image_name.value = "my_image"
    ctg.combine_or_split.value = cellprofiler.modules.colortogray.COMBINE
    ctg.red_contribution.value = 1
    ctg.green_contribution.value = 2
    ctg.blue_contribution.value = 3
    ctg.grayscale_name.value = "my_grayscale"
    pipeline = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(inj)
    pipeline.add_module(ctg)
    pipeline.test_valid()

    measurements = cellprofiler.measurement.Measurements()
    object_set = cellprofiler.object.ObjectSet()
    image_set_list = cellprofiler.image.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")
    assert grayscale
    img = grayscale.image
    numpy.testing.assert_almost_equal(img[0, 0], 1.0 / 6.0)
    numpy.testing.assert_almost_equal(img[0, 25], 1.0 / 3.0)
    numpy.testing.assert_almost_equal(img[25, 0], 1.0 / 2.0)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
def make_workspace(pixel_data, mask=None):
    image = cellprofiler.image.Image(pixel_data, mask)
    image_set_list = cellprofiler.image.ImageSetList()

    image_set = image_set_list.get_image_set(0)
    image_set.add(IMAGE_NAME, image)

    module = cellprofiler.modules.identifydeadworms.IdentifyDeadWorms()
    module.set_module_num(1)
    module.image_name.value = IMAGE_NAME
    module.object_name.value = OBJECTS_NAME

    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

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

    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set,
        cellprofiler.object.ObjectSet(),
        cellprofiler.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
 def test_03_01_well_row_column(self):
     # Make sure that Metadata_Well is generated if we have
     # Metadata_Row and Metadata_Column
     #
     for row_tag, column_tag in (("row", "column"), ("wellrow",
                                                     "wellcolumn"),
                                 ("well_row", "well_column")):
         module = cellprofiler.modules.metadata.Metadata()
         module.wants_metadata.value = True
         em = module.extraction_methods[0]
         em.filter_choice.value = cellprofiler.modules.metadata.F_ALL_IMAGES
         em.extraction_method.value = cellprofiler.modules.metadata.X_MANUAL_EXTRACTION
         em.source.value = cellprofiler.modules.metadata.XM_FILE_NAME
         em.file_regexp.value = (
             "^Channel(?P<Wavelength>[1-2])-"
             "(?P<%(row_tag)s>[A-H])-"
             "(?P<%(column_tag)s>[0-9]{2}).tif$") % locals()
         em.filter_choice.value = cellprofiler.modules.metadata.F_ALL_IMAGES
         url = "file:/imaging/analysis/Channel1-C-05.tif"
         self.check(module, url, [{
             "Wavelength": "1",
             row_tag: "C",
             column_tag: "05",
             cellprofiler.measurement.FTR_WELL: "C05"
         }])
         pipeline = cellprofiler.pipeline.Pipeline()
         imgs = cellprofiler.modules.images.Images()
         imgs.filter_choice.value = cellprofiler.modules.images.FILTER_CHOICE_NONE
         imgs.module_num = 1
         pipeline.add_module(imgs)
         module.module_num = 2
         pipeline.add_module(module)
         self.assertIn(
             cellprofiler.measurement.M_WELL,
             [c[1] for c in module.get_measurement_columns(pipeline)])
    def test_04_02_dont_load_file(self):
        csv_text = """"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
""" % (
            self.test_filename,
            self.test_path,
        )
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            self.assertEqual(len(imgset.names), 0)
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 1
        pipeline.add_module(c0)
        try:
            module.wants_images.value = False
            pipeline.run()
            self.assertTrue(c0_ran[0])
        finally:
            os.remove(filename)
    def test_04_01_load_objects(self):
        r = numpy.random.RandomState()
        r.seed(41)
        labels = numpy.random.randint(0, 10, size=(30, 40))
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cellprofiler.preferences.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(numpy.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = cellprofiler.modules.loadsingleimage.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(
                cellprofiler.modules.loadsingleimage.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = cellprofiler.modules.loadsingleimage.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            pipeline = cellprofiler.pipeline.Pipeline()

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

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cellprofiler.measurement.Measurements()
            object_set = cellprofiler.object.ObjectSet()
            image_set_list = cellprofiler.image.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cellprofiler.workspace.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)
            numpy.testing.assert_equal(labels, o.segmented)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (cellprofiler.measurement.C_COUNT, OBJECTS_NAME))), 9)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (cellprofiler.modules.loadsingleimage.C_OBJECTS_FILE_NAME,
                     OBJECTS_NAME))), filename)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (cellprofiler.modules.loadsingleimage.C_OBJECTS_PATH_NAME,
                     OBJECTS_NAME))), directory)
            for feature in (cellprofiler.measurement.M_LOCATION_CENTER_X,
                            cellprofiler.measurement.M_LOCATION_CENTER_Y,
                            cellprofiler.measurement.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_04_01_extent(self):
        module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
        module.object_groups[0].name.value = "SomeObjects"
        module.calculate_zernikes.value = True
        object_set = cellprofiler.object.ObjectSet()
        labels = numpy.zeros((10, 20), int)
        # 3/4 of a square is covered
        labels[5:7, 5:10] = 1
        labels[7:9, 5:15] = 1
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        object_set.add_objects(objects, "SomeObjects")
        module.module_num = 1
        image_set_list = cellprofiler.image.ImageSetList()
        measurements = cellprofiler.measurement.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)

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

        pipeline.add_listener(callback)
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set_list.get_image_set(0),
                                                     object_set, measurements, image_set_list)
        module.run(workspace)
        values = measurements.get_current_measurement(
                "SomeObjects", "_".join((cellprofiler.modules.measureobjectsizeshape.AREA_SHAPE, cellprofiler.modules.measureobjectsizeshape.F_EXTENT)))
        self.assertEqual(len(values), 1)
        self.assertAlmostEqual(values[0], .75)
 def test_03_01_well_row_column(self):
     # Make sure that Metadata_Well is generated if we have
     # Metadata_Row and Metadata_Column
     #
     for row_tag, column_tag in (
         ("row", "column"),
         ("wellrow", "wellcolumn"),
         ("well_row", "well_column")):
         module = cellprofiler.modules.metadata.Metadata()
         module.wants_metadata.value=True
         em = module.extraction_methods[0]
         em.filter_choice.value = cellprofiler.modules.metadata.F_ALL_IMAGES
         em.extraction_method.value = cellprofiler.modules.metadata.X_MANUAL_EXTRACTION
         em.source.value = cellprofiler.modules.metadata.XM_FILE_NAME
         em.file_regexp.value = (
             "^Channel(?P<Wavelength>[1-2])-"
             "(?P<%(row_tag)s>[A-H])-"
             "(?P<%(column_tag)s>[0-9]{2}).tif$") % locals()
         em.filter_choice.value = cellprofiler.modules.metadata.F_ALL_IMAGES
         url = "file:/imaging/analysis/Channel1-C-05.tif"
         self.check(module, url,
                    [{ "Wavelength":"1",
                       row_tag:"C",
                       column_tag:"05",
                       cellprofiler.measurement.FTR_WELL: "C05"}])
         pipeline = cellprofiler.pipeline.Pipeline()
         imgs = cellprofiler.modules.images.Images()
         imgs.filter_choice.value = cellprofiler.modules.images.FILTER_CHOICE_NONE
         imgs.module_num = 1
         pipeline.add_module(imgs)
         module.module_num = 2
         pipeline.add_module(module)
         self.assertIn(
             cellprofiler.measurement.M_WELL,
             [c[1] for c in module.get_measurement_columns(pipeline)])
    def test_01_00_zeros(self):
        """Run on an empty labels matrix"""
        object_set = cellprofiler.object.ObjectSet()
        labels = numpy.zeros((10, 20), int)
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        object_set.add_objects(objects, "SomeObjects")
        module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
        settings = ["SomeObjects", "Yes"]
        module.set_settings_from_values(settings, 1, module.module_class())
        module.module_num = 1
        image_set_list = cellprofiler.image.ImageSetList()
        measurements = cellprofiler.measurement.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set_list.get_image_set(0),
                                                     object_set, measurements, image_set_list)
        module.run(workspace)

        for f in (cellprofiler.modules.measureobjectsizeshape.F_AREA, cellprofiler.modules.measureobjectsizeshape.F_CENTER_X, cellprofiler.modules.measureobjectsizeshape.F_CENTER_Y,
                  cellprofiler.modules.measureobjectsizeshape.F_ECCENTRICITY, cellprofiler.modules.measureobjectsizeshape.F_EULER_NUMBER,
                  cellprofiler.modules.measureobjectsizeshape.F_EXTENT, cellprofiler.modules.measureobjectsizeshape.F_FORM_FACTOR,
                  cellprofiler.modules.measureobjectsizeshape.F_MAJOR_AXIS_LENGTH, cellprofiler.modules.measureobjectsizeshape.F_MINOR_AXIS_LENGTH,
                  cellprofiler.modules.measureobjectsizeshape.F_ORIENTATION, cellprofiler.modules.measureobjectsizeshape.F_PERIMETER,
                  cellprofiler.modules.measureobjectsizeshape.F_SOLIDITY, cellprofiler.modules.measureobjectsizeshape.F_COMPACTNESS,
                  cellprofiler.modules.measureobjectsizeshape.F_MAXIMUM_RADIUS, cellprofiler.modules.measureobjectsizeshape.F_MEAN_RADIUS,
                  cellprofiler.modules.measureobjectsizeshape.F_MEDIAN_RADIUS,
                  cellprofiler.modules.measureobjectsizeshape.F_MIN_FERET_DIAMETER, cellprofiler.modules.measureobjectsizeshape.F_MAX_FERET_DIAMETER):
            m = cellprofiler.modules.measureobjectsizeshape.AREA_SHAPE + "_" + f
            a = measurements.get_current_measurement('SomeObjects', m)
            self.assertEqual(len(a), 0)
 def make_workspace(self,
                    labels,
                    operation,
                    iterations=1,
                    wants_outlines=False,
                    wants_fill_holes=False):
     object_set = cellprofiler.object.ObjectSet()
     objects = cellprofiler.object.Objects()
     objects.segmented = labels
     object_set.add_objects(objects, INPUT_NAME)
     module = cellprofiler.modules.expandorshrinkobjects.ExpandOrShrink()
     module.object_name.value = INPUT_NAME
     module.output_object_name.value = OUTPUT_NAME
     module.outlines_name.value = OUTLINES_NAME
     module.operation.value = operation
     module.iterations.value = iterations
     module.wants_outlines.value = wants_outlines
     module.wants_fill_holes.value = wants_fill_holes
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     image_set_list = cellprofiler.image.ImageSetList()
     workspace = cellprofiler.workspace.Workspace(pipeline,
                                                  module,
                                                  image_set_list.get_image_set(0),
                                                  object_set,
                                                  cellprofiler.measurement.Measurements(),
                                                  image_set_list)
     return workspace, module
Exemple #18
0
    def make_workspace(self, image, mask=None, labels=None, binary_image=None):
        '''Make a workspace and IdentifyPrimaryObjects module
        
        image - the intensity image for thresholding
        
        mask - if present, the "don't analyze" mask of the intensity image
        
        labels - if thresholding per-object, the labels matrix needed
        
        binary_image - if thresholding using a binary image, the image
        '''
        module = YS.IdentifyYeastCells()
        module.module_num = 1
        module.input_image_name.value = IMAGE_NAME
        module.object_name.value = OBJECTS_NAME
        module.binary_image.value = BINARY_IMAGE_NAME
        module.masking_objects.value = MASKING_OBJECTS_NAME

        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)
        m = cpmeas.Measurements()
        cpimage = cpi.Image(image, mask=mask)
        m.add(IMAGE_NAME, cpimage)
        if binary_image is not None:
            m.add(BINARY_IMAGE_NAME, cpi.Image(binary_image))
        object_set = cpo.ObjectSet()
        if labels is not None:
            o = cpo.Objects()
            o.segmented = labels
            object_set.add_objects(o, MASKING_OBJECTS_NAME)
        workspace = cellprofiler.workspace.Workspace(pipeline, module, m,
                                                     object_set, m, None)
        return workspace, module
Exemple #19
0
    def make_workspace(self, parents, children, fake_measurement=False):
        """Make a workspace for testing Relate"""
        pipeline = cellprofiler.pipeline.Pipeline()
        if fake_measurement:

            class FakeModule(cellprofiler.module.Module):
                def get_measurement_columns(self, pipeline):
                    return [
                        (
                            CHILD_OBJECTS,
                            MEASUREMENT,
                            cellprofiler.measurement.COLTYPE_FLOAT,
                        ),
                        (
                            CHILD_OBJECTS,
                            IGNORED_MEASUREMENT,
                            cellprofiler.measurement.COLTYPE_INTEGER,
                        ),
                    ]

            module = FakeModule()
            module.module_num = 1
            pipeline.add_module(module)
        module = cellprofiler.modules.relateobjects.Relate()
        module.x_name.value = PARENT_OBJECTS
        module.y_name.value = CHILD_OBJECTS
        module.find_parent_child_distances.value = (
            cellprofiler.modules.relateobjects.D_NONE
        )
        module.wants_child_objects_saved.value = False
        module.module_num = 2 if fake_measurement else 1
        pipeline.add_module(module)
Exemple #20
0
def make_tile_workspace(images):
    module = cellprofiler.modules.tile.Tile()
    module.set_module_num(1)
    module.tile_method.value = cellprofiler.modules.tile.T_ACROSS_CYCLES
    module.input_image.value = INPUT_IMAGE_NAME
    module.output_image.value = OUTPUT_IMAGE_NAME

    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    image_set_list = cellprofiler.image.ImageSetList()
    for i, image in enumerate(images):
        image_set = image_set_list.get_image_set(i)
        image_set.add(INPUT_IMAGE_NAME, cellprofiler.image.Image(image))

    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set_list.get_image_set(0),
        cellprofiler.object.ObjectSet(),
        cellprofiler.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
 def check(self, module, url, dd, keys=None, xml=None):
     """Check that running the metadata module on a url generates the expected dictionary"""
     pipeline = cellprofiler.pipeline.Pipeline()
     imgs = cellprofiler.modules.images.Images()
     imgs.filter_choice.value = cellprofiler.modules.images.FILTER_CHOICE_NONE
     imgs.module_num = 1
     pipeline.add_module(imgs)
     module.module_num = 2
     pipeline.add_module(module)
     pipeline.add_urls([url])
     m = cellprofiler.measurement.Measurements()
     workspace = cellprofiler.workspace.Workspace(pipeline, module, None,
                                                  None, m, None)
     file_list = workspace.file_list
     file_list.add_files_to_filelist([url])
     if xml is not None:
         file_list.add_metadata(url, xml)
     ipds = pipeline.get_image_plane_details(workspace)
     self.assertEqual(len(ipds), len(dd))
     for d, ipd in zip(dd, ipds):
         self.assertDictContainsSubset(d, ipd.metadata)
     all_keys = list(pipeline.get_available_metadata_keys().keys())
     if keys is not None:
         for key in keys:
             self.assertIn(key, all_keys)
    def test_04_01_load_file(self):
        csv_text = '''"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
''' % (self.test_filename, self.test_path)
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DNA")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement('MD5Digest_DNA')
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue('PathName_DNA' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('PathName_DNA'),
                             self.test_path)
            self.assertTrue('FileName_DNA' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('FileName_DNA'),
                             self.test_filename)
        finally:
            os.remove(filename)
Exemple #23
0
def test_non_contiguous():
    """make sure MeasureObjectAreaShape doesn't crash if fed non-contiguous objects"""
    module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
    module.object_groups[0].name.value = "SomeObjects"
    module.calculate_zernikes.value = True
    object_set = cellprofiler.object.ObjectSet()
    labels = numpy.zeros((10, 20), int)
    labels[1:9, 1:5] = 1
    labels[4:6, 6:19] = 1
    objects = cellprofiler.object.Objects()
    objects.segmented = labels
    object_set.add_objects(objects, "SomeObjects")
    module.set_module_num(1)
    image_set_list = cellprofiler.image.ImageSetList()
    measurements = cellprofiler.measurement.Measurements()
    pipeline = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(module)

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set_list.get_image_set(0),
        object_set,
        measurements,
        image_set_list,
    )
    module.run(workspace)
    values = measurements.get_current_measurement("SomeObjects", "AreaShape_Perimeter")
    assert len(values) == 1
    assert values[0] == 54
    def test_03_01_non_contiguous(self):
        '''make sure MeasureObjectAreaShape doesn't crash if fed non-contiguous objects'''
        module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
        module.object_groups[0].name.value = "SomeObjects"
        module.calculate_zernikes.value = True
        object_set = cellprofiler.object.ObjectSet()
        labels = numpy.zeros((10, 20), int)
        labels[1:9, 1:5] = 1
        labels[4:6, 6:19] = 1
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        object_set.add_objects(objects, "SomeObjects")
        module.module_num = 1
        image_set_list = cellprofiler.image.ImageSetList()
        measurements = cellprofiler.measurement.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)

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

        pipeline.add_listener(callback)
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set_list.get_image_set(0),
                                                     object_set, measurements, image_set_list)
        module.run(workspace)
        values = measurements.get_current_measurement("SomeObjects",
                                                      "AreaShape_Perimeter")
        self.assertEqual(len(values), 1)
        self.assertEqual(values[0], 54)
def make_workspace(labels, mode, distance=0, neighbors_labels=None):
    """Make a workspace for testing MeasureObjectNeighbors"""
    module = cellprofiler.modules.measureobjectneighbors.MeasureObjectNeighbors(
    )
    module.set_module_num(1)
    module.object_name.value = OBJECTS_NAME
    module.distance_method.value = mode
    module.distance.value = distance
    pipeline = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(module)
    object_set = cellprofiler.object.ObjectSet()
    image_set_list = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    measurements = cellprofiler.measurement.Measurements()
    measurements.group_index = 1
    measurements.group_number = 1
    workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set,
                                                 object_set, measurements,
                                                 image_set_list)
    objects = cellprofiler.object.Objects()
    objects.segmented = labels
    object_set.add_objects(objects, OBJECTS_NAME)
    if neighbors_labels is None:
        module.neighbors_name.value = OBJECTS_NAME
    else:
        module.neighbors_name.value = NEIGHBORS_NAME
        objects = cellprofiler.object.Objects()
        objects.segmented = neighbors_labels
        object_set.add_objects(objects, NEIGHBORS_NAME)
    return workspace, module
Exemple #26
0
    def test_04_01_extent(self):
        module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape(
        )
        module.object_groups[0].name.value = "SomeObjects"
        module.calculate_zernikes.value = True
        object_set = cellprofiler.object.ObjectSet()
        labels = numpy.zeros((10, 20), int)
        # 3/4 of a square is covered
        labels[5:7, 5:10] = 1
        labels[7:9, 5:15] = 1
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        object_set.add_objects(objects, "SomeObjects")
        module.module_num = 1
        image_set_list = cellprofiler.image.ImageSetList()
        measurements = cellprofiler.measurement.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)

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

        pipeline.add_listener(callback)
        workspace = cellprofiler.workspace.Workspace(
            pipeline, module, image_set_list.get_image_set(0), object_set,
            measurements, image_set_list)
        module.run(workspace)
        values = measurements.get_current_measurement(
            "SomeObjects", "_".join(
                (cellprofiler.modules.measureobjectsizeshape.AREA_SHAPE,
                 cellprofiler.modules.measureobjectsizeshape.F_EXTENT)))
        self.assertEqual(len(values), 1)
        self.assertAlmostEqual(values[0], .75)
Exemple #27
0
 def make_workspace(self,
                    pixel_data,
                    mask=None,
                    objects=None,
                    dimensions=2):
     image_set_list = cellprofiler.image.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     object_set = cellprofiler.object.ObjectSet()
     image = cellprofiler.image.Image(pixel_data, dimensions=dimensions)
     if not mask is None:
         image.mask = mask
     image_set.add(IMAGES_NAME, image)
     if not objects is None:
         o = cellprofiler.object.Objects()
         o.segmented = objects
         object_set.add_objects(o, OBJECTS_NAME)
     module = M.MeasureImageQuality()
     module.images_choice.value = M.O_SELECT
     module.image_groups[0].include_image_scalings.value = False
     module.image_groups[0].image_names.value = IMAGES_NAME
     module.image_groups[0].use_all_threshold_methods.value = False
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     workspace = cellprofiler.workspace.Workspace(
         pipeline, module, image_set, object_set,
         cellprofiler.measurement.Measurements(), image_set_list)
     return workspace
 def make_workspace(self,
                    labels,
                    operation,
                    iterations=1,
                    wants_outlines=False,
                    wants_fill_holes=False):
     object_set = cellprofiler.object.ObjectSet()
     objects = cellprofiler.object.Objects()
     objects.segmented = labels
     object_set.add_objects(objects, INPUT_NAME)
     module = cellprofiler.modules.expandorshrinkobjects.ExpandOrShrink()
     module.object_name.value = INPUT_NAME
     module.output_object_name.value = OUTPUT_NAME
     module.outlines_name.value = OUTLINES_NAME
     module.operation.value = operation
     module.iterations.value = iterations
     module.wants_outlines.value = wants_outlines
     module.wants_fill_holes.value = wants_fill_holes
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     image_set_list = cellprofiler.image.ImageSetList()
     workspace = cellprofiler.workspace.Workspace(
         pipeline, module, image_set_list.get_image_set(0), object_set,
         cellprofiler.measurement.Measurements(), image_set_list)
     return workspace, module
def test_split_all():
    img = get_my_image()
    inj = cellprofiler.modules.injectimage.InjectImage("my_image", img)
    inj.set_module_num(1)
    ctg = cellprofiler.modules.colortogray.ColorToGray()
    ctg.set_module_num(2)
    ctg.image_name.value = "my_image"
    ctg.combine_or_split.value = cellprofiler.modules.colortogray.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 = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(inj)
    pipeline.add_module(ctg)
    pipeline.test_valid()

    measurements = cellprofiler.measurement.Measurements()
    object_set = cellprofiler.object.ObjectSet()
    image_set_list = cellprofiler.image.ImageSetList()
    workspace = cellprofiler.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(
        cellprofiler.workspace.Workspace(
            pipeline, inj, image_set, object_set, measurements, None
        )
    )
    ctg.run(
        cellprofiler.workspace.Workspace(
            pipeline, ctg, image_set, object_set, measurements, None
        )
    )
    red = image_set.get_image("my_red")
    assert red
    img = red.image
    numpy.testing.assert_almost_equal(img[0, 0], 1)
    numpy.testing.assert_almost_equal(img[0, 25], 0)
    numpy.testing.assert_almost_equal(img[25, 0], 0)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
    green = image_set.get_image("my_green")
    assert green
    img = green.image
    numpy.testing.assert_almost_equal(img[0, 0], 0)
    numpy.testing.assert_almost_equal(img[0, 25], 1)
    numpy.testing.assert_almost_equal(img[25, 0], 0)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
    blue = image_set.get_image("my_blue")
    assert blue
    img = blue.image
    numpy.testing.assert_almost_equal(img[0, 0], 0)
    numpy.testing.assert_almost_equal(img[0, 25], 0)
    numpy.testing.assert_almost_equal(img[25, 0], 1)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
    def test_05_01_overlapping(self):
        '''Test object measurement with two overlapping objects in ijv format'''

        i, j = numpy.mgrid[0:10, 0:20]
        m = (i > 1) & (i < 9) & (j > 1) & (j < 19)
        m1 = m & (i < j)
        m2 = m & (i < 9 - j)
        mlist = []
        olist = []
        for m in (m1, m2):
            objects = cellprofiler.object.Objects()
            objects.segmented = m.astype(int)
            olist.append(objects)
        ijv = numpy.column_stack((
            numpy.hstack([numpy.argwhere(m)[:, 0] for m in (m1, m2)]),
            numpy.hstack([numpy.argwhere(m)[:, 1] for m in (m1, m2)]),
            numpy.array([1] * numpy.sum(m1) + [2] * numpy.sum(m2))))
        objects = cellprofiler.object.Objects()
        objects.ijv = ijv
        olist.append(objects)
        for objects in olist:
            module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
            module.object_groups[0].name.value = "SomeObjects"
            module.calculate_zernikes.value = True
            object_set = cellprofiler.object.ObjectSet()
            object_set.add_objects(objects, "SomeObjects")
            module.module_num = 1
            image_set_list = cellprofiler.image.ImageSetList()
            measurements = cellprofiler.measurement.Measurements()
            mlist.append(measurements)
            pipeline = cellprofiler.pipeline.Pipeline()
            pipeline.add_module(module)

            def callback(caller, event):
                self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent))
                pipeline.add_listener(callback)

            workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                         image_set_list.get_image_set(0),
                                                         object_set, measurements, image_set_list)
            module.run(workspace)

        pipeline = cellprofiler.pipeline.Pipeline()
        for c in module.get_measurement_columns(pipeline):
            oname, feature = c[:2]
            if oname != "SomeObjects":
                continue
            measurements = mlist[0]
            self.assertTrue(isinstance(measurements, cellprofiler.measurement.Measurements))
            v1 = measurements.get_current_measurement(oname, feature)
            self.assertEqual(len(v1), 1)
            v1 = v1[0]
            measurements = mlist[1]
            v2 = measurements.get_current_measurement(oname, feature)
            self.assertEqual(len(v2), 1)
            v2 = v2[0]
            expected = (v1, v2)
            v = mlist[2].get_current_measurement(oname, feature)
            self.assertEqual(tuple(v), expected)
def run_image_set(projection_type, images_and_masks, frequency=9, run_last=True):
    image_set_list = cellprofiler.image.ImageSetList()
    image_count = len(images_and_masks)
    for i in range(image_count):
        pixel_data, mask = images_and_masks[i]
        if mask is None:
            image = cellprofiler.image.Image(pixel_data)
        else:
            image = cellprofiler.image.Image(pixel_data, mask)
        image_set_list.get_image_set(i).add(IMAGE_NAME, image)
    #
    # Add bogus image at end for 2nd group
    #
    bogus_image = cellprofiler.image.Image(numpy.zeros((10, 20)))
    image_set_list.get_image_set(image_count).add(IMAGE_NAME, bogus_image)

    pipeline = cellprofiler.pipeline.Pipeline()
    module = cellprofiler.modules.makeprojection.MakeProjection()
    module.set_module_num(1)
    module.image_name.value = IMAGE_NAME
    module.projection_image_name.value = PROJECTED_IMAGE_NAME
    module.projection_type.value = projection_type
    module.frequency.value = frequency
    pipeline.add_module(module)
    m = cellprofiler.measurement.Measurements()
    workspace = cellprofiler.workspace.Workspace(
        pipeline, module, None, None, m, image_set_list
    )
    module.prepare_run(workspace)
    module.prepare_group(workspace, {}, list(range(1, len(images_and_masks) + 1)))
    for i in range(image_count):
        if i > 0:
            image_set_list.purge_image_set(i - 1)
        w = cellprofiler.workspace.Workspace(
            pipeline,
            module,
            image_set_list.get_image_set(i),
            cellprofiler.object.ObjectSet(),
            m,
            image_set_list,
        )
        if i < image_count - 1 or run_last:
            module.run(w)
    module.post_group(w, {})
    image = w.image_set.get_image(PROJECTED_IMAGE_NAME)
    #
    # Make sure that the image provider is reset after prepare_group
    #
    module.prepare_group(workspace, {}, [image_count + 1])
    image_set = image_set_list.get_image_set(image_count)
    w = cellprofiler.workspace.Workspace(
        pipeline, module, image_set, cellprofiler.object.ObjectSet(), m, image_set_list
    )
    module.run(w)
    image_provider = image_set.get_image_provider(PROJECTED_IMAGE_NAME)
    assert numpy.max(image_provider.count) == 1

    return image
    def make_workspace(self, parents, children, fake_measurement=False):
        """Make a workspace for testing Relate"""
        pipeline = cellprofiler.pipeline.Pipeline()
        if fake_measurement:

            class FakeModule(cellprofiler.module.Module):
                def get_measurement_columns(self, pipeline):
                    return [
                        (
                            CHILD_OBJECTS,
                            MEASUREMENT,
                            cellprofiler.measurement.COLTYPE_FLOAT,
                        ),
                        (
                            CHILD_OBJECTS,
                            IGNORED_MEASUREMENT,
                            cellprofiler.measurement.COLTYPE_INTEGER,
                        ),
                    ]

            module = FakeModule()
            module.set_module_num(1)
            pipeline.add_module(module)
        module = cellprofiler.modules.relateobjects.Relate()
        module.x_name.value = PARENT_OBJECTS
        module.y_name.value = CHILD_OBJECTS
        module.find_parent_child_distances.value = (
            cellprofiler.modules.relateobjects.D_NONE)
        module.wants_child_objects_saved.value = False
        new_module_num = 2 if fake_measurement else 1
        module.set_module_num(new_module_num)
        pipeline.add_module(module)
        object_set = cellprofiler.object.ObjectSet()
        image_set_list = cellprofiler.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        m = cellprofiler.measurement.Measurements()
        m.add_image_measurement(cellprofiler.measurement.GROUP_NUMBER, 1)
        m.add_image_measurement(cellprofiler.measurement.GROUP_INDEX, 1)
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set, object_set, m,
                                                     image_set_list)
        o = cellprofiler.object.Objects()
        if parents.shape[1] == 3:
            # IJV format
            o.ijv = parents
        else:
            o.segmented = parents
        object_set.add_objects(o, PARENT_OBJECTS)
        o = cellprofiler.object.Objects()
        if children.shape[1] == 3:
            o.ijv = children
        else:
            o.segmented = children
        object_set.add_objects(o, CHILD_OBJECTS)
        return workspace, module
 def test_prepare_run():
     # regression test for issue #673 and #1161
     #
     # If LoadSingleImage appears first, pathname data does not show
     # up in the measurements.
     #
     tests.modules.maybe_download_sbs()
     folder = "ExampleSBSImages"
     path = os.path.join(tests.modules.example_images_directory(), folder)
     filename = "Channel1-01-A-01.tif"
     pipeline = cellprofiler.pipeline.Pipeline()
     lsi = cellprofiler.modules.loadsingleimage.LoadSingleImage()
     lsi.set_module_num(1)
     lsi.directory.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME
     lsi.directory.custom_path = path
     lsi.file_settings[0].image_name.value = get_image_name(0)
     lsi.file_settings[0].file_name.value = filename
     pipeline.add_module(lsi)
     li = cellprofiler.modules.loadimages.LoadImages()
     li.set_module_num(2)
     pipeline.add_module(li)
     li.match_method.value = cellprofiler.modules.loadimages.MS_EXACT_MATCH
     li.location.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME
     li.location.custom_path = path
     li.images[0].common_text.value = "Channel2-"
     m = cellprofiler.measurement.Measurements()
     workspace = cellprofiler.workspace.Workspace(
         pipeline,
         lsi,
         m,
         cellprofiler.object.ObjectSet(),
         m,
         cellprofiler.image.ImageSetList(),
     )
     assert pipeline.prepare_run(workspace)
     assert 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(
         (cellprofiler.measurement.C_FILE_NAME, get_image_name(0)))
     assert m[cellprofiler.measurement.IMAGE, m_file, 2] == filename
     #
     # Can we retrieve the image?
     #
     pixel_data = m.get_image(get_image_name(0)).pixel_data
     assert not numpy.isscalar(pixel_data)
def make_pipeline(
    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 = cellprofiler.modules.measuregranularity.MeasureGranularity()
    module.set_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 = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    img = cellprofiler.image.Image(image, mask)
    image_set.add(IMAGE_NAME, img)
    pipeline = cellprofiler.pipeline.Pipeline()

    def error_callback(event, caller):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(error_callback)
    pipeline.add_module(module)
    object_set = cellprofiler.object.ObjectSet()
    if labels is not None:
        objects = cellprofiler.object.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 = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set,
        object_set,
        cellprofiler.measurement.Measurements(),
        image_set_list,
    )
    return module, workspace
Exemple #35
0
def make_workspace(labels,
                   overlap_choice,
                   masking_objects=None,
                   masking_image=None,
                   renumber=True):
    module = cellprofiler.modules.maskobjects.MaskObjects()
    module.set_module_num(1)
    module.object_name.value = INPUT_OBJECTS
    module.remaining_objects.value = OUTPUT_OBJECTS
    module.mask_choice.value = (cellprofiler.modules.maskobjects.MC_OBJECTS
                                if masking_objects is not None else
                                cellprofiler.modules.maskobjects.MC_IMAGE)
    module.masking_image.value = MASKING_IMAGE
    module.masking_objects.value = MASKING_OBJECTS
    module.retain_or_renumber.value = (
        cellprofiler.modules.maskobjects.R_RENUMBER
        if renumber else cellprofiler.modules.maskobjects.R_RETAIN)
    module.overlap_choice.value = overlap_choice

    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

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

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

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

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

    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set,
        object_set,
        cellprofiler.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
    def test_04_01_load_objects(self):
        r = numpy.random.RandomState()
        r.seed(41)
        labels = numpy.random.randint(0, 10, size=(30, 40))
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cellprofiler.preferences.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(numpy.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = cellprofiler.modules.loadsingleimage.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(cellprofiler.modules.loadsingleimage.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = cellprofiler.modules.loadsingleimage.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            pipeline = cellprofiler.pipeline.Pipeline()

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

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cellprofiler.measurement.Measurements()
            object_set = cellprofiler.object.ObjectSet()
            image_set_list = cellprofiler.image.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cellprofiler.workspace.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)
            numpy.testing.assert_equal(labels, o.segmented)
            self.assertEqual(m.get_current_image_measurement(
                    "_".join((cellprofiler.measurement.C_COUNT, OBJECTS_NAME))), 9)
            self.assertEqual(m.get_current_image_measurement(
                    "_".join((cellprofiler.modules.loadsingleimage.C_OBJECTS_FILE_NAME, OBJECTS_NAME))), filename)
            self.assertEqual(m.get_current_image_measurement(
                    "_".join((cellprofiler.modules.loadsingleimage.C_OBJECTS_PATH_NAME, OBJECTS_NAME))), directory)
            for feature in (cellprofiler.measurement.M_LOCATION_CENTER_X, cellprofiler.measurement.M_LOCATION_CENTER_Y,
                            cellprofiler.measurement.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_09_01_load_bcb_file(self):

        csv_text = """ELN_RUN_ID,CBIP_RUN_ID,ASSAY_PLATE_BARCODE,\
MX_PLATE_ID,ASSAY_WELL_POSITION,ASSAY_WELL_ROLE,SITE_X,SITE_Y,\
MICROSCOPE,SOURCE_DESCRIPTION,DATE_CREATED,FILE_PATH,FILE_NAME,\
CPD_PLATE_MAP_NAME,CPD_WELL_POSITION,BROAD_ID,\
CPD_MMOL_CONC,SOURCE_NAME,SOURCE_COMPOUND_NAME,CPD_SMILES
"4012-10-W01-01-02","4254","BR00021547","20777","N01","COMPOUND",\
"2","2","GS IX Micro","DAPI","2010/03/19 06:01:12","%s",\
"%s","C-4012-00-D80-001_Rev3","N01",\
"BRD-K71194192-001-01-6","2.132352941","ChemBridge","",\
"Oc1ccnc(SCC(=O)Nc2ccc(Oc3ccccc3)cc2)n1"
""" % (
            self.test_path,
            self.test_filename,
        )
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DAPI")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement("MD5Digest_DAPI")
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue(
                "PathName_DAPI" in m.get_feature_names(cellprofiler.measurement.IMAGE)
            )
            self.assertEqual(
                m.get_current_image_measurement("PathName_DAPI"), self.test_path
            )
            self.assertTrue(
                "FileName_DAPI" in m.get_feature_names(cellprofiler.measurement.IMAGE)
            )
            self.assertEqual(
                m.get_current_image_measurement("FileName_DAPI"), self.test_filename
            )
        finally:
            os.remove(filename)
    def test_06_get_settings_list(self):
        module1 = instantiate_module("IdentifyPrimaryObjects")
        module1.module_num = 1
        module2 = instantiate_module("Smooth")
        module2.module_num = 2
        module3 = instantiate_module("Resize")
        module3.module_num = 3
        module4 = self.make_instance()
        module4.module_num = 4
        module4.parameters[0].module_names.value = "IdentifyPrimaryObjects #1"
        module4.add_parameter()
        module4.parameters[1].module_names.value = "Smooth #2"
        module4.add_parameter()
        module4.parameters[2].module_names.value = "Resize #3"
        module4.add_parameter()
        module4.parameters[3].module_names.value = "BayesianOptimisation #4"

        pipeline = cellprofiler.pipeline.Pipeline()

        pipeline.add_module(module1)
        pipeline.add_module(module2)
        pipeline.add_module(module3)
        pipeline.add_module(module4)

        settings_list = module4.get_settings_from_modules(pipeline)

        self.assertTrue("Use advanced settings?" in settings_list)
        self.assertTrue("Select smoothing method" in settings_list)
        self.assertTrue("Resizing factor" in settings_list)
        self.assertTrue("No. of settings to be adjusted" in settings_list)
def test_01_02_split_all():
    img = get_my_image()
    inj = cellprofiler.modules.injectimage.InjectImage("my_image", img)
    inj.module_num = 1
    ctg = cellprofiler.modules.colortogray.ColorToGray()
    ctg.module_num = 2
    ctg.image_name.value = "my_image"
    ctg.combine_or_split.value = cellprofiler.modules.colortogray.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 = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(inj)
    pipeline.add_module(ctg)
    pipeline.test_valid()

    measurements = cellprofiler.measurement.Measurements()
    object_set = cellprofiler.object.ObjectSet()
    image_set_list = cellprofiler.image.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")
    assert red
    img = red.image
    numpy.testing.assert_almost_equal(img[0, 0], 1)
    numpy.testing.assert_almost_equal(img[0, 25], 0)
    numpy.testing.assert_almost_equal(img[25, 0], 0)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
    green = image_set.get_image("my_green")
    assert green
    img = green.image
    numpy.testing.assert_almost_equal(img[0, 0], 0)
    numpy.testing.assert_almost_equal(img[0, 25], 1)
    numpy.testing.assert_almost_equal(img[25, 0], 0)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
    blue = image_set.get_image("my_blue")
    assert blue
    img = blue.image
    numpy.testing.assert_almost_equal(img[0, 0], 0)
    numpy.testing.assert_almost_equal(img[0, 25], 0)
    numpy.testing.assert_almost_equal(img[25, 0], 1)
    numpy.testing.assert_almost_equal(img[25, 25], 0)
Exemple #40
0
 def check(self, module, url, expected):
     '''Check filtering of one URL using the module as configured'''
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_urls([url])
     module.module_num = 1
     pipeline.add_module(module)
     m = cellprofiler.measurement.Measurements()
     workspace = cellprofiler.workspace.Workspace(pipeline, module, None, None, m, None)
     file_list = pipeline.get_filtered_file_list(workspace)
     if expected:
         self.assertEqual(len(file_list), 1)
         self.assertEqual(file_list[0], url)
     else:
         self.assertEqual(len(file_list), 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 = cellprofiler.modules.measureobjectoverlap.MeasureObjectOverlap(
        )
        module.module_num = 1
        module.object_name_GT.value = GROUND_TRUTH_OBJ
        module.object_name_ID.value = ID_OBJ
        module.wants_emd.value = True
        pipeline = cellprofiler.pipeline.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)
        image_set_list = cellprofiler.image.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 = cellprofiler.image.Image(d["image"],
                                             mask=d.get("mask"),
                                             crop_mask=d.get("crop_mask"))
            image_set.add(name, image)
        object_set = cellprofiler.object.ObjectSet()
        for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ,
                                                               id_obj)):
            object = cellprofiler.object.Objects()
            if d.shape[1] == 3:
                object.ijv = d
            else:
                object.segmented = d
            object_set.add_objects(object, name)
        workspace = cellprofiler.workspace.Workspace(
            pipeline,
            module,
            image_set,
            object_set,
            cellprofiler.measurement.Measurements(),
            image_set_list,
        )
        return workspace, module
 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.
     #
     tests.modules.maybe_download_sbs()
     folder = "ExampleSBSImages"
     path = os.path.join(tests.modules.example_images_directory(), folder)
     filename = "Channel1-01-A-01.tif"
     pipeline = cellprofiler.pipeline.Pipeline()
     lsi = cellprofiler.modules.loadsingleimage.LoadSingleImage()
     lsi.module_num = 1
     lsi.directory.dir_choice = cellprofiler.setting.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 = cellprofiler.modules.loadimages.LoadImages()
     li.module_num = 2
     pipeline.add_module(li)
     li.match_method.value = cellprofiler.modules.loadimages.MS_EXACT_MATCH
     li.location.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME
     li.location.custom_path = path
     li.images[0].common_text.value = "Channel2-"
     m = cellprofiler.measurement.Measurements()
     workspace = cellprofiler.workspace.Workspace(pipeline, lsi, m, cellprofiler.object.ObjectSet(), m,
                                                  cellprofiler.image.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((cellprofiler.measurement.C_FILE_NAME, self.get_image_name(0)))
     self.assertEqual(m[cellprofiler.measurement.IMAGE, m_file, 2], filename)
     #
     # Can we retrieve the image?
     #
     pixel_data = m.get_image(self.get_image_name(0)).pixel_data
     self.assertFalse(numpy.isscalar(pixel_data))
    def test_04_02_object_outlines(self):
        labels = numpy.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()
        cellprofiler.preferences.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(numpy.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = cellprofiler.modules.loadsingleimage.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(cellprofiler.modules.loadsingleimage.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = cellprofiler.modules.loadsingleimage.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            fs.wants_outlines.value = True
            fs.outlines_name.value = OUTLINES_NAME
            pipeline = cellprofiler.pipeline.Pipeline()

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

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cellprofiler.measurement.Measurements()
            object_set = cellprofiler.object.ObjectSet()
            image_set_list = cellprofiler.image.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cellprofiler.workspace.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)
            numpy.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, parents, children, fake_measurement=False):
        '''Make a workspace for testing Relate'''
        pipeline = cellprofiler.pipeline.Pipeline()
        if fake_measurement:
            class FakeModule(cellprofiler.module.Module):
                def get_measurement_columns(self, pipeline):
                    return [(CHILD_OBJECTS, MEASUREMENT, cellprofiler.measurement.COLTYPE_FLOAT),
                            (CHILD_OBJECTS, IGNORED_MEASUREMENT, cellprofiler.measurement.COLTYPE_INTEGER)]

            module = FakeModule()
            module.module_num = 1
            pipeline.add_module(module)
        module = cellprofiler.modules.relateobjects.Relate()
        module.x_name.value = PARENT_OBJECTS
        module.x_child_name.value = CHILD_OBJECTS
        module.find_parent_child_distances.value = cellprofiler.modules.relateobjects.D_NONE
        module.module_num = 2 if fake_measurement else 1
        pipeline.add_module(module)
        object_set = cellprofiler.object.ObjectSet()
        image_set_list = cellprofiler.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        m = cellprofiler.measurement.Measurements()
        m.add_image_measurement(cellprofiler.measurement.GROUP_NUMBER, 1)
        m.add_image_measurement(cellprofiler.measurement.GROUP_INDEX, 1)
        workspace = cellprofiler.workspace.Workspace(pipeline,
                                                     module,
                                                     image_set,
                                                     object_set,
                                                     m,
                                                     image_set_list)
        o = cellprofiler.object.Objects()
        if parents.shape[1] == 3:
            # IJV format
            o.ijv = parents
        else:
            o.segmented = parents
        object_set.add_objects(o, PARENT_OBJECTS)
        o = cellprofiler.object.Objects()
        if children.shape[1] == 3:
            o.ijv = children
        else:
            o.segmented = children
        object_set.add_objects(o, CHILD_OBJECTS)
        return workspace, module
Exemple #45
0
 def make_workspace(self, image, size_method, interpolation,
                    mask=None, cropping=None, dimensions=2):
     module = cellprofiler.modules.resize.Resize()
     module.x_name.value = INPUT_IMAGE_NAME
     module.y_name.value = OUTPUT_IMAGE_NAME
     module.size_method.value = size_method
     module.interpolation.value = interpolation
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     image_set_list = cellprofiler.image.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     image = cellprofiler.image.Image(image, mask, cropping, dimensions=dimensions)
     image_set.add(INPUT_IMAGE_NAME, image)
     workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set,
                                                  cellprofiler.object.ObjectSet(),
                                                  cellprofiler.measurement.Measurements(),
                                                  image_set_list)
     return workspace, module
    def make_workspace(self, labels, parent_image=None):
        object_set = cellprofiler.object.ObjectSet()
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        objects.parent_image = parent_image
        object_set.add_objects(objects, OBJECTS_NAME)

        pipeline = cellprofiler.pipeline.Pipeline()
        module = cellprofiler.modules.measureimageareaoccupied.MeasureImageAreaOccupied()
        module.module_num = 1
        module.operands[0].operand_objects.value = OBJECTS_NAME
        pipeline.add_module(module)
        image_set_list = cellprofiler.image.ImageSetList()
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set_list.get_image_set(0),
                                                     object_set,
                                                     cellprofiler.measurement.Measurements(),
                                                     image_set_list)
        return workspace
    def test_09_01_load_bcb_file(self):

        csv_text = '''ELN_RUN_ID,CBIP_RUN_ID,ASSAY_PLATE_BARCODE,\
MX_PLATE_ID,ASSAY_WELL_POSITION,ASSAY_WELL_ROLE,SITE_X,SITE_Y,\
MICROSCOPE,SOURCE_DESCRIPTION,DATE_CREATED,FILE_PATH,FILE_NAME,\
CPD_PLATE_MAP_NAME,CPD_WELL_POSITION,BROAD_ID,\
CPD_MMOL_CONC,SOURCE_NAME,SOURCE_COMPOUND_NAME,CPD_SMILES
"4012-10-W01-01-02","4254","BR00021547","20777","N01","COMPOUND",\
"2","2","GS IX Micro","DAPI","2010/03/19 06:01:12","%s",\
"%s","C-4012-00-D80-001_Rev3","N01",\
"BRD-K71194192-001-01-6","2.132352941","ChemBridge","",\
"Oc1ccnc(SCC(=O)Nc2ccc(Oc3ccccc3)cc2)n1"
''' % (self.test_path, self.test_filename)
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DAPI")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement('MD5Digest_DAPI')
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue('PathName_DAPI' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('PathName_DAPI'),
                             self.test_path)
            self.assertTrue('FileName_DAPI' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('FileName_DAPI'),
                             self.test_filename)
        finally:
            os.remove(filename)
    def test_06_01_images_to_process(self):
        #
        # Test MeasureImageQuality.images_to_process on a pipeline with a
        # variety of image providers.
        #
        expected_names = ["foo", "bar"]
        pipeline = cellprofiler.pipeline.Pipeline()
        module1 = cellprofiler.modules.namesandtypes.NamesAndTypes()
        module1.module_num = 1
        module1.assignment_method.value = \
            cellprofiler.modules.namesandtypes.ASSIGN_RULES
        module1.add_assignment()
        module1.add_assignment()
        module1.assignments[0].image_name.value = expected_names[0]
        module1.assignments[0].load_as_choice.value = \
            cellprofiler.modules.namesandtypes.LOAD_AS_GRAYSCALE_IMAGE
        #
        # TO_DO: issue #652
        #    This test should fail at some later date when we can detect
        #    that an illumination function should not be QA measured
        #
        module1.assignments[1].image_name.value = expected_names[1]
        module1.assignments[1].load_as_choice.value = \
            cellprofiler.modules.namesandtypes.LOAD_AS_ILLUMINATION_FUNCTION
        module1.assignments[2].load_as_choice.value = \
            cellprofiler.modules.namesandtypes.LOAD_AS_OBJECTS
        pipeline.add_module(module1)

        module2 = cellprofiler.modules.smooth.Smooth()
        module2.module_num = 2
        module2.image_name.value = expected_names[0]
        module2.filtered_image_name.value = "henry"
        pipeline.add_module(module2)

        miq_module = M.MeasureImageQuality()
        miq_module.module_num = 3
        miq_module.images_choice.value = M.O_ALL_LOADED
        image_names = miq_module.images_to_process(
                miq_module.image_groups[0], None, pipeline)
        self.assertEqual(len(image_names), len(expected_names))
        for image_name in image_names:
            self.assertTrue(image_name in expected_names)
    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 = cellprofiler.modules.measureobjectoverlap.MeasureObjectOverlap()
        module.module_num = 1
        module.object_name_GT.value = GROUND_TRUTH_OBJ
        module.object_name_ID.value = ID_OBJ
        module.wants_emd.value = True
        pipeline = cellprofiler.pipeline.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)
        image_set_list = cellprofiler.image.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 = cellprofiler.image.Image(d["image"],
                                             mask=d.get("mask"),
                                             crop_mask=d.get("crop_mask"))
            image_set.add(name, image)
        object_set = cellprofiler.object.ObjectSet()
        for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj),
                        (ID_OBJ, id_obj)):
            object = cellprofiler.object.Objects()
            if d.shape[1] == 3:
                object.ijv = d
            else:
                object.segmented = d
            object_set.add_objects(object, name)
        workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set,
                                                     object_set, cellprofiler.measurement.Measurements(),
                                                     image_set_list)
        return workspace, module
    def make_workspace(self, ground_truth, test, dimensions=2):
        '''Make a workspace with a ground-truth image and a test image

        ground_truth and test are dictionaries with the following keys:
        image     - the pixel data
        mask      - (optional) the mask data
        crop_mask - (optional) a cropping mask

        returns a workspace and module
        '''
        module = cellprofiler.modules.measureimageoverlap.MeasureImageOverlap()
        module.module_num = 1
        module.ground_truth.value = GROUND_TRUTH_IMAGE_NAME
        module.test_img.value = TEST_IMAGE_NAME
        module.wants_emd.value = True

        pipeline = cellprofiler.pipeline.Pipeline()

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

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

        image_set_list = cellprofiler.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)

        for name, d in ((GROUND_TRUTH_IMAGE_NAME, ground_truth),
                        (TEST_IMAGE_NAME, test)):
            image = cellprofiler.image.Image(
                d["image"],
                mask=d.get("mask"),
                crop_mask=d.get("crop_mask"),
                dimensions=dimensions
            )
            image_set.add(name, image)

        workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set,
                                                     cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(),
                                                     image_set_list)
        return workspace, module
    def make_workspace(self, labels):
        image_set_list = cellprofiler.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        object_set = cellprofiler.object.ObjectSet()
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        object_set.add_objects(objects, OBJECTS_NAME)
        m = cellprofiler.measurement.Measurements()
        module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
        module.module_num = 1
        module.object_groups[0].name.value = OBJECTS_NAME
        pipeline = cellprofiler.pipeline.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)
        workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set,
                                                     m, image_set_list)
        return workspace, module
    def test_04_02_dont_load_file(self):
        csv_text = '''"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
''' % (self.test_filename, self.test_path)
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            self.assertEqual(len(imgset.names), 0)
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 1
        pipeline.add_module(c0)
        try:
            module.wants_images.value = False
            pipeline.run()
            self.assertTrue(c0_ran[0])
        finally:
            os.remove(filename)
    def make_workspace(self, file_names):
        module = cellprofiler.modules.loadsingleimage.LoadSingleImage()
        module.module_num = 1
        module.directory.set_dir_choice(cellprofiler.modules.loadsingleimage.DEFAULT_INPUT_FOLDER_NAME)
        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 = cellprofiler.pipeline.Pipeline()

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

        pipeline.add_listener(callback)
        pipeline.add_module(module)
        image_set_list = cellprofiler.image.ImageSetList()
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set_list.get_image_set(0),
                                                     cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(),
                                                     image_set_list)
        return workspace, module
    def make_pipeline(self, csv_text, name=None):
        if name is None:
            handle, name = tempfile.mkstemp(".csv")
            fd = os.fdopen(handle, 'w')
        else:
            fd = open(name, "w")
        fd.write(csv_text)
        fd.close()
        csv_path, csv_file = os.path.split(name)
        module = cellprofiler.modules.loaddata.LoadText()
        module.csv_directory.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME
        module.csv_directory.custom_path = csv_path
        module.csv_file_name.value = csv_file
        module.module_num = 1
        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)

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

        pipeline.add_listener(error_callback)
        return pipeline, module, name
    def test_03_02_zernikes_are_different(self):
        '''Regression test of IMG-773'''

        numpy.random.seed(32)
        labels = numpy.zeros((40, 20), int)
        #
        # Make two "objects" composed of random foreground/background
        #
        labels[1:19, 1:19] = (numpy.random.uniform(size=(18, 18)) > .5).astype(int)
        labels[21:39, 1:19] = (numpy.random.uniform(size=(18, 18)) > .5).astype(int) * 2
        objects = cellprofiler.object.Objects()
        objects.segmented = labels
        object_set = cellprofiler.object.ObjectSet()
        object_set.add_objects(objects, "SomeObjects")
        module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape()
        module.object_groups[0].name.value = "SomeObjects"
        module.calculate_zernikes.value = True
        module.module_num = 1
        image_set_list = cellprofiler.image.ImageSetList()
        measurements = cellprofiler.measurement.Measurements()
        pipeline = cellprofiler.pipeline.Pipeline()
        pipeline.add_module(module)

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

        pipeline.add_listener(callback)
        workspace = cellprofiler.workspace.Workspace(pipeline, module,
                                                     image_set_list.get_image_set(0),
                                                     object_set, measurements, image_set_list)
        module.run(workspace)
        features = [x[1] for x in module.get_measurement_columns(pipeline)
                    if x[0] == "SomeObjects" and
                    x[1].startswith("AreaShape_Zernike")]
        for feature in features:
            values = measurements.get_current_measurement(
                    "SomeObjects", feature)
            self.assertEqual(len(values), 2)
            self.assertNotEqual(values[0], values[1])
def test_01_03_combine_channels():
    numpy.random.seed(13)
    image = numpy.random.uniform(size=(20, 10, 5))
    image_set_list = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    image_set.add(IMAGE_NAME, cellprofiler.image.Image(image))

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

    channel_indexes = numpy.array([2, 0, 3])
    factors = numpy.random.uniform(size=3)
    divisor = numpy.sum(factors)
    expected = numpy.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 = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    workspace = Workspace(pipeline, module, image_set, cellprofiler.object.ObjectSet(),
                          cellprofiler.measurement.Measurements(), image_set_list)
    module.run(workspace)
    pixels = image_set.get_image(module.grayscale_name.value).pixel_data
    assert pixels.ndim == 2
    assert tuple(pixels.shape) == (20, 10)
    numpy.testing.assert_almost_equal(expected, pixels)
    def make_workspace(self, input_image, input_mask=None,
                       reference_image=None, reference_mask=None,
                       measurement=None):
        pipeline = cellprofiler.pipeline.Pipeline()

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

        pipeline.add_listener(callback)
        object_set = cellprofiler.object.ObjectSet()
        image_set_list = cellprofiler.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        measurements = cellprofiler.measurement.Measurements()
        module_number = 1
        module = cellprofiler.modules.rescaleintensity.RescaleIntensity()
        module.x_name.value = INPUT_NAME
        if isinstance(input_image, (tuple, list)):
            first_input_image = input_image[0]
        else:
            first_input_image = input_image
        if isinstance(input_mask, (tuple, list)):
            first_input_mask = input_mask[0]
        else:
            first_input_mask = input_mask
        if first_input_mask is None:
            image = cellprofiler.image.Image(first_input_image)
        else:
            image = cellprofiler.image.Image(first_input_image, first_input_mask)
        ii = cellprofiler.modules.injectimage.InjectImage(INPUT_NAME, input_image, input_mask)
        ii.module_num = module_number
        module_number += 1
        pipeline.add_module(ii)
        image_set.add(INPUT_NAME, image)
        module.y_name.value = OUTPUT_NAME
        if reference_image is not None:
            module.matching_image_name.value = REFERENCE_NAME
            if reference_mask is None:
                image = cellprofiler.image.Image(reference_image)
            else:
                image = cellprofiler.image.Image(reference_image, mask=reference_mask)
            image_set.add(REFERENCE_NAME, image)
            ii = cellprofiler.modules.injectimage.InjectImage(REFERENCE_NAME, reference_image, reference_mask)
            ii.module_num = module_number
            module_number += 1
            pipeline.add_module(ii)
        module.module_num = module_number
        pipeline.add_module(module)
        if measurement is not None:
            module.divisor_measurement.value = MEASUREMENT_NAME
            measurements.add_image_measurement(MEASUREMENT_NAME, measurement)
        workspace = cellprofiler.workspace.Workspace(pipeline,
                                                     module,
                                                     image_set,
                                                     object_set,
                                                     measurements,
                                                     image_set_list)
        return workspace, module
def test_01_04_split_channels():
    numpy.random.seed(13)
    image = numpy.random.uniform(size=(20, 10, 5))
    image_set_list = cellprofiler.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    image_set.add(IMAGE_NAME, cellprofiler.image.Image(image))

    module = cellprofiler.modules.colortogray.ColorToGray()
    module.module_num = 1
    module.image_name.value = IMAGE_NAME
    module.combine_or_split.value = cellprofiler.modules.colortogray.SPLIT
    module.rgb_or_channels.value = cellprofiler.modules.colortogray.CH_CHANNELS
    module.add_channel()
    module.add_channel()
    module.add_channel()
    module.add_channel()

    channel_indexes = numpy.array([1, 4, 2])
    for i, channel_index in enumerate(channel_indexes):
        module.channels[i].channel_choice.value = module.channel_names[channel_index]
        module.channels[i].image_name.value = OUTPUT_IMAGE_F % i

    pipeline = cellprofiler.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    workspace = Workspace(pipeline, module, image_set, cellprofiler.object.ObjectSet(),
                          cellprofiler.measurement.Measurements(), image_set_list)
    module.run(workspace)
    for i, channel_index in enumerate(channel_indexes):
        pixels = image_set.get_image(module.channels[i].image_name.value).pixel_data
        assert pixels.ndim == 2
        assert tuple(pixels.shape) == (20, 10)
        numpy.testing.assert_almost_equal(image[:, :, channel_index], pixels)
 def make_workspace(self, pixel_data, mask=None, objects=None, dimensions=2):
     image_set_list = cellprofiler.image.ImageSetList()
     image_set = image_set_list.get_image_set(0)
     object_set = cellprofiler.object.ObjectSet()
     image = cellprofiler.image.Image(pixel_data, dimensions=dimensions)
     if not mask is None:
         image.mask = mask
     image_set.add(IMAGES_NAME, image)
     if not objects is None:
         o = cellprofiler.object.Objects()
         o.segmented = objects
         object_set.add_objects(o, OBJECTS_NAME)
     module = M.MeasureImageQuality()
     module.images_choice.value = M.O_SELECT
     module.image_groups[0].include_image_scalings.value = False
     module.image_groups[0].image_names.value = IMAGES_NAME
     module.image_groups[0].use_all_threshold_methods.value = False
     module.module_num = 1
     pipeline = cellprofiler.pipeline.Pipeline()
     pipeline.add_module(module)
     workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set,
                                                  object_set,
                                                  cellprofiler.measurement.Measurements(), image_set_list)
     return workspace