def test_anonymize(self): header = highlevel.make_header(technician='tech', recording_additional='radd', patientname='name', patient_additional='padd', patientcode='42', equipment='eeg', admincode='420', gender='Male', birthdate='05.09.1980') annotations = [[0.01, -1, 'begin'], [0.5, -1, 'middle'], [10, -1, 'end']] header['annotations'] = annotations signal_headers = highlevel.make_signal_headers( ['ch' + str(i) for i in range(3)]) signals = np.random.rand(3, 256 * 300) * 200 #5 minutes of eeg highlevel.write_edf(self.personalized, signals, signal_headers, header) highlevel.anonymize_edf(self.personalized, new_file=self.anonymized, to_remove=[ 'patientname', 'birthdate', 'admincode', 'patientcode', 'technician' ], new_values=['x', '', 'xx', 'xxx', 'xxxx'], verify=True, verbose=True) new_header = highlevel.read_edf_header(self.anonymized) self.assertEqual(new_header['birthdate'], '') self.assertEqual(new_header['patientname'], 'x') self.assertEqual(new_header['admincode'], 'xx') self.assertEqual(new_header['patientcode'], 'xxx') self.assertEqual(new_header['technician'], 'xxxx') highlevel.anonymize_edf(self.personalized, to_remove=[ 'patientname', 'birthdate', 'admincode', 'patientcode', 'technician' ], new_values=['x', '', 'xx', 'xxx', 'xxxx'], verify=True) new_header = highlevel.read_edf_header(self.personalized[:-4] + '_anonymized.edf') self.assertEqual(new_header['birthdate'], '') self.assertEqual(new_header['patientname'], 'x') self.assertEqual(new_header['admincode'], 'xx') self.assertEqual(new_header['patientcode'], 'xxx') self.assertEqual(new_header['technician'], 'xxxx') with self.assertRaises(AssertionError): highlevel.anonymize_edf(self.personalized, new_file=self.anonymized, to_remove=[ 'patientname', 'birthdate', 'admincode', 'patientcode', 'technician' ], new_values=['x', '', 'xx', 'xxx'], verify=True)
def extract_ecg(edf_file, copy_folder): filename = os.path.basename(edf_file) new_edf_file = os.path.join(copy_folder, filename) if os.path.exists(new_edf_file): return try: header = highlevel.read_edf_header(edf_file) except: print(f'error in file {edf_file}') return channels = header['channels'] try: channels.remove('cs_ECG') except: print(f'warning, {edf_file} has no cs_ECG') ch_names = [x for x in channels if 'ECG' in x.upper()] if len(ch_names) > 1: print( f'Warning, these are present: {ch_names}, selecting {ch_names[0]}') ch_name = ch_names[0] signals, shead, header = highlevel.read_edf(edf_file, ch_names=[ch_name], digital=True, verbose=False) shead[0]['label'] = 'ECG' assert len(signals) > 0, 'signal empty' try: highlevel.write_edf(new_edf_file, signals, shead, header, digital=True) except: shead[0]['digital_min'] = signals.min() shead[0]['digital_max'] = signals.max() highlevel.write_edf(new_edf_file, signals, shead, header, digital=True)
def test_read_header(self): header = highlevel.read_edf_header(self.test_generator) self.assertEqual(len(header), 14) self.assertEqual(len(header['channels']), 11) self.assertEqual(len(header['SignalHeaders']), 11) self.assertEqual(header['Duration'], 600) self.assertEqual(header['admincode'], 'Dr. X') self.assertEqual(header['birthdate'], '30 jun 1969') self.assertEqual(header['equipment'], 'test generator') self.assertEqual(header['gender'], 'Male') self.assertEqual(header['patient_additional'], 'patient') self.assertEqual(header['patientcode'], 'abcxyz99') self.assertEqual(header['patientname'], 'Hans Muller') self.assertEqual(header['technician'], 'Mr. Spotty')
def convert_hypnograms(datadir): """ This function is quite a hack to read the edf hypnogram as a byte array. I found no working reader for the hypnogram edfs. """ print('Converting hypnograms') files = [x for x in os.listdir(datadir) if x.endswith('Hypnogram.edf')] for file in tqdm(files): file = os.path.join(datadir, file) hypnogram = [] annot = highlevel.read_edf_header(file)['annotations'] for bstart, blength, bstage in annot: length = int(blength.decode()) stage = bstage.decode() if 'movement' in stage.lower(): stage = 'M' stage = [str(stage[-1])] * (length // 30) hypnogram.extend(stage) csv_file = file.replace('-Hypnogram', '')[:-5] + '0-PSG.csv' with open(csv_file, "w") as f: writer = csv.writer(f, lineterminator='\r') writer.writerows(hypnogram)
help='Number of columns to display in the viewer') parser.add_argument( '-page', type=int, default=0, help='At which page (epoch*gridsize) to start the viewer') args = parser.parse_args() edf_file = args.edf_file nrows = args.nrows ncols = args.ncols page = args.page if edf_file is None: edf_file = misc.choose_file(exts=['edf', 'npy'], title='Choose a EDF to display') print('loading {}'.format(edf_file)) channels = highlevel.read_edf_header(edf_file)['channels'] ch_nr = channels.index([ch for ch in channels if 'ECG' in ch.upper()][0]) data, sheader, header = highlevel.read_edf(edf_file, ch_nrs=ch_nr) data = data[0] fs = sheader[0]['sample_rate'] title = os.path.basename(edf_file) self = ECGPlotter(data=data, fs=fs, startpage=page, nrows=nrows, ncols=ncols, title=title) plt.show(block=True)
return df_ann # To improve source_selection = st.sidebar.selectbox('Chose source seleciton', options=['Physionet', 'La Teppe'], index=0) if source_selection == 'La Teppe': edf_patient_selection = st.sidebar.selectbox('Chose patient', options=edf_files, index=0) edf_file = edf_path + edf_patient_selection headers = highlevel.read_edf_header(edf_file) channels = headers['channels'] limited_channels = [ 'ECG1+ECG1-', 'EMG1+EMG1-', 'EMG6+EMG6-', 'EMG2G2', 'EMG3G2', 'EMG2G2-EMG3G2' ] limited_channels = [ limited_channels[i] for i in range(len(limited_channels)) if limited_channels[i] in channels ] channels_selection = st.sidebar.selectbox('select channel', options=limited_channels, index=0)
e.g. another name for ECG that has not been added to the mappings dictionary that is present in config.py @author: skjerns """ import config as cfg import os import sleep_utils import misc import ospath from tqdm import tqdm from pyedflib.highlevel import read_edf_header ch_mapping = cfg.mapping_channels missing = set() if __name__ == '__main__': files = [] for dataset in cfg.datasets.values(): files.extend(ospath.list_files(dataset, exts='edf', subfolders=True)) ch_mapping = cfg.mapping_channels for file in tqdm(files): channels = read_edf_header(file)['channels'] for ch in channels: if not ch in ch_mapping and not ch in ch_mapping.values(): missing.add(ch) print(ch)