-
Notifications
You must be signed in to change notification settings - Fork 530
/
test_trace.py
2734 lines (2506 loc) · 106 KB
/
test_trace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import math
import pickle
import warnings
from copy import deepcopy
from unittest import mock
from packaging.version import parse as parse_version
import numpy as np
import numpy.ma as ma
from obspy import Stream, Trace, __version__, read, read_inventory
from obspy import UTCDateTime as UTC
from obspy.core import Stats
from obspy.io.xseed import Parser
import pytest
class TestTrace:
"""
Test suite for obspy.core.trace.Trace.
"""
@staticmethod
def __remove_processing(tr):
"""
Removes all processing information in the trace object.
Useful for testing.
"""
if "processing" not in tr.stats:
return
del tr.stats.processing
def test_init(self):
"""
Tests the __init__ method of the Trace class.
"""
# NumPy ndarray
tr = Trace(data=np.arange(4))
assert len(tr) == 4
# NumPy masked array
data = np.ma.array([0, 1, 2, 3], mask=[True, True, False, False])
tr = Trace(data=data)
assert len(tr) == 4
# other data types will raise
with pytest.raises(ValueError):
Trace(data=[0, 1, 2, 3])
with pytest.raises(ValueError):
Trace(data=(0, 1, 2, 3))
with pytest.raises(ValueError):
Trace(data='1234')
def test_setattr(self):
"""
Tests the __setattr__ method of the Trace class.
"""
# NumPy ndarray
tr = Trace()
tr.data = np.arange(4)
assert len(tr) == 4
# NumPy masked array
tr = Trace()
tr.data = np.ma.array([0, 1, 2, 3], mask=[True, True, False, False])
assert len(tr) == 4
# other data types will raise
tr = Trace()
with pytest.raises(ValueError):
tr.__setattr__('data', [0, 1, 2, 3])
with pytest.raises(ValueError):
tr.__setattr__('data', (0, 1, 2, 3))
with pytest.raises(ValueError):
tr.__setattr__('data', '1234')
def test_len(self):
"""
Tests the __len__ and count methods of the Trace class.
"""
trace = Trace(data=np.arange(1000))
assert len(trace) == 1000
assert trace.count() == 1000
def test_mul(self):
"""
Tests the __mul__ method of the Trace class.
"""
tr = Trace(data=np.arange(10))
st = tr * 5
assert len(st) == 5
# you may only multiply using an integer
with pytest.raises(TypeError):
tr.__mul__(2.5)
with pytest.raises(TypeError):
tr.__mul__('1234')
def test_truediv(self):
"""
Tests the __truediv__ method of the Trace class.
"""
tr = Trace(data=np.arange(1000))
st = tr / 5
assert len(st) == 5
assert len(st[0]) == 200
# you may only multiply using an integer
with pytest.raises(TypeError):
tr.__truediv__(2.5)
with pytest.raises(TypeError):
tr.__truediv__('1234')
def test_ltrim(self):
"""
Tests the ltrim method of the Trace class.
"""
# set up
trace = Trace(data=np.arange(1000))
start = UTC(2000, 1, 1, 0, 0, 0, 0)
trace.stats.starttime = start
trace.stats.sampling_rate = 200.0
end = UTC(2000, 1, 1, 0, 0, 4, 995000)
# verify
trace.verify()
# UTCDateTime/int/float required
with pytest.raises(TypeError):
trace._ltrim('1234')
with pytest.raises(TypeError):
trace._ltrim([1, 2, 3, 4])
# ltrim 100 samples
tr = deepcopy(trace)
tr._ltrim(0.5)
tr.verify()
np.testing.assert_array_equal(tr.data[0:5],
np.array([100, 101, 102, 103, 104]))
assert len(tr.data) == 900
assert tr.stats.npts == 900
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start + 0.5
assert tr.stats.endtime == end
# ltrim 202 samples
tr = deepcopy(trace)
tr._ltrim(1.010)
tr.verify()
np.testing.assert_array_equal(tr.data[0:5],
np.array([202, 203, 204, 205, 206]))
assert len(tr.data) == 798
assert tr.stats.npts == 798
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start + 1.010
assert tr.stats.endtime == end
# ltrim to UTCDateTime
tr = deepcopy(trace)
tr._ltrim(UTC(2000, 1, 1, 0, 0, 1, 10000))
tr.verify()
np.testing.assert_array_equal(tr.data[0:5],
np.array([202, 203, 204, 205, 206]))
assert len(tr.data) == 798
assert tr.stats.npts == 798
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start + 1.010
assert tr.stats.endtime == end
# some sanity checks
# negative start time as datetime
tr = deepcopy(trace)
tr._ltrim(start - 1, pad=True)
tr.verify()
assert tr.stats.starttime == start - 1
np.testing.assert_array_equal(trace.data, tr.data[200:])
assert tr.stats.endtime == trace.stats.endtime
# negative start time as integer
tr = deepcopy(trace)
tr._ltrim(-100, pad=True)
tr.verify()
assert tr.stats.starttime == start - 100
delta = 100 * trace.stats.sampling_rate
np.testing.assert_array_equal(trace.data, tr.data[int(delta):])
assert tr.stats.endtime == trace.stats.endtime
# start time > end time
tr = deepcopy(trace)
tr._ltrim(trace.stats.endtime + 100)
tr.verify()
assert tr.stats.starttime == trace.stats.endtime + 100
np.testing.assert_array_equal(tr.data, np.empty(0))
assert tr.stats.endtime == tr.stats.starttime
# start time == end time
tr = deepcopy(trace)
tr._ltrim(5)
tr.verify()
assert tr.stats.starttime == trace.stats.starttime + 5
np.testing.assert_array_equal(tr.data, np.empty(0))
assert tr.stats.endtime == tr.stats.starttime
# start time == end time
tr = deepcopy(trace)
tr._ltrim(5.1)
tr.verify()
assert tr.stats.starttime == trace.stats.starttime + 5.1
np.testing.assert_array_equal(tr.data, np.empty(0))
assert tr.stats.endtime == tr.stats.starttime
def test_rtrim(self):
"""
Tests the rtrim method of the Trace class.
"""
# set up
trace = Trace(data=np.arange(1000))
start = UTC(2000, 1, 1, 0, 0, 0, 0)
trace.stats.starttime = start
trace.stats.sampling_rate = 200.0
end = UTC(2000, 1, 1, 0, 0, 4, 995000)
trace.verify()
# UTCDateTime/int/float required
with pytest.raises(TypeError):
trace._rtrim('1234')
with pytest.raises(TypeError):
trace._rtrim([1, 2, 3, 4])
# rtrim 100 samples
tr = deepcopy(trace)
tr._rtrim(0.5)
tr.verify()
np.testing.assert_array_equal(tr.data[-5:],
np.array([895, 896, 897, 898, 899]))
assert len(tr.data) == 900
assert tr.stats.npts == 900
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start
assert tr.stats.endtime == end - 0.5
# rtrim 202 samples
tr = deepcopy(trace)
tr._rtrim(1.010)
tr.verify()
np.testing.assert_array_equal(tr.data[-5:],
np.array([793, 794, 795, 796, 797]))
assert len(tr.data) == 798
assert tr.stats.npts == 798
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start
assert tr.stats.endtime == end - 1.010
# rtrim 1 minute via UTCDateTime
tr = deepcopy(trace)
tr._rtrim(UTC(2000, 1, 1, 0, 0, 3, 985000))
tr.verify()
np.testing.assert_array_equal(tr.data[-5:],
np.array([793, 794, 795, 796, 797]))
assert len(tr.data) == 798
assert tr.stats.npts == 798
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start
assert tr.stats.endtime == end - 1.010
# some sanity checks
# negative end time
tr = deepcopy(trace)
t = UTC(1999, 12, 31)
tr._rtrim(t)
tr.verify()
assert tr.stats.endtime == t
np.testing.assert_array_equal(tr.data, np.empty(0))
# negative end time with given seconds
tr = deepcopy(trace)
tr._rtrim(100)
tr.verify()
assert tr.stats.endtime == trace.stats.endtime - 100
np.testing.assert_array_equal(tr.data, np.empty(0))
assert tr.stats.endtime == tr.stats.starttime
# end time > start time
tr = deepcopy(trace)
t = UTC(2001)
tr._rtrim(t)
tr.verify()
assert tr.stats.endtime == t
np.testing.assert_array_equal(tr.data, np.empty(0))
assert tr.stats.endtime == tr.stats.starttime
# end time > start time given seconds
tr = deepcopy(trace)
tr._rtrim(5.1)
tr.verify()
delta = int(math.floor(round(5.1 * trace.stats.sampling_rate, 7)))
endtime = trace.stats.starttime + trace.stats.delta * \
(trace.stats.npts - delta - 1)
assert tr.stats.endtime == endtime
np.testing.assert_array_equal(tr.data, np.empty(0))
# end time == start time
# returns one sample!
tr = deepcopy(trace)
tr._rtrim(4.995)
tr.verify()
np.testing.assert_array_equal(tr.data, np.array([0]))
assert len(tr.data) == 1
assert tr.stats.npts == 1
assert tr.stats.sampling_rate == 200.0
assert tr.stats.starttime == start
assert tr.stats.endtime == start
def test_rtrim_with_padding(self):
"""
Tests the _rtrim() method of the Trace class with padding. It has
already been tested in the two sided trimming tests. This is just to
have an explicit test. Also tests issue #429.
"""
# set up
trace = Trace(data=np.arange(10))
start = UTC(2000, 1, 1, 0, 0, 0, 0)
trace.stats.starttime = start
trace.stats.sampling_rate = 1.0
trace.verify()
# Pad with no fill_value will mask the additional values.
tr = trace.copy()
end = tr.stats.endtime
tr._rtrim(end + 10, pad=True)
assert tr.stats.endtime == trace.stats.endtime + 10
np.testing.assert_array_equal(tr.data[0:10], np.arange(10))
# Check that the first couple of entries are not masked.
assert not tr.data[0:10].mask.any()
# All the other entries should be masked.
assert tr.data[10:].mask.all()
# Pad with fill_value.
tr = trace.copy()
end = tr.stats.endtime
tr._rtrim(end + 10, pad=True, fill_value=-33)
assert tr.stats.endtime == trace.stats.endtime + 10
# The first ten entries should not have changed.
np.testing.assert_array_equal(tr.data[0:10], np.arange(10))
# The rest should be filled with the fill_value.
np.testing.assert_array_equal(tr.data[10:], np.ones(10) * -33)
def test_trim(self):
"""
Tests the trim method of the Trace class.
"""
# set up
trace = Trace(data=np.arange(1001))
start = UTC(2000, 1, 1, 0, 0, 0, 0)
trace.stats.starttime = start
trace.stats.sampling_rate = 200.0
end = UTC(2000, 1, 1, 0, 0, 5, 0)
trace.verify()
# rtrim 100 samples
trace.trim(0.5, 0.5)
trace.verify()
np.testing.assert_array_equal(trace.data[-5:],
np.array([896, 897, 898, 899, 900]))
np.testing.assert_array_equal(trace.data[:5],
np.array([100, 101, 102, 103, 104]))
assert len(trace.data) == 801
assert trace.stats.npts == 801
assert trace.stats.sampling_rate == 200.0
assert trace.stats.starttime == start + 0.5
assert trace.stats.endtime == end - 0.5
# start time should be before end time
with pytest.raises(ValueError):
trace.trim(end, start)
def test_trim_all_does_not_change_dtype(self):
"""
If a Trace is completely trimmed, e.g. no data samples are remaining,
the dtype should remain unchanged.
A trace with no data samples is not really senseful but the dtype
should not be changed anyways.
"""
# Choose non native dtype.
tr = Trace(np.arange(100, dtype=np.int16))
tr.trim(UTC(10000), UTC(20000))
# Assert the result.
assert len(tr.data) == 0
assert tr.data.dtype == np.int16
def test_add_trace_with_gap(self):
"""
Tests __add__ method of the Trace class.
"""
# set up
tr1 = Trace(data=np.arange(1000))
tr1.stats.sampling_rate = 200
start = UTC(2000, 1, 1, 0, 0, 0, 0)
tr1.stats.starttime = start
tr2 = Trace(data=np.arange(0, 1000)[::-1])
tr2.stats.sampling_rate = 200
tr2.stats.starttime = start + 10
# verify
tr1.verify()
tr2.verify()
# add
trace = tr1 + tr2
# stats
assert trace.stats.starttime == start
assert trace.stats.endtime == start + 14.995
assert trace.stats.sampling_rate == 200
assert trace.stats.npts == 3000
# data
assert len(trace) == 3000
assert trace[0] == 0
assert trace[999] == 999
assert ma.is_masked(trace[1000])
assert ma.is_masked(trace[1999])
assert trace[2000] == 999
assert trace[2999] == 0
# verify
trace.verify()
def test_add_trace_with_overlap(self):
"""
Tests __add__ method of the Trace class.
"""
# set up
tr1 = Trace(data=np.arange(1000))
tr1.stats.sampling_rate = 200
start = UTC(2000, 1, 1, 0, 0, 0, 0)
tr1.stats.starttime = start
tr2 = Trace(data=np.arange(0, 1000)[::-1])
tr2.stats.sampling_rate = 200
tr2.stats.starttime = start + 4
# add
trace = tr1 + tr2
# stats
assert trace.stats.starttime == start
assert trace.stats.endtime == start + 8.995
assert trace.stats.sampling_rate == 200
assert trace.stats.npts == 1800
# data
assert len(trace) == 1800
assert trace[0] == 0
assert trace[799] == 799
assert trace[800].mask
assert trace[999].mask
assert trace[1000] == 799
assert trace[1799] == 0
# verify
trace.verify()
def test_add_same_trace(self):
"""
Tests __add__ method of the Trace class.
"""
# set up
tr1 = Trace(data=np.arange(1001))
# add
trace = tr1 + tr1
# should return exact the same values
assert trace.stats == tr1.stats
np.testing.assert_array_equal(trace.data, tr1.data)
# verify
trace.verify()
def test_add_trace_within_trace(self):
"""
Tests __add__ method of the Trace class.
"""
# set up
tr1 = Trace(data=np.arange(1001))
tr1.stats.sampling_rate = 200
start = UTC(2000, 1, 1, 0, 0, 0, 0)
tr1.stats.starttime = start
tr2 = Trace(data=np.arange(201))
tr2.stats.sampling_rate = 200
tr2.stats.starttime = start + 1
# add
trace = tr1 + tr2
# should return exact the same values like trace 1
assert trace.stats == tr1.stats
mask = np.zeros(len(tr1)).astype(np.bool_)
mask[200:401] = True
np.testing.assert_array_equal(trace.data.mask, mask)
np.testing.assert_array_equal(trace.data.data[:200], tr1.data[:200])
np.testing.assert_array_equal(trace.data.data[401:], tr1.data[401:])
# add the other way around
trace = tr2 + tr1
# should return exact the same values like trace 1
assert trace.stats == tr1.stats
np.testing.assert_array_equal(trace.data.mask, mask)
np.testing.assert_array_equal(trace.data.data[:200], tr1.data[:200])
np.testing.assert_array_equal(trace.data.data[401:], tr1.data[401:])
# verify
trace.verify()
def test_add_gap_and_overlap(self):
"""
Test order of merging traces.
"""
# set up
tr1 = Trace(data=np.arange(1000))
tr1.stats.sampling_rate = 200
start = UTC(2000, 1, 1, 0, 0, 0, 0)
tr1.stats.starttime = start
tr2 = Trace(data=np.arange(1000)[::-1])
tr2.stats.sampling_rate = 200
tr2.stats.starttime = start + 4
tr3 = Trace(data=np.arange(1000)[::-1])
tr3.stats.sampling_rate = 200
tr3.stats.starttime = start + 12
# overlap
overlap = tr1 + tr2
assert len(overlap) == 1800
mask = np.zeros(1800).astype(np.bool_)
mask[800:1000] = True
np.testing.assert_array_equal(overlap.data.mask, mask)
np.testing.assert_array_equal(overlap.data.data[:800], tr1.data[:800])
np.testing.assert_array_equal(overlap.data.data[1000:], tr2.data[200:])
# overlap + gap
overlap_gap = overlap + tr3
assert len(overlap_gap) == 3400
mask = np.zeros(3400).astype(np.bool_)
mask[800:1000] = True
mask[1800:2400] = True
np.testing.assert_array_equal(overlap_gap.data.mask, mask)
np.testing.assert_array_equal(overlap_gap.data.data[:800],
tr1.data[:800])
np.testing.assert_array_equal(overlap_gap.data.data[1000:1800],
tr2.data[200:])
np.testing.assert_array_equal(overlap_gap.data.data[2400:], tr3.data)
# gap
gap = tr2 + tr3
assert len(gap) == 2600
mask = np.zeros(2600).astype(np.bool_)
mask[1000:1600] = True
np.testing.assert_array_equal(gap.data.mask, mask)
np.testing.assert_array_equal(gap.data.data[:1000], tr2.data)
np.testing.assert_array_equal(gap.data.data[1600:], tr3.data)
def test_add_into_gap(self):
"""
Test __add__ method of the Trace class
Adding a trace that fits perfectly into gap in a trace
"""
my_array = np.arange(6, dtype=np.int32)
stats = Stats()
stats.network = 'VI'
stats['starttime'] = UTC(2009, 8, 5, 0, 0, 0)
stats['npts'] = 0
stats['station'] = 'IKJA'
stats['channel'] = 'EHZ'
stats['sampling_rate'] = 1
bigtrace = Trace(data=np.array([], dtype=np.int32), header=stats)
bigtrace_sort = bigtrace.copy()
stats['npts'] = len(my_array)
my_trace = Trace(data=my_array, header=stats)
stats['npts'] = 2
trace1 = Trace(data=my_array[0:2].copy(), header=stats)
stats['starttime'] = UTC(2009, 8, 5, 0, 0, 2)
trace2 = Trace(data=my_array[2:4].copy(), header=stats)
stats['starttime'] = UTC(2009, 8, 5, 0, 0, 4)
trace3 = Trace(data=my_array[4:6].copy(), header=stats)
tr1 = bigtrace
tr2 = bigtrace_sort
for method in [0, 1]:
# Random
bigtrace = tr1.copy()
bigtrace = bigtrace.__add__(trace1, method=method)
bigtrace = bigtrace.__add__(trace3, method=method)
bigtrace = bigtrace.__add__(trace2, method=method)
# Sorted
bigtrace_sort = tr2.copy()
bigtrace_sort = bigtrace_sort.__add__(trace1, method=method)
bigtrace_sort = bigtrace_sort.__add__(trace2, method=method)
bigtrace_sort = bigtrace_sort.__add__(trace3, method=method)
for tr in (bigtrace, bigtrace_sort):
assert isinstance(tr, Trace)
assert not isinstance(tr.data, np.ma.masked_array)
assert (bigtrace_sort.data == my_array).all()
fail_pattern = "\n\tExpected %s\n\tbut got %s"
failinfo = fail_pattern % (my_trace, bigtrace_sort)
failinfo += fail_pattern % (my_trace.data, bigtrace_sort.data)
assert bigtrace_sort == my_trace, failinfo
failinfo = fail_pattern % (my_array, bigtrace.data)
assert (bigtrace.data == my_array).all(), failinfo
failinfo = fail_pattern % (my_trace, bigtrace)
failinfo += fail_pattern % (my_trace.data, bigtrace.data)
assert bigtrace == my_trace, failinfo
for array_ in (bigtrace.data, bigtrace_sort.data):
failinfo = fail_pattern % (my_array.dtype, array_.dtype)
assert my_array.dtype == array_.dtype, failinfo
def test_slice(self):
"""
Tests the slicing of trace objects.
"""
tr = Trace(data=np.arange(10, dtype=np.int32))
mempos = tr.data.ctypes.data
t = tr.stats.starttime
tr1 = tr.slice(t + 2, t + 8)
tr1.data[0] = 10
assert tr.data[2] == 10
assert tr.data.ctypes.data == mempos
assert tr.data[2:9].ctypes.data == tr1.data.ctypes.data
assert tr1.data.ctypes.data - 8 == mempos
# Test the processing information for the slicing. The sliced trace
# should have a processing information showing that it has been
# trimmed. The original trace should have nothing.
tr = Trace(data=np.arange(10, dtype=np.int32))
tr2 = tr.slice(tr.stats.starttime)
assert "processing" not in tr.stats
assert "processing" in tr2.stats
assert "trim" in tr2.stats.processing[0]
def test_slice_no_starttime_or_endtime(self):
"""
Tests the slicing of trace objects with no start time or end time
provided. Compares results against the equivalent trim() operation
"""
tr_orig = Trace(data=np.arange(10, dtype=np.int32))
tr = tr_orig.copy()
# two time points outside the trace and two inside
t1 = tr.stats.starttime - 2
t2 = tr.stats.starttime + 2
t3 = tr.stats.endtime - 3
t4 = tr.stats.endtime + 2
# test 1: only removing data at left side
tr_trim = tr_orig.copy()
tr_trim.trim(starttime=t2)
assert tr_trim == tr.slice(starttime=t2)
tr2 = tr.slice(starttime=t2, endtime=t4)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
# test 2: only removing data at right side
tr_trim = tr_orig.copy()
tr_trim.trim(endtime=t3)
assert tr_trim == tr.slice(endtime=t3)
tr2 = tr.slice(starttime=t1, endtime=t3)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
# test 3: not removing data at all
tr_trim = tr_orig.copy()
tr_trim.trim(starttime=t1, endtime=t4)
tr2 = tr.slice()
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(starttime=t1)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(endtime=t4)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(starttime=t1, endtime=t4)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr_trim.trim()
tr2 = tr.slice()
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(starttime=t1)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(endtime=t4)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(starttime=t1, endtime=t4)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
# test 4: removing data at left and right side
tr_trim = tr_orig.copy()
tr_trim.trim(starttime=t2, endtime=t3)
assert tr_trim == tr.slice(t2, t3)
assert tr_trim == tr.slice(starttime=t2, endtime=t3)
# test 5: no data left after operation
tr_trim = tr_orig.copy()
tr_trim.trim(starttime=t4)
tr2 = tr.slice(starttime=t4)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
tr2 = tr.slice(starttime=t4, endtime=t4 + 1)
self.__remove_processing(tr_trim)
self.__remove_processing(tr2)
assert tr_trim == tr2
def test_slice_nearest_sample(self):
"""
Tests slicing with the nearest sample flag set to on or off.
"""
tr = Trace(data=np.arange(6))
# Samples at:
# 0 10 20 30 40 50
tr.stats.sampling_rate = 0.1
# Nearest sample flag defaults to true.
tr2 = tr.slice(UTC(4), UTC(44))
assert tr2.stats.starttime == UTC(0)
assert tr2.stats.endtime == UTC(40)
tr2 = tr.slice(UTC(8), UTC(48))
assert tr2.stats.starttime == UTC(10)
assert tr2.stats.endtime == UTC(50)
# Setting it to False changes the returned values.
tr2 = tr.slice(UTC(4), UTC(44), nearest_sample=False)
assert tr2.stats.starttime == UTC(10)
assert tr2.stats.endtime == UTC(40)
tr2 = tr.slice(UTC(8), UTC(48), nearest_sample=False)
assert tr2.stats.starttime == UTC(10)
assert tr2.stats.endtime == UTC(40)
def test_trim_floating_point(self):
"""
Tests the slicing of trace objects.
"""
# Create test array that allows for easy testing.
tr = Trace(data=np.arange(11))
org_stats = deepcopy(tr.stats)
org_data = deepcopy(tr.data)
# Save memory position of array.
mem_pos = tr.data.ctypes.data
# Just some sanity tests.
assert tr.stats.starttime == UTC(0)
assert tr.stats.endtime == UTC(10)
# Create temp trace object used for testing.
st = tr.stats.starttime
# This is supposed to include the start and end times and should
# therefore cut right at 2 and 8.
temp = deepcopy(tr)
temp.trim(st + 2.1, st + 7.1)
# Should be identical.
temp2 = deepcopy(tr)
temp2.trim(st + 2.0, st + 8.0)
assert temp.stats.starttime == UTC(2)
assert temp.stats.endtime == UTC(7)
assert temp.stats.npts == 6
assert temp2.stats.npts == 7
# self.assertEqual(temp.stats, temp2.stats)
np.testing.assert_array_equal(temp.data, temp2.data[:-1])
# Create test array that allows for easy testing.
# Check if the data is the same.
assert temp.data.ctypes.data != tr.data[2:9].ctypes.data
np.testing.assert_array_equal(tr.data[2:8], temp.data)
# Using out of bounds times should not do anything but create
# a copy of the stats.
temp = deepcopy(tr)
temp.trim(st - 2.5, st + 200)
# The start and end times should not change.
assert temp.stats.starttime == UTC(0)
assert temp.stats.endtime == UTC(10)
assert temp.stats.npts == 11
# Alter the new stats to make sure the old one stays intact.
temp.stats.starttime = UTC(1000)
assert org_stats == tr.stats
# Check if the data address is not the same, that is it is a copy
assert temp.data.ctypes.data != tr.data.ctypes.data
np.testing.assert_array_equal(tr.data, temp.data)
# Make sure the original Trace object did not change.
np.testing.assert_array_equal(tr.data, org_data)
assert tr.data.ctypes.data == mem_pos
assert tr.stats == org_stats
# Use more complicated times and sampling rate.
tr = Trace(data=np.arange(111))
tr.stats.starttime = UTC(111.11111)
tr.stats.sampling_rate = 50.0
org_stats = deepcopy(tr.stats)
org_data = deepcopy(tr.data)
# Save memory position of array.
mem_pos = tr.data.ctypes.data
# Create temp trace object used for testing.
temp = deepcopy(tr)
temp.trim(UTC(111.22222), UTC(112.99999),
nearest_sample=False)
# Should again be identical. XXX NOT!
temp2 = deepcopy(tr)
temp2.trim(UTC(111.21111), UTC(113.01111),
nearest_sample=False)
np.testing.assert_array_equal(temp.data, temp2.data[1:-1])
# Check stuff.
assert temp.stats.starttime == UTC(111.23111)
assert temp.stats.endtime == UTC(112.991110)
# Check if the data is the same.
temp = deepcopy(tr)
temp.trim(UTC(0), UTC(1000 * 1000))
assert temp.data.ctypes.data != tr.data.ctypes.data
# starttime must be in conformance with sampling rate
t = UTC(111.11111)
assert temp.stats.starttime == t
delta = int((tr.stats.starttime - t) * tr.stats.sampling_rate + .5)
np.testing.assert_array_equal(tr.data, temp.data[delta:delta + 111])
# Make sure the original Trace object did not change.
np.testing.assert_array_equal(tr.data, org_data)
assert tr.data.ctypes.data == mem_pos
assert tr.stats == org_stats
def test_trim_floating_point_with_padding_1(self):
"""
Tests the slicing of trace objects with the use of the padding option.
"""
# Create test array that allows for easy testing.
tr = Trace(data=np.arange(11))
org_stats = deepcopy(tr.stats)
org_data = deepcopy(tr.data)
# Save memory position of array.
mem_pos = tr.data.ctypes.data
# Just some sanity tests.
assert tr.stats.starttime == UTC(0)
assert tr.stats.endtime == UTC(10)
# Create temp trace object used for testing.
st = tr.stats.starttime
# Using out of bounds times should not do anything but create
# a copy of the stats.
temp = deepcopy(tr)
temp.trim(st - 2.5, st + 200, pad=True)
assert temp.stats.starttime.timestamp == -2.0
assert temp.stats.endtime.timestamp == 200
assert temp.stats.npts == 203
mask = np.zeros(203).astype(np.bool_)
mask[:2] = True
mask[13:] = True
np.testing.assert_array_equal(temp.data.mask, mask)
# Alter the new stats to make sure the old one stays intact.
temp.stats.starttime = UTC(1000)
assert org_stats == tr.stats
# Check if the data address is not the same, that is it is a copy
assert temp.data.ctypes.data != tr.data.ctypes.data
np.testing.assert_array_equal(tr.data, temp.data[2:13])
# Make sure the original Trace object did not change.
np.testing.assert_array_equal(tr.data, org_data)
assert tr.data.ctypes.data == mem_pos
assert tr.stats == org_stats
def test_trim_floating_point_with_padding_2(self):
"""
Use more complicated times and sampling rate.
"""
tr = Trace(data=np.arange(111))
tr.stats.starttime = UTC(111.11111)
tr.stats.sampling_rate = 50.0
org_stats = deepcopy(tr.stats)
org_data = deepcopy(tr.data)
# Save memory position of array.
mem_pos = tr.data.ctypes.data
# Create temp trace object used for testing.
temp = deepcopy(tr)
temp.trim(UTC(111.22222), UTC(112.99999),
nearest_sample=False)
# Should again be identical.#XXX not
temp2 = deepcopy(tr)
temp2.trim(UTC(111.21111), UTC(113.01111),
nearest_sample=False)
np.testing.assert_array_equal(temp.data, temp2.data[1:-1])
# Check stuff.
assert temp.stats.starttime == UTC(111.23111)
assert temp.stats.endtime == UTC(112.991110)
# Check if the data is the same.
temp = deepcopy(tr)
temp.trim(UTC(0), UTC(1000 * 1000), pad=True)
assert temp.data.ctypes.data != tr.data.ctypes.data
# starttime must be in conformance with sampling rate
t = UTC(1969, 12, 31, 23, 59, 59, 991110)
assert temp.stats.starttime == t
delta = int((tr.stats.starttime - t) * tr.stats.sampling_rate + .5)
np.testing.assert_array_equal(tr.data, temp.data[delta:delta + 111])
# Make sure the original Trace object did not change.
np.testing.assert_array_equal(tr.data, org_data)
assert tr.data.ctypes.data == mem_pos
assert tr.stats == org_stats
def test_add_sanity(self):
"""
Test sanity checks in __add__ method of the Trace object.
"""
tr = Trace(data=np.arange(10))
# you may only add a Trace object
with pytest.raises(TypeError):
tr.__add__(1234)
with pytest.raises(TypeError):
tr.__add__('1234')
with pytest.raises(TypeError):
tr.__add__([1, 2, 3, 4])
# trace id
tr2 = Trace()
tr2.stats.station = 'TEST'
with pytest.raises(TypeError):
tr.__add__(tr2)
# sample rate
tr2 = Trace()
tr2.stats.sampling_rate = 20
with pytest.raises(TypeError):
tr.__add__(tr2)
# calibration factor
tr2 = Trace()
tr2.stats.calib = 20
with pytest.raises(TypeError):
tr.__add__(tr2)
# data type
tr2 = Trace()
tr2.data = np.arange(10, dtype=np.float32)
with pytest.raises(TypeError):
tr.__add__(tr2)
def test_add_overlaps_default_method(self):
"""
Test __add__ method of the Trace object.
"""
# 1
# overlapping trace with differing data
# Trace 1: 0000000
# Trace 2: 1111111
tr1 = Trace(data=np.zeros(7))
tr2 = Trace(data=np.ones(7))
tr2.stats.starttime = tr1.stats.starttime + 5
# 1 + 2 : 00000--11111
tr = tr1 + tr2
assert isinstance(tr.data, np.ma.masked_array)
assert tr.data.tolist() == [0, 0, 0, 0, 0, None, None, 1, 1, 1, 1, 1]
# 2 + 1 : 00000--11111
tr = tr2 + tr1
assert isinstance(tr.data, np.ma.masked_array)
assert tr.data.tolist() == [0, 0, 0, 0, 0, None, None, 1, 1, 1, 1, 1]
# 2
# overlapping trace with same data
# Trace 1: 0000000
# Trace 2: 0000000
tr1 = Trace(data=np.zeros(7))
tr2 = Trace(data=np.zeros(7))
tr2.stats.starttime = tr1.stats.starttime + 5
# 1 + 2 : 000000000000
tr = tr1 + tr2
assert isinstance(tr.data, np.ndarray)
np.testing.assert_array_equal(tr.data, np.zeros(12))
# 2 + 1 : 000000000000
tr = tr2 + tr1
assert isinstance(tr.data, np.ndarray)
np.testing.assert_array_equal(tr.data, np.zeros(12))
# 3
# contained trace with same data
# Trace 1: 1111111111
# Trace 2: 11
tr1 = Trace(data=np.ones(10))
tr2 = Trace(data=np.ones(2))
tr2.stats.starttime = tr1.stats.starttime + 5
# 1 + 2 : 1111111111
tr = tr1 + tr2
assert isinstance(tr.data, np.ndarray)
np.testing.assert_array_equal(tr.data, np.ones(10))
# 2 + 1 : 1111111111
tr = tr2 + tr1
assert isinstance(tr.data, np.ndarray)
np.testing.assert_array_equal(tr.data, np.ones(10))
# 4
# contained trace with differing data
# Trace 1: 0000000000
# Trace 2: 11
tr1 = Trace(data=np.zeros(10))
tr2 = Trace(data=np.ones(2))
tr2.stats.starttime = tr1.stats.starttime + 5
# 1 + 2 : 00000--000
tr = tr1 + tr2
assert isinstance(tr.data, np.ma.masked_array)
assert tr.data.tolist() == [0, 0, 0, 0, 0, None, None, 0, 0, 0]
# 2 + 1 : 00000--000
tr = tr2 + tr1
assert isinstance(tr.data, np.ma.masked_array)
assert tr.data.tolist() == [0, 0, 0, 0, 0, None, None, 0, 0, 0]
# 5
# completely contained trace with same data until end
# Trace 1: 1111111111
# Trace 2: 1111111111
tr1 = Trace(data=np.ones(10))
tr2 = Trace(data=np.ones(10))
# 1 + 2 : 1111111111
tr = tr1 + tr2
assert isinstance(tr.data, np.ndarray)
np.testing.assert_array_equal(tr.data, np.ones(10))
# 6
# completely contained trace with differing data
# Trace 1: 0000000000
# Trace 2: 1111111111
tr1 = Trace(data=np.zeros(10))
tr2 = Trace(data=np.ones(10))
# 1 + 2 : ----------
tr = tr1 + tr2
assert isinstance(tr.data, np.ma.masked_array)
assert tr.data.tolist() == [None] * 10