Files
MeltMind/tests/test_g1_component_splits.py
T

56 lines
2.8 KiB
Python

"""Leak protection, deterministic allocation and small-corpus failure disclosure."""
import unittest
from tools.freeze_g1_components import DSU, action, MERGE, NOEDGE, split_components
class ComponentTests(unittest.TestCase):
def test_transitive_key_and_cross_key_edges(self):
d = DSU('abcdef')
for a, b in [('a','b'), ('c','d'), ('b','c'), ('e','f')]:
d.union(a, b)
self.assertEqual({d.root(x) for x in 'abcd'}, {'a'})
self.assertNotEqual(d.root('a'), d.root('e'))
def test_two_reviewer_conservative_action(self):
self.assertEqual(action('unrelated','unrelated'), NOEDGE)
for a in ['unrelated', 'UNRESOLVED', 'rigid / same part']:
for b in ['unrelated', 'UNRESOLVED', 'rigid / same part']:
if (a,b) != ('unrelated','unrelated'):
self.assertEqual(action(a,b), MERGE)
with self.assertRaises(AssertionError): action('new category', 'unrelated')
def test_allocation_partition_prefix_and_input_order_invariance(self):
c = [dict(component_id=f'c{i:03}',parts=(i%7)+1) for i in range(43)]
s = split_components(c)
self.assertEqual(s, split_components(list(reversed(c))))
ids = {x['component_id'] for x in c}
tests = [x for f in s['folds'] for x in f['test']]
self.assertEqual(len(tests), len(set(tests)))
self.assertEqual(set(tests), ids)
# Independently reconstruct each placement using the published priority.
by_id = {x['component_id']:x for x in c}
bins = [[] for _ in range(5)]
for cid in s['placement_order']:
k = min(range(5), key=lambda j:(sum(by_id[x]['parts'] for x in bins[j]),len(bins[j]),j))
bins[k].append(cid)
self.assertEqual(bins, [f['test'] for f in s['folds']])
for f in s['folds']:
a,b,d = map(set, (f['test'],f['validation'],f['learning_order']))
self.assertFalse(a&b or b&d or a&d)
self.assertEqual(a|b|d, ids)
self.assertEqual(len(b), (len(ids)-len(a)+4)//5)
for p, divisor in [('25',4),('50',2),('100',1)]:
self.assertEqual(f['pools'][p], f['learning_order'][:(len(d)+divisor-1)//divisor])
self.assertEqual(f['noncomparability_reasons'], [])
def test_small_corpus_flags_without_component_cut(self):
s = split_components([dict(component_id='huge',parts=100)])
self.assertEqual(s['folds'][0]['test'], ['huge'])
for f in s['folds']:
self.assertIn('FEWER_THAN_FIVE_COMPONENTS', f['noncomparability_reasons'])
self.assertIn('EMPTY_LEARNING_POOL', f['noncomparability_reasons'])
self.assertIn('COINCIDENT_LEARNING_TIERS', f['noncomparability_reasons'])
if __name__ == '__main__': unittest.main()