Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import datetime
  2.  
  3. from time import perf_counter
  4. script_start = perf_counter()
  5.  
  6. GOAL_DISTANCE = 4000
  7.  
  8. def is_leap_year(year):
  9. return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
  10.  
  11. def goal_progression(goal, steps):
  12. step = goal / steps
  13. return [i * step for i in range(steps + 1)]
  14.  
  15. ###############################################################################
  16. ####### Calculate yearly progression ##########################################
  17. ###############################################################################
  18.  
  19. # As soon as we filter the activities in any way, the speed drops from about
  20. # 1s to just over 20s. Also happens if filtering is done through the interface.
  21. # I guess the cache is useless at that point...
  22. full_metrics = GC.seasonMetrics(all=True, filter='Sport="Run"')
  23. metrics = list(zip(full_metrics['date'], full_metrics['Distance']))
  24.  
  25. today = datetime.date.today()
  26. start = datetime.date(year = full_metrics['date'][0].year, month = 1, day = 1)
  27. a_day = datetime.timedelta(days = 1)
  28.  
  29. distances = {}
  30. while start <= today:
  31. if not start.year in distances:
  32. distances[start.year] = [0]
  33.  
  34. distance_today = 0
  35. while metrics and metrics[0][0] <= start:
  36. distance_today += metrics[0][1]
  37. metrics = metrics[1:]
  38. distances[start.year].append(distances[start.year][-1] + distance_today)
  39.  
  40. start = start + a_day
  41.  
  42. ###############################################################################
  43. ####### Normalise yearly progression ##########################################
  44. ###############################################################################
  45.  
  46. for year in distances:
  47. if is_leap_year(year):
  48. # Treat 28 and 29 Feb as if they were a single day
  49. distances[year][59] = distances[year].pop(60)
  50. if len(distances[year]) < 366:
  51. distances[year] += (366 - len(distances[year])) * [None]
  52.  
  53. ###############################################################################
  54. ####### Plotting ##############################################################
  55. ###############################################################################
  56.  
  57. import plotly
  58. import plotly.graph_objs as go
  59.  
  60. data = [go.Scatter(x = list(range(366)), y = distances[year], mode = 'lines', name = year) for year in distances]
  61. data.append(go.Scatter(x = list(range(366)), y = goal_progression(GOAL_DISTANCE, 365), mode = 'lines', name = 'Goal'))
  62.  
  63. f = plotly.offline.plot({
  64. 'data': data
  65. }, auto_open = False)
  66. GC.webpage(f)
  67.  
  68. script_end = perf_counter()
  69. print("script duration:", script_end - script_start, "seconds")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement