-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gpx_stats.py
137 lines (106 loc) · 4.43 KB
/
test_gpx_stats.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
import unittest
import numpy as np
import pandas as pd
from gpxpy.gpx import GPXTrackPoint, GPXTrackSegment
from gpx_data_utils import (
gpx_point_to_array,
gpx_segment_to_array,
gpx_segment_from_array,
)
from gpx_stats import convert_path_to_feature, smoothen_coordinates
class TestConvertPathToArray(unittest.TestCase):
def setUp(self):
self.segment_1 = GPXTrackSegment(
[
GPXTrackPoint(longitude=0.1, latitude=1, elevation=10),
GPXTrackPoint(longitude=0.2, latitude=2, elevation=20),
GPXTrackPoint(longitude=0.3, latitude=3, elevation=30),
]
)
self.expected_path_1 = np.array(
[
[0.0, 0.0, 0.0],
[1.0049875621, 0.0, 10],
[2.00997512422, 0.0, 20],
[0.0, 0.0, 0.0],
]
)
def test_segment_empty(self):
with self.assertRaises(AssertionError):
convert_path_to_feature(GPXTrackSegment(), 1)
def test_segment_too_long(self):
with self.assertRaises(AssertionError):
convert_path_to_feature(self.segment_1, 1)
def test_segment_short(self):
path = convert_path_to_feature(self.segment_1, 4)
np.testing.assert_array_almost_equal(path, self.expected_path_1)
class TestSmoothenCoordinatesShort(unittest.TestCase):
def setUp(self):
self.segment_1 = GPXTrackSegment(
[
GPXTrackPoint(longitude=0.1, latitude=1, elevation=10),
GPXTrackPoint(longitude=0.2, latitude=2, elevation=20),
GPXTrackPoint(longitude=0.3, latitude=3, elevation=30),
GPXTrackPoint(longitude=0.5, latitude=-1, elevation=0),
]
)
self.expected_1 = GPXTrackSegment(
[
GPXTrackPoint(longitude=0.2, latitude=2, elevation=20),
GPXTrackPoint(longitude=1.0 / 3, latitude=4.0 / 3, elevation=50.0 / 3),
]
)
def test_segment_list_empty(self):
smoothen_coordinates([])
def test_segment_empty(self):
smoothen_coordinates([GPXTrackSegment()])
def test_segment_short_1(self):
input = [GPXTrackSegment(self.segment_1.points[0:3])]
smoothen_coordinates(input)
expected_result = [GPXTrackSegment([self.expected_1.points[0]])]
self.assertEqual(len(input), 1)
self.assertEqual(len(input[0].points), 1)
np.testing.assert_array_almost_equal(
gpx_point_to_array(input[0].points[0]),
gpx_point_to_array(expected_result[0].points[0]),
)
def test_segment_short_2(self):
input = [GPXTrackSegment(self.segment_1.points)]
smoothen_coordinates(input)
expected_result = [self.expected_1]
self.assertEqual(len(input), 1)
self.assertEqual(len(input[0].points), len(expected_result[0].points))
np.testing.assert_array_almost_equal(
gpx_point_to_array(input[0].points[0]),
gpx_point_to_array(expected_result[0].points[0]),
)
class TestSmoothenCoordinatesLonger(unittest.TestCase):
def setUp(self):
segment_1_as_array = np.random.normal(scale=5, size=(25, 3))
segment_1 = gpx_segment_from_array(segment_1_as_array)
segment_2_as_array = np.random.normal(scale=2, size=(9, 3))
segment_2 = gpx_segment_from_array(segment_2_as_array)
self.segments = [segment_1, segment_2]
self.segments_as_arrays = [segment_1_as_array, segment_2_as_array]
def test_window_size_3(self):
expected_as_df = [
pd.DataFrame(self.segments_as_arrays[i]).rolling(3).mean()[2:]
for i in range(len(self.segments_as_arrays))
]
smoothen_coordinates(self.segments, window_size=3)
for i in range(len(self.segments)):
np.testing.assert_array_almost_equal(
expected_as_df[i].values, gpx_segment_to_array(self.segments[i])
)
def test_window_size_5(self):
expected_as_df = [
pd.DataFrame(self.segments_as_arrays[i]).rolling(5).mean()[4:]
for i in range(len(self.segments_as_arrays))
]
smoothen_coordinates(self.segments, window_size=5)
for i in range(len(self.segments)):
np.testing.assert_array_almost_equal(
expected_as_df[i].values, gpx_segment_to_array(self.segments[i])
)
if __name__ == "__main__":
unittest.main()