Skip to content

Commit 61b46d3

Browse files
committed
Some clean
1 parent a7213be commit 61b46d3

File tree

1 file changed

+16
-65
lines changed

1 file changed

+16
-65
lines changed

neo/rawio/spikegadgetsrawio.py

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ def _parse_header(self):
4747
f.seek(0)
4848
header_txt = f.read(header_size).decode('utf8')
4949

50-
#~ print(header_txt[-10:])
51-
#~ f.seek(header_size)
52-
#~ print(f.read(10))
53-
54-
#~ exit()
5550
# explore xml header
5651
root = ElementTree.fromstring(header_txt)
5752
gconf = sr = root.find('GlobalConfiguration')
@@ -64,70 +59,40 @@ def _parse_header(self):
6459
# explore sub stream and count packet size
6560
# first bytes is 0x55
6661
packet_size = 1
67-
68-
#~ main_dtype = []
6962
stream_bytes = {}
7063
for device in hconf:
7164
stream_id = device.attrib['name']
7265
num_bytes = int(device.attrib['numBytes'])
7366
stream_bytes[stream_id] = packet_size
7467
packet_size += num_bytes
75-
#~ print('packet_size', packet_size)
76-
#~ print(stream_bytes)
77-
#~ exit()
7868

7969
# timesteamps 4 uint32
8070
self._timestamp_byte = packet_size
8171
packet_size += 4
8272

8373
packet_size += 2 * num_ephy_channels
8474

85-
print('packet_size', packet_size)
86-
87-
#~ num_ephy_channels = num_ephy_channels * 4
88-
#~ num_ephy_channels = 0
89-
#~ chan_ids = []
90-
#~ for chan_ind, trode in enumerate(sconf):
91-
#~ for spikechan in trode:
92-
#~ print(spikechan, spikechan.attrib)
93-
#~ num_ephy_channels += 1
94-
#~ chan_ids.append(int(spikechan.attrib['hwChan']))
95-
#~ print('num_ephy_channels', num_ephy_channels)
96-
#~ print(np.sort(chan_ids))
97-
98-
99-
100-
101-
75+
# read the binary part lazily
10276
raw_memmap = np.memmap(self.filename, mode='r', offset=header_size, dtype='<u1')
103-
104-
inds, = np.nonzero(raw_memmap == 0x55)
105-
#~ print(inds)
106-
#~ print(np.diff(inds))
107-
#~ exit()
108-
109-
11077
num_packet = raw_memmap.size // packet_size
111-
#~ print(num_packet, num_packet*packet_size, raw_memmap.size)
11278
raw_memmap = raw_memmap[:num_packet*packet_size]
11379
self._raw_memmap = raw_memmap.reshape(-1, packet_size)
114-
11580

81+
# create signal channels
11682
stream_ids = []
11783
signal_streams = []
11884
signal_channels = []
11985

120-
# walk deveice and keep only "analog" one
86+
# walk in xml device and keep only "analog" one
12187
self._mask_channels_bytes = {}
12288
for device in hconf:
12389
stream_id = device.attrib['name']
12490
for channel in device:
125-
#~ print(channel, channel.attrib)
126-
91+
12792
if 'interleavedDataIDByte' in channel.attrib:
12893
# TODO deal with "headstageSensor" wich have interleaved
12994
continue
130-
95+
13196
if channel.attrib['dataType'] == 'analog':
13297

13398
if stream_id not in stream_ids:
@@ -138,17 +103,15 @@ def _parse_header(self):
138103

139104
name = channel.attrib['id']
140105
chan_id = channel.attrib['id']
141-
dtype = 'int16' # TODO check this
106+
dtype = 'int16'
142107
units = 'uV' # TODO check where is the info
143108
gain = 1. # TODO check where is the info
144-
offset = 0. # TODO check where is the info
109+
offset = 0.
145110
signal_channels.append((name, chan_id, self._sampling_rate, 'int16',
146111
units, gain, offset, stream_id))
147112

148-
#~ self._bytes_in_streams[stream_id].append()
149113
num_bytes = stream_bytes[stream_id] + int(channel.attrib['startByte'])
150114
chan_mask = np.zeros(packet_size, dtype='bool')
151-
# int6: 2 bytes
152115
chan_mask[num_bytes] = True
153116
chan_mask[num_bytes+1] = True
154117
self._mask_channels_bytes[stream_id].append(chan_mask)
@@ -166,37 +129,33 @@ def _parse_header(self):
166129
chan_id = schan.attrib['hwChan']
167130
units = 'uV' # TODO check where is the info
168131
gain = 1. # TODO check where is the info
169-
offset = 0. # TODO check where is the info
132+
offset = 0.
170133
signal_channels.append((name, chan_id, self._sampling_rate, 'int16',
171134
units, gain, offset, stream_id))
172135

173136
chan_mask = np.zeros(packet_size, dtype='bool')
174-
175137
num_bytes = packet_size - 2 * num_ephy_channels + 2 * chan_ind
176138
chan_mask[num_bytes] = True
177139
chan_mask[num_bytes+1] = True
178140
self._mask_channels_bytes[stream_id].append(chan_mask)
179141

180142
chan_ind += 1
181143

182-
# make mask as array
144+
# make mask as array (used in _get_analogsignal_chunk(...))
183145
self._mask_streams = {}
184146
for stream_id, l in self._mask_channels_bytes.items():
185147
mask = np.array(l)
186148
self._mask_channels_bytes[stream_id] = mask
187149
self._mask_streams[stream_id] = np.any(mask, axis=0)
188150

189-
190-
191151
signal_streams = np.array(signal_streams, dtype=_signal_stream_dtype)
192152
signal_channels = np.array(signal_channels, dtype=_signal_channel_dtype)
193153

194-
195-
# No events
154+
# No events channels
196155
event_channels = []
197156
event_channels = np.array(event_channels, dtype=_event_channel_dtype)
198157

199-
# No spikes
158+
# No spikes channels
200159
spike_channels = []
201160
spike_channels = np.array(spike_channels, dtype=_spike_channel_dtype)
202161

@@ -235,16 +194,13 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
235194
stream_id = self.header['signal_streams'][stream_index]['id']
236195

237196
raw_unit8 = self._raw_memmap[i_start:i_stop]
238-
#~ print('raw_unit8', raw_unit8.shape, raw_unit8.dtype)
239197

240198
num_chan = len(self._mask_channels_bytes[stream_id])
241199
if channel_indexes is None:
242-
# no loop
200+
# no loop : entire stream mask
243201
stream_mask = self._mask_streams[stream_id]
244202
else:
245-
#~ print('channel_indexes', channel_indexes)
246-
#~ print('chan_inds', chan_inds)
247-
203+
# acculate mask
248204
if instance(channel_indexes, slice):
249205
chan_inds = np.arange(num_chan)[channel_indexes]
250206
else:
@@ -253,18 +209,13 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
253209
for chan_ind in chan_inds:
254210
chan_mask = self._mask_channels_bytes[stream_id][chan_ind]
255211
stream_mask |= chan_mask
212+
# TODO : make a fix when "channel_indexes" arein wring order.
256213

257-
258-
259-
#~ print(stream_mask)
260-
261-
# thisi fo a copy
214+
# this do a copy from memmap to memory
262215
raw_unit8_mask = raw_unit8[:, stream_mask]
263216
shape = raw_unit8_mask.shape
264217
shape = (shape[0], shape[1] // 2)
218+
# reshape the and re type by view
265219
raw_unit16 = raw_unit8_mask.flatten().view('int16').reshape(shape)
266-
#~ print(raw_unit16.shape,raw_unit16.strides)
267220

268221
return raw_unit16
269-
270-

0 commit comments

Comments
 (0)