Skip to content

Commit ed4a9e0

Browse files
authored
Merge branch 'main' into 3_uncertainties_in_plots_and_fits
2 parents b8f9526 + 8734c86 commit ed4a9e0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/ibex_bluesky_core/callbacks/fitting/fitting_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,17 @@ def guess(
331331
# Guessing. gradient of linear-slope part of function
332332
dy = np.gradient(y) # Return array of differences in y
333333
max_dy = np.max(dy) # Return max y difference, this will always be on the upwards slope
334-
dx = x[1] - x[0] # Find x step
334+
dx = abs(x[1] - x[0]) # Find x step
335335
gradient = max_dy / dx
336336

337337
d2y = np.diff(dy) # Double differentiate y to find how gradients change
338338
inflection0 = x[np.argmax(d2y)] # Where there is positive gradient change
339339

340340
background = min(y) # The lowest y value is the background
341-
inflections_diff = -(background - y[np.argmax(y)]) / gradient
341+
if gradient != 0.0:
342+
inflections_diff = -(background - y[np.argmax(y)]) / gradient
343+
else:
344+
inflections_diff = dx # Fallback case, guess one x step
342345
# As linear, using y - y1 = m(x - x1) -> x = (y - y1) / gradient - x1
343346

344347
# The highest y value + slightly more to account for further convergence

tests/callbacks/fitting/test_fitting_methods.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ def test_guess_inflections_diff(self):
447447

448448
assert 3.0 == pytest.approx(outp["inflections_diff"].value, rel=1e-2)
449449

450+
def test_guess_inflections_diff_with_all_zero_data(self):
451+
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0])
452+
y = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
453+
outp = SlitScan.guess()(x, y)
454+
455+
assert 1.0 == pytest.approx(outp["inflections_diff"].value, rel=1e-2)
456+
450457
def test_guess_height_above_inflection1(self):
451458
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0])
452459
y = np.array([1.0, 1.0, 2.0, 3.0, 4.0, 4.0])

0 commit comments

Comments
 (0)