Spaces:
Running
Running
Download test_models.py from minjiechen/ThinkPower: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/spaces/minjiechen/ThinkPower/resolve/main/test_models.py
- Command line
-
hf download hf://spaces/minjiechen/ThinkPower/test_models.py
-
curl -L -o test_models.py https://huggingface.co/spaces/minjiechen/ThinkPower/resolve/main/test_models.py
3.19 kB
| import unittest,math,io,base64,os | |
| from unittest.mock import patch | |
| from PIL import Image | |
| from models import analyze,evaluate,valid,state_model,control | |
| from agent import inspect_image | |
| class ModelsTest(unittest.TestCase): | |
| def test_paper_reference(self): | |
| d=analyze({});values={r['id']:r['total']*1000 for r in d['rows']} | |
| expected={'xfly2p':20,'xfly4p':26.4235032770828,'3lbuck':27.4401693585629,'2sbuck':26.0078105935821,'buck':22.4401693585629} | |
| for k,v in expected.items():self.assertAlmostEqual(values[k],v,places=8) | |
| def test_asymptotes_not_added(self): | |
| r=next(r for r in analyze({})['rows'] if r['id']=='2sbuck') | |
| self.assertAlmostEqual(r['total'],.01+math.hypot(.01,.0125)) | |
| self.assertNotAlmostEqual(r['total'],r['fsl']+r['ssl']) | |
| def test_overlap_continuity(self): | |
| p=analyze({})['parameters'];a=evaluate('xfly2p',.25,p);b=evaluate('xfly2p',.25000000001,p) | |
| self.assertAlmostEqual(a['total'],b['total'],places=9) | |
| self.assertGreater(evaluate('xfly2p',.4,p)['ssl'],0) | |
| def test_ratios_domains(self): | |
| for n in [2,4,6,8,16]: | |
| d=analyze({'ratio':n,'topologies':['xfly2p','xfly4p','3lbuck','2sbuck','buck','boost','ns']}) | |
| self.assertEqual(d['M'],1/n) | |
| for r in d['rows']: | |
| if r['valid']:self.assertTrue(math.isfinite(r['total'])) | |
| if n==2:self.assertFalse(d['rows'][0]['valid']);self.assertFalse(d['rows'][1]['valid']) | |
| self.assertFalse(d['rows'][-1]['valid']);self.assertFalse(d['rows'][-2]['valid']) | |
| def test_area_budget(self): | |
| for r in analyze({})['rows']:self.assertAlmostEqual(sum(r['allocation']['areas'])*2,12) | |
| def test_invalid_inputs(self): | |
| for p in [{'cf_nf':0},{'fs_mhz':0},{'ratio':float('nan')},{'rs_mohm':-1},{'topologies':['unknown']},{'topologies':[]}]: | |
| with self.assertRaises(ValueError):analyze(p) | |
| def test_state_allocation(self): | |
| states=[{'fraction':.25,'switch_currents':[.5,.5,0,0,0,0,.5,.5],'capacitor_currents':[],'inductor_currents':[.5,.5]}, {'fraction':.25,'switch_currents':[0,0,.5,.5,.5,.5,0,0],'capacitor_currents':[],'inductor_currents':[.5,.5]}, {'fraction':.5,'switch_currents':[0,0,.5,.5,0,0,.5,.5],'capacitor_currents':[],'inductor_currents':[.5,.5]}] | |
| r=state_model({'states':states});self.assertAlmostEqual(r['rfet'],(2+math.sqrt(3))*.02/6);self.assertAlmostEqual(sum(r['optimal_areas']),12) | |
| def test_charge_imbalance_rejected(self): | |
| with self.assertRaisesRegex(ValueError,'charge balance'):state_model({'states':[{'fraction':1,'switch_currents':[1],'capacitor_currents':[1]}]}) | |
| def test_control_reference(self): | |
| c=control({});self.assertAlmostEqual(c['phase_margin'],79.95,places=2);self.assertEqual(c['unity_crossings'],1);self.assertTrue(21000<c['f_dm']<23000) | |
| def test_image_no_fake_recognition(self): | |
| im=Image.new('RGB',(30,30),'white');b=io.BytesIO();im.save(b,format='PNG') | |
| with patch.dict(os.environ,{},clear=True):r=inspect_image({'image':'data:image/png;base64,'+base64.b64encode(b.getvalue()).decode()}) | |
| self.assertFalse(r['configured']);self.assertEqual(r['candidate_id'],'unknown');self.assertEqual(r['components'],[]) | |
| def test_malformed_image(self): | |
| with self.assertRaises(ValueError):inspect_image({'image':'data:image/svg+xml;base64,xxxx'}) | |
| if __name__=='__main__':unittest.main() | |