Skip to content

Commit

Permalink
only warn when writing zero rates instead of error
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Dec 19, 2023
1 parent dd2e848 commit b48588c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/pynwb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ def __init__(self, **kwargs):
if isinstance(timestamps, TimeSeries):
timestamps.__add_link('timestamp_link', self)
elif self.rate is not None:
if self.rate <= 0:
if self.rate < 0:
self._error_on_new_warn_on_construct(
error_msg='Rate must be a positive value.'
error_msg='Rate must not be a negative value.'
)
elif self.rate == 0.0 and get_data_shape(data)[0] > 1:
warn('Timeseries has a rate of 0.0 Hz, but the length of the data is greater than 1.')
if self.starting_time is None: # override default if rate is provided but not starting time
self.starting_time = 0.0
self.starting_time_unit = self.__time_unit
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,12 @@ def test_get_data_in_units(self):
assert_array_equal(ts.get_data_in_units(), [1., 2., 3.])

def test_non_positive_rate(self):
with self.assertRaisesWith(ValueError, 'Rate must be a positive value.'):
with self.assertRaisesWith(ValueError, 'Rate must not be a negative value.'):
TimeSeries(name='test_ts', data=list(), unit='volts', rate=-1.0)
with self.assertRaisesWith(ValueError, 'Rate must be a positive value.'):
TimeSeries(name='test_ts1', data=list(), unit='volts', rate=0.0)

with self.assertWarnsWith(UserWarning,
'Timeseries has a rate of 0.0 Hz, but the length of the data is greater than 1.'):
TimeSeries(name='test_ts1', data=[1, 2, 3], unit='volts', rate=0.0)

def test_file_with_non_positive_rate_in_construct_mode(self):
"""Test that UserWarning is raised when rate is 0 or negative
Expand All @@ -419,7 +421,7 @@ def test_file_with_non_positive_rate_in_construct_mode(self):
parent=None,
object_id="test",
in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg='Rate must be a positive value.'):
with self.assertWarnsWith(warn_type=UserWarning, exc_msg='Rate must not be a negative value.'):
obj.__init__(
name="test_ts",
data=list(),
Expand Down

0 comments on commit b48588c

Please sign in to comment.