Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def import_annotations(self, fname):
"""Import annotations from a CSV file."""
descs, onsets, durations = [], [], []
fs = self.current["data"].info["sfreq"]
with open(fname) as f:
f.readline() # skip header
for line in f:
ann = line.split(",")
if len(ann) == 3: # type, onset, duration
onset = float(ann[1].strip())
duration = float(ann[2].strip())
if onset > self.current["data"].n_times / fs:
msg = ("One or more annotations are outside of the "
"data range.")
raise InvalidAnnotationsError(msg)
else:
descs.append(ann[0].strip())
onsets.append(onset)
durations.append(duration)
annotations = mne.Annotations(onsets, durations, descs)
self.current["data"].set_annotations(annotations)
def import_file(self, f, text, ffilter="*"):
"""Import file."""
fname = QFileDialog.getOpenFileName(self, text, filter=ffilter)[0]
if fname:
try:
f(fname)
except LabelsNotFoundError as e:
QMessageBox.critical(self, "Channel labels not found", str(e))
except InvalidAnnotationsError as e:
QMessageBox.critical(self, "Invalid annotations", str(e))