Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
69 lines
3.3 KiB
Python
69 lines
3.3 KiB
Python
"""Geometric ownership, missing evidence, and translation invariance of window checks."""
|
|
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
import unittest
|
|
|
|
import shapely
|
|
from shapely.affinity import translate
|
|
from shapely.geometry import box, LineString
|
|
|
|
source = Path(os.environ.get('WINDOW_SOURCE', str(Path(__file__).resolve().parents[1] /
|
|
'simulation/urban-plan-research/replay/linear_window_support.py')))
|
|
spec = importlib.util.spec_from_file_location('linear_window_support', source)
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
|
|
|
|
class WindowTests(unittest.TestCase):
|
|
def test_curved_disjoint_cells_preserve_area_and_holes(self):
|
|
p = LineString([(0, 0), (13, 2), (17, 7), (24, 10)]).buffer(.1)
|
|
ps = list(m.cells(p))
|
|
self.assertLess(shapely.union_all(ps).symmetric_difference(p).area, 1e-10)
|
|
self.assertAlmostEqual(sum(x.area for x in ps), p.area)
|
|
ring = box(0, 0, 12, 12).difference(box(3, 3, 9, 9))
|
|
self.assertAlmostEqual(shapely.union_all(list(m.cells(ring))).area, ring.area)
|
|
|
|
def test_long_chain_gets_two_view_local_checks(self):
|
|
p = box(0, 0, 70, .15)
|
|
def sample(part):
|
|
self.assertLessEqual(part.bounds[2]-part.bounds[0], 3.000001)
|
|
return [dict(valid=1., support=.94)]*2, ['a', 'b']
|
|
summary, rows = m.inspect(p, sample)
|
|
self.assertAlmostEqual(summary['measured_fraction'], 1.)
|
|
self.assertGreater(len(rows), 20)
|
|
self.assertAlmostEqual(summary['area_fractions']['paint-supported'], 1.)
|
|
|
|
def test_missing_occluded_and_disagreeing_views_never_become_negative(self):
|
|
for ev, expected in [([], 'missing-two-view-coverage'),
|
|
([dict(valid=1., support=0.)], 'missing-two-view-coverage'),
|
|
([dict(valid=.4, support=0.)]*2, 'occluded-or-outside-road'),
|
|
([dict(valid=1., support=0.), dict(valid=1., support=.99)], 'mixed-paint'),
|
|
([dict(valid=1., support=float('nan'))]*2, 'invalid-evidence')]:
|
|
self.assertEqual(m.classify(ev, 20), expected)
|
|
self.assertEqual(m.classify([dict(valid=1., support=0.)]*2, 0), 'unmeasured-footprint')
|
|
self.assertEqual(m.classify([dict(valid=1., support=0.)]*2, 20), 'weak-paint')
|
|
self.assertFalse(m.POLICY['automatic_removal'])
|
|
|
|
def test_translation_does_not_select_a_location(self):
|
|
p = box(0, 0, 30, .15)
|
|
read = lambda part: ([dict(valid=1., support=.9)]*2, ['a', 'b'])
|
|
before, a = m.inspect(p, read)
|
|
after, b = m.inspect(translate(p, 100000.31, -10000.7), read)
|
|
self.assertEqual(len(a), len(b))
|
|
self.assertAlmostEqual(before['measured_fraction'], after['measured_fraction'])
|
|
self.assertEqual([r['status'] for r in a], [r['status'] for r in b])
|
|
|
|
def test_mixed_visibility_is_not_hidden_by_average(self):
|
|
rows = [dict(status='paint-supported', area_m2=8), dict(status='weak-paint', area_m2=1),
|
|
dict(status='missing-two-view-coverage', area_m2=1)]
|
|
summary = m.summarize(rows, 10)
|
|
self.assertAlmostEqual(summary['measured_fraction'], .9)
|
|
self.assertAlmostEqual(summary['area_fractions']['weak-paint'], .1)
|
|
with self.assertRaises(ValueError):
|
|
m.summarize(rows, 9)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|