perwin / pyimfit

Python wrapper for Imfit (https://github.com/perwin/imfit)
GNU General Public License v3.0
33 stars 4 forks source link

Return fit parameters as dictionary #2

Closed danjampro closed 1 year ago

danjampro commented 3 years ago

At the moment, fit parameters obtained via getRawParameters are returned as a np.array object. This is not particularly convenient as the names of the parameters aren't necessarily known, and I can't see nice way of mapping array indices to parameter names.

I would suggest that getRawParameters should return a (nested) dictionary, like:

{component_1: 
    {par_name1: val1, par_name2: val2},
component_2: 
    {par_name1: val1, par_name2: val2},
...}

Apologies if this functionality already exists.

perwin commented 3 years ago

That's a good suggestion. It might be better to add a new function (since it's useful to have the existing function return a pure numpy array in certain circumstances); let me look into what might be the best solution.

perwin commented 3 years ago

Sorry for the delay in working on this. I'm currently thinking it would make sense to have two new "parameter return" methods (in addition to getRawParameters):

  1. A method which returns a list of OrderedDict entries, one for each function set; internally, each element of the list would resemble your suggestion (the dicts for each component would also be OrderedDicts): [ {component_1: {par_name1: val1, par_name2: val2}, component_2: {par_name1: val1, par_name2: val2}, ...}, {component_1: {par_name1: val1, par_name2: val2}, ... ]

  2. A method which returns ModelDescription object.

How does that sound?

danjampro commented 3 years ago

Sorry for the delay in working on this. I'm currently thinking it would make sense to have two new "parameter return" methods (in addition to getRawParameters):

  1. A method which returns a list of OrderedDict entries, one for each function set; internally, each element of the list would resemble your suggestion (the dicts for each component would also be OrderedDicts): [ {component_1: {par_name1: val1, par_name2: val2}, component_2: {par_name1: val1, par_name2: val2}, ...}, {component_1: {par_name1: val1, par_name2: val2}, ... ]
  2. A method which returns ModelDescription object.

How does that sound?

@perwin Sounds great! Thanks for working on this.

perwin commented 3 years ago

Here's my current idea for the dict output. The idea is that this is a format which can also be used to define a new ModelDescription object; it also needs to handle the possibility of multiple function sets, which makes it more complicated than your initial suggestion.

{ 'options': optionsDict or None,  'function_sets': list of function-set dicts }
where
    optionsDict = {'GAIN':float, 'ORIGINAL_SKY': float, etc.}
and individual function-set dicts look like this (one per function set in the model):
    { 'name': str or None   [default], 'X0': x0-value, 'Y0': y0-value, 'function-list': list of function dicts }

Function dicts look like this:
   {  'name': str    ["Exponential", "Sersic", etc.], 'label' : str or None [default],  'parameters' : parameters-dict }

Finally, parameter dicts are OrderedDicts with str: float pairs, e.g.
   { 'PA': 45.0, 'ell': 0.23, 'I_0': 103.2, 'h': 22.1}

A very simple example, for a model with a single Gaussian component (ignoring any image-description parameters like GAIN, etc.):

bestfit_model = { 'function_sets': [{'X0': 1023.2, 'Y0': 985.4, 
                    'function_list': [{'name': 'Gaussian", 
                        'parameters': {'PA': 0.0, 'ell': 0.2, 'I_0': 1.0, 'sigma': 12.3}}] }] }

The parameter dicts might be modified to contain optional limits/'fixed' specifications, or perhaps this would be better handled by an optional 'parameter_limits' dict as part of the function dict.

How does that look to you?

perwin commented 2 years ago

There is (finally) a new release (0.11.1) incorporating the return-fitted-model-as-dict functionality (including the ability to define new ModelDescription objects using dicts). You should be able to install it via pip.

perwin commented 1 year ago

I'm going to close this issue, but feel free to re-open it if there are problems with the dict return functionality.