"""Analytic acceptance for the new reproduction, independent of real labels.""" import unittest from itertools import permutations import numpy as np from gre24_features import (mesh, exterior_faces, gate_nodes, solve_eikonal, cell_flow_speed, strict_volume_cdf, nodal_gradient, spatial_moment, per_geometry_metrics, appendix_summary, features) def box(nx=5, ny=3, nz=3): axes = [np.linspace(0, 2, nx), np.linspace(0, 1, ny), np.linspace(0, 1, nz)] x = np.stack(np.meshgrid(*axes, indexing='ij'), axis=-1).reshape(-1, 3) ids = np.arange(len(x)).reshape(nx, ny, nz); t=[] for i in range(nx-1): for j in range(ny-1): for k in range(nz-1): for perm in permutations(range(3)): v=np.array([i,j,k]); row=[ids[tuple(v)]] for a in perm: v=v.copy();v[a]+=1;row.append(ids[tuple(v)]) t.append(row) return x, np.array(t, dtype=np.int32) class Gre24Checks(unittest.TestCase): def test_planar_speed_two(self): x,t=box();g=mesh(x,t);source=np.flatnonzero(x[:,0]==0) phi=solve_eikonal(g,source,2.) np.testing.assert_allclose(phi,x[:,0]/2,atol=1e-7,rtol=1e-7) def test_strict_ties_volume_weights(self): np.testing.assert_allclose(strict_volume_cdf([0,1,1,2],[1,1,1,1]),[0,.25,.25,.75]) np.testing.assert_allclose(strict_volume_cdf([0,1,1,2],[1,2,3,4]),[0,.1,.1,.6]) def test_rod_volume_mapping(self): x,t=box(21,2,2);g=mesh(x,t) f=strict_volume_cdf(x[:,0],g['mass']) self.assertLessEqual(np.max(np.abs(f-x[:,0]/2)),1/20+1e-12) def test_disk_ideal_volume_mapping(self): # Annular volume quadrature of the analytic radial order, not a tetFIM claim. edges=np.linspace(0,1,101);radius=(edges[:-1]+edges[1:])/2 mass=np.pi*np.diff(edges**2) f=strict_volume_cdf(radius,mass) self.assertLess(np.max(np.abs(f-radius**2)),.011) def test_affine_gradient_and_constant(self): x,t=box();g=mesh(x,t);a=np.array([2.,-3.,.7]) grad=nodal_gradient(g,x@a+4,normalize=False) np.testing.assert_allclose(grad,np.tile(a,(len(x),1)),atol=1e-12) np.testing.assert_array_equal(nodal_gradient(g,np.full(len(x),7.)),np.zeros_like(x)) def test_moment_rotation(self): x,t=box(3,3,3);g=mesh(x,t);di=x[:,0] s,_=spatial_moment(g,di) theta=.37;rot=np.array([[np.cos(theta),-np.sin(theta),0],[np.sin(theta),np.cos(theta),0],[0,0,1.]]) sr,_=spatial_moment(mesh(x@rot.T,t),di) def matrix(a): return a[:,np.array([[0,1,2],[1,3,4],[2,4,5]])] sm=matrix(s);np.testing.assert_allclose(np.trace(sm,axis1=1,axis2=2),1,atol=1e-12) self.assertTrue(np.all(np.linalg.eigvalsh(sm)>=-1e-12)) np.testing.assert_allclose(matrix(sr),rot@sm@rot.T,atol=1e-12) def test_all_wall_tet_supplement(self): x=np.array([[0,0,0],[1,0,0],[0,1,0],[0,0,1.]]) t=np.array([[0,1,2,3]]);g=mesh(x,t) speed,zero=cell_flow_speed(g,np.zeros(4),exterior_faces(t)) np.testing.assert_allclose(speed,[1/(4*np.sqrt(3))],atol=1e-12) np.testing.assert_array_equal(zero,[0]) def test_gate_original_id_tie(self): x=np.array([[-1.,0,0],[1.,0,0]]) ids,mode=gate_nodes(x,[17,3],[0,0,0]);np.testing.assert_array_equal(ids,[1]) def test_three_metrics_and_undefined(self): r=per_geometry_metrics([1,3],[2,3]) np.testing.assert_allclose([r['rrmse_rms'],r['rrmse_maximum'],r['rrmse_mean']], [1/np.sqrt(10),1/(3*np.sqrt(2)),1/(2*np.sqrt(2))]) z=per_geometry_metrics([0,0],[0,1]);self.assertIsNone(z['rrmse_rms']) def test_split_then_average(self): r=appendix_summary([[0,1,2],[10,20,30],[2,3,4],[0,0,100],[7,8,9]]) self.assertEqual(r['mean_of_split_summaries']['median'],6.4) def test_complete_pipeline_and_fail_closed(self): x,t=box();a,m=features(x,t,np.arange(len(x)),x[0]) self.assertEqual(a['features'].shape,(len(x),19)) self.assertGreater(m['all_wall_tetrahedra'],0) self.assertTrue(np.isfinite(a['features']).all()) with self.assertRaises(ValueError):solve_eikonal(mesh(x,t),np.array([0]),0.) if __name__=='__main__':unittest.main()