-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotpdf
executable file
·67 lines (60 loc) · 2.36 KB
/
plotpdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import argparse
from pdfstat import pdfstat
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys
from matplotlib.dates import DateFormatter, DayLocator, MonthLocator
from datetime import datetime, timedelta
from itertools import takewhile
def plot_data(ax, data):
ax.plot(data['time'], data['page'])
# ax.plot(data['time']+timedelta(days=7/2), data['page'].rolling(7).mean().resample('H').interpolate('cubic'))
def plot_goal(ax, x, limit, goal):
page, time = x
maxtime = limit['time']
delta = maxtime - time
print(maxtime)
ax.plot([time, maxtime], [page, page+delta.days*goal], linestyle='--')
# TODO: figure if plt.axline can be used
# delta = timedelta(hours=1)
# x = (time.to_datetime64(), page)
# y = ((time+delta).to_datetime64(), page + goal)
# print(x,y)
# plt.axline(x,y)
def make_plot(data, outpath, goal_rate):
fig, ax = plt.subplots()
plot_data(ax, data)
# data.plot(ax=ax, legend=False)
if goal_rate:
plot_goal(ax, data.iloc[-1], data.iloc[0], goal_rate)
#ax.set_title("The Art of Electronics", x=0.5, y=0.9, fontsize=16)
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_minor_locator(DayLocator(interval=2))
ax.xaxis.set_major_formatter(DateFormatter('%b'))
plt.setp(ax.get_xticklabels(), rotation=30, ha="right", rotation_mode="anchor")
fig.tight_layout()
fig.savefig(outpath, transparent=True)
def get_data(path, days):
ps = pdfstat.PdfStat()
p = pdfstat.normalized_path(path)
if not ps.db.is_tracked(p):
sys.exit("Document not tracked!")
all_data = ps.db.doc_data(p)
now = datetime.now()
return pd.DataFrame(takewhile(lambda x: (now-x.time).days < days, all_data))
def normalize(data):
data['page'] -= data['page'].iloc[-1]
def parse_args():
parser = argparse.ArgumentParser(description="Make a graph displaying progress of reading documents.")
parser.add_argument("doc", type=pdfstat.normalized_path, help="The document")
parser.add_argument("out", help="The output file.")
parser.add_argument("--goal", help="Goal pages/day", type=float)
parser.add_argument("--days", help="How many days to plot", default=30, type=int)
return parser.parse_args()
args = parse_args()
data = get_data(args.doc, args.days)
normalize(data)
# print(data)
make_plot(data, args.out, args.goal)