Skip to content

Commit 1c40cd5

Browse files
Bump version to v0.0.1a4
1 parent aa20d6b commit 1c40cd5

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

pyeyesweb/data_models/sliding_window.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ def max_length(self, value: int):
5454
if value <= 0:
5555
raise ValueError("max_length must be positive.")
5656
if value != self._max_length:
57+
old_max_length = self._max_length # keep old value
5758
self._max_length = value
58-
self._resize()
59+
self._resize(old_max_length)
5960

6061
def __init__(self, max_length: int, n_columns: int):
6162
# Validate inputs
@@ -75,7 +76,7 @@ def __init__(self, max_length: int, n_columns: int):
7576
if n_columns > 10_000: # 10k features
7677
raise ValueError(f"n_columns too large ({n_columns}), maximum is 10,000")
7778

78-
self._lock = threading.Lock()
79+
self._lock = threading.RLock()
7980

8081
self._max_length = max_length
8182
self._n_columns = n_columns
@@ -124,15 +125,17 @@ def __repr__(self) -> str:
124125
data, timestamps = self.to_array()
125126
return f"""data=\n{data},\ntimestamps=\n{timestamps}"""
126127

127-
def _resize(self):
128-
old_data, old_timestamps = self.to_array()
128+
def _resize(self, old_max_length: int):
129+
# build indices with old_max_length, not the new one
130+
indices = (self._start + np.arange(self._size)) % old_max_length
131+
old_data = self._buffer[indices].copy()
132+
old_timestamps = self._timestamp[indices].copy()
133+
129134
new_buffer = np.empty((self._max_length, self._n_columns), dtype=np.float32)
130135
new_timestamps = np.empty(self._max_length, dtype=np.float64)
131136

132-
# Determine how many samples we can keep
133137
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)]
138+
new_buffer[:keep, :self._n_columns] = old_data[-keep:, :self._n_columns]
136139
new_timestamps[:keep] = old_timestamps[-keep:]
137140

138141
self._buffer = new_buffer
@@ -187,6 +190,11 @@ def append(self, samples: Union[np.ndarray, list], timestamp: Optional[float] =
187190
idx = self._start
188191
self._start = (self._start + 1) % self._max_length
189192

193+
if idx >= self._buffer.shape[0]:
194+
raise RuntimeError(
195+
f"Internal error: idx={idx} but buffer length={self._buffer.shape[0]}"
196+
)
197+
190198
self._buffer[idx] = value
191199
self._timestamp[idx] = timestamp
192200

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pyeyesweb"
7-
version = "0.0.1a3"
7+
version = "0.0.1a4"
88
requires-python = ">=3.8"
99
authors = [
1010
{name = "InfoMus Lab - Casa Paganini", email = "cp.infomus@gmail.com"}

0 commit comments

Comments
 (0)