annotate fit.py @ 69:fba792d5f83b draft

planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
author rv43
date Fri, 10 Mar 2023 16:02:04 +0000
parents f31ef7bfb430
children 1cf15b61cd83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1 #!/usr/bin/env python3
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
2
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
3 # -*- coding: utf-8 -*-
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
4 """
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
5 Created on Mon Dec 6 15:36:22 2021
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
6
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
7 @author: rv43
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
8 """
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
9
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
10 import logging
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
11
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
12 from asteval import Interpreter, get_ast_names
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
13 from copy import deepcopy
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
14 from lmfit import Model, Parameters
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
15 from lmfit.model import ModelResult
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
16 from lmfit.models import ConstantModel, LinearModel, QuadraticModel, PolynomialModel,\
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
17 ExponentialModel, StepModel, RectangleModel, ExpressionModel, GaussianModel,\
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
18 LorentzianModel
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
19 import numpy as np
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
20 from os import cpu_count, getpid, listdir, mkdir, path
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
21 from re import compile, sub
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
22 from shutil import rmtree
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
23 from sympy import diff, simplify
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
24 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
25 from joblib import Parallel, delayed
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
26 have_joblib = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
27 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
28 have_joblib = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
29 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
30 import xarray as xr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
31 have_xarray = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
32 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
33 have_xarray = False
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
34
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
35 from .general import illegal_value, is_int, is_dict_series, is_index, index_nearest, \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
36 almost_equal, quick_plot #, eval_expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
37 #from sys import path as syspath
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
38 #syspath.append(f'/nfs/chess/user/rv43/msnctools/msnctools')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
39 #from general import illegal_value, is_int, is_dict_series, is_index, index_nearest, \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
40 # almost_equal, quick_plot #, eval_expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
41
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
42 from sys import float_info
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
43 float_min = float_info.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
44 float_max = float_info.max
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
45
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
46 # sigma = fwhm_factor*fwhm
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
47 fwhm_factor = {
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
48 'gaussian': f'fwhm/(2*sqrt(2*log(2)))',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
49 'lorentzian': f'0.5*fwhm',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
50 'splitlorentzian': f'0.5*fwhm', # sigma = sigma_r
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
51 'voight': f'0.2776*fwhm', # sigma = gamma
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
52 'pseudovoight': f'0.5*fwhm'} # fraction = 0.5
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
53
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
54 # amplitude = height_factor*height*fwhm
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
55 height_factor = {
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
56 'gaussian': f'height*fwhm*0.5*sqrt(pi/log(2))',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
57 'lorentzian': f'height*fwhm*0.5*pi',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
58 'splitlorentzian': f'height*fwhm*0.5*pi', # sigma = sigma_r
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
59 'voight': f'3.334*height*fwhm', # sigma = gamma
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
60 'pseudovoight': f'1.268*height*fwhm'} # fraction = 0.5
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
61
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
62 class Fit:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
63 """Wrapper class for lmfit
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
64 """
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
65 def __init__(self, y, x=None, models=None, normalize=True, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
66 if not isinstance(normalize, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
67 raise ValueError(f'Invalid parameter normalize ({normalize})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
68 self._mask = None
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
69 self._model = None
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
70 self._norm = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
71 self._normalized = False
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
72 self._parameters = Parameters()
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
73 self._parameter_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
74 self._parameter_norms = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
75 self._linear_parameters = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
76 self._nonlinear_parameters = []
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
77 self._result = None
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
78 self._try_linear_fit = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
79 self._y = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
80 self._y_norm = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
81 self._y_range = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
82 if 'try_linear_fit' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
83 try_linear_fit = kwargs.pop('try_linear_fit')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
84 if not isinstance(try_linear_fit, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
85 illegal_value(try_linear_fit, 'try_linear_fit', 'Fit.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
86 self._try_linear_fit = try_linear_fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
87 if y is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
88 if isinstance(y, (tuple, list, np.ndarray)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
89 self._x = np.asarray(x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
90 elif have_xarray and isinstance(y, xr.DataArray):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
91 if x is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
92 logging.warning('Ignoring superfluous input x ({x}) in Fit.__init__')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
93 if y.ndim != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
94 illegal_value(y.ndim, 'DataArray dimensions', 'Fit:__init__', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
95 self._x = np.asarray(y[y.dims[0]])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
96 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
97 illegal_value(y, 'y', 'Fit:__init__', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
98 self._y = y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
99 if self._x.ndim != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
100 raise ValueError(f'Invalid dimension for input x ({self._x.ndim})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
101 if self._x.size != self._y.size:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
102 raise ValueError(f'Inconsistent x and y dimensions ({self._x.size} vs '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
103 f'{self._y.size})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
104 if 'mask' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
105 self._mask = kwargs.pop('mask')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
106 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
107 y_min = float(self._y.min())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
108 self._y_range = float(self._y.max())-y_min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
109 if normalize and self._y_range > 0.0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
110 self._norm = (y_min, self._y_range)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
111 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
112 self._mask = np.asarray(self._mask).astype(bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
113 if self._x.size != self._mask.size:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
114 raise ValueError(f'Inconsistent x and mask dimensions ({self._x.size} vs '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
115 f'{self._mask.size})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
116 y_masked = np.asarray(self._y)[~self._mask]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
117 y_min = float(y_masked.min())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
118 self._y_range = float(y_masked.max())-y_min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
119 if normalize and self._y_range > 0.0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
120 if normalize and self._y_range > 0.0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
121 self._norm = (y_min, self._y_range)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
122 if models is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
123 if callable(models) or isinstance(models, str):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
124 kwargs = self.add_model(models, **kwargs)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
125 elif isinstance(models, (tuple, list)):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
126 for model in models:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
127 kwargs = self.add_model(model, **kwargs)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
128 self.fit(**kwargs)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
129
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
130 @classmethod
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
131 def fit_data(cls, y, models, x=None, normalize=True, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
132 return(cls(y, x=x, models=models, normalize=normalize, **kwargs))
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
133
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
134 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
135 def best_errors(self):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
136 if self._result is None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
137 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
138 return({name:self._result.params[name].stderr for name in sorted(self._result.params)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
139 if name != 'tmp_normalization_offset_c'})
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
140
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
141 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
142 def best_fit(self):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
143 if self._result is None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
144 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
145 return(self._result.best_fit)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
146
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
147 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
148 def best_parameters(self):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
149 if self._result is None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
150 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
151 parameters = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
152 for name in sorted(self._result.params):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
153 if name != 'tmp_normalization_offset_c':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
154 par = self._result.params[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
155 parameters[name] = {'value': par.value, 'error': par.stderr,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
156 'init_value': par.init_value, 'min': par.min, 'max': par.max,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
157 'vary': par.vary, 'expr': par.expr}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
158 return(parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
159
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
160 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
161 def best_results(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
162 """Convert the input data array to a data set and add the fit results.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
163 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
164 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
165 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
166 if isinstance(self._y, xr.DataArray):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
167 best_results = self._y.to_dataset()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
168 dims = self._y.dims
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
169 fit_name = f'{self._y.name}_fit'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
170 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
171 coords = {'x': (['x'], self._x)}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
172 dims = ('x')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
173 best_results = xr.Dataset(coords=coords)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
174 best_results['y'] = (dims, self._y)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
175 fit_name = 'y_fit'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
176 best_results[fit_name] = (dims, self.best_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
177 if self._mask is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
178 best_results['mask'] = self._mask
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
179 best_results.coords['par_names'] = ('peak', [name for name in self.best_values.keys()])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
180 best_results['best_values'] = (['par_names'], [v for v in self.best_values.values()])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
181 best_results['best_errors'] = (['par_names'], [v for v in self.best_errors.values()])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
182 best_results.attrs['components'] = self.components
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
183 return(best_results)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
184
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
185 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
186 def best_values(self):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
187 if self._result is None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
188 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
189 return({name:self._result.params[name].value for name in sorted(self._result.params)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
190 if name != 'tmp_normalization_offset_c'})
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
191
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
192 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
193 def chisqr(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
194 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
195 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
196 return(self._result.chisqr)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
197
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
198 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
199 def components(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
200 components = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
201 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
202 logging.warning('Unable to collect components in Fit.components')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
203 return(components)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
204 for component in self._result.components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
205 if 'tmp_normalization_offset_c' in component.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
206 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
207 parameters = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
208 for name in component.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
209 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
210 parameters[name] = {'free': par.vary, 'value': self._result.params[name].value}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
211 if par.expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
212 parameters[name]['expr'] = par.expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
213 expr = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
214 if isinstance(component, ExpressionModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
215 name = component._name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
216 if name[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
217 name = name[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
218 expr = component.expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
219 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
220 prefix = component.prefix
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
221 if len(prefix):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
222 if prefix[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
223 prefix = prefix[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
224 name = f'{prefix} ({component._name})'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
225 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
226 name = f'{component._name}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
227 if expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
228 components[name] = {'parameters': parameters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
229 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
230 components[name] = {'expr': expr, 'parameters': parameters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
231 return(components)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
232
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
233 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
234 def covar(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
235 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
236 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
237 return(self._result.covar)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
238
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
239 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
240 def init_parameters(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
241 if self._result is None or self._result.init_params is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
242 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
243 parameters = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
244 for name in sorted(self._result.init_params):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
245 if name != 'tmp_normalization_offset_c':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
246 par = self._result.init_params[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
247 parameters[name] = {'value': par.value, 'min': par.min, 'max': par.max,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
248 'vary': par.vary, 'expr': par.expr}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
249 return(parameters)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
250
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
251 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
252 def init_values(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
253 if self._result is None or self._result.init_params is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
254 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
255 return({name:self._result.init_params[name].value for name in
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
256 sorted(self._result.init_params) if name != 'tmp_normalization_offset_c'})
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
257
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
258 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
259 def normalization_offset(self):
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
260 if self._result is None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
261 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
262 if self._norm is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
263 return(0.0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
264 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
265 if self._result.init_params is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
266 normalization_offset = self._result.init_params['tmp_normalization_offset_c']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
267 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
268 normalization_offset = self._result.params['tmp_normalization_offset_c']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
269 return(normalization_offset)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
270
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
271 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
272 def num_func_eval(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
273 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
274 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
275 return(self._result.nfev)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
276
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
277 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
278 def parameters(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
279 return({name:{'min': par.min, 'max': par.max, 'vary': par.vary, 'expr': par.expr}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
280 for name, par in self._parameters.items() if name != 'tmp_normalization_offset_c'})
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
281
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
282 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
283 def redchi(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
284 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
285 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
286 return(self._result.redchi)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
287
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
288 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
289 def residual(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
290 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
291 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
292 return(self._result.residual)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
293
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
294 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
295 def success(self):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
296 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
297 return(None)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
298 if not self._result.success:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
299 # print(f'ier = {self._result.ier}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
300 # print(f'lmdif_message = {self._result.lmdif_message}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
301 # print(f'message = {self._result.message}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
302 # print(f'nfev = {self._result.nfev}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
303 # print(f'redchi = {self._result.redchi}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
304 # print(f'success = {self._result.success}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
305 if self._result.ier == 0 or self._result.ier == 5:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
306 logging.warning(f'ier = {self._result.ier}: {self._result.message}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
307 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
308 logging.warning(f'ier = {self._result.ier}: {self._result.message}')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
309 return(True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
310 # self.print_fit_report()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
311 # self.plot()
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
312 return(self._result.success)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
313
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
314 @property
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
315 def var_names(self):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
316 """Intended to be used with covar
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
317 """
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
318 if self._result is None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
319 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
320 return(getattr(self._result, 'var_names', None))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
321
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
322 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
323 def x(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
324 return(self._x)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
325
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
326 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
327 def y(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
328 return(self._y)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
329
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
330 def print_fit_report(self, result=None, show_correl=False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
331 if result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
332 result = self._result
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
333 if result is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
334 print(result.fit_report(show_correl=show_correl))
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
335
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
336 def add_parameter(self, **parameter):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
337 if not isinstance(parameter, dict):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
338 raise ValueError(f'Invalid parameter ({parameter})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
339 if parameter.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
340 raise KeyError(f'Illegal "expr" key in parameter {parameter}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
341 name = parameter['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
342 if not isinstance(name, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
343 raise ValueError(f'Illegal "name" value ({name}) in parameter {parameter}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
344 if parameter.get('norm') is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
345 self._parameter_norms[name] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
346 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
347 norm = parameter.pop('norm')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
348 if self._norm is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
349 logging.warning(f'Ignoring norm in parameter {name} in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
350 f'Fit.add_parameter (normalization is turned off)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
351 self._parameter_norms[name] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
352 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
353 if not isinstance(norm, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
354 raise ValueError(f'Illegal "norm" value ({norm}) in parameter {parameter}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
355 self._parameter_norms[name] = norm
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
356 vary = parameter.get('vary')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
357 if vary is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
358 if not isinstance(vary, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
359 raise ValueError(f'Illegal "vary" value ({vary}) in parameter {parameter}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
360 if not vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
361 if 'min' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
362 logging.warning(f'Ignoring min in parameter {name} in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
363 f'Fit.add_parameter (vary = {vary})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
364 parameter.pop('min')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
365 if 'max' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
366 logging.warning(f'Ignoring max in parameter {name} in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
367 f'Fit.add_parameter (vary = {vary})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
368 parameter.pop('max')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
369 if self._norm is not None and name not in self._parameter_norms:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
370 raise ValueError(f'Missing parameter normalization type for paremeter {name}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
371 self._parameters.add(**parameter)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
372
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
373 def add_model(self, model, prefix=None, parameters=None, parameter_norms=None, **kwargs):
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
374 # Create the new model
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
375 # print(f'at start add_model:\nself._parameters:\n{self._parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
376 # print(f'at start add_model: kwargs = {kwargs}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
377 # print(f'parameters = {parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
378 # print(f'parameter_norms = {parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
379 # if len(self._parameters.keys()):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
380 # print('\nAt start adding model:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
381 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
382 # print(f'parameter_norms:\n{self._parameter_norms}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
383 if prefix is not None and not isinstance(prefix, str):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
384 logging.warning('Ignoring illegal prefix: {model} {type(model)}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
385 prefix = None
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
386 if prefix is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
387 pprefix = ''
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
388 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
389 pprefix = prefix
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
390 if parameters is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
391 if isinstance(parameters, dict):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
392 parameters = (parameters, )
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
393 elif not is_dict_series(parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
394 illegal_value(parameters, 'parameters', 'Fit.add_model', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
395 parameters = deepcopy(parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
396 if parameter_norms is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
397 if isinstance(parameter_norms, dict):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
398 parameter_norms = (parameter_norms, )
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
399 if not is_dict_series(parameter_norms):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
400 illegal_value(parameter_norms, 'parameter_norms', 'Fit.add_model', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
401 new_parameter_norms = {}
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
402 if callable(model):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
403 # Linear fit not yet implemented for callable models
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
404 self._try_linear_fit = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
405 if parameter_norms is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
406 if parameters is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
407 raise ValueError('Either "parameters" or "parameter_norms" is required in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
408 f'{model}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
409 for par in parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
410 name = par['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
411 if not isinstance(name, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
412 raise ValueError(f'Illegal "name" value ({name}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
413 if par.get('norm') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
414 norm = par.pop('norm')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
415 if not isinstance(norm, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
416 raise ValueError(f'Illegal "norm" value ({norm}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
417 new_parameter_norms[f'{pprefix}{name}'] = norm
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
418 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
419 for par in parameter_norms:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
420 name = par['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
421 if not isinstance(name, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
422 raise ValueError(f'Illegal "name" value ({name}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
423 norm = par.get('norm')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
424 if norm is None or not isinstance(norm, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
425 raise ValueError(f'Illegal "norm" value ({norm}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
426 new_parameter_norms[f'{pprefix}{name}'] = norm
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
427 if parameters is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
428 for par in parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
429 if par.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
430 raise KeyError(f'Illegal "expr" key ({par.get("expr")}) in parameter '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
431 f'{name} for a callable model {model}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
432 name = par['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
433 if not isinstance(name, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
434 raise ValueError(f'Illegal "name" value ({name}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
435 # RV FIX callable model will need partial deriv functions for any linear pars to get the linearized matrix, so for now skip linear solution option
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
436 newmodel = Model(model, prefix=prefix)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
437 elif isinstance(model, str):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
438 if model == 'constant': # Par: c
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
439 newmodel = ConstantModel(prefix=prefix)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
440 new_parameter_norms[f'{pprefix}c'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
441 self._linear_parameters.append(f'{pprefix}c')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
442 elif model == 'linear': # Par: slope, intercept
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
443 newmodel = LinearModel(prefix=prefix)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
444 new_parameter_norms[f'{pprefix}slope'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
445 new_parameter_norms[f'{pprefix}intercept'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
446 self._linear_parameters.append(f'{pprefix}slope')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
447 self._linear_parameters.append(f'{pprefix}intercept')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
448 elif model == 'quadratic': # Par: a, b, c
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
449 newmodel = QuadraticModel(prefix=prefix)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
450 new_parameter_norms[f'{pprefix}a'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
451 new_parameter_norms[f'{pprefix}b'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
452 new_parameter_norms[f'{pprefix}c'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
453 self._linear_parameters.append(f'{pprefix}a')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
454 self._linear_parameters.append(f'{pprefix}b')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
455 self._linear_parameters.append(f'{pprefix}c')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
456 elif model == 'gaussian': # Par: amplitude, center, sigma (fwhm, height)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
457 newmodel = GaussianModel(prefix=prefix)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
458 new_parameter_norms[f'{pprefix}amplitude'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
459 new_parameter_norms[f'{pprefix}center'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
460 new_parameter_norms[f'{pprefix}sigma'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
461 self._linear_parameters.append(f'{pprefix}amplitude')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
462 self._nonlinear_parameters.append(f'{pprefix}center')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
463 self._nonlinear_parameters.append(f'{pprefix}sigma')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
464 # parameter norms for height and fwhm are needed to get correct errors
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
465 new_parameter_norms[f'{pprefix}height'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
466 new_parameter_norms[f'{pprefix}fwhm'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
467 elif model == 'lorentzian': # Par: amplitude, center, sigma (fwhm, height)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
468 newmodel = LorentzianModel(prefix=prefix)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
469 new_parameter_norms[f'{pprefix}amplitude'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
470 new_parameter_norms[f'{pprefix}center'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
471 new_parameter_norms[f'{pprefix}sigma'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
472 self._linear_parameters.append(f'{pprefix}amplitude')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
473 self._nonlinear_parameters.append(f'{pprefix}center')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
474 self._nonlinear_parameters.append(f'{pprefix}sigma')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
475 # parameter norms for height and fwhm are needed to get correct errors
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
476 new_parameter_norms[f'{pprefix}height'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
477 new_parameter_norms[f'{pprefix}fwhm'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
478 elif model == 'exponential': # Par: amplitude, decay
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
479 newmodel = ExponentialModel(prefix=prefix)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
480 new_parameter_norms[f'{pprefix}amplitude'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
481 new_parameter_norms[f'{pprefix}decay'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
482 self._linear_parameters.append(f'{pprefix}amplitude')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
483 self._nonlinear_parameters.append(f'{pprefix}decay')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
484 elif model == 'step': # Par: amplitude, center, sigma
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
485 form = kwargs.get('form')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
486 if form is not None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
487 kwargs.pop('form')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
488 if form is None or form not in ('linear', 'atan', 'arctan', 'erf', 'logistic'):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
489 raise ValueError(f'Invalid parameter form for build-in step model ({form})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
490 newmodel = StepModel(prefix=prefix, form=form)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
491 new_parameter_norms[f'{pprefix}amplitude'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
492 new_parameter_norms[f'{pprefix}center'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
493 new_parameter_norms[f'{pprefix}sigma'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
494 self._linear_parameters.append(f'{pprefix}amplitude')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
495 self._nonlinear_parameters.append(f'{pprefix}center')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
496 self._nonlinear_parameters.append(f'{pprefix}sigma')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
497 elif model == 'rectangle': # Par: amplitude, center1, center2, sigma1, sigma2
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
498 form = kwargs.get('form')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
499 if form is not None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
500 kwargs.pop('form')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
501 if form is None or form not in ('linear', 'atan', 'arctan', 'erf', 'logistic'):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
502 raise ValueError('Invalid parameter form for build-in rectangle model '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
503 f'({form})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
504 newmodel = RectangleModel(prefix=prefix, form=form)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
505 new_parameter_norms[f'{pprefix}amplitude'] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
506 new_parameter_norms[f'{pprefix}center1'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
507 new_parameter_norms[f'{pprefix}center2'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
508 new_parameter_norms[f'{pprefix}sigma1'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
509 new_parameter_norms[f'{pprefix}sigma2'] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
510 self._linear_parameters.append(f'{pprefix}amplitude')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
511 self._nonlinear_parameters.append(f'{pprefix}center1')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
512 self._nonlinear_parameters.append(f'{pprefix}center2')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
513 self._nonlinear_parameters.append(f'{pprefix}sigma1')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
514 self._nonlinear_parameters.append(f'{pprefix}sigma2')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
515 elif model == 'expression': # Par: by expression
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
516 expr = kwargs['expr']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
517 if not isinstance(expr, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
518 raise ValueError(f'Illegal "expr" value ({expr}) in {model}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
519 kwargs.pop('expr')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
520 if parameter_norms is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
521 logging.warning('Ignoring parameter_norms (normalization determined from '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
522 'linearity)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
523 if parameters is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
524 for par in parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
525 if par.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
526 raise KeyError(f'Illegal "expr" key ({par.get("expr")}) in parameter '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
527 f'({par}) for an expression model')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
528 if par.get('norm') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
529 logging.warning(f'Ignoring "norm" key in parameter ({par}) '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
530 '(normalization determined from linearity)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
531 par.pop('norm')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
532 name = par['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
533 if not isinstance(name, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
534 raise ValueError(f'Illegal "name" value ({name}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
535 ast = Interpreter()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
536 expr_parameters = [name for name in get_ast_names(ast.parse(expr))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
537 if name != 'x' and name not in self._parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
538 and name not in ast.symtable]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
539 # print(f'\nexpr_parameters: {expr_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
540 # print(f'expr = {expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
541 if prefix is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
542 newmodel = ExpressionModel(expr=expr)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
543 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
544 for name in expr_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
545 expr = sub(rf'\b{name}\b', f'{prefix}{name}', expr)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
546 expr_parameters = [f'{prefix}{name}' for name in expr_parameters]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
547 # print(f'\nexpr_parameters: {expr_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
548 # print(f'expr = {expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
549 newmodel = ExpressionModel(expr=expr, name=name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
550 # print(f'\nnewmodel = {newmodel.__dict__}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
551 # print(f'params_names = {newmodel._param_names}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
552 # print(f'params_names = {newmodel.param_names}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
553 # Remove already existing names
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
554 for name in newmodel.param_names.copy():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
555 if name not in expr_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
556 newmodel._func_allargs.remove(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
557 newmodel._param_names.remove(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
558 # print(f'params_names = {newmodel._param_names}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
559 # print(f'params_names = {newmodel.param_names}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
560 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
561 raise ValueError(f'Unknown build-in fit model ({model})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
562 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
563 illegal_value(model, 'model', 'Fit.add_model', raise_error=True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
564
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
565 # Add the new model to the current one
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
566 # print('\nBefore adding model:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
567 # print(f'\nnewmodel = {newmodel.__dict__}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
568 # if len(self._parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
569 # self._parameters.pretty_print()
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
570 if self._model is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
571 self._model = newmodel
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
572 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
573 self._model += newmodel
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
574 new_parameters = newmodel.make_params()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
575 self._parameters += new_parameters
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
576 # print('\nAfter adding model:')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
577 # print(f'\nnewmodel = {newmodel.__dict__}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
578 # print(f'\nnew_parameters = {new_parameters}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
579 # self._parameters.pretty_print()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
580
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
581 # Check linearity of expression model paremeters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
582 if isinstance(newmodel, ExpressionModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
583 for name in newmodel.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
584 if not diff(newmodel.expr, name, name):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
585 if name not in self._linear_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
586 self._linear_parameters.append(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
587 new_parameter_norms[name] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
588 # print(f'\nADDING {name} TO LINEAR')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
589 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
590 if name not in self._nonlinear_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
591 self._nonlinear_parameters.append(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
592 new_parameter_norms[name] = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
593 # print(f'\nADDING {name} TO NONLINEAR')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
594 # print(f'new_parameter_norms:\n{new_parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
595
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
596 # Scale the default initial model parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
597 if self._norm is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
598 for name, norm in new_parameter_norms.copy().items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
599 par = self._parameters.get(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
600 if par is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
601 new_parameter_norms.pop(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
602 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
603 if par.expr is None and norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
604 value = par.value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
605 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
606 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
607 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
608 _min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
609 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
610 _max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
611 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
612 # print('\nAfter norm defaults:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
613 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
614 # print(f'parameters:\n{parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
615 # print(f'all_parameters:\n{list(self.parameters)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
616 # print(f'new_parameter_norms:\n{new_parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
617 # print(f'parameter_norms:\n{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
618
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
619 # Initialize the model parameters from parameters
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
620 if prefix is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
621 prefix = ""
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
622 if parameters is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
623 for parameter in parameters:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
624 name = parameter['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
625 if not isinstance(name, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
626 raise ValueError(f'Illegal "name" value ({name}) in input parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
627 if name not in new_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
628 name = prefix+name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
629 parameter['name'] = name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
630 if name not in new_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
631 logging.warning(f'Ignoring superfluous parameter info for {name}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
632 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
633 if name in self._parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
634 parameter.pop('name')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
635 if 'norm' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
636 if not isinstance(parameter['norm'], bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
637 illegal_value(parameter['norm'], 'norm', 'Fit.add_model',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
638 raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
639 new_parameter_norms[name] = parameter['norm']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
640 parameter.pop('norm')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
641 if parameter.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
642 if 'value' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
643 logging.warning(f'Ignoring value in parameter {name} '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
644 f'(set by expression: {parameter["expr"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
645 parameter.pop('value')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
646 if 'vary' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
647 logging.warning(f'Ignoring vary in parameter {name} '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
648 f'(set by expression: {parameter["expr"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
649 parameter.pop('vary')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
650 if 'min' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
651 logging.warning(f'Ignoring min in parameter {name} '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
652 f'(set by expression: {parameter["expr"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
653 parameter.pop('min')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
654 if 'max' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
655 logging.warning(f'Ignoring max in parameter {name} '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
656 f'(set by expression: {parameter["expr"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
657 parameter.pop('max')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
658 if 'vary' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
659 if not isinstance(parameter['vary'], bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
660 illegal_value(parameter['vary'], 'vary', 'Fit.add_model',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
661 raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
662 if not parameter['vary']:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
663 if 'min' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
664 logging.warning(f'Ignoring min in parameter {name} in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
665 f'Fit.add_model (vary = {parameter["vary"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
666 parameter.pop('min')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
667 if 'max' in parameter:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
668 logging.warning(f'Ignoring max in parameter {name} in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
669 f'Fit.add_model (vary = {parameter["vary"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
670 parameter.pop('max')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
671 self._parameters[name].set(**parameter)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
672 parameter['name'] = name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
673 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
674 illegal_value(parameter, 'parameter name', 'Fit.model', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
675 self._parameter_norms = {**self._parameter_norms, **new_parameter_norms}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
676 # print('\nAfter parameter init:')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
677 # self._parameters.pretty_print()
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
678 # print(f'parameters:\n{parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
679 # print(f'new_parameter_norms:\n{new_parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
680 # print(f'parameter_norms:\n{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
681 # print(f'kwargs:\n{kwargs}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
682
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
683 # Initialize the model parameters from kwargs
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
684 for name, value in {**kwargs}.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
685 full_name = f'{pprefix}{name}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
686 if full_name in new_parameter_norms and isinstance(value, (int, float)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
687 kwargs.pop(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
688 if self._parameters[full_name].expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
689 self._parameters[full_name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
690 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
691 logging.warning(f'Ignoring parameter {name} in Fit.fit (set by expression: '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
692 f'{self._parameters[full_name].expr})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
693 # print('\nAfter kwargs init:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
694 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
695 # print(f'parameter_norms:\n{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
696 # print(f'kwargs:\n{kwargs}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
697
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
698 # Check parameter norms (also need it for expressions to renormalize the errors)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
699 if self._norm is not None and (callable(model) or model == 'expression'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
700 missing_norm = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
701 for name in new_parameters.valuesdict():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
702 if name not in self._parameter_norms:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
703 print(f'new_parameters:\n{new_parameters.valuesdict()}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
704 print(f'self._parameter_norms:\n{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
705 logging.error(f'Missing parameter normalization type for {name} in {model}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
706 missing_norm = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
707 if missing_norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
708 raise ValueError
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
709
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
710 # print(f'at end add_model:\nself._parameters:\n{list(self.parameters)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
711 # print(f'at end add_model: kwargs = {kwargs}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
712 # print(f'\nat end add_model: newmodel:\n{newmodel.__dict__}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
713 return(kwargs)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
714
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
715 def fit(self, interactive=False, guess=False, **kwargs):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
716 # Check inputs
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
717 if self._model is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
718 logging.error('Undefined fit model')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
719 return
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
720 if not isinstance(interactive, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
721 illegal_value(interactive, 'interactive', 'Fit.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
722 if not isinstance(guess, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
723 illegal_value(guess, 'guess', 'Fit.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
724 if 'try_linear_fit' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
725 try_linear_fit = kwargs.pop('try_linear_fit')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
726 if not isinstance(try_linear_fit, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
727 illegal_value(try_linear_fit, 'try_linear_fit', 'Fit.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
728 if not self._try_linear_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
729 logging.warning('Ignore superfluous keyword argument "try_linear_fit" (not '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
730 'yet supported for callable models)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
731 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
732 self._try_linear_fit = try_linear_fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
733 # if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
734 # if 'parameters' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
735 # raise ValueError('Invalid parameter parameters ({kwargs["parameters"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
736 # else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
737 if self._result is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
738 if guess:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
739 logging.warning('Ignoring input parameter guess in Fit.fit during refitting')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
740 guess = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
741
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
742 # Check for circular expressions
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
743 # FIX TODO
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
744 # for name1, par1 in self._parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
745 # if par1.expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
746
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
747 # Apply mask if supplied:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
748 if 'mask' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
749 self._mask = kwargs.pop('mask')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
750 if self._mask is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
751 self._mask = np.asarray(self._mask).astype(bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
752 if self._x.size != self._mask.size:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
753 raise ValueError(f'Inconsistent x and mask dimensions ({self._x.size} vs '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
754 f'{self._mask.size})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
755
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
756 # Estimate initial parameters with build-in lmfit guess method (only for a single model)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
757 # print(f'\nat start fit: kwargs = {kwargs}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
758 #RV print('\nAt start of fit:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
759 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
760 # print(f'parameter_norms:\n{self._parameter_norms}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
761 if guess:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
762 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
763 self._parameters = self._model.guess(self._y, x=self._x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
764 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
765 self._parameters = self._model.guess(np.asarray(self._y)[~self._mask],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
766 x=self._x[~self._mask])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
767 # print('\nAfter guess:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
768 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
769
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
770 # Add constant offset for a normalized model
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
771 if self._result is None and self._norm is not None and self._norm[0]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
772 self.add_model('constant', prefix='tmp_normalization_offset_', parameters={'name': 'c',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
773 'value': -self._norm[0], 'vary': False, 'norm': True})
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
774 #'value': -self._norm[0]/self._norm[1], 'vary': False, 'norm': False})
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
775
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
776 # Adjust existing parameters for refit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
777 if 'parameters' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
778 parameters = kwargs.pop('parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
779 if isinstance(parameters, dict):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
780 parameters = (parameters, )
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
781 elif not is_dict_series(parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
782 illegal_value(parameters, 'parameters', 'Fit.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
783 for par in parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
784 name = par['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
785 if name not in self._parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
786 raise ValueError(f'Unable to match {name} parameter {par} to an existing one')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
787 if self._parameters[name].expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
788 raise ValueError(f'Unable to modify {name} parameter {par} (currently an '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
789 'expression)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
790 if par.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
791 raise KeyError(f'Illegal "expr" key in {name} parameter {par}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
792 self._parameters[name].set(vary=par.get('vary'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
793 self._parameters[name].set(min=par.get('min'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
794 self._parameters[name].set(max=par.get('max'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
795 self._parameters[name].set(value=par.get('value'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
796 #RV print('\nAfter adjust:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
797 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
798
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
799 # Apply parameter updates through keyword arguments
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
800 # print(f'kwargs = {kwargs}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
801 # print(f'parameter_norms = {self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
802 for name in set(self._parameters) & set(kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
803 value = kwargs.pop(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
804 if self._parameters[name].expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
805 self._parameters[name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
806 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
807 logging.warning(f'Ignoring parameter {name} in Fit.fit (set by expression: '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
808 f'{self._parameters[name].expr})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
809
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
810 # Check for uninitialized parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
811 for name, par in self._parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
812 if par.expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
813 value = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
814 if value is None or np.isinf(value) or np.isnan(value):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
815 if interactive:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
816 value = input_num(f'Enter an initial value for {name}', default=1.0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
817 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
818 value = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
819 if self._norm is None or name not in self._parameter_norms:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
820 self._parameters[name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
821 elif self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
822 self._parameters[name].set(value=value*self._norm[1])
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
823
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
824 # Check if model is linear
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
825 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
826 linear_model = self._check_linearity_model()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
827 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
828 linear_model = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
829 # print(f'\n\n--------> linear_model = {linear_model}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
830 if kwargs.get('check_only_linearity') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
831 return(linear_model)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
832
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
833 # Normalize the data and initial parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
834 #RV print('\nBefore normalization:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
835 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
836 # print(f'parameter_norms:\n{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
837 self._normalize()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
838 # print(f'norm = {self._norm}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
839 #RV print('\nAfter normalization:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
840 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
841 # self.print_fit_report()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
842 # print(f'parameter_norms:\n{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
843
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
844 if linear_model:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
845 # Perform a linear fit by direct matrix solution with numpy
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
846 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
847 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
848 self._fit_linear_model(self._x, self._y_norm)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
849 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
850 self._fit_linear_model(self._x[~self._mask],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
851 np.asarray(self._y_norm)[~self._mask])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
852 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
853 linear_model = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
854 if not linear_model:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
855 # Perform a non-linear fit with lmfit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
856 # Prevent initial values from sitting at boundaries
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
857 self._parameter_bounds = {name:{'min': par.min, 'max': par.max} for name, par in
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
858 self._parameters.items() if par.vary}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
859 for par in self._parameters.values():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
860 if par.vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
861 par.set(value=self._reset_par_at_boundary(par, par.value))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
862 # print('\nAfter checking boundaries:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
863 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
864
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
865 # Perform the fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
866 # fit_kws = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
867 # if 'Dfun' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
868 # fit_kws = {'Dfun': kwargs.pop('Dfun')}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
869 # self._result = self._model.fit(self._y_norm, self._parameters, x=self._x,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
870 # fit_kws=fit_kws, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
871 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
872 self._result = self._model.fit(self._y_norm, self._parameters, x=self._x, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
873 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
874 self._result = self._model.fit(np.asarray(self._y_norm)[~self._mask],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
875 self._parameters, x=self._x[~self._mask], **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
876 #RV print('\nAfter fit:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
877 # print(f'\nself._result ({self._result}):\n\t{self._result.__dict__}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
878 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
879 # self.print_fit_report()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
880
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
881 # Set internal parameter values to fit results upon success
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
882 if self.success:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
883 for name, par in self._parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
884 if par.expr is None and par.vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
885 par.set(value=self._result.params[name].value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
886 # print('\nAfter update parameter values:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
887 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
888
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
889 # Renormalize the data and results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
890 self._renormalize()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
891 #RV print('\nAfter renormalization:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
892 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
893 # self.print_fit_report()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
894
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
895 def plot(self, y=None, y_title=None, result=None, skip_init=False, plot_comp_legends=False,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
896 plot_residual=False, plot_masked_data=True, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
897 if result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
898 result = self._result
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
899 if result is None:
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
900 return
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
901 plots = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
902 legend = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
903 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
904 mask = np.zeros(self._x.size).astype(bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
905 plot_masked_data = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
906 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
907 mask = self._mask
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
908 if y is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
909 if not isinstance(y, (tuple, list, np.ndarray)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
910 illegal_value(y, 'y', 'Fit.plot')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
911 if len(y) != len(self._x):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
912 logging.warning('Ignoring parameter y in Fit.plot (wrong dimension)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
913 y = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
914 if y is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
915 if y_title is None or not isinstance(y_title, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
916 y_title = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
917 plots += [(self._x, y, '.')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
918 legend += [y_title]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
919 if self._y is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
920 plots += [(self._x, np.asarray(self._y), 'b.')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
921 legend += ['data']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
922 if plot_masked_data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
923 plots += [(self._x[mask], np.asarray(self._y)[mask], 'bx')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
924 legend += ['masked data']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
925 if isinstance(plot_residual, bool) and plot_residual:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
926 plots += [(self._x[~mask], result.residual, 'k-')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
927 legend += ['residual']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
928 plots += [(self._x[~mask], result.best_fit, 'k-')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
929 legend += ['best fit']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
930 if not skip_init and hasattr(result, 'init_fit'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
931 plots += [(self._x[~mask], result.init_fit, 'g-')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
932 legend += ['init']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
933 components = result.eval_components(x=self._x[~mask])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
934 num_components = len(components)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
935 if 'tmp_normalization_offset_' in components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
936 num_components -= 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
937 if num_components > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
938 eval_index = 0
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
939 for modelname, y in components.items():
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
940 if modelname == 'tmp_normalization_offset_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
941 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
942 if modelname == '_eval':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
943 modelname = f'eval{eval_index}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
944 if len(modelname) > 20:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
945 modelname = f'{modelname[0:16]} ...'
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
946 if isinstance(y, (int, float)):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
947 y *= np.ones(self._x[~mask].size)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
948 plots += [(self._x[~mask], y, '--')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
949 if plot_comp_legends:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
950 if modelname[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
951 legend.append(modelname[:-1])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
952 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
953 legend.append(modelname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
954 title = kwargs.get('title')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
955 if title is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
956 kwargs.pop('title')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
957 quick_plot(tuple(plots), legend=legend, title=title, block=True, **kwargs)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
958
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
959 @staticmethod
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
960 def guess_init_peak(x, y, *args, center_guess=None, use_max_for_center=True):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
961 """ Return a guess for the initial height, center and fwhm for a peak
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
962 """
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
963 # print(f'\n\nargs = {args}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
964 # print(f'center_guess = {center_guess}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
965 # quick_plot(x, y, vlines=center_guess, block=True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
966 center_guesses = None
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
967 x = np.asarray(x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
968 y = np.asarray(y)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
969 if len(x) != len(y):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
970 logging.error(f'Invalid x and y lengths ({len(x)}, {len(y)}), skip initial guess')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
971 return(None, None, None)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
972 if isinstance(center_guess, (int, float)):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
973 if len(args):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
974 logging.warning('Ignoring additional arguments for single center_guess value')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
975 center_guesses = [center_guess]
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
976 elif isinstance(center_guess, (tuple, list, np.ndarray)):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
977 if len(center_guess) == 1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
978 logging.warning('Ignoring additional arguments for single center_guess value')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
979 if not isinstance(center_guess[0], (int, float)):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
980 raise ValueError(f'Invalid parameter center_guess ({type(center_guess[0])})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
981 center_guess = center_guess[0]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
982 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
983 if len(args) != 1:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
984 raise ValueError(f'Invalid number of arguments ({len(args)})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
985 n = args[0]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
986 if not is_index(n, 0, len(center_guess)):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
987 raise ValueError('Invalid argument')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
988 center_guesses = center_guess
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
989 center_guess = center_guesses[n]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
990 elif center_guess is not None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
991 raise ValueError(f'Invalid center_guess type ({type(center_guess)})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
992 # print(f'x = {x}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
993 # print(f'y = {y}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
994 # print(f'center_guess = {center_guess}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
995
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
996 # Sort the inputs
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
997 index = np.argsort(x)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
998 x = x[index]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
999 y = y[index]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1000 miny = y.min()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1001 # print(f'miny = {miny}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1002 # print(f'x_range = {x[0]} {x[-1]} {len(x)}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1003 # print(f'y_range = {y[0]} {y[-1]} {len(y)}')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1004 # quick_plot(x, y, vlines=center_guess, block=True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1005
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1006 # xx = x
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1007 # yy = y
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1008 # Set range for current peak
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1009 # print(f'n = {n}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1010 # print(f'center_guesses = {center_guesses}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1011 if center_guesses is not None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1012 if len(center_guesses) > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1013 index = np.argsort(center_guesses)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1014 n = list(index).index(n)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1015 # print(f'n = {n}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1016 # print(f'index = {index}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1017 center_guesses = np.asarray(center_guesses)[index]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1018 # print(f'center_guesses = {center_guesses}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1019 if n == 0:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1020 low = 0
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1021 upp = index_nearest(x, (center_guesses[0]+center_guesses[1])/2)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1022 elif n == len(center_guesses)-1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1023 low = index_nearest(x, (center_guesses[n-1]+center_guesses[n])/2)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1024 upp = len(x)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1025 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1026 low = index_nearest(x, (center_guesses[n-1]+center_guesses[n])/2)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1027 upp = index_nearest(x, (center_guesses[n]+center_guesses[n+1])/2)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1028 # print(f'low = {low}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1029 # print(f'upp = {upp}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1030 x = x[low:upp]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1031 y = y[low:upp]
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1032 # quick_plot(x, y, vlines=(x[0], center_guess, x[-1]), block=True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1033
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1034 # Estimate FHHM
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1035 maxy = y.max()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1036 # print(f'x_range = {x[0]} {x[-1]} {len(x)}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1037 # print(f'y_range = {y[0]} {y[-1]} {len(y)} {miny} {maxy}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1038 # print(f'center_guess = {center_guess}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1039 if center_guess is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1040 center_index = np.argmax(y)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1041 center = x[center_index]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1042 height = maxy-miny
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1043 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1044 if use_max_for_center:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1045 center_index = np.argmax(y)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1046 center = x[center_index]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1047 if center_index < 0.1*len(x) or center_index > 0.9*len(x):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1048 center_index = index_nearest(x, center_guess)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1049 center = center_guess
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1050 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1051 center_index = index_nearest(x, center_guess)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1052 center = center_guess
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1053 height = y[center_index]-miny
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1054 # print(f'center_index = {center_index}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1055 # print(f'center = {center}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1056 # print(f'height = {height}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1057 half_height = miny+0.5*height
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1058 # print(f'half_height = {half_height}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1059 fwhm_index1 = 0
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1060 for i in range(center_index, fwhm_index1, -1):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1061 if y[i] < half_height:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1062 fwhm_index1 = i
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1063 break
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1064 # print(f'fwhm_index1 = {fwhm_index1} {x[fwhm_index1]}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1065 fwhm_index2 = len(x)-1
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1066 for i in range(center_index, fwhm_index2):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1067 if y[i] < half_height:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1068 fwhm_index2 = i
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1069 break
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1070 # print(f'fwhm_index2 = {fwhm_index2} {x[fwhm_index2]}')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1071 # quick_plot((x,y,'o'), vlines=(x[fwhm_index1], center, x[fwhm_index2]), block=True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1072 if fwhm_index1 == 0 and fwhm_index2 < len(x)-1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1073 fwhm = 2*(x[fwhm_index2]-center)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1074 elif fwhm_index1 > 0 and fwhm_index2 == len(x)-1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1075 fwhm = 2*(center-x[fwhm_index1])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1076 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1077 fwhm = x[fwhm_index2]-x[fwhm_index1]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1078 # print(f'fwhm_index1 = {fwhm_index1} {x[fwhm_index1]}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1079 # print(f'fwhm_index2 = {fwhm_index2} {x[fwhm_index2]}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1080 # print(f'fwhm = {fwhm}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1081
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1082 # Return height, center and FWHM
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1083 # quick_plot((x,y,'o'), (xx,yy), vlines=(x[fwhm_index1], center, x[fwhm_index2]), block=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1084 return(height, center, fwhm)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1085
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1086 def _check_linearity_model(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1087 """Identify the linearity of all model parameters and check if the model is linear or not
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1088 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1089 if not self._try_linear_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1090 logging.info('Skip linearity check (not yet supported for callable models)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1091 return(False)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1092 free_parameters = [name for name, par in self._parameters.items() if par.vary]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1093 for component in self._model.components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1094 if 'tmp_normalization_offset_c' in component.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1095 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1096 if isinstance(component, ExpressionModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1097 for name in free_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1098 if diff(component.expr, name, name):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1099 # print(f'\t\t{component.expr} is non-linear in {name}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1100 self._nonlinear_parameters.append(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1101 if name in self._linear_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1102 self._linear_parameters.remove(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1103 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1104 model_parameters = component.param_names.copy()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1105 for basename, hint in component.param_hints.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1106 name = f'{component.prefix}{basename}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1107 if hint.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1108 model_parameters.remove(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1109 for name in model_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1110 expr = self._parameters[name].expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1111 if expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1112 for nname in free_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1113 if name in self._nonlinear_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1114 if diff(expr, nname):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1115 # print(f'\t\t{component} is non-linear in {nname} (through {name} = "{expr}")')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1116 self._nonlinear_parameters.append(nname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1117 if nname in self._linear_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1118 self._linear_parameters.remove(nname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1119 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1120 assert(name in self._linear_parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1121 # print(f'\n\nexpr ({type(expr)}) = {expr}\nnname ({type(nname)}) = {nname}\n\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1122 if diff(expr, nname, nname):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1123 # print(f'\t\t{component} is non-linear in {nname} (through {name} = "{expr}")')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1124 self._nonlinear_parameters.append(nname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1125 if nname in self._linear_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1126 self._linear_parameters.remove(nname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1127 # print(f'\nfree parameters:\n\t{free_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1128 # print(f'linear parameters:\n\t{self._linear_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1129 # print(f'nonlinear parameters:\n\t{self._nonlinear_parameters}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1130 if any(True for name in self._nonlinear_parameters if self._parameters[name].vary):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1131 return(False)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1132 return(True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1133
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1134 def _fit_linear_model(self, x, y):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1135 """Perform a linear fit by direct matrix solution with numpy
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1136 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1137 # Construct the matrix and the free parameter vector
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1138 # print(f'\nparameters:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1139 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1140 # print(f'\nparameter_norms:\n\t{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1141 # print(f'\nlinear_parameters:\n\t{self._linear_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1142 # print(f'nonlinear_parameters:\n\t{self._nonlinear_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1143 free_parameters = [name for name, par in self._parameters.items() if par.vary]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1144 # print(f'free parameters:\n\t{free_parameters}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1145 expr_parameters = {name:par.expr for name, par in self._parameters.items()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1146 if par.expr is not None}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1147 model_parameters = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1148 for component in self._model.components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1149 if 'tmp_normalization_offset_c' in component.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1150 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1151 model_parameters += component.param_names
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1152 for basename, hint in component.param_hints.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1153 name = f'{component.prefix}{basename}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1154 if hint.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1155 expr_parameters.pop(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1156 model_parameters.remove(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1157 # print(f'expr parameters:\n{expr_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1158 # print(f'model parameters:\n\t{model_parameters}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1159 norm = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1160 if self._normalized:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1161 norm = self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1162 # print(f'\n\nself._normalized = {self._normalized}\nnorm = {norm}\nself._norm = {self._norm}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1163 # Add expression parameters to asteval
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1164 ast = Interpreter()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1165 # print(f'Adding to asteval sym table:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1166 for name, expr in expr_parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1167 # print(f'\tadding {name} {expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1168 ast.symtable[name] = expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1169 # Add constant parameters to asteval
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1170 # (renormalize to use correctly in evaluation of expression models)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1171 for name, par in self._parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1172 if par.expr is None and not par.vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1173 if self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1174 # print(f'\tadding {name} {par.value*norm}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1175 ast.symtable[name] = par.value*norm
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1176 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1177 # print(f'\tadding {name} {par.value}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1178 ast.symtable[name] = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1179 A = np.zeros((len(x), len(free_parameters)), dtype='float64')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1180 y_const = np.zeros(len(x), dtype='float64')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1181 have_expression_model = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1182 for component in self._model.components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1183 if isinstance(component, ConstantModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1184 name = component.param_names[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1185 # print(f'\nConstant model: {name} {self._parameters[name]}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1186 if name in free_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1187 # print(f'\t\t{name} is a free constant set matrix column {free_parameters.index(name)} to 1.0')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1188 A[:,free_parameters.index(name)] = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1189 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1190 if self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1191 delta_y_const = self._parameters[name]*np.ones(len(x))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1192 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1193 delta_y_const = (self._parameters[name]*norm)*np.ones(len(x))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1194 y_const += delta_y_const
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1195 # print(f'\ndelta_y_const ({type(delta_y_const)}):\n{delta_y_const}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1196 elif isinstance(component, ExpressionModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1197 have_expression_model = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1198 const_expr = component.expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1199 # print(f'\nExpression model:\nconst_expr: {const_expr}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1200 for name in free_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1201 dexpr_dname = diff(component.expr, name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1202 if dexpr_dname:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1203 const_expr = f'{const_expr}-({str(dexpr_dname)})*{name}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1204 # print(f'\tconst_expr: {const_expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1205 if not self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1206 dexpr_dname = f'({dexpr_dname})/{norm}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1207 # print(f'\t{component.expr} is linear in {name}\n\t\tadd "{str(dexpr_dname)}" to matrix as column {free_parameters.index(name)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1208 fx = [(lambda _: ast.eval(str(dexpr_dname)))(ast(f'x={v}')) for v in x]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1209 # print(f'\tfx:\n{fx}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1210 if len(ast.error):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1211 raise ValueError(f'Unable to evaluate {dexpr_dname}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1212 A[:,free_parameters.index(name)] += fx
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1213 # if self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1214 # print(f'\t\t{component.expr} is linear in {name} add "{str(dexpr_dname)}" to matrix as column {free_parameters.index(name)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1215 # A[:,free_parameters.index(name)] += fx
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1216 # else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1217 # print(f'\t\t{component.expr} is linear in {name} add "({str(dexpr_dname)})/{norm}" to matrix as column {free_parameters.index(name)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1218 # A[:,free_parameters.index(name)] += np.asarray(fx)/norm
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1219 # FIX: find another solution if expr not supported by simplify
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1220 const_expr = str(simplify(f'({const_expr})/{norm}'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1221 # print(f'\nconst_expr: {const_expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1222 delta_y_const = [(lambda _: ast.eval(const_expr))(ast(f'x = {v}')) for v in x]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1223 y_const += delta_y_const
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1224 # print(f'\ndelta_y_const ({type(delta_y_const)}):\n{delta_y_const}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1225 if len(ast.error):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1226 raise ValueError(f'Unable to evaluate {const_expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1227 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1228 free_model_parameters = [name for name in component.param_names
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1229 if name in free_parameters or name in expr_parameters]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1230 # print(f'\nBuild-in model ({component}):\nfree_model_parameters: {free_model_parameters}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1231 if not len(free_model_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1232 y_const += component.eval(params=self._parameters, x=x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1233 elif isinstance(component, LinearModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1234 if f'{component.prefix}slope' in free_model_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1235 A[:,free_parameters.index(f'{component.prefix}slope')] = x
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1236 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1237 y_const += self._parameters[f'{component.prefix}slope'].value*x
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1238 if f'{component.prefix}intercept' in free_model_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1239 A[:,free_parameters.index(f'{component.prefix}intercept')] = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1240 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1241 y_const += self._parameters[f'{component.prefix}intercept'].value* \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1242 np.ones(len(x))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1243 elif isinstance(component, QuadraticModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1244 if f'{component.prefix}a' in free_model_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1245 A[:,free_parameters.index(f'{component.prefix}a')] = x**2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1246 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1247 y_const += self._parameters[f'{component.prefix}a'].value*x**2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1248 if f'{component.prefix}b' in free_model_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1249 A[:,free_parameters.index(f'{component.prefix}b')] = x
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1250 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1251 y_const += self._parameters[f'{component.prefix}b'].value*x
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1252 if f'{component.prefix}c' in free_model_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1253 A[:,free_parameters.index(f'{component.prefix}c')] = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1254 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1255 y_const += self._parameters[f'{component.prefix}c'].value*np.ones(len(x))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1256 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1257 # At this point each build-in model must be strictly proportional to each linear
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1258 # model parameter. Without this assumption, the model equation is needed
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1259 # For the current build-in lmfit models, this can only ever be the amplitude
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1260 assert(len(free_model_parameters) == 1)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1261 name = f'{component.prefix}amplitude'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1262 assert(free_model_parameters[0] == name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1263 assert(self._parameter_norms[name])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1264 expr = self._parameters[name].expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1265 if expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1266 # print(f'\t{component} is linear in {name} add to matrix as column {free_parameters.index(name)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1267 parameters = deepcopy(self._parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1268 parameters[name].set(value=1.0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1269 index = free_parameters.index(name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1270 A[:,free_parameters.index(name)] += component.eval(params=parameters, x=x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1271 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1272 const_expr = expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1273 # print(f'\tconst_expr: {const_expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1274 parameters = deepcopy(self._parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1275 parameters[name].set(value=1.0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1276 dcomp_dname = component.eval(params=parameters, x=x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1277 # print(f'\tdcomp_dname ({type(dcomp_dname)}):\n{dcomp_dname}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1278 for nname in free_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1279 dexpr_dnname = diff(expr, nname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1280 if dexpr_dnname:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1281 assert(self._parameter_norms[name])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1282 # print(f'\t\td({expr})/d{nname} = {dexpr_dnname}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1283 # print(f'\t\t{component} is linear in {nname} (through {name} = "{expr}", add to matrix as column {free_parameters.index(nname)})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1284 fx = np.asarray(dexpr_dnname*dcomp_dname, dtype='float64')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1285 # print(f'\t\tfx ({type(fx)}): {fx}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1286 # print(f'free_parameters.index({nname}): {free_parameters.index(nname)}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1287 if self._parameter_norms[nname]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1288 A[:,free_parameters.index(nname)] += fx
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1289 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1290 A[:,free_parameters.index(nname)] += fx/norm
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1291 const_expr = f'{const_expr}-({dexpr_dnname})*{nname}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1292 # print(f'\t\tconst_expr: {const_expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1293 const_expr = str(simplify(f'({const_expr})/{norm}'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1294 # print(f'\tconst_expr: {const_expr}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1295 fx = [(lambda _: ast.eval(const_expr))(ast(f'x = {v}')) for v in x]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1296 # print(f'\tfx: {fx}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1297 delta_y_const = np.multiply(fx, dcomp_dname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1298 y_const += delta_y_const
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1299 # print(f'\ndelta_y_const ({type(delta_y_const)}):\n{delta_y_const}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1300 # print(A)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1301 # print(y_const)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1302 solution, residual, rank, s = np.linalg.lstsq(A, y-y_const, rcond=None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1303 # print(f'\nsolution ({type(solution)} {solution.shape}):\n\t{solution}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1304 # print(f'\nresidual ({type(residual)} {residual.shape}):\n\t{residual}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1305 # print(f'\nrank ({type(rank)} {rank.shape}):\n\t{rank}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1306 # print(f'\ns ({type(s)} {s.shape}):\n\t{s}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1307
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1308 # Assemble result (compensate for normalization in expression models)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1309 for name, value in zip(free_parameters, solution):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1310 self._parameters[name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1311 if self._normalized and (have_expression_model or len(expr_parameters)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1312 for name, norm in self._parameter_norms.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1313 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1314 if par.expr is None and norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1315 self._parameters[name].set(value=par.value*self._norm[1])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1316 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1317 # print(f'\nself._parameter_norms:\n\t{self._parameter_norms}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1318 self._result = ModelResult(self._model, deepcopy(self._parameters))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1319 self._result.best_fit = self._model.eval(params=self._parameters, x=x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1320 if self._normalized and (have_expression_model or len(expr_parameters)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1321 if 'tmp_normalization_offset_c' in self._parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1322 offset = self._parameters['tmp_normalization_offset_c']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1323 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1324 offset = 0.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1325 self._result.best_fit = (self._result.best_fit-offset-self._norm[0])/self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1326 if self._normalized:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1327 for name, norm in self._parameter_norms.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1328 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1329 if par.expr is None and norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1330 value = par.value/self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1331 self._parameters[name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1332 self._result.params[name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1333 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1334 self._result.residual = self._result.best_fit-y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1335 self._result.components = self._model.components
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1336 self._result.init_params = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1337 # quick_plot((x, y, '.'), (x, y_const, 'g'), (x, self._result.best_fit, 'k'), (x, self._result.residual, 'r'), block=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1338
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1339 def _normalize(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1340 """Normalize the data and initial parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1341 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1342 if self._normalized:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1343 return
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1344 if self._norm is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1345 if self._y is not None and self._y_norm is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1346 self._y_norm = np.asarray(self._y)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1347 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1348 if self._y is not None and self._y_norm is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1349 self._y_norm = (np.asarray(self._y)-self._norm[0])/self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1350 self._y_range = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1351 for name, norm in self._parameter_norms.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1352 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1353 if par.expr is None and norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1354 value = par.value/self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1355 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1356 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1357 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1358 _min /= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1359 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1360 _max /= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1361 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1362 self._normalized = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1363
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1364 def _renormalize(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1365 """Renormalize the data and results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1366 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1367 if self._norm is None or not self._normalized:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1368 return
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1369 self._normalized = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1370 for name, norm in self._parameter_norms.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1371 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1372 if par.expr is None and norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1373 value = par.value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1374 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1375 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1376 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1377 _min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1378 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1379 _max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1380 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1381 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1382 return
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1383 self._result.best_fit = self._result.best_fit*self._norm[1]+self._norm[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1384 for name, par in self._result.params.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1385 if self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1386 if par.stderr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1387 par.stderr *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1388 if par.expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1389 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1390 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1391 value = par.value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1392 if par.init_value is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1393 par.init_value *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1394 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1395 _min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1396 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1397 _max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1398 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1399 if hasattr(self._result, 'init_fit'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1400 self._result.init_fit = self._result.init_fit*self._norm[1]+self._norm[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1401 if hasattr(self._result, 'init_values'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1402 init_values = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1403 for name, value in self._result.init_values.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1404 if name not in self._parameter_norms or self._parameters[name].expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1405 init_values[name] = value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1406 elif self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1407 init_values[name] = value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1408 self._result.init_values = init_values
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1409 for name, par in self._result.init_params.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1410 if par.expr is None and self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1411 value = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1412 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1413 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1414 value *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1415 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1416 _min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1417 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1418 _max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1419 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1420 par.init_value = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1421 # Don't renormalize chisqr, it has no useful meaning in physical units
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1422 #self._result.chisqr *= self._norm[1]*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1423 if self._result.covar is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1424 for i, name in enumerate(self._result.var_names):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1425 if self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1426 for j in range(len(self._result.var_names)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1427 if self._result.covar[i,j] is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1428 self._result.covar[i,j] *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1429 if self._result.covar[j,i] is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1430 self._result.covar[j,i] *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1431 # Don't renormalize redchi, it has no useful meaning in physical units
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1432 #self._result.redchi *= self._norm[1]*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1433 if self._result.residual is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1434 self._result.residual *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1435
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1436 def _reset_par_at_boundary(self, par, value):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1437 assert(par.vary)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1438 name = par.name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1439 _min = self._parameter_bounds[name]['min']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1440 _max = self._parameter_bounds[name]['max']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1441 if np.isinf(_min):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1442 if not np.isinf(_max):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1443 if self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1444 upp = _max-0.1*self._y_range
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1445 elif _max == 0.0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1446 upp = _max-0.1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1447 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1448 upp = _max-0.1*abs(_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1449 if value >= upp:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1450 return(upp)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1451 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1452 if np.isinf(_max):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1453 if self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1454 low = _min+0.1*self._y_range
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1455 elif _min == 0.0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1456 low = _min+0.1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1457 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1458 low = _min+0.1*abs(_min)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1459 if value <= low:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1460 return(low)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1461 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1462 low = 0.9*_min+0.1*_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1463 upp = 0.1*_min+0.9*_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1464 if value <= low:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1465 return(low)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1466 elif value >= upp:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1467 return(upp)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1468 return(value)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1469
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1470
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1471 class FitMultipeak(Fit):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1472 """Fit data with multiple peaks
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1473 """
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1474 def __init__(self, y, x=None, normalize=True):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1475 super().__init__(y, x=x, normalize=normalize)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1476 self._fwhm_max = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1477 self._sigma_max = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1478
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1479 @classmethod
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1480 def fit_multipeak(cls, y, centers, x=None, normalize=True, peak_models='gaussian',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1481 center_exprs=None, fit_type=None, background_order=None, background_exp=False,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1482 fwhm_max=None, plot_components=False):
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1483 """Make sure that centers and fwhm_max are in the correct units and consistent with expr
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1484 for a uniform fit (fit_type == 'uniform')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1485 """
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1486 fit = cls(y, x=x, normalize=normalize)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1487 success = fit.fit(centers, fit_type=fit_type, peak_models=peak_models, fwhm_max=fwhm_max,
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1488 center_exprs=center_exprs, background_order=background_order,
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1489 background_exp=background_exp, plot_components=plot_components)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1490 if success:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1491 return(fit.best_fit, fit.residual, fit.best_values, fit.best_errors, fit.redchi, \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1492 fit.success)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1493 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1494 return(np.array([]), np.array([]), {}, {}, float_max, False)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1495
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1496 def fit(self, centers, fit_type=None, peak_models=None, center_exprs=None, fwhm_max=None,
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1497 background_order=None, background_exp=False, plot_components=False,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1498 param_constraint=False):
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1499 self._fwhm_max = fwhm_max
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1500 # Create the multipeak model
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1501 self._create_model(centers, fit_type, peak_models, center_exprs, background_order,
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1502 background_exp, param_constraint)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1503
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1504 # RV: Obsolete Normalize the data and results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1505 # print('\nBefore fit before normalization in FitMultipeak:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1506 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1507 # self._normalize()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1508 # print('\nBefore fit after normalization in FitMultipeak:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1509 # self._parameters.pretty_print()
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1510
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1511 # Perform the fit
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1512 try:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1513 if param_constraint:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1514 super().fit(fit_kws={'xtol': 1.e-5, 'ftol': 1.e-5, 'gtol': 1.e-5})
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1515 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1516 super().fit()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1517 except:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1518 return(False)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1519
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1520 # Check for valid fit parameter results
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1521 fit_failure = self._check_validity()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1522 success = True
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1523 if fit_failure:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1524 if param_constraint:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1525 logging.warning(' -> Should not happen with param_constraint set, fail the fit')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1526 success = False
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1527 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1528 logging.info(' -> Retry fitting with constraints')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1529 self.fit(centers, fit_type, peak_models, center_exprs, fwhm_max=fwhm_max,
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1530 background_order=background_order, background_exp=background_exp,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1531 plot_components=plot_components, param_constraint=True)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1532 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1533 # RV: Obsolete Renormalize the data and results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1534 # print('\nAfter fit before renormalization in FitMultipeak:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1535 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1536 # self.print_fit_report()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1537 # self._renormalize()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1538 # print('\nAfter fit after renormalization in FitMultipeak:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1539 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1540 # self.print_fit_report()
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1541
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1542 # Print report and plot components if requested
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1543 if plot_components:
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1544 self.print_fit_report()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1545 self.plot()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1546
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1547 return(success)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1548
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1549 def _create_model(self, centers, fit_type=None, peak_models=None, center_exprs=None,
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1550 background_order=None, background_exp=False, param_constraint=False):
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1551 """Create the multipeak model
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1552 """
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1553 if isinstance(centers, (int, float)):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1554 centers = [centers]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1555 num_peaks = len(centers)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1556 if peak_models is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1557 peak_models = num_peaks*['gaussian']
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1558 elif isinstance(peak_models, str):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1559 peak_models = num_peaks*[peak_models]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1560 if len(peak_models) != num_peaks:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1561 raise ValueError(f'Inconsistent number of peaks in peak_models ({len(peak_models)} vs '+
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1562 f'{num_peaks})')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1563 if num_peaks == 1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1564 if fit_type is not None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1565 logging.debug('Ignoring fit_type input for fitting one peak')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1566 fit_type = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1567 if center_exprs is not None:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1568 logging.debug('Ignoring center_exprs input for fitting one peak')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1569 center_exprs = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1570 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1571 if fit_type == 'uniform':
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1572 if center_exprs is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1573 center_exprs = [f'scale_factor*{cen}' for cen in centers]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1574 if len(center_exprs) != num_peaks:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1575 raise ValueError(f'Inconsistent number of peaks in center_exprs '+
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1576 f'({len(center_exprs)} vs {num_peaks})')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1577 elif fit_type == 'unconstrained' or fit_type is None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1578 if center_exprs is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1579 logging.warning('Ignoring center_exprs input for unconstrained fit')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1580 center_exprs = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1581 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1582 raise ValueError(f'Invalid fit_type in fit_multigaussian {fit_type}')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1583 self._sigma_max = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1584 if param_constraint:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1585 min_value = float_min
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1586 if self._fwhm_max is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1587 self._sigma_max = np.zeros(num_peaks)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1588 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1589 min_value = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1590
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1591 # Reset the fit
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1592 self._model = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1593 self._parameters = Parameters()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1594 self._result = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1595
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1596 # Add background model
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1597 if background_order is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1598 if background_order == 0:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1599 self.add_model('constant', prefix='background', parameters=
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1600 {'name': 'c', 'value': float_min, 'min': min_value})
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1601 elif background_order == 1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1602 self.add_model('linear', prefix='background', slope=0.0, intercept=0.0)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1603 elif background_order == 2:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1604 self.add_model('quadratic', prefix='background', a=0.0, b=0.0, c=0.0)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1605 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1606 raise ValueError(f'Invalid parameter background_order ({background_order})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1607 if background_exp:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1608 self.add_model('exponential', prefix='background', parameters=(
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1609 {'name': 'amplitude', 'value': float_min, 'min': min_value},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1610 {'name': 'decay', 'value': float_min, 'min': min_value}))
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1611
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1612 # Add peaks and guess initial fit parameters
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1613 ast = Interpreter()
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1614 if num_peaks == 1:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1615 height_init, cen_init, fwhm_init = self.guess_init_peak(self._x, self._y)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1616 if self._fwhm_max is not None and fwhm_init > self._fwhm_max:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1617 fwhm_init = self._fwhm_max
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1618 ast(f'fwhm = {fwhm_init}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1619 ast(f'height = {height_init}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1620 sig_init = ast(fwhm_factor[peak_models[0]])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1621 amp_init = ast(height_factor[peak_models[0]])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1622 sig_max = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1623 if self._sigma_max is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1624 ast(f'fwhm = {self._fwhm_max}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1625 sig_max = ast(fwhm_factor[peak_models[0]])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1626 self._sigma_max[0] = sig_max
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1627 self.add_model(peak_models[0], parameters=(
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1628 {'name': 'amplitude', 'value': amp_init, 'min': min_value},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1629 {'name': 'center', 'value': cen_init, 'min': min_value},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1630 {'name': 'sigma', 'value': sig_init, 'min': min_value, 'max': sig_max}))
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1631 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1632 if fit_type == 'uniform':
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1633 self.add_parameter(name='scale_factor', value=1.0)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1634 for i in range(num_peaks):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1635 height_init, cen_init, fwhm_init = self.guess_init_peak(self._x, self._y, i,
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1636 center_guess=centers)
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1637 if self._fwhm_max is not None and fwhm_init > self._fwhm_max:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1638 fwhm_init = self._fwhm_max
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1639 ast(f'fwhm = {fwhm_init}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1640 ast(f'height = {height_init}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1641 sig_init = ast(fwhm_factor[peak_models[i]])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1642 amp_init = ast(height_factor[peak_models[i]])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1643 sig_max = None
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1644 if self._sigma_max is not None:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1645 ast(f'fwhm = {self._fwhm_max}')
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1646 sig_max = ast(fwhm_factor[peak_models[i]])
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1647 self._sigma_max[i] = sig_max
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1648 if fit_type == 'uniform':
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1649 self.add_model(peak_models[i], prefix=f'peak{i+1}_', parameters=(
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1650 {'name': 'amplitude', 'value': amp_init, 'min': min_value},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1651 {'name': 'center', 'expr': center_exprs[i]},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1652 {'name': 'sigma', 'value': sig_init, 'min': min_value,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1653 'max': sig_max}))
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1654 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1655 self.add_model('gaussian', prefix=f'peak{i+1}_', parameters=(
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1656 {'name': 'amplitude', 'value': amp_init, 'min': min_value},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1657 {'name': 'center', 'value': cen_init, 'min': min_value},
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1658 {'name': 'sigma', 'value': sig_init, 'min': min_value,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1659 'max': sig_max}))
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1660
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1661 def _check_validity(self):
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1662 """Check for valid fit parameter results
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1663 """
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1664 fit_failure = False
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1665 index = compile(r'\d+')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1666 for name, par in self.best_parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1667 if 'background' in name:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1668 # if ((name == 'backgroundc' and par['value'] <= 0.0) or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1669 # (name.endswith('amplitude') and par['value'] <= 0.0) or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1670 if ((name.endswith('amplitude') and par['value'] <= 0.0) or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1671 (name.endswith('decay') and par['value'] <= 0.0)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1672 logging.info(f'Invalid fit result for {name} ({par["value"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1673 fit_failure = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1674 elif (((name.endswith('amplitude') or name.endswith('height')) and
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1675 par['value'] <= 0.0) or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1676 ((name.endswith('sigma') or name.endswith('fwhm')) and par['value'] <= 0.0) or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1677 (name.endswith('center') and par['value'] <= 0.0) or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1678 (name == 'scale_factor' and par['value'] <= 0.0)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1679 logging.info(f'Invalid fit result for {name} ({par["value"]})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1680 fit_failure = True
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1681 if name.endswith('sigma') and self._sigma_max is not None:
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1682 if name == 'sigma':
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1683 sigma_max = self._sigma_max[0]
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1684 else:
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1685 sigma_max = self._sigma_max[int(index.search(name).group())-1]
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1686 if par['value'] > sigma_max:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1687 logging.info(f'Invalid fit result for {name} ({par["value"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1688 fit_failure = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1689 elif par['value'] == sigma_max:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1690 logging.warning(f'Edge result on for {name} ({par["value"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1691 if name.endswith('fwhm') and self._fwhm_max is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1692 if par['value'] > self._fwhm_max:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1693 logging.info(f'Invalid fit result for {name} ({par["value"]})')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1694 fit_failure = True
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1695 elif par['value'] == self._fwhm_max:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1696 logging.warning(f'Edge result on for {name} ({par["value"]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1697 return(fit_failure)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1698
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1699
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1700 class FitMap(Fit):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1701 """Fit a map of data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1702 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1703 def __init__(self, ymap, x=None, models=None, normalize=True, transpose=None, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1704 super().__init__(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1705 self._best_errors = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1706 self._best_fit = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1707 self._best_parameters = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1708 self._best_values = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1709 self._inv_transpose = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1710 self._max_nfev = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1711 self._memfolder = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1712 self._new_parameters = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1713 self._out_of_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1714 self._plot = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1715 self._print_report = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1716 self._redchi = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1717 self._redchi_cutoff = 0.1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1718 self._skip_init = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1719 self._success = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1720 self._transpose = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1721 self._try_no_bounds = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1722
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1723 # At this point the fastest index should always be the signal dimension so that the slowest
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1724 # ndim-1 dimensions are the map dimensions
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1725 if isinstance(ymap, (tuple, list, np.ndarray)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1726 self._x = np.asarray(x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1727 elif have_xarray and isinstance(ymap, xr.DataArray):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1728 if x is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1729 logging.warning('Ignoring superfluous input x ({x}) in Fit.__init__')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1730 self._x = np.asarray(ymap[ymap.dims[-1]])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1731 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1732 illegal_value(ymap, 'ymap', 'FitMap:__init__', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1733 self._ymap = ymap
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1734
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1735 # Verify the input parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1736 if self._x.ndim != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1737 raise ValueError(f'Invalid dimension for input x {self._x.ndim}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1738 if self._ymap.ndim < 2:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1739 raise ValueError('Invalid number of dimension of the input dataset '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1740 f'{self._ymap.ndim}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1741 if self._x.size != self._ymap.shape[-1]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1742 raise ValueError(f'Inconsistent x and y dimensions ({self._x.size} vs '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1743 f'{self._ymap.shape[-1]})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1744 if not isinstance(normalize, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1745 logging.warning(f'Invalid value for normalize ({normalize}) in Fit.__init__: '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1746 'setting normalize to True')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1747 normalize = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1748 if isinstance(transpose, bool) and not transpose:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1749 transpose = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1750 if transpose is not None and self._ymap.ndim < 3:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1751 logging.warning(f'Transpose meaningless for {self._ymap.ndim-1}D data maps: ignoring '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1752 'transpose')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1753 if transpose is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1754 if self._ymap.ndim == 3 and isinstance(transpose, bool) and transpose:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1755 self._transpose = (1, 0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1756 elif not isinstance(transpose, (tuple, list)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1757 logging.warning(f'Invalid data type for transpose ({transpose}, '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1758 f'{type(transpose)}) in Fit.__init__: setting transpose to False')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1759 elif len(transpose) != self._ymap.ndim-1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1760 logging.warning(f'Invalid dimension for transpose ({transpose}, must be equal to '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1761 f'{self._ymap.ndim-1}) in Fit.__init__: setting transpose to False')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1762 elif any(i not in transpose for i in range(len(transpose))):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1763 logging.warning(f'Invalid index in transpose ({transpose}) '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1764 f'in Fit.__init__: setting transpose to False')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1765 elif not all(i==transpose[i] for i in range(self._ymap.ndim-1)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1766 self._transpose = transpose
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1767 if self._transpose is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1768 self._inv_transpose = tuple(self._transpose.index(i)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1769 for i in range(len(self._transpose)))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1770
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1771 # Flatten the map (transpose if requested)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1772 # Store the flattened map in self._ymap_norm, whether normalized or not
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1773 if self._transpose is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1774 self._ymap_norm = np.transpose(np.asarray(self._ymap), list(self._transpose)+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1775 [len(self._transpose)])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1776 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1777 self._ymap_norm = np.asarray(self._ymap)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1778 self._map_dim = int(self._ymap_norm.size/self._x.size)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1779 self._map_shape = self._ymap_norm.shape[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1780 self._ymap_norm = np.reshape(self._ymap_norm, (self._map_dim, self._x.size))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1781
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1782 # Check if a mask is provided
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1783 if 'mask' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1784 self._mask = kwargs.pop('mask')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1785 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1786 ymap_min = float(self._ymap_norm.min())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1787 ymap_max = float(self._ymap_norm.max())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1788 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1789 self._mask = np.asarray(self._mask).astype(bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1790 if self._x.size != self._mask.size:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1791 raise ValueError(f'Inconsistent mask dimension ({self._x.size} vs '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1792 f'{self._mask.size})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1793 ymap_masked = np.asarray(self._ymap_norm)[:,~self._mask]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1794 ymap_min = float(ymap_masked.min())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1795 ymap_max = float(ymap_masked.max())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1796
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1797 # Normalize the data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1798 self._y_range = ymap_max-ymap_min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1799 if normalize and self._y_range > 0.0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1800 self._norm = (ymap_min, self._y_range)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1801 self._ymap_norm = (self._ymap_norm-self._norm[0])/self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1802 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1803 self._redchi_cutoff *= self._y_range**2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1804 if models is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1805 if callable(models) or isinstance(models, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1806 kwargs = self.add_model(models, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1807 elif isinstance(models, (tuple, list)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1808 for model in models:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1809 kwargs = self.add_model(model, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1810 self.fit(**kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1811
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1812 @classmethod
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1813 def fit_map(cls, x, ymap, models, normalize=True, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1814 return(cls(x, ymap, models, normalize=normalize, **kwargs))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1815
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1816 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1817 def best_errors(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1818 return(self._best_errors)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1819
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1820 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1821 def best_fit(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1822 return(self._best_fit)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1823
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1824 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1825 def best_results(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1826 """Convert the input data array to a data set and add the fit results.
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1827 """
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1828 if self.best_values is None or self.best_errors is None or self.best_fit is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1829 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1830 if not have_xarray:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1831 logging.warning('Unable to load xarray module')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1832 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1833 best_values = self.best_values
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1834 best_errors = self.best_errors
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1835 if isinstance(self._ymap, xr.DataArray):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1836 best_results = self._ymap.to_dataset()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1837 dims = self._ymap.dims
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1838 fit_name = f'{self._ymap.name}_fit'
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1839 else:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1840 coords = {f'dim{n}_index':([f'dim{n}_index'], range(self._ymap.shape[n]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1841 for n in range(self._ymap.ndim-1)}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1842 coords['x'] = (['x'], self._x)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1843 dims = list(coords.keys())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1844 best_results = xr.Dataset(coords=coords)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1845 best_results['y'] = (dims, self._ymap)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1846 fit_name = 'y_fit'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1847 best_results[fit_name] = (dims, self.best_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1848 if self._mask is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1849 best_results['mask'] = self._mask
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1850 for n in range(best_values.shape[0]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1851 best_results[f'{self._best_parameters[n]}_values'] = (dims[:-1], best_values[n])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1852 best_results[f'{self._best_parameters[n]}_errors'] = (dims[:-1], best_errors[n])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1853 best_results.attrs['components'] = self.components
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1854 return(best_results)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1855
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1856 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1857 def best_values(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1858 return(self._best_values)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1859
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1860 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1861 def chisqr(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1862 logging.warning('property chisqr not defined for fit.FitMap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1863 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1864
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1865 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1866 def components(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1867 components = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1868 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1869 logging.warning('Unable to collect components in FitMap.components')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1870 return(components)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1871 for component in self._result.components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1872 if 'tmp_normalization_offset_c' in component.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1873 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1874 parameters = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1875 for name in component.param_names:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1876 if self._parameters[name].vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1877 parameters[name] = {'free': True}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1878 elif self._parameters[name].expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1879 parameters[name] = {'free': False, 'expr': self._parameters[name].expr}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1880 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1881 parameters[name] = {'free': False, 'value': self.init_parameters[name]['value']}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1882 expr = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1883 if isinstance(component, ExpressionModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1884 name = component._name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1885 if name[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1886 name = name[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1887 expr = component.expr
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1888 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1889 prefix = component.prefix
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1890 if len(prefix):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1891 if prefix[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1892 prefix = prefix[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1893 name = f'{prefix} ({component._name})'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1894 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1895 name = f'{component._name}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1896 if expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1897 components[name] = {'parameters': parameters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1898 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1899 components[name] = {'expr': expr, 'parameters': parameters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1900 return(components)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1901
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1902 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1903 def covar(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1904 logging.warning('property covar not defined for fit.FitMap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1905 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1906
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1907 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1908 def max_nfev(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1909 return(self._max_nfev)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1910
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1911 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1912 def num_func_eval(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1913 logging.warning('property num_func_eval not defined for fit.FitMap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1914 return(None)
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1915
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1916 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1917 def out_of_bounds(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1918 return(self._out_of_bounds)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1919
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1920 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1921 def redchi(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1922 return(self._redchi)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1923
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1924 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1925 def residual(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1926 if self.best_fit is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1927 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1928 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1929 return(np.asarray(self._ymap)-self.best_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1930 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1931 ymap_flat = np.reshape(np.asarray(self._ymap), (self._map_dim, self._x.size))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1932 ymap_flat_masked = ymap_flat[:,~self._mask]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1933 ymap_masked = np.reshape(ymap_flat_masked,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1934 list(self._map_shape)+[ymap_flat_masked.shape[-1]])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1935 return(ymap_masked-self.best_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1936
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1937 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1938 def success(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1939 return(self._success)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1940
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1941 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1942 def var_names(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1943 logging.warning('property var_names not defined for fit.FitMap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1944 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1945
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1946 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1947 def y(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1948 logging.warning('property y not defined for fit.FitMap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1949 return(None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1950
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1951 @property
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1952 def ymap(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1953 return(self._ymap)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1954
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1955 def best_parameters(self, dims=None):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1956 if dims is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1957 return(self._best_parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1958 if not isinstance(dims, (list, tuple)) or len(dims) != len(self._map_shape):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1959 illegal_value(dims, 'dims', 'FitMap.best_parameters', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1960 if self.best_values is None or self.best_errors is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1961 logging.warning(f'Unable to obtain best parameter values for dims = {dims} in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1962 'FitMap.best_parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1963 return({})
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1964 # Create current parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1965 parameters = deepcopy(self._parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1966 for n, name in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1967 if self._parameters[name].vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1968 parameters[name].set(value=self.best_values[n][dims])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1969 parameters[name].stderr = self.best_errors[n][dims]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1970 parameters_dict = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1971 for name in sorted(parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1972 if name != 'tmp_normalization_offset_c':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1973 par = parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1974 parameters_dict[name] = {'value': par.value, 'error': par.stderr,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1975 'init_value': self.init_parameters[name]['value'], 'min': par.min,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1976 'max': par.max, 'vary': par.vary, 'expr': par.expr}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1977 return(parameters_dict)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1978
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1979 def freemem(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1980 if self._memfolder is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1981 return
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1982 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1983 rmtree(self._memfolder)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1984 self._memfolder = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1985 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1986 logging.warning('Could not clean-up automatically.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1987
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1988 def plot(self, dims, y_title=None, plot_residual=False, plot_comp_legends=False,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1989 plot_masked_data=True):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1990 if not isinstance(dims, (list, tuple)) or len(dims) != len(self._map_shape):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1991 illegal_value(dims, 'dims', 'FitMap.plot', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1992 if self._result is None or self.best_fit is None or self.best_values is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1993 logging.warning(f'Unable to plot fit for dims = {dims} in FitMap.plot')
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
1994 return
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1995 if y_title is None or not isinstance(y_title, str):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1996 y_title = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1997 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1998 mask = np.zeros(self._x.size).astype(bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
1999 plot_masked_data = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2000 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2001 mask = self._mask
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2002 plots = [(self._x, np.asarray(self._ymap[dims]), 'b.')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2003 legend = [y_title]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2004 if plot_masked_data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2005 plots += [(self._x[mask], np.asarray(self._ymap)[(*dims,mask)], 'bx')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2006 legend += ['masked data']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2007 plots += [(self._x[~mask], self.best_fit[dims], 'k-')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2008 legend += ['best fit']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2009 if plot_residual:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2010 plots += [(self._x[~mask], self.residual[dims], 'k--')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2011 legend += ['residual']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2012 # Create current parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2013 parameters = deepcopy(self._parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2014 for name in self._best_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2015 if self._parameters[name].vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2016 parameters[name].set(value=
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2017 self.best_values[self._best_parameters.index(name)][dims])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2018 for component in self._result.components:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2019 if 'tmp_normalization_offset_c' in component.param_names:
49
26f99fdd8d61 "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
rv43
parents:
diff changeset
2020 continue
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2021 if isinstance(component, ExpressionModel):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2022 prefix = component._name
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2023 if prefix[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2024 prefix = prefix[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2025 modelname = f'{prefix}: {component.expr}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2026 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2027 prefix = component.prefix
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2028 if len(prefix):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2029 if prefix[-1] == '_':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2030 prefix = prefix[:-1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2031 modelname = f'{prefix} ({component._name})'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2032 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2033 modelname = f'{component._name}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2034 if len(modelname) > 20:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2035 modelname = f'{modelname[0:16]} ...'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2036 y = component.eval(params=parameters, x=self._x[~mask])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2037 if isinstance(y, (int, float)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2038 y *= np.ones(self._x[~mask].size)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2039 plots += [(self._x[~mask], y, '--')]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2040 if plot_comp_legends:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2041 legend.append(modelname)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2042 quick_plot(tuple(plots), legend=legend, title=str(dims), block=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2043
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2044 def fit(self, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2045 # t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2046 # Check input parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2047 if self._model is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2048 logging.error('Undefined fit model')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2049 if 'num_proc' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2050 num_proc = kwargs.pop('num_proc')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2051 if not is_int(num_proc, ge=1):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2052 illegal_value(num_proc, 'num_proc', 'FitMap.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2053 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2054 num_proc = cpu_count()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2055 if num_proc > 1 and not have_joblib:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2056 logging.warning(f'Missing joblib in the conda environment, running FitMap serially')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2057 num_proc = 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2058 if num_proc > cpu_count():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2059 logging.warning(f'The requested number of processors ({num_proc}) exceeds the maximum '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2060 f'number of processors, num_proc reduced to ({cpu_count()})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2061 num_proc = cpu_count()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2062 if 'try_no_bounds' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2063 self._try_no_bounds = kwargs.pop('try_no_bounds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2064 if not isinstance(self._try_no_bounds, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2065 illegal_value(self._try_no_bounds, 'try_no_bounds', 'FitMap.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2066 if 'redchi_cutoff' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2067 self._redchi_cutoff = kwargs.pop('redchi_cutoff')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2068 if not is_num(self._redchi_cutoff, gt=0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2069 illegal_value(self._redchi_cutoff, 'redchi_cutoff', 'FitMap.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2070 if 'print_report' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2071 self._print_report = kwargs.pop('print_report')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2072 if not isinstance(self._print_report, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2073 illegal_value(self._print_report, 'print_report', 'FitMap.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2074 if 'plot' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2075 self._plot = kwargs.pop('plot')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2076 if not isinstance(self._plot, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2077 illegal_value(self._plot, 'plot', 'FitMap.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2078 if 'skip_init' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2079 self._skip_init = kwargs.pop('skip_init')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2080 if not isinstance(self._skip_init, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2081 illegal_value(self._skip_init, 'skip_init', 'FitMap.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2082
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2083 # Apply mask if supplied:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2084 if 'mask' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2085 self._mask = kwargs.pop('mask')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2086 if self._mask is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2087 self._mask = np.asarray(self._mask).astype(bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2088 if self._x.size != self._mask.size:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2089 raise ValueError(f'Inconsistent x and mask dimensions ({self._x.size} vs '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2090 f'{self._mask.size})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2091
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2092 # Add constant offset for a normalized single component model
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2093 if self._result is None and self._norm is not None and self._norm[0]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2094 self.add_model('constant', prefix='tmp_normalization_offset_', parameters={'name': 'c',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2095 'value': -self._norm[0], 'vary': False, 'norm': True})
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2096 #'value': -self._norm[0]/self._norm[1], 'vary': False, 'norm': False})
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2097
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2098 # Adjust existing parameters for refit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2099 if 'parameters' in kwargs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2100 # print('\nIn FitMap before adjusting existing parameters for refit:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2101 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2102 # if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2103 # raise ValueError('Invalid parameter parameters ({parameters})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2104 # if self._best_values is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2105 # raise ValueError('Valid self._best_values required for refitting in FitMap.fit')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2106 parameters = kwargs.pop('parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2107 # print(f'\nparameters:\n{parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2108 if isinstance(parameters, dict):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2109 parameters = (parameters, )
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2110 elif not is_dict_series(parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2111 illegal_value(parameters, 'parameters', 'Fit.fit', raise_error=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2112 for par in parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2113 name = par['name']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2114 if name not in self._parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2115 raise ValueError(f'Unable to match {name} parameter {par} to an existing one')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2116 if self._parameters[name].expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2117 raise ValueError(f'Unable to modify {name} parameter {par} (currently an '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2118 'expression)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2119 value = par.get('value')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2120 vary = par.get('vary')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2121 if par.get('expr') is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2122 raise KeyError(f'Illegal "expr" key in {name} parameter {par}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2123 self._parameters[name].set(value=value, vary=vary, min=par.get('min'),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2124 max=par.get('max'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2125 # Overwrite existing best values for fixed parameters when a value is specified
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2126 # print(f'best values befored resetting:\n{self._best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2127 if isinstance(value, (int, float)) and vary is False:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2128 for i, nname in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2129 if nname == name:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2130 self._best_values[i] = value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2131 # print(f'best values after resetting (value={value}, vary={vary}):\n{self._best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2132 #RV print('\nIn FitMap after adjusting existing parameters for refit:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2133 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2134
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2135 # Check for uninitialized parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2136 for name, par in self._parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2137 if par.expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2138 value = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2139 if value is None or np.isinf(value) or np.isnan(value):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2140 value = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2141 if self._norm is None or name not in self._parameter_norms:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2142 self._parameters[name].set(value=value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2143 elif self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2144 self._parameters[name].set(value=value*self._norm[1])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2145
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2146 # Create the best parameter list, consisting of all varying parameters plus the expression
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2147 # parameters in order to collect their errors
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2148 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2149 # Initial fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2150 assert(self._best_parameters is None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2151 self._best_parameters = [name for name, par in self._parameters.items()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2152 if par.vary or par.expr is not None]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2153 num_new_parameters = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2154 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2155 # Refit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2156 assert(len(self._best_parameters))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2157 self._new_parameters = [name for name, par in self._parameters.items()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2158 if name != 'tmp_normalization_offset_c' and name not in self._best_parameters and
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2159 (par.vary or par.expr is not None)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2160 num_new_parameters = len(self._new_parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2161 num_best_parameters = len(self._best_parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2162
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2163 # Flatten and normalize the best values of the previous fit, remove the remaining results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2164 # of the previous fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2165 if self._result is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2166 # print('\nBefore flatten and normalize:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2167 # print(f'self._best_values:\n{self._best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2168 self._out_of_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2169 self._max_nfev = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2170 self._redchi = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2171 self._success = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2172 self._best_fit = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2173 self._best_errors = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2174 assert(self._best_values is not None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2175 assert(self._best_values.shape[0] == num_best_parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2176 assert(self._best_values.shape[1:] == self._map_shape)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2177 if self._transpose is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2178 self._best_values = np.transpose(self._best_values,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2179 [0]+[i+1 for i in self._transpose])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2180 self._best_values = [np.reshape(self._best_values[i], self._map_dim)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2181 for i in range(num_best_parameters)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2182 if self._norm is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2183 for i, name in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2184 if self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2185 self._best_values[i] /= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2186 #RV print('\nAfter flatten and normalize:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2187 #RV print(f'self._best_values:\n{self._best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2188
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2189 # Normalize the initial parameters (and best values for a refit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2190 # print('\nIn FitMap before normalize:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2191 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2192 # print(f'\nparameter_norms:\n{self._parameter_norms}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2193 self._normalize()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2194 # print('\nIn FitMap after normalize:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2195 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2196 # print(f'\nparameter_norms:\n{self._parameter_norms}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2197
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2198 # Prevent initial values from sitting at boundaries
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2199 self._parameter_bounds = {name:{'min': par.min, 'max': par.max}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2200 for name, par in self._parameters.items() if par.vary}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2201 for name, par in self._parameters.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2202 if par.vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2203 par.set(value=self._reset_par_at_boundary(par, par.value))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2204 # print('\nAfter checking boundaries:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2205 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2206
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2207 # Set parameter bounds to unbound (only use bounds when fit fails)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2208 if self._try_no_bounds:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2209 for name in self._parameter_bounds.keys():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2210 self._parameters[name].set(min=-np.inf, max=np.inf)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2211
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2212 # Allocate memory to store fit results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2213 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2214 x_size = self._x.size
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2215 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2216 x_size = self._x[~self._mask].size
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2217 if num_proc == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2218 self._out_of_bounds_flat = np.zeros(self._map_dim, dtype=bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2219 self._max_nfev_flat = np.zeros(self._map_dim, dtype=bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2220 self._redchi_flat = np.zeros(self._map_dim, dtype=np.float64)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2221 self._success_flat = np.zeros(self._map_dim, dtype=bool)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2222 self._best_fit_flat = np.zeros((self._map_dim, x_size),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2223 dtype=self._ymap_norm.dtype)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2224 self._best_errors_flat = [np.zeros(self._map_dim, dtype=np.float64)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2225 for _ in range(num_best_parameters+num_new_parameters)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2226 if self._result is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2227 self._best_values_flat = [np.zeros(self._map_dim, dtype=np.float64)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2228 for _ in range(num_best_parameters)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2229 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2230 self._best_values_flat = self._best_values
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2231 self._best_values_flat += [np.zeros(self._map_dim, dtype=np.float64)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2232 for _ in range(num_new_parameters)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2233 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2234 self._memfolder = './joblib_memmap'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2235 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2236 mkdir(self._memfolder)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2237 except FileExistsError:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2238 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2239 filename_memmap = path.join(self._memfolder, 'out_of_bounds_memmap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2240 self._out_of_bounds_flat = np.memmap(filename_memmap, dtype=bool,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2241 shape=(self._map_dim), mode='w+')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2242 filename_memmap = path.join(self._memfolder, 'max_nfev_memmap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2243 self._max_nfev_flat = np.memmap(filename_memmap, dtype=bool,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2244 shape=(self._map_dim), mode='w+')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2245 filename_memmap = path.join(self._memfolder, 'redchi_memmap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2246 self._redchi_flat = np.memmap(filename_memmap, dtype=np.float64,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2247 shape=(self._map_dim), mode='w+')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2248 filename_memmap = path.join(self._memfolder, 'success_memmap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2249 self._success_flat = np.memmap(filename_memmap, dtype=bool,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2250 shape=(self._map_dim), mode='w+')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2251 filename_memmap = path.join(self._memfolder, 'best_fit_memmap')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2252 self._best_fit_flat = np.memmap(filename_memmap, dtype=self._ymap_norm.dtype,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2253 shape=(self._map_dim, x_size), mode='w+')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2254 self._best_errors_flat = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2255 for i in range(num_best_parameters+num_new_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2256 filename_memmap = path.join(self._memfolder, f'best_errors_memmap_{i}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2257 self._best_errors_flat.append(np.memmap(filename_memmap, dtype=np.float64,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2258 shape=self._map_dim, mode='w+'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2259 self._best_values_flat = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2260 for i in range(num_best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2261 filename_memmap = path.join(self._memfolder, f'best_values_memmap_{i}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2262 self._best_values_flat.append(np.memmap(filename_memmap, dtype=np.float64,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2263 shape=self._map_dim, mode='w+'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2264 if self._result is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2265 self._best_values_flat[i][:] = self._best_values[i][:]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2266 for i in range(num_new_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2267 filename_memmap = path.join(self._memfolder,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2268 f'best_values_memmap_{i+num_best_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2269 self._best_values_flat.append(np.memmap(filename_memmap, dtype=np.float64,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2270 shape=self._map_dim, mode='w+'))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2271
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2272 # Update the best parameter list
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2273 if num_new_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2274 self._best_parameters += self._new_parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2275
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2276 # Perform the first fit to get model component info and initial parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2277 current_best_values = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2278 # print(f'0 before:\n{current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2279 # t1 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2280 self._result = self._fit(0, current_best_values, return_result=True, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2281 # t2 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2282 # print(f'0 after:\n{current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2283 # print('\nAfter the first fit:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2284 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2285 # print(self._result.fit_report(show_correl=False))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2286
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2287 # Remove all irrelevant content from self._result
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2288 for attr in ('_abort', 'aborted', 'aic', 'best_fit', 'best_values', 'bic', 'calc_covar',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2289 'call_kws', 'chisqr', 'ci_out', 'col_deriv', 'covar', 'data', 'errorbars',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2290 'flatchain', 'ier', 'init_vals', 'init_fit', 'iter_cb', 'jacfcn', 'kws',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2291 'last_internal_values', 'lmdif_message', 'message', 'method', 'nan_policy',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2292 'ndata', 'nfev', 'nfree', 'params', 'redchi', 'reduce_fcn', 'residual', 'result',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2293 'scale_covar', 'show_candidates', 'calc_covar', 'success', 'userargs', 'userfcn',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2294 'userkws', 'values', 'var_names', 'weights', 'user_options'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2295 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2296 delattr(self._result, attr)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2297 except AttributeError:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2298 # logging.warning(f'Unknown attribute {attr} in fit.FtMap._cleanup_result')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2299 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2300
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2301 # t3 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2302 if num_proc == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2303 # Perform the remaining fits serially
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2304 for n in range(1, self._map_dim):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2305 # print(f'{n} before:\n{current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2306 self._fit(n, current_best_values, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2307 # print(f'{n} after:\n{current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2308 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2309 # Perform the remaining fits in parallel
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2310 num_fit = self._map_dim-1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2311 # print(f'num_fit = {num_fit}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2312 if num_proc > num_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2313 logging.warning(f'The requested number of processors ({num_proc}) exceeds the '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2314 f'number of fits, num_proc reduced to ({num_fit})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2315 num_proc = num_fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2316 num_fit_per_proc = 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2317 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2318 num_fit_per_proc = round((num_fit)/num_proc)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2319 if num_proc*num_fit_per_proc < num_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2320 num_fit_per_proc +=1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2321 # print(f'num_fit_per_proc = {num_fit_per_proc}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2322 num_fit_batch = min(num_fit_per_proc, 40)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2323 # print(f'num_fit_batch = {num_fit_batch}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2324 with Parallel(n_jobs=num_proc) as parallel:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2325 parallel(delayed(self._fit_parallel)(current_best_values, num_fit_batch,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2326 n_start, **kwargs) for n_start in range(1, self._map_dim, num_fit_batch))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2327 # t4 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2328
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2329 # Renormalize the initial parameters for external use
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2330 if self._norm is not None and self._normalized:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2331 init_values = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2332 for name, value in self._result.init_values.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2333 if name not in self._parameter_norms or self._parameters[name].expr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2334 init_values[name] = value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2335 elif self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2336 init_values[name] = value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2337 self._result.init_values = init_values
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2338 for name, par in self._result.init_params.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2339 if par.expr is None and self._parameter_norms.get(name, False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2340 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2341 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2342 value = par.value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2343 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2344 _min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2345 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2346 _max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2347 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2348 par.init_value = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2349
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2350 # Remap the best results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2351 # t5 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2352 self._out_of_bounds = np.copy(np.reshape(self._out_of_bounds_flat, self._map_shape))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2353 self._max_nfev = np.copy(np.reshape(self._max_nfev_flat, self._map_shape))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2354 self._redchi = np.copy(np.reshape(self._redchi_flat, self._map_shape))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2355 self._success = np.copy(np.reshape(self._success_flat, self._map_shape))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2356 self._best_fit = np.copy(np.reshape(self._best_fit_flat,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2357 list(self._map_shape)+[x_size]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2358 self._best_values = np.asarray([np.reshape(par, list(self._map_shape))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2359 for par in self._best_values_flat])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2360 self._best_errors = np.asarray([np.reshape(par, list(self._map_shape))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2361 for par in self._best_errors_flat])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2362 if self._inv_transpose is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2363 self._out_of_bounds = np.transpose(self._out_of_bounds, self._inv_transpose)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2364 self._max_nfev = np.transpose(self._max_nfev, self._inv_transpose)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2365 self._redchi = np.transpose(self._redchi, self._inv_transpose)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2366 self._success = np.transpose(self._success, self._inv_transpose)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2367 self._best_fit = np.transpose(self._best_fit,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2368 list(self._inv_transpose)+[len(self._inv_transpose)])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2369 self._best_values = np.transpose(self._best_values,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2370 [0]+[i+1 for i in self._inv_transpose])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2371 self._best_errors = np.transpose(self._best_errors,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2372 [0]+[i+1 for i in self._inv_transpose])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2373 del self._out_of_bounds_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2374 del self._max_nfev_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2375 del self._redchi_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2376 del self._success_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2377 del self._best_fit_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2378 del self._best_values_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2379 del self._best_errors_flat
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2380 # t6 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2381
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2382 # Restore parameter bounds and renormalize the parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2383 for name, par in self._parameter_bounds.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2384 self._parameters[name].set(min=par['min'], max=par['max'])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2385 self._normalized = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2386 if self._norm is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2387 for name, norm in self._parameter_norms.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2388 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2389 if par.expr is None and norm:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2390 value = par.value*self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2391 _min = par.min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2392 _max = par.max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2393 if not np.isinf(_min) and abs(_min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2394 _min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2395 if not np.isinf(_max) and abs(_max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2396 _max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2397 par.set(value=value, min=_min, max=_max)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2398 # t7 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2399 # print(f'total run time in fit: {t7-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2400 # print(f'run time first fit: {t2-t1:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2401 # print(f'run time remaining fits: {t4-t3:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2402 # print(f'run time remapping results: {t6-t5:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2403
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2404 # print('\n\nAt end fit:')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2405 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2406 # print(f'self._best_values:\n{self._best_values}\n\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2407
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2408 # Free the shared memory
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2409 self.freemem()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2410
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2411 def _fit_parallel(self, current_best_values, num, n_start, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2412 num = min(num, self._map_dim-n_start)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2413 for n in range(num):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2414 # print(f'{n_start+n} before:\n{current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2415 self._fit(n_start+n, current_best_values, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2416 # print(f'{n_start+n} after:\n{current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2417
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2418 def _fit(self, n, current_best_values, return_result=False, **kwargs):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2419 #RV print(f'\n\nstart FitMap._fit {n}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2420 #RV print(f'current_best_values = {current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2421 #RV print(f'self._best_parameters = {self._best_parameters}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2422 #RV print(f'self._new_parameters = {self._new_parameters}\n\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2423 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2424 # Set parameters to current best values, but prevent them from sitting at boundaries
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2425 if self._new_parameters is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2426 # Initial fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2427 for name, value in current_best_values.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2428 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2429 par.set(value=self._reset_par_at_boundary(par, value))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2430 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2431 # Refit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2432 for i, name in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2433 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2434 if name in self._new_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2435 if name in current_best_values:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2436 par.set(value=self._reset_par_at_boundary(par, current_best_values[name]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2437 elif par.expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2438 par.set(value=self._best_values[i][n])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2439 #RV print(f'\nbefore fit {n}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2440 #RV self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2441 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2442 result = self._model.fit(self._ymap_norm[n], self._parameters, x=self._x, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2443 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2444 result = self._model.fit(self._ymap_norm[n][~self._mask], self._parameters,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2445 x=self._x[~self._mask], **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2446 # print(f'\nafter fit {n}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2447 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2448 # print(result.fit_report(show_correl=False))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2449 out_of_bounds = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2450 for name, par in self._parameter_bounds.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2451 value = result.params[name].value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2452 if not np.isinf(par['min']) and value < par['min']:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2453 out_of_bounds = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2454 break
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2455 if not np.isinf(par['max']) and value > par['max']:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2456 out_of_bounds = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2457 break
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2458 self._out_of_bounds_flat[n] = out_of_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2459 if self._try_no_bounds and out_of_bounds:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2460 # Rerun fit with parameter bounds in place
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2461 for name, par in self._parameter_bounds.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2462 self._parameters[name].set(min=par['min'], max=par['max'])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2463 # Set parameters to current best values, but prevent them from sitting at boundaries
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2464 if self._new_parameters is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2465 # Initial fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2466 for name, value in current_best_values.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2467 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2468 par.set(value=self._reset_par_at_boundary(par, value))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2469 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2470 # Refit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2471 for i, name in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2472 par = self._parameters[name]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2473 if name in self._new_parameters:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2474 if name in current_best_values:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2475 par.set(value=self._reset_par_at_boundary(par,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2476 current_best_values[name]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2477 elif par.expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2478 par.set(value=self._best_values[i][n])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2479 # print('\nbefore fit')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2480 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2481 # print(result.fit_report(show_correl=False))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2482 if self._mask is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2483 result = self._model.fit(self._ymap_norm[n], self._parameters, x=self._x, **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2484 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2485 result = self._model.fit(self._ymap_norm[n][~self._mask], self._parameters,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2486 x=self._x[~self._mask], **kwargs)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2487 # print(f'\nafter fit {n}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2488 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2489 # print(result.fit_report(show_correl=False))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2490 out_of_bounds = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2491 for name, par in self._parameter_bounds.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2492 value = result.params[name].value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2493 if not np.isinf(par['min']) and value < par['min']:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2494 out_of_bounds = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2495 break
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2496 if not np.isinf(par['max']) and value > par['max']:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2497 out_of_bounds = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2498 break
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2499 # print(f'{n} redchi < redchi_cutoff = {result.redchi < self._redchi_cutoff} success = {result.success} out_of_bounds = {out_of_bounds}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2500 # Reset parameters back to unbound
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2501 for name in self._parameter_bounds.keys():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2502 self._parameters[name].set(min=-np.inf, max=np.inf)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2503 assert(not out_of_bounds)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2504 if result.redchi >= self._redchi_cutoff:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2505 result.success = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2506 if result.nfev == result.max_nfev:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2507 # print(f'Maximum number of function evaluations reached for n = {n}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2508 # logging.warning(f'Maximum number of function evaluations reached for n = {n}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2509 if result.redchi < self._redchi_cutoff:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2510 result.success = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2511 self._max_nfev_flat[n] = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2512 if result.success:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2513 assert(all(True for par in current_best_values if par in result.params.values()))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2514 for par in result.params.values():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2515 if par.vary:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2516 current_best_values[par.name] = par.value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2517 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2518 logging.warning(f'Fit for n = {n} failed: {result.lmdif_message}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2519 # Renormalize the data and results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2520 self._renormalize(n, result)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2521 if self._print_report:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2522 print(result.fit_report(show_correl=False))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2523 if self._plot:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2524 dims = np.unravel_index(n, self._map_shape)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2525 if self._inv_transpose is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2526 dims= tuple(dims[self._inv_transpose[i]] for i in range(len(dims)))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2527 super().plot(result=result, y=np.asarray(self._ymap[dims]), plot_comp_legends=True,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2528 skip_init=self._skip_init, title=str(dims))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2529 #RV print(f'\n\nend FitMap._fit {n}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2530 #RV print(f'current_best_values = {current_best_values}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2531 # self._parameters.pretty_print()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2532 # print(result.fit_report(show_correl=False))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2533 #RV print(f'\nself._best_values_flat:\n{self._best_values_flat}\n\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2534 if return_result:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2535 return(result)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2536
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2537 def _renormalize(self, n, result):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2538 self._redchi_flat[n] = np.float64(result.redchi)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2539 self._success_flat[n] = result.success
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2540 if self._norm is None or not self._normalized:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2541 self._best_fit_flat[n] = result.best_fit
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2542 for i, name in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2543 self._best_values_flat[i][n] = np.float64(result.params[name].value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2544 self._best_errors_flat[i][n] = np.float64(result.params[name].stderr)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2545 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2546 pars = set(self._parameter_norms) & set(self._best_parameters)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2547 for name, par in result.params.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2548 if name in pars and self._parameter_norms[name]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2549 if par.stderr is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2550 par.stderr *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2551 if par.expr is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2552 par.value *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2553 if self._print_report:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2554 if par.init_value is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2555 par.init_value *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2556 if not np.isinf(par.min) and abs(par.min) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2557 par.min *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2558 if not np.isinf(par.max) and abs(par.max) != float_min:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2559 par.max *= self._norm[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2560 self._best_fit_flat[n] = result.best_fit*self._norm[1]+self._norm[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2561 for i, name in enumerate(self._best_parameters):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2562 self._best_values_flat[i][n] = np.float64(result.params[name].value)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2563 self._best_errors_flat[i][n] = np.float64(result.params[name].stderr)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2564 if self._plot:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2565 if not self._skip_init:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2566 result.init_fit = result.init_fit*self._norm[1]+self._norm[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents: 65
diff changeset
2567 result.best_fit = np.copy(self._best_fit_flat[n])