Skip to content

Commit d25689e

Browse files
committed
oliver review
1 parent 2c172c6 commit d25689e

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/pynwb/base.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,26 +405,29 @@ def get_starting_time(self):
405405
Returns
406406
-------
407407
float or None
408-
The starting time in seconds, or None if the TimeSeries has no data.
408+
The starting time in seconds, or None if no starting time is defined
409+
and there is no data.
409410
"""
410-
if self.num_samples is None or self.num_samples == 0:
411-
return None
412-
if self.fields.get('timestamps'):
411+
if self.starting_time is not None:
412+
return self.starting_time
413+
elif self.num_samples is not None and self.num_samples > 0:
413414
return float(self.timestamps[0])
414415
else:
415-
return self.starting_time
416+
# No starting_time defined and no data (e.g., empty timestamps-based TimeSeries)
417+
return None
416418

417419
def get_duration(self):
418420
"""
419421
Get the duration of this TimeSeries in seconds.
420422
421423
Returns the time span from the first sample to the last sample.
422-
For a single sample, returns 0.
424+
For a single sample or empty TimeSeries with a defined starting_time, returns 0.
423425
424426
Returns
425427
-------
426428
float or None
427-
The duration in seconds, or None if the TimeSeries has no data.
429+
The duration in seconds, or None if the TimeSeries has no data and
430+
no starting_time defined.
428431
429432
Notes
430433
-----
@@ -435,19 +438,21 @@ def get_duration(self):
435438
recording time. If you need to account for the last sample's duration
436439
(e.g., for continuous recordings), add 1/rate manually.
437440
"""
438-
n = self.num_samples
439-
if n is None or n == 0:
441+
if self.num_samples is None or self.num_samples == 0:
442+
# Empty TimeSeries with a starting_time has duration 0
443+
if self.starting_time is not None:
444+
return 0.0
440445
return None
441446

442-
if n == 1:
447+
if self.num_samples == 1:
443448
return 0.0
444449

445450
if self.fields.get('timestamps'):
446451
timestamps = self.timestamps
447452
return float(timestamps[-1] - timestamps[0])
448453
else:
449454
# Rate-based
450-
return (n - 1) / self.rate
455+
return (self.num_samples - 1) / self.rate
451456

452457
def get_data_in_units(self):
453458
"""

tests/unit/test_base.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,14 @@ def test_get_starting_time_with_timestamps(self):
555555
ts = mock_TimeSeries(data=[1, 2, 3], timestamps=[2.5, 3.5, 4.5])
556556
self.assertEqual(ts.get_starting_time(), 2.5)
557557

558-
def test_get_starting_time_empty_data(self):
559-
"""Test get_starting_time with empty data returns None"""
560-
ts = mock_TimeSeries(data=[], rate=10.0)
558+
def test_get_starting_time_empty_data_with_starting_time(self):
559+
"""Test get_starting_time with empty data but defined starting_time"""
560+
ts = mock_TimeSeries(data=[], rate=10.0, starting_time=5.0)
561+
self.assertEqual(ts.get_starting_time(), 5.0)
562+
563+
def test_get_starting_time_empty_data_no_starting_time(self):
564+
"""Test get_starting_time with empty data and no starting_time returns None"""
565+
ts = mock_TimeSeries(data=[], timestamps=[])
561566
self.assertIsNone(ts.get_starting_time())
562567

563568
def test_get_duration_with_rate(self):
@@ -580,9 +585,14 @@ def test_get_duration_single_sample(self):
580585
ts_timestamps = mock_TimeSeries(data=[1], timestamps=[5.0])
581586
self.assertEqual(ts_timestamps.get_duration(), 0.0)
582587

583-
def test_get_duration_empty_data(self):
584-
"""Test get_duration with empty data returns None"""
585-
ts = mock_TimeSeries(data=[], rate=10.0)
588+
def test_get_duration_empty_data_with_starting_time(self):
589+
"""Test get_duration with empty data but defined starting_time returns 0"""
590+
ts = mock_TimeSeries(data=[], rate=10.0, starting_time=5.0)
591+
self.assertEqual(ts.get_duration(), 0.0)
592+
593+
def test_get_duration_empty_data_no_starting_time(self):
594+
"""Test get_duration with empty data and no starting_time returns None"""
595+
ts = mock_TimeSeries(data=[], timestamps=[])
586596
self.assertIsNone(ts.get_duration())
587597

588598

0 commit comments

Comments
 (0)