Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 9317fad

Browse files
Merge pull request #723 from emthompson-usgs/clipfix
Fix bugs in clipping methods
2 parents 57a9994 + 6cb973d commit 9317fad

7 files changed

Lines changed: 92 additions & 26 deletions

File tree

gmprocess/waveform_processing/clipping/clip_detection.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, st, test_all=False):
3939

4040
def _clean_trace(self, tr):
4141
'''
42-
Helper function to clean the trace
42+
Pre-processing steps.
4343
4444
Args:
4545
tr (StationTrace):
@@ -49,13 +49,7 @@ def _clean_trace(self, tr):
4949
clean_tr (StationTrace):
5050
Cleaned trace.
5151
'''
52-
t_1 = tr.stats.starttime
53-
t_2 = t_1 + 180
54-
clean_tr = tr.copy()
55-
clean_tr.trim(t_1, t_2)
56-
clean_tr.detrend(type='constant')
57-
clean_tr.normalize()
58-
return clean_tr
52+
return tr
5953

6054
def _detect(self, clip_tr):
6155
'''

gmprocess/waveform_processing/clipping/histogram.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,24 @@ def _get_clip_intervals(self, signal, peaks, thresh):
239239
clip_intervals = self._merge_intervals(clip_intervals, 1)
240240
return clip_intervals
241241

242+
def _clean_trace(self, tr):
243+
'''
244+
Pre-processing steps.
245+
246+
Args:
247+
tr (StationTrace):
248+
A single trace in the record.
249+
250+
Returns:
251+
clean_tr (StationTrace):
252+
Cleaned trace.
253+
'''
254+
t_1 = tr.stats.starttime
255+
t_2 = t_1 + 180
256+
clean_tr = tr.copy()
257+
clean_tr.trim(t_1, t_2)
258+
return clean_tr
259+
242260
def _detect(self, tr):
243261
'''
244262
Test for clipping using the histogram-based method. This is a slight

gmprocess/waveform_processing/clipping/jerk.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Jerk(ClipDetection):
2727
See parent class.
2828
'''
2929

30-
def __init__(self, st, point_thresh=25, test_all=False):
30+
def __init__(self, st, point_thresh=400, test_all=False):
3131
'''
3232
Constructs all neccessary attributes for the Max_Amp class.
3333
@@ -48,6 +48,24 @@ def __init__(self, st, point_thresh=25, test_all=False):
4848
self.num_outliers = None
4949
self._get_results()
5050

51+
def _clean_trace(self, tr):
52+
'''
53+
Pre-processing steps.
54+
55+
Args:
56+
tr (StationTrace):
57+
A single trace in the record.
58+
59+
Returns:
60+
clean_tr (StationTrace):
61+
Cleaned trace.
62+
'''
63+
t_1 = tr.stats.starttime
64+
t_2 = t_1 + 180
65+
clean_tr = tr.copy()
66+
clean_tr.trim(t_1, t_2)
67+
return clean_tr
68+
5169
def _detect(self, tr):
5270
'''
5371
Check for jerk outliers. Based on method described by:
@@ -65,11 +83,10 @@ def _detect(self, tr):
6583
bool:
6684
Is the trace clipped?
6785
'''
68-
temp_st = self.st
69-
temp_st.differentiate()
70-
if tr.stats.channel[1] == 'H':
71-
temp_st.differentiate()
72-
abs_diff = np.abs(tr.data)
86+
temp_tr = tr.copy()
87+
temp_tr.differentiate()
88+
temp_tr.differentiate()
89+
abs_diff = np.abs(temp_tr.data)
7390
median_x100 = 100 * np.median(abs_diff)
7491
i_jerk, = np.where(abs_diff >= median_x100)
7592
num_outliers = len(i_jerk)

gmprocess/waveform_processing/clipping/ping.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ def __init__(self, st, percent_thresh=0.57, test_all=False):
5151
self.num_outliers = None
5252
self._get_results()
5353

54+
def _clean_trace(self, tr):
55+
'''
56+
Pre-processing steps.
57+
58+
Args:
59+
tr (StationTrace):
60+
A single trace in the record.
61+
62+
Returns:
63+
clean_tr (StationTrace):
64+
Cleaned trace.
65+
'''
66+
t_1 = tr.stats.starttime
67+
t_2 = t_1 + 180
68+
clean_tr = tr.copy()
69+
clean_tr.trim(t_1, t_2)
70+
return clean_tr
71+
5472
def _detect(self, tr):
5573
'''
5674
If any two points differ by more than a threshold, fail the trace.

gmprocess/waveform_processing/clipping/std_dev.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ def __init__(self, st, amp_thresh=0.85, n_std=12, std_thresh=0.001,
6565
self.num_outliers = None
6666
self._get_results()
6767

68+
def _clean_trace(self, tr):
69+
'''
70+
Helper function to clean the trace
71+
72+
Args:
73+
tr (StationTrace):
74+
A single trace in the record.
75+
76+
Returns:
77+
clean_tr (StationTrace):
78+
Cleaned trace.
79+
'''
80+
t_1 = tr.stats.starttime
81+
t_2 = t_1 + 180
82+
clean_tr = tr.copy()
83+
clean_tr.trim(t_1, t_2)
84+
clean_tr.detrend(type='constant')
85+
clean_tr.normalize()
86+
return clean_tr
87+
6888
def _detect(self, tr):
6989
'''
7090
For all points with amplitude greater than amp_thresh, calculate

tests/gmprocess/waveform_processing/clipping/histogram_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
import os
54
import numpy as np
65
from gmprocess.io.read import read_data
76
from gmprocess.io.test_utils import read_data_dir
@@ -25,7 +24,7 @@ def test_num_clip_intervals():
2524

2625
np.testing.assert_equal(
2726
num_clip_intervals,
28-
np.array([0, 9, 37, 10, 17, 7])
27+
np.array([0, 9, 37, 10, 16, 8])
2928
)
3029

3130

@@ -47,11 +46,11 @@ def test_all_num_clip_intervals():
4746
num_clip_intervals,
4847
np.array([[
4948
0, 0, 0],
50-
[0, 9, 17],
49+
[0, 9, 18],
5150
[37, 31, 8],
5251
[10, 23, 0],
53-
[17, 37, 20],
54-
[0, 0, 7]
52+
[16, 40, 21],
53+
[0, 0, 8]
5554
])
5655
)
5756

tests/gmprocess/waveform_processing/clipping/jerk_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_num_outliers():
2525

2626
np.testing.assert_equal(
2727
num_outliers,
28-
np.array([1137, 878, 923, 1111, 1025, 1356])
28+
np.array([1145, 1227, 872, 860, 926, 1205])
2929
)
3030

3131

@@ -46,12 +46,12 @@ def test_all_num_outliers():
4646
np.testing.assert_equal(
4747
num_outliers,
4848
np.array([
49-
[0, 1137, 1060],
50-
[0, 878, 1298],
51-
[0, 923, 806],
52-
[0, 1111, 1277],
53-
[0, 1025, 1071],
54-
[0, 1356, 1542]
49+
[1145, 1137, 1158],
50+
[1227, 878, 1290],
51+
[872, 923, 1158],
52+
[860, 1111, 1381],
53+
[926, 1025, 954],
54+
[1205, 1356, 1600]
5555
])
5656
)
5757

0 commit comments

Comments
 (0)