Exemple #1
0
def make_workspace(labels,
                   operation,
                   iterations=1,
                   wants_outlines=False,
                   wants_fill_holes=False):
    object_set = cellprofiler_core.object.ObjectSet()
    objects = cellprofiler_core.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.operation.value = operation
    module.iterations.value = iterations
    module.wants_fill_holes.value = wants_fill_holes
    module.set_module_num(1)
    pipeline = cellprofiler_core.pipeline.Pipeline()
    pipeline.add_module(module)
    image_set_list = cellprofiler_core.image.ImageSetList()
    workspace = cellprofiler_core.workspace.Workspace(
        pipeline,
        module,
        image_set_list.get_image_set(0),
        object_set,
        cellprofiler_core.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
Exemple #2
0
def test_invert_binary_invert():
    #
    # Regression for issue #1329
    #
    r = numpy.random.RandomState()
    r.seed(1102)
    m = cellprofiler_core.measurement.Measurements()
    pixel_data = r.uniform(size=(20, 20)) > 0.5
    m.add("inputimage", cellprofiler_core.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.set_module_num(1)
    pipeline = cellprofiler_core.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.set_module_num(2)
    pipeline = cellprofiler_core.pipeline.Pipeline()
    workspace = cellprofiler_core.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 > 0.5)
    def make_workspace(self, parents, children, fake_measurement=False):
        """Make a workspace for testing Relate"""
        pipeline = cellprofiler_core.pipeline.Pipeline()
        if fake_measurement:

            class FakeModule(cellprofiler_core.module.Module):
                def get_measurement_columns(self, pipeline):
                    return [
                        (
                            CHILD_OBJECTS,
                            MEASUREMENT,
                            cellprofiler_core.measurement.COLTYPE_FLOAT,
                        ),
                        (
                            CHILD_OBJECTS,
                            IGNORED_MEASUREMENT,
                            cellprofiler_core.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_core.object.ObjectSet()
        image_set_list = cellprofiler_core.image.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        m = cellprofiler_core.measurement.Measurements()
        m.add_image_measurement(cellprofiler_core.measurement.GROUP_NUMBER, 1)
        m.add_image_measurement(cellprofiler_core.measurement.GROUP_INDEX, 1)
        workspace = cellprofiler_core.workspace.Workspace(
            pipeline, module, image_set, object_set, m, image_set_list
        )
        o = cellprofiler_core.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_core.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 #4
0
def make_workspace(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.set_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_core.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event,
                              cellprofiler_core.pipeline.event.RunException)

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

    image_set_list = cellprofiler_core.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_core.image.Image(
            d["image"],
            mask=d.get("mask"),
            crop_mask=d.get("crop_mask"),
            dimensions=dimensions,
        )
        image_set.add(name, image)

    workspace = cellprofiler_core.workspace.Workspace(
        pipeline,
        module,
        image_set,
        cellprofiler_core.object.ObjectSet(),
        cellprofiler_core.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
Exemple #5
0
def make_obj_workspace(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.set_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_core.pipeline.Pipeline()

    def callback(caller, event):
        assert not isinstance(event, cellprofiler_core.pipeline.event.RunException)

    pipeline.add_listener(callback)
    pipeline.add_module(module)
    image_set_list = cellprofiler_core.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_core.image.Image(
            d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")
        )
        image_set.add(name, image)
    object_set = cellprofiler_core.object.ObjectSet()
    for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ, id_obj)):
        object = cellprofiler_core.object.Objects()
        if d.shape[1] == 3:
            object.ijv = d
        else:
            object.segmented = d
        object_set.add_objects(object, name)
    workspace = cellprofiler_core.workspace.Workspace(
        pipeline,
        module,
        image_set,
        object_set,
        cellprofiler_core.measurement.Measurements(),
        image_set_list,
    )
    return workspace, module
Exemple #6
0
def run_imagemath(images, modify_module_fn=None, measurement=None):
    """Run the ImageMath module, returning the image created

    images - a list of dictionaries. The dictionary has keys:
             pixel_data - image pixel data
             mask - mask for image
             cropping - cropping mask for image
    modify_module_fn - a function of the signature, fn(module)
             that allows the test to modify the module.
    measurement - an image measurement value
    """
    image_set_list = cellprofiler_core.image.ImageSetList()
    image_set = image_set_list.get_image_set(0)
    module = cellprofiler.modules.imagemath.ImageMath()
    module.set_module_num(1)
    for i, image in enumerate(images):
        pixel_data = image["pixel_data"]
        mask = image.get("mask", None)
        cropping = image.get("cropping", None)
        if i >= 2:
            module.add_image()
        name = "inputimage%s" % i
        module.images[i].image_name.value = name
        img = cellprofiler_core.image.Image(pixel_data,
                                            mask=mask,
                                            crop_mask=cropping)
        image_set.add(name, img)
    module.output_image_name.value = "outputimage"
    if modify_module_fn is not None:
        modify_module_fn(module)
    pipeline = cellprofiler_core.pipeline.Pipeline()
    pipeline.add_module(module)
    measurements = cellprofiler_core.measurement.Measurements()
    if measurement is not None:
        measurements.add_image_measurement(MEASUREMENT_NAME, str(measurement))
    workspace = cellprofiler_core.workspace.Workspace(
        pipeline,
        module,
        image_set,
        cellprofiler_core.object.ObjectSet(),
        measurements,
        image_set_list,
    )
    module.run(workspace)
    return image_set.get_image("outputimage")
def make_pipeline(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_core.setting.ABSOLUTE_FOLDER_NAME
    module.csv_directory.custom_path = csv_path
    module.csv_file_name.value = csv_file
    module.set_module_num(1)
    pipeline = cellprofiler_core.pipeline.Pipeline()
    pipeline.add_module(module)

    def error_callback(caller, event):
        assert not isinstance(event, cellprofiler_core.pipeline.event.RunException)

    pipeline.add_listener(error_callback)
    return pipeline, module, name
Exemple #8
0
def test_add_and_do_nothing():
    #
    # Regression for issue #1333 - add one, do nothing, input image
    # is changed
    #
    r = numpy.random.RandomState()
    r.seed(1101)
    m = cellprofiler_core.measurement.Measurements()
    pixel_data = r.uniform(size=(20, 20))
    m.add("inputimage", cellprofiler_core.image.Image(pixel_data))
    module = cellprofiler.modules.imagemath.ImageMath()
    module.images[0].image_name.value = "inputimage"
    module.output_image_name.value = "outputimage"
    module.operation.value = cellprofiler.modules.imagemath.O_NONE
    module.addend.value = 0.5
    module.set_module_num(1)
    pipeline = cellprofiler_core.pipeline.Pipeline()
    pipeline.add_module(module)
    workspace = cellprofiler_core.workspace.Workspace(pipeline, module, m,
                                                      None, m, None)
    module.run(workspace)
    numpy.testing.assert_array_almost_equal(
        pixel_data,
        m.get_image("inputimage").pixel_data)
Exemple #9
0
def test_module_must_be_last():
    """Make sure that the pipeline is invalid if CreateBatchFiles is not last"""
    #
    # First, make sure that a naked CPModule tests valid
    #
    pipeline = cellprofiler_core.pipeline.Pipeline()
    module = cellprofiler_core.module.Module()
    module.set_module_num(len(pipeline.modules()) + 1)
    pipeline.add_module(module)
    pipeline.test_valid()
    #
    # Make sure that CreateBatchFiles on its own tests valid
    #
    pipeline = cellprofiler_core.pipeline.Pipeline()
    module = cellprofiler.modules.createbatchfiles.CreateBatchFiles()
    module.set_module_num(len(pipeline.modules()) + 1)
    pipeline.add_module(module)
    pipeline.test_valid()

    module = cellprofiler_core.module.Module()
    module.set_module_num(len(pipeline.modules()) + 1)
    pipeline.add_module(module)
    with pytest.raises(cellprofiler_core.setting.ValidationError):
        pipeline.test_valid()
 def loader(module_num, mn=mn):
     module = cellprofiler_core.modules.instantiate_module(mn)
     module.set_module_num(module_num)
     return module