Ejemplo n.º 1
0
def test_legend_was_not_called(kwargs, plot_calls, figure):
    plot_fitting(data=FIT_DATA,
                 func=FUNC,
                 a=A1,
                 title_name=TITLE_NAME,
                 legend=False)
    ax = figure.add_subplot.return_value
    ax.legend.assert_called_once_with(False)
Ejemplo n.º 2
0
def test_plot_unknown_a_type(mock_figure):
    with pytest.raises(
            PlottingError,
            match=("^3.4 has unmatching type. Can except only numpy arrays, "
                   "lists of numpy arrays and dictionaries.$"),
    ):
        plot_fitting(data=FIT_DATA,
                     func=FUNC,
                     a=3.4,
                     title_name=TITLE_NAME,
                     legend=False)
 def plot_fitting(self, func, data, a):  # pylint: disable=invalid-name
     """Create a fitting plot."""
     return plot_fitting(
         func=func,
         data=data,
         title_name=self.title,
         xlabel=self.xlabel,
         ylabel=self.ylabel,
         grid=self.grid,
         legend=self.legend,
         a=a,
         xmin=self.xmin,
         xmax=self.xmax,
     )
Ejemplo n.º 4
0
    def startup(self):
        """
        Construct and show the Toga application.

        Usually, you would add your application to a main content box.
        We then create a main window (with a name matching the app), and
        show the main window.
        """
        main_box = EddingtonBox(style=Pack(direction=COLUMN))
        main_box.add(HeaderBox())

        self.input_file_box = InputFileBox(
            on_choose_record=self.choose_records)
        self.input_file_box.on_input_file_change = self.reset_fitting_data
        self.input_file_box.on_csv_read = self.read_csv
        self.input_file_box.on_excel_read = self.read_excel
        self.input_file_box.on_select_excel_file = self.select_default_sheet
        main_box.add(self.input_file_box)

        self.data_columns_box = DataColumnsBox(
            on_columns_change=self.on_data_columns_change)
        self.data_columns_box.add(
            toga.Button("Explore",
                        on_press=self.explore,
                        style=Pack(padding_left=SMALL_PADDING)))
        main_box.add(self.data_columns_box)

        self.fitting_function_box = FittingFunctionBox(
            on_fitting_function_load=self.on_fitting_function_load)
        self.fitting_function_box.add(
            toga.Button(label="Fit", on_press=self.fit),
            toga.Button(label="Load module", on_press=self.load_module),
        )
        main_box.add(self.fitting_function_box)

        self.initial_guess_box = ParametersBox(
            on_parameters_change=self.reset_fitting_result)
        self.initial_guess_box.add(toga.Label(text="Initial Guess:"))
        main_box.add(self.initial_guess_box)

        self.plot_boxes = {}
        self.can_plot_map = {}
        self.add_plot_configuration_box(
            option_label="Data",
            button_label="Plot data",
            plot_method=lambda **kwargs: plot_data(
                data=self.data_columns_box.fitting_data, **kwargs),
            can_plot=self.can_plot_data,
            suffix="Data",
            has_legend=False,
        )
        self.add_plot_configuration_box(
            option_label="Initial guess",
            button_label="Plot initial guess",
            plot_method=lambda **kwargs: plot_fitting(
                func=self.fitting_function_box.fitting_function,
                data=self.data_columns_box.fitting_data,
                a=self.initial_guess_box.a0,
                **kwargs,
            ),
            suffix="Initial Guess",
            can_plot=self.can_plot_initial_guess,
        )
        self.add_plot_configuration_box(
            option_label="Fit",
            button_label="Plot fit",
            plot_method=lambda **kwargs: plot_fitting(
                func=self.fitting_function_box.fitting_function,
                data=self.data_columns_box.fitting_data,
                a=self.fitting_result.a,
                **kwargs,
            ),
            suffix="Fitting",
            can_plot=self.can_plot_fit,
        )
        self.add_plot_configuration_box(
            option_label="Residuals",
            button_label="Plot residuals",
            plot_method=lambda **kwargs: plot_residuals(
                func=self.fitting_function_box.fitting_function,
                data=self.data_columns_box.fitting_data,
                a=self.fitting_result.a,
                **kwargs,
            ),
            suffix="Residuals",
            can_plot=self.can_plot_fit,
            has_legend=False,
        )
        self.plot_options_container = toga.OptionContainer(style=Pack(flex=5))
        self.plot_options_container.app = self
        for label, box in self.plot_boxes.items():
            self.plot_options_container.add(label, box)

        main_box.add(self.plot_options_container)

        self.output_box = OutputBox(on_save_output=self.on_save_output)
        main_box.add(self.output_box)

        main_box.add(FooterBox())

        self.main_window = toga.MainWindow(title=self.formal_name,
                                           size=MAIN_WINDOW_SIZE)
        self.main_window.content = main_box

        self.check_latest_version()
        self.commands.add(
            # File group
            toga.Command(
                self.input_file_box.select_file,
                label="Upload data file",
                shortcut=toga.Key.MOD_1 + "o",
                group=toga.Group.FILE,
                order=1,
            ),
            toga.Command(
                self.load_module,
                label="Load module",
                shortcut=toga.Key.MOD_1 + "m",
                group=toga.Group.FILE,
                order=2,
            ),
            toga.Command(
                self.choose_records,
                label="Choose records",
                shortcut=toga.Key.MOD_1 + "d",
                group=toga.Group.FILE,
                order=3,
            ),
            toga.Command(
                self.output_box.choose_output_dir,
                label="Choose output directory",
                shortcut=toga.Key.MOD_1 + "u",
                group=toga.Group.FILE,
                order=4,
            ),
            toga.Command(
                self.on_save_output,
                label="Save plots and results",
                shortcut=toga.Key.MOD_1 + "s",
                group=toga.Group.FILE,
                order=5,
            ),
            toga.Command(
                lambda widget: self.open_latest_version_webpage(),
                label="Install Eddington-GUI latest version",
                group=toga.Group.FILE,
                order=6,
                enabled=self.has_newer_version,
            ),
            toga.Command(
                self.fit,
                label="Fit result",
                shortcut=toga.Key.MOD_1 + "e",
                group=PLOT_GROUP,
            ),
            toga.Command(
                lambda _: self.set_font_size(FontSize.SMALL),
                "Set small font size",
                group=toga.Group.VIEW,
                order=FontSize.SMALL.value,
            ),
            toga.Command(
                lambda _: self.set_font_size(FontSize.MEDIUM),
                "Set medium font size",
                group=toga.Group.VIEW,
                order=FontSize.MEDIUM.value,
            ),
            toga.Command(
                lambda _: self.set_font_size(FontSize.LARGE),
                "Set large font size",
                group=toga.Group.VIEW,
                order=FontSize.LARGE.value,
            ),
        )
        self.set_font_size(FontSize.DEFAULT)
        if not has_matplotlib:
            self.main_window.info_dialog(
                "Error",
                (f"Error loading matplotlib.\nPlease go to {self.faq_url}"
                 " and see how to solve this problem"),
            )
            webbrowser.open(self.faq_url)

        self.main_window.show()

        if self.has_newer_version and self.main_window.question_dialog(
                "Update is available",
            (f"A new version of {self.formal_name} is available! "
             "would you like to download it?"),
        ):
            self.open_latest_version_webpage()
Ejemplo n.º 5
0
def test_legend_was_called(kwargs, plot_calls, figure):
    plot_fitting(data=FIT_DATA, func=FUNC, title_name=TITLE_NAME, **kwargs)
    ax = figure.add_subplot.return_value
    ax.legend.assert_called_once()
Ejemplo n.º 6
0
def test_plot_fitting_without_boundaries(kwargs, plot_calls, figure):
    plot_fitting(data=FIT_DATA, func=FUNC, title_name=TITLE_NAME, **kwargs)
    ax = figure.add_subplot.return_value
    assert_calls(ax.plot, plot_calls, rel=EPSILON)