-
Notifications
You must be signed in to change notification settings - Fork 4
/
paq2py.py
109 lines (94 loc) · 3.17 KB
/
paq2py.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
paq2py
Read PAQ file (from PackIO) into python
Lloyd Russell 2015
"""
import numpy as np
def paq_read(file_path=None, plot=False):
"""
Read PAQ file (from PackIO) into python
Lloyd Russell 2015
Parameters
==========
file_path : str, optional
full path to file to read in. if none is supplied a load file dialog
is opened, buggy on mac osx - Tk/matplotlib. Default: None.
plot : bool, optional
plot the data after reading? Default: False.
Returns
=======
data : ndarray
the data as a m-by-n array where m is the number of channels and n is
the number of datapoints
chan_names : list of str
the names of the channels provided in PackIO
hw_chans : list of str
the hardware lines corresponding to each channel
units : list of str
the units of measurement for each channel
rate : int
the acquisition sample rate, in Hz
"""
# file load gui
if file_path is None:
import Tkinter
import tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
root.destroy()
# open file
fid = open(file_path, 'rb')
# get sample rate
rate = int(np.fromfile(fid, dtype='>f', count=1))
# get number of channels
num_chans = int(np.fromfile(fid, dtype='>f', count=1))
# get channel names
chan_names = []
for i in range(num_chans):
num_chars = int(np.fromfile(fid, dtype='>f', count=1))
chan_name = ''
for j in range(num_chars):
chan_name = chan_name + chr(int(np.fromfile(fid, dtype='>f', count=1)[0]))
chan_names.append(chan_name)
# get channel hardware lines
hw_chans = []
for i in range(num_chans):
num_chars = int(np.fromfile(fid, dtype='>f', count=1))
hw_chan = ''
for j in range(num_chars):
hw_chan = hw_chan + chr(int(np.fromfile(fid, dtype='>f', count=1)[0]))
hw_chans.append(hw_chan)
# get acquisition units
units = []
for i in range(num_chans):
num_chars = int(np.fromfile(fid, dtype='>f', count=1))
unit = ''
for j in range(num_chars):
unit = unit + chr(int(np.fromfile(fid, dtype='>f', count=1)[0]))
units.append(unit)
# get data
temp_data = np.fromfile(fid, dtype='>f', count=-1)
num_datapoints = int(len(temp_data)/num_chans)
data = np.reshape(temp_data, [num_datapoints, num_chans]).transpose()
# close file
fid.close()
# plot
if plot:
# import matplotlib
# matplotlib.use('QT4Agg')
import matplotlib.pylab as plt
f, axes = plt.subplots(num_chans, 1, sharex=True, figsize=(10, num_chans), frameon=False)
for idx, ax in enumerate(axes):
ax.plot(data[idx])
ax.set_xlim([0, num_datapoints-1])
ax.set_ylim([data[idx].min()-1, data[idx].max()+1])
# ax.set_ylabel(units[idx])
ax.set_title(chan_names[idx])
plt.tight_layout()
plt.show()
return {"data": data,
"chan_names": chan_names,
"hw_chans": hw_chans,
"units": units,
"rate": rate}