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)
""" Created on Thu Feb 27 14:40:36 2020 add @author: skjerns """ import config import os from tqdm import tqdm import ospath from pyedflib import highlevel folder = "Z:/NT1-HRV-data" new_folder = ospath.join(folder, "new") os.makedirs(new_folder, exist_ok=True) mapping = config.mapping_channels files = ospath.list_files(folder, exts='edf') for file in tqdm(files): name = ospath.basename(file)[:-4] new_file = ospath.join(new_folder, name + ".edf") if os.path.exists(new_file): print(f"{new_file} exists, skipping") continue highlevel.anonymize_edf(file, new_file, to_remove = ['patientcode', 'patientname'], new_values = [name, name], verify=False) highlevel.rename_channels(new_file, mapping=mapping, new_file=new_file)