import unittest import numpy as np from scipy.sparse import csr_matrix from phase2_b11_math import full_bars,reconstruct,gate_decision from phase2_h1_topology import events def graph(n,edges): a=[i for i,j in edges]+[j for i,j in edges];b=[j for i,j in edges]+[i for i,j in edges] return csr_matrix((np.ones(len(a)),(a,b)),shape=(n,n)) class B11Tests(unittest.TestCase): def test_required_chain(self): s=np.array([4.,1.,2.]);a=graph(3,[(0,1),(1,2)]) clean,detail=reconstruct(s,[.970,.015,.015],np.arange(3),a) np.testing.assert_array_equal(clean,[4.,1.,1.]);self.assertEqual(len(detail['bars']),2) self.assertAlmostEqual(detail['bars'][1]['persistence'],.015) def test_single_plateau_identity(self): s=np.array([4.,4.,2.,1.]);a=graph(4,[(0,1),(1,2),(2,3)]) clean,d=reconstruct(s,np.ones(4),np.arange(4),a) np.testing.assert_array_equal(clean,s);self.assertEqual(d['bars'][0]['birth_nodes'],[0,1]) def test_disconnected_components_keep_infinite(self): s=np.array([4.,1.,2.,0.]);a=graph(4,[(0,1),(2,3)]) clean,d=reconstruct(s,np.ones(4),np.arange(4),a) np.testing.assert_array_equal(clean,s);self.assertEqual(d['infinite_classes'],2) def test_compound_all_dying_bars_match_b3(self): s=np.array([4.,3.,2.,1.,1.]);a=graph(5,[(0,3),(1,3),(1,4),(2,4)]) d=full_bars(s,np.ones(5),np.arange(5),a);e=events(s,np.ones(5),np.arange(5),np.c_[np.arange(5),np.zeros((5,2))],a) finite=[(b['birth_raw_id'],b['birth'],b['death'],b['persistence']) for b in d['bars'] if not b['infinite']] old=[(b['birth_raw_id'],b['birth'],b['death'],b['persistence']) for event in e['events'] for b in event['bars']] self.assertEqual(sorted(finite),sorted(old));self.assertEqual(len(finite),2) def test_exact_threshold_kept_and_raw_id_elder(self): s=np.array([4.,1.,2.]);a=graph(3,[(0,1),(1,2)]) clean,_=reconstruct(s,[30.,1.,1.],np.arange(3),a,threshold=1/32) np.testing.assert_array_equal(clean,s) tied=np.array([4.,1.,4.]);clean,d=reconstruct(tied,[1.,100.,1.],np.array([10,20,1]),a,threshold=1.) np.testing.assert_array_equal(clean,[1.,1.,4.]);self.assertEqual(next(b for b in d['bars'] if b['infinite'])['birth_raw_id'],1) def test_gate_empty_and_incomplete_cannot_pass(self): paired={'last_location':[(0.,0.)]*50,'kendall_tau_b':[(1.,1.)]*50} self.assertEqual(gate_decision(dict(precision=.1,recall=.9),dict(precision=.2,recall=.9),paired,50,[])['decision'],'RETAIN_FOR_INDEPENDENT_PANEL') self.assertEqual(gate_decision(dict(precision=.1,recall=.9),dict(precision=None,recall=0.),paired,50,[])['decision'],'PARK_UNDEFINED_OR_COVERAGE') self.assertEqual(gate_decision(dict(precision=.1,recall=.9),dict(precision=.2,recall=.9),paired,49,[])['decision'],'PARK_UNDEFINED_OR_COVERAGE') if __name__=='__main__':unittest.main()