import unittest,numpy as np from shapely.geometry import LineString from plan_refinement import clamped_spline,smooth_profile_path,replace_interval,refine_profile class Ridge: def material(self,p):return p[:,1] def score(self,p,n,sign):return np.exp(-((p[:,1]-.8)/.12)**2),np.ones(len(p)) class Tests(unittest.TestCase): def test_profile_follows_continuous_ridge(self): p=np.c_[np.linspace(0,30,101),np.zeros(101)];curve,report,_=refine_profile(p,Ridge(),band=1.2) self.assertGreater(np.median(curve[50:-50,1]),.65);np.testing.assert_array_equal(curve[[0,-1]],p[[0,-1]]) def test_masked_gap_keeps_ridge_continuation(self): offsets=np.linspace(-1,1,21);score=np.exp(-((offsets-.6)/.15)**2)[None,:]*np.ones((60,1));w=np.ones_like(score);w[20:40]=0 path,_=smooth_profile_path(score,w,offsets,.3,prior=0) self.assertTrue(np.all(abs(path[20:40]-.6)<.11)) def test_spline_preserves_endpoints_and_tangent(self): p=np.c_[np.linspace(0,20,61),np.zeros(61),np.ones(61)*2];target=p[:,:2].copy();target[:,1]=np.sin(np.linspace(0,np.pi,61)) c,m=clamped_spline(p,target,np.ones(61));np.testing.assert_array_equal(c[[0,-1]],p[[0,-1]]) self.assertLess(abs((c[1,1]-c[0,1])/(c[1,0]-c[0,0])),.02);self.assertTrue(LineString(c).is_simple) def test_straight_observation_stays_straight(self): p=np.c_[np.linspace(0,30,101),np.ones(101)*3];c,_=clamped_spline(p,p,np.ones(101));self.assertLess(np.max(abs(c[:,1]-3)),1e-8) def test_replacement_keeps_outside_vertices(self): p=np.array([[0,0],[2,0],[8,0],[10,0]]);c=replace_interval(p,2,8,[[2,0],[5,.5],[8,0]]);np.testing.assert_array_equal(c,[[0,0],[2,0],[5,.5],[8,0],[10,0]]) def test_replacement_rejects_detached_anchor(self): with self.assertRaises(ValueError):replace_interval([[0,0],[10,0]],2,8,[[2,.1],[8,0]]) class AccessTests(unittest.TestCase): def test_access_sides_stay_open(self): from driveway_refinement import split_access a,b,report=split_access([[0,0,2],[20,0,2]],[[8,0,2],[12,0,2]],[[8,-6,2],[12,-6,2]]) np.testing.assert_array_equal(a,[[0,0,2],[8,0,2],[8,-6,2]]);np.testing.assert_array_equal(b,[[12,-6,2],[12,0,2],[20,0,2]]) self.assertFalse(LineString(a).intersects(LineString(b)));self.assertEqual(report['source_interval_m'],[8.,12.]) def test_access_rejects_crossed_sides(self): from driveway_refinement import split_access with self.assertRaises(ValueError):split_access([[0,0],[20,0]],[[8,0],[12,0]],[[14,-6],[6,-6]]) class LongLineTests(unittest.TestCase): def test_long_consensus_rejects_local_bright_distractors(self): from long_edge_refinement import fit_line_consensus x=np.linspace(-35,35,281);true=np.c_[x,.01*x+.8+.02*np.sin(x)] debris=np.c_[np.linspace(-8,8,60),np.linspace(-1.8,-.6,60)] slope,offset,chosen,_=fit_line_consensus(np.vstack([true,debris]),np.r_[np.ones(281),np.ones(60)*2],2) self.assertAlmostEqual(slope,.01,places=3);self.assertAlmostEqual(offset,.8,places=2);self.assertLess(chosen[281:].sum(),5) def test_short_uninformative_support_is_rejected(self): from long_edge_refinement import fit_line_consensus with self.assertRaises(ValueError):fit_line_consensus(np.c_[np.linspace(0,2,30),np.ones(30)],np.ones(30),2) def test_invalid_observations_rejected(self): from long_edge_refinement import fit_line_consensus with self.assertRaises(ValueError):fit_line_consensus(np.ones((30,2))*np.nan,np.ones(30),2) if __name__=='__main__':unittest.main(verbosity=2)