Skip to content

Commit d7f46ee

Browse files
author
Daniel Crepeau
committed
Updates based on pull request #1288
1 parent 235f987 commit d7f46ee

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

neo/io/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* :attr:`KlustaKwikIO`
4040
* :attr:`KwikIO`
4141
* :attr:`MaxwellIO`
42+
* :attr:`MedIO`
4243
* :attr:`MicromedIO`
4344
* :attr:`NeoMatlabIO`
4445
* :attr:`NestIO`
@@ -164,6 +165,10 @@
164165
.. autoclass:: neo.io.MaxwellIO
165166
166167
.. autoattribute:: extensions
168+
169+
.. autoclass:: neo.io.MedIO
170+
171+
.. autoattribute:: extensions
167172
168173
.. autoclass:: neo.io.MicromedIO
169174

neo/rawio/medrawio.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,33 @@ def _parse_header(self):
9191
for chan_idx, chan_info in enumerate(self.sess.session_info['channels']):
9292
chan_freq = chan_info['metadata']['sampling_frequency']
9393

94+
# set MED session reference channel to be this channel, so the correct contigua is returned
95+
self.sess.set_reference_channel(chan_info['metadata']['channel_name'])
96+
contigua = self.sess.find_discontinuities()
97+
98+
# find total number of samples in this channel
99+
chan_num_samples = 0
100+
for seg_idx in range(len(contigua)):
101+
chan_num_samples += (contigua[seg_idx]['end_index'] - contigua[seg_idx]['start_index']) + 1
102+
94103
# see if we need a new stream, or add channel to existing stream
95104
add_to_existing_stream_info = False
96105
for stream_info in self._stream_info:
97-
if chan_freq == stream_info['sampling_frequency']:
106+
if chan_freq == stream_info['sampling_frequency'] and chan_num_samples == stream_info['num_samples']:
98107
# found a match, so add it!
99108
add_to_existing_stream_info = True
100109
stream_info['chan_list'].append((chan_idx, chan_info['metadata']['channel_name']))
101110
stream_info['raw_chans'].append(chan_info['metadata']['channel_name'])
111+
break
102112

103113
if not add_to_existing_stream_info:
104114
self._num_stream_info += 1
105-
# set MED session reference channel to be this channel, so the correct contigua is returned
106-
self.sess.set_reference_channel(chan_info['metadata']['channel_name'])
107-
contigua = self.sess.find_discontinuities()
108-
109-
# find total number of samples in this stream
110-
num_samples_in_stream = 0
111-
for seg_idx in range(len(contigua)):
112-
num_samples_in_stream += (contigua[seg_idx]['end_index'] - contigua[seg_idx]['start_index']) + 1
113115

114116
new_stream_info = {'sampling_frequency': chan_info['metadata']['sampling_frequency'], \
115117
'chan_list': [(chan_idx, chan_info['metadata']['channel_name'])], \
116118
'contigua' : contigua, \
117119
'raw_chans' : [chan_info['metadata']['channel_name']], \
118-
'num_samples' : num_samples_in_stream }
120+
'num_samples' : chan_num_samples}
119121

120122
self._stream_info.append(new_stream_info)
121123

@@ -126,8 +128,7 @@ def _parse_header(self):
126128
signal_channels = []
127129

128130
# fill in signal_streams and signal_channels info
129-
signal_stream_counter = 0
130-
for stream_info in self._stream_info:
131+
for signal_stream_counter, stream_info in enumerate(self._stream_info):
131132

132133
# get the stream start time, which is the start time of the first continuous section
133134
stream_start_time = (stream_info['contigua'][0]["start_time"] + self._session_time_offset ) / 1e6
@@ -140,8 +141,6 @@ def _parse_header(self):
140141
# add entry for signal_channels for each channel in a stream
141142
for chan in stream_info['chan_list']:
142143
signal_channels.append((chan[1], chan[0], stream_info['sampling_frequency'], 'int32', 'uV', 1, 0, stream_id))
143-
144-
signal_stream_counter += 1
145144

146145
signal_streams = np.array(signal_streams, dtype=_signal_stream_dtype)
147146
signal_channels = np.array(signal_channels, dtype=_signal_channel_dtype)
@@ -233,7 +232,7 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop,
233232
self.sess.set_channel_active(self._stream_info[stream_index]['raw_chans'][channel_idx])
234233
self.sess.set_reference_channel(self._stream_info[stream_index]['raw_chans'][channel_indexes[0]])
235234

236-
# Return empty dataset if start/stop sampes are equal
235+
# Return empty dataset if start/stop samples are equal
237236
if i_start == i_stop:
238237
raw_signals = np.zeros((0, num_channels), dtype='int32')
239238
return raw_signals
@@ -245,10 +244,10 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop,
245244
# Create "sample_major" 2D numpy array from the result of read_by_index()
246245
samps_returned = len(self.sess.data['channels'][0]['data'])
247246
raw_signals = np.array([], dtype=np.int32)
248-
for chan in self.sess.data['channels']:
249-
raw_signals = np.concatenate((raw_signals, chan['data']))
250-
raw_signals = raw_signals.reshape(num_channels, samps_returned)
251-
raw_signals = raw_signals.transpose() # this makes is sample_major rather than channel_major
247+
248+
raw_signals = np.empty((i_stop - i_start, num_channels))
249+
for i, chan in enumerate(self.sess.data['channels']):
250+
raw_signals[:,i] = chan['data']
252251

253252
return raw_signals
254253

neo/test/iotest/test_medio.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ def test_read_segment_lazy(self):
4848
np.testing.assert_array_equal(seg.analogsignals[0][0][:3], [-1, -4, -4])
4949

5050
for anasig in seg.analogsignals:
51-
assert isinstance(anasig, AnalogSignal)
5251
self.assertNotEqual(anasig.size, 0)
5352
for st in seg.spiketrains:
54-
assert isinstance(st, SpikeTrain)
5553
self.assertNotEqual(st.size, 0)
5654

5755
# annotations

neo/test/rawiotest/test_medrawio.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_scan_med_directory(self):
4343
self.assertEqual(rawio.num_channels_in_session, 3)
4444
self.assertEqual(rawio.header['signal_channels'].size, 3)
4545

46-
# Verity if found the names of the 3 channels
46+
# Verify if found the names of the 3 channels
4747
self.assertEqual(rawio.header['signal_channels'][0][0], 'CSC_0001')
4848
self.assertEqual(rawio.header['signal_channels'][1][0], 'CSC_0002')
4949
self.assertEqual(rawio.header['signal_channels'][2][0], 'CSC_0003')
@@ -156,9 +156,9 @@ def test_scan_med_directory(self):
156156
# They are 3 "Note" type events, and 2 "NlxP", or neuralynx, type events.
157157
# The first segment has one event, and the second and third segments
158158
# each have 2 events.
159-
self.assertEqual(rawio._event_count(0, 0, 0), 1)
160-
self.assertEqual(rawio._event_count(0, 1, 0), 2)
161-
self.assertEqual(rawio._event_count(0, 2, 0), 2)
159+
self.assertEqual(rawio.event_count(0, 0, 0), 1)
160+
self.assertEqual(rawio.event_count(0, 1, 0), 2)
161+
self.assertEqual(rawio.event_count(0, 2, 0), 2)
162162

163163
# Get array of all events in first segment of data
164164
events = rawio._get_event_timestamps(0, 0, 0, rawio._segment_t_start(0, 0), rawio._segment_t_stop(0, 0))
@@ -206,9 +206,9 @@ def test_scan_med_directory(self):
206206
# They are 3 "Note" type events, and 2 "NlxP", or neuralynx, type events.
207207
# The first segment has one event, and the second and third segments
208208
# each have 2 events.
209-
self.assertEqual(rawio._event_count(0, 0, 0), 1)
210-
self.assertEqual(rawio._event_count(0, 1, 0), 2)
211-
self.assertEqual(rawio._event_count(0, 2, 0), 2)
209+
self.assertEqual(rawio.event_count(0, 0, 0), 1)
210+
self.assertEqual(rawio.event_count(0, 1, 0), 2)
211+
self.assertEqual(rawio.event_count(0, 2, 0), 2)
212212

213213
# Get array of all events in first segment of data
214214
events = rawio._get_event_timestamps(0, 0, 0, rawio._segment_t_start(0, 0), rawio._segment_t_stop(0, 0))

0 commit comments

Comments
 (0)