Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions neo/io/neuralynxio.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def __init__(self, dirname='', filename='', use_cache=False, cache_path='same_as
----------
dirname : str
Directory containing data files
filename : str
Name of a single ncs, nse, nev, or ntt file to include in dataset. Will be ignored,
if dirname is provided.
use_cache : bool, optional
Cache results of initial file scans for faster loading in subsequent runs.
Default: False
Expand Down
33 changes: 25 additions & 8 deletions neo/rawio/neuralynxrawio/ncssections.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def __hash__(self):
return (f'{self.sampFreqUsed};{self.microsPerSampUsed};'
f'{[s.__hash__() for s in self.sects]}').__hash__()

def is_equivalent(self, other, rel_tol=0, abs_tol=0):
if len(self.sects) != len(other.sects):
return False
else:
# do not check for gaps if only a single section is present
for sec_id in range(len(self.sects) - 1):
if not self.sects[sec_id].is_equivalent(
other.sects[sec_id], rel_tol=rel_tol, abs_tol=abs_tol):
return False
return True


class NcsSection:
"""
Expand Down Expand Up @@ -66,6 +77,12 @@ def __hash__(self):
s = f'{self.startRec};{self.startTime};{self.endRec};{self.endTime};{self.n_samples}'
return s.__hash__()

def is_equivalent(self, other, rel_tol=0, abs_tol=0):
eq_start = math.isclose(self.startTime, other.startTime, rel_tol=rel_tol, abs_tol=abs_tol)
eq_end = math.isclose(self.endTime, other.endTime, rel_tol=rel_tol, abs_tol=abs_tol)
return eq_start & eq_end


def before_time(self, rhb):
"""
Determine if this section is completely before another section in time.
Expand Down Expand Up @@ -211,9 +228,9 @@ def _buildGivenActualFrequency(ncsMemMap, actualSampFreq, reqFreq):
raise IOError("Sampling frequency in first record doesn't agree with header.")
chanNum = ncsMemMap['channel_id'][0]

nb = NcsSections()
nb.sampFreqUsed = actualSampFreq
nb.microsPerSampUsed = NcsSectionsFactory.get_micros_per_samp_for_freq(actualSampFreq)
secs = NcsSections()
secs.sampFreqUsed = actualSampFreq
secs.microsPerSampUsed = NcsSectionsFactory.get_micros_per_samp_for_freq(actualSampFreq)

# check if file is one block of records, which is often the case, and avoid full parse
lastBlkI = ncsMemMap.shape[0] - 1
Expand All @@ -231,15 +248,15 @@ def _buildGivenActualFrequency(ncsMemMap, actualSampFreq, reqFreq):
n_samples = NcsSection._RECORD_SIZE * lastBlkI
curBlock = NcsSection(0, ts0, lastBlkI, lastBlkEndTime, n_samples)

nb.sects.append(curBlock)
return nb
secs.sects.append(curBlock)
return secs

# otherwise need to scan looking for breaks
else:
blkOnePredTime = NcsSectionsFactory.calc_sample_time(actualSampFreq, ts0, nb0)
curBlock = NcsSection(0, ts0, -1, -1, -1)
nb.sects.append(curBlock)
return NcsSectionsFactory._parseGivenActualFrequency(ncsMemMap, nb, chanNum, reqFreq,
secs.sects.append(curBlock)
return NcsSectionsFactory._parseGivenActualFrequency(ncsMemMap, secs, chanNum, reqFreq,
blkOnePredTime)

@staticmethod
Expand Down Expand Up @@ -369,7 +386,7 @@ def _buildForMaxGap(ncsMemMap, nomFreq):
endTime = NcsSectionsFactory.calc_sample_time(nomFreq, lts, lnb)
curBlock = NcsSection(0, ts0, lastBlkI, endTime, numSampsForPred)
nb.sects.append(curBlock)
nb.sampFreqUsed = numSampsForPred / (lts - ts0) * 1e6
nb.sampFreqUsed = (numSampsForPred + lnb) / (endTime - ts0) * 1e6
nb.microsPerSampUsed = NcsSectionsFactory.get_micros_per_samp_for_freq(nb.sampFreqUsed)

# otherwise parse records to determine blocks using default maximum gap length
Expand Down
Loading