Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def loadEDF(self):
if not self.edf:
try:
self.edf = pyedflib.EdfReader(self.edf_pathname)
except OSError as osErr:
print("OSError:", "Loading", self.edf_pathname)
raise (osErr)
for ch in self.channels: # ['C3','C4','O1','O2','EOG-L','EOG-R','EMG','A1','A2']
myprint('Loading', ch)
if isinstance(self.channels_used[ch], int):
self.loaded_channels[ch] = self.edf.readSignal(self.channels_used[ch])
if self.edf.getPhysicalDimension(self.channels_used[ch]).lower() == 'mv':
myprint('mv')
self.loaded_channels[ch] *= 1e3
elif self.edf.getPhysicalDimension(self.channels_used[ch]).lower() == 'v':
myprint('v')
self.loaded_channels[ch] *= 1e6
def read_pyedf(edf_file, ch_nrs=None, ch_names=None, digital=False, verbose=True):
"""
Reads an EDF file using pyEDFlib and returns the data, header and signalheaders
:param edf_file: link to an edf file
:param ch_nrs: The numbers of channels to read
:param ch_names: The names of channels to read
:returns: signals, signal_headers, header
"""
assert (ch_nrs is None) or (ch_names is None), 'names xor numbers should be supplied'
if ch_nrs is not None and not isinstance(ch_nrs, list): ch_nrs = [ch_nrs]
if ch_names is not None and not isinstance(ch_names, list): ch_names = [ch_names]
with pyedflib.EdfReader(edf_file) as f:
# see which channels we want to load
channels = [ch.upper() for ch in f.getSignalLabels()]
if ch_names is not None:
ch_nrs = []
for ch in ch_names:
if not ch.upper() in channels:
warnings.warn('{} is not in source file (contains {})'.format(ch.upper(), channels))
print('will be ignored.')
else:
ch_nrs.append(channels.index(ch.upper()))
if ch_nrs is None: # no numbers means we load all
n = f.signals_in_file
ch_nrs = np.arange(n)
# load headers, signal information and
header = f.getHeader()
signal_headers = [f.getSignalHeaders()[c] for c in ch_nrs]
def read_edfrecord(filename, channel, shhs='1'):
# SHHS1 is at 125 Hz, SHHS2 at 128Hz
assert channel=='EEG' or channel=='EEG(sec)', "Channel must be EEG or EEG(sec)"
f = pyedflib.EdfReader(filename)
print("Startdatetime: ", f.getStartdatetime())
signal_labels = f.getSignalLabels()
assert channel in signal_labels
idx_chan = [i for i, x in enumerate(signal_labels) if x==channel][0]
sigbuf = np.zeros((f.getNSamples()[idx_chan]))
sigbuf[:] = f.readSignal(idx_chan)
samplingrate = len(sigbuf) / f.file_duration
print("sampling rate: ", samplingrate)
if shhs=='1':
print("30s * 125Hz divides signal length ?: ", len(sigbuf)%(30*125)==0)
assert samplingrate == 125., "Sampling rate is not 125Hz on this record"
else:
print("30s * 128Hz divides signal length ?: ", len(sigbuf)%(30*128)==0)
assert samplingrate == 128., "Sampling rate is not 128Hz on this record"
return sigbuf
def loadHeader(self):
if not self.edf:
print(self.edf_pathname)
self.edf = pyedflib.EdfReader(self.edf_pathname)
signal_labels = self.edf.getSignalLabels()
return signal_labels
-------
f : EdfReader object
object containing the handle to the file
Examples
--------
>>> import pyedflib.data
>>> f = pyedflib.data.test_generator()
>>> f.signals_in_file == 11
True
>>> f._close()
>>> del f
"""
fname = os.path.join(os.path.dirname(__file__), 'test_generator.edf')
f = pyedflib.EdfReader(fname)
return f
def add_view_from_file(self, file: Path, panel_index: int=None):
# TODO: both edf and xdf should be moved to tracking!!!
if file.suffix == '.edf':
import pyedflib
with pyedflib.EdfReader(str(file)) as f:
labels = f.getSignalLabels()
for label in labels:
index = labels.index(label)
wav = tracking.Wave(f.readSignal(index), f.getSampleFrequency(index))
wav.label = label
wav.path = file.with_name(file.stem + '-' + label + '.wav')
wav.min = f.getPhysicalMinimum(index)
wav.max = f.getPhysicalMaximum(index)
wav.unit = f.getPhysicalDimension(index)
self.add_view(wav, panel_index=panel_index, y_min=wav.min, y_max=wav.max)
elif file.suffix == '.xdf':
import openxdf
xdf = openxdf.OpenXDF(file)
signals = openxdf.Signal(xdf, file.with_suffix('.nkamp'))
# TODO: automate this, why are the xdf.header names different from signals.list_channels?
for label in ['ECG', 'Chin']: