def get_sizes(X, dataset_size): f = pyedflib.EdfReader("database/chb01_{}.edf".format(1)) #f = pyedflib.EdfReader("database/chb01_01.edf") n = f.signals_in_file input_size = n length = f.getNSamples()[0] sigbufs = np.zeros((n, f.getNSamples()[0])) for i in np.arange(n): sigbufs[i, :] = f.readSignal(i) sigbufs_test = sigbufs print('SIZE sigbufs', np.shape(sigbufs_test)) X = np.zeros((dataset_size, 1, 1, input_size, length, 1)) X[0, 0, 0, :, :, 0] = sigbufs_test for j in range(2, dataset_size + 1): if (j == 19 or j == 28 or j == 35 or j == 44 or j == 45): j += 1 #print(("Reading:dataset_01/chb01_{}.edf ...".format(j))) f = pyedflib.EdfReader("database/chb01_{}.edf".format(j)) for i in np.arange(n): sigbufs[i, :] = f.readSignal(i) sigbufs_test = sigbufs X[j - 1, 0, 0, :, :, 0] = sigbufs_test #max_value = np.amax(abs(X)) #X = X/max_value print(length) return X, input_size, length
def test_EdfReader_Close_file(self): try: f = pyedflib.EdfReader(self.edf_data_file) except IOError: print('cannot open', self.edf_data_file) np.testing.assert_raises(IOError, 'cannot open file') return np.testing.assert_equal(f.getSignalLabels()[0], 'squarewave') f._close() f._close() del f try: f = pyedflib.EdfReader(self.edf_data_file) except IOError: print('cannot open', self.edf_data_file) np.testing.assert_raises(IOError, 'cannot open file') return np.testing.assert_equal(f.getSignalLabels()[0], 'squarewave') del f try: f = pyedflib.EdfReader(self.edf_data_file) except IOError: print('cannot open', self.edf_data_file) np.testing.assert_raises(IOError, 'cannot open file') return np.testing.assert_equal(f.getSignalLabels()[0], 'squarewave') del f
def test_SampleWritingContextManager(self): channel_info1 = {'label':'test_label1', 'dimension':'mV', 'sample_rate':100, 'physical_max':1.0,'physical_min':-1.0, 'digital_max':8388607,'digital_min':-8388608, 'prefilter':'pre1','transducer':'trans1'} channel_info2 = {'label':'test_label2', 'dimension':'mV', 'sample_rate':100, 'physical_max':1.0,'physical_min':-1.0, 'digital_max':8388607,'digital_min':-8388608, 'prefilter':'pre2','transducer':'trans2'} with pyedflib.EdfWriter(self.bdfplus_data_file, 2, file_type=pyedflib.FILETYPE_BDFPLUS) as f: f.setSignalHeader(0,channel_info1) f.setSignalHeader(1,channel_info2) data1 = np.ones(500) * 0.1 data2 = np.ones(500) * 0.2 data_list = [] data_list.append(data1) data_list.append(data2) f.writeSamples(data_list) with pyedflib.EdfReader(self.bdfplus_data_file) as f: data1_read = f.readSignal(0) data2_read = f.readSignal(1) with pyedflib.EdfReader(self.bdfplus_data_file) as f: data1_read = f.readSignal(0) data2_read = f.readSignal(1) self.assertEqual(f.filetype, pyedflib.FILETYPE_BDFPLUS) np.testing.assert_equal(len(data1), len(data1_read)) np.testing.assert_equal(len(data2), len(data2_read)) np.testing.assert_almost_equal(data1, data1_read) np.testing.assert_almost_equal(data2, data2_read)
def test_EdfReader_ReadAnnotations(self): try: f = pyedflib.EdfReader(self.edf_data_file, pyedflib.DO_NOT_READ_ANNOTATIONS) except IOError: print('cannot open', self.edf_data_file) return ann_index, ann_duration, ann_text = f.readAnnotations() np.testing.assert_equal(ann_index.size, 0) del f try: f = pyedflib.EdfReader(self.edf_data_file, pyedflib.READ_ALL_ANNOTATIONS) except IOError: print('cannot open', self.edf_data_file) return ann_index, ann_duration, ann_text = f.readAnnotations() np.testing.assert_almost_equal(ann_index[0], 0) np.testing.assert_almost_equal(ann_index[1], 600) np.testing.assert_equal(f.signals_in_file, 11) np.testing.assert_equal(f.datarecords_in_file, 600) for i in np.arange(11): np.testing.assert_almost_equal(f.getSampleFrequencies()[i], 200) np.testing.assert_equal(f.getNSamples()[i], 120000) del f
def _load_neuracle(data_dir): """ neuracle file loader :param data_dir: root data dir for the experiment :return: data: ndarray, (channels, timesteps) ch_name: list, name of channels timestamp: list, index of trigger """ f = { 'data': os.path.join(data_dir, 'data.bdf'), 'evt': os.path.join(data_dir, 'evt.bdf') } # read data f_data = pyedflib.EdfReader(f['data']) ch_names = f_data.getSignalLabels() data = np.array([f_data.readSignal(i) for i in range(f_data.signals_in_file)]) # sample frequiencies sfreq = f_data.getSampleFrequencies() assert np.unique(sfreq).size == 1 if cfg.amp_info.samplerate != sfreq[0]: warnings.warn('Samplerate in config file does not equal to data file record') cfg.amp_info.samplerate = int(sfreq[0]) # read event f_evt = pyedflib.EdfReader(f['evt']) event, _, _ = f_evt.readAnnotations() event = list(map(lambda x: int(x * cfg.amp_info.samplerate), event)) return data, ch_names, event
def _add_start_timestamp(data_dir, fin, ajileFormat, patient_code, day): if ajileFormat == True: f = pyedflib.EdfReader(data_dir + '/purged_' + patient_code + '_day_' + str(day) + '.edf') else: f = pyedflib.EdfReader(data_dir + '/' + patient_code + '_' + str(day) + '.edf') startDateTime = f.getStartdatetime() tStamp = (startDateTime - dt.utcfromtimestamp(0) ).total_seconds() #convert back with dt.utcfromtimestamp() f._close() del f gc.collect() #Remove time vector (unnecessary) e = "/time_sec" in fin if e: del fin['time_sec'] #Don't need time vector (can create it later) #Save timestamp e = "/start_timestamp" in fin if e: del fin['start_timestamp'] fin.create_dataset("start_timestamp", data=tStamp) return tStamp
def test_read_incorrect_file(self): # this should simply not be found and not trigger the UTF8 warning with self.assertRaises(FileNotFoundError): f = pyedflib.EdfReader('does_not_exist') # this file is corrupted and should be found, but not be read # so the Exception should be OSError and not FileNotFoundError with self.assertRaises(OSError): with open(self.tmp_edf_file, 'wb') as f: f.write(b'0123456789') f = pyedflib.EdfReader(self.tmp_edf_file) # now we create a file that is not EDF/BDF compliant # this should raise an OSError and not a FileNotFoundError with self.assertRaises(OSError) as cm: channel_info = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 256, 'sample_frequency': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 8388607, 'digital_min': -8388608, 'prefilter': 'pre1', 'transducer': 'trans1'} with pyedflib.EdfWriter(self.bdf_accented_file, 1, file_type=pyedflib.FILETYPE_BDFPLUS) as f: f.setSignalHeader(0,channel_info) f.setTechnician('tec1') data = np.ones(100) * 0.1 f.writePhysicalSamples(data) with open(self.bdf_accented_file, 'rb') as f: content = bytearray(f.read()) content[181] = 58 with open(self.bdf_accented_file, 'wb') as f: f.write(content) f = pyedflib.EdfReader(self.bdf_accented_file) # However, the error before should not be a FileNotFoundError assert not isinstance(cm.exception, FileNotFoundError)
def get_windows(conf): input_path=conf['input_path'] #input path to csv or edf files output_path=conf['output_path'] #top of directory for processed data sigfiles=conf['input_files'] #list of csv/edf files hypfiles=conf['hyp_files'] #list of txt files containing sleep stage annotations win_dur=conf['win_dur'] #duration of extracted windows win_ovlp=conf['win_ovlp'] chan_list=[['Fp1',re.compile('EEG Fp1-CLE')], ['Fp2',re.compile('EEG Fp2-CLE')], ['F7',re.compile('EEG F7-CLE')], ['F3',re.compile('EEG F3-CLE')], ['Fpz',re.compile('EEG Fpz-CLE')], ['F4',re.compile('EEG F4-CLE')], ['F8',re.compile('EEG F8-CLE')], ['T3',re.compile('EEG T3-CLE')], ['C3',re.compile('EEG C3-CLE')], ['Cz',re.compile('EEG Cz-CLE')], ['C4',re.compile('EEG C4-CLE')], ['T4',re.compile('EEG T4-CLE')], ['T5',re.compile('EEG T5-CLE')], ['P3',re.compile('EEG P3-CLE')], ['Pz',re.compile('EEG *[Pp]z-CLE')], ['P4',re.compile('EEG P4-CLE')], ['T6',re.compile('EEG T6-CLE')], ['O1',re.compile('EEG O1-CLE')], ['O2',re.compile('EEG O2-CLE')], ['A2',re.compile('EEG A.-CLE')]] if not os.path.exists(output_path): os.makedirs(output_path) for i in range(len(sigfiles)): print('On example #'+str(i+1)) inFile = pyedflib.EdfReader(input_path+sigfiles[i]) ch_idx = np.zeros(len(chan_list)-1,dtype=int) all_chan=inFile.getSignalLabels() for j in range(len(chan_list)-1): ch_idx[j] = [x for x in range(len(all_chan)) if chan_list[j][1].match(all_chan[x])][0] comp_idx = [x for x in range(len(all_chan)) if chan_list[-1][1].match(all_chan[x])][0] ch_names=[chan_list[x][0]+'-'+chan_list[-1][0] for x in range(len(chan_list)-1)] base_fname = sigfiles[i].split('.')[0] outFile = open(output_path+base_fname+'_windows.csv','w') fs = int(inFile.getSignalHeader(ch_idx[0])['sample_rate']) sig_len = int(inFile.getFileDuration()*fs) pts_in_win = int(win_dur * fs) win_step = int((1-win_ovlp)*win_dur*fs) hf = pyedflib.EdfReader(input_path+str(hypfiles[i])) annot = hf.readAnnotations() count = 0 for start in range(0,sig_len-pts_in_win,win_step): try: stg = str(annot[2][np.where(annot[0]*fs > start)[0][0]]) count+=1 outFile.write('Window#'+str(count)+','+stg+'\n') print_segment(inFile,ch_idx,comp_idx,ch_names,start,pts_in_win,outFile) except: break outFile.close() return output_path
def importEdf(path): f = None try: f = pyedflib.EdfReader(path) except: pyedflib.EdfReader(path).close() f = pyedflib.EdfReader(path) return f
def __init__(self, path, annotations_path): self.file = pyedflib.EdfReader(path) self.number_of_signal = self.file.signals_in_file self.signals_list = [] for i in np.arange(self.number_of_signal): self.signals_list.append( \ Signal(self.file.getLabel(i), self.file.readSignal(i), \ self.file.getSampleFrequency(i))) self.annotations_file = pyedflib.EdfReader(annotations_path) self.annotations = self.annotations_file.readAnnotations()
def test_TestRoundingEDF(self): channel_info1 = { 'label': 'test_label1', 'dimension': 'mV', 'sample_rate': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 32767, 'digital_min': -32768, 'prefilter': 'pre1', 'transducer': 'trans1' } f = pyedflib.EdfWriter(self.edfplus_data_file, 1, file_type=pyedflib.FILETYPE_EDFPLUS) f.setSignalHeader(0, channel_info1) time = np.linspace(0, 5, 500) data1 = np.sin(2 * np.pi * 1 * time) data_list = [] data_list.append(data1) f.writeSamples(data_list) f.close() del f f = pyedflib.EdfReader(self.edfplus_data_file) data1_read = f.readSignal(0) f._close() del f np.testing.assert_equal(len(data1), len(data1_read)) np.testing.assert_almost_equal(data1, data1_read, decimal=4) f = pyedflib.EdfWriter(self.edfplus_data_file, 1, file_type=pyedflib.FILETYPE_EDFPLUS) f.setSignalHeader(0, channel_info1) data_list = [] data_list.append(data1_read) f.writeSamples(data_list) f.close() del f f = pyedflib.EdfReader(self.edfplus_data_file) data2_read = f.readSignal(0) f._close() del f np.testing.assert_equal(len(data1), len(data2_read)) np.testing.assert_almost_equal(data1, data2_read, decimal=4) np.testing.assert_almost_equal(data1_read, data2_read, decimal=10)
def import_files(file, file_type="accel", temp_sample_int=1): if file is not None and not os.path.exists(file): print("\nFile {} does not exists.".format(file)) return None, None if file is None: return None, None if file is not None and os.path.exists(file): print("\nImporting {}...".format(file)) if file_type == "accel": acc_file = pyedflib.EdfReader(file) h = acc_file.getSignalHeader(0) s = acc_file.getStartdatetime() fs = h["sample_rate"] df = pd.DataFrame([ acc_file.readSignal(0), acc_file.readSignal(1), acc_file.readSignal(2) ]).transpose() df.columns = ["x", "y", "z"] df.insert(loc=0, column="Timestamp", value=pd.date_range(start=s, periods=df.shape[0], freq="{}ms".format(1000 / fs))) if file_type == "temperature": temp_file = pyedflib.EdfReader(file) h = temp_file.getSignalHeader(0) s = temp_file.getStartdatetime() fs = h["sample_rate"] df = pd.DataFrame( temp_file.readSignal(0)[::int(temp_sample_int * fs)], columns=["Temperature"]) df.insert(loc=0, column="Timestamp", value=pd.date_range( start=s, freq="{}S".format(temp_sample_int), periods=df.shape[0])) fs = 1 / temp_sample_int print("Complete.") return df, fs
def test_EdfReader_Close_file(self): f = pyedflib.EdfReader(self.edf_data_file) np.testing.assert_equal(f.getSignalLabels()[0], 'squarewave') # Don't close the file but try to reopen it and verify that it fails. with np.testing.assert_raises(IOError): ff = pyedflib.EdfReader(self.edf_data_file) # Now close and verify it can be re-opened/read. f.close() ff = pyedflib.EdfReader(self.edf_data_file) np.testing.assert_equal(ff.getSignalLabels()[0], 'squarewave') del f, ff
def _add_signal_labels(data_dir, ajileFormat, fin, patient_code, day): if ajileFormat == True: f = pyedflib.EdfReader(data_dir + '/purged_' + patient_code + '_day_' + str(day) + '.edf') else: f = pyedflib.EdfReader(data_dir + '/' + patient_code + '_' + str(day) + '.edf') signal_labels = f.getSignalLabels() #Add channel labels to saved data e = "/chanLabels" in fin if not e: dset5 = fin.create_dataset("chanLabels", data=str(signal_labels)) return signal_labels
def load_data_from_NEDC(frame, epilepsy_catalog_path, no_epilepsy_catalog_path, channels): e_path = epilepsy_catalog_path ne_path = no_epilepsy_catalog_path paths = [(e_path, 1), (ne_path, 0)] data = [] labels = [] for path_f in paths: files = [f for f in listdir(path_f[0]) if isfile(join(path_f[0], f))] for filename in files: input_file = pyedflib.EdfReader(path_f[0] + '/' + filename) n = input_file.signals_in_file n = n - 3 sigbufs = np.zeros((len(channels), input_file.getNSamples()[0])) idx = 0 for i in np.arange(n): if np.array(channels).__contains__(i): sigbufs[idx, :] = input_file.readSignal(i) idx = idx + 1 f_data, f_labels = Divide_one_participant_signal_on_frames( sigbufs, path_f[1], frame) for d in f_data: #data.append(f_data[:][:][:][0]) data.append(d) for f in f_labels: #labels.append(f_labels[:][0]) labels.append(f) return data, labels
def import_files(self): print("\nImporting data...") if self.la_file is not None: print("-Left ankle") self.df_la = pd.read_csv(self.la_file, skiprows=100, usecols=[0, 1, 2, 3, 6]) self.df_la.columns = ["Timestamp", "x", "y", "z", "temperature"] self.df_la["Timestamp"] = pd.to_datetime(self.df_la["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f") if self.ra_file is not None: print("-Right ankle") self.df_ra = pd.read_csv(self.ra_file, skiprows=100, usecols=[0, 1, 2, 3, 6]) self.df_ra.columns = ["Timestamp", "x", "y", "z", "temperature"] self.df_ra["Timestamp"] = pd.to_datetime(self.df_ra["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f") if self.lw_file is not None: print("-Left wrist") self.df_lw = pd.read_csv(self.lw_file, skiprows=100, usecols=[0, 1, 2, 3, 6]) self.df_lw.columns = ["Timestamp", "x", "y", "z", "temperature"] self.df_lw["Timestamp"] = pd.to_datetime(self.df_lw["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f") if self.rw_d_file is not None: print("-Right wrist #1") self.df_rw_d = pd.read_csv(self.rw_d_file, skiprows=100, usecols=[0, 1, 2, 3, 6]) self.df_rw_d.columns = ["Timestamp", "x", "y", "z", "temperature"] self.df_rw_d["Timestamp"] = pd.to_datetime(self.df_rw_d["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f") if self.rw_p_file is not None: print("-Right wrist #2") self.df_rw_p = pd.read_csv(self.rw_p_file, skiprows=100, usecols=[0, 1, 2, 3, 6]) self.df_rw_p.columns = ["Timestamp", "x", "y", "z", "temperature"] self.df_rw_p["Timestamp"] = pd.to_datetime(self.df_rw_p["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f") if self.bf_file is not None: print("-Bittium Faros") import pyedflib file = pyedflib.EdfReader(self.bf_file) x = file.readSignal(chn=0, start=0) y = file.readSignal(chn=1, start=0) z = file.readSignal(chn=2, start=0) self.bf_fs = file.getSampleFrequencies()[1] starttime = file.getStartdatetime() timestamps = pd.date_range(start=starttime, end=starttime + timedelta(seconds=len(x)/self.bf_fs), freq="{}ms".format(1000/self.bf_fs)) self.df_bf = pd.DataFrame(list(zip(timestamps, x, y, z)), columns=["Timestamp", "x", "y", "z"]) file.close() if self.event_log is not None: print("-Event log") self.df_event = pd.read_excel(self.event_log) self.df_event["Start"] = pd.to_datetime(self.df_event["Start"]) self.df_event["Stop"] = pd.to_datetime(self.df_event["Stop"]) print("Complete.")
def extract_metadata(file_path:str, test_group:str): """Extract metadata from an edf file. Output path: ROOT/meta_data/test_group/*.csv """ f = pyedflib.EdfReader(file_path) # open the edf file participant_id = re.search( '[A-Z]\d+-\d-\d', file_path.split('/')[-1]).group() signals_in_file = f.getSignalLabels() signal_duration = f.file_duration # in seconds headers = f.getHeader() start_dt = headers.pop('startdate', None) signal_info = f.getSignalHeaders() all_info = {'participant_id': participant_id, 'participant_group': test_group, 'start_datetime': str(start_dt), 'signals_in_file': {'number_of_signals': len(signals_in_file), 'signal_labels': signals_in_file, 'signal_duration': signal_duration}, 'headers': headers, 'signal_info': signal_info} output_fname = file_path.split('/')[-1].replace('.edf', '.json') output_dir = os.path.join(ROOT+'_meta_data/', test_group) if not os.path.isdir(output_dir): os.makedirs(output_dir) with open(os.path.join(output_dir, output_fname), 'w') as fn: json.dump(all_info, fn)
def add_view_from_file(self, file: Path, panel_index: int = None): 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) else: try: track_obj = tracking.Track.read(file) except Exception as e: logging.exception(e) else: self.add_view(track_obj, panel_index=panel_index)
def test_EdfReader_signalInfos(self): try: f = pyedflib.EdfReader(self.edf_data_file) except IOError: print('cannot open', self.edf_data_file) return np.testing.assert_equal(f.getSignalLabels()[0], 'squarewave') np.testing.assert_equal(f.getLabel(0), 'squarewave') np.testing.assert_equal(f.getPrefilter(0), 'pre1') np.testing.assert_equal(f.getTransducer(0), 'trans1') for i in np.arange(1, 11): np.testing.assert_equal(f.getPhysicalDimension(i), 'uV') np.testing.assert_equal(f.getSampleFrequency(i), 200) np.testing.assert_equal(f.getSampleFrequencies()[i], 200) np.testing.assert_equal(f.getSignalLabels()[1], 'ramp') np.testing.assert_equal(f.getSignalLabels()[2], 'pulse') np.testing.assert_equal(f.getSignalLabels()[3], 'noise') np.testing.assert_equal(f.getSignalLabels()[4], 'sine 1 Hz') np.testing.assert_equal(f.getSignalLabels()[5], 'sine 8 Hz') np.testing.assert_equal(f.getSignalLabels()[6], 'sine 8.1777 Hz') np.testing.assert_equal(f.getSignalLabels()[7], 'sine 8.5 Hz') np.testing.assert_equal(f.getSignalLabels()[8], 'sine 15 Hz') np.testing.assert_equal(f.getSignalLabels()[9], 'sine 17 Hz') np.testing.assert_equal(f.getSignalLabels()[10], 'sine 50 Hz') # testing file info and file_info_log f.file_info() f.file_info_long() f.close()
def test_BdfReader_Read_accented_file(self): channel_info = { 'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 8388607, 'digital_min': -8388608, 'prefilter': 'pre1', 'transducer': 'trans1' } f = pyedflib.EdfWriter(self.bdf_accented_file, 1, file_type=pyedflib.FILETYPE_BDFPLUS) f.setSignalHeader(0, channel_info) f.setTechnician('tec1') data = np.ones(100) * 0.1 f.writePhysicalSamples(data) f.writePhysicalSamples(data) del f f = pyedflib.EdfReader(self.bdf_accented_file, pyedflib.READ_ALL_ANNOTATIONS, pyedflib.DO_NOT_CHECK_FILE_SIZE) np.testing.assert_equal(f.getTechnician(), 'tec1') np.testing.assert_equal(f.getLabel(0), 'test_label') np.testing.assert_equal(f.getPhysicalDimension(0), 'mV') np.testing.assert_equal(f.getPrefilter(0), 'pre1') np.testing.assert_equal(f.getTransducer(0), 'trans1') np.testing.assert_equal(f.getSampleFrequency(0), 100) np.testing.assert_equal(f.getSignalHeader(0), channel_info) np.testing.assert_equal(f.getSignalHeaders(), [channel_info]) del f
def getData(self): fileObj = QFileDialog.getOpenFileName( self, "Choose the file", dir=".", filter="Data files (*.mat *.edf)") if fileObj[0] == '': return filePath = fileObj[0] fileType = filePath.split('.')[-1] chNames = None if fileType == 'mat': fileData = scipy.io.loadmat(filePath) data = [ fileData[key] for key in fileData if type(fileData[key]) == np.ndarray ][0] if np.shape(data)[0] > np.shape(data)[1]: data = data.transpose() elif fileType == 'edf': f = pyedflib.EdfReader(filePath) n = f.signals_in_file chNames = f.getSignalLabels() data = np.zeros((n, f.getNSamples()[0])) for i in np.arange(n): try: data[i, :] = f.readSignal(i) except Exception as e: print(chNames[i], ' skipped', e) print(np.shape(data)) return filePath, data, chNames
def loadFile(self): f = pyedflib.EdfReader(self.filePath) self.numChannels = f.signals_in_file print ("number of signals in file = ", self.numChannels) self.signal_labels = f.getSignalLabels() print ("signal labels = ", self.signal_labels) # numSamples = 3600 * 256 = 921,600 self.numSamples = f.getNSamples()[0] # sampleFrequency = 256 self.sampleFrequency = f.getSampleFrequency(0) self.sigbufs = np.zeros((self.numChannels, self.numSamples)) for i in np.arange(self.numChannels): self.sigbufs[i, :] = f.readSignal(i) # sigbufs above is a 23 x 921600 matrix # transpose it so that it becomes 921600 x 23 matrix self.sigbufs = self.sigbufs.transpose() # cleanup the sigbufs and signal_labels so that duplicate signals # are eliminated. columnsToDel = [] labelsRead = [] for i in range(len(self.signal_labels)): if (re.search('\w+\d+-\w+\d+', self.signal_labels[i]) == None): columnsToDel.append(i) if (self.signal_labels[i] in labelsRead): if (i not in columnsToDel): columnsToDel.append[i] # Now remove the columns from sig_labels and sigbufs self.sigbufs = np.delete(self.sigbufs, columnsToDel, axis=1) self.signal_labels = np.delete(self.signal_labels, columnsToDel, axis=0) print ("new signal labels = ", self.signal_labels)
def __load_edf_data(path): edf = None try: edf = pyedflib.EdfReader(path) except OSError: print('psg load err') return edf
def test_generator(): """ Get an sample EDF-file Parameters ---------- None Returns ------- 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 = get_generator_filename() f = pyedflib.EdfReader(fname) return f
def __init__(self, edf_path_dir, edf_file_name, channels=None): self.edf_path_dir = edf_path_dir self.edf_file_name = edf_file_name groups_edf_file = re.match("S(\\d+)R(\\d+).edf", edf_file_name).groups() self.subject = groups_edf_file[0] self.run_execution = groups_edf_file[1] self.__file = pyedflib.EdfReader( os.path.join(edf_path_dir, edf_file_name)) annotations = self.__file.readAnnotations() self.__onset_events = annotations[0] self.__duration_events = annotations[1] self.__events = annotations[2] self.frequency = self.__file.getSampleFrequencies()[0] self.n_samples = int( np.round(np.sum(self.__duration_events), decimals=2) * self.frequency) self.n_channels = self.__file.signals_in_file if channels is None else len( channels) self.channels = np.arange( self.n_channels) if channels is None else channels self.channels_labels = np.array( self.__file.getSignalLabels())[self.channels] self.data, self.labels = self.__read()
def loadEDF(self): if not self.edf: try: self.edf = pyedflib.EdfReader(self.edf_filename) except OSError as osErr: print("OSError:", "Loading", self.edf_filename) raise osErr for ch in self.channels: # ['C3','C4','O1','O2','EOG-L','EOG-R','EMG','A1','A2'] self.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': self.myprint('mv') self.loaded_channels[ch] *= 1e3 elif self.edf.getPhysicalDimension(self.channels_used[ch]).lower() == 'v': self.myprint('v') self.loaded_channels[ch] *= 1e6 fs = int(self.edf.samplefrequency(self.channels_used[ch])) # fs = Decimal(fs).quantize(Decimal('.0001'), rounding=ROUND_DOWN) print('fs', fs) self.resampling(ch, fs) print('Resampling done') # Trim excess self.trim(ch) else: print('channel[', ch, '] was empty (skipped)', sep='') del self.channels_used[ch]
def load_edf_file(filename, channels_to_load, cohort, channel_alias, merged): f = pyedflib.EdfReader(filename) labels = f.getSignalLabels() contained = {channel_alias[e]: i for (i, e) in enumerate(labels) if e in channel_alias} if not contained or len(contained) != len(channels_to_load): print(labels) print(contained) return -1 fss = f.getSampleFrequencies() if cohort == 'SHHS': if merged == True: fs = fss[contained['ecg']] n = f.getNSamples()[contained['ecg']] X = np.zeros((len(channels_to_load), n)) #lowcut = .3 #highcut = 40. for chan_name, chan_idx_in_file in contained.items(): # g = f.getPhysicalMaximum(chan_idx_in_file) / f.getDigitalMaximum(chan_idx_in_file) # x = g*f.readSignal(chan_idx_in_file) x = f.readSignal(chan_idx_in_file) if fss[chan_idx_in_file] != fs: time = np.arange(0, len(x) / fss[chan_idx_in_file], 1 / fs) t = np.arange(0, len(x) / fss[chan_idx_in_file], 1 / fss[chan_idx_in_file]) F = interp1d(t, x, kind='linear', fill_value = 'extrapolate') x = F(time) X[channels_to_load[chan_name],:] = x data = {'x': X, 'fs': fs, 'labels': labels} return data
def loadHeader(self): if not self.edf: print(self.edf_filename) self.edf = pyedflib.EdfReader(self.edf_filename) signal_labels = self.edf.getSignalLabels() return signal_labels
def __init__(self, buffer_size=500): # read data from local file path = 'sample_short.edf' edf_file = pyedflib.EdfReader(path) samples = edf_file.getNSamples() # For the sample data, these are the channel labels # 8. EOGl:A2, 9. EOGr:A1, 0. C3:A2, 1. C4:A1, 6. EMG # sampling rate of the data is 256 Hz self.total_samples = samples[0] self.data = np.empty([5, self.total_samples]) self.sampling_rate = edf_file.getSampleFrequency(0) # C3-A2 self.data[0, :] = np.asarray(edf_file.readSignal(0)) # C4-A1 self.data[1, :] = np.asarray(edf_file.readSignal(1)) # EOG-L-A2 self.data[2, :] = np.asarray(edf_file.readSignal(8)) # EOG-R-A1 self.data[3, :] = np.asarray(edf_file.readSignal(9)) # chin-EMG self.data[4, :] = np.asarray(edf_file.readSignal(6)) # No of samples to read each time self.buffer_size = buffer_size self.buffer_samples = int(buffer_size * 256.0 / 1000) self.current_location = 0 self.last_call_time = None edf_file._close()
def read_edf_header(edf_file): """ Reads the header and signal headers of an EDF file and it's annotations Parameters ---------- edf_file : str EDF/BDF file to read. Returns ------- summary : dict header of the edf file as dictionary. """ assert os.path.isfile(edf_file), 'file {} does not exist'.format(edf_file) with pyedflib.EdfReader(edf_file) as f: annotations = f.read_annotation() annotations = [[t // 10000000, d if d else -1, x] for t, d, x in annotations] summary = f.getHeader() summary['Duration'] = f.getFileDuration() summary['SignalHeaders'] = f.getSignalHeaders() summary['channels'] = f.getSignalLabels() summary['annotations'] = annotations del f return summary