"""Worker-only numeric equivalence at hazard boundaries and disconnected patches.""" import argparse import json import time from pathlib import Path import numpy as np import terrain_costs as costs parser = argparse.ArgumentParser() parser.add_argument("--output", type=Path, required=True) parser.add_argument("--terrain", type=Path) args = parser.parse_args() assert costs._NATIVE is not None, "Compiled implementation must be installed" engine = costs._NATIVE rng = np.random.default_rng(230923) cases = [] for dtype in (np.float32, np.float64): for _ in range(200): points = rng.uniform(-0.4, 0.4, (rng.integers(8, 180), 3)).astype(dtype) points[:, 2] *= 0.3 cases.append(points) for distance in (0.119999, 0.12, 0.120001): for jump in (0.100099, 0.1001, 0.100101): cases.append(np.array([[0, 0, 0], [distance, 0, jump]], dtype=dtype)) for points in cases: costs._NATIVE = None expected = costs.connected_grade(points) costs._NATIVE = engine assert costs.connected_grade(points) == expected neighborhood_cases = 0 for dtype in (np.float32, np.float64): for offset in (0, -240, 1000): points = rng.uniform(-2, 2, (600, 4)).astype(dtype) points[:, :2] += offset # Include exact radius/cell boundaries, negative cells and duplicates. points[:7, :3] = ( np.array( [ [0, 0, 0], [0.4, 0, 0], [-0.4, 0, 0], [0.399999, 0, 0], [0.400001, 0, 0], [0, 0.4, 0], [0, 0, 0], ], dtype=dtype, ) + offset ) reference = costs._reference_neighborhoods(points) native = costs._NativeNeighborhoods(points) for index in range(len(points)): assert np.array_equal(native(index), reference(index)), (dtype, offset, index) neighborhood_cases += 1 costs._NATIVE = None expected, count = costs.supported_slope_costs(points) costs._NATIVE = engine actual, native_count = costs.supported_slope_costs(points) assert native_count == count and np.array_equal(actual, expected) timings = {} if args.terrain: terrain = np.load(args.terrain) for name, implementation in (("reference", None), ("native", engine)): costs._NATIVE = implementation normalize = costs.TerrainCostNormalizer() timings[name] = [] for _ in range(5): started = time.monotonic() result, corrected = normalize(terrain) timings[name].append((time.monotonic() - started) * 1000) if name == "reference": expected, count = result, corrected else: assert np.array_equal(result, expected) and count == corrected report = dict(passed=True, cases=len(cases), neighborhoods=neighborhood_cases, timings_ms=timings) args.output.write_text(json.dumps(report)) print(json.dumps(report))