예제 #1
0
    def save_run_output(self, sample_num, directory_path, output_file):
        """Save the parameter settings and images for the current run of
        'self.__module'."""
        for i, setting in enumerate(self.__module.visible_settings()):
            value_to_write = ""

            # Do not write settings without values, ie, buttons etc
            if setting.get_text() != "":
                value_to_write = str(setting.get_value())
                if isinstance(setting,
                              cellprofiler_core.setting.ImageNameProvider):
                    # Save image
                    image = self.__measurements.get_image(value_to_write)
                    path = os.path.join(directory_path,
                                        value_to_write + "_" + str(sample_num))
                    self.save_image(image, path)
                    value_to_write += "_" + str(sample_num) + ".jpg"
                    # ~*~
                    # elif isinstance(setting, settings.ObjectNameProvider):
                    #    print 'Bingo! ' + str(value_to_write)
                    #    objects =\
                    #        self.__workspace.get_object_set().get_objects(setting.get_value())
                    #    value_to_write = str(len(objects.indices))
                    # ~^~
            if i < len(self.__module.visible_settings()
                       ) - 1 and value_to_write != "":
                value_to_write += "\t"
            if i == len(self.__module.visible_settings()) - 1:
                value_to_write += "\n"

            # Write line to 'open_file'
            if value_to_write != "":
                output_file.write(value_to_write)
예제 #2
0
    def get_parameters_list(self):
        """Get and return the list of unbounded parameters.

        This method first identifies all unbounded parameters in 'module'.
        Next, it updates a list of mappings to tuples:
            a parameter's 0-based index -> (its label, its 1-based module index),
            eg, 0 -> (Crop, 2); 1 -> (Crop, 3); 2 -> (IdentifyPrimaryObjects, 4).
        """
        parameters_list = []
        # Get unique keys of visible parameters
        visible_settings_keys = []
        for setting in self.__module.visible_settings():
            visible_settings_keys.append(setting.key())
        # Update dictionary of visible, unbounded parameters:
        #   0-based index in 'parameters_list' -> (label, 1-based module number)
        for i, setting in enumerate(self.__module.settings()):
            if (setting.key() in visible_settings_keys and
                    self.get_parameter_type(setting) == PARAM_CLASS_UNBOUNDED):
                parameters_list.append((setting.get_text(), i + 1))
        return parameters_list
예제 #3
0
    def on_button(self, event):
        if event.GetId() == ID_SAMPLE_BUTTON:
            # try:
            self.validate_input_ranges()
            number = self.calc_number_samples()

            sample_dialog = wx.MessageDialog(
                self,
                "Proceed with calculating " + str(number) + " samples?",
                caption="Confirm sample size",
                style=wx.ICON_QUESTION | wx.OK | wx.CANCEL,
            )
            if sample_dialog.ShowModal() == wx.ID_OK:

                save_dialog = wx.FileDialog(
                    event.GetEventObject(),
                    message="Save sampled output",
                    wildcard="Tab separated values (*.tsv)|*.tsv",
                    style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
                )
                if save_dialog.ShowModal() == wx.ID_OK:
                    # 1. Copy original parameter values
                    original_values = []
                    for setting in self.__module.visible_settings():
                        original_values.append(setting.get_value())

                    # 2. Sample input parameters
                    self.__sample_list = self.generate_parameter_samples()

                    # 3. Open output file and write headers
                    output_file = open(save_dialog.GetPath(), "w")
                    headers = ""
                    for i, setting in enumerate(
                            self.__module.visible_settings()):
                        # Do not write settings without values, ie, buttons etc
                        if setting.get_text() != "":
                            headers += setting.get_text()
                            if i < len(self.__module.visible_settings()) - 1:
                                headers += "\t"
                    headers += "\n"
                    output_file.write(headers)

                    # 4. Run pipeline once for each sample

                    # ~*~
                    self.Show(False)
                    progressDialog = wx.ProgressDialog(
                        parent=self,
                        title="Sampling parameters",
                        message="Run 1",
                        maximum=len(self.__sample_list),
                    )
                    size = progressDialog.GetSize()
                    size.SetWidth(2 * size.GetWidth())
                    progressDialog.SetSize(size)
                    # ~^~

                    for i, l in enumerate(self.__sample_list):
                        # print '\nStarting run ' + str(i+1) + '...'

                        for j, value in enumerate(l):
                            if value is not None:
                                setting_nr = self.__parameters_list[j][1]
                                setting = self.__module.setting(setting_nr)
                                setting.set_value(value)
                                # print str(setting.get_text()) + ' -> ' + str(setting.get_value())

                        # ~*~
                        progressDialog.Update(
                            i + 1,
                            newmsg="Executing run " + str(i + 1) + " of " +
                            str(len(self.__sample_list)),
                        )
                        # ~^~

                        # It's not very efficient to run the complete pipeline
                        # when only the last module's parameter values are
                        # different. However, this is the only way I can get
                        # the images to update correctly for the last module. Ie,
                        # when I don't prepare and run the pipeline from scratch
                        # for every different configuration of the last module,
                        # I get the images generated by the first run for every
                        # run.

                        # 4.1 Prepare to run pipeline
                        self.prepare_for_run()
                        # 4.2 Run modules
                        for module in self.__pipeline.modules():
                            if (module.get_module_num() <=
                                    self.__module.get_module_num()):
                                self.run_module(module)
                        # 4.3 Save output
                        self.save_run_output(i, save_dialog.GetDirectory(),
                                             output_file)

                        # This is the way to run headless, if only I could get at the images...
                        # self.stop_now = False
                        # running_pipeline = self.__pipeline.run_with_yield(
                        #    run_in_background=False,
                        #    status_callback=self.status_callback)
                        # while not self.stop_now:
                        #    measurements = running_pipeline.next()

                        # print '...run completed.'
                    # 5. Close output file
                    output_file.close()

                    # ~*~
                    progressDialog.Destroy()
                    # ~^~

                    # 6. Set parameters back to original values and close window
                    for i, setting in enumerate(
                            self.__module.visible_settings()):
                        setting.set_value(original_values[i])
                    self.Close(True)
예제 #4
0
    def validate_input_ranges(self):
        """Validates input ranges specified by the input widgets.

        This method uses CellProfiler's validate methods on settings to test
        whether the ranges specified on the UI are acceptable. When these do
        not validate, the user is shown an error message that lists all
        violations as well as the error message received back from CellProfiler.
        """
        # First, test whether selected parameters are valid
        message = ""
        for i, checkbox in enumerate(self.__check_box_list):
            if checkbox.IsChecked():
                setting = self.__module.setting(self.__parameters_list[i][1])
                input_size = self.get_parameter_input_size(setting)
                if input_size == 1:
                    widget_idx = self.__parameters_to_widgets_list[i][0]
                    lower_value = self.__lower_bound_spin_ctrl_list[
                        widget_idx].GetValue()
                    upper_value = self.__upper_bound_spin_ctrl_list[
                        widget_idx].GetValue()
                elif input_size == 2:
                    widget_idx = self.__parameters_to_widgets_list[i][0]
                    lower_value = (
                        self.__lower_bound_spin_ctrl_list[widget_idx].GetValue(
                        ),
                        self.__lower_bound_spin_ctrl_list[widget_idx +
                                                          1].GetValue(),
                    )
                    upper_value = (
                        self.__upper_bound_spin_ctrl_list[widget_idx].GetValue(
                        ),
                        self.__upper_bound_spin_ctrl_list[widget_idx +
                                                          1].GetValue(),
                    )

                old_value = setting.get_value()
                try:
                    setting.set_value(lower_value)
                    setting.test_valid(self.__pipeline)
                except cellprofiler_core.setting.ValidationError as instance:
                    message += ("'" + str(setting.get_text()) +
                                "': lower bound invalid, " + "\n\t" +
                                str(instance.message) + "\n")
                try:
                    setting.set_value(upper_value)
                    setting.test_valid(self.__pipeline)
                except cellprofiler_core.setting.ValidationError as instance:
                    message += ("'" + str(setting.get_text()) +
                                "': upper bound invalid, " + "\n\t" +
                                str(instance.message) + "\n")
                setting.set_value(old_value)

        # Second, if there are invalid parameters, tell the user
        if len(message) > 0:
            message = "Invalid sample settings:\n\n" + message
            dialog = wx.MessageDialog(
                self,
                message,
                caption="Sample settings error",
                style=wx.ICON_ERROR | wx.OK,
            )
            dialog.ShowModal()
            raise Exception(message)