Skip to content

Instantly share code, notes, and snippets.

@PeterNSteinmetz
Created November 13, 2024 01:27
Show Gist options
  • Save PeterNSteinmetz/0472b4a7fd75749922a5c76d2b0d4c6f to your computer and use it in GitHub Desktop.
Save PeterNSteinmetz/0472b4a7fd75749922a5c76d2b0d4c6f to your computer and use it in GitHub Desktop.
Test of python-neo Issue #1597
"""
Script to test retrieval of timestamps in Gavin Yang Neuralynx .ncs and .nev files.
"""
from neo.rawio.neuralynxrawio.neuralynxrawio import NeuralynxRawIO
# nev file
nevRawIO = NeuralynxRawIO(dirname='.', include_filenames=['Events_0012.nev'], keep_original_times=True)
nevRawIO.parse_header()
assert(nevRawIO.global_t_start==0)
assert(nevRawIO.global_t_stop==3150.419734954834)
# there are two event channels in this .nev file because there are two separate event_ids used,
# 4 and 19.
assert(nevRawIO.event_channels_count()==2)
# Retrieving without specifying the event_channel_index, returns the 0th one by default,
# corresponding to event_id==4. The first event with this id is the 3rd one in the file. There
# are only 2 events in this event_channel.
assert(nevRawIO.event_count(0,0,0)==2)
event_timestamps, _, event_labels = nevRawIO.get_event_timestamps()
# value in microseconds from binary inspection of file
assert(event_timestamps[0]==1597946624394866)
event_timestamps_rescaled = nevRawIO.rescale_event_timestamp(event_timestamps)
assert(event_timestamps_rescaled[0]==1597946624.394866) # in seconds
# The first and last events in this file belong to channel with event_id==19, with
# event_channel_index == 1, which contains 3 events.
event_timestamps, _, event_labels = nevRawIO.get_event_timestamps(0,0,1)
assert(len(event_timestamps)==3)
# The value of global_t_stop is erroneously set to the difference between the first and last
# events in this case. This is a bug, though not clearly related to that reported in Issue #1597.
assert(event_timestamps[2]-event_timestamps[0]==3150419735)
# ncs file
ncsRawIO = NeuralynxRawIO(dirname='.', include_filenames=['photo1_0012.ncs'], keep_original_times=True)
ncsRawIO.parse_header()
assert(ncsRawIO.global_t_start==0)
# This file is much longer in real time, possibly due to the gaps combined with starting and
# stopping the recording.
assert(ncsRawIO.global_t_stop==12532.693468093872)
assert(ncsRawIO.block_count()==1)
# this shows there are gaps in the timestamps between contiguous blocks of recording in this file
assert(ncsRawIO.segment_count(0)==4942)
# first sample in the first signal channel
str0tStart = ncsRawIO.get_signal_t_start(0,0,0)
str0Len = ncsRawIO.get_signal_size(0,0,0)
# value from binary inspection of file converted to seconds
# Note that this is very close to the global_tstart of the .Nev file.
assert(str0tStart==1597936900.445226)
assert(str0Len==15872) # there are this many 512 sample records in the first contiguous block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment