def upgrade_settings(self, setting_values, variable_revision_number,
                         module_name):
        if variable_revision_number == 1:
            setting_values = (setting_values[:5] +
                              [cpprefs.get_default_image_directory()] +
                              setting_values[5:])
            variable_revision_number = 2
        if variable_revision_number == 2:
            setting_values = (
                setting_values[:6] +
                [int(re.sub(r"\.|rc\d{1}", "", cellprofiler.__version__))] +
                setting_values[6:])
            variable_revision_number = 3
        if variable_revision_number == 3:
            # Pickled image list is now the batch state
            self.batch_state = np.array(zlib.compress(setting_values[4]))
            setting_values = setting_values[:4] + setting_values[5:]
            variable_revision_number = 4
        if variable_revision_number == 4:
            setting_values = setting_values[:4] + [False] + setting_values[4:]
            variable_revision_number = 5
        if variable_revision_number == 5:
            # added from_old_matlab
            setting_values = setting_values[:7] + [False] + setting_values[7:]
            variable_revision_number = 6
        if variable_revision_number == 6:
            # added go_to_website
            setting_values = setting_values[:8] + [False] + setting_values[8:]
            variable_revision_number = 7
        if variable_revision_number == 7:
            setting_values = setting_values[:8] + setting_values[9:]
            variable_revision_number = 8

        return setting_values, variable_revision_number
    def create_settings(self):
        """Create the module settings and name the module"""
        self.wants_default_output_directory = cps.Binary(
            "Store batch files in default output folder?",
            True,
            doc="""\
Select "*Yes*" to store batch files in the Default Output folder.
Select "*No*" to enter the path to the folder that will be used to
store these files. The Default Output folder can be set by clicking the "View output settings" button in the main CP window, or in CellProfiler Preferences. """
            % globals(),
        )

        self.custom_output_directory = cps.Text(
            "Output folder path",
            cpprefs.get_default_output_directory(),
            doc=
            "Enter the path to the output folder. (Used only if not using the default output folder)",
        )

        # Worded this way not because I am windows-centric but because it's
        # easier than listing every other OS in the universe except for VMS
        self.remote_host_is_windows = cps.Binary(
            "Are the cluster computers running Windows?",
            False,
            doc="""\
Select "*Yes*" if the cluster computers are running one of the
Microsoft Windows operating systems. In this case, **CreateBatchFiles**
will modify all paths to use the Windows file separator (backslash \\\\ ).
Select "*No*" for **CreateBatchFiles** to modify all paths to use the
Unix or Macintosh file separator (slash / ).""" % globals(),
        )

        self.batch_mode = cps.Binary("Hidden: in batch mode", False)
        self.distributed_mode = cps.Binary("Hidden: in distributed mode",
                                           False)
        self.default_image_directory = cps.Setting(
            "Hidden: default input folder at time of save",
            cpprefs.get_default_image_directory(),
        )
        self.revision = cps.Integer("Hidden: revision number", 0)
        self.from_old_matlab = cps.Binary("Hidden: from old matlab", False)
        self.acknowledge_old_matlab = cps.DoSomething(
            "Could not update CP1.0 pipeline to be compatible with CP2.0.  See module notes.",
            "OK",
            self.clear_old_matlab,
        )
        self.mappings = []
        self.add_mapping()
        self.add_mapping_button = cps.DoSomething(
            "",
            "Add another path mapping",
            self.add_mapping,
            doc="""\
Use this option if another path must be mapped because there is a difference
between how the local computer sees a folder location vs. how the cluster
computer sees the folder location.""",
        )
Exemple #3
0
 def __init__(self, parent_sizer, panel, progress_panel, status_panel):
     self.__panel = panel
     self.__parent_sizer = parent_sizer
     panel.AutoLayout = True
     panel.SetSizer(wx.BoxSizer(wx.VERTICAL))
     static_box_sizer = wx.BoxSizer(wx.VERTICAL)
     panel.Sizer.Add(static_box_sizer, 1, wx.EXPAND)
     self.__sizer = static_box_sizer
     self.__image_folder_panel = wx.Panel(panel)
     self.__image_folder_panel.SetAutoLayout(True)
     self.__image_edit_box = self.__make_folder_panel(
         self.__image_folder_panel,
         get_default_image_directory(),
         lambda: get_recent_files(DEFAULT_IMAGE_DIRECTORY),
         "Default Input Folder",
         DEFAULT_IMAGE_FOLDER_HELP,
         [
             set_default_image_directory,
             self.__notify_pipeline_list_view_directory_change,
         ],
         refresh_action=self.refresh_input_directory,
     )
     self.__output_folder_panel = wx.Panel(panel)
     self.__output_folder_panel.SetAutoLayout(True)
     self.__output_edit_box = self.__make_folder_panel(
         self.__output_folder_panel,
         get_default_output_directory(),
         lambda: get_recent_files(DEFAULT_OUTPUT_DIRECTORY),
         "Default Output Folder",
         DEFAULT_OUTPUT_FOLDER_HELP,
         [
             set_default_output_directory,
             self.__notify_pipeline_list_view_directory_change,
         ],
     )
     self.__odds_and_ends_panel = wx.Panel(panel)
     self.__odds_and_ends_panel.SetAutoLayout(True)
     self.__make_odds_and_ends_panel()
     self.__status_panel = status_panel
     status_panel.Sizer = wx.BoxSizer()
     self.__status_text = wx.StaticText(status_panel,
                                        style=wx.SUNKEN_BORDER,
                                        label=WELCOME_MESSAGE)
     status_panel.Sizer.Add(self.__status_text, 1, wx.EXPAND)
     self.__progress_panel = progress_panel
     self.__progress_panel.AutoLayout = True
     self.__make_progress_panel()
     self.__sizer.AddMany([
         (self.__image_folder_panel, 0, wx.EXPAND | wx.ALL, 1),
         (self.__output_folder_panel, 0, wx.EXPAND | wx.ALL, 1),
         (self.__odds_and_ends_panel, 0, wx.EXPAND | wx.ALL, 1),
     ])
     self.show_status_text()
     self.__errors = set()
     self.__pipeline_list_view = None
     self.__progress_watcher = None
Exemple #4
0
    def save_default_folders_to_measurements(self):
        from ..constants.pipeline import M_DEFAULT_INPUT_FOLDER
        from ..constants.pipeline import M_DEFAULT_OUTPUT_FOLDER
        from cellprofiler_core.preferences import get_default_image_directory
        from cellprofiler_core.preferences import get_default_output_directory

        self.measurements.add_experiment_measurement(
            M_DEFAULT_INPUT_FOLDER, get_default_image_directory())
        self.measurements.add_experiment_measurement(
            M_DEFAULT_OUTPUT_FOLDER, get_default_output_directory())
    def save_pipeline(self, workspace, outf=None):
        """Save the pipeline in Batch_data.mat

        Save the pickled image_set_list state in a setting and put this
        module in batch mode.

        if outf is not None, it is used as a file object destination.
        """
        if outf is None:
            if self.wants_default_output_directory.value:
                path = cpprefs.get_default_output_directory()
            else:
                path = cpprefs.get_absolute_path(
                    self.custom_output_directory.value)
            h5_path = os.path.join(path, F_BATCH_DATA_H5)
        else:
            h5_path = outf

        image_set_list = workspace.image_set_list
        pipeline = workspace.pipeline
        m = cpmeas.Measurements(copy=workspace.measurements, filename=h5_path)
        try:
            assert isinstance(pipeline, cpp.Pipeline)
            assert isinstance(m, cpmeas.Measurements)

            orig_pipeline = pipeline
            pipeline = pipeline.copy()
            # this use of workspace.frame is okay, since we're called from
            # prepare_run which happens in the main wx thread.
            target_workspace = cpw.Workspace(pipeline, None, None, None, m,
                                             image_set_list, workspace.frame)
            pipeline.prepare_to_create_batch(target_workspace, self.alter_path)
            bizarro_self = pipeline.module(self.module_num)
            bizarro_self.revision.value = int(
                re.sub(r"\.|rc\d{1}", "", cellprofiler.__version__))
            if self.wants_default_output_directory:
                bizarro_self.custom_output_directory.value = self.alter_path(
                    cpprefs.get_default_output_directory())
            bizarro_self.default_image_directory.value = self.alter_path(
                cpprefs.get_default_image_directory())
            bizarro_self.batch_mode.value = True
            pipeline.write_pipeline_measurement(m)
            orig_pipeline.write_pipeline_measurement(m, user_pipeline=True)
            #
            # Write the path mappings to the batch measurements
            #
            m.write_path_mappings([(mapping.local_directory.value,
                                    mapping.remote_directory.value)
                                   for mapping in self.mappings])
            return h5_path
        finally:
            m.close()
    def estimate_absorbance(self):
        """Load an image and use it to estimate the absorbance of a stain

        Returns a 3-tuple of the R/G/B absorbances
        """
        from cellprofiler_core.image import FileImage
        import wx

        dlg = wx.FileDialog(None, "Choose reference image",
                            get_default_image_directory())
        dlg.Wildcard = (
            "Image file (*.tif, *.tiff, *.bmp, *.png, *.gif, *.jpg)|"
            "*.tif;*.tiff;*.bmp;*.png;*.gif;*.jpg")
        if dlg.ShowModal() == wx.ID_OK:
            lip = FileImage("dummy", "", dlg.Path)
            image = lip.provide_image(None).pixel_data
            if image.ndim < 3:
                wx.MessageBox(
                    "You must calibrate the absorbance using a color image",
                    "Error: not color image",
                    style=wx.OK | wx.ICON_ERROR,
                )
                return None
            #
            # Log-transform the image
            #
            eps = 1.0 / 256.0 / 2.0
            log_image = numpy.log(image + eps)
            data = [-log_image[:, :, i].flatten() for i in range(3)]
            #
            # Order channels by strength
            #
            sums = [numpy.sum(x) for x in data]
            order = numpy.lexsort([sums])
            #
            # Calculate relative absorbance against the strongest.
            # Fit Ax = y to find A where x is the strongest and y
            # is each in turn.
            #
            strongest = data[order[-1]][:, numpy.newaxis]
            absorbances = [
                scipy.linalg.lstsq(strongest, d)[0][0] for d in data
            ]
            #
            # Normalize
            #
            absorbances = numpy.array(absorbances)
            return absorbances / numpy.sqrt(numpy.sum(absorbances**2))
        return None
Exemple #7
0
    def prepare_run(self, workspace):
        """Set up omero image providers inside the image_set_list"""
        pipeline = workspace.pipeline
        image_set_list = workspace.image_set_list
        if pipeline.in_batch_mode():
            # TODO: Rewrite the OmeroImageProvider such that it can be used in batch mode
            # e.g., omero session keys could be used to attach to existing sessions to
            # keep OmeroImageProviders from creating a new session every time an image should be loaded
            return False

        if cpp.get_headless():
            print(
                "OmeroLoadImages running in headless mode: image directory parameter will be used as omero object id"
            )
            self.omero_object_id.set_value(
                int(cpp.get_default_image_directory()))
            print("omero object id = %d" % self.omero_object_id.value)
            print("omero object type = %s" % self.omero_object.value)

        self.create_omero_gateway()
        if self.omero_object == MS_IMAGE:
            omero_image_list = [
                self.omero_gateway.getImage(self.omero_object_id.value)
            ]
        elif self.omero_object == MS_DATASET:
            # Get dataset without leaves(=images&pixels)
            dataset = self.omero_gateway.getDataset(self.omero_object_id.value,
                                                    False)
            self.dataset_name = dataset.getName().getValue()
            omero_image_list = self.get_images_from_dataset(
                self.omero_object_id.value)
        elif self.omero_object == MS_PLATE:
            self.wells = self.get_wells_from_plate(self.omero_object_id.value)
            self.plate_name = self.wells[0].getPlate().getName().getValue()
            omero_image_list = []
            for well in self.wells:
                for wellsample in well.iterateWellSamples():
                    omero_image_list.append(wellsample.getImage())

        # get names and pixels from omero images
        pixels_list = []
        for omero_image in omero_image_list:
            image_id = omero_image.getId().getValue()
            pixels_list += self.omero_gateway.getPixelsFromImage(image_id)

        # add images to image sets
        image_set_count = len(pixels_list)
        for i in range(0, image_set_count):
            image_set = image_set_list.get_image_set(i)
            pixels = pixels_list[i]
            pixels_id = pixels.getId().getValue()
            sizeZ = pixels.getSizeZ().getValue()
            sizeC = pixels.getSizeC().getValue()
            sizeT = pixels.getSizeT().getValue()
            for channel in self.channels:
                for z in range(0, sizeZ):
                    for t in range(0, sizeT):
                        c = int(channel.channel_number.value)
                        self.save_image_set_info(
                            image_set,
                            channel.cpimage_name.value,
                            P_OMERO,
                            V_OMERO,
                            self.omero_gateway,
                            pixels_id,
                            z,
                            c,
                            t,
                        )
        return True
    def add_mapping(self):
        group = cps.SettingsGroup()
        group.append(
            "local_directory",
            cps.Text(
                "Local root path",
                cpprefs.get_default_image_directory(),
                doc="""\
Enter the path to files on this computer. This is the root path on the
local machine (i.e., the computer setting up the batch files).

For instance, a Windows machine might access files images by mounting the file system using a drive
letter, like this:

``Z:\your_data\images``

and the cluster computers access the same file system like this:

``/server_name/your_name/your_data/images``

In this case, since the ``your_data\images`` portion of the path is
the same for both, the local root path is the portion prior, i.e.,
``Z:\`` and similarly for the cluster root path, i.e.,
``/server_name/your_name/``.

If **CreateBatchFiles** finds any pathname that matches the local root path
at the beginning, it will replace that matching portion with the cluster root path.

For example, if you have mapped the remote cluster machine like this:

``Z:\your_data\images``

(on a Windows machine, for instance) and the cluster machine sees the same folder like this:

``/server_name/your_name/your_data/images``

you would enter ``Z:\`` here for the local root path and ``/server_name/your_name/`` for the
cluster root path in the next setting.""",
            ),
        )

        group.append(
            "remote_directory",
            cps.Text(
                "Cluster root path",
                cpprefs.get_default_image_directory(),
                doc="""\
Enter the path to files on the cluster. This is the cluster root path,
i.e., how the cluster machine sees the top-most folder where your
input/output files are stored.

For instance, a Windows machine might access files images by mounting the file system using a drive
letter, like this:

``Z:\your_data\images``

and the cluster computers access the same file system like this:

``/server_name/your_name/your_data/images``

In this case, since the ``your_data\images`` portion of the path is
the same for both, the local root path is the portion prior, i.e.,
``Z:\`` and similarly for the cluster root path, i.e.,
``/server_name/your_name/``.

If **CreateBatchFiles** finds any pathname that matches the local root path
at the beginning, it will replace that matching portion with the cluster root path.

For example, if you have mapped the remote cluster machine like this:

``Z:\your_data\images``

(on a Windows machine, for instance) and the cluster machine sees the same folder like this:

``/server_name/your_name/your_data/images``

you would enter ``Z:\`` in the previous setting for the local root
path and ``/server_name/your_name/`` here for the cluster root path.""",
            ),
        )
        group.append(
            "remover",
            cps.RemoveSettingButton("", "Remove this path mapping",
                                    self.mappings, group),
        )
        group.append("divider", cps.Divider(line=False))
        self.mappings.append(group)
Exemple #9
0
 def __on_preferences_image_directory_event(self, event):
     if self.__image_edit_box.GetValue() != get_default_image_directory():
         self.__image_edit_box.SetValue(get_default_image_directory())