-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsssio.py
214 lines (168 loc) · 6.57 KB
/
sssio.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
import sys
import os
from pathlib import Path
import dill
import numpy as np
import neo
import quantities as pq
import logging
"""
## ####### ###### ###### #### ## ## ######
## ## ## ## ## ## ## ## ### ## ## ##
## ## ## ## ## ## #### ## ##
## ## ## ## #### ## #### ## ## ## ## ## ####
## ## ## ## ## ## ## ## ## #### ## ##
## ## ## ## ## ## ## ## ## ### ## ##
######## ####### ###### ###### #### ## ## ######
"""
def create_logger(filename=None, filemode="w"):
"""provide a filename to also log to file, otherwise just print"""
# log_fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
log_fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
date_fmt = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(log_fmt, datefmt=date_fmt)
# get all loggers
logger = logging.getLogger()
# scope restrictions
# logging.getLogger('matplotlib').setLevel(logging.WARNING)
# logging.getLogger('functions').setLevel(logging.INFO)
disable = dict(matplotlib=logging.WARNING, functions=logging.INFO) # to be an arg
if disable is not None:
for module, level in disable.items():
logging.getLogger(module).setLevel(level)
# for printing to stdout
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO) # <- this needs to be set as a default argument
sys.excepthook = handle_unhandled_exception
# config logger for writing to file
# file_handler = logging.FileHandler(filename="%s.log" % exp_name, mode='w')
if filename is not None:
file_handler = logging.FileHandler(filename=filename, mode=filemode)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
def get_logger(filename=None):
# to return a previously created logger
logger = create_logger(filename, filemode="a")
return logger
# logging unhandled exceptions
def handle_unhandled_exception(exc_type, exc_value, exc_traceback):
# TODO make this cleaner that it doesn't use global namespace
logging.critical(
"Unhandled exception", exc_info=(exc_type, exc_value, exc_traceback)
)
"""
###### ####### ## ## ## ## ######## ######## ######## ######## ######## ######
## ## ## ## ### ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## #### ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ###### ######## ## ###### ######## ######
## ## ## ## #### ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ### ## ## ## ## ## ## ## ## ## ## ##
###### ####### ## ## ### ######## ## ## ## ######## ## ## ######
"""
supported_filetypes = [".asc", ".smr", ".bin", ".csv"]
def csv2seg(path, t_col=0, v_col=1, header=1):
with open(path, "r") as fH:
lines = [line.strip() for line in fH.readlines()]
times = []
values = []
for i, line in enumerate(lines):
if i > header:
t = line.split(",")[t_col]
v = line.split(",")[v_col]
times.append(t)
values.append(v)
fs = 1 / np.average(np.diff(np.array(times)))
Asig = neo.core.AnalogSignal(
np.array(values), units=pq.uV, sampling_rate=fs * pq.Hz
)
segment = neo.core.Segment()
segment.analogsignals = [Asig]
segment.annotate(filename=str(path))
return segment
def asc2seg(path):
"""reads an autospike .asc file into a neo segment"""
header_rows = 6
with open(path, "r") as fH:
lines = [line.strip() for line in fH.readlines()]
header = lines[:header_rows]
data = lines[header_rows:]
rec_fac = float(header[3].split(" ")[3])
fs = float(header[4].split(" ")[3])
Data = np.array([d.split("\t")[1] for d in data], dtype="float")
Asig = neo.core.AnalogSignal(Data, units=pq.uV, sampling_rate=fs * pq.Hz)
segment = neo.core.Segment()
segment.analogsignals = [Asig]
segment.annotate(filename=str(path))
return segment
def raw2seg(path, fs, dtype):
"""reads a raw binary file into a neo segment. Requires manual
specification of data type and sampling rate"""
Data = np.fromfile(path, dtype=dtype)
Asig = neo.core.AnalogSignal(Data, units=pq.uV, sampling_rate=fs * pq.Hz)
segment = neo.core.Segment()
segment.analogsignals = [Asig]
segment.annotate(filename=str(path))
return segment
def smr2seg(path, channel_index=0):
"""channel_index selects the respective channel in the .smr file
that contains the voltage data"""
channel_index = int(channel_index)
reader = neo.io.Spike2IO(path)
(Blk,) = reader.read(lazy=False)
segment = Blk.segments[0]
try:
segment.analogsignals = [segment.analogsignals[channel_index]]
except IndexError:
logging.error(
"trying to access channel with index %i in .smr file %s, but the channel is not present"
% (channel_index, path)
)
segment.annotate(filename=str(path))
return segment
"""
#### #######
## ## ##
## ## ##
## ## ##
## ## ##
## ## ##
#### #######
"""
def dill2blk(path):
with open(path, "rb") as fH:
Blk = dill.load(fH)
return Blk
def blk2dill(Blk, path):
"""dumps a block via dill"""
with open(path, "wb") as fH:
dill.dump(Blk, fH)
def get_data(path):
"""reads data at path"""
ext = os.path.splitext(path)[1]
if ext == ".dill":
return dill2blk(path)
if __name__ == "__main__":
""" """
path = Path(sys.argv[1]) #
args = sys.argv[2:] if len(sys.argv) > 1 else None
if path.suffix in supported_filetypes:
match path.suffix:
case ".asc":
seg = asc2seg(path)
case ".csv":
seg = csv2seg(path, *args)
case ".smr":
seg = smr2seg(path, *args)
case ".bin":
seg = raw2seg(path, *args)
else:
logging.critical(
"reading files of type %s is currently not supported, but feel free to post an issue on github.com/grg2rsr/SSSort"
)
# seg = smr2seg(path, channel_index=0)
Blk = neo.core.Block()
Blk.segments = [seg]
blk2dill(Blk, path.with_suffix(".dill"))