Sliding Window
SlidingWindow
A thread-safe sliding window buffer for storing samples with timestamps.
This class implements a circular buffer that maintains a fixed-size window of the most recent samples. When the buffer is full, new samples overwrite the oldest ones. Each sample is associated with a timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_length
|
int
|
Maximum number of samples the window can hold. |
required |
n_columns
|
int
|
Number of columns (features) in each sample. |
required |
Attributes:
Name | Type | Description |
---|---|---|
_lock |
Lock
|
Thread lock for thread-safe operations. |
_max_length |
int
|
Maximum capacity of the buffer. |
_n_columns |
int
|
Number of columns per sample. |
_buffer |
ndarray
|
Circular buffer storing the samples, shape (max_length, n_columns). |
_timestamp |
ndarray
|
Array storing timestamps for each sample, shape (max_length,). |
_start |
int
|
Index of the oldest sample in the buffer. |
_size |
int
|
Current number of samples in the buffer. |
Examples:
>>> window = SlidingWindow(max_length=100, n_columns=3)
>>> window.append([1.0, 2.0, 3.0])
>>> window.append(np.array([4.0, 5.0, 6.0]), timestamp=1234567890.0)
>>> data, timestamps = window.to_array()
>>> print(f"Buffer contains {len(window)} samples")
Source code in pyeyesweb/data_models/sliding_window.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
__del__()
Clean up allocated numpy arrays when the object is destroyed.
This helps ensure memory is released promptly rather than waiting for Python's garbage collector.
Source code in pyeyesweb/data_models/sliding_window.py
__len__()
Return the current number of samples in the sliding window.
Returns:
Type | Description |
---|---|
int
|
Number of samples currently stored in the buffer. |
Examples:
>>> window = SlidingWindow(max_length=10, n_columns=2)
>>> print(len(window)) # 0
>>> window.append([1.0, 2.0])
>>> print(len(window)) # 1
Source code in pyeyesweb/data_models/sliding_window.py
__repr__()
Return a concise representation showing max length, shape, and the samples with timestamps.
append(samples, timestamp=None)
Append a new sample to the sliding window.
If the buffer is not full, the sample is added to the next available position. If the buffer is full, the oldest sample is overwritten.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samples
|
ndarray or list
|
Sample data to append. Must have exactly n_columns elements. |
required |
timestamp
|
float
|
Timestamp associated with the sample. If None, uses the current monotonic time. |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If samples is not a numpy array or list. |
ValueError
|
If the sample shape doesn't match the expected number of columns. |
Examples:
>>> window = SlidingWindow(max_length=10, n_columns=2)
>>> window.append([1.0, 2.0])
>>> window.append(np.array([3.0, 4.0]), timestamp=1234567890.0)
Source code in pyeyesweb/data_models/sliding_window.py
is_full()
Check if the sliding window buffer is at maximum capacity.
Returns:
Type | Description |
---|---|
bool
|
True if the buffer contains max_length samples, False otherwise. |
Examples:
>>> window = SlidingWindow(max_length=2, n_columns=1)
>>> print(window.is_full()) # False
>>> window.append([1.0])
>>> window.append([2.0])
>>> print(window.is_full()) # True
Source code in pyeyesweb/data_models/sliding_window.py
reset()
Reset the sliding window to empty state.
Clears all samples and timestamps from the buffer and resets internal counters. The buffer arrays are filled with NaN values.
Examples:
>>> window = SlidingWindow(max_length=10, n_columns=2)
>>> window.append([1.0, 2.0])
>>> print(len(window)) # 1
>>> window.reset()
>>> print(len(window)) # 0
Source code in pyeyesweb/data_models/sliding_window.py
to_array()
Return the current contents of the sliding window as arrays.
The returned arrays contain samples and timestamps in chronological order, with the oldest sample first and the newest sample last.
Returns:
Name | Type | Description |
---|---|---|
samples |
ndarray
|
Array of shape (current_size, n_columns) containing all samples in the buffer in chronological order. |
timestamps |
ndarray
|
Array of shape (current_size,) containing timestamps corresponding to each sample in chronological order. |
Examples:
>>> window = SlidingWindow(max_length=5, n_columns=2)
>>> window.append([1.0, 2.0])
>>> window.append([3.0, 4.0])
>>> samples, timestamps = window.to_array()
>>> print(samples.shape) # (2, 2)
>>> print(timestamps.shape) # (2,)