Skip to content

Instantly share code, notes, and snippets.

@donkirkby
Last active August 29, 2015 14:06
Show Gist options
  • Save donkirkby/c2de5d80ffbbde2c046d to your computer and use it in GitHub Desktop.
Save donkirkby/c2de5d80ffbbde2c046d to your computer and use it in GitHub Desktop.
Mnemosyne history graph
from bz2 import BZ2File # @UnresolvedImport
from datetime import datetime
from glob import glob
import itertools
import matplotlib
import matplotlib.pyplot as plt
MNEMOSYNE_ROOTS = ["/home/don/Dropbox/Mnemosyne_old/.mnemosyne/",
"/home/don/.local/share/mnemosyne/"]
class History(object):
def __init__(self):
self.sdates = []
self.dates = []
self.scheduled_counts = []
self.unmemorised_counts = []
self.total_counts = []
def __repr__(self):
return 'History()'
def process_file(self, f):
scheduled = unmemorised = total = 0
current_date = ''
# Add an extra blank line at the end to trigger processing of final day.
for line in itertools.chain(f, ['3000-01-01 Saved 0 0 0']):
if 'Saved' in line:
date = line.split(' ')[0]
counts = map(int, line.rstrip().split(' ')[-3:])
if date != current_date:
if current_date != '' and not current_date.startswith('2014-08-11'):
self.sdates.append(current_date)
self.scheduled_counts.append(scheduled)
self.unmemorised_counts.append(unmemorised)
self.total_counts.append(total)
current_date = date
(scheduled,
unmemorised,
total) = counts
unmemorised += scheduled
else:
scheduled = min(scheduled, counts[0])
unmemorised = min(unmemorised, counts[1]+counts[0])
total = max(total, counts[2])
self.dates = map(lambda s: datetime.strptime(s, "%Y-%m-%d"), self.sdates)
def plot_history(history):
plot_dates = matplotlib.dates.date2num(history.dates)
fig, ax1 = plt.subplots()
ax1.plot_date(plot_dates, history.scheduled_counts, '-')
ax1.plot_date(plot_dates, history.unmemorised_counts, '-')
ax1.plot_date(plot_dates, history.total_counts, '-')
fig.autofmt_xdate()
plt.show()
if __name__ == '__main__':
h = History()
for root in MNEMOSYNE_ROOTS:
archives = glob(root + 'history/*.bz2')
archives.sort()
for archive in archives:
with BZ2File(archive, 'rU') as f:
h.process_file(f)
with open(root + 'log.txt', 'rU') as f:
h.process_file(f)
plot_history(h)
elif __name__ == '__live_coding__':
from StringIO import StringIO
f = StringIO("""\
2014-08-11 21:42:14 : Saved database 495 57 2946
2014-08-12 21:38:41 : Saved database 481 60 2946
2014-08-12 21:40:01 : Saved database 508 55 2956
2014-08-13 21:39:27 : Program started : Mnemosyne 2.2.1 posix linux2
""")
History().process_file(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment