From 81fe1fe6cdd325df2fcff4aec42807c0ef15e347 Mon Sep 17 00:00:00 2001 From: LucaCerina Date: Fri, 16 Feb 2024 09:40:50 +0100 Subject: [PATCH] Check on real length of binary eventrecord --- physio_cassette/physio_cassette.py | 3 ++- tests/test_eventrecord.py | 32 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/test_eventrecord.py diff --git a/physio_cassette/physio_cassette.py b/physio_cassette/physio_cassette.py index bc6df8e..20443e9 100644 --- a/physio_cassette/physio_cassette.py +++ b/physio_cassette/physio_cassette.py @@ -746,7 +746,8 @@ def __len__(self): if not self.is_binary: return self.data.n_measurements() elif self.data.n_measurements()>1: - return (self.data.n_measurements()-1)//2 + remainder = self.data.n_measurements() % 2 # Assess that the start/0 value may be overwritten + return (self.data.n_measurements()-remainder)//2 else: return 0 # Assuming a binary EventRecord with only the start/0 value to be empty diff --git a/tests/test_eventrecord.py b/tests/test_eventrecord.py new file mode 100644 index 0000000..fd6c239 --- /dev/null +++ b/tests/test_eventrecord.py @@ -0,0 +1,32 @@ +from datetime import datetime, timedelta + +import pytest + +from physio_cassette.physio_cassette import EventRecord + + +class TestEventRecord: + @pytest.fixture + def len_binary(self): + yield 10 + + @pytest.fixture + def simple_binary_record(self, len_binary): + # Simple binary array + t0 = datetime.fromtimestamp(0) + input_array = [1,0]*len_binary + yield EventRecord.from_state_array('simple_binary', t0=t0, input_array=input_array, start_value=0) + + @pytest.fixture + def simple_binary_nostart_record(self, len_binary): + # Binary array with start value covered by data + t0 = datetime.fromtimestamp(0) + input_array = [1,0]*len_binary + ts = [t0+timedelta(seconds=i) for i in range(len(input_array))] + yield EventRecord.from_state_array('simple_binary', t0=t0, input_array=input_array, ts_array=ts, start_value=0) + + def test_length(self, simple_binary_record, simple_binary_nostart_record, len_binary): + + # Binary records return correct number of events even if start value changes + assert simple_binary_record.n_events==len_binary + assert simple_binary_nostart_record.n_events==len_binary \ No newline at end of file