Skip to content

Commit 28cc1a5

Browse files
committed
Add strict_gap_mode=True/False to handle new clock sustem in neuralynx
1 parent 23bc316 commit 28cc1a5

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

neo/rawio/neuralynxrawio/ncssections.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def _buildNcsGeneric(ncsMemMap, sampFreq, gapTolerance=0):
487487
return ncsSects
488488

489489
@staticmethod
490-
def build_for_ncs_file(ncsMemMap, nlxHdr, gapTolerance=None):
490+
def build_for_ncs_file(ncsMemMap, nlxHdr, gapTolerance=None, strict_gap_mode=True):
491491
"""
492492
Build an NcsSections object for an NcsFile, given as a memmap and NlxHeader,
493493
handling gap detection appropriately given the file type as specified by the header.
@@ -512,15 +512,25 @@ def build_for_ncs_file(ncsMemMap, nlxHdr, gapTolerance=None):
512512
microsPerSampUsed = math.floor(NcsSectionsFactory.get_micros_per_samp_for_freq(freq))
513513
sampFreqUsed = NcsSectionsFactory.get_freq_for_micros_per_samp(microsPerSampUsed)
514514
if gapTolerance is None:
515-
gapTolerance = 0
515+
if strict_gap_mode:
516+
# this is the old behavior, maybe we could put 0.9 sample interval no ?
517+
gapTolerance = 0
518+
else:
519+
gapTolerance = 0
520+
516521
ncsSects = NcsSectionsFactory._buildNcsGeneric(ncsMemMap, sampFreqUsed, gapTolerance=gapTolerance)
517522
ncsSects.sampFreqUsed = sampFreqUsed
518523
ncsSects.microsPerSampUsed = microsPerSampUsed
519524

520525
elif acqType in ["DIGITALLYNX", "DIGITALLYNXSX", "CHEETAH64", "CHEETAH560", "RAWDATAFILE"]:
521526
# digital lynx style with fractional frequency and micros per samp determined from block times
522527
if gapTolerance is None:
523-
gapTolerance = round(NcsSectionsFactory._maxGapSampFrac * 1e6 / freq)
528+
if strict_gap_mode:
529+
# this is the old behavior
530+
gapTolerance = round(NcsSectionsFactory._maxGapSampFrac * 1e6 / freq)
531+
else:
532+
# quarter of paquet size is tolerate
533+
gapTolerance = round(0.25 * NcsSection._RECORD_SIZE * 1e6 / freq)
524534
ncsSects = NcsSectionsFactory._buildNcsGeneric(ncsMemMap, freq, gapTolerance=gapTolerance)
525535

526536

@@ -543,8 +553,12 @@ def build_for_ncs_file(ncsMemMap, nlxHdr, gapTolerance=None):
543553

544554
elif acqType == "BML" or acqType == "ATLAS":
545555
# BML & ATLAS style with fractional frequency and micros per samp
546-
if gapTolerance is None:
556+
if strict_gap_mode:
557+
# this is the old behavior, maybe we could put 0.9 sample interval no ?
547558
gapTolerance = 0
559+
else:
560+
# quarter of paquet size is tolerate
561+
gapTolerance = round(0.25 * NcsSection._RECORD_SIZE * 1e6 / freq)
548562
ncsSects = NcsSectionsFactory._buildNcsGeneric(ncsMemMap, freq, gapTolerance=gapTolerance)
549563
ncsSects.sampFreqUsed = freq
550564
ncsSects.microsPerSampUsed = NcsSectionsFactory.get_micros_per_samp_for_freq(freq)

neo/rawio/neuralynxrawio/neuralynxrawio.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ class NeuralynxRawIO(BaseRawIO):
8585
keep_original_times: bool, default: False
8686
If True, keep original start time as in files,
8787
Otherwise set 0 of time to first time in dataset
88-
88+
strict_gap_mode: bool, default: True
89+
Detect gaps using strict mode or not.
90+
* strict_gap_mode = True then a gap is consider when timstamp difference between two
91+
consequtive data packet is more than one sample interval.
92+
* strict_gap_mode = False then a gap has an increased tolerance. Some new system with different clock need this option
93+
otherwise, too many gaps are detected
94+
8995
Notes
9096
-----
9197
* This IO supports NCS, NEV, NSE and NTT file formats (not NVT or NRD yet)
@@ -125,7 +131,7 @@ class NeuralynxRawIO(BaseRawIO):
125131
("samples", "int16", (NcsSection._RECORD_SIZE)),
126132
]
127133

128-
def __init__(self, dirname="", filename="", exclude_filename=None, keep_original_times=False, **kargs):
134+
def __init__(self, dirname="", filename="", exclude_filename=None, keep_original_times=False, strict_gap_mode=True, **kargs):
129135

130136
if dirname != "":
131137
self.dirname = dirname
@@ -137,6 +143,7 @@ def __init__(self, dirname="", filename="", exclude_filename=None, keep_original
137143
raise ValueError("One of dirname or filename must be provided.")
138144

139145
self.keep_original_times = keep_original_times
146+
self.strict_gap_mode = strict_gap_mode
140147
self.exclude_filename = exclude_filename
141148
BaseRawIO.__init__(self, **kargs)
142149

@@ -790,7 +797,7 @@ def scan_stream_ncs_files(self, ncs_filenames):
790797

791798
verify_sec_struct = NcsSectionsFactory._verifySectionsStructure
792799
if not chanSectMap or (not verify_sec_struct(data, chan_ncs_sections)):
793-
chan_ncs_sections = NcsSectionsFactory.build_for_ncs_file(data, nlxHeader)
800+
chan_ncs_sections = NcsSectionsFactory.build_for_ncs_file(data, nlxHeader, strict_gap_mode=self.strict_gap_mode)
794801

795802
# register file section structure for all contained channels
796803
for chan_uid in zip(nlxHeader["channel_names"], np.asarray(nlxHeader["channel_ids"], dtype=str)):

0 commit comments

Comments
 (0)