Пример #1
0
    def merge_files(destination, sources, force_headless=False):
        is_headless = force_headless or get_headless()
        if not is_headless:
            import wx
        if len(sources) == 0:
            return
        if not is_headless:
            progress = wx.ProgressDialog(
                "Writing " + destination,
                "Loading " + sources[0],
                maximum=len(sources) * 4 + 1,
                style=wx.PD_CAN_ABORT
                | wx.PD_APP_MODAL
                | wx.PD_ELAPSED_TIME
                | wx.PD_REMAINING_TIME,
            )
        count = 0
        try:
            pipeline = cpp.Pipeline()
            has_error = [False]

            def callback(caller, event):
                if isinstance(event, cpp.LoadExceptionEvent):
                    has_error = True
                    wx.MessageBox(
                        message="Could not load %s: %s" % (sources[0], event.error),
                        caption="Failed to load %s" % sources[0],
                    )
                    has_error[0] = True

            pipeline.add_listener(callback)

            pipeline.load(sources[0])
            if has_error[0]:
                return
            if destination.lower().endswith(".h5"):
                mdest = cpmeas.Measurements(filename=destination, multithread=False)
                h5_dest = True
            else:
                mdest = cpmeas.Measurements(multithread=False)
                h5_dest = False
            for source in sources:
                if not is_headless:
                    count += 1
                    keep_going, skip = progress.Update(count, "Loading " + source)
                    if not keep_going:
                        return
                if h5py.is_hdf5(source):
                    msource = cpmeas.Measurements(
                        filename=source, mode="r", multithread=False
                    )
                else:
                    msource = cpmeas.load_measurements(source)
                dest_image_numbers = mdest.get_image_numbers()
                source_image_numbers = msource.get_image_numbers()
                if len(dest_image_numbers) == 0 or len(source_image_numbers) == 0:
                    offset_source_image_numbers = source_image_numbers
                else:
                    offset_source_image_numbers = (
                        np.max(dest_image_numbers)
                        - np.min(source_image_numbers)
                        + source_image_numbers
                        + 1
                    )
                for object_name in msource.get_object_names():
                    if object_name in mdest.get_object_names():
                        destfeatures = mdest.get_feature_names(object_name)
                    else:
                        destfeatures = []
                    for feature in msource.get_feature_names(object_name):
                        if object_name == cpmeas.EXPERIMENT:
                            if not mdest.has_feature(object_name, feature):
                                src_value = msource.get_experiment_measurement(feature)
                                mdest.add_experiment_measurement(feature, src_value)
                            continue
                        src_values = msource.get_measurement(
                            object_name, feature, image_set_number=source_image_numbers
                        )
                        mdest[
                            object_name, feature, offset_source_image_numbers
                        ] = src_values
                    destset = set(destfeatures)
            if not is_headless:
                keep_going, skip = progress.Update(
                    count + 1, "Saving to " + destination
                )
                if not keep_going:
                    return
            if not h5_dest:
                pipeline.save_measurements(destination, mdest)
        finally:
            if not is_headless:
                progress.Destroy()
Пример #2
0
    def load(self, filename, load_pipeline):
        """Load a workspace from a .cpi file

        filename - path to file to load

        load_pipeline - true to load the pipeline from the file, false to
                        use the current pipeline.
        """
        import shutil
        from .pipeline import (
            M_PIPELINE,
            M_DEFAULT_INPUT_FOLDER,
            M_DEFAULT_OUTPUT_FOLDER,
        )
        import cellprofiler.measurement as cpmeas
        from cellprofiler.preferences import (
            set_default_image_directory,
            set_default_output_directory,
        )

        image_set_and_measurements_are_same = False
        if self.__measurements is not None:
            image_set_and_measurements_are_same = id(
                self.__measurements) == id(self.__image_set)
            self.close()
        self.__loading = True
        try:
            #
            # Copy the file to a temporary location before opening
            #
            fd, self.__filename = cpmeas.make_temporary_file()
            os.close(fd)

            shutil.copyfile(filename, self.__filename)

            self.__measurements = cpmeas.Measurements(filename=self.__filename,
                                                      mode="r+")
            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)
            if load_pipeline and self.__measurements.has_feature(
                    cpmeas.EXPERIMENT, M_PIPELINE):
                pipeline_txt = self.__measurements.get_experiment_measurement(
                    M_PIPELINE)
                # CP 3.1.8 cpproj (and possibly before) saved info in bytes; converting to python 3 string
                if type(pipeline_txt) == bytes:
                    pipeline_txt = (pipeline_txt.decode(
                        "unicode_escape").encode("utf-8").replace(
                            b"\\x00",
                            b"").decode("unicode_escape").replace("ÿþ", ""))
                self.pipeline.load(six.moves.StringIO(pipeline_txt))
            elif load_pipeline:
                self.pipeline.clear()
            else:
                fd = six.moves.StringIO()
                self.pipeline.savetxt(fd, save_image_plane_details=False)
                self.__measurements.add_experiment_measurement(
                    M_PIPELINE, fd.getvalue())

            for feature, function in (
                (M_DEFAULT_INPUT_FOLDER, set_default_image_directory),
                (M_DEFAULT_OUTPUT_FOLDER, set_default_output_directory),
            ):
                if self.measurements.has_feature(cpmeas.EXPERIMENT, feature):
                    path = self.measurements[cpmeas.EXPERIMENT, feature]
                    if os.path.isdir(path):
                        function(path)
            if image_set_and_measurements_are_same:
                self.__image_set = self.__measurements

        finally:
            self.__loading = False
        self.notify(self.WorkspaceLoadedEvent(self))
    def make_workspace(self,
                       control_points,
                       lengths,
                       radii,
                       image,
                       mask=None,
                       auximage=None):
        '''Create a workspace containing the control point measurements

        control_points - an n x 2 x m array where n is the # of control points,
                         and m is the number of objects.
        lengths - the length of each object
        radii - the radii_from_training defining the radius at each control pt
        image - the image to be straightened
        mask - the mask associated with the image (default = no mask)
        auximage - a second image to be straightnened (default = no second image)
        '''
        module = S.StraightenWorms()
        module.objects_name.value = OBJECTS_NAME
        module.straightened_objects_name.value = STRAIGHTENED_OBJECTS_NAME
        module.images[0].image_name.value = IMAGE_NAME
        module.images[
            0].straightened_image_name.value = STRAIGHTENED_IMAGE_NAME
        module.flip_image.value = IMAGE_NAME
        module.module_num = 1

        # Trick the module into thinking it's read the data file

        class P:
            def __init__(self):
                self.radii_from_training = radii

        module.training_set_directory.dir_choice = cps.URL_FOLDER_NAME
        module.training_set_directory.custom_path = "http://www.cellprofiler.org"
        module.training_set_file_name.value = "TrainingSet.xml"
        module.training_params = {"TrainingSet.xml": (P(), "URL")}

        pipeline = cpp.Pipeline()
        pipeline.add_module(module)

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

        pipeline.add_listener(callback)

        m = cpmeas.Measurements()
        for i, (y, x) in enumerate(control_points):
            for v, f in ((x, S.F_CONTROL_POINT_X), (y, S.F_CONTROL_POINT_Y)):
                feature = "_".join((S.C_WORM, f, str(i + 1)))
                m.add_measurement(OBJECTS_NAME, feature, v)
        feature = "_".join((S.C_WORM, S.F_LENGTH))
        m.add_measurement(OBJECTS_NAME, feature, lengths)

        image_set_list = cpi.ImageSetList()
        image_set = image_set_list.get_image_set(0)
        image_set.add(IMAGE_NAME, cpi.Image(image, mask))

        if auximage is not None:
            image_set.add(AUX_IMAGE_NAME, cpi.Image(auximage))
            module.add_image()
            module.images[1].image_name.value = AUX_IMAGE_NAME
            module.images[
                1].straightened_image_name.value = AUX_STRAIGHTENED_IMAGE_NAME

        object_set = cpo.ObjectSet()
        objects = cpo.Objects()
        labels = np.zeros(image.shape, int)
        for i in range(control_points.shape[2]):
            if lengths[i] == 0:
                continue
            self.rebuild_worm_from_control_points_approx(
                control_points[:, :, i], radii, labels, i + 1)
        objects.segmented = labels

        object_set.add_objects(objects, OBJECTS_NAME)

        workspace = cpw.Workspace(pipeline, module, image_set, object_set, m,
                                  image_set_list)
        return workspace, module