Skip to content

Commit 157d0de

Browse files
authored
Merge pull request #42 from hugovk/cache-processed-activities
2 parents 3f05e06 + fe894f9 commit 157d0de

11 files changed

+53
-4
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ version.source = "vcs"
6161
local_scheme = "no-local-version"
6262

6363
[tool.isort]
64+
add_imports = "from __future__ import annotations"
6465
profile = "black"

src/stravavis/__main__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from . import cli
24

35
if __name__ == "__main__":

src/stravavis/cli.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import argparse
24
import glob
35
import os.path
@@ -89,7 +91,7 @@ def main():
8991
if os.path.isdir(args.path):
9092
args.path = os.path.join(args.path, "*")
9193

92-
filenames = glob.glob(args.path)
94+
filenames = sorted(glob.glob(args.path))
9395
if not filenames:
9496
sys.exit(f"No files found matching {args.path}")
9597

src/stravavis/plot_calendar.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import calmap
24
import matplotlib.pyplot as plt
35
import pandas as pd

src/stravavis/plot_dumbbell.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pandas as pd
24
from plotnine import (
35
aes,

src/stravavis/plot_elevations.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import math
24

35
import matplotlib.pyplot as plt

src/stravavis/plot_facets.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import math
24

35
import matplotlib.pyplot as plt

src/stravavis/plot_landscape.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import matplotlib.pyplot as plt
24
import pandas as pd
35
from rich.progress import track

src/stravavis/plot_map.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from math import log, pi, tan
24

35
import matplotlib.pyplot as plt

src/stravavis/process_activities.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pandas as pd
24

35

src/stravavis/process_data.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from __future__ import annotations
22

3+
import hashlib
34
import math
5+
import tempfile
46
from multiprocessing import Pool
7+
from pathlib import Path
58

69
import fit2gpx
710
import gpxpy
811
import pandas as pd
912
from rich.progress import track
1013

1114

12-
def process_file(fpath):
15+
def process_file(fpath: str) -> pd.DataFrame | None:
1316
if fpath.endswith(".gpx"):
1417
return process_gpx(fpath)
1518
elif fpath.endswith(".fit"):
@@ -18,7 +21,7 @@ def process_file(fpath):
1821

1922
# Function for processing an individual GPX file
2023
# Ref: https://pypi.org/project/gpxpy/
21-
def process_gpx(gpxfile):
24+
def process_gpx(gpxfile: str) -> pd.DataFrame | None:
2225
with open(gpxfile, encoding="utf-8") as f:
2326
try:
2427
activity = gpxpy.parse(f)
@@ -64,7 +67,7 @@ def process_gpx(gpxfile):
6467

6568
# Function for processing an individual FIT file
6669
# Ref: https://github.com/dodo-saba/fit2gpx
67-
def process_fit(fitfile):
70+
def process_fit(fitfile: str) -> pd.DataFrame:
6871
conv = fit2gpx.Converter()
6972
df_lap, df = conv.fit_to_dataframes(fname=fitfile)
7073

@@ -101,9 +104,33 @@ def process_fit(fitfile):
101104
return df
102105

103106

107+
def load_cache(filenames: list[str]) -> tuple[Path, pd.DataFrame | None]:
108+
# Create a cache key from the filenames
109+
key = hashlib.md5("".join(filenames).encode("utf-8")).hexdigest()
110+
111+
# Create a cache directory
112+
dir_name = Path(tempfile.gettempdir()) / "stravavis"
113+
dir_name.mkdir(parents=True, exist_ok=True)
114+
cache_filename = dir_name / f"cached_activities_{key}.pkl"
115+
print(f"Cache filename: {cache_filename}")
116+
117+
# Load cache if it exists
118+
try:
119+
df = pd.read_pickle(cache_filename)
120+
print("Loaded cached activities")
121+
return cache_filename, df
122+
except FileNotFoundError:
123+
print("Cache not found")
124+
return cache_filename, None
125+
126+
104127
# Function for processing (unzipped) GPX and FIT files in a directory (path)
105128
def process_data(filenames: list[str]) -> pd.DataFrame:
106129
# Process all files (GPX or FIT)
130+
cache_filename, df = load_cache(filenames)
131+
if df is not None:
132+
return df
133+
107134
with Pool() as pool:
108135
try:
109136
it = pool.imap_unordered(process_file, filenames)
@@ -117,4 +144,7 @@ def process_data(filenames: list[str]) -> pd.DataFrame:
117144

118145
df["time"] = pd.to_datetime(df["time"], utc=True)
119146

147+
# Save cache
148+
df.to_pickle(cache_filename)
149+
120150
return df

0 commit comments

Comments
 (0)