Files
MeltMind/tools/test_phase2_residual_math.py

45 lines
2.2 KiB
Python

"""Synthetic interface checks only; no model fit or project data access."""
import unittest
import numpy as np
from phase2_residual_math import prior, residual_target, decode_seconds, learning_clock
class ResidualInterfaceTests(unittest.TestCase):
def test_component_equal_clock_not_node_or_part_weighted(self):
self.assertEqual(learning_clock([1., 3., 10.], ['a', 'a', 'b']), 6.)
self.assertAlmostEqual(learning_clock([1., 3., 10.], ['a', 'b', 'c']), 14./3)
with self.assertRaises(ValueError): learning_clock([], [])
with self.assertRaises(ValueError): learning_clock([0.], ['a'])
def test_negative_residual_and_exact_reconstruction(self):
f = np.array([0., .25, .75]); y = np.array([0., .125, .5])
r = residual_target(y, f, 2., fit_dtype='float64')
self.assertTrue(np.any(r < 0))
np.testing.assert_array_equal(decode_seconds(r, f, 2., 4.), y*4.)
def test_zero_residual_is_prior(self):
f = np.array([0., .25, .5], dtype=np.float32)
np.testing.assert_array_equal(decode_seconds(np.zeros(3), f, 1.5, 2.), np.asarray(f, dtype=np.float64)*3.)
def test_no_posthoc_clamp(self):
y = decode_seconds(np.array([-1., 2.]), np.array([0., 1.]), 1., 1.)
np.testing.assert_array_equal(y, [-1., 3.])
def test_bad_shapes_and_inputs(self):
for f in [np.array([]), np.array([np.nan]), np.array([-0.1]), np.array([1.1]), np.zeros((2, 1))]:
with self.assertRaises(ValueError): prior(f, 1.)
for clock in [0., -1., float('nan'), float('inf')]:
with self.assertRaises(ValueError): prior(np.array([.5]), clock)
with self.assertRaises(ValueError): residual_target(np.array([1., 2.]), np.array([.5]), 1., fit_dtype='float32')
with self.assertRaises(ValueError): decode_seconds(np.array([0.]), np.array([.5]), 1., 0.)
def test_permutation_equivariance(self):
f = np.array([0., .1, .1, .7]); y = np.array([0., .05, .2, .8]); order = np.array([2, 0, 3, 1])
a = residual_target(y, f, .9, fit_dtype='float32')
b = residual_target(y[order], f[order], .9, fit_dtype='float32')
np.testing.assert_array_equal(a[order], b)
if __name__ == '__main__':
unittest.main()