From 5300c528518c9b109a05a9814939e7146d3f6002 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Mon, 14 Feb 2022 10:36:27 +0200 Subject: [PATCH 1/2] More progress bars for slow loops --- src/stravavis/plot_landscape.py | 16 +++++++++------- src/stravavis/plot_map.py | 4 +++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/stravavis/plot_landscape.py b/src/stravavis/plot_landscape.py index 6de6d5e..6364d42 100644 --- a/src/stravavis/plot_landscape.py +++ b/src/stravavis/plot_landscape.py @@ -1,5 +1,7 @@ import matplotlib.pyplot as plt import pandas as pd +from rich.progress import track + def plot_landscape(df, output_file = 'landscape.png'): @@ -12,24 +14,24 @@ def plot_landscape(df, output_file = 'landscape.png'): # Create a list of activity names activities = df['name'].unique() n = len(activities) - + # Normalize dist processed = [] - - for i in range(n): + + for i in track(range(n), "Processing tracks"): df_i = df[df['name'] == activities[i]] df_i["dist_norm"] = (df_i["dist"] - df_i["dist"].min()) / (df_i["dist"].max() - df_i["dist"].min()) processed.append(df_i) - + df = pd.concat(processed) - + # Plot activities one by one - for i in range(n): + for i in track(range(n), "Plotting activities"): X = df[df['name'] == activities[i]]['dist_norm'] Y = df[df['name'] == activities[i]]['ele'] plt.fill_between(X, Y, color = 'black', alpha = 0.03, linewidth = 0) plt.plot(X, Y, color = 'black', alpha = 0.125, linewidth = 0.25) - + # Update plot aesthetics plt.axis('off') plt.margins(0) diff --git a/src/stravavis/plot_map.py b/src/stravavis/plot_map.py index 5eb7fe1..ddb9cd2 100644 --- a/src/stravavis/plot_map.py +++ b/src/stravavis/plot_map.py @@ -1,4 +1,6 @@ import matplotlib.pyplot as plt +from rich.progress import track + def plot_map(df, lon_min=None, lon_max= None, lat_min=None, lat_max=None, alpha=0.3, linewidth=0.3, output_file="map.png"): @@ -24,7 +26,7 @@ def plot_map(df, lon_min=None, lon_max= None, lat_min=None, lat_max=None, n = len(activities) # Plot activities one by one - for i in range(n): + for i in track(range(n), "Plotting activities"): X = df[df['name'] == activities[i]]['lon'] Y = df[df['name'] == activities[i]]['lat'] plt.plot(X, Y, color = 'black', alpha = alpha, linewidth = linewidth) From a9060fbc9ac5182042a30bb340016b91212a3f7f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Mon, 14 Feb 2022 11:06:50 +0200 Subject: [PATCH 2/2] Iterate over list rather than using an index --- src/stravavis/plot_landscape.py | 15 +++++++-------- src/stravavis/plot_map.py | 9 ++++----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/stravavis/plot_landscape.py b/src/stravavis/plot_landscape.py index 6364d42..6ad21e2 100644 --- a/src/stravavis/plot_landscape.py +++ b/src/stravavis/plot_landscape.py @@ -13,24 +13,23 @@ def plot_landscape(df, output_file = 'landscape.png'): # Create a list of activity names activities = df['name'].unique() - n = len(activities) # Normalize dist processed = [] - for i in track(range(n), "Processing tracks"): - df_i = df[df['name'] == activities[i]] + for activity in track(activities, "Processing tracks"): + df_i = df[df['name'] == activity] df_i["dist_norm"] = (df_i["dist"] - df_i["dist"].min()) / (df_i["dist"].max() - df_i["dist"].min()) processed.append(df_i) df = pd.concat(processed) # Plot activities one by one - for i in track(range(n), "Plotting activities"): - X = df[df['name'] == activities[i]]['dist_norm'] - Y = df[df['name'] == activities[i]]['ele'] - plt.fill_between(X, Y, color = 'black', alpha = 0.03, linewidth = 0) - plt.plot(X, Y, color = 'black', alpha = 0.125, linewidth = 0.25) + for activity in track(activities, "Plotting activities"): + x = df[df['name'] == activity]['dist_norm'] + y = df[df['name'] == activity]['ele'] + plt.fill_between(x, y, color='black', alpha=0.03, linewidth=0) + plt.plot(x, y, color='black', alpha=0.125, linewidth=0.25) # Update plot aesthetics plt.axis('off') diff --git a/src/stravavis/plot_map.py b/src/stravavis/plot_map.py index ddb9cd2..f466b5e 100644 --- a/src/stravavis/plot_map.py +++ b/src/stravavis/plot_map.py @@ -23,13 +23,12 @@ def plot_map(df, lon_min=None, lon_max= None, lat_min=None, lat_max=None, # Create a list of activity names activities = df['name'].unique() - n = len(activities) # Plot activities one by one - for i in track(range(n), "Plotting activities"): - X = df[df['name'] == activities[i]]['lon'] - Y = df[df['name'] == activities[i]]['lat'] - plt.plot(X, Y, color = 'black', alpha = alpha, linewidth = linewidth) + for activity in track(activities, "Plotting activities"): + x = df[df['name'] == activity]['lon'] + y = df[df['name'] == activity]['lat'] + plt.plot(x, y, color='black', alpha=alpha, linewidth=linewidth) # Update plot aesthetics plt.axis('off')