Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pysp2/util/normalized_derivative_method.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import xarray as xr

def central_difference(S, num_records=None, normalize=True):
def central_difference(S, num_records=None, normalize=True, baseline_to_zero=True):

"""
Compute fourth order derivative S'(t) using the
Expand Down Expand Up @@ -39,6 +39,11 @@ def central_difference(S, num_records=None, normalize=True):

for ch in channels:
y = S[ch].isel(event_index=slice(0, num_records)).values

# Baseline shift: make each record's minimum be 0
if baseline_to_zero:
y_min = np.nanmin(y, axis=1, keepdims=True) # shape (n_records, 1)
y = y - y_min
d = np.full_like(y, np.nan, dtype=np.float64)

# Interior points (vectorized)
Expand Down
Binary file modified tests/baseline/test_plot_normalized_derivative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/test_ndm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def test_central_difference():
my_sp2b = pysp2.io.read_sp2(pysp2.testing.EXAMPLE_SP2B)
my_ini = pysp2.io.read_config(pysp2.testing.EXAMPLE_INI)
my_binary = pysp2.util.gaussian_fit(my_sp2b, my_ini, parallel=False)
dSdt = pysp2.util.central_difference(my_binary, normalize=False)
dSdt = pysp2.util.central_difference(my_binary, normalize=False, baseline_to_zero=False)

np.testing.assert_almost_equal(dSdt['Data_ch4'].isel(event_index=5876, time=0).item(),
8.3333333333e6, decimal=2)
Expand All @@ -15,7 +15,7 @@ def test_central_difference():
1.5e7, decimal=2)
assert np.isfinite(dSdt).all()

dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True)
dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True, baseline_to_zero=False)
np.testing.assert_almost_equal(dSdt_norm['Data_ch4'].isel(event_index=5876, time=0).item(),
8.3333333333e6/-30168, decimal=2)
np.testing.assert_almost_equal(dSdt_norm['Data_ch4'].isel(event_index=5876, time=99).item(),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_plot_normalized_derivative():
my_sp2b = pysp2.io.read_sp2(pysp2.testing.EXAMPLE_SP2B)
my_ini = pysp2.io.read_config(pysp2.testing.EXAMPLE_INI)
my_binary = pysp2.util.gaussian_fit(my_sp2b, my_ini, parallel=False)
dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True)
dSdt_norm = pysp2.util.central_difference(my_binary, normalize=True, baseline_to_zero=True)

# Test the plotting function for channel 0 and record number 2
ax = plot_normalized_derivative(dSdt_norm, record_no=304, chn=0)
ax = plot_normalized_derivative(dSdt_norm, record_no=499, chn=0)
fig = ax.figure

return fig
Loading