Skip to content

Commit c1c8828

Browse files
committed
Treat NaNs as missing values for gaps in ring buffer
Signed-off-by: cwasicki <[email protected]>
1 parent 0a41680 commit c1c8828

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
- A new class `Fuse` has been added to represent fuses. This class has a member variable `max_current` which represents the maximum current that can course through the fuse. If the current flowing through a fuse is greater than this limit, then the fuse will break the circuit.
2222

23+
- NaN values are treated as missing when gaps are determined in the `OrderedRingBuffer`.
2324

2425
## Bug Fixes
2526

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def gaps(self) -> List[Gap]:
108108
"""
109109
return self._gaps
110110

111+
def has_value(self, sample: Sample[QuantityT]) -> bool:
112+
"""Check if a sample has a value and it's not NaN.
113+
114+
Args:
115+
sample: sample to check.
116+
117+
Returns:
118+
True if the sample has a value and it's not NaN.
119+
"""
120+
return not (sample.value is None or sample.value.isnan())
121+
111122
@property
112123
def maxlen(self) -> int:
113124
"""Get the max length.
@@ -154,10 +165,14 @@ def update(self, sample: Sample[QuantityT]) -> None:
154165
)
155166

156167
# Update data
157-
value: float = np.nan if sample.value is None else sample.value.base_value
168+
if self.has_value(sample):
169+
assert sample.value is not None
170+
value = sample.value.base_value
171+
else:
172+
value = np.nan
158173
self._buffer[self.datetime_to_index(timestamp)] = value
159174

160-
self._update_gaps(timestamp, prev_newest, sample.value is None)
175+
self._update_gaps(timestamp, prev_newest, not self.has_value(sample))
161176

162177
@property
163178
def time_bound_oldest(self) -> datetime:

tests/timeseries/test_ringbuffer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,16 @@ def test_gaps() -> None:
229229
assert len(buffer.gaps) == 2
230230

231231
buffer.update(Sample(dt(3), Quantity(np.nan)))
232-
assert len(buffer) == 4 # should be 3
233-
assert len(buffer.gaps) == 1 # should be 2
232+
assert len(buffer) == 3
233+
assert len(buffer.gaps) == 2
234234

235235
buffer.update(Sample(dt(2), Quantity(np.nan)))
236-
assert len(buffer) == 4 # should be 2
237-
assert len(buffer.gaps) == 1 # should be 2
236+
assert len(buffer) == 2
237+
assert len(buffer.gaps) == 2
238238

239239
buffer.update(Sample(dt(3), Quantity(3)))
240-
assert len(buffer) == 4 # should be 3
241-
assert len(buffer.gaps) == 1 # should be 2
240+
assert len(buffer) == 3
241+
assert len(buffer.gaps) == 2
242242

243243
buffer.update(Sample(dt(2), Quantity(2)))
244244
assert len(buffer) == 4

0 commit comments

Comments
 (0)