diff --git a/pyedflib/highlevel.py b/pyedflib/highlevel.py index 6b65664..938b6bc 100644 --- a/pyedflib/highlevel.py +++ b/pyedflib/highlevel.py @@ -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 {}' \ @@ -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' diff --git a/pyedflib/tests/test_highlevel.py b/pyedflib/tests/test_highlevel.py index 54f8e27..64d73b6 100644 --- a/pyedflib/tests/test_highlevel.py +++ b/pyedflib/tests/test_highlevel.py @@ -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)] @@ -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