Skip to content

Commit 65ec2c6

Browse files
committed
add fitting methods as free functions
1 parent d215587 commit 65ec2c6

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

doc/fitting/fitting.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Below is a full example showing how to use standard `matplotlib` & `bluesky` fun
1515
```py
1616
import matplotlib.pyplot as plt
1717
from ibex_bluesky_core.callbacks import LiveFit, LivePlot
18-
from ibex_bluesky_core.fitting import Gaussian
18+
from ibex_bluesky_core.fitting import gaussian
1919
from bluesky.callbacks import LiveFitPlot
2020

2121
# Create a new figure to plot onto.
@@ -25,7 +25,7 @@ ax = plt.gca()
2525
# ax is shared by fit_callback and plot_callback
2626

2727
plot_callback = LivePlot(y="y_signal", x="x_signal", ax=ax, yerr="yerr_signal")
28-
fit_callback = LiveFit(Gaussian.fit(), y="y_signal", x="x_signal", yerr="yerr_signal", update_every=0.5)
28+
fit_callback = LiveFit(gaussian(), y="y_signal", x="x_signal", yerr="yerr_signal", update_every=0.5)
2929
# Using the yerr parameter allows you to use error bars.
3030
# update_every = in seconds, how often to recompute the fit. If `None`, do not compute until the end. Default is 1.
3131
fit_plot_callback = LiveFitPlot(fit_callback, ax=ax, color="r")
@@ -71,6 +71,7 @@ We support **standard fits** for the following trends in data. See [Standard Fit
7171
-------
7272

7373
Each of the above fit classes has a `.fit()` which returns an object of type [`FitMethod`](ibex_bluesky_core.fitting.FitMethod). This tells [`LiveFit`](ibex_bluesky_core.callbacks.LiveFit) how to perform fitting on the data. [`FitMethod`](ibex_bluesky_core.fitting.FitMethod) is defined in `ibex_bluesky_core.fitting`.
74+
Shorthand for this is just to use the lower-case version ie. `gaussian()` instead of `Gaussian().fit()`.
7475

7576
There are *two* ways that you can choose how to fit a model to your data:
7677

@@ -92,7 +93,7 @@ The `[FIT].fit()` function will pass the [`FitMethod`](ibex_bluesky_core.fitting
9293
**Note:** that for the fits in the above table that require parameters, you will need to pass value(s) to their `.fit` method. For example Polynomial fitting:
9394

9495
```py
95-
lf = LiveFit(Polynomial.fit(3), y="y_signal", x="x_signal", update_every=0.5)
96+
lf = LiveFit(polynomial(3), y="y_signal", x="x_signal", update_every=0.5)
9697
# For a polynomial of degree 3
9798
```
9899

manual_system_tests/dae_scan.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
RunPerPointController,
2020
SimpleDae,
2121
)
22-
from ibex_bluesky_core.fitting import Linear
22+
from ibex_bluesky_core.fitting import linear
2323
from ibex_bluesky_core.run_engine import get_run_engine
2424
from ibex_bluesky_core.utils import get_pv_prefix
2525

@@ -71,7 +71,7 @@ def dae_scan_plan() -> Generator[Msg, None, None]:
7171
x=block.name,
7272
y=reducer.intensity.name,
7373
yerr=reducer.intensity_stddev.name,
74-
fit=Linear.fit(),
74+
fit=linear(),
7575
measured_fields=[
7676
controller.run_number.name,
7777
reducer.det_counts.name,

src/ibex_bluesky_core/callbacks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def dae_scan_plan():
9393
x=block.name,
9494
y=reducer.intensity.name,
9595
yerr=reducer.intensity_stddev.name,
96-
fit=Linear.fit(),
96+
fit=linear(),
9797
...
9898
)
9999
...

src/ibex_bluesky_core/fitting.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
"SlitScan",
2525
"TopHat",
2626
"Trapezoid",
27+
"dampedosc",
28+
"erf",
29+
"erfc",
30+
"gaussian",
31+
"linear",
32+
"lorentzian",
33+
"polynomial",
34+
"slitscan",
35+
"tophat",
36+
"trapezoid",
2737
]
2838

2939

@@ -593,3 +603,24 @@ def guess(
593603
return init_guess
594604

595605
return guess
606+
607+
608+
gaussian = Gaussian.fit
609+
610+
lorentzian = Lorentzian.fit
611+
612+
linear = Linear.fit
613+
614+
polynomial = Polynomial.fit
615+
616+
dampedosc = DampedOsc.fit
617+
618+
slitscan = SlitScan.fit
619+
620+
erf = ERF.fit
621+
622+
erfc = ERFC.fit
623+
624+
tophat = TopHat.fit
625+
626+
trapezoid = Trapezoid.fit

src/ibex_bluesky_core/plans/reflectometry/_det_map_align.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
Waiter,
3030
check_dae_strategies,
3131
)
32-
from ibex_bluesky_core.fitting import Gaussian
32+
from ibex_bluesky_core.fitting import gaussian
3333
from ibex_bluesky_core.plan_stubs import call_qt_aware
3434

3535
__all__ = ["DetMapAlignResult", "angle_scan_plan", "height_and_angle_scan_plan"]
@@ -52,7 +52,7 @@ def _height_scan_callback_and_fit(
5252
x=height.name,
5353
y=intensity,
5454
yerr=f"{intensity}_err",
55-
fit=Gaussian().fit(),
55+
fit=gaussian(),
5656
add_peak_stats=False,
5757
ax=ax,
5858
live_fit_logger_postfix="_height",
@@ -81,7 +81,7 @@ def _angle_scan_callback_and_fit(
8181
x=angle_name,
8282
y=counts_name,
8383
yerr=f"{counts_name}_err",
84-
fit=Gaussian().fit(),
84+
fit=gaussian(),
8585
add_peak_stats=False,
8686
add_table_cb=False,
8787
ax=ax,

0 commit comments

Comments
 (0)