annotate fit.py @ 71:1cf15b61cd83 draft

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