Example #1
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 = 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).encode("utf-8")
                self.pipeline.load(StringIO(pipeline_txt))
            elif load_pipeline:
                self.pipeline.clear()
            else:
                fd = 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))
Example #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))