Skip to content

Commit 23485bb

Browse files
authored
Merge pull request #210 from InsightLab/data_aug
Data aug
2 parents 6e046cd + 42cd650 commit 23485bb

File tree

9 files changed

+1780
-597
lines changed

9 files changed

+1780
-597
lines changed

docs/api/pymove.utils.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ pymove.utils.mem module
8484
:undoc-members:
8585
:show-inheritance:
8686

87+
pymove.utils.networkx module
88+
----------------------------
89+
90+
.. automodule:: pymove.utils.networkx
91+
:members:
92+
:undoc-members:
93+
:show-inheritance:
94+
8795
pymove.utils.trajectories module
8896
--------------------------------
8997

pymove/tests/test_utils_data_augmentation.py

Lines changed: 271 additions & 306 deletions
Large diffs are not rendered by default.
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
import json
2+
import os
3+
4+
import pandas as pd
5+
from networkx.classes.digraph import DiGraph
6+
from networkx.testing import assert_graphs_equal
7+
from numpy.testing import assert_equal
8+
9+
from pymove.utils.constants import (
10+
DATETIME,
11+
LATITUDE,
12+
LOCAL_LABEL,
13+
LONGITUDE,
14+
PREV_LOCAL,
15+
TID,
16+
TRAJ_ID,
17+
)
18+
from pymove.utils.networkx import (
19+
_populate_graph,
20+
build_transition_graph_from_df,
21+
build_transition_graph_from_dict,
22+
graph_to_dict,
23+
read_graph_json,
24+
save_graph_as_json,
25+
)
26+
27+
dict_graph = {
28+
'nodes': {
29+
'coords': { '85': (-3.8347478, -38.592189), '673': (-3.8235834, -38.590389),
30+
'394': (-3.813889, -38.5904445), '263': (-3.9067654, -38.5907723),
31+
'224': (-3.8857223, -38.5928892), '623': (-3.8828723, -38.5929789)},
32+
'datetime': { '85': ['2017-09-02 22:00:27'], '673': ['2017-09-02 22:01:36'],
33+
'394': ['2017-09-02 22:03:08'], '263': ['2017-09-02 22:03:46'],
34+
'224': ['2017-09-02 22:07:19'], '623': ['2017-09-02 22:07:40']},
35+
'freq_source': {'85': 1, '673': 0, '394': 0, '263': 0, '224': 0, '623': 0},
36+
'freq_target': {'85': 0, '673': 0, '394': 0, '263': 0, '224': 0, '623': 1}},
37+
'edges': {
38+
'85': {'673': {'weight': 1, 'mean_times': '0 days 00:01:09'}},
39+
'673': {'394': {'weight': 1, 'mean_times': '0 days 00:01:32'}},
40+
'394': {'263': {'weight': 1, 'mean_times': '0 days 00:00:38'}},
41+
'263': {'224': {'weight': 1, 'mean_times': '0 days 00:03:33'}},
42+
'224': {'623': {'weight': 1, 'mean_times': '0 days 00:00:21'}},
43+
'623': {}}}
44+
45+
list_data1 = {
46+
TRAJ_ID: [[1, 1, 1, 1, 1, 1]],
47+
DATETIME: [[pd.Timestamp('2017-09-02 22:00:27'),
48+
pd.Timestamp('2017-09-02 22:01:36'),
49+
pd.Timestamp('2017-09-02 22:03:08'),
50+
pd.Timestamp('2017-09-02 22:03:46'),
51+
pd.Timestamp('2017-09-02 22:07:19'),
52+
pd.Timestamp('2017-09-02 22:07:40')]],
53+
LOCAL_LABEL: [[85, 673, 394, 263, 224, 623]],
54+
LATITUDE: [[-3.8347478, -3.8235834, -3.813889,
55+
-3.9067654, -3.8857223, -3.8828723]],
56+
LONGITUDE: [[-38.592189, -38.590389, -38.5904445,
57+
-38.5907723, -38.5928892, -38.5929789]],
58+
TID: [['12017090222', '12017090222', '12017090222',
59+
'12017090222', '12017090222', '12017090222']]
60+
}
61+
62+
63+
def _transition_graph():
64+
expected_graph = DiGraph()
65+
expected_graph.add_node('85', coords=(-3.8347478, -38.592189),
66+
datetime=['2017-09-02 22:00:27'], freq_source=1, freq_target=0)
67+
expected_graph.add_node('673', coords=(-3.8235834, -38.590389),
68+
datetime=['2017-09-02 22:01:36'], freq_source=0, freq_target=0)
69+
expected_graph.add_node('394', coords=(-3.813889, -38.5904445),
70+
datetime=['2017-09-02 22:03:08'], freq_source=0, freq_target=0)
71+
expected_graph.add_node('263', coords=(-3.9067654, -38.5907723),
72+
datetime=['2017-09-02 22:03:46'], freq_source=0, freq_target=0)
73+
expected_graph.add_node('224', coords=(-3.8857223, -38.5928892),
74+
datetime=['2017-09-02 22:07:19'], freq_source=0, freq_target=0)
75+
expected_graph.add_node('623', coords=(-3.8828723, -38.5929789),
76+
datetime=['2017-09-02 22:07:40'], freq_source=0, freq_target=1)
77+
expected_graph.add_edge( '85', '673', weight=1, mean_times='0 days 00:01:09')
78+
expected_graph.add_edge('673', '394', weight=1, mean_times='0 days 00:01:32')
79+
expected_graph.add_edge('394', '263', weight=1, mean_times='0 days 00:00:38')
80+
expected_graph.add_edge('263', '224', weight=1, mean_times='0 days 00:03:33')
81+
expected_graph.add_edge('224', '623', weight=1, mean_times='0 days 00:00:21')
82+
83+
return expected_graph
84+
85+
86+
def test_populate_graph():
87+
row = pd.DataFrame(list_data1).loc[0]
88+
89+
nodes = {'datetime': {}, 'coords': {}, 'freq_source': {}, 'freq_target': {}}
90+
edges = {}
91+
92+
expected_nodes = {
93+
'datetime': {'85': ['2017-09-02 22:00:27'], '673': ['2017-09-02 22:01:36'],
94+
'394': ['2017-09-02 22:03:08'], '263': ['2017-09-02 22:03:46'],
95+
'224': ['2017-09-02 22:07:19'], '623': ['2017-09-02 22:07:40']},
96+
'coords': {'85': (-3.8347478, -38.592189), '673': (-3.8235834, -38.590389),
97+
'394': (-3.813889, -38.5904445), '263': (-3.9067654, -38.5907723),
98+
'224': (-3.8857223, -38.5928892), '623': (-3.8828723, -38.5929789)},
99+
'freq_source': {'85': 1, '673': 0, '394': 0, '263': 0, '224': 0, '623': 0},
100+
'freq_target': {'85': 0, '673': 0, '394': 0, '263': 0, '224': 0, '623': 1}}
101+
102+
expected_edges = {
103+
'85': {'673': {'weight': 1, 'mean_times': '0 days 00:01:09'}},
104+
'673': {'394': {'weight': 1, 'mean_times': '0 days 00:01:32'}},
105+
'394': {'263': {'weight': 1, 'mean_times': '0 days 00:00:38'}},
106+
'263': {'224': {'weight': 1, 'mean_times': '0 days 00:03:33'}},
107+
'224': {'623': {'weight': 1, 'mean_times': '0 days 00:00:21'}}}
108+
109+
_populate_graph(row, nodes, edges)
110+
nodes, edges
111+
112+
assert_equal(expected_nodes, nodes)
113+
assert_equal(expected_edges, edges)
114+
115+
116+
def test_build_transition_graph_from_dict():
117+
expected_graph = _transition_graph()
118+
119+
graph = build_transition_graph_from_dict(dict_graph)
120+
121+
assert_graphs_equal(expected_graph, graph)
122+
123+
124+
def test_build_transition_graph_from_df():
125+
expected_graph = _transition_graph()
126+
127+
traj_df = pd.DataFrame(list_data1)
128+
129+
graph = build_transition_graph_from_df(traj_df)
130+
131+
assert_graphs_equal(expected_graph, graph)
132+
133+
134+
def test_graph_to_dict():
135+
graph = _transition_graph()
136+
137+
expected_dict = {
138+
'nodes': {
139+
'coords': { '85': (-3.8347478, -38.592189), '673': (-3.8235834, -38.590389),
140+
'394': (-3.813889, -38.5904445), '263': (-3.9067654, -38.5907723),
141+
'224': (-3.8857223, -38.5928892), '623': (-3.8828723, -38.5929789)},
142+
'datetime': { '85': ['2017-09-02 22:00:27'], '673': ['2017-09-02 22:01:36'],
143+
'394': ['2017-09-02 22:03:08'], '263': ['2017-09-02 22:03:46'],
144+
'224': ['2017-09-02 22:07:19'], '623': ['2017-09-02 22:07:40']},
145+
'freq_source': {'85': 1, '673': 0, '394': 0, '263': 0, '224': 0, '623': 0},
146+
'freq_target': {'85': 0, '673': 0, '394': 0, '263': 0, '224': 0, '623': 1}},
147+
'edges': {
148+
'85': {'673': {'weight': 1, 'mean_times': '0 days 00:01:09'}},
149+
'673': {'394': {'weight': 1, 'mean_times': '0 days 00:01:32'}},
150+
'394': {'263': {'weight': 1, 'mean_times': '0 days 00:00:38'}},
151+
'263': {'224': {'weight': 1, 'mean_times': '0 days 00:03:33'}},
152+
'224': {'623': {'weight': 1, 'mean_times': '0 days 00:00:21'}},
153+
'623': {}}}
154+
155+
dict_graph = graph_to_dict(graph)
156+
157+
assert_equal(expected_dict, dict_graph)
158+
159+
160+
def test_save_graph_as_json(tmpdir):
161+
162+
expected = {
163+
'nodes': {
164+
'coords': { '85': (-3.8347478, -38.592189), '673': (-3.8235834, -38.590389),
165+
'394': (-3.813889, -38.5904445), '263': (-3.9067654, -38.5907723),
166+
'224': (-3.8857223, -38.5928892), '623': (-3.8828723, -38.5929789)},
167+
'datetime': { '85': ['2017-09-02 22:00:27'], '673': ['2017-09-02 22:01:36'],
168+
'394': ['2017-09-02 22:03:08'], '263': ['2017-09-02 22:03:46'],
169+
'224': ['2017-09-02 22:07:19'], '623': ['2017-09-02 22:07:40']},
170+
'freq_source': {'85': 1, '673': 0, '394': 0, '263': 0, '224': 0, '623': 0},
171+
'freq_target': {'85': 0, '673': 0, '394': 0, '263': 0, '224': 0, '623': 1}},
172+
'edges': {
173+
'85': {'673': {'weight': 1, 'mean_times': '0 days 00:01:09'}},
174+
'673': {'394': {'weight': 1, 'mean_times': '0 days 00:01:32'}},
175+
'394': {'263': {'weight': 1, 'mean_times': '0 days 00:00:38'}},
176+
'263': {'224': {'weight': 1, 'mean_times': '0 days 00:03:33'}},
177+
'224': {'623': {'weight': 1, 'mean_times': '0 days 00:00:21'}},
178+
'623': {}}}
179+
180+
d = tmpdir.mkdir('utils')
181+
182+
file_write_default = d.join('test_save_graph.json')
183+
filename_write_default = os.path.join(
184+
file_write_default.dirname, file_write_default.basename
185+
)
186+
187+
graph = _transition_graph()
188+
save_graph_as_json(graph, filename_write_default)
189+
saved_graph = read_graph_json(filename_write_default)
190+
191+
assert_equal(saved_graph, expected)
192+
193+
194+
def test_read_graph_json(tmpdir):
195+
196+
expected = {
197+
'nodes': {
198+
'coords': { '85': (-3.8347478, -38.592189), '673': (-3.8235834, -38.590389),
199+
'394': (-3.813889, -38.5904445), '263': (-3.9067654, -38.5907723),
200+
'224': (-3.8857223, -38.5928892), '623': (-3.8828723, -38.5929789)},
201+
'datetime': { '85': ['2017-09-02 22:00:27'], '673': ['2017-09-02 22:01:36'],
202+
'394': ['2017-09-02 22:03:08'], '263': ['2017-09-02 22:03:46'],
203+
'224': ['2017-09-02 22:07:19'], '623': ['2017-09-02 22:07:40']},
204+
'freq_source': {'85': 1, '673': 0, '394': 0, '263': 0, '224': 0, '623': 0},
205+
'freq_target': {'85': 0, '673': 0, '394': 0, '263': 0, '224': 0, '623': 1}},
206+
'edges': {
207+
'85': {'673': {'weight': 1, 'mean_times': '0 days 00:01:09'}},
208+
'673': {'394': {'weight': 1, 'mean_times': '0 days 00:01:32'}},
209+
'394': {'263': {'weight': 1, 'mean_times': '0 days 00:00:38'}},
210+
'263': {'224': {'weight': 1, 'mean_times': '0 days 00:03:33'}},
211+
'224': {'623': {'weight': 1, 'mean_times': '0 days 00:00:21'}},
212+
'623': {}}}
213+
214+
d = tmpdir.mkdir('utils')
215+
216+
file_write_default = d.join('test_read_graph.json')
217+
filename_write_default = os.path.join(
218+
file_write_default.dirname, file_write_default.basename
219+
)
220+
221+
graph = _transition_graph()
222+
223+
with open(filename_write_default, 'w') as f:
224+
json.dump(dict_graph, f)
225+
226+
saved_graph = read_graph_json(filename_write_default)
227+
228+
assert_equal(saved_graph, expected)

0 commit comments

Comments
 (0)