annotate fit.py @ 75:d5e1d4ea2b7e draft default tip

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