Skip to content

Commit 3aa1329

Browse files
SimonHeybrockclaude
andcommitted
Fix VariableBuffer to preserve variances on buffer expansion
Modified _allocate_buffer() in VariableBuffer to pass with_variances parameter to sc.empty(), ensuring variances are preserved when the buffer expands. Added regression test to verify the fix works correctly. Original prompt: "I just fixed a bug in VariableBuffer, which would previously raise if data had variances. Can you write a test to demonstrate this fix was necessary and prevent regression?" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent f4a23f7 commit 3aa1329

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/ess/livedata/dashboard/temporal_buffers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,12 @@ def _allocate_buffer(self, template: sc.Variable, size: int) -> sc.Variable:
247247
sizes = {self._concat_dim: size}
248248
sizes.update(template.sizes)
249249

250-
return sc.empty(sizes=sizes, dtype=template.dtype, unit=template.unit)
250+
return sc.empty(
251+
sizes=sizes,
252+
dtype=template.dtype,
253+
unit=template.unit,
254+
with_variances=template.variances is not None,
255+
)
251256

252257
def _expand_to_fit(self, min_size: int) -> None:
253258
"""Expand buffer to accommodate at least min_size elements."""

tests/dashboard/temporal_buffers_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,36 @@ def test_scalar_to_1d(self):
694694
result = buffer.get()
695695
assert result.sizes == {'time': 3}
696696
assert list(result.values) == [42.0, 43.0, 44.0]
697+
698+
def test_append_with_variances_preserves_variances_on_expansion(self):
699+
"""Test that variances are preserved when buffer expands.
700+
701+
Regression test for bug where expanding the buffer would lose variances
702+
because sc.empty() wasn't called with with_variances flag.
703+
"""
704+
# Create data with variances
705+
data = sc.array(
706+
dims=['x'], values=[1.0, 2.0], variances=[0.1, 0.2], unit='counts'
707+
)
708+
buffer = VariableBuffer(data=data, max_capacity=100, concat_dim='time')
709+
710+
# Verify initial data has variances
711+
assert buffer.get().variances is not None
712+
713+
# Append enough single slices to trigger multiple buffer expansions
714+
# Initial capacity is 16, so we need > 16 appends
715+
for i in range(20):
716+
new_data = sc.array(
717+
dims=['x'],
718+
values=[float(i + 3), float(i + 4)],
719+
variances=[0.3 + i * 0.01, 0.4 + i * 0.01],
720+
unit='counts',
721+
)
722+
assert buffer.append(new_data), f"Failed to append slice {i}"
723+
724+
# Verify final result still has variances after expansion
725+
result = buffer.get()
726+
assert result.variances is not None
727+
assert result.sizes['time'] == 21
728+
# Verify variances were preserved from at least one slice
729+
assert result.variances[0, 0] > 0

0 commit comments

Comments
 (0)