iquana 0.4.0__tar.gz
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.
- iquana-0.4.0/LICENSE +28 -0
- iquana-0.4.0/PKG-INFO +416 -0
- iquana-0.4.0/README.md +384 -0
- iquana-0.4.0/pyproject.toml +160 -0
- iquana-0.4.0/setup.cfg +4 -0
- iquana-0.4.0/src/iquana/__init__.py +42 -0
- iquana-0.4.0/src/iquana/console.py +126 -0
- iquana-0.4.0/src/iquana/data/__init__.py +36 -0
- iquana-0.4.0/src/iquana/data/loader.py +207 -0
- iquana-0.4.0/src/iquana/data/ops.py +269 -0
- iquana-0.4.0/src/iquana/data/provenance.py +151 -0
- iquana-0.4.0/src/iquana/data/resolution.py +368 -0
- iquana-0.4.0/src/iquana/data/weighting.py +167 -0
- iquana-0.4.0/src/iquana/examples.py +894 -0
- iquana-0.4.0/src/iquana/fileio.py +42 -0
- iquana-0.4.0/src/iquana/fitter.py +2265 -0
- iquana-0.4.0/src/iquana/fitting/__init__.py +22 -0
- iquana-0.4.0/src/iquana/fitting/base.py +227 -0
- iquana-0.4.0/src/iquana/fitting/bumps_engine.py +582 -0
- iquana-0.4.0/src/iquana/fitting/multi_bumps.py +495 -0
- iquana-0.4.0/src/iquana/fitting/scipy_engine.py +233 -0
- iquana-0.4.0/src/iquana/fitting/theory.py +123 -0
- iquana-0.4.0/src/iquana/inversion/__init__.py +63 -0
- iquana-0.4.0/src/iquana/inversion/basis.py +66 -0
- iquana-0.4.0/src/iquana/inversion/estimate.py +590 -0
- iquana-0.4.0/src/iquana/inversion/result.py +183 -0
- iquana-0.4.0/src/iquana/inversion/solver.py +517 -0
- iquana-0.4.0/src/iquana/modeling/__init__.py +14 -0
- iquana-0.4.0/src/iquana/modeling/constraints.py +1320 -0
- iquana-0.4.0/src/iquana/modeling/parameters.py +1428 -0
- iquana-0.4.0/src/iquana/modeling/polydispersity.py +208 -0
- iquana-0.4.0/src/iquana/modeling/structure_factor.py +167 -0
- iquana-0.4.0/src/iquana/multi_plotting.py +344 -0
- iquana-0.4.0/src/iquana/multi_results.py +675 -0
- iquana-0.4.0/src/iquana/multifit.py +2374 -0
- iquana-0.4.0/src/iquana/persistence.py +872 -0
- iquana-0.4.0/src/iquana/plotting.py +1105 -0
- iquana-0.4.0/src/iquana/report.py +548 -0
- iquana-0.4.0/src/iquana/reporting.py +390 -0
- iquana-0.4.0/src/iquana/results.py +471 -0
- iquana-0.4.0/src/iquana.egg-info/PKG-INFO +416 -0
- iquana-0.4.0/src/iquana.egg-info/SOURCES.txt +71 -0
- iquana-0.4.0/src/iquana.egg-info/dependency_links.txt +1 -0
- iquana-0.4.0/src/iquana.egg-info/requires.txt +25 -0
- iquana-0.4.0/src/iquana.egg-info/top_level.txt +1 -0
- iquana-0.4.0/tests/test_bayesian.py +392 -0
- iquana-0.4.0/tests/test_composite_models.py +827 -0
- iquana-0.4.0/tests/test_console.py +135 -0
- iquana-0.4.0/tests/test_constraints.py +440 -0
- iquana-0.4.0/tests/test_data_loader.py +177 -0
- iquana-0.4.0/tests/test_data_ops.py +370 -0
- iquana-0.4.0/tests/test_examples.py +455 -0
- iquana-0.4.0/tests/test_fit_quality.py +915 -0
- iquana-0.4.0/tests/test_fitting_bumps.py +125 -0
- iquana-0.4.0/tests/test_fitting_lmfit.py +106 -0
- iquana-0.4.0/tests/test_multifit_parameters.py +509 -0
- iquana-0.4.0/tests/test_multifit_plotting.py +209 -0
- iquana-0.4.0/tests/test_multifit_results.py +628 -0
- iquana-0.4.0/tests/test_multifit_review_regressions.py +844 -0
- iquana-0.4.0/tests/test_persistence.py +1225 -0
- iquana-0.4.0/tests/test_plotting.py +209 -0
- iquana-0.4.0/tests/test_polydispersity.py +502 -0
- iquana-0.4.0/tests/test_pr_inversion.py +983 -0
- iquana-0.4.0/tests/test_public_api.py +114 -0
- iquana-0.4.0/tests/test_q_range.py +192 -0
- iquana-0.4.0/tests/test_resolution.py +552 -0
- iquana-0.4.0/tests/test_result_schema.py +155 -0
- iquana-0.4.0/tests/test_results.py +72 -0
- iquana-0.4.0/tests/test_sans_fitter.py +249 -0
- iquana-0.4.0/tests/test_structure_factor.py +328 -0
- iquana-0.4.0/tests/test_theory_preview.py +387 -0
- iquana-0.4.0/tests/test_truth_recovery.py +256 -0
- iquana-0.4.0/tests/test_weighting.py +623 -0
iquana-0.4.0/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, IQuana contributors
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
+
may be used to endorse or promote products derived from this software
|
|
17
|
+
without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
iquana-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: iquana
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: A flexible template for fitting SANS data with SasModels
|
|
5
|
+
Author: IQuana contributors
|
|
6
|
+
License: BSD-3-Clause
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: numpy>=2.0
|
|
11
|
+
Requires-Dist: plotly>=5.0
|
|
12
|
+
Requires-Dist: scipy>=1.7
|
|
13
|
+
Requires-Dist: sasmodels>=1.0
|
|
14
|
+
Requires-Dist: sasdata>=0.8
|
|
15
|
+
Requires-Dist: bumps>=1.0
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
18
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
19
|
+
Requires-Dist: pandas>=2.0.0; extra == "dev"
|
|
20
|
+
Provides-Extra: report
|
|
21
|
+
Requires-Dist: kaleido>=0.2; extra == "report"
|
|
22
|
+
Provides-Extra: docs
|
|
23
|
+
Requires-Dist: mkdocs>=1.5; extra == "docs"
|
|
24
|
+
Requires-Dist: mkdocs-material>=9.5; extra == "docs"
|
|
25
|
+
Requires-Dist: mkdocstrings[python]>=0.25; extra == "docs"
|
|
26
|
+
Provides-Extra: notebooks
|
|
27
|
+
Requires-Dist: jupyter>=1.0; extra == "notebooks"
|
|
28
|
+
Requires-Dist: notebook>=7.0; extra == "notebooks"
|
|
29
|
+
Requires-Dist: ipykernel>=6.0; extra == "notebooks"
|
|
30
|
+
Requires-Dist: ipywidgets>=8.0; extra == "notebooks"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
<p align="center">
|
|
34
|
+
<a href="https://ai4se1dk.github.io/IQuana/">
|
|
35
|
+
<picture>
|
|
36
|
+
<source media="(prefers-color-scheme: dark)" srcset="iquana-banner-dark.png">
|
|
37
|
+
<source media="(prefers-color-scheme: light)" srcset="iquana-banner-light.png">
|
|
38
|
+
<img src="iquana-banner-light.png" alt="IQuana — Small-Angle Neutron Scattering model fitting" width="360">
|
|
39
|
+
</picture>
|
|
40
|
+
</a>
|
|
41
|
+
</p>
|
|
42
|
+
|
|
43
|
+
# IQuana - SANS Model Fitter
|
|
44
|
+
|
|
45
|
+
[](https://github.com/ai4se1dk/IQuana/actions/workflows/ci.yml)
|
|
46
|
+
[](https://ai4se1dk.github.io/IQuana/)
|
|
47
|
+
[](https://pypi.python.org/pypi/IQuana)
|
|
48
|
+
[](https://ai4se1dk.github.io/IQuana/)
|
|
49
|
+
[](https://app.codacy.com/gh/ai4se1dk/IQuana/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
A flexible, model-agnostic Python template for fitting Small-Angle Neutron Scattering (SANS) data using the SasModels library.
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- **Model-Agnostic Design**: Works with any model from the SasModels library (cylinder, sphere, core_shell, etc.)
|
|
57
|
+
- **Multiple Fitting Engines**: Supports both BUMPS (default) and LMFit optimization engines
|
|
58
|
+
- **Flexible Data Loading**: Reads CSV, XML, and HDF5 formats via sasdata, including files holding several datasets — pick one with `load_data(path, dataset=...)` by index or name
|
|
59
|
+
- **Example Data Built In**: A curated set of ready-to-fit datasets plus a simulator that generates data — `examples.load_fitter('silica_spheres')` or `examples.simulate('sphere', radius=50)`
|
|
60
|
+
- **Polydispersity and Custom Models**: Size distributions on any polydisperse parameter via `set_pd_param()`, and your own SasModels plugin models loaded by file path
|
|
61
|
+
- **Resolution Control**: Choose how instrument smearing is applied — dQ from the data, none, custom pinhole (`dq_over_q`) or slit — with `set_resolution()`
|
|
62
|
+
- **Data Weighting**: Choose which σ divides each residual — the data's dI, √|I|, |I| or none — with `set_weighting()`, matching SasView's Fit Page; this is also what lets the bumps engine and `fit_bayesian()` fit data without uncertainties
|
|
63
|
+
- **Q-Range Restriction**: Fit only a chosen [qmin, qmax] window (e.g. trim beam-stop or background-dominated points)
|
|
64
|
+
- **Dataset Arithmetic**: Add, subtract, multiply, and divide datasets (or scale by constants) with propagated uncertainties via `data_ops` — e.g. background subtraction and transmission correction before fitting
|
|
65
|
+
- **User-Friendly Parameter Management**: Easy-to-use interface for setting parameter values, bounds, and fitting flags
|
|
66
|
+
- **Theory Preview**: See the model at the current parameters before fitting — `plot_model()` for data, curve and residuals, `calculate()` for the intensities on any Q grid, and `compare()` to overlay several parameter sets
|
|
67
|
+
- **Interactive Visualization**: Automatic plotting of data, fitted model, and residuals with Plotly
|
|
68
|
+
- **Simultaneous Fitting**: Fit several datasets together with shared or related parameters via `MultiFitter` — contrast variation, temperature series and multi-configuration data, with per-dataset models, Q ranges and resolution, plus constraints such as `'0.8 * h2o.scale'`
|
|
69
|
+
- **Bayesian Analysis**: Posterior sampling with BUMPS DREAM (MCMC) plus corner, marginal, predictive-band, correlation, and trace plots
|
|
70
|
+
- **P(r) Inversion**: Model-free pair distance distribution analysis (indirect Fourier transform) via `pr_inversion` — D_max exploration, automatic regularization/term selection, and Rg/I(0)/positivity diagnostics
|
|
71
|
+
- **Fit-Quality Reporting**: Reduced χ² alongside raw χ², point and parameter counts, covariance/correlation matrices, a convergence verdict and a warning when a fitted parameter rests on a bound — all via `fitter.get_fit_report()`, which renders as a table in a notebook, as text, or as Markdown
|
|
72
|
+
- **Result Export**: Save fitted parameters and curves to CSV files
|
|
73
|
+
- **Save, Reload and Share**: Store the whole analysis (data source, model, parameters, links, resolution, result) as JSON with `save_analysis()` / `IQuana.load_analysis()`, or write a single shareable HTML report with `report()`
|
|
74
|
+
|
|
75
|
+
## Installation
|
|
76
|
+
|
|
77
|
+
### Option 1: Using pip (recommended for users)
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# Clone the repository
|
|
81
|
+
git clone https://github.com/ai4se1dk/IQuana.git
|
|
82
|
+
cd IQuana
|
|
83
|
+
|
|
84
|
+
# Install the package
|
|
85
|
+
pip install -e .
|
|
86
|
+
|
|
87
|
+
# Or install with development dependencies
|
|
88
|
+
pip install -e ".[dev]"
|
|
89
|
+
|
|
90
|
+
# The Jupyter stack is optional - only needed to run notebooks/
|
|
91
|
+
pip install -e ".[notebooks]"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Option 2: Using Pixi (recommended for development)
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
# Clone the repository
|
|
98
|
+
git clone https://github.com/ai4se1dk/IQuana.git
|
|
99
|
+
cd IQuana
|
|
100
|
+
|
|
101
|
+
# Install dependencies with Pixi
|
|
102
|
+
pixi install
|
|
103
|
+
|
|
104
|
+
# Run tests
|
|
105
|
+
pixi run test
|
|
106
|
+
|
|
107
|
+
# Run demo notebook
|
|
108
|
+
pixi run run-demo
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Quick Start
|
|
112
|
+
|
|
113
|
+
No data of your own yet? Start from a bundled example:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from iquana import examples
|
|
117
|
+
|
|
118
|
+
examples.describe() # see what's available
|
|
119
|
+
fitter = examples.load_fitter('silica_spheres') # data + model + parameters
|
|
120
|
+
result = fitter.fit()
|
|
121
|
+
fitter.plot_results()
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Or simulate data, so you can check the fit recovers it:
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
data = examples.simulate('sphere', radius=50, noise=0.02, seed=0)
|
|
128
|
+
data.truth['radius'] # 50.0
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
See the [Example Data guide](docs/examples.md) for the full collection.
|
|
132
|
+
|
|
133
|
+
With your own data:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from iquana import IQuana
|
|
137
|
+
|
|
138
|
+
# Create fitter instance
|
|
139
|
+
fitter = IQuana()
|
|
140
|
+
|
|
141
|
+
# Load your data
|
|
142
|
+
fitter.load_data('my_sans_data.csv')
|
|
143
|
+
|
|
144
|
+
# Set the model (any model from SasModels!)
|
|
145
|
+
fitter.set_model('cylinder')
|
|
146
|
+
|
|
147
|
+
# Optionally restrict the Q range used for fitting
|
|
148
|
+
fitter.set_q_range(qmin=0.01, qmax=0.3)
|
|
149
|
+
|
|
150
|
+
# View initial parameter values
|
|
151
|
+
fitter.get_params()
|
|
152
|
+
|
|
153
|
+
# Configure parameters for fitting
|
|
154
|
+
fitter.set_param('radius', value=20, min=1, max=100, vary=True)
|
|
155
|
+
fitter.set_param('length', value=400, min=10, max=1000, vary=True)
|
|
156
|
+
fitter.set_param('scale', value=0.1, min=0, max=1, vary=True)
|
|
157
|
+
fitter.set_param('background', value=0.01, min=0, max=1, vary=True)
|
|
158
|
+
|
|
159
|
+
# View current parameters
|
|
160
|
+
fitter.get_params()
|
|
161
|
+
|
|
162
|
+
# Check the starting values against the data before fitting
|
|
163
|
+
fitter.plot_model()
|
|
164
|
+
|
|
165
|
+
# Perform the fit (using BUMPS by default)
|
|
166
|
+
result = fitter.fit(engine='bumps', method='amoeba')
|
|
167
|
+
|
|
168
|
+
# Visualize results
|
|
169
|
+
fitter.plot_results(show_residuals=True)
|
|
170
|
+
|
|
171
|
+
# Judge the fit: reduced chi-squared, correlations, bound checks
|
|
172
|
+
report = fitter.get_fit_report()
|
|
173
|
+
print(report)
|
|
174
|
+
|
|
175
|
+
# Save results
|
|
176
|
+
fitter.save_results('fit_results.csv')
|
|
177
|
+
|
|
178
|
+
# Save the whole analysis, and reopen it later or elsewhere
|
|
179
|
+
fitter.save_analysis('my_analysis.json')
|
|
180
|
+
fitter = IQuana.load_analysis('my_analysis.json')
|
|
181
|
+
|
|
182
|
+
# One shareable document: settings, tables and the plot
|
|
183
|
+
fitter.report('my_analysis.html')
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Switching Models
|
|
187
|
+
|
|
188
|
+
The fitter is completely model-agnostic. Simply load a different model:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
# Try with a sphere model instead
|
|
192
|
+
fitter.set_model('sphere')
|
|
193
|
+
fitter.get_params() # See different parameters!
|
|
194
|
+
|
|
195
|
+
fitter.set_param('radius', value=25, min=5, max=100, vary=True)
|
|
196
|
+
result = fitter.fit()
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Switching Fitting Engines
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
# Use BUMPS (default)
|
|
203
|
+
result = fitter.fit(engine='bumps', method='amoeba')
|
|
204
|
+
|
|
205
|
+
# Or use LMFit
|
|
206
|
+
result = fitter.fit(engine='lmfit', method='leastsq')
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Working with Structure Factors
|
|
210
|
+
|
|
211
|
+
Combine any SasModels form factor with an interaction model to capture correlated systems.
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
fitter.set_model('sphere')
|
|
215
|
+
|
|
216
|
+
# Apply a structure factor (creates sphere@hardsphere product model)
|
|
217
|
+
fitter.set_structure_factor('hardsphere', radius_effective_mode='link_radius')
|
|
218
|
+
|
|
219
|
+
# Inspect linked parameters and run the fit as usual
|
|
220
|
+
fitter.get_params()
|
|
221
|
+
result = fitter.fit()
|
|
222
|
+
|
|
223
|
+
# Remove the structure factor to go back to the pure form factor
|
|
224
|
+
fitter.remove_structure_factor()
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
- **Structure factors:** queried from sasmodels at runtime — call `get_structure_factors()` for the full list (currently includes `hardsphere`, `hayter_msa`, `squarewell`, `stickyhardsphere`, `two_yukawa`).
|
|
228
|
+
- **Radius handling:** use `radius_effective_mode='link_radius'` to keep `radius_effective` equal to the form-factor `radius`, or leave the default `unconstrained` to fit it independently.
|
|
229
|
+
- **State helpers:** `get_structure_factor()` returns the active structure factor so notebooks/scripts can branch as needed.
|
|
230
|
+
|
|
231
|
+
## Combining Models
|
|
232
|
+
|
|
233
|
+
Describe a dataset with several models at once — for example a low-Q diffuse
|
|
234
|
+
feature plus a high-Q peak — fitted simultaneously against one dataset:
|
|
235
|
+
|
|
236
|
+
```python
|
|
237
|
+
fitter = IQuana()
|
|
238
|
+
fitter.load_data('data.csv')
|
|
239
|
+
|
|
240
|
+
# Combine models; parameters get friendly per-model prefixes
|
|
241
|
+
fitter.set_models('dab', 'peak_lorentz')
|
|
242
|
+
fitter.set_param('dab_cor_length', value=50, min=1, max=500, vary=True)
|
|
243
|
+
fitter.set_param('peak_lorentz_peak_pos', value=0.1, min=0.01, max=0.5, vary=True)
|
|
244
|
+
fitter.set_param('background', value=0.001, min=0, max=0.1, vary=True)
|
|
245
|
+
|
|
246
|
+
result = fitter.fit(engine='bumps')
|
|
247
|
+
|
|
248
|
+
# See which feature each model accounts for
|
|
249
|
+
fitter.plot_results(show_components=True)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
The combined intensity is
|
|
253
|
+
|
|
254
|
+
$I(q) = \text{scale} \cdot \sum_i \text{scale}_i \cdot I_i(q) + \text{background}$
|
|
255
|
+
|
|
256
|
+
so the global `scale` and `background` are shared by all components natively,
|
|
257
|
+
while each component has its own `<model>_scale`.
|
|
258
|
+
|
|
259
|
+
**Custom names (monikers)** — for long model names, duplicates, or physics labels:
|
|
260
|
+
|
|
261
|
+
```python
|
|
262
|
+
fitter.set_models(small='sphere', large='sphere', shared=['sld', 'sld_solvent'])
|
|
263
|
+
fitter.set_param('small_radius', value=20, vary=True)
|
|
264
|
+
fitter.set_param('large_radius', value=200, vary=True)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
**Sharing parameters** — any name in `shared=[...]` that exists in ≥ 2
|
|
268
|
+
components becomes a single unprefixed parameter driving all of them
|
|
269
|
+
(`sld` above). Polydispersity configuration stays per-component under the
|
|
270
|
+
prefixed names.
|
|
271
|
+
|
|
272
|
+
**Component curves** — after fitting a `'+'` mixture, `plot_results(show_components=True)`
|
|
273
|
+
overlays one dashed curve per component, each drawn as
|
|
274
|
+
`scale · part_scale · I_part(q)` (background shown implicitly in the total).
|
|
275
|
+
|
|
276
|
+
> **Advanced:** the raw sasmodels expression syntax is also available and keeps
|
|
277
|
+
> sasmodels' native `A_`/`B_` names: `fitter.set_model('dab+peak_lorentz')`.
|
|
278
|
+
> For after-the-fact or asymmetric sharing, use equality links:
|
|
279
|
+
> `fitter.link_params('large_sld', to='small_sld')` and
|
|
280
|
+
> `fitter.unlink_params('large_sld')`. Composite models and parameter links
|
|
281
|
+
> currently require the `bumps` engine.
|
|
282
|
+
|
|
283
|
+
## Simultaneous Fitting
|
|
284
|
+
|
|
285
|
+
Fit several datasets at once, sharing the parameters they have in common:
|
|
286
|
+
|
|
287
|
+
```python
|
|
288
|
+
from iquana import MultiFitter
|
|
289
|
+
|
|
290
|
+
fit = MultiFitter()
|
|
291
|
+
fit.add('h2o', 'contrast_h2o.xml', model='sphere')
|
|
292
|
+
fit.add('d2o', 'contrast_d2o.xml', model='sphere')
|
|
293
|
+
|
|
294
|
+
for name in ('h2o', 'd2o'):
|
|
295
|
+
fit[name].set_param('radius', value=45, min=10, max=100, vary=True)
|
|
296
|
+
fit[name].set_param('scale', value=0.02, min=0.001, max=0.1, vary=True)
|
|
297
|
+
fit[name].set_param('background', value=0.01, min=0, max=0.1, vary=True)
|
|
298
|
+
|
|
299
|
+
fit.share('radius', 'scale') # one radius and scale across both curves
|
|
300
|
+
fit.constrain('h2o.sld_solvent', -0.56) # known solvent contrasts
|
|
301
|
+
fit.constrain('d2o.sld_solvent', 6.34)
|
|
302
|
+
|
|
303
|
+
fit.describe() # datasets, relationships, free-parameter count
|
|
304
|
+
result = fit.fit()
|
|
305
|
+
print(fit.get_fit_report())
|
|
306
|
+
fit.plot_results() # one data/fit + residual panel per dataset
|
|
307
|
+
fit.save_results('contrast_results')
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Parameters are addressed as `dataset.parameter`. Beyond `share()`, use
|
|
311
|
+
`link_params('cyl.radius', to='sph.radius')` to make one parameter follow
|
|
312
|
+
another, and `constrain('d2o.scale', '0.8 * h2o.scale')` for a fixed value or
|
|
313
|
+
an arithmetic relationship. Each dataset keeps its own model, Q range, mask and
|
|
314
|
+
resolution — nothing is merged or interpolated — and a shared parameter costs
|
|
315
|
+
exactly one degree of freedom.
|
|
316
|
+
|
|
317
|
+
See the [Simultaneous Fitting guide](docs/multifit.md) for choosing what to
|
|
318
|
+
share, how constraint bounds are enforced, and what dataset weights mean for
|
|
319
|
+
the reported uncertainties.
|
|
320
|
+
|
|
321
|
+
## Bayesian Analysis
|
|
322
|
+
|
|
323
|
+
Sample the full posterior distribution of the varying parameters with the
|
|
324
|
+
DREAM MCMC sampler (built into BUMPS — no extra dependencies):
|
|
325
|
+
|
|
326
|
+
```python
|
|
327
|
+
fitter.set_model('sphere')
|
|
328
|
+
fitter.set_param('radius', value=50, min=10, max=200, vary=True)
|
|
329
|
+
fitter.set_param('scale', value=0.1, min=0.01, max=1.0, vary=True)
|
|
330
|
+
|
|
331
|
+
# Run the Bayesian fit (prints point estimates + credible intervals + diagnostics)
|
|
332
|
+
result = fitter.fit_bayesian(samples=10000, burn=200)
|
|
333
|
+
|
|
334
|
+
# Corner plot of the posterior
|
|
335
|
+
fitter.plot_posterior_pairs()
|
|
336
|
+
|
|
337
|
+
# More displays
|
|
338
|
+
fitter.plot_param_distribution('radius') # marginal posterior
|
|
339
|
+
fitter.plot_posterior_predictive() # 95% credible band over the data
|
|
340
|
+
fitter.plot_param_correlations() # correlation heatmap
|
|
341
|
+
fitter.plot_trace() # MCMC chain traces
|
|
342
|
+
|
|
343
|
+
# Raw chain access / export
|
|
344
|
+
posterior = fitter.get_posterior()
|
|
345
|
+
posterior.save_posterior_csv('posterior_chain.csv')
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
See the [User Guide](https://ai4se1dk.github.io/IQuana/usage/) for details.
|
|
349
|
+
|
|
350
|
+
### Available Methods
|
|
351
|
+
|
|
352
|
+
**BUMPS methods:**
|
|
353
|
+
- `'amoeba'` - Nelder-Mead simplex (default, robust)
|
|
354
|
+
- `'lm'` - Levenberg-Marquardt
|
|
355
|
+
- `'newton'` - Newton's method
|
|
356
|
+
- `'de'` - Differential evolution
|
|
357
|
+
|
|
358
|
+
**LMFit methods:**
|
|
359
|
+
- `'leastsq'` - Levenberg-Marquardt (default)
|
|
360
|
+
- `'least_squares'` - Trust Region Reflective
|
|
361
|
+
- `'differential_evolution'` - Global optimizer
|
|
362
|
+
- `'powell'`, `'nelder'`, etc.
|
|
363
|
+
|
|
364
|
+
## Demo Notebooks
|
|
365
|
+
|
|
366
|
+
- [notebooks/iquana_demo.ipynb](notebooks/iquana_demo.ipynb) — comprehensive demonstration of the fitting workflow with examples.
|
|
367
|
+
- [notebooks/example_datasets.ipynb](notebooks/example_datasets.ipynb) — the bundled example datasets and the data simulator, including checking that a fit recovers the known truth.
|
|
368
|
+
- [notebooks/composite_models.ipynb](notebooks/composite_models.ipynb) — several models against one dataset with `set_models()`: per-component curves and shared parameters.
|
|
369
|
+
- [notebooks/data_operations_demo.ipynb](notebooks/data_operations_demo.ipynb) — dataset arithmetic: background subtraction, rescaling and combining datasets with propagated uncertainties, then fitting the result.
|
|
370
|
+
- [notebooks/bayesian_sampling.ipynb](notebooks/bayesian_sampling.ipynb) — Bayesian posterior sampling API (`fit_bayesian()`) and the associated posterior plots.
|
|
371
|
+
- [notebooks/pr_inversion_demo.ipynb](notebooks/pr_inversion_demo.ipynb) — model-free P(r) inversion: D_max exploration, automatic inversion, and diagnostics.
|
|
372
|
+
- [notebooks/resolution_control.ipynb](notebooks/resolution_control.ipynb) — explicit resolution (smearing) control: the four modes, what each hands sasmodels, and what ignoring resolution costs.
|
|
373
|
+
- [notebooks/sasmodels-basic.ipynb](notebooks/sasmodels-basic.ipynb) — the same kind of fit done directly with sasmodels and BUMPS, without IQuana, for comparison.
|
|
374
|
+
- [notebooks/simultaneous_fitting.ipynb](notebooks/simultaneous_fitting.ipynb) — fitting several datasets together with `MultiFitter`: contrast variation, shared parameters, constraints, and joint diagnostics.
|
|
375
|
+
- [notebooks/four_form_factors.ipynb](notebooks/four_form_factors.ipynb) — fitting a sum of four form factors against one dataset: what to fix, why a global optimizer is needed, and how to read the parameter correlations that remain.
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
## Design Philosophy
|
|
379
|
+
|
|
380
|
+
This implementation follows a **template pattern** where:
|
|
381
|
+
|
|
382
|
+
1. The core fitting logic is abstracted into a reusable class
|
|
383
|
+
2. Models are loaded dynamically from SasModels - no hardcoded model assumptions
|
|
384
|
+
3. Parameters are discovered automatically from the model definition
|
|
385
|
+
4. Multiple optimization engines are supported through a unified interface
|
|
386
|
+
5. The user maintains full control over parameter initialization and bounds
|
|
387
|
+
|
|
388
|
+
## Implementation Details
|
|
389
|
+
|
|
390
|
+
### Engine Adapters
|
|
391
|
+
|
|
392
|
+
The fitter implements adapter patterns for both BUMPS and LMFit:
|
|
393
|
+
|
|
394
|
+
- **BUMPS**: Uses native `sasmodels.bumps_model` integration
|
|
395
|
+
- **LMFit**: Uses `sasmodels.direct_model.DirectModel` with a custom residual function
|
|
396
|
+
|
|
397
|
+
### Parameter Management
|
|
398
|
+
|
|
399
|
+
Parameters are stored internally with:
|
|
400
|
+
- `value`: Current/initial value
|
|
401
|
+
- `min`, `max`: Bounds
|
|
402
|
+
- `vary`: Fitting flag
|
|
403
|
+
- `description`: From model metadata
|
|
404
|
+
|
|
405
|
+
This allows the fitter to work with any model without prior knowledge of its parameters.
|
|
406
|
+
|
|
407
|
+
## License
|
|
408
|
+
|
|
409
|
+
BSD 3-Clause License. See [LICENSE](LICENSE) for the full text.
|
|
410
|
+
|
|
411
|
+
## References
|
|
412
|
+
|
|
413
|
+
- SasModels: https://github.com/SasView/sasmodels
|
|
414
|
+
- BUMPS: https://github.com/bumps/bumps
|
|
415
|
+
- LMFit: https://lmfit.github.io/lmfit-py/
|
|
416
|
+
- Plotly: https://plotly.com/python/
|