@@ -217,7 +217,6 @@ def _parse_header(self):
217217 if name + "_npy" in info :
218218 data = np .load (info [name + "_npy" ], mmap_mode = "r" )
219219 info [name ] = data
220-
221220 # check that events have timestamps
222221 assert "timestamps" in info , "Event stream does not have timestamps!"
223222 # Updates for OpenEphys v0.6:
@@ -253,30 +252,50 @@ def _parse_header(self):
253252 # 'states' was introduced in OpenEphys v0.6. For previous versions, events used 'channel_states'
254253 if "states" in info or "channel_states" in info :
255254 states = info ["channel_states" ] if "channel_states" in info else info ["states" ]
255+
256256 if states .size > 0 :
257257 timestamps = info ["timestamps" ]
258258 labels = info ["labels" ]
259- rising = np .where (states > 0 )[0 ]
260- falling = np .where (states < 0 )[0 ]
261-
262- # infer durations
259+
260+ # Identify unique channels based on state values
261+ channels = np .unique (np .abs (states ))
262+
263+ rising_indices = []
264+ falling_indices = []
265+
266+ for channel in channels :
267+ # Find rising and falling edges for each channel
268+ rising = np .where (states == channel )[0 ]
269+ falling = np .where (states == - channel )[0 ]
270+
271+ # Ensure each rising has a corresponding falling
272+ if rising .size > 0 and falling .size > 0 :
273+ if rising [0 ] > falling [0 ]:
274+ falling = falling [1 :]
275+ if rising .size > falling .size :
276+ rising = rising [:- 1 ]
277+
278+ rising_indices .extend (rising )
279+ falling_indices .extend (falling )
280+
281+ rising_indices = np .array (rising_indices )
282+ falling_indices = np .array (falling_indices )
283+
284+ # Sort the indices to maintain chronological order
285+ sorted_order = np .argsort (rising_indices )
286+ rising_indices = rising_indices [sorted_order ]
287+ falling_indices = falling_indices [sorted_order ]
288+
263289 durations = None
264- if len (states ) > 0 :
265- # make sure first event is rising and last is falling
266- if states [0 ] < 0 :
267- falling = falling [1 :]
268- if states [- 1 ] > 0 :
269- rising = rising [:- 1 ]
270-
271- if len (rising ) == len (falling ):
272- durations = timestamps [falling ] - timestamps [rising ]
273- if not self ._use_direct_evt_timestamps :
274- timestamps = timestamps / info ["sample_rate" ]
275- durations = durations / info ["sample_rate" ]
276-
277- info ["rising" ] = rising
278- info ["timestamps" ] = timestamps [rising ]
279- info ["labels" ] = labels [rising ]
290+ if len (rising_indices ) == len (falling_indices ):
291+ durations = timestamps [falling_indices ] - timestamps [rising_indices ]
292+ if not self ._use_direct_evt_timestamps :
293+ timestamps = timestamps / info ["sample_rate" ]
294+ durations = durations / info ["sample_rate" ]
295+
296+ info ["rising" ] = rising_indices
297+ info ["timestamps" ] = timestamps [rising_indices ]
298+ info ["labels" ] = labels [rising_indices ]
280299 info ["durations" ] = durations
281300
282301 # no spike read yet
0 commit comments