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

+37
View File
@@ -523,5 +523,42 @@ class TestAccumulation(unittest.TestCase):
self.assertTrue(accu.lock.acquire(timeout=1), "lock leaked after ValueError in write_to_tecmag")
accu.lock.release()
def test_empty_state_accessors(self):
"""Empty Accumulation accessors must return ndarrays, min/max helpers 0."""
accu = Accumulation()
self.assertIsInstance(accu.x, np.ndarray)
self.assertEqual(accu.x.size, 0)
self.assertIsInstance(accu.get_ydata(), np.ndarray)
self.assertEqual(accu.get_ydata().size, 0)
self.assertIsInstance(accu.get_yerr(0), np.ndarray)
self.assertEqual(accu.get_yerr(0).size, 0)
self.assertEqual(accu.get_xmin(), 0)
self.assertEqual(accu.get_xmax(), 0)
self.assertEqual(accu.get_ymin(), 0)
self.assertEqual(accu.get_ymax(), 0)
def test_get_yerr_without_statistics_on_empty(self):
"""get_yerr on an empty accumulation without statistics must not crash."""
accu = Accumulation(error=False)
err = accu.get_yerr(0)
self.assertIsInstance(err, np.ndarray)
self.assertEqual(err.size, 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]]
y_2 = [[100.0, 400.0, 900.0], [225.0, 625.0, 1225.0]]
accu = Accumulation(x=x, y=y, y_2=y_2, n=1,
index=[(0, 2)], sampl_freq=1000.0, error=True)
self.assertIsInstance(accu.x, np.ndarray)
np.testing.assert_array_equal(accu.x, [0.0, 1.0, 2.0])
for ch in accu.y:
self.assertIsInstance(ch, np.ndarray)
for ch in accu.y_square:
self.assertIsInstance(ch, np.ndarray)
np.testing.assert_array_equal(accu.y[0], [10, 20, 30])
np.testing.assert_array_equal(accu.y_square[0], [100.0, 400.0, 900.0])
if __name__ == "__main__":
unittest.main()