Created
October 13, 2016 16:56
-
-
Save matham/61c45e66567b07f20840eaea0488767a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import numpy as np | |
import matplotlib.pylab as plt | |
import matplotlib | |
from datetime import datetime | |
def import_data(filename, n_wheels): | |
with open(filename) as fh: | |
reader = csv.reader(fh, delimiter='\t') | |
rows = [[r[0]] + list(map(int, r[1:])) for r in reader] | |
data = np.zeros((len(rows), n_wheels)) | |
for i, row in enumerate(rows): | |
data[i, :] = row[1:1 + n_wheels] | |
return data.T, [r[0] for r in rows] | |
def plot_actogram(data, times, n_wheels, animals): | |
if n_wheels != len(animals): | |
raise Exception("Animals doesn't match n_wheels") | |
N, T = data.shape | |
data /= np.max(data) | |
times = [datetime.strptime(t, '%m/%d/%y %H:%M') for t in times] | |
times = matplotlib.dates.date2num(times) | |
fig = plt.figure() | |
ax = fig.add_subplot(1, 1, 1) | |
for i in range(N): | |
d = data[i, :] != 0 | |
plt.plot_date(times[d], data[i, d] + i, '.', markersize=2, xdate=True) | |
plt.grid(which='both') | |
major_ticks = np.arange(8) + .25 | |
minor_ticks = np.arange(32) * .25 | |
ax.set_yticks(major_ticks) | |
ax.set_yticks(minor_ticks, minor=True) | |
ax.set_yticklabels(animals) | |
ax.grid(which='minor', alpha=0.4) | |
vals = ax.get_xticks() | |
minor = [] | |
for i, val in enumerate(vals): | |
for t in range(4): | |
minor.append(val + t / 4.) | |
ax.set_xticks(minor, minor=True) | |
ax.grid(which='minor', alpha=0.4) | |
plt.tight_layout() | |
plt.show() | |
if __name__ == '__main__': | |
n_wheels = 8 | |
animals = ['1730', '1734', '1736', '1743', '1744', '1745', '1746', '1747'] | |
filename = r'E:\image j text.txt' | |
data, t = import_data(filename, n_wheels) | |
plot_actogram(data, t, n_wheels, animals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment