"""Additional adversarial operator tests and durable unit-test record.""" import io import json from pathlib import Path import sys import unittest import numpy as np MODEL = Path('/data/digital-drosophila/models/male-cns-lif/terminal-scope-diagnostic-v1') EXP = Path('/data/digital-drosophila/experiments/phase4ab-terminal-scope') sys.path.insert(0, str(MODEL)) from scope import blocked_weights import diagnostic _, _, _, _, save, digest, _, _ = diagnostic.load_context() stream = io.StringIO() suite = unittest.defaultTestLoader.discover(str(MODEL), pattern='test_scope.py') result = unittest.TextTestRunner(stream=stream, verbosity=2).run(suite) assert result.wasSuccessful(), stream.getvalue() rng = np.random.default_rng(41337) raw = rng.integers(1, 100000, size=10000, dtype=np.int64) contacts = np.asarray([rng.integers(0, n+1) for n in raw], dtype=np.int64) base = (raw.astype(np.float32)*np.float32(.275)*rng.choice([-1, 0, 1], size=len(raw))).astype(np.float32) raw_bytes, base_bytes = raw.tobytes(), base.tobytes() out = blocked_weights(base, raw, contacts) assert np.isfinite(out).all() and np.all(np.abs(out) <= np.abs(base)) assert np.all(out[contacts == raw] == 0) assert out[contacts == 0].tobytes() == base[contacts == 0].tobytes() assert np.all(np.sign(out[out != 0]) == np.sign(base[out != 0])) assert raw_bytes == raw.tobytes() and base_bytes == base.tobytes() for bad in [np.array([np.inf], np.float32), np.array([np.nan], np.float32)]: try: blocked_weights(bad, np.array([2], np.int64), np.array([1], np.int64)) except ValueError: pass else: raise AssertionError('nonfinite accepted') save(EXP/'regression/unit-tests.json', {'pass': True, 'unit_tests': result.testsRun, 'fuzz_cases': len(raw), 'fuzz_seed': 41337, 'finite': True, 'never_sign_flip_or_amplify': True, 'input_nonmutation': True, 'zero_identity_bitwise': True, 'unittest_log': stream.getvalue(), 'source_sha256': digest(Path(__file__))}) print('PASS', result.testsRun, 'unit tests +', len(raw), 'randomized operator cases')