Skip to content

Commit

Permalink
Fix rebin error when nan present in raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
bjheinen committed May 13, 2024
1 parent 2754296 commit 6704c04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions LiquidDiffract/core/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def rebin_data(x, y, dx=0.02, x_lim=None, extrapolate_mode='fill'):
x_rebin = np.arange(x_lim[0], x_lim[1], dx)
else:
x_rebin = np.arange(0, x[-1], dx)
# Check for any nans in y and remove via interpolation
if np.isnan(np.sum(y)):
y = interp_nan(y)
if extrapolate_mode == 'fill':
f_interp = scipy.interpolate.interp1d(x, y,
kind='cubic',
Expand Down
7 changes: 5 additions & 2 deletions tests/core/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,17 @@ def test_rebin_data(self):

def test_rebin_nan(self):
# Test nan values in y have little effect
x = np.arange(0, 12, 0.001)
x = np.arange(0, 12.0, 0.001)
y = np.cos(x)
# Inject single np.nan
y[500] = np.nan
expected_rebin_y = np.cos(np.arange(0, 12, 0.02))
_, rebin_y = data_utils.rebin_data(x, y, dx=0.02)
# Check interpolation in rebinning accurate
self.assertArrayEqual(rebin_y, expected_rebin_y)
# y[500] --> rebin_y[25] (0.02/0.001 = 20)
# Check arrays equal skipping interpolated value
self.assertFloatArrayEqual(np.delete(rebin_y, 25), np.delete(expected_rebin_y, 25))
self.assertFloatEqual(rebin_y[25], expected_rebin_y[25], rtol=1e-6)

def test_rebin_extrapolate_fill(self):
x = np.arange(0.34, 12.0, 0.02)
Expand Down

0 comments on commit 6704c04

Please sign in to comment.