@@ -40,38 +40,102 @@ def compiled_workspaces(workspace: Workspace) -> dict[str, CompiledWorkspace]:
4040 }
4141
4242
43- def test_default_targets_are_checksum_targets (
44- compiled_workspaces : dict [str , CompiledWorkspace ],
45- ):
46- for compiled in compiled_workspaces .values ():
47- assert set (compiled .functions ) == set (CHECKSUM_IDS )
48- assert set (compiled .coordinate_maps ) == {"default_model" }
49-
50-
51- @pytest .mark .parametrize ("backend" , BACKENDS )
52- @pytest .mark .parametrize ("checksum" , CHECKSUMS , ids = CHECKSUM_IDS )
53- def test_reproduce_checksums (
54- compiled_workspaces : dict [str , CompiledWorkspace ],
55- model_definition : ModelDefinition ,
56- checksum : dict [str , Any ],
57- backend : str ,
58- ):
59- compiled = compiled_workspaces [backend ]
60- value = _evaluate (compiled , model_definition , checksum )
61- assert value == pytest .approx (_parse_checksum (checksum ["value" ]), rel = 1e-9 )
62-
43+ def describe_compile_workspace ():
44+ def it_uses_checksum_targets_by_default (
45+ compiled_workspaces : dict [str , CompiledWorkspace ],
46+ ):
47+ for compiled in compiled_workspaces .values ():
48+ assert set (compiled .functions ) == set (CHECKSUM_IDS )
49+ assert set (compiled .coordinate_maps ) == {"default_model" }
50+
51+ @pytest .mark .parametrize ("backend" , BACKENDS )
52+ @pytest .mark .parametrize ("checksum" , CHECKSUMS , ids = CHECKSUM_IDS )
53+ def it_reproduces_checksums (
54+ compiled_workspaces : dict [str , CompiledWorkspace ],
55+ model_definition : ModelDefinition ,
56+ checksum : dict [str , Any ],
57+ backend : str ,
58+ ):
59+ compiled = compiled_workspaces [backend ]
60+ value = _evaluate (compiled , model_definition , checksum )
61+ assert value == pytest .approx (_parse_checksum (checksum ["value" ]), rel = 1e-9 )
62+
63+ @pytest .mark .parametrize ("checksum" , CHECKSUMS , ids = CHECKSUM_IDS )
64+ def it_makes_backends_agree (
65+ compiled_workspaces : dict [str , CompiledWorkspace ],
66+ model_definition : ModelDefinition ,
67+ checksum : dict [str , Any ],
68+ ):
69+ values = {
70+ backend : _evaluate (compiled , model_definition , checksum )
71+ for backend , compiled in compiled_workspaces .items ()
72+ }
73+ assert values ["jax" ] == pytest .approx (values ["numpy" ], rel = 1e-12 )
6374
64- @pytest .mark .parametrize ("checksum" , CHECKSUMS , ids = CHECKSUM_IDS )
65- def test_backends_agree (
66- compiled_workspaces : dict [str , CompiledWorkspace ],
67- model_definition : ModelDefinition ,
68- checksum : dict [str , Any ],
69- ):
70- values = {
71- backend : _evaluate (compiled , model_definition , checksum )
72- for backend , compiled in compiled_workspaces .items ()
73- }
74- assert values ["jax" ] == pytest .approx (values ["numpy" ], rel = 1e-12 )
75+ def it_compiles_distribution_coordinates (model_definition : ModelDefinition ):
76+ workspace = load_workspace (model_definition )
77+ compiled = compile_workspace (
78+ workspace , backend = "numpy" , targets = ["default_model" ]
79+ )
80+ coordinates = compiled .coordinate_maps ["default_model" ]
81+ point = {"m_31" : 1.9101377207489973 , "cos_theta_31" : - 0.2309352648098208 }
82+ assert set (coordinates ) == {"sigma1" , "sigma2" , "sigma3" }
83+ assert all (float (function (point )) > 0 for function in coordinates .values ())
84+
85+ def it_compiles_a_named_function (workspace : Workspace ):
86+ compiled = compile_workspace (workspace , backend = "numpy" , targets = ["L1600_BW" ])
87+ assert compiled .coordinate_maps == {}
88+ assert compiled .coordinates == {}
89+
90+ def it_selects_distribution_coordinates (workspace : Workspace ):
91+ compiled = compile_workspace (
92+ workspace ,
93+ backend = "numpy" ,
94+ targets = ["default_model" ],
95+ coordinates = ["sigma2" , "sigma3" ],
96+ )
97+ assert compiled .coordinates ["default_model" ] == ("sigma2" , "sigma3" )
98+
99+ def it_overrides_distribution_parameters (workspace : Workspace ):
100+ model = workspace .distributions ["default_model" ]
101+ coupling_overrides = {
102+ symbol : 0
103+ for symbol in model .parameter_defaults
104+ if str (symbol ).startswith ("c^" )
105+ }
106+ compiled = compile_workspace (
107+ workspace ,
108+ backend = "numpy" ,
109+ targets = ["default_model" ],
110+ parameter_overrides = coupling_overrides ,
111+ )
112+ point = {"m_31" : 1.9101377207489973 , "cos_theta_31" : - 0.2309352648098208 }
113+ invariants = {
114+ name : function (point )
115+ for name , function in compiled .coordinate_maps ["default_model" ].items ()
116+ }
117+ value = compiled .functions ["default_model" ](invariants )
118+ assert float (value ) == pytest .approx (0 )
119+
120+ def it_rejects_unknown_backends_targets_and_coordinates (workspace : Workspace ):
121+ with pytest .raises (ValueError , match = "Unsupported numerical backend" ):
122+ compile_workspace (workspace , backend = "unknown" )
123+ with pytest .raises (KeyError , match = "missing" ):
124+ compile_workspace (workspace , backend = "numpy" , targets = ["missing" ])
125+ with pytest .raises (ValueError , match = "two distinct invariants" ):
126+ compile_workspace (
127+ workspace ,
128+ backend = "numpy" ,
129+ targets = ["default_model" ],
130+ coordinates = ["sigma1" ],
131+ )
132+
133+ def it_reports_a_missing_backend_package (
134+ workspace : Workspace , monkeypatch : pytest .MonkeyPatch
135+ ):
136+ monkeypatch .setattr ("importlib.util.find_spec" , lambda _ : None )
137+ with pytest .raises (ImportError , match = "requires the optional 'jax' package" ):
138+ compile_workspace (workspace , backend = "jax" , targets = ["L1600_BW" ])
75139
76140
77141def _evaluate (
@@ -125,70 +189,3 @@ def _parse_checksum(value: complex | str, /) -> complex:
125189 if isinstance (value , str ):
126190 return complex (value .replace (" " , "" ).replace ("i" , "j" ))
127191 return complex (value )
128-
129-
130- def test_compile_distribution_coordinates (model_definition : ModelDefinition ):
131- workspace = load_workspace (model_definition )
132- compiled = compile_workspace (workspace , backend = "numpy" , targets = ["default_model" ])
133- coordinates = compiled .coordinate_maps ["default_model" ]
134- point = {"m_31" : 1.9101377207489973 , "cos_theta_31" : - 0.2309352648098208 }
135- assert set (coordinates ) == {"sigma1" , "sigma2" , "sigma3" }
136- assert all (float (function (point )) > 0 for function in coordinates .values ())
137-
138-
139- def test_compile_named_function (workspace : Workspace ):
140- compiled = compile_workspace (workspace , backend = "numpy" , targets = ["L1600_BW" ])
141- assert compiled .coordinate_maps == {}
142- assert compiled .coordinates == {}
143-
144-
145- def test_select_distribution_coordinates (workspace : Workspace ):
146- compiled = compile_workspace (
147- workspace ,
148- backend = "numpy" ,
149- targets = ["default_model" ],
150- coordinates = ["sigma2" , "sigma3" ],
151- )
152- assert compiled .coordinates ["default_model" ] == ("sigma2" , "sigma3" )
153-
154-
155- def test_override_distribution_parameters (workspace : Workspace ):
156- model = workspace .distributions ["default_model" ]
157- coupling_overrides = {
158- symbol : 0 for symbol in model .parameter_defaults if str (symbol ).startswith ("c^" )
159- }
160- compiled = compile_workspace (
161- workspace ,
162- backend = "numpy" ,
163- targets = ["default_model" ],
164- parameter_overrides = coupling_overrides ,
165- )
166- point = {"m_31" : 1.9101377207489973 , "cos_theta_31" : - 0.2309352648098208 }
167- invariants = {
168- name : function (point )
169- for name , function in compiled .coordinate_maps ["default_model" ].items ()
170- }
171- value = compiled .functions ["default_model" ](invariants )
172- assert float (value ) == pytest .approx (0 )
173-
174-
175- def test_rejects_unknown_backend_and_target (workspace : Workspace ):
176- with pytest .raises (ValueError , match = "Unsupported numerical backend" ):
177- compile_workspace (workspace , backend = "unknown" )
178- with pytest .raises (KeyError , match = "missing" ):
179- compile_workspace (workspace , backend = "numpy" , targets = ["missing" ])
180- with pytest .raises (ValueError , match = "two distinct invariants" ):
181- compile_workspace (
182- workspace ,
183- backend = "numpy" ,
184- targets = ["default_model" ],
185- coordinates = ["sigma1" ],
186- )
187-
188-
189- def test_reports_missing_backend_package (
190- workspace : Workspace , monkeypatch : pytest .MonkeyPatch
191- ):
192- monkeypatch .setattr ("importlib.util.find_spec" , lambda _ : None )
193- with pytest .raises (ImportError , match = "requires the optional 'jax' package" ):
194- compile_workspace (workspace , backend = "jax" , targets = ["L1600_BW" ])
0 commit comments