-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
101 lines (84 loc) · 3.33 KB
/
utilities.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
import numpy as np
import plotly.express as px
import tcxparser
import datetime
def get_discrete_colormap(Ntrace, colormap='sunset', order='reverse'):
'''
create discrete color map as line_mapbox don't handle continuous color map
[Some Color options]
mabpox_style = "dark", colormap = 'thermal', forward
mabpox_style = "dark", colormap = 'sunset', reverse
mabpox_style = "light", colormap = 'agsunset', forward
[Reference]
* solution from https://stackoverflow.com/questions/68575172/how-to-correctly-use-colormaps-for-plotly-express-line-mapbox
* discr_map: https://plotly.com/python-api-reference/generated/plotly.express.line_mapbox.html#plotly.express.line_mapbox
* Plotly build-in continuous color: https://plotly.com/python/builtin-colorscales/
'''
discr_map = {}
for i in range(0, Ntrace):
if order == 'reverse':
color_rgb = px.colors.sample_colorscale(colormap, 1 - float(i)/Ntrace) # reverse-order
else:
color_rgb = px.colors.sample_colorscale(colormap, float(i)/Ntrace) # normal-order
# formatting color_rgb
color_rgb = color_rgb[0].replace('rgb(', '').replace(')', '').split(',')
# the value is in CSS-color format
discr_map.update({i: '#%02x%02x%02x' % (int(color_rgb[0]), int(color_rgb[1]), int(color_rgb[2]))})
return discr_map
def processTCX(tcx_file):
'''
To access datas
- get positions: tcx.position_values()
- get duration (seconds): tcx.duration
- get distance: tcx.root.Activities.Activity.Lap[0].DistanceMeters
- get pace (second/km): pace = tcx.duration / (tcx.root.Activities.Activity.Lap[0].DistanceMeters / 1000)
'''
# [Load the data into a tcxparser object]
tcx = tcxparser.TCXParser(tcx_file)
# [Process the data]
# get position into latitudes and longitudes
position = np.array(tcx.position_values())
lats = position[:, 0]
lons = position[:, 1]
# turn utc time into seconds
time_utc = tcx.time_values()
time_seconds = []
for t in time_utc:
utc_datetime = datetime.datetime.strptime(t, "%Y-%m-%dT%H:%M:%SZ")
second = utc_datetime.timestamp()
time_seconds = np.append(time_seconds, second)
# [Output]
data = dict(lat=lats,
lon=lons,
time=time_seconds)
return data
def plotly_animation_button():
'''
get buttons for animation {play, Pause}
'''
updatemenus = [dict(
buttons = [
dict(
args = [None, {"frame": {"duration": 200, "redraw": True},
"fromcurrent": True}],
label = "Play",
method = "animate"
),
dict(
args = [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
label = "Pause",
method = "animate"
)
],
direction = "left",
pad = {"r": 10, "t": 10},
showactive = False,
type = "buttons",
x = 0.1,
xanchor = "right",
y = 0,
yanchor = "top"
)]
return updatemenus