-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmea-runner.py
executable file
·215 lines (176 loc) · 7.16 KB
/
mea-runner.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
import os
import sys
import argparse
import glob
import logging
import warnings
import pandas as pd
warnings.filterwarnings("ignore", category=DeprecationWarning)
MEA_TOOLS_SETTINGS_DIR = os.path.expanduser('~/.mea-tools')
if not os.path.exists(MEA_TOOLS_SETTINGS_DIR):
os.makedirs(MEA_TOOLS_SETTINGS_DIR)
logging.basicConfig(filename=os.path.join(MEA_TOOLS_SETTINGS_DIR, 'log.txt'),
level=logging.INFO)
def view(args):
import pymea.ui.viewer
if args.spikes is not None:
if not os.path.exists(args.spikes):
print('No such file or directory: %s.' % args.spikes)
return
if os.path.exists(args.FILE):
spike_file = None
analog_file = None
if args.FILE.endswith('.csv'):
spike_file = args.FILE
if os.path.exists(args.FILE[:-4] + '.h5'):
analog_file = args.FILE[:-4] + '.h5'
elif args.FILE.endswith('.h5'):
analog_file = args.FILE
if args.spikes is not None:
spike_file = args.spikes
elif os.path.exists(args.FILE[:-3] + '.csv'):
spike_file = args.FILE[:-3] + '.csv'
else:
raise IOError('Invalid input file, must be of type csv or h5.')
if args.FILE.endswith('csv'):
show = 'raster'
else:
show = 'analog'
pymea.ui.viewer.run(analog_file, spike_file, show)
else:
print('No such file or directory.')
def info(args):
import pymea as mea
if args.FILE.endswith('.h5'):
if os.path.exists(args.FILE):
store = mea.MEARecording(args.FILE)
print(store)
def detect_spikes(args):
if len(args.FILES) == 1:
files = [f for f in glob.glob(args.FILES[0])
if f.endswith('.h5') and os.path.exists(f)]
else:
files = [f for f in args.FILES
if f.endswith('.h5') and os.path.exists(f)]
import pymea as mea
for i, f in enumerate(files):
try:
mea.export_spikes(f, args.amplitude,
sort=args.sort,
conductance=args.sort,
neg_only=args.neg_only)
print('%d of %d exported.' % (i + 1, len(files)))
except Exception as e:
print('Error processing: %s' % f)
logging.exception(e)
def tag_cond(args):
if len(args.FILES) < 2:
print('Must specify src and dest files.')
return
src = args.FILES[-1]
if len(args.FILES) == 2:
files = [f for f in glob.glob(args.FILES[0])
if f.endswith('.csv') and os.path.exists(f)]
else:
files = [f for f in args.FILES[:-1] if f.endswith('.csv')]
seqs = []
with open(src, 'r') as f:
for line in f:
seqs.append([s.lower().strip() for s in line.split(',')])
import pymea as mea
for i, f in enumerate(files):
conductance_locs = []
df = pd.read_csv(f)
spikes = mea.MEASpikeDict(df)
for seq in seqs:
e_keep = seq[0]
sub_df = pd.concat([spikes[tag] for tag in seq])
cofiring = pd.concat(mea.cofiring_events(sub_df, 0.0007))
conductance_locs.extend(
list(cofiring[cofiring.electrode != e_keep].index.values))
df['conductance'] = False
df.ix[conductance_locs, 'conductance'] = True
df.to_csv(f, index=False)
print('%d of %d exported.' % (i + 1, len(files)))
def export_cond(args):
keys = [s.strip().lower() for s in args.ELECTRODES.split(',')]
spike_file = args.FILE[:-3]+'.csv'
if not os.path.exists(args.FILE):
print('File not found.')
return
if not os.path.exists(spike_file):
print('Spike file not found, you must detect spikes first.')
return
import pymea as mea
mea.export_conduction_waveforms(keys, spike_file, args.FILE,
args.window/1000.0)
def main():
parser = argparse.ArgumentParser(prog='mea')
subparsers = parser.add_subparsers()
parser_view = subparsers.add_parser('view',
help='View a data file')
parser_view.add_argument('FILE',
action='store',
help='File name or path.')
parser_view.add_argument('--spikes',
type=str,
default=None,
help='File name or path for spike data.')
parser_view.set_defaults(func=view)
parser_info = subparsers.add_parser('info',
help='Display file information.')
parser_info.add_argument('FILE',
action='store',
help='File name or path.')
parser_info.set_defaults(func=info)
parser_detect_spikes = subparsers.add_parser(
'detect', help='Detect spikes in h5 files.', aliases=['export_spikes'])
parser_detect_spikes.add_argument('--amplitude',
type=float,
default=6.0,
help='Amplitude threshold in std devs.')
parser_detect_spikes.add_argument('--neg-only',
dest='neg_only',
action='store_true',
help='Only detect negative amplitudes.')
parser_detect_spikes.add_argument('--sort',
dest='sort',
action='store_true',
help='Sort spikes after detection.')
parser_detect_spikes.add_argument('--no-sort',
dest='sort',
action='store_false',
help='Do not sort spikes after detection.') # noqa
parser_detect_spikes.add_argument('FILES',
help='Files to convert.',
nargs='+')
parser_detect_spikes.set_defaults(sort=True, func=detect_spikes)
parser_tag = subparsers.add_parser(
'tag', help='Tag conductance traces using specified file.'
)
parser_tag.add_argument('FILES',
help='[src] [dest ...].',
nargs='+')
parser_tag.set_defaults(sort=True, func=tag_cond)
parser_export_cond = subparsers.add_parser(
'export_cond', help='Export conduction waveforms.'
)
parser_export_cond.add_argument('--window',
type=float,
default=5.0,
help='Width in ms of extracted window.')
parser_export_cond.add_argument('FILE')
parser_export_cond.add_argument('ELECTRODES')
parser_export_cond.set_defaults(sort=True, func=export_cond)
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_usage()
else:
try:
logging.info(sys.argv)
args.func(args)
except Exception as e:
logging.exception(e)
if __name__ == '__main__':
main()