nxs-analysis-tools 0.0.47__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nxs-analysis-tools might be problematic. Click here for more details.

@@ -1,259 +1,259 @@
1
- """
2
- Module for fitting of linecuts using the lmfit package.
3
- """
4
-
5
- import operator
6
- from lmfit.model import Model
7
- from lmfit.model import CompositeModel
8
- import matplotlib.pyplot as plt
9
- import numpy as np
10
-
11
-
12
- class LinecutModel:
13
- """
14
- A class representing a linecut model for data analysis and fitting.
15
-
16
- Attributes
17
- ----------
18
- y : array-like or None
19
- The dependent variable data.
20
- x : array-like or None
21
- The independent variable data.
22
- y_eval : array-like or None
23
- The evaluated y-values of the fitted model.
24
- x_eval : array-like or None
25
- The x-values used for evaluation.
26
- y_eval_components : dict or None
27
- The evaluated y-values of the model components.
28
- y_fit_components : dict or None
29
- The fitted y-values of the model components.
30
- y_fit : array-like or None
31
- The fitted y-values of the model.
32
- x_fit : array-like or None
33
- The x-values used for fitting.
34
- y_init_fit : array-like or None
35
- The initial guess of the y-values.
36
- params : Parameters or None
37
- The parameters of the model.
38
- model_components : Model or list of Models or None
39
- The model component(s) used for fitting.
40
- model : Model or None
41
- The composite model used for fitting.
42
- modelresult : ModelResult or None
43
- The result of the model fitting.
44
- data : NXdata or None
45
- The 1D linecut data used for analysis.
46
-
47
- Methods
48
- -------
49
- __init__(self, data=None)
50
- Initialize the LinecutModel.
51
- set_data(self, data)
52
- Set the data for analysis.
53
- set_model_components(self, model_components)
54
- Set the model components.
55
- set_param_hint(self, *args, **kwargs)
56
- Set parameter hints for the model.
57
- make_params(self)
58
- Create and initialize the parameters for the model.
59
- guess(self)
60
- Perform initial guesses for each model component.
61
- print_initial_params(self)
62
- Print out initial guesses for each parameter of the model.
63
- plot_initial_guess(self, numpoints=None)
64
- Plot the initial guess.
65
- fit(self)
66
- Fit the model to the data.
67
- plot_fit(self, numpoints=None, fit_report=True, **kwargs)
68
- Plot the fitted model.
69
- print_fit_report(self)
70
- Print the fit report.
71
- """
72
- def __init__(self, data=None):
73
- """
74
- Initialize the LinecutModel.
75
- """
76
- self.y = None
77
- self.x = None
78
- self.y_eval = None
79
- self.x_eval = None
80
- self.y_eval_components = None
81
- self.y_fit_components = None
82
- self.y_fit = None
83
- self.x_fit = None
84
- self.y_init_fit = None
85
- self.params = None
86
- self.model_components = None
87
- self.model = None
88
- self.modelresult = None
89
- self.data = data if data is not None else None
90
- if self.data is not None:
91
- self.x = data[data.axes].nxdata
92
- self.y = data[data.signal].nxdata
93
-
94
- def set_data(self, data):
95
- """
96
- Set the data for analysis.
97
-
98
- Parameters
99
- ----------
100
- data : NXdata
101
- The 1D linecut data to be used for analysis.
102
- """
103
- self.data = data
104
- self.x = data[data.axes].nxdata
105
- self.y = data[data.signal].nxdata
106
-
107
- def set_model_components(self, model_components):
108
- """
109
- Set the model components
110
-
111
- Parameters
112
- ----------
113
- model_components : Model or list of Models
114
- The model component(s) to be used for fitting,
115
- which will be combined into a CompositeModel.
116
- """
117
-
118
- # If the model only has one component, then use it as the model
119
- if isinstance(model_components, Model):
120
- self.model = model_components
121
- # Else, combine the components into a composite model and use that as the
122
- else:
123
- self.model_components = model_components
124
- self.model = model_components[0]
125
-
126
- # Combine remaining components into the composite model
127
- for component in model_components[1:]:
128
- self.model = CompositeModel(self.model, component, operator.add)
129
-
130
- def set_param_hint(self, *args, **kwargs):
131
- """
132
- Set parameter hints for the model.
133
-
134
- Parameters
135
- ----------
136
- *args : positional arguments
137
- Positional arguments passed to the `set_param_hint` method of the underlying model.
138
-
139
- **kwargs : keyword arguments
140
- Keyword arguments passed to the `set_param_hint` method of the underlying model.
141
- """
142
-
143
- self.model.set_param_hint(*args, **kwargs)
144
-
145
- def make_params(self):
146
- """
147
- Create and initialize the parameters for the model.
148
-
149
- Returns
150
- -------
151
- Parameters
152
- The initialized parameters for the model.
153
- """
154
- # Intialize empty parameters (in function)
155
- params = self.model.make_params()
156
- self.params = params
157
-
158
- return params
159
-
160
- def guess(self):
161
- """
162
- Perform initial guesses for each model component.
163
- """
164
- for model_component in list(self.model_components):
165
- self.params.update(model_component.guess(self.y, x=self.x))
166
-
167
- def print_initial_params(self):
168
- """
169
- Print out initial guesses for each parameter of the model.
170
- """
171
- model = self.model
172
- for param, hint in model.param_hints.items():
173
- print(f'{param}')
174
- for key, value in hint.items():
175
- print(f'\t{key}: {value}')
176
-
177
- def plot_initial_guess(self, numpoints=None):
178
- """
179
- Plot initial guess.
180
- """
181
- model = self.model
182
- params = self.params
183
- x = self.x
184
- y = self.y
185
- y_init_fit = model.eval(params=params, x=x)
186
- self.y_init_fit = y_init_fit
187
- plt.plot(x, y, 'o', label='data')
188
- plt.plot(x, y_init_fit, '--', label='guess')
189
-
190
- # Plot the components of the model
191
- if numpoints is None:
192
- numpoints = len(self.x)
193
- self.x_eval = np.linspace(self.x.min(), self.x.max(), numpoints)
194
- y_init_fit_components = model.eval_components(params=params, x=self.x_eval)
195
- ax = plt.gca()
196
- for model_component, value in y_init_fit_components.items():
197
- ax.fill_between(self.x_eval, value, alpha=0.3, label=model_component)
198
- plt.legend()
199
- plt.show()
200
-
201
- def fit(self):
202
- """
203
- Fit the model to the data.
204
-
205
- This method fits the model to the data using the specified parameters and the x-values.
206
- It updates the model result, fitted y-values, and the evaluated components.
207
-
208
- """
209
- self.modelresult = self.model.fit(self.y, self.params, x=self.x)
210
- self.y_fit = self.modelresult.eval(x=self.x)
211
- self.y_fit_components = self.modelresult.eval_components(x=self.x)
212
-
213
- def plot_fit(self, numpoints=None, fit_report=True, **kwargs):
214
- """
215
- Plot the fitted model.
216
-
217
- This method plots the fitted model along with the original data.
218
- It evaluates the model and its components at the specified number of points (numpoints)
219
- and plots the results.
220
-
221
- Parameters
222
- ----------
223
- numpoints : int, optional
224
- Number of points to evaluate the model and its components. If not provided,
225
- it defaults to the length of the x-values.
226
-
227
- fit_report : bool, optional
228
- Whether to print the fit report. Default is True.
229
-
230
- **kwargs : dict, optional
231
- Additional keyword arguments to be passed to the `plot` method.
232
-
233
- Returns
234
- -------
235
- ax : matplotlib.axes.Axes
236
- The Axes object containing the plot.
237
-
238
- """
239
- if numpoints is None:
240
- numpoints = len(self.x)
241
- self.x_eval = np.linspace(self.x.min(), self.x.max(), numpoints)
242
- self.y_eval = self.modelresult.eval(x=self.x_eval)
243
- self.y_eval_components = self.modelresult.eval_components(x=self.x_eval)
244
- self.modelresult.plot(numpoints=numpoints, **kwargs)
245
- ax = plt.gca()
246
- for model_component, value in self.y_eval_components.items():
247
- ax.fill_between(self.x_eval, value, alpha=0.3, label=model_component)
248
- # ax.plot(self.x_eval, value, label=model_component)
249
- plt.legend()
250
- plt.show()
251
- if fit_report:
252
- print(self.modelresult.fit_report())
253
- return ax
254
-
255
- def print_fit_report(self):
256
- """
257
- Show fit report.
258
- """
259
- print(self.modelresult.fit_report())
1
+ """
2
+ Module for fitting of linecuts using the lmfit package.
3
+ """
4
+
5
+ import operator
6
+ from lmfit.model import Model
7
+ from lmfit.model import CompositeModel
8
+ import matplotlib.pyplot as plt
9
+ import numpy as np
10
+
11
+
12
+ class LinecutModel:
13
+ """
14
+ A class representing a linecut model for data analysis and fitting.
15
+
16
+ Attributes
17
+ ----------
18
+ y : array-like or None
19
+ The dependent variable data.
20
+ x : array-like or None
21
+ The independent variable data.
22
+ y_eval : array-like or None
23
+ The evaluated y-values of the fitted model.
24
+ x_eval : array-like or None
25
+ The x-values used for evaluation.
26
+ y_eval_components : dict or None
27
+ The evaluated y-values of the model components.
28
+ y_fit_components : dict or None
29
+ The fitted y-values of the model components.
30
+ y_fit : array-like or None
31
+ The fitted y-values of the model.
32
+ x_fit : array-like or None
33
+ The x-values used for fitting.
34
+ y_init_fit : array-like or None
35
+ The initial guess of the y-values.
36
+ params : Parameters or None
37
+ The parameters of the model.
38
+ model_components : Model or list of Models or None
39
+ The model component(s) used for fitting.
40
+ model : Model or None
41
+ The composite model used for fitting.
42
+ modelresult : ModelResult or None
43
+ The result of the model fitting.
44
+ data : NXdata or None
45
+ The 1D linecut data used for analysis.
46
+
47
+ Methods
48
+ -------
49
+ __init__(self, data=None)
50
+ Initialize the LinecutModel.
51
+ set_data(self, data)
52
+ Set the data for analysis.
53
+ set_model_components(self, model_components)
54
+ Set the model components.
55
+ set_param_hint(self, *args, **kwargs)
56
+ Set parameter hints for the model.
57
+ make_params(self)
58
+ Create and initialize the parameters for the model.
59
+ guess(self)
60
+ Perform initial guesses for each model component.
61
+ print_initial_params(self)
62
+ Print out initial guesses for each parameter of the model.
63
+ plot_initial_guess(self, numpoints=None)
64
+ Plot the initial guess.
65
+ fit(self)
66
+ Fit the model to the data.
67
+ plot_fit(self, numpoints=None, fit_report=True, **kwargs)
68
+ Plot the fitted model.
69
+ print_fit_report(self)
70
+ Print the fit report.
71
+ """
72
+ def __init__(self, data=None):
73
+ """
74
+ Initialize the LinecutModel.
75
+ """
76
+ self.y = None
77
+ self.x = None
78
+ self.y_eval = None
79
+ self.x_eval = None
80
+ self.y_eval_components = None
81
+ self.y_fit_components = None
82
+ self.y_fit = None
83
+ self.x_fit = None
84
+ self.y_init_fit = None
85
+ self.params = None
86
+ self.model_components = None
87
+ self.model = None
88
+ self.modelresult = None
89
+ self.data = data if data is not None else None
90
+ if self.data is not None:
91
+ self.x = data[data.axes].nxdata
92
+ self.y = data[data.signal].nxdata
93
+
94
+ def set_data(self, data):
95
+ """
96
+ Set the data for analysis.
97
+
98
+ Parameters
99
+ ----------
100
+ data : NXdata
101
+ The 1D linecut data to be used for analysis.
102
+ """
103
+ self.data = data
104
+ self.x = data[data.axes].nxdata
105
+ self.y = data[data.signal].nxdata
106
+
107
+ def set_model_components(self, model_components):
108
+ """
109
+ Set the model components
110
+
111
+ Parameters
112
+ ----------
113
+ model_components : Model or list of Models
114
+ The model component(s) to be used for fitting,
115
+ which will be combined into a CompositeModel.
116
+ """
117
+
118
+ # If the model only has one component, then use it as the model
119
+ if isinstance(model_components, Model):
120
+ self.model = model_components
121
+ # Else, combine the components into a composite model and use that as the
122
+ else:
123
+ self.model_components = model_components
124
+ self.model = model_components[0]
125
+
126
+ # Combine remaining components into the composite model
127
+ for component in model_components[1:]:
128
+ self.model = CompositeModel(self.model, component, operator.add)
129
+
130
+ def set_param_hint(self, *args, **kwargs):
131
+ """
132
+ Set parameter hints for the model.
133
+
134
+ Parameters
135
+ ----------
136
+ *args : positional arguments
137
+ Positional arguments passed to the `set_param_hint` method of the underlying model.
138
+
139
+ **kwargs : keyword arguments
140
+ Keyword arguments passed to the `set_param_hint` method of the underlying model.
141
+ """
142
+
143
+ self.model.set_param_hint(*args, **kwargs)
144
+
145
+ def make_params(self):
146
+ """
147
+ Create and initialize the parameters for the model.
148
+
149
+ Returns
150
+ -------
151
+ Parameters
152
+ The initialized parameters for the model.
153
+ """
154
+ # Initialize empty parameters (in function)
155
+ params = self.model.make_params()
156
+ self.params = params
157
+
158
+ return params
159
+
160
+ def guess(self):
161
+ """
162
+ Perform initial guesses for each model component.
163
+ """
164
+ for model_component in list(self.model_components):
165
+ self.params.update(model_component.guess(self.y, x=self.x))
166
+
167
+ def print_initial_params(self):
168
+ """
169
+ Print out initial guesses for each parameter of the model.
170
+ """
171
+ model = self.model
172
+ for param, hint in model.param_hints.items():
173
+ print(f'{param}')
174
+ for key, value in hint.items():
175
+ print(f'\t{key}: {value}')
176
+
177
+ def plot_initial_guess(self, numpoints=None):
178
+ """
179
+ Plot initial guess.
180
+ """
181
+ model = self.model
182
+ params = self.params
183
+ x = self.x
184
+ y = self.y
185
+ y_init_fit = model.eval(params=params, x=x)
186
+ self.y_init_fit = y_init_fit
187
+ plt.plot(x, y, 'o', label='data')
188
+ plt.plot(x, y_init_fit, '--', label='guess')
189
+
190
+ # Plot the components of the model
191
+ if numpoints is None:
192
+ numpoints = len(self.x)
193
+ self.x_eval = np.linspace(self.x.min(), self.x.max(), numpoints)
194
+ y_init_fit_components = model.eval_components(params=params, x=self.x_eval)
195
+ ax = plt.gca()
196
+ for model_component, value in y_init_fit_components.items():
197
+ ax.fill_between(self.x_eval, value, alpha=0.3, label=model_component)
198
+ plt.legend()
199
+ plt.show()
200
+
201
+ def fit(self):
202
+ """
203
+ Fit the model to the data.
204
+
205
+ This method fits the model to the data using the specified parameters and the x-values.
206
+ It updates the model result, fitted y-values, and the evaluated components.
207
+
208
+ """
209
+ self.modelresult = self.model.fit(self.y, self.params, x=self.x)
210
+ self.y_fit = self.modelresult.eval(x=self.x)
211
+ self.y_fit_components = self.modelresult.eval_components(x=self.x)
212
+
213
+ def plot_fit(self, numpoints=None, fit_report=True, **kwargs):
214
+ """
215
+ Plot the fitted model.
216
+
217
+ This method plots the fitted model along with the original data.
218
+ It evaluates the model and its components at the specified number of points (numpoints)
219
+ and plots the results.
220
+
221
+ Parameters
222
+ ----------
223
+ numpoints : int, optional
224
+ Number of points to evaluate the model and its components. If not provided,
225
+ it defaults to the length of the x-values.
226
+
227
+ fit_report : bool, optional
228
+ Whether to print the fit report. Default is True.
229
+
230
+ **kwargs : dict, optional
231
+ Additional keyword arguments to be passed to the `plot` method.
232
+
233
+ Returns
234
+ -------
235
+ ax : matplotlib.axes.Axes
236
+ The Axes object containing the plot.
237
+
238
+ """
239
+ if numpoints is None:
240
+ numpoints = len(self.x)
241
+ self.x_eval = np.linspace(self.x.min(), self.x.max(), numpoints)
242
+ self.y_eval = self.modelresult.eval(x=self.x_eval)
243
+ self.y_eval_components = self.modelresult.eval_components(x=self.x_eval)
244
+ self.modelresult.plot(numpoints=numpoints, **kwargs)
245
+ ax = plt.gca()
246
+ for model_component, value in self.y_eval_components.items():
247
+ ax.fill_between(self.x_eval, value, alpha=0.3, label=model_component)
248
+ # ax.plot(self.x_eval, value, label=model_component)
249
+ plt.legend()
250
+ plt.show()
251
+ if fit_report:
252
+ print(self.modelresult.fit_report())
253
+ return ax
254
+
255
+ def print_fit_report(self):
256
+ """
257
+ Show fit report.
258
+ """
259
+ print(self.modelresult.fit_report())