normalize list/array handling in data classes

Establish one invariant across the Drawable hierarchy: x is always a
numpy ndarray, y is always a list of per-channel ndarrays, and empty
data is the same types with zero length.

- Drawable: x starts as empty ndarray; min/max helpers return 0 on
  empty data instead of raising; drop contradictory isinstance(self.y,
  list) branches
- ADC_Result/Accumulation: empty constructors build ndarray x; full
  constructors coerce x/y/y_square with numpy.asarray; Accumulation
  get_ydata/get_yerr return empty ndarrays and no longer crash on
  empty statistics-less accumulations
- MeasurementResult: fix get_lineplotdata to return the accumulated
  means (previously the never-populated self.y, so the GUI line plot
  could never draw); remove dead float/int branches in CSV and HDF
  writers
- Error_Result: get_xdata/get_ydata return ndarrays

Add unittest coverage for the invariant, empty-state accessors,
constructor coercion, line plot data, CSV output and HDF round-trip.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
markusro committed 2026-10-18 16:38:41 +02:00
1 parent 35d040426b
commit 947c4c7f8d
10 files changed
+245 -58

No files matched your search

+26
View File
@@ -316,5 +316,31 @@ class TestADCResult(unittest.TestCase):
self.assertRaises(ValueError, adc1.__rfloordiv__, adc2)
self.assertRaises(ValueError, adc1.__pow__, adc2)
def test_empty_state_types(self):
"""Empty ADC_Result must hold an ndarray x and a list-of-ndarray y."""
adc = ADC_Result()
self.assertIsInstance(adc.x, np.ndarray)
self.assertIsInstance(adc.y, list)
self.assertEqual(adc.x.size, 0)
self.assertEqual(adc.get_xmin(), 0)
self.assertEqual(adc.get_xmax(), 0)
self.assertEqual(adc.get_xminpos(), 0)
self.assertEqual(adc.get_ymin(), 0)
self.assertEqual(adc.get_ymax(), 0)
self.assertEqual(adc.get_yminpos(), 0)
def test_constructor_coerces_lists(self):
"""Plain lists passed to the constructor must become numpy arrays."""
x = [0.0, 1.0, 2.0]
y = [[10, 20, 30], [15, 25, 35]]
index = [(0, 2)]
adc = ADC_Result(x, y, index, 1000.0, {"key": "value"}, 1, datetime(2023, 1, 1))
self.assertIsInstance(adc.x, np.ndarray)
np.testing.assert_array_equal(adc.x, [0.0, 1.0, 2.0])
for ch in adc.y:
self.assertIsInstance(ch, np.ndarray)
np.testing.assert_array_equal(adc.y[0], [10, 20, 30])
np.testing.assert_array_equal(adc.y[1], [15, 25, 35])
if __name__ == "__main__":
unittest.main()