Skip to content
Snippets Groups Projects
Commit 1f6bc71f authored by Erin (Josh) Rigler's avatar Erin (Josh) Rigler
Browse files

TimeseriesUtility.pad_and_trim_trace trim fix

Applied the same fix to the trim logic in method
TimeseriesUtility.pad_and_trim_trace() as previously applied
to the pad logic. Added a unit test that would have failed
using the old logic.
parent 09ad155f
No related branches found
No related tags found
No related merge requests found
...@@ -507,7 +507,7 @@ def pad_and_trim_trace(trace, starttime, endtime): ...@@ -507,7 +507,7 @@ def pad_and_trim_trace(trace, starttime, endtime):
trace_delta = trace.stats.delta trace_delta = trace.stats.delta
if trace_starttime < starttime: if trace_starttime < starttime:
# trim to starttime # trim to starttime
cnt = int(math.ceil((starttime - trace_starttime) / trace_delta)) cnt = int(math.ceil(round((starttime - trace_starttime) / trace_delta, 6)))
if cnt > 0: if cnt > 0:
trace.data = trace.data[cnt:] trace.data = trace.data[cnt:]
trace_starttime = trace_starttime + trace_delta * cnt trace_starttime = trace_starttime + trace_delta * cnt
...@@ -515,6 +515,7 @@ def pad_and_trim_trace(trace, starttime, endtime): ...@@ -515,6 +515,7 @@ def pad_and_trim_trace(trace, starttime, endtime):
elif trace_starttime > starttime: elif trace_starttime > starttime:
# pad to starttime # pad to starttime
cnt = int(round((trace_starttime - starttime) / trace_delta, 6)) cnt = int(round((trace_starttime - starttime) / trace_delta, 6))
# cnt = int((trace_starttime - starttime) / trace_delta)
if cnt > 0: if cnt > 0:
trace.data = numpy.concatenate( trace.data = numpy.concatenate(
[numpy.full(cnt, numpy.nan, dtype=numpy.float64), trace.data] [numpy.full(cnt, numpy.nan, dtype=numpy.float64), trace.data]
...@@ -523,12 +524,13 @@ def pad_and_trim_trace(trace, starttime, endtime): ...@@ -523,12 +524,13 @@ def pad_and_trim_trace(trace, starttime, endtime):
trace.stats.starttime = trace_starttime trace.stats.starttime = trace_starttime
if trace_endtime > endtime: if trace_endtime > endtime:
# trim to endtime, at least 1 sample to remove # trim to endtime, at least 1 sample to remove
cnt = int(math.ceil((trace_endtime - endtime) / trace_delta)) cnt = int(math.ceil(round((trace_endtime - endtime) / trace_delta, 6)))
trace.data = trace.data[:-cnt] trace.data = trace.data[:-cnt]
trace.stats.npts = len(trace.data) trace.stats.npts = len(trace.data)
elif trace_endtime < endtime: elif trace_endtime < endtime:
# pad to endtime # pad to endtime
cnt = int(round((endtime - trace_endtime) / trace.stats.delta, 6)) cnt = int(round((endtime - trace_endtime) / trace.stats.delta, 6))
# cnt = int((endtime - trace_endtime) / trace.stats.delta)
if cnt > 0: if cnt > 0:
trace.data = numpy.concatenate( trace.data = numpy.concatenate(
[trace.data, numpy.full(cnt, numpy.nan, dtype=numpy.float64)] [trace.data, numpy.full(cnt, numpy.nan, dtype=numpy.float64)]
......
...@@ -425,6 +425,36 @@ def test_pad_and_trim_trace(): ...@@ -425,6 +425,36 @@ def test_pad_and_trim_trace():
assert_array_equal(trace.data, [numpy.nan, 1, 2, 3, numpy.nan]) assert_array_equal(trace.data, [numpy.nan, 1, 2, 3, numpy.nan])
def test_pad_and_trim_trace_fixfloat():
"""TimeseriesUtility_test.test_pad_and_trim_trace_fixfloat()
This tests whether pad_and_trim_trace() handles a known
floating point precision trouble-maker encountered with
UTCDateTimes correctly.
"""
trace = _create_trace(
numpy.linspace(1, 56, 56), "X", UTCDateTime("2020-05-28T15:53:50.7"), delta=0.1
)
assert_equal(trace.stats.starttime, UTCDateTime("2020-05-28T15:53:50.7"))
assert_equal(trace.stats.endtime, UTCDateTime("2020-05-28T15:53:56.2"))
# This should create a 70 sample trace of 10 Hz data, inclusive of the
# known startime and endtime. Early versions of pad_and_trim_trace() did
# not handle this scenario correctly, returning 68 samples, and missing
# both the 1st and last of the expected samples.
TimeseriesUtility.pad_and_trim_trace(
trace,
starttime=UTCDateTime("2020-05-28T15:53:50.0"),
endtime=UTCDateTime("2020-05-28T15:53:56.9"),
)
assert_equal(trace.stats.starttime, UTCDateTime("2020-05-28T15:53:50.0"))
assert_equal(trace.stats.endtime, UTCDateTime("2020-05-28T15:53:56.9"))
assert_array_equal(
trace.data,
numpy.concatenate(
([numpy.nan] * 7, numpy.linspace(1, 56, 56), [numpy.nan] * 7)
),
)
def _create_trace(data, channel, starttime, delta=60.0): def _create_trace(data, channel, starttime, delta=60.0):
stats = Stats() stats = Stats()
stats.channel = channel stats.channel = channel
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment