/
test_util_testing.py
106 lines (94 loc) · 3.59 KB
/
test_util_testing.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
# -*- coding: utf-8 -*-
"""
Tests for obspy's testing utilities.
"""
import numpy as np
from obspy import read, Trace
from obspy.core.util.testing import streams_almost_equal, traces_almost_equal
class TestAlmostEqual:
"""
Tests for fuzzy equality comparisons for traces.
"""
def test_identical_traces(self):
"""
Should return True on identical streams but false if a value is
greatly changed.
"""
tr1, tr2 = read()[0], read()[0]
assert traces_almost_equal(tr1, tr2)
# when one number is changed significantly it should return False
tr1.data[0] = (tr1.data[0] + 1) * 1000
assert not traces_almost_equal(tr1, tr2)
def test_slightly_modified_data(self):
"""
Traces that are "close" should be considered almost equal.
"""
tr1, tr2 = read()[0], read()[0]
# alter one trace's data slightly
tr1.data *= (1. + 1e-6)
assert traces_almost_equal(tr1, tr2)
def test_empty_traces(self):
"""
Empty traces should be considered almost equal.
"""
tr1, tr2 = Trace(), Trace()
assert traces_almost_equal(tr1, tr2)
def test_different_stats_no_processing(self):
"""
If only the stats are different traces should not be considered almost
equal.
"""
tr1 = Trace(header=dict(network='UU', station='TMU', channel='HHZ'))
tr2 = Trace(header=dict(network='UU', station='TMU', channel='HHN'))
assert not traces_almost_equal(tr1, tr2)
assert not traces_almost_equal(tr2, tr1)
def test_processing(self):
"""
Differences in processing attr of stats should only count if
processing is True.
"""
tr1, tr2 = read()[0], read()[0]
# Detrend each traces once, then second trace twice for two entries
# in processing.
tr1.detrend()
tr2.detrend()
tr1.detrend()
assert traces_almost_equal(tr1, tr2, default_stats=True)
assert not traces_almost_equal(tr1, tr2, default_stats=False)
def test_nan(self):
"""
Ensure NaNs eval equal if equal_nan is used, else they do not.
"""
tr1, tr2 = read()[0], read()[0]
tr1.data[0], tr2.data[0] = np.NaN, np.NaN
assert traces_almost_equal(tr1, tr2, equal_nan=True)
assert not traces_almost_equal(tr1, tr2, equal_nan=False)
def test_unequal_trace_lengths(self):
"""
Ensure traces with different lengths are not almost equal.
"""
tr1, tr2 = read()[0], read()[0]
tr2.data = tr2.data[:-1]
assert not traces_almost_equal(tr1, tr2)
def test_not_a_trace(self):
"""
Ensure comparing to someething that is not a trace returns False.
"""
tr1 = read()[0]
assert not traces_almost_equal(tr1, 1)
assert not traces_almost_equal(tr1, None)
assert not traces_almost_equal(tr1, 'not a trace')
def test_stream_almost_equal(self):
"""
Basic tests for almost equal. More rigorous testing is done on the
Trace's almost_equal method, which gets called by this one.
"""
# identical streams should be almost equal
st1, st2 = read(), read()
assert streams_almost_equal(st1, st2)
# passing something other than a stream should not be almost equal
assert not streams_almost_equal(st1, None)
assert not streams_almost_equal(st1, 1.1)
# passing streams of different lengths should not be almost equal
st2 = st2[1:]
assert not streams_almost_equal(st1, st2)