Files
digital-drosophila/registry/phase4aq-conditioning-execution/source-snapshot/test_boundary_sampler.py
2026-09-30 20:17:50 +08:00

83 lines
4.8 KiB
Python

"""Source/API and synthetic orchestration tests; never actual CUDA sampling."""
import copy
import hashlib
import importlib.util
import json
from pathlib import Path
import sys
import tempfile
import types
import unittest
from unittest.mock import patch
import numpy as np
import boundary_sampler as S
class SamplerTests(unittest.TestCase):
def test_actual_frozen_dependency_pins(self):
for name,sha in S.PINS.items():
with self.subTest(name=name):self.assertEqual(S.A.digest(S.LAB/name),sha)
def test_real_sampler_signature_lazy_GPU_and_request_window(self):
raw=(S.LAB/'conditioning_inputs.py').read_bytes()
module=types.ModuleType('_ci_cpu_fixture');exec(compile(raw,str(S.LAB/'conditioning_inputs.py'),'exec'),module.__dict__)
import inspect
self.assertEqual(list(inspect.signature(module.generate_cs_requests).parameters),
['targets','rates_hz','seed','steps','cs_start_tick','cs_stop_tick','device','max_uniform_draws','dt_ms'])
targets=np.array([5,2],np.int32);forced=np.ones((1200,2),np.bool_)
result=module.requests_from_forced(forced,targets,cs_start_tick=0,cs_stop_tick=1000)
self.assertEqual(len(result['requests']['tick']),2000)
self.assertEqual(int(result['requests']['tick'].max()),999)
self.assertTrue(np.array_equal(result['requests']['neuron_idx'][:2],np.array([2,5],np.int32)))
def run_fixture(self,*,mutate_seed=False):
"""Fake sampler is intentionally NOT a stochastic or neural validation."""
with tempfile.TemporaryDirectory() as td:
root=Path(td).resolve();(root/'protocol').mkdir();(root/'inputs/static').mkdir(parents=True);(root/'runs').mkdir()
regfile=root/'protocol/preregistered.json';full=root/'full.json';full.write_text('{}')
plan=S.PLAN.build_plan();tables={};streams={};calls=[]
for cue in ('A','B'):
tables[cue]=dict(targets=np.array([5,2],np.int32),rates_hz=np.array([10.,20.],np.float32))
with (root/f'inputs/static/{cue}-rates.npz').open('wb') as f:np.savez(f,**tables[cue])
stream=copy.deepcopy(plan['evaluation_streams'][cue]);stream['semantic_key']=stream.pop('key')
if mutate_seed and cue=='B':stream['seed']+=1
streams[(S.PLAN.BLOCK,cue)]=stream
reg=dict(schema='conditioning-execution-registration-v1',plan=plan,source_sha256={},prepared_file_sha256={},
input_sha256={str(S.LAB/n):sha for n,sha in S.PINS.items()},original_full_plan=S.A.pin(full))
regfile.write_text(json.dumps(reg))
ci=types.ModuleType('conditioning_inputs');i=types.ModuleType('conditioning_preflight_inputs');ep=types.ModuleType('evaluation_plan')
i.load_rate_tables=lambda root:dict(tables=tables)
ep._streams=lambda full:streams
def generate(targets,rates,**kw):
calls.append(kw)
self.assertEqual(kw['steps'],1200);self.assertEqual(kw['cs_start_tick'],0)
self.assertEqual(kw['cs_stop_tick'],1000);self.assertEqual(kw['max_uniform_draws'],2400)
return dict(requests=dict(tick=np.array([0,999],np.int32),neuron_idx=np.array([2,5],np.int32)),audit={'synthetic_fixture':True})
ci.generate_cs_requests=generate
real_import=__import__
def intercepted(name,*args,**kwargs):
if name in ('conditioning_inputs','conditioning_preflight_inputs','evaluation_plan'):
return {'conditioning_inputs':ci,'conditioning_preflight_inputs':i,'evaluation_plan':ep}[name]
return real_import(name,*args,**kwargs)
old_path=list(sys.path)
try:
with patch('builtins.__import__',side_effect=intercepted):
result=S.run(regfile,S.A.digest(regfile),root/'runs/generated')
self.assertEqual(len(calls),2);self.assertNotEqual(calls[0]['seed'],calls[1]['seed'])
self.assertEqual(result['uniform_draws'],4800);self.assertFalse(result['brain_executed'])
self.assertFalse(result['reuse_executed_here']);self.assertFalse(result['US_added'])
self.assertEqual(set(result['files_sha256']),{'A-requests.npz','A-sampler.json','B-requests.npz','B-sampler.json'})
with patch('builtins.__import__',side_effect=intercepted):
with self.assertRaisesRegex(ValueError,'Exclusive new sampler'):S.run(regfile,S.A.digest(regfile),root/'runs/generated')
return result
finally:sys.path[:]=old_path
def test_only_two_fixed_batches_and_honest_scope(self):self.run_fixture()
def test_wrong_full_semantic_seed_rejected(self):
with self.assertRaisesRegex(ValueError,'Evaluation semantic stream differs'):self.run_fixture(mutate_seed=True)
if __name__=='__main__':unittest.main()