Expose numeric trend segment formulas from native reconstruction models

This commit is contained in:
2026-09-27 03:13:39 +03:00
parent de4609ea00
commit f3937eaf14
7 changed files with 203 additions and 4 deletions

View File

@@ -0,0 +1,32 @@
"""Formula coefficients must reproduce the native reconstruction, not a refit."""
import unittest
from set_devices.signal_reconstruction import reconstruct
from set_devices.plot_formula import linear_piece
class PlotFormulaTests(unittest.TestCase):
def test_all_methods_match_native_values_on_irregular_and_duplicate_knots(self):
points = [(0, 2), (.3, 8), (.3, 4), (1.1, -3), (2, 7), (4, 1), (5, 2)]
for method in ('linear', 'pchip', 'spline', 'polynomial'):
result = reconstruct(points, method, 101, 3, with_model=True)
for x, y in result.points:
piece = next(piece for piece in result.pieces if piece.left <= x <= piece.right)
self.assertAlmostEqual(y, piece.value(x), places=9)
def test_polynomial_model_with_fewer_output_samples_than_coefficients(self):
points = [(i, 1 + i * .2 - i ** 2 * .03 + i ** 5 * .0001) for i in range(9)]
result = reconstruct(points, 'polynomial', 2, 5, with_model=True)
for x, y in points:
self.assertAlmostEqual(y, result.pieces[0].value(x), places=9)
def test_source_line_and_formula_use_displayed_origin(self):
piece = linear_piece([1000, 1100, 1200], [1, 3, 0], 1050)
self.assertAlmostEqual(2, piece.value(1050))
text = piece.text(1000, 'мс')
self.assertIn('y = 1 +2·u', text)
self.assertIn('0 ≤ x ≤ 100 мс', text)
self.assertIsNone(linear_piece([0, 1, 2], [1, float('nan'), 3], .5))
if __name__ == '__main__':
unittest.main()