Skip to content

Commit 5c4f2e5

Browse files
resize sliding_window.py at runtime and add __repr__ method
1 parent 55a591f commit 5c4f2e5

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

pyeyesweb/data_models/sliding_window.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ class SlidingWindow:
4545
>>> print(f"Buffer contains {len(window)} samples")
4646
"""
4747

48+
@property
49+
def max_length(self):
50+
return self._max_length
51+
52+
@max_length.setter
53+
def max_length(self, value: int):
54+
if value <= 0:
55+
raise ValueError("max_length must be positive.")
56+
if value != self._max_length:
57+
self._max_length = value
58+
self._resize()
59+
4860
def __init__(self, max_length: int, n_columns: int):
4961
# Validate inputs
5062
if not isinstance(max_length, int):
@@ -86,6 +98,48 @@ def __del__(self):
8698
if hasattr(self, '_timestamp'):
8799
del self._timestamp
88100

101+
def __len__(self) -> int:
102+
"""
103+
Return the current number of samples in the sliding window.
104+
105+
Returns
106+
-------
107+
int
108+
Number of samples currently stored in the buffer.
109+
110+
Examples
111+
--------
112+
>>> window = SlidingWindow(max_length=10, n_columns=2)
113+
>>> print(len(window)) # 0
114+
>>> window.append([1.0, 2.0])
115+
>>> print(len(window)) # 1
116+
"""
117+
with self._lock:
118+
return self._size
119+
120+
def __repr__(self) -> str:
121+
"""
122+
Return a concise representation showing max length, shape, and the samples with timestamps.
123+
"""
124+
data, timestamps = self.to_array()
125+
return f"""data=\n{data},\ntimestamps=\n{timestamps}"""
126+
127+
def _resize(self):
128+
old_data, old_timestamps = self.to_array()
129+
new_buffer = np.empty((self._max_length, self._n_columns), dtype=np.float32)
130+
new_timestamps = np.empty(self._max_length, dtype=np.float64)
131+
132+
# Determine how many samples we can keep
133+
keep = min(len(old_data), self._max_length)
134+
new_buffer[:keep, :min(old_data.shape[1], self._n_columns)] = old_data[-keep:,
135+
:min(old_data.shape[1], self._n_columns)]
136+
new_timestamps[:keep] = old_timestamps[-keep:]
137+
138+
self._buffer = new_buffer
139+
self._timestamp = new_timestamps
140+
self._start = 0
141+
self._size = keep
142+
89143
def append(self, samples: Union[np.ndarray, list], timestamp: Optional[float] = None) -> None:
90144
"""
91145
Append a new sample to the sliding window.
@@ -205,22 +259,3 @@ def is_full(self) -> bool:
205259
"""
206260
with self._lock:
207261
return self._size == self._max_length
208-
209-
def __len__(self) -> int:
210-
"""
211-
Return the current number of samples in the sliding window.
212-
213-
Returns
214-
-------
215-
int
216-
Number of samples currently stored in the buffer.
217-
218-
Examples
219-
--------
220-
>>> window = SlidingWindow(max_length=10, n_columns=2)
221-
>>> print(len(window)) # 0
222-
>>> window.append([1.0, 2.0])
223-
>>> print(len(window)) # 1
224-
"""
225-
with self._lock:
226-
return self._size

0 commit comments

Comments
 (0)