Skip to content

Commit dd63557

Browse files
committed
rewrite everything
1 parent c19ad41 commit dd63557

File tree

10 files changed

+234
-207
lines changed

10 files changed

+234
-207
lines changed

doc/callbacks/fitting/chained_fitting.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Chained Fitting ({py:obj}`ChainedLiveFit <ibex_bluesky_core.callbacks.ChainedLiveFit>`)
1+
# Chained Fitting ({py:obj}`~ibex_bluesky_core.callbacks.ChainedLiveFit`)
22

3-
{py:obj}`ChainedLiveFit <ibex_bluesky_core.callbacks.ChainedLiveFit>` is a specialised callback that manages multiple {py:obj}`LiveFit <ibex_bluesky_core.callbacks.LiveFit>` instances in a chain, where each fit's results inform the next fit's initial parameters. This is particularly useful when fitting datasets where the initial guess for each fit depends on the parameters obtained from a previous fit.
3+
{py:obj}`~ibex_bluesky_core.callbacks.ChainedLiveFit` is a specialised callback that manages multiple {py:obj}`~ibex_bluesky_core.callbacks.LiveFit` instances in a chain, where each fit's results inform the next fit's initial parameters. This is particularly useful when fitting datasets where the initial guess for each fit depends on the parameters obtained from a previous fit.
44

55
This is useful for when you need to be careful with your curve fitting due to the presence of noisy data. It allows you to fit your widest (full) wavelength band first and then using its fit parameters as the initial guess of the parameters for the next fit
66

7-
To show how we expect this to be used, we will use a {py:obj}`DualRunDae <ibex_bluesky_core.devices.polarisingdae.DualRunDae>` configured with multiple wavelength bands, to highlight the need to carry over fitting parameters. The example below shows two wavelength bands, first wider than the second. We will fit to the data in the widest wavelength band first, and carry over the results of that fit to the guess of the next fit, to improve the chances of that fit converging.
7+
To show how we expect this to be used, we will use a {py:obj}`~ibex_bluesky_core.devices.polarisingdae.DualRunDae` configured with multiple wavelength bands, to highlight the need to carry over fitting parameters. The example below shows two wavelength bands, first wider than the second. We will fit to the data in the widest wavelength band first, and carry over the results of that fit to the guess of the next fit, to improve the chances of that fit converging.
88

99
```python
1010
from typing import Generator
@@ -45,11 +45,11 @@ def plan() -> Generator[Msg, None, None]:
4545

4646
The list of signal names for each independent variable should be passed in the same order that the fits are to be applied.
4747

48-
A list of matplotlib axes is accepted, which will mean that {external+bluesky:py:obj}`LiveFitPlots <bluesky.callbacks.mpl_plotting.LiveFitPlot>` are created per {py:obj}`LiveFit <ibex_bluesky_core.callbacks.LiveFit>`, and it will plot the each respective fit to an axis. {external+bluesky:py:obj}`LiveFitPlots <bluesky.callbacks.mpl_plotting.LiveFitPlot>` are not created if you do not pass `ax`.
48+
A list of matplotlib axes is accepted, which will mean that {external+bluesky:py:obj}`LiveFitPlots <bluesky.callbacks.mpl_plotting.LiveFitPlot>` are created per {py:obj}`~ibex_bluesky_core.callbacks.LiveFit`, and it will plot the each respective fit to an axis. {external+bluesky:py:obj}`LiveFitPlots <bluesky.callbacks.mpl_plotting.LiveFitPlot>` are not created if you do not pass `ax`.
4949

5050
Similar to the `y` parameter, uncertainties for each dependent variable are accepted in `yerr`.
5151

52-
{py:obj}`ChainedLiveFit <ibex_bluesky_core.callbacks.ChainedLiveFit>` currently has the following limitations:
52+
{py:obj}`~ibex_bluesky_core.callbacks.ChainedLiveFit` currently has the following limitations:
5353
- The method for fitting must be the same across all dependent variables.
5454
- Parameter uncertainties are not carried over between fits
55-
- If a fit fails to converge, subsequent fits will use their default guess functions
55+
- If a fit fails to converge, the next fits will use their default guess functions

doc/callbacks/fitting/fitting.md

Lines changed: 69 additions & 62 deletions
Large diffs are not rendered by default.

doc/callbacks/fitting/livefit_logger.md

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
{#livefit_logger}
2-
# Saving fit results to file (`LiveFitLogger`)
2+
# Saving fit results to file ({py:obj}`~ibex_bluesky_core.callbacks.LiveFitLogger`)
33

4-
## Fitting Files
4+
The {py:obj}`~ibex_bluesky_core.callbacks.LiveFitLogger` callback exists to write the results of a {py:obj}`~ibex_bluesky_core.callbacks.LiveFit` to file. These are designed to be human-readable files, rather than machine readable.
55

6-
The callback ([`LiveFitLogger`](ibex_bluesky_core.callbacks.LiveFitLogger)) exists to write all fitting metrics from [`LiveFit`](ibex_bluesky_core.callbacks.LiveFit) to file. These are designed to be human readable files rather than machine readable.
6+
The files written by this callback contain:
7+
- The result of an {py:obj}`lmfit.model.ModelResult.fit_report`, which contains fit statistics such as r-squared, optimized values of fit parameters, and information about correlations if present.
8+
- Two blank lines
9+
- A csv table containing `x`, `y`, optionally `y` uncertainty, and modelled `y`
710

8-
This callback provides you with useful metrics such as `R-squared` and `chi-square`, then providing you with a table of the raw collected data included modelled `y` data and `y` uncertainty.
11+
---
912

10-
### Example
11-
An example of using this could be:
13+
:::{collapse} Example output file from this callback (click to expand)
14+
```
15+
[[Model]]
16+
Model(Gaussian [amp * exp(-((x - x0) ** 2) / (2 * sigma**2)) + background])
17+
[[Fit Statistics]]
18+
# fitting method = leastsq
19+
# function evals = 36
20+
# data points = 98
21+
# variables = 4
22+
chi-square = 148.424352
23+
reduced chi-square = 1.57898247
24+
Akaike info crit = 48.6805775
25+
Bayesian info crit = 59.0204474
26+
R-squared = 0.95949614
27+
[[Variables]]
28+
amp: 230.617045 +/- 10.4151289 (4.52%) (init = 287)
29+
sigma: 0.00969209 +/- 2.7953e-04 (2.88%) (init = 0.009036228)
30+
x0: 0.49905249 +/- 3.5203e-04 (0.07%) (init = 0.4999994)
31+
background: 0.59641742 +/- 0.13909858 (23.32%) (init = 0)
32+
[[Correlations]] (unreported correlations are < 0.100)
33+
C(amp, sigma) = -0.6254
34+
C(sigma, x0) = +0.1980
35+
C(amp, x0) = -0.1360
36+
37+
38+
x,y,y uncertainty,modelled y
39+
0.29442974330116267,0.9362155048960608,1.1732576103594294,0.596417422655042
40+
0.2987958123011627,1.7278474231222112,1.411659205995935,0.596417422655042
41+
0.3013446053011628,2.2956249406851517,1.770613464899424,0.596417422655042
42+
0.30558076430116277,3.574729064039327,2.1816903049886642,0.596417422655042
43+
0.31062594030116286,1.0235264249142935,1.2440461004858383,0.596417422655042
44+
...
45+
```
46+
:::
47+
48+
---
1249

50+
:::{collapse} Example configuration (click to expand)
1351
```python
1452
def some_plan() -> Generator[Msg, None, None]:
1553
... # Set up prefix, reducers, controllers etc. here
@@ -31,7 +69,10 @@ def some_plan() -> Generator[Msg, None, None]:
3169
def _inner() -> Generator[Msg, None, None]:
3270
... # Continue to plan
3371
```
72+
:::
73+
74+
---
3475

35-
This will put the all fitting data collected over the run into a `.csv` file, named after the `uid` of the scan, in the `C:\\Instrument\\Var\\logs\\bluesky\\fitting` path provided to the callback. You should provide a `postfix` to append to the end of the filename to disambiguate different fits and to avoid overwriting fitting files- it is only one file per fit completed.
76+
This will write data describing a fit to a file, into the path provided to the callback. If multiple fits are present simultaneously, use the `postfix` argument to append to the end of the filename to disambiguate different fits and to avoid overwriting fitting files.
3677

37-
If you provide a signal name for the `yerr` argument then an extra column for `y uncertainty` will be displayed in the fitting file. You have the option to not provide anything for this argument if you do not want to have uncertainty information in your fitting file. Keep in mind that even if you provide `yerr` in [`LiveFitLogger`](ibex_bluesky_core.callbacks.LiveFitLogger), you will still need to provide `yerr` in [`LiveFit`](ibex_bluesky_core.callbacks.LiveFit) if you want uncertainty/weight per point to influence the fit.
78+
If you provide a signal name for the `yerr` argument, then an extra column for `y uncertainty` will be displayed in the fitting file. Keep in mind that even if you provide `yerr` in {py:obj}`~ibex_bluesky_core.callbacks.LiveFitLogger`, you will still *also* need to provide `yerr` in {py:obj}`~ibex_bluesky_core.callbacks.LiveFit` if you want uncertainty/weight per point to influence the fit.

doc/callbacks/fitting/standard_fits.md

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{#standard_fitting_models}
22
# Standard Fitting Models
33

4-
## Linear
5-
6-
API Reference: [`Linear`](ibex_bluesky_core.fitting.Linear)
4+
## {py:obj}`~ibex_bluesky_core.fitting.Linear`
75

86
- `c1` - Gradient
97
- `c0` - (y) Intercept
@@ -12,9 +10,7 @@ API Reference: [`Linear`](ibex_bluesky_core.fitting.Linear)
1210
y = c_1x + c_0
1311
```
1412

15-
## Polynomial
16-
17-
API Reference: [`Polynomial`](ibex_bluesky_core.fitting.Polynomial)
13+
## {py:obj}`~ibex_bluesky_core.fitting.Polynomial`
1814

1915
- `cn` ... `c0` - Polynomial coefficients
2016

@@ -23,9 +19,7 @@ For a polynomial degree `n`:
2319
y = c_{n}x^n + c_{n-1}x^{n-1} + ... + c_1 * x^1 + c_0
2420
```
2521

26-
## Gaussian
27-
28-
API Reference: [`Gaussian`](ibex_bluesky_core.fitting.Gaussian)
22+
## {py:obj}`~ibex_bluesky_core.fitting.Gaussian`
2923

3024
- `amp` - The maximum height of the Gaussian above `background`
3125
- `sigma` - A scalar for Gaussian width
@@ -38,9 +32,7 @@ y = \text{amp} * e^{-\frac{(x - x0) ^ 2}{2 * \text{sigma}^2}} + \text{background
3832

3933
![GaussianModel](./images_fits/gaussian.png)
4034

41-
## Lorentzian
42-
43-
API Reference: [`Lorentzian`](ibex_bluesky_core.fitting.Lorentzian)
35+
## {py:obj}`~ibex_bluesky_core.fitting.Lorentzian`
4436

4537
- `amp` - The maximum height of the Lorentzian above `background`
4638
- `sigma` - A scalar for Lorentzian width
@@ -53,9 +45,7 @@ y = \frac{\text{amp}}{1 + \frac{x - \text{center}}{\text{sigma}}^2} + \text{back
5345

5446
![LorentzianModel](./images_fits/lorentzian.png)
5547

56-
## Damped Oscillator (DampedOsc)
57-
58-
API Reference: [`DampedOsc`](ibex_bluesky_core.fitting.DampedOsc)
48+
## Damped Oscillator ({py:obj}`~ibex_bluesky_core.fitting.DampedOsc`)
5949

6050
- `center` - The centre (x) of the oscillation
6151
- `amp` - The maximum height of the curve above 0
@@ -68,9 +58,7 @@ y = \text{amp} * \cos((x - \text{center}) * \text{freq}) * e^{-\frac{x - \text{c
6858

6959
![DampedOscModel](./images_fits/damped_osc.png)
7060

71-
## Slit Scan (SlitScan)
72-
73-
API Reference: [`SlitScan`](ibex_bluesky_core.fitting.SlitScan)
61+
## Slit Scan ({py:obj}`~ibex_bluesky_core.fitting.SlitScan`)
7462

7563
- `background` $b$ - The minimum value (y) of the model
7664
- `inflection0` $i_0$ - The x coord of the first inflection point
@@ -92,9 +80,7 @@ y = \min(\text{lin_seg}, \text{exp_seg})
9280

9381
![SlitScanModel](./images_fits/slit_scan.png)
9482

95-
## Error Function (ERF)
96-
97-
API Reference: [`ERF`](ibex_bluesky_core.fitting.ERF)
83+
## Error Function ({py:obj}`~ibex_bluesky_core.fitting.ERF`)
9884

9985
- `cen` - The centre (x) of the model
10086
- `stretch` - A horizontal stretch factor for the model
@@ -107,9 +93,7 @@ y = background + scale * erf(stretch * (x - cen))
10793

10894
![ERFModel](./images_fits/erf.png)
10995

110-
## Complementary Error Function (ERFC)
111-
112-
API Reference: [`ERFC`](ibex_bluesky_core.fitting.ERFC)
96+
## Complementary Error Function ({py:obj}`~ibex_bluesky_core.fitting.ERFC`)
11397

11498
- `cen` - The centre (x) of the model
11599
- `stretch` - A horizontal stretch factor for the model
@@ -122,9 +106,7 @@ y = background + scale * erfc(stretch * (x - cen))
122106

123107
![ERFCModel](./images_fits/erfc.png)
124108

125-
## Top Hat (TopHat)
126-
127-
API Reference: [`TopHat`](ibex_bluesky_core.fitting.TopHat)
109+
## Top Hat ({py:obj}`~ibex_bluesky_core.fitting.TopHat`)
128110

129111
- `cen` - The centre (x) of the model
130112
- `width` - How wide the 'hat' is
@@ -141,9 +123,7 @@ y =
141123

142124
![TopHatModel](./images_fits/tophat.png)
143125

144-
## Trapezoid
145-
146-
API Reference: [`Trapezoid`](ibex_bluesky_core.fitting.Trapezoid)
126+
## {py:obj}`~ibex_bluesky_core.fitting.Trapezoid`
147127

148128
- `cen` - The centre (x) of the model
149129
- `gradient` - How steep the edges of the trapezoid are
@@ -163,9 +143,7 @@ y = \min(g(x), \text{background} + \text{height})
163143

164144
![TrapezoidModel](./images_fits/trapezoid.png)
165145

166-
## Negative Trapezoid
167-
168-
API Reference: [`NegativeTrapezoid`](ibex_bluesky_core.fitting.NegativeTrapezoid)
146+
## {py:obj}`~ibex_bluesky_core.fitting.NegativeTrapezoid`
169147

170148
This model is the same shape as the trapezoid described above, but with a negative height.
171149

@@ -185,9 +163,7 @@ g(x) = \max(f(x), \text{background} - \text{height})
185163
y = \min(g(x), \text{background})
186164
```
187165

188-
## Muon Momentum
189-
190-
API Reference: [`MuonMomentum`](ibex_bluesky_core.fitting.MuonMomentum)
166+
## Muon Momentum ({py:obj}`~ibex_bluesky_core.fitting.MuonMomentum`)
191167

192168
Fits data from a momentum scan, it is designed for the specific use case of scanning over magnet current on muon instruments.
193169

doc/callbacks/isiscallbacks.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{#icc}
2-
# ISIS Standard Callbacks ({py:obj}`ISISCallbacks <ibex_bluesky_core.callbacks.ISISCallbacks>`)
2+
# ISIS Standard Callbacks ({py:obj}`~ibex_bluesky_core.callbacks.ISISCallbacks`)
33

4-
{py:obj}`ISISCallbacks <ibex_bluesky_core.callbacks.ISISCallbacks>` is a helper to add common callbacks to 1-dimensional scans with a single dependent variable (with an optional uncertainty) and a single independent variable. This should avoid some repetition as you don't need to pass `x` and `y` to each callback.
4+
{py:obj}`~ibex_bluesky_core.callbacks.ISISCallbacks` is a helper to add common callbacks to 1-dimensional scans with a single dependent variable (with an optional uncertainty) and a single independent variable. This should avoid some repetition as you don't need to pass `x` and `y` to each callback.
55

66
It is composed of the following callbacks:
77
- {py:obj}`ibex_bluesky_core.callbacks.LiveFit`
@@ -16,13 +16,13 @@ It is composed of the following callbacks:
1616

1717
## Live table
1818

19-
A {external+bluesky:py:obj}`LiveTable <bluesky.callbacks.LiveTable>` is enabled by default. This will show the values of X and Y according to their `seq_num` or event order.
19+
A {external+bluesky:py:obj}`~bluesky.callbacks.LiveTable` is enabled by default. This will show the values of X and Y according to their `seq_num` or event order.
2020

2121
You can pass optional fields to be displayed in the LiveTable with the `fields_for_live_table` argument or the `measured_fields` argument if you want the fields to be put in the human-readable file (see {ref}`below. <hr_files_icc>`)
2222

2323
## Plotting
2424

25-
Plotting is enabled by default and running a plan with {py:obj}`ISISCallbacks <ibex_bluesky_core.callbacks.ISISCallbacks>` will close any current active plots.
25+
Plotting is enabled by default and running a plan with {py:obj}`~ibex_bluesky_core.callbacks.ISISCallbacks` will close any current active plots.
2626

2727
## Fitting
2828

@@ -32,7 +32,7 @@ After a scan has run you can get the fitting results by using the {py:obj}`live_
3232

3333
## Centre of mass and Peak Stats
3434

35-
These are both enabled by default. To access {py:obj}`Centre of Mass <ibex_bluesky_core.callbacks.CentreOfMass>` information after a plan, use the {py:obj}`com <ibex_bluesky_core.callbacks.ISISCallbacks.com>` property.
35+
These are both enabled by default. To access {py:obj}`~ibex_bluesky_core.callbacks.CentreOfMass` information after a plan, use the {py:obj}`com <ibex_bluesky_core.callbacks.ISISCallbacks.com>` property.
3636

3737
To access {external+bluesky:py:obj}`Peak Stats <bluesky.callbacks.fitting.PeakStats>` after a plan use the {py:obj}`peak_stats <ibex_bluesky_core.callbacks.ISISCallbacks.peak_stats>` property.
3838

doc/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
"sphinx.ext.viewcode",
4646
# Mermaid diagrams
4747
"sphinxcontrib.mermaid",
48+
# Collapsible sections
49+
'sphinx_toolbox.collapse',
4850
]
4951
mermaid_d3_zoom = True
5052
napoleon_google_docstring = True

0 commit comments

Comments
 (0)