Skip to content

Commit

Permalink
Merge pull request NeuralEnsemble#1426 from NeuralEnsemble/black-form…
Browse files Browse the repository at this point in the history
…atting

Black formatting
  • Loading branch information
alejoe91 authored Mar 3, 2024
2 parents d9c8683 + b1d61fc commit 6cd8158
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
27 changes: 15 additions & 12 deletions neo/rawio/neuralynxrawio/ncssections.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
The challenge addressed by the NcsSectionsFactory of this module is to separate those intended
gaps from those introduced by a quirk of the hardware, recording software, or file format.
"""

import math
import numpy as np

Expand Down Expand Up @@ -87,12 +88,12 @@ class NcsSection:
_RECORD_SIZE = 512 # nb sample per signal record

def __init__(self, startRec, startTime, endRec, endTime, n_samples):
self.startRec = startRec # index of starting record
self.startTime = startTime # starttime of first record
self.endRec = endRec # index of last record (inclusive)
self.endTime = endTime # end time of last record, that is, the end time of the last
# sampling period contained in the last record of the section
self.n_samples = n_samples # number of samples in record which are valid
self.startRec = startRec # index of starting record
self.startTime = startTime # starttime of first record
self.endRec = endRec # index of last record (inclusive)
self.endTime = endTime # end time of last record, that is, the end time of the last
# sampling period contained in the last record of the section
self.n_samples = n_samples # number of samples in record which are valid

def __eq__(self, other):
return (
Expand Down Expand Up @@ -200,7 +201,7 @@ def _parseGivenActualFrequency(ncsMemMap, ncsSects, chanNum, reqFreq, blkOnePred
delta_prediction = ((ncsMemMap["nb_valid"][:-1] / ncsSects.sampFreqUsed) * 1e6).astype(np.int64)
gap_inds = np.flatnonzero((delta - delta_prediction) != 0)
gap_inds += 1
sections_limits = [ 0 ] + gap_inds.tolist() + [len(ncsMemMap)]
sections_limits = [0] + gap_inds.tolist() + [len(ncsMemMap)]

for i in range(len(gap_inds) + 1):
start = sections_limits[i]
Expand All @@ -209,9 +210,10 @@ def _parseGivenActualFrequency(ncsMemMap, ncsSects, chanNum, reqFreq, blkOnePred
NcsSection(
startRec=start,
startTime=ncsMemMap["timestamp"][start],
endRec=stop-1,
endTime=ncsMemMap["timestamp"][stop-1] + np.uint64(ncsMemMap["nb_valid"][stop-1] / ncsSects.sampFreqUsed * 1e6),
n_samples=np.sum(ncsMemMap["nb_valid"][start:stop])
endRec=stop - 1,
endTime=ncsMemMap["timestamp"][stop - 1]
+ np.uint64(ncsMemMap["nb_valid"][stop - 1] / ncsSects.sampFreqUsed * 1e6),
n_samples=np.sum(ncsMemMap["nb_valid"][start:stop]),
)
)

Expand Down Expand Up @@ -272,13 +274,14 @@ def _buildGivenActualFrequency(ncsMemMap, actualSampFreq, reqFreq):
ncsSects.sects.append(curBlock)
return ncsSects


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

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion neo/rawio/neuralynxrawio/neuralynxrawio.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def _parse_header(self):
internal_ids = []
else:
data = self._get_file_map(filename)
if data.shape[0] == 0: # empty file
if data.shape[0] == 0: # empty file
self._empty_nse_ntt.append(filename)
internal_ids = np.unique(data[["event_id", "ttl_input"]]).tolist()
for internal_event_id in internal_ids:
Expand Down
39 changes: 20 additions & 19 deletions neo/rawio/neuralynxrawio/nlxheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,52 +81,53 @@ def _to_bool(txt):
"bml": dict(
datetime1_regex=r"## Time Opened: \(m/d/y\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
filename_regex=r"## File Name: (?P<filename>\S+)",
datetimeformat="%m/%d/%y %H:%M:%S.%f"
datetimeformat="%m/%d/%y %H:%M:%S.%f",
),
# Cheetah after version 1 and before version 5
"bv5": dict(
datetime1_regex=r"## Time Opened: \(m/d/y\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
filename_regex=r"## File Name: (?P<filename>\S+)",
datetimeformat="%m/%d/%Y %H:%M:%S.%f"
datetimeformat="%m/%d/%Y %H:%M:%S.%f",
),
# Cheetah version 5.4.0
"v5.4.0": dict(
datetime1_regex=r"## Time Opened \(m/d/y\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
datetime2_regex=r"## Time Closed \(m/d/y\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
filename_regex=r"## File Name: (?P<filename>\S+)",
datetimeformat="%m/%d/%Y %H:%M:%S.%f"
datetimeformat="%m/%d/%Y %H:%M:%S.%f",
),
# Cheetah version 5.6.0, some range of versions in between
"v5.6.0": dict(
datetime1_regex=r"## Time Opened: \(m/d/y\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
filename_regex=r"## File Name: (?P<filename>\S+)",
datetimeformat="%m/%d/%Y %H:%M:%S.%f"
datetimeformat="%m/%d/%Y %H:%M:%S.%f",
),
# Cheetah version 5 before and including v 5.6.4 as well as version 1
"bv5.6.4": dict(
datetime1_regex=r"## Time Opened \(m/d/y\): (?P<date>\S+)" r" \(h:m:s\.ms\) (?P<time>\S+)",
datetime2_regex=r"## Time Closed \(m/d/y\): (?P<date>\S+)" r" \(h:m:s\.ms\) (?P<time>\S+)",
filename_regex=r"## File Name (?P<filename>\S+)",
datetimeformat="%m/%d/%Y %H:%M:%S.%f"
datetimeformat="%m/%d/%Y %H:%M:%S.%f",
),
"neuraview2": dict(
datetime1_regex=r"## Date Opened: \(mm/dd/yyy\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
datetime2_regex=r"## Date Closed: \(mm/dd/yyy\): (?P<date>\S+)" r" At Time: (?P<time>\S+)",
filename_regex=r"## File Name: (?P<filename>\S+)",
datetimeformat="%m/%d/%Y %H:%M:%S"
datetimeformat="%m/%d/%Y %H:%M:%S",
),
'peg': dict(
datetime1_regex=r'-TimeCreated (?P<date>\S+) (?P<time>\S+)',
datetime2_regex=r'-TimeClosed (?P<date>\S+) (?P<time>\S+)',
"peg": dict(
datetime1_regex=r"-TimeCreated (?P<date>\S+) (?P<time>\S+)",
datetime2_regex=r"-TimeClosed (?P<date>\S+) (?P<time>\S+)",
filename_regex=r'-OriginalFileName "?(?P<filename>\S+)"?',
datetimeformat=r'%Y/%m/%d %H:%M:%S',
datetime2format=r'%Y/%m/%d %H:%M:%S.f'),
datetimeformat=r"%Y/%m/%d %H:%M:%S",
datetime2format=r"%Y/%m/%d %H:%M:%S.f",
),
# Cheetah after v 5.6.4 and default for others such as Pegasus
"def": dict(
datetime1_regex=r"-TimeCreated (?P<date>\S+) (?P<time>\S+)",
datetime2_regex=r"-TimeClosed (?P<date>\S+) (?P<time>\S+)",
filename_regex=r'-OriginalFileName "?(?P<filename>\S+)"?',
datetimeformat="%Y/%m/%d %H:%M:%S"
datetimeformat="%Y/%m/%d %H:%M:%S",
),
}

Expand All @@ -144,7 +145,7 @@ def __init__(self, filename, props_only=False):

# must start with 8 # characters
if not props_only and not txt_header.startswith("########"):
ValueError("Neuralynx files must start with 8 # characters.")
ValueError("Neuralynx files must start with 8 # characters.")

self.read_properties(filename, txt_header)

Expand All @@ -163,7 +164,6 @@ def build_with_properties_only(filename):
"""
res = OrderedDict()


def read_properties(self, filename, txt_header):
"""
Read properties from header and place in OrderedDictionary which this object is.
Expand Down Expand Up @@ -272,8 +272,8 @@ def readTimeDate(self, txt_header):
elif an == "Neuraview":
hpd = NlxHeader.header_pattern_dicts["neuraview2"]
av = Version("2")
elif an == 'Pegasus':
hpd = NlxHeader.header_pattern_dicts['peg']
elif an == "Pegasus":
hpd = NlxHeader.header_pattern_dicts["peg"]
av = Version("2")
else:
an = "Unknown"
Expand All @@ -289,15 +289,16 @@ def readTimeDate(self, txt_header):
dt1 = sr.groupdict()
try: # allow two possible formats for date and time
self["recording_opened"] = datetime.datetime.strptime(
dt1["date"] + " " + dt1["time"], hpd["datetimeformat"])
dt1["date"] + " " + dt1["time"], hpd["datetimeformat"]
)
except:
try:
self["recording_opened"] = datetime.datetime.strptime(
dt1["date"] + " " + dt1["time"], hpd["datetime2format"])
dt1["date"] + " " + dt1["time"], hpd["datetime2format"]
)
except:
self["recording_opened"] = None


# close time, if available
if "datetime2_regex" in hpd:
sr = re.search(hpd["datetime2_regex"], txt_header)
Expand Down
2 changes: 1 addition & 1 deletion neo/test/rawiotest/test_neuralynxrawio.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestNeuralynxRawIO(
"neuralynx/Cheetah_v5.5.1/original_data",
"neuralynx/Cheetah_v5.6.3/original_data",
"neuralynx/Cheetah_v5.7.4/original_data",
"neuralynx/Cheetah_v6.3.2/incomplete_blocks"
"neuralynx/Cheetah_v6.3.2/incomplete_blocks",
]

def test_scan_ncs_files(self):
Expand Down

0 comments on commit 6cd8158

Please sign in to comment.