From c47277ad2d6146e8612080fbf4fe5632f58297e3 Mon Sep 17 00:00:00 2001 From: Cody Mitchell Date: Sun, 13 Sep 2026 23:08:43 -0500 Subject: [PATCH] Ignore corrupt trailing timestamps when filling idle sleep --- pygt3x/reader.py | 7 ++-- tests/test_ism_corrupt_tail.py | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tests/test_ism_corrupt_tail.py diff --git a/pygt3x/reader.py b/pygt3x/reader.py index f4674ac..aefee88 100644 --- a/pygt3x/reader.py +++ b/pygt3x/reader.py @@ -144,6 +144,7 @@ def _get_data_default(self, num_rows=None): last_idsm_ts = 0 # Initialize evt in case there are no events in the GT3x file evt = None + last_valid_timestamp = None for evt in self.read_events(num_rows): if not evt.is_checksum_valid: @@ -152,6 +153,8 @@ def _get_data_default(self, num_rows=None): ) continue + last_valid_timestamp = evt.header.timestamp + try: type = Types(evt.header.event_type) except ValueError: @@ -279,8 +282,8 @@ def _get_data_default(self, num_rows=None): # Idle sleep mode was started but not finished before the recording # ended. This means that we might be missing some records at the end of # the file. - assert evt is not None - idle_sleep_mode_ended = evt.header.timestamp + assert last_valid_timestamp is not None + idle_sleep_mode_ended = last_valid_timestamp payload = self._validate_payload( self._fill_ism( idle_sleep_mode_started - (dt_idm - 1), diff --git a/tests/test_ism_corrupt_tail.py b/tests/test_ism_corrupt_tail.py new file mode 100644 index 0000000..0e1018e --- /dev/null +++ b/tests/test_ism_corrupt_tail.py @@ -0,0 +1,63 @@ +from functools import reduce +from operator import xor +import struct +from zipfile import ZipFile + +import numpy as np +import pandas as pd +import pytest + +from pygt3x.reader import FileReader + + +START = 1600000000 + + +def _record(event_type, second, payload, corrupt=False): + header = struct.pack("