-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtests.py
189 lines (157 loc) · 5.92 KB
/
tests.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
import unittest
from glider_utils.yo import (
find_yo_extrema
)
from glider_utils.yo.filters import (
filter_profile_depth,
filter_profile_time,
filter_profile_distance,
filter_profile_number_of_points
)
from glider_utils.gps import (
interpolate_gps
)
from glider_utils.ctd import (
calculate_practical_salinity,
calculate_density
)
import numpy as np
import pprint
def is_continuous(profiled_dataset):
last_profile_id = 0
for i, row in enumerate(profiled_dataset):
profile_diff = abs(last_profile_id - row[2])
if profile_diff == 1:
last_profile_id = row[2]
elif profile_diff > 1:
print(
"Inconsistency @: %d, Last Profile: %d, Current: %d"
% (i, last_profile_id, row[2])
)
return False
return True
def is_complete(profiled_dataset, dataset):
return len(profiled_dataset) == len(dataset)
ctd_filepath = 'ctd_dataset.csv'
class TestFindProfile(unittest.TestCase):
def setUp(self):
self.dataset = np.loadtxt(ctd_filepath, delimiter=',')
self.profiled_dataset = find_yo_extrema(
self.dataset[:, 0], self.dataset[:, 3]
)
def test_find_profile(self):
self.assertNotEqual(
len(self.profiled_dataset),
0
)
self.assertTrue(is_complete(self.profiled_dataset, self.dataset))
def test_extreme_depth_filter(self):
filtered_profiled_dataset = filter_profile_depth(
self.profiled_dataset, 10000
)
uniques = np.unique(filtered_profiled_dataset[:, 2])
self.assertEqual(len(uniques), 1)
def test_filter_profile_depth(self):
filtered_profiled_dataset = filter_profile_depth(
self.profiled_dataset, 36
)
self.assertNotEqual(
len(np.unique(self.profiled_dataset[:, 2])),
len(np.unique(filtered_profiled_dataset[:, 2]))
)
self.assertTrue(is_continuous(filtered_profiled_dataset))
self.assertTrue(is_complete(filtered_profiled_dataset, self.dataset))
def test_filter_profile_time(self):
filtered_profiled_dataset = filter_profile_time(
self.profiled_dataset, 300
)
self.assertNotEqual(
len(np.unique(self.profiled_dataset[:, 2])),
len(np.unique(filtered_profiled_dataset[:, 2]))
)
self.assertTrue(is_continuous(filtered_profiled_dataset))
self.assertTrue(is_complete(filtered_profiled_dataset, self.dataset))
def test_filter_profile_distance(self):
filtered_profiled_dataset = filter_profile_distance(
self.profiled_dataset, 150
)
self.assertNotEqual(
len(np.unique(self.profiled_dataset[:, 2])),
len(np.unique(filtered_profiled_dataset[:, 2]))
)
self.assertTrue(is_continuous(filtered_profiled_dataset))
self.assertTrue(is_complete(filtered_profiled_dataset, self.dataset))
def test_filter_profile_number_of_points(self):
filtered_profiled_dataset = filter_profile_number_of_points(
self.profiled_dataset, 20
)
self.assertNotEqual(
len(np.unique(self.profiled_dataset[:, 2])),
len(np.unique(filtered_profiled_dataset[:, 2]))
)
self.assertTrue(is_continuous(filtered_profiled_dataset))
self.assertTrue(is_complete(filtered_profiled_dataset, self.dataset))
def test_default_filter_composite(self):
filtered_profiled_dataset = filter_profile_depth(self.profiled_dataset)
filtered_profiled_dataset = filter_profile_number_of_points(
filtered_profiled_dataset, 20
)
filtered_profiled_dataset = filter_profile_time(
filtered_profiled_dataset
)
filtered_profiled_dataset = filter_profile_distance(
filtered_profiled_dataset
)
self.assertNotEqual(
len(np.unique(self.profiled_dataset[:, 2])),
len(np.unique(filtered_profiled_dataset[:, 2]))
)
self.assertTrue(is_continuous(filtered_profiled_dataset))
self.assertTrue(is_complete(filtered_profiled_dataset, self.dataset))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(filtered_profiled_dataset)
class TestInterpolateGPS(unittest.TestCase):
def setUp(self):
self.ctd_dataset = np.loadtxt(ctd_filepath, delimiter=',')
def test_interpolate_gps(self):
est_lat, est_lon = interpolate_gps(
self.ctd_dataset[:, 0],
self.ctd_dataset[:, 4], self.ctd_dataset[:, 5]
)
self.assertEqual(len(est_lat), len(est_lon))
self.assertEqual(len(self.ctd_dataset[:, 0]), len(est_lat))
class TestSalinity(unittest.TestCase):
def setUp(self):
self.ctd_dataset = np.loadtxt(ctd_filepath, delimiter=',')
def test_practical_salinity(self):
salinity = calculate_practical_salinity(
self.ctd_dataset[:, 0],
self.ctd_dataset[:, 1],
self.ctd_dataset[:, 2],
self.ctd_dataset[:, 3]
)
self.assertEqual(len(self.ctd_dataset[:, 0]), len(salinity))
class TestDensity(unittest.TestCase):
def setUp(self):
self.ctd_dataset = np.loadtxt(ctd_filepath, delimiter=',')
self.lat, self.lon = interpolate_gps(
self.ctd_dataset[:, 0],
self.ctd_dataset[:, 4], self.ctd_dataset[:, 5]
)
def test_density(self):
salinity = calculate_practical_salinity(
self.ctd_dataset[:, 0],
self.ctd_dataset[:, 1],
self.ctd_dataset[:, 2],
self.ctd_dataset[:, 3]
)
density = calculate_density(
self.ctd_dataset[:, 0],
self.ctd_dataset[:, 2],
self.ctd_dataset[:, 3],
salinity,
self.lat, self.lon
)
self.assertEqual(len(self.ctd_dataset[:, 0]), len(density))
if __name__ == '__main__':
unittest.main()