Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
geomag-algorithms
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ghsc
National Geomagnetism Program
geomag-algorithms
Commits
1ef1b635
Commit
1ef1b635
authored
6 years ago
by
Jeremy M Fee
Committed by
Claycomb, Abram Earl
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for pad_and_trim_trace(), fix trim logic error using ceil
parent
016351c7
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
geomagio/TimeseriesUtility.py
+7
-7
7 additions, 7 deletions
geomagio/TimeseriesUtility.py
test/TimeseriesUtility_test.py
+52
-0
52 additions, 0 deletions
test/TimeseriesUtility_test.py
with
59 additions
and
7 deletions
geomagio/TimeseriesUtility.py
+
7
−
7
View file @
1ef1b635
"""
Timeseries Utilities
"""
from
builtins
import
range
from
datetime
import
datetime
import
math
import
numpy
import
obspy.core
...
...
@@ -366,9 +367,9 @@ def pad_and_trim_trace(trace, starttime, endtime):
trace_delta
=
trace
.
stats
.
delta
if
trace_starttime
<
starttime
:
# trim to starttime
cnt
=
int
((
starttime
-
trace_starttime
)
/
trace_delta
)
+
1
cnt
=
math
.
ceil
((
starttime
-
trace_starttime
)
/
trace_delta
)
if
cnt
>
0
:
trace
.
data
=
trace
.
data
[
cnt
+
1
:]
trace
.
data
=
trace
.
data
[
cnt
:]
trace_starttime
=
trace_starttime
+
trace_delta
*
cnt
trace
.
stats
.
starttime
=
trace_starttime
elif
trace_starttime
>
starttime
:
...
...
@@ -381,11 +382,10 @@ def pad_and_trim_trace(trace, starttime, endtime):
trace_starttime
=
trace_starttime
-
trace_delta
*
cnt
trace
.
stats
.
starttime
=
trace_starttime
if
trace_endtime
>
endtime
:
# trim to endtime
cnt
=
int
((
trace_endtime
-
endtime
)
/
trace_delta
)
if
cnt
>
0
:
trace
.
data
=
trace
.
data
[:
-
cnt
]
trace
.
stats
.
npts
=
len
(
trace
.
data
)
# trim to endtime, at least 1 sample to remove
cnt
=
math
.
ceil
((
trace_endtime
-
endtime
)
/
trace_delta
)
trace
.
data
=
trace
.
data
[:
-
cnt
]
trace
.
stats
.
npts
=
len
(
trace
.
data
)
elif
trace_endtime
<
endtime
:
# pad to endtime
cnt
=
int
((
endtime
-
trace_endtime
)
/
trace
.
stats
.
delta
)
...
...
This diff is collapsed.
Click to expand it.
test/TimeseriesUtility_test.py
+
52
−
0
View file @
1ef1b635
...
...
@@ -8,6 +8,7 @@ from geomagio import TimeseriesUtility
from
obspy.core
import
Stream
,
Stats
,
Trace
,
UTCDateTime
assert_almost_equal
=
numpy
.
testing
.
assert_almost_equal
assert_array_equal
=
numpy
.
testing
.
assert_array_equal
def
test_create_empty_trace
():
...
...
@@ -259,6 +260,57 @@ def test_pad_timeseries():
assert_equals
(
numpy
.
isnan
(
trace1
.
data
[
0
]),
numpy
.
isnan
(
numpy
.
NaN
))
def
test_pad_and_trim_trace
():
"""
TimeseriesUtility_test.test_pad_and_trim_trace()
"""
trace
=
_create_trace
([
0
,
1
,
2
,
3
,
4
],
'
X
'
,
UTCDateTime
(
"
2018-01-01
"
))
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:04:00Z
"
))
# starttime between first and second sample
# expect first sample to be removed, start at next sample, end at same
TimeseriesUtility
.
pad_and_trim_trace
(
trace
,
starttime
=
UTCDateTime
(
"
2018-01-01T00:00:30Z
"
),
endtime
=
trace
.
stats
.
endtime
)
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2018-01-01T00:01:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:04:00Z
"
))
assert_array_equal
(
trace
.
data
,
[
1
,
2
,
3
,
4
])
# endtime between last and second to last samples
TimeseriesUtility
.
pad_and_trim_trace
(
trace
,
starttime
=
UTCDateTime
(
"
2018-01-01T00:00:30Z
"
),
endtime
=
UTCDateTime
(
"
2018-01-01T00:03:50Z
"
))
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2018-01-01T00:01:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:03:00Z
"
))
assert_array_equal
(
trace
.
data
,
[
1
,
2
,
3
])
# pad outward
TimeseriesUtility
.
pad_and_trim_trace
(
trace
,
starttime
=
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
),
endtime
=
UTCDateTime
(
"
2018-01-01T00:05:00Z
"
))
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:05:00Z
"
))
assert_array_equal
(
trace
.
data
,
[
numpy
.
nan
,
1
,
2
,
3
,
numpy
.
nan
,
numpy
.
nan
])
# remove exactly one sample
TimeseriesUtility
.
pad_and_trim_trace
(
trace
,
starttime
=
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
),
endtime
=
UTCDateTime
(
"
2018-01-01T00:04:00Z
"
))
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:04:00Z
"
))
assert_array_equal
(
trace
.
data
,
[
numpy
.
nan
,
1
,
2
,
3
,
numpy
.
nan
])
# pad start and trim end
TimeseriesUtility
.
pad_and_trim_trace
(
trace
,
starttime
=
UTCDateTime
(
"
2017-12-31T23:58:59Z
"
),
endtime
=
UTCDateTime
(
"
2018-01-01T00:03:00Z
"
))
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2017-12-31T23:59:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:03:00Z
"
))
assert_array_equal
(
trace
.
data
,
[
numpy
.
nan
,
numpy
.
nan
,
1
,
2
,
3
])
# pad end and trim start
TimeseriesUtility
.
pad_and_trim_trace
(
trace
,
starttime
=
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
),
endtime
=
UTCDateTime
(
"
2018-01-01T00:04:00Z
"
))
assert_equals
(
trace
.
stats
.
starttime
,
UTCDateTime
(
"
2018-01-01T00:00:00Z
"
))
assert_equals
(
trace
.
stats
.
endtime
,
UTCDateTime
(
"
2018-01-01T00:04:00Z
"
))
assert_array_equal
(
trace
.
data
,
[
numpy
.
nan
,
1
,
2
,
3
,
numpy
.
nan
])
def
_create_trace
(
data
,
channel
,
starttime
,
delta
=
60.
):
stats
=
Stats
()
stats
.
channel
=
channel
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment