How to use the cellprofiler.measurement.Measurements function in CellProfiler

To help you get started, we’ve selected a few CellProfiler examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CellProfiler / CellProfiler / tests / modules / test_loadsingleimage.py View on Github external
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)
github CellProfiler / CellProfiler / tests / modules / test_labelimages.py View on Github external
module = cellprofiler.modules.labelimages.LabelImages()
    pipeline = cellprofiler.pipeline.Pipeline()

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

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

    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
github CellProfiler / CellProfiler / tests / modules / test_loadimages.py View on Github external
return
    file_name = "DrosophilaEmbryo_GFPHistone.avi"
    avi_path = tests.modules.testimages_directory()
    tests.modules.maybe_download_tesst_image(file_name)
    module = cellprofiler.modules.loadimages.LoadImages()
    module.file_types.value = cellprofiler.modules.loadimages.FF_AVI_MOVIES
    module.images[0].common_text.value = file_name
    module.images[0].channels[0].image_name.value = "MyImage"
    module.location.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME
    module.location.custom_path = avi_path
    module.set_module_num(1)
    pipeline = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(module)
    pipeline.add_listener(error_callback)
    image_set_list = cellprofiler.image.ImageSetList()
    m = cellprofiler.measurement.Measurements()
    workspace = cellprofiler.workspace.Workspace(
        pipeline, module, None, None, m, image_set_list
    )
    module.prepare_run(workspace)
    assert m.image_set_count == 65
    module.prepare_group(workspace, (), [1, 2, 3])
    image_set = image_set_list.get_image_set(0)
    workspace = cellprofiler.workspace.Workspace(
        pipeline, module, image_set, cellprofiler.object.ObjectSet(), m, image_set_list
    )
    module.run(workspace)
    assert "MyImage" in image_set.names
    image = image_set.get_image("MyImage")
    img1 = image.pixel_data
    assert tuple(img1.shape) == (264, 542, 3)
    t = m.get_current_image_measurement(
github CellProfiler / CellProfiler / tests / modules / test_flipandrotate.py View on Github external
image_set = image_set_list.get_image_set(0)
    image_set.add(IMAGE_NAME, img)
    module = cellprofiler.modules.flipandrotate.FlipAndRotate()
    module.image_name.value = IMAGE_NAME
    module.output_name.value = OUTPUT_IMAGE
    module.set_module_num(1)
    if fn is not None:
        fn(module)
    pipeline = cellprofiler.pipeline.Pipeline()
    pipeline.add_module(module)

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

    pipeline.add_listener(error_callback)
    measurements = cellprofiler.measurement.Measurements()
    workspace = cellprofiler.workspace.Workspace(
        pipeline,
        module,
        image_set,
        cellprofiler.object.ObjectSet(),
        measurements,
        image_set_list,
    )
    module.run(workspace)
    feature = cellprofiler.modules.flipandrotate.M_ROTATION_F % OUTPUT_IMAGE
    assert feature in measurements.get_feature_names(cellprofiler.measurement.IMAGE)
    angle = measurements.get_current_image_measurement(feature)
    output_image = image_set.get_image(OUTPUT_IMAGE)
    return output_image, angle
github CellProfiler / CellProfiler / tests / modules / test_identifyobjectsingrid.py View on Github external
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
github CellProfiler / CellProfiler / tests / modules / test_matchtemplate.py View on Github external
def measurements():
    return cellprofiler.measurement.Measurements()
github CellProfiler / CellProfiler / tests / modules / test_loadimages.py View on Github external
pipeline.load(io.StringIO(pipeline_text))

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

    pipeline.add_listener(callback)
    m = [
        m
        for m in pipeline.modules()
        if isinstance(m, cellprofiler.modules.loadimages.LoadImages)
    ][0]
    m1 = cellprofiler.measurement.Measurements()
    w1 = cellprofiler.workspace.Workspace(pipeline, m, m1, None, m1, None)
    pipeline.prepare_run(w1)

    m2 = cellprofiler.measurement.Measurements()
    w2 = cellprofiler.workspace.Workspace(pipeline, m, m2, None, m2, None)
    urls = [
        cellprofiler.modules.loadimages.pathname2url(os.path.join(directory, filename))
        for filename in os.listdir(directory)
        if fn_filter(filename)
    ]
    w2.file_list.add_files_to_filelist(urls)
    pipeline.add_urls(urls, False)
    pipeline.convert_legacy_input_modules()
    pipeline.prepare_run(w2)

    ff1 = m1.get_feature_names(cellprofiler.measurement.IMAGE)
    ffexpected = [
        f.replace("IMAGE_FOR_", "")
        for f in ff1
        if not f.startswith(cellprofiler.measurement.C_METADATA)
github CellProfiler / CellProfiler / cellprofiler / measurement.py View on Github external
data = data[0]
            if data is None:
                data = []
            self.hdf5_dict[
                EXPERIMENT, feature_name, 0, data_type
            ] = Measurements.wrap_string(data)
        elif object_name == IMAGE:
            if numpy.isscalar(image_set_number):
                image_set_number = [image_set_number]
                data = [data]
            data = [
                d
                if d is None or d is numpy.NaN
                else Measurements.wrap_string(d)
                if numpy.isscalar(d)
                else Measurements.wrap_string(d[0])
                if data_type is None
                else d
                for d in data
            ]
            self.hdf5_dict[IMAGE, feature_name, image_set_number, data_type] = data
            for n in image_set_number:
                if not self.hdf5_dict.has_data(object_name, IMAGE_NUMBER, n):
                    self.hdf5_dict[IMAGE, IMAGE_NUMBER, n] = n
        else:
            self.hdf5_dict[
                object_name, feature_name, image_set_number, data_type
            ] = data
            for n, d in (
                ((image_set_number, data),)
                if numpy.isscalar(image_set_number)
                else list(zip(image_set_number, data))
github CellProfiler / CellProfiler / cellprofiler / analysis.py View on Github external
fd.write(self.initial_measurements_buf)
                    fd.close()
                    initial_measurements = cellprofiler.measurement.Measurements(
                        filename=filename, mode="r"
                    )
                    measurements = cellprofiler.measurement.Measurements(
                        image_set_start=None, copy=initial_measurements, mode="a"
                    )
                finally:
                    if initial_measurements is not None:
                        initial_measurements.close()
                    os.unlink(filename)
            else:
                with open(self.output_path, "wb") as fd:
                    fd.write(self.initial_measurements_buf)
                measurements = cellprofiler.measurement.Measurements(
                    image_set_start=None, filename=self.output_path, mode="a"
                )
            # The shared dicts are needed in jobserver()
            self.shared_dicts = [m.get_dictionary() for m in self.pipeline.modules()]
            workspace = cellprofiler.workspace.Workspace(
                self.pipeline,
                None,
                None,
                None,
                measurements,
                cellprofiler.image.ImageSetList(),
            )

            if image_set_end is None:
                image_set_end = measurements.get_image_numbers()[-1]
            image_sets_to_process = list(
github CellProfiler / CellProfiler / cellprofiler / workspace.py View on Github external
def create(self):
        """Create a new workspace file

        filename - name of the workspace file
        """
        from .measurement import Measurements, make_temporary_file

        if isinstance(self.measurements, Measurements):
            self.close()

        fd, self.__filename = make_temporary_file()
        self.__measurements = Measurements(filename=self.__filename, mode="w")
        os.close(fd)
        if self.__file_list is not None:
            self.__file_list.remove_notification_callback(self.__on_file_list_changed)
        self.__file_list = cellprofiler.utilities.hdf5_dict.HDF5FileList(
            self.measurements.hdf5_dict.hdf5_file
        )
        self.__file_list.add_notification_callback(self.__on_file_list_changed)
        self.notify(self.WorkspaceCreatedEvent(self))