Skip to content

Commit

Permalink
updates docs & new unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbdoughty committed Oct 29, 2024
1 parent 3ac77ed commit d7700de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
20 changes: 10 additions & 10 deletions doc/fitting/fitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ See the following example on how to define these.

import lmfit

def model(x: float, m: float, c: float) -> float:
def model(x: float, c1: float, c0: float) -> float:

return m * x + c # y = mx + c
return c1 * x + c0 # y = mx + c

def guess(x: npt.NDArray[np.float64], y: npt.NDArray[np.float64]) -> dict[str, lmfit.Parameter]:

Expand All @@ -125,12 +125,12 @@ def guess(x: npt.NDArray[np.float64], y: npt.NDArray[np.float64]) -> dict[str, l
numerator = sum(x * y) - sum(x) * sum(y)
denominator = sum(x**2) - sum(x) ** 2

m = numerator / denominator
c = (sum(y) - m * sum(x)) / len(x)
c1 = numerator / denominator
c0 = (sum(y) - c1 * sum(x)) / len(x)

init_guess = {
"m": lmfit.Parameter("m", m), # gradient
"c": lmfit.Parameter("c", c), # y - intercept
"c1": lmfit.Parameter("c1", c1), # gradient
"c0": lmfit.Parameter("c0", c0), # y - intercept
}

return init_guess
Expand All @@ -155,9 +155,9 @@ This means that aslong as the parameters returned from the guess function match
import lmfit
from ibex_bluesky_core.callbacks.fitting.fitting_utils import Linear

def different_model(x: float, m: float, c: float) -> float:
def different_model(x: float, c1: float, c0: float) -> float:

return m * x + c ** 2 # y = mx + (c ** 2)
return c1 * x + c0 ** 2 # y = mx + (c ** 2)


fit_method = FitMethod(different_model, Linear.guess())
Expand All @@ -179,8 +179,8 @@ from ibex_bluesky_core.callbacks.fitting.fitting_utils import Linear
def different_guess(x: float, m: float, c: float) -> float:

init_guess = {
"m": lmfit.Parameter("m", 1), # gradient
"c": lmfit.Parameter("c", 0), # y - intercept
"c1": lmfit.Parameter("c1", 1), # gradient
"c0": lmfit.Parameter("c0", 0), # y - intercept
}

return init_guess
Expand Down
10 changes: 5 additions & 5 deletions doc/fitting/standard_fits.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

## Linear

- `m` - Gradient
- `c` - (y) Intercept
- `c1` - Gradient
- `c0` - (y) Intercept

```{math}
y = mx + c
y = c_1x + c_0
```

## Polynomial

- `a` ... `d` - Polynomial coefficients
- `cn` ... `c0` - Polynomial coefficients

For a polynomial degree `n`:
```{math}
y = ax^n + bx^n-1 + ... + cx^1 + d
y = c_{n}x^n + c_{n-1}x^n-1 + ... + c_1 * x^1 + c_0
```

## Gaussian
Expand Down
7 changes: 7 additions & 0 deletions tests/callbacks/fitting/test_fitting_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ def test_y_intercept_guess(self):

assert pytest.approx(outp["c0"]) == 0.0

def test_zero_gradient_guess(self):
x = np.array([-1.0, 0.0, 1.0])
y = np.array([0.0, 0.0, 0.0])
outp = Linear.guess()(x, y)

assert pytest.approx(outp["c1"]) == 0.0


class TestPolynomial:
class TestPolynomialModel:
Expand Down

0 comments on commit d7700de

Please sign in to comment.