Skip to content

Commit 6a0b915

Browse files
authored
special case for fit trace with all 0 columns, some pep8 fixes (#257)
1 parent 4afc0c8 commit 6a0b915

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ API Changes
99

1010
Bug Fixes
1111
^^^^^^^^^
12+
- When all-zero bin encountered in fit_trace with peak_method=gaussian, the bin peak
13+
will be set to NaN in this caseto work better with DogBoxLSQFitter. [#257]
1214

1315
Other changes
1416
^^^^^^^^^^^^^

specreduce/tests/test_tracing.py

+17
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ def test_fit_trace():
174174
FitTrace(img, bins=ncols + 1)
175175

176176

177+
def test_fit_trace_gaussian_all_zero():
178+
"""
179+
Test fit_trace when peak_method is 'gaussian', which uses DogBoxLSQFitter
180+
for the fit for each bin peak and does not work well with all-zero columns.
181+
In this case, an all zero bin should fall back to NaN to for its'
182+
peak to be filtered out in the final fit for the trace.
183+
"""
184+
img = mk_img(ncols=100)
185+
# add some all-zero columns so there is an all-zero bin
186+
img[:, 10:20] = 0
187+
188+
t = FitTrace(img, bins=10, peak_method='gaussian')
189+
190+
# this is a pretty flat trace, so make sure the fit reflects that
191+
assert np.all((t.trace >= 99) & (t.trace <= 101))
192+
193+
177194
@pytest.mark.filterwarnings("ignore:The fit may be unsuccessful")
178195
@pytest.mark.filterwarnings("ignore:Model is linear in parameters")
179196
class TestMasksTracing:

specreduce/tracing.py

+7
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ def _fit_trace(self, img):
413413

414414
if self.peak_method == "gaussian":
415415

416+
# if bin is fully 0, set bin peak to nan so it doesn't bias the
417+
# all-bin fit. DogBoxLSQFitter, which is always used for the bin
418+
# center fits when peak_method is gaussian, does not like all zeros.
419+
if np.all(z_i == 0.0):
420+
y_bins[i] = np.nan
421+
continue
422+
416423
peak_y_i = ilum2[z_i.argmax()]
417424

418425
yy_i_above_half_max = np.sum(z_i > (z_i.max() / 2))

0 commit comments

Comments
 (0)