Script to parse the Webtime Tracker CSV file and plot it
#!/usr/bin/env python | |
import argparse | |
from datetime import datetime | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
def smoothed(t: np.ndarray, smoothing: int) -> np.ndarray: | |
if smoothing <= 0: | |
return t | |
left, right = smoothing // 2, (smoothing - 1) // 2 | |
window = np.hanning(smoothing) | |
window /= window.sum() | |
smooth_t = np.convolve(t, window)[left:-right] | |
return smooth_t | |
def main(args: argparse.Namespace) -> None: | |
data = pd.read_csv(args.csv) | |
largest = data.loc[data.sum(1).nlargest(args.num_sites).index.values] | |
domains = list(largest["Domain"]) | |
times = largest.to_numpy()[:, 1:] | |
dates = [ | |
datetime.strptime(date_string, '%Y-%m-%d') | |
for date_string in data.columns[1:] | |
] | |
fig = plt.figure(figsize=(10, 5)) | |
for domain, time in zip(domains, times): | |
time = (np.array(time, dtype=np.int32) + 30) // 60 | |
plt.plot(dates, smoothed(time, args.smoothing), label=domain) | |
plt.legend(title="Sites", bbox_to_anchor=(1.05, 1), loc="upper left") | |
fig.autofmt_xdate() | |
plt.xlabel("Date") | |
plt.ylabel("Minutes") | |
plt.tight_layout() | |
plt.savefig("time.svg") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Parses Webtime tracker data") | |
parser.add_argument("csv", help="Path to the Webtime CSV") | |
parser.add_argument("-n", "--num-sites", type=int, default=10, | |
help="Number of sites to look at") | |
parser.add_argument("-s", "--smoothing", type=int, default=20, | |
help="Smoothing window length") | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment