Skip to content

Commit b2feb65

Browse files
authored
Merge pull request #196 from InsightLab/examples
Correcting formation wrongs on datetime and Putting examples on visual module
2 parents b19b7c0 + 672c440 commit b2feb65

File tree

2 files changed

+166
-52
lines changed

2 files changed

+166
-52
lines changed

pymove/utils/datetime.py

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -524,17 +524,17 @@ def create_time_slot_in_minute(
524524
>>> from pymove.utils.datetime import create_time_slot_in_minute
525525
>>> from pymove import datetime
526526
>>> data
527-
lat lon datetime id
528-
0 39.984094 116.319236 2008-10-23 05:44:05 1
529-
1 39.984198 116.319322 2008-10-23 05:56:06 1
530-
2 39.984224 116.319402 2008-10-23 05:56:11 1
531-
3 39.984224 116.319402 2008-10-23 06:10:15 1
527+
lat lon datetime id
528+
0 39.984094 116.319236 2008-10-23 05:44:05 1
529+
1 39.984198 116.319322 2008-10-23 05:56:06 1
530+
2 39.984224 116.319402 2008-10-23 05:56:11 1
531+
3 39.984224 116.319402 2008-10-23 06:10:15 1
532532
>>> datetime.create_time_slot_in_minute(data, inplace=False)
533-
lat lon datetime id time_slot
534-
0 39.984094 116.319236 2008-10-23 05:44:05 1 22
535-
1 39.984198 116.319322 2008-10-23 05:56:06 1 23
536-
2 39.984224 116.319402 2008-10-23 05:56:11 1 23
537-
3 39.984224 116.319402 2008-10-23 06:10:15 1 24
533+
lat lon datetime id time_slot
534+
0 39.984094 116.319236 2008-10-23 05:44:05 1 22
535+
1 39.984198 116.319322 2008-10-23 05:56:06 1 23
536+
2 39.984224 116.319402 2008-10-23 05:56:11 1 23
537+
3 39.984224 116.319402 2008-10-23 06:10:15 1 24
538538
"""
539539
if data.dtypes[label_datetime] != 'datetime64[ns]':
540540
raise ValueError('{} colum must be of type datetime'.format(label_datetime))
@@ -572,17 +572,21 @@ def generate_time_statistics(
572572
-------
573573
>>> from pymove.utils.datetime import generate_time_statistics
574574
>>> df
575-
local_label prev_local time_to_prev id
576-
0 house NaN NaN 1
577-
1 market house 720.0 1
578-
2 market market 5.0 1
579-
3 market market 1.0 1
580-
4 school market 844.0 1
575+
local_label prev_local time_to_prev id
576+
0 house NaN NaN 1
577+
1 market house 720.0 1
578+
2 market market 5.0 1
579+
3 market market 1.0 1
580+
4 school market 844.0 1
581581
>>> generate_time_statistics(df)
582-
local_label prev_local mean std min max sum count
583-
0 house market 844.0 0.000000 844.0 844.0 844.0 1
584-
1 market house 720.0 0.000000 720.0 720.0 720.0 1
585-
2 market market 3.0 2.828427 1.0 5.0 6.0 2
582+
local_label prev_local mean std \
583+
min max sum count
584+
0 house market 844.0 0.000000 \
585+
844.0 844.0 844.0 1
586+
1 market house 720.0 0.000000 \
587+
720.0 720.0 720.0 1
588+
2 market market 3.0 2.828427 \
589+
1.0 5.0 6.0 2
586590
"""
587591
df_statistics = data.groupby(
588592
[local_label, PREV_LOCAL]
@@ -619,13 +623,12 @@ def _calc_time_threshold(seg_mean: float, seg_std: float) -> float:
619623
Examples
620624
--------
621625
>>> from pymove.utils.datetime import _calc_time_threshold
622-
>>> print(_calc_time_threshold(12.3,2.1))
626+
>>> print(_calc_time_threshold(12.3, 2.1))
623627
14.4
624-
>>> print(_calc_time_threshold(1,1.5))
628+
>>> print(_calc_time_threshold(1, 1.5))
625629
2.5
626-
>>> print(_calc_time_threshold(-2,2))
630+
>>> print(_calc_time_threshold(-2, 2))
627631
0.0
628-
629632
"""
630633
threshold = seg_std + seg_mean
631634
threshold = float('{:.1f}'.format(threshold))
@@ -665,27 +668,27 @@ def threshold_time_statistics(
665668
-------
666669
>>> from pymove.utils.datetime import generate_time_statistics
667670
>>> df
668-
local_label prev_local time_to_prev id
669-
0 house NaN NaN 1
670-
1 market house 720.0 1
671-
2 market market 5.0 1
672-
3 market market 1.0 1
673-
4 school market 844.0 1
671+
local_label prev_local time_to_prev id
672+
0 house NaN NaN 1
673+
1 market house 720.0 1
674+
2 market market 5.0 1
675+
3 market market 1.0 1
676+
4 school market 844.0 1
674677
>>> statistics = generate_time_statistics(df)
675678
>>> statistics
676-
local_label prev_local mean std min max sum count
677-
0 house market 844.0 0.000000 844.0 844.0 844.0 1
678-
1 market house 720.0 0.000000 720.0 720.0 720.0 1
679-
2 market market 3.0 2.828427 1.0 5.0 6.0 2
679+
local_label prev_local mean std min max sum count
680+
0 house market 844.0 0.000000 844.0 844.0 844.0 1
681+
1 market house 720.0 0.000000 720.0 720.0 720.0 1
682+
2 market market 3.0 2.828427 1.0 5.0 6.0 2
680683
>>> threshold_time_statistics(statistics)
681-
local_label prev_local mean std min max sum count
682-
0 house market 844.0 0.000000 844.0 844.0 844.0 1
683-
1 market house 720.0 0.000000 720.0 720.0 720.0 1
684-
2 market market 3.0 2.828427 1.0 5.0 6.0 2
685-
threshold
686-
0 844.0
687-
1 720.0
688-
2 5.8
684+
local_label prev_local mean std min \
685+
max sum count threshold
686+
0 house market 844.0 0.000000 844.0 \
687+
844.0 844.0 1 844.0
688+
1 market house 720.0 0.000000 720.0 \
689+
720.0 720.0 1 720.0
690+
2 market market 3.0 2.828427 1.0 \
691+
5.0 6.0 2 5.8
689692
"""
690693
if not inplace:
691694
df_statistics = df_statistics.copy()

pymove/utils/visual.py

Lines changed: 121 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,69 @@ def add_map_legend(m: Map, title: Text, items: List[Tuple]):
4040
----------
4141
https://github.com/python-visualization/folium/issues/528#issuecomment-421445303
4242
43+
Examples
44+
--------
45+
>>> import folium
46+
>>> from pymove.utils.visual import add_map_legend
47+
>>> df
48+
lat lon datetime id
49+
0 39.984094 116.319236 2008-10-23 05:53:05 1
50+
1 39.984198 116.319322 2008-10-23 05:53:06 1
51+
2 39.984224 116.319402 2008-10-23 05:53:11 1
52+
3 39.984211 116.319389 2008-10-23 05:53:16 2
53+
4 39.984217 116.319422 2008-10-23 05:53:21 2
54+
>>> m = folium.Map(location=[df.lat.median(), df.lon.median()])
55+
>>> folium.PolyLine(mdf[['lat', 'lon']], color='red').add_to(m)
56+
>>> pm.visual.add_map_legend(m, 'Color by ID', [(1, 'red')])
57+
>>> m.get_root().to_dict()
58+
{
59+
"name": "Figure",
60+
"id": "1d32230cd6c54b19b35ceaa864e61168",
61+
"children": {
62+
"map_6f1abc8eacee41e8aa9d163e6bbb295f": {
63+
"name": "Map",
64+
"id": "6f1abc8eacee41e8aa9d163e6bbb295f",
65+
"children": {
66+
"openstreetmap": {
67+
"name": "TileLayer",
68+
"id": "f58c3659fea348cb828775f223e1e6a4",
69+
"children": {}
70+
},
71+
"poly_line_75023fd7df01475ea5e5606ddd7f4dd2": {
72+
"name": "PolyLine",
73+
"id": "75023fd7df01475ea5e5606ddd7f4dd2",
74+
"children": {}
75+
}
76+
}
77+
},
78+
"map_legend": { # legend element
79+
"name": "MacroElement",
80+
"id": "72911b4418a94358ba8790aab93573d1",
81+
"children": {}
82+
}
83+
},
84+
"header": {
85+
"name": "Element",
86+
"id": "e46930fc4152431090b112424b5beb6a",
87+
"children": {
88+
"meta_http": {
89+
"name": "Element",
90+
"id": "868e20baf5744e82baf8f13a06849ecc",
91+
"children": {}
92+
}
93+
}
94+
},
95+
"html": {
96+
"name": "Element",
97+
"id": "9c4da9e0aac349f594e2d23298bac171",
98+
"children": {}
99+
},
100+
"script": {
101+
"name": "Element",
102+
"id": "d092078607c04076bf58bd4593fa1684",
103+
"children": {}
104+
}
105+
}
43106
"""
44107
item = "<li><span style='background:%s;'></span>%s</li>"
45108
list_items = '\n'.join([item % (c, n) for (n, c) in items])
@@ -136,7 +199,7 @@ def add_map_legend(m: Map, title: Text, items: List[Tuple]):
136199
macro = MacroElement()
137200
macro._template = Template(template)
138201

139-
m.get_root().add_child(macro)
202+
m.get_root().add_child(macro, name='map_legend')
140203

141204

142205
def generate_color() -> Text:
@@ -146,6 +209,14 @@ def generate_color() -> Text:
146209
Returns
147210
-------
148211
Random HEX color
212+
213+
Examples
214+
--------
215+
>>> from pymove.utils.visual import generate_color
216+
>>> print(generate_color(), type(generate_color()))
217+
'#E0FFFF' <class 'str'>
218+
>>> print(generate_color(), type(generate_color()))
219+
'#808000' <class 'str'>
149220
"""
150221
return COLORS[randint(0, len(COLORS))]
151222

@@ -167,9 +238,11 @@ def rgb(rgb_colors: Tuple[float, float, float]) -> Tuple[int, int, int]:
167238
168239
Examples
169240
--------
170-
>>> from pymove.visualization.visualization import rgb
171-
>>> rgb([0.6,0.2,0.2])
172-
(51, 51, 153)
241+
>>> from pymove.utils.visual import rgb
242+
>>> print(rgb((0.1, 0.2, 0.7)), type(rgb((0.1, 0.2, 0.7))))
243+
(51, 178, 25) <class 'tuple'>
244+
>>> print(rgb((0.5, 0.4, 0.1)), type(rgb((0.5, 0.4, 0.1))))
245+
(102, 25, 127) <class 'tuple'>
173246
"""
174247
blue = rgb_colors[0]
175248
red = rgb_colors[1]
@@ -194,9 +267,11 @@ def hex_rgb(rgb_colors: Tuple[float, float, float]) -> Text:
194267
195268
Examples
196269
--------
197-
>>> from pymove.visualization.visualization import hex_rgb
198-
>>> hex_rgb([0.6,0.2,0.2])
199-
'#333399'
270+
>>> from pymove.utils.visual import hex_rgb
271+
>>> print(hex_rgb((0.1, 0.2, 0.7)), type(hex_rgb((0.1, 0.2, 0.7))))
272+
'#33B219' <class 'str'>
273+
>>> print(hex_rgb((0.5, 0.4, 0.1)), type(hex_rgb((0.5, 0.4, 0.1))))
274+
'#66197F' <class 'str'>
200275
"""
201276
return '#%02X%02X%02X' % rgb(rgb_colors)
202277

@@ -216,6 +291,16 @@ def cmap_hex_color(cmap: ListedColormap, i: int) -> Text:
216291
-------
217292
str
218293
Represents corresponding hex str
294+
295+
Examples
296+
--------
297+
>>> from pymove.utils.visual import cmap_hex_color
298+
>>> import matplotlib.pyplot as plt
299+
>>> jet = plt.get_cmap('jet') # This comand generates a Linear Segmented Colormap
300+
>>> print(cmap_hex_color(jet, 0))
301+
'#000080'
302+
>>> print(cmap_hex_color(jet, 1))
303+
'#000084'
219304
"""
220305
return rgb2hex(cmap(i))
221306

@@ -233,6 +318,12 @@ def get_cmap(cmap: Text) -> Colormap:
233318
-------
234319
Colormap
235320
matplotlib colormap
321+
322+
Examples
323+
--------
324+
>>> from pymove.utils.visual import get_cmap
325+
>>> print(get_cmap('Greys')
326+
<matplotlib.colors.LinearSegmentedColormap object at 0x7f743fc04bb0>
236327
"""
237328
return _get_cmap(cmap)
238329

@@ -252,8 +343,28 @@ def save_wkt(
252343
label_id : str
253344
Represents column name of trajectory id
254345
346+
Returns
347+
-------
348+
File: A file.wkt that contains geometric points that build a map visualization
349+
350+
Examples
351+
--------
352+
>>> from pymove.utils.visual import save_wkt
353+
>>> df.head()
354+
lat lon datetime id
355+
0 39.984094 116.319236 2008-10-23 05:53:05 1
356+
1 39.984198 116.319322 2008-10-23 05:53:06 1
357+
2 39.984224 116.319402 2008-10-23 05:53:11 1
358+
3 39.984211 116.319389 2008-10-23 05:53:16 2
359+
4 39.984217 116.319422 2008-10-23 05:53:21 2
360+
>>> save_wkt(df, 'test.wkt', 'id')
361+
>>> with open('test.wtk') as f:
362+
>>> print(f.read())
363+
'id;linestring'
364+
'1;LINESTRING(116.319236 39.984094,116.319322 39.984198,116.319402 39.984224)'
365+
'2;LINESTRING(116.319389 39.984211,116.319422 39.984217)'
255366
"""
256-
str_ = '%s;linestring\n' % label_id
367+
wtk = '%s;linestring\n' % label_id
257368
ids = move_data[label_id].unique()
258369
for id_ in ids:
259370
move_df = move_data[move_data[label_id] == id_]
@@ -263,6 +374,6 @@ def save_wkt(
263374
for x in move_df[[LONGITUDE, LATITUDE]].values
264375
)
265376
curr_str += ')\n'
266-
str_ += curr_str
377+
wtk += curr_str
267378
with open(filename, 'w') as f:
268-
f.write(str_)
379+
f.write(wtk)

0 commit comments

Comments
 (0)