Buckets:
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| import numpy as np | |
| from real2sim.error_trace import MaskErrorTrace | |
| class ErrorTraceTests(unittest.TestCase): | |
| def make_trace(self, observed=None): | |
| target = np.zeros((3, 2, 4, 4), dtype=bool) | |
| simulated = target.copy() | |
| target[2, 0, 1, 2] = True | |
| simulated[1, 0, 1, 2] = True | |
| return MaskErrorTrace( | |
| target, | |
| simulated, | |
| np.full((3, 2), 0.9), | |
| np.ones((3, 2), bool) if observed is None else observed, | |
| [0.0, 0.1], | |
| [30, 33], | |
| geom_ids=np.full((2, 4, 4), 7), | |
| geom_mapping={"7": {"name": "right_finger", "actor": "right"}}, | |
| ) | |
| def test_pixel_traces_both_sides_of_actor_mismatch(self): | |
| query = self.make_trace().query(30, 2, 1) | |
| self.assertEqual(query["real_object"], "object") | |
| self.assertEqual(query["sim_object"], "right") | |
| self.assertEqual(query["sim_geom"]["name"], "right_finger") | |
| self.assertTrue(query["actor_errors"]["object"]["false_negative"]) | |
| self.assertTrue(query["actor_errors"]["right"]["false_positive"]) | |
| self.assertFalse(query["physical_cause_identified"]) | |
| def test_unknown_tracks_do_not_become_trusted_errors(self): | |
| trace = self.make_trace(observed=np.zeros((3, 2), bool)) | |
| self.assertTrue(trace.query(30, 2, 1)["disagreement"]) | |
| self.assertFalse(trace.query(30, 2, 1)["trusted_disagreement"]) | |
| self.assertIsNone(trace.summary()["worst_actor"]) | |
| def test_saved_trace_retains_pixel_identity(self): | |
| original = self.make_trace() | |
| with tempfile.TemporaryDirectory() as folder: | |
| path = Path(folder) / "trace.npz" | |
| original.save(path) | |
| loaded = MaskErrorTrace.load(path) | |
| self.assertEqual(original.query(30, 2, 1), loaded.query(30, 2, 1)) | |
| def test_pixel_and_source_frame_bounds(self): | |
| trace = self.make_trace() | |
| for frame, x, y in [(31, 2, 1), (30, 4, 1), (30, 2, -1)]: | |
| with self.assertRaises(ValueError): | |
| trace.query(frame, x, y) | |
| class TargetedRefinementTests(unittest.TestCase): | |
| def setUpClass(cls): | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "examples")) | |
| from refine_physics import ( | |
| block_is_admissible, | |
| candidate_is_acceptable, | |
| include_query_frame, | |
| ) | |
| cls.block_is_admissible = staticmethod(block_is_admissible) | |
| cls.candidate_is_acceptable = staticmethod(candidate_is_acceptable) | |
| cls.include_query_frame = staticmethod(include_query_frame) | |
| def test_total_gain_cannot_hide_worsening_selected_actor(self): | |
| baseline = {"stable": True, "loss": 0.5, "actor_losses": [0.3, 0.7, 0.5]} | |
| candidate = {"stable": True, "loss": 0.45, "actor_losses": [0.3, 0.75, 0.3]} | |
| self.assertFalse(self.candidate_is_acceptable(candidate, baseline, 1)) | |
| self.assertTrue(self.candidate_is_acceptable(candidate, baseline, 2)) | |
| def test_unrelated_parameter_block_is_excluded(self): | |
| baseline = {"stable": True, "loss": 0.5, "actor_losses": [0.3, 0.7, 0.5]} | |
| candidate = {"stable": True, "loss": 0.45, "actor_losses": [0.3, 0.7, 0.3]} | |
| self.assertFalse(self.block_is_admissible("friction", [candidate], baseline, 0)) | |
| self.assertTrue(self.block_is_admissible("friction", [candidate], baseline, 2)) | |
| def test_queried_frame_is_included_between_sampled_frames(self): | |
| selected = self.include_query_frame( | |
| np.array([0, 2]), np.array([30, 33, 36]), 33 | |
| ) | |
| np.testing.assert_array_equal(selected, [0, 1, 2]) | |
| if __name__ == "__main__": | |
| unittest.main() | |
Xet Storage Details
- Size:
- 3.77 kB
- Xet hash:
- 09b4a1299d044784cbc30534af0dcd4795bf485d0bf95c5a106966c12ca7a5e0
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.