Skip to content

Commit

Permalink
fix rename channels
Browse files Browse the repository at this point in the history
  • Loading branch information
skjerns committed Jun 24, 2022
1 parent fe8fa3c commit 11586dc
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions pyedflib/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def anonymize_edf(edf_file, new_file=None,
write_edf(new_file, signals, signal_headers, header, digital=True)
if verify:
compare_edf(edf_file, new_file, verbose=verbose)
return True
return new_file


def rename_channels(edf_file, mapping, new_file=None, verbose=False):
Expand All @@ -804,28 +804,21 @@ def rename_channels(edf_file, mapping, new_file=None, verbose=False):
True if successful, False if failed.
"""
header = read_edf_header(edf_file)
channels = header['channels']
if new_file is None:
file, ext = os.path.splitext(edf_file)
new_file = file + '_renamed' + ext

signals, signal_headers, header = read_edf(edf_file, digital=True)
channels = [shead['label'] for shead in signal_headers]

signal_headers = []
signals = []
for ch_nr in tqdm(range(len(channels)), disable=not verbose):
signal, signal_header, _ = read_edf(edf_file, digital=True,
ch_nrs=ch_nr, verbose=verbose)
ch = signal_header[0]['label']
if ch in mapping :
if verbose: print('{} to {}'.format(ch, mapping[ch]))
ch = mapping[ch]
signal_header[0]['label']=ch
else:
if verbose: print('no mapping for {}, leave as it is'.format(ch))
signal_headers.append(signal_header[0])
signals.append(signal.squeeze())
for ch in mapping:
assert ch in channels, f'{ch} was not found in channels: {channels}'

return write_edf(new_file, signals, signal_headers, header, digital=True)
for shead in signal_headers:
if shead['label'] in mapping:
shead['label'] = mapping[shead['label']]
write_edf(new_file, signals, signal_headers, header, digital=True)
return new_file


def change_polarity(edf_file, channels, new_file=None, verify=True,
Expand Down Expand Up @@ -869,4 +862,4 @@ def change_polarity(edf_file, channels, new_file=None, verify=True,
write_edf(new_file, signals, signal_headers, header,
digital=True, correct=False, verbose=verbose)
if verify: compare_edf(edf_file, new_file)
return True
return new_file

0 comments on commit 11586dc

Please sign in to comment.