Skip to content

Instantly share code, notes, and snippets.

@BaksiLi
Last active June 6, 2019 09:30
Show Gist options
  • Save BaksiLi/c138430c01cfc981efcbbb4c95846a68 to your computer and use it in GitHub Desktop.
Save BaksiLi/c138430c01cfc981efcbbb4c95846a68 to your computer and use it in GitHub Desktop.
Plot Activity Log from OctoMouse data
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import os
import shutil
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.offsetbox import AnchoredText
def archive_files() -> None:
# get root dir info
source = os.getcwd()
files = os.listdir(source)
# check destination dir
destination = os.path.join(source, 'archive')
os.makedirs(destination, exist_ok=True) # if no dir then create
# move files to destination dir
for f in files:
if f[:4] == 'plot' and f[-4:] == '.pdf':
shutil.move(f, destination)
def plot_usage(data: pd.DataFrame) -> None:
# create two subgraphs on a row
fig, (ax1, ax2) = plt.subplots(
nrows=1, ncols=2, sharex=True, figsize=(8, 5)
)
fig.suptitle('Activity log')
# subgraph 1
ax1.set_title('keystrokes')
data.plot(y='keystrokes', use_index=True, legend=False, ax=ax1)
ax1.plot([data['keystrokes'][:-1].mean()] * len(data),
label='Mean',
color='r',
linestyle='--')
# subgraph 2
ax2.set_title('Trackpad moving distance (in m)')
data.plot(y='trackpad distance', use_index=True, legend=False, ax=ax2)
ax2.plot([data['trackpad distance'][:-1].mean()] * len(data),
label='Mean',
color='r',
linestyle='--')
# archive old plots
#archive_files()
plt.show()
#plt.savefig('plot_{}.pdf'.format(datetime.date.today()))
def plot_usage_two_scales(data: pd.DataFrame) -> None:
# create two subgraphs on a row
fig, ax1 = plt.subplots()
plt.title('Activity log')
plt.xlabel('Days')
plt.legend()
# subgraph 1
colour = 'tab:blue'
mean = data['keystrokes'][:-1].mean()
plot1 = data.plot(
y='keystrokes',
color=colour,
xticks=[i for i in range(0, len(data), 5)],
use_index=True,
ax=ax1,
legend=False
)
ax1.plot([mean] * len(data),
label=' Mean: {}'.format(int(mean)),
color=colour,
linestyle='--')
ax1.set_ylabel('Number of Keystrokes')
# subgraph 2
ax2 = ax1.twinx()
colour = 'tab:red'
mean = data['trackpad distance'][:-1].mean()
plot2 = data.plot(
y='trackpad distance', color=colour, use_index=True, ax=ax2
)
ax2.plot([mean] * len(data),
label=' Mean: {:.1f} $m$'.format(mean),
color=colour,
linestyle='--')
ax2.set_ylabel('Trackpad Moving Distance (m)')
# legends
h1, l1 = plot1.get_legend_handles_labels()
h2, l2 = plot2.get_legend_handles_labels()
plt.legend(h1 + h2, l1 + l2, loc=1)
# panel
box = AnchoredText(
'Total Keys/Dist: {} / {:.1f} $m$'.format(
int(data['keystrokes'][:-1].sum()),
data['trackpad distance'][:-1].sum()
),
loc='lower left',
bbox_to_anchor=(.1, .1),
frameon=False
)
ax1.add_artist(box)
# TODO: https://matplotlib.org/gallery/widgets/textbox.html#sphx-glr-gallery-widgets-textbox-py TEXTBOX??
# archive old plots
archive_files()
fig.tight_layout()
fig.subplots_adjust(top=0.9)
# plt.show()
plt.savefig('plot_{}.pdf'.format(datetime.date.today()))
if __name__ == '__main__':
# read csv from OctoMouse with the last row dropped
data = pd.read_csv('*.csv')[:-1]
data['trackpad distance'] = (
data['mouse distance'] + data['scroll distance']
) * 1000 # in metres
# plot graphdata['mouse distance'] + data['scroll distance']
#plot_usage(data)
plot_usage_two_scales(data)
@BaksiLi
Copy link
Author

BaksiLi commented Jun 6, 2019

Data Sample:

date,elapsed seconds,mouse clicks,mouse distance,scroll distance,keystrokes
2019-05-17,30360,3567,0.48,0.04,19379
2019-05-18,26820,3758,0.49,0.04,18868
2019-05-19,36240,2848,0.34,0.02,17233
2019-05-20,34560,5435,0.53,0.04,35942
2019-05-21,37560,2627,0.12,0.02,31991
2019-05-22,852,93,0.02,0.00,1099
646F6E7A_00000000_00000001_6E7A6361_696D6963,0,0,0.00,0.00,0

Store above csv as *.csv in the same directory from analyse.py.

The plot:

plot_2019-05-22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment