Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change assertion to warning #180

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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