Created
October 16, 2022 11:50
-
-
Save achilleas-k/e99da34922adddc2d958b1c185b6e257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Export data using https://www.strava.com/athlete/delete_your_account | |
# Yes, the data export option is on the Delete Your Account page. | |
# | |
# Run this script on the 'activities.csv'. | |
import sys | |
from datetime import datetime, timedelta | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas | |
DATE_FMT = r"%b %d, %Y, %I:%M:%S %p" | |
def _parse_date(val: str) -> datetime: | |
return datetime.strptime(val, DATE_FMT) | |
def _convert_date(col) -> np.ndarray: | |
return np.array([_convert_date(value) for value in col]) | |
CONVERTERS = { | |
"Activity Date": _parse_date, | |
} | |
def load(filepath: str) -> pandas.DataFrame: | |
with open(filepath) as datafile: | |
return pandas.read_csv(datafile, converters=CONVERTERS) | |
def describe(row): | |
time = timedelta(seconds=int(row["Elapsed Time"])) | |
dist = row["Distance"] | |
speed = row["Average Speed"] * 3600 / 1000 # m/s to km/h | |
name = row["Activity Name"] | |
date = row["Activity Date"] | |
aid = row["Activity ID"] | |
print(f"{date} {name}:\n\t{dist} km {speed:.2f} km/h {time}\n\thttps://strava.com/activities/{aid}\n") | |
def plot_distance(data: pandas.DataFrame): | |
plt.figure() | |
ax = plt.axes() | |
dates = data["Activity Date"] | |
dist = data["Distance"] | |
maxen = dist.cummax() | |
ax.bar(dates, dist, label="activity distance") | |
ax.plot(dates, maxen, "--", label="cumulative maximum") | |
# find indexes that changed max | |
changes = np.flatnonzero(np.diff(maxen)) + 1 | |
print("Running maximum distance") | |
for idx in changes: | |
describe(data.iloc[idx]) | |
plt.text(dates[idx], maxen[idx]+0.5, str(maxen[idx]), size=16, ha="center") | |
plt.ylabel("Distance (km)") | |
plt.xlabel("Date") | |
plt.legend() | |
plt.show() | |
def main(): | |
plt.style.use('tableau-colorblind10') | |
fname = sys.argv[1] | |
print(f"Loading data from {fname}") | |
data = load(fname) | |
plot_distance(data) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment