41 lines
2.7 KiB
Python
41 lines
2.7 KiB
Python
import tempfile,unittest,json
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
import numpy as np
|
|
import phase2_e2_postfit as p
|
|
|
|
class Postfit(unittest.TestCase):
|
|
def test_budget_includes_live_full_job_reservations(self):
|
|
self.assertTrue(p.admission(4000,3,1800));self.assertFalse(p.admission(7100,3,1800));self.assertFalse(p.admission(14300,0,120))
|
|
def test_missing_arm_keeps_native_nan(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
f=Path(tmp)/'extra.npy';np.save(f,np.full((2,2),np.nan));e={'c':{'arms':{'T':{'path':str(f),'sha256':p.sha(f),'valid':False}}}}
|
|
self.assertTrue(np.isnan(p.extras({'case':'c','nodes':2},'T',e)).all())
|
|
e['c']['arms']['T']['valid']=True
|
|
with self.assertRaises(ValueError):p.extras({'case':'c','nodes':2},'T',e)
|
|
def test_query_identity_checks_multiplicity_and_wall(self):
|
|
q=dict(raw_ids=np.array([2,4]),counts=np.array([1,3]),coordinates_over_Rg=np.zeros((2,3)),near_wall=np.array([True,False]));p.query_identity(q,q)
|
|
for key in ['counts','near_wall','raw_ids']:
|
|
z={k:v.copy() for k,v in q.items()};z[key]=z[key][::-1]
|
|
with self.assertRaises(ValueError):p.query_identity(q,z)
|
|
def test_no_score_target_access_before_freeze(self):
|
|
with patch.object(p,'verify',side_effect=ValueError('unfrozen')),patch.object(p,'cached') as cache:
|
|
with self.assertRaises(ValueError):p.score(Path('/nonexistent'),{})
|
|
cache.assert_not_called()
|
|
def test_train_uses_actual_fold_and_full_missing_rows(self):
|
|
class Booster:
|
|
def save_config(self):return '{}'
|
|
class Model:
|
|
def fit(self,X,Y,sample_weight):
|
|
self.X=X.copy();self.Y=Y.copy();self.w=sample_weight.copy()
|
|
def save_model(self,path):Path(path).write_bytes(b'synthetic-model')
|
|
def get_booster(self):return Booster()
|
|
model=Model();row=dict(case='case',nodes=2,v5_cache=dict(output_sha256={'target.npy':'target','raw_ids.npy':'raw'}));c={'folds':[{}, {}, {'rows':{'learning':[row]}}]}
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);(root/'features-manifest.json').write_text(json.dumps({'entries':[{'case':'case'}]}));data=dict(features=np.ones((2,19)),raw_ids=np.array([1,3]),target=np.array([.2,.4]))
|
|
with patch.object(p,'verify'),patch.object(p,'cached',side_effect=lambda row,k:data[k]),patch.object(p,'extras',return_value=np.full((2,2),np.nan)),patch.object(p,'xgb_model',return_value=model) as factory:p.train(root,c,4)
|
|
factory.assert_called_once_with(0,1);self.assertEqual(model.X.shape,(2,21));self.assertTrue(np.isnan(model.X[:,19:]).all());np.testing.assert_allclose(model.Y,[.2,.4]);np.testing.assert_array_equal(model.w,[1,1])
|
|
a=p.read(root/'cells/04/train/target-access.json')[0];self.assertEqual((a['fold'],a['role'],a['arm']),(2,'learning','T'))
|
|
|
|
if __name__=='__main__':unittest.main()
|