OxIonics / ionics_fits

Small python fitting library with an emphasis on Atomic Molecular and Optical Physics
Apache License 2.0
0 stars 0 forks source link

Introduce heuristic attribute and change parameter initialisation #45

Closed AUTProgram closed 1 year ago

AUTProgram commented 1 year ago

This PR modifies the way in which initial values for parameters are obtained. An attribute heuristic has been added to the ModelParameter class, which is used if both of fixed_to and initialised_to are None. There is now only one method to get the initial guess for parameters during fitting, called get_initial_value. This method may return None only before estimate_parameters has been called. If get_initial_value returns None for any parameter after pre-fit estimation has been completed, the fitter raises an error.

As part of this PR, bound checking for fixed parameters is added. Functionality is further changed such that if the initial value is outside bounds, an error is thrown rather than silently clipping the parameter.

AUTProgram commented 1 year ago

I have run unit tests, all work except one that throws

message = 'Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.', category = None

    def warn_external(message, category=None):
        """
        `warnings.warn` wrapper that sets *stacklevel* to "outside Matplotlib".

        The original emitter of the warning can be obtained by patching this
        function back to `warnings.warn`, i.e. ``_api.warn_external =
        warnings.warn`` (or ``functools.partial(warnings.warn, stacklevel=2)``,
        etc.).
        """
        frame = sys._getframe()
        for stacklevel in itertools.count(1):  # lgtm[py/unused-loop-variable]
            if frame is None:
                # when called in embedded context may hit frame is None
                break
            if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.(?!tests\.))",
                            # Work around sphinx-gallery not setting __name__.
                            frame.f_globals.get("__name__", "")):
                break
            frame = frame.f_back
>       warnings.warn(message, category, stacklevel)
E       UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

..\..\AppData\Local\pypoetry\Cache\virtualenvs\ionics-fits-fyXcmhes-py3.8\lib\site-packages\matplotlib\_api\__init__.py:363: UserWarning
================================================================================================================================================= short test summary info ==================================================================================================================================================
FAILED test/test_polynomial.py::test_power_n - UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

Not sure if this is caused by the configuration of the pc I am using.

AUTProgram commented 1 year ago

All tests finish correctly

AUTProgram commented 1 year ago

@hartytp Let me know if you are happy with the docstrings now.

hartytp commented 1 year ago

@hartytp Let me know if you are happy with the docstrings now.

which one?

AUTProgram commented 1 year ago

which one?

Those for get_initial_value and estimate_parameters mainly. I will have to copy the docstring of estimate_parameters for each model.

AUTProgram commented 1 year ago

I believe this is ready to be merged

hartytp commented 1 year ago

Can you sort the test failure out though https://github.com/OxIonics/ionics_fits/actions/runs/3429613116/jobs/5715467301#step:8:30

hartytp commented 1 year ago

🎆