Skip to content

Commit

Permalink
Merge pull request #180 from holgern/make_assertions_more_lenient
Browse files Browse the repository at this point in the history
change assertion to warning
  • Loading branch information
skjerns authored Jun 24, 2022
2 parents 902e956 + f0f45b5 commit 40a5aa6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 9 additions & 6 deletions pyedflib/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ def write_edf(edf_file, signals, signal_headers, header=None, digital=False,
dmin, dmax = shead['digital_min'], shead['digital_max']
pmin, pmax = shead['physical_min'], shead['physical_max']
label = shead['label']
# physical resolution, acceptable rounding error if within this range
sig_res = abs((dmax-dmin)/(pmax-pmin))

if digital: # exception as it will lead to clipping
assert dmin<=sig.min(), \
'digital_min is {}, but signal_min is {}' \
Expand All @@ -489,12 +492,12 @@ def write_edf(edf_file, signals, signal_headers, header=None, digital=False,
assert pmin != pmax, \
'physical_min {} should be different from physical_max {}'.format(pmin,pmax)
else: # only warning, as this will not lead to clipping
assert pmin<=sig.min(), \
'phys_min is {}, but signal_min is {} ' \
'for channel {}'.format(pmin, sig.min(), label)
assert pmax>=sig.max(), \
'phys_max is {}, but signal_max is {} ' \
'for channel {}'.format(pmax, sig.max(), label)
if pmin>=(sig.min()+sig_res):
warnings.warn('phys_min is {}, but signal_min is {} ' \
'for channel {}'.format(pmin, sig.min(), label))
if pmax<=(sig.max()-sig_res):
warnings.warn('phys_max is {}, but signal_max is {} ' \
'for channel {}'.format(pmax, sig.max(), label))


frequency_key = 'sample_rate' if shead.get('sample_frequency') is None else 'sample_frequency'
Expand Down
22 changes: 20 additions & 2 deletions pyedflib/tests/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_read_write_diff_sfreq(self):
np.testing.assert_allclose(s1, s2)

def test_assertion_dmindmax(self):

import warnings
# test digital and dmin wrong
signals =[np.random.randint(-2048, 2048, 256*60).astype(np.int32)]
sheaders = [highlevel.make_signal_header('ch1', sample_frequency=256)]
Expand All @@ -196,9 +196,27 @@ def test_assertion_dmindmax(self):
sheaders = [highlevel.make_signal_header('ch1', sample_frequency=256)]
sheaders[0]['physical_min'] = -200
sheaders[0]['physical_max'] = 200
with self.assertRaises(AssertionError):
with self.assertWarns(UserWarning):
highlevel.write_edf(self.edfplus_data_file, signals, sheaders, digital=False)

signals = [np.random.randint(-2048, 2048, 256*60)]
sheaders = [highlevel.make_signal_header('ch1', sample_frequency=256)]
sheaders[0]['physical_min'] = np.min(signals)
sheaders[0]['physical_max'] = np.max(signals)
# should not give a warning as its within bounds
with self.assertRaises(AssertionError):
with self.assertWarns(UserWarning):
highlevel.write_edf(self.edfplus_data_file, signals, sheaders, digital=False)

signals = [np.random.randint(-2048, 2048, 256*60)]
sheaders = [highlevel.make_signal_header('ch1', sample_frequency=256)]
sheaders[0]['physical_min'] = np.min(signals) - 0.01
sheaders[0]['physical_max'] = np.max(signals) + 0.01
# should not give a warning as its within bounds of resolution
with self.assertRaises(AssertionError):
with self.assertWarns(UserWarning):
highlevel.write_edf(self.edfplus_data_file, signals, sheaders, digital=False)


def test_read_write_accented(self):
signals = np.random.rand(3, 256*60) # then rescale to 0-1
Expand Down

0 comments on commit 40a5aa6

Please sign in to comment.