@@ -228,6 +228,154 @@ def test_background_sheet(tmpdir, monkeypatch):
228228 assert dict_design ["defaultvalues" ]["extraseed" ] == 0
229229
230230
231+ def _write_background_workbook (filename , background , corr_matrix , background_name ):
232+ """Write a workbook with a background sheet and a 'bgcorr' correlation sheet."""
233+ general_input = pd .DataFrame (
234+ data = [
235+ ["designtype" , "onebyone" ],
236+ ["repeats" , 3 ],
237+ ["rms_seeds" , "default" ],
238+ ["background" , background_name ],
239+ ["distribution_seed" , 42 ],
240+ ]
241+ )
242+ defaultvalues = pd .DataFrame (
243+ columns = ["param_name" , "default_value" ], data = [["PARAM_A" , 0 ], ["PARAM_B" , 0 ]]
244+ )
245+ with pd .ExcelWriter (filename , engine = "openpyxl" ) as writer :
246+ general_input .to_excel (
247+ writer , sheet_name = "general_input" , index = False , header = None
248+ )
249+ MOCK_DESIGNINPUT .to_excel (
250+ writer , sheet_name = "designinput" , index = False , header = None
251+ )
252+ defaultvalues .to_excel (writer , sheet_name = "defaultvalues" , index = False )
253+ background .to_excel (
254+ writer , sheet_name = "backgroundsheet" , index = False , header = None
255+ )
256+ if corr_matrix is not None :
257+ corr_matrix .to_excel (writer , sheet_name = "bgcorr" )
258+
259+
260+ BACKGROUND_WITH_CORR = pd .DataFrame (
261+ data = [
262+ ["param_name" , "dist_name" , "dist_param1" , "dist_param2" , "corr_sheet" ],
263+ ["PARAM_A" , "uniform" , 0 , 1 , "bgcorr" ],
264+ ["PARAM_B" , "uniform" , 0 , 1 , "bgcorr" ],
265+ ]
266+ )
267+
268+
269+ def test_background_correlation_sheet_with_mismatching_index_and_columns (
270+ tmpdir , monkeypatch
271+ ):
272+ """Correlation matrices for background parameters must be validated the same
273+ way as correlation matrices for ordinary sensitivities."""
274+ monkeypatch .chdir (tmpdir )
275+ corr_matrix = pd .DataFrame (
276+ [[1.0 , np .nan ], [0.5 , 1.0 ]],
277+ index = ["PARAM_A" , "PARAM_B" ],
278+ columns = ["PARAM_A" , "PARAM_TYPO" ],
279+ )
280+ _write_background_workbook (
281+ "designinput.xlsx" , BACKGROUND_WITH_CORR , corr_matrix , "backgroundsheet"
282+ )
283+
284+ with pytest .raises (
285+ ValueError , match = "Mismatch between column and index in correlation"
286+ ):
287+ excel_to_dict ("designinput.xlsx" )
288+
289+
290+ def test_background_correlation_sheet_with_mismatching_parameters (tmpdir , monkeypatch ):
291+ """Parameters pointing to a correlation sheet from the background sheet must
292+ match the parameters in that correlation sheet exactly."""
293+ monkeypatch .chdir (tmpdir )
294+ corr_matrix = pd .DataFrame (
295+ [[1.0 , np .nan ], [0.5 , 1.0 ]],
296+ index = ["PARAM_A" , "PARAM_TYPO" ],
297+ columns = ["PARAM_A" , "PARAM_TYPO" ],
298+ )
299+ _write_background_workbook (
300+ "designinput.xlsx" , BACKGROUND_WITH_CORR , corr_matrix , "backgroundsheet"
301+ )
302+
303+ with pytest .raises (ValueError , match = "Mismatch between parameters" ):
304+ excel_to_dict ("designinput.xlsx" )
305+
306+
307+ def test_background_sheet_that_does_not_exist (tmpdir , monkeypatch ):
308+ """A background sheet name that does not exist must be reported, listing the
309+ sheets that are available."""
310+ monkeypatch .chdir (tmpdir )
311+ _write_background_workbook (
312+ "designinput.xlsx" , BACKGROUND_WITH_CORR , None , "typo_sheet"
313+ )
314+
315+ with pytest .raises (ValueError , match = "Sheets in workbook" ) as exc_info :
316+ excel_to_dict ("designinput.xlsx" )
317+
318+ message = str (exc_info .value )
319+ assert "typo_sheet" in message
320+ assert "backgroundsheet" in message
321+ assert "Use 'None' as background" in message
322+
323+
324+ @pytest .mark .parametrize (
325+ "background_name" , ["Backgroundsheet" , "background_sheet" , " backgroundsheet " ]
326+ )
327+ def test_background_sheet_name_is_matched_softly (tmpdir , monkeypatch , background_name ):
328+ monkeypatch .chdir (tmpdir )
329+ corr_matrix = pd .DataFrame (
330+ [[1.0 , np .nan ], [0.5 , 1.0 ]],
331+ index = ["PARAM_A" , "PARAM_B" ],
332+ columns = ["PARAM_A" , "PARAM_B" ],
333+ )
334+ _write_background_workbook (
335+ "designinput.xlsx" , BACKGROUND_WITH_CORR , corr_matrix , background_name
336+ )
337+
338+ background = excel_to_dict ("designinput.xlsx" )["background" ]
339+ assert list (background ["parameters" ]) == ["PARAM_A" , "PARAM_B" ]
340+
341+
342+ def test_background_file_that_does_not_exist (tmpdir , monkeypatch ):
343+ monkeypatch .chdir (tmpdir )
344+ _write_background_workbook (
345+ "designinput.xlsx" , BACKGROUND_WITH_CORR , None , "missing_background.csv"
346+ )
347+
348+ with pytest .raises (ValueError , match = "Failed to resolve path" ):
349+ excel_to_dict ("designinput.xlsx" )
350+
351+
352+ @pytest .mark .parametrize ("background_name" , ["None" , "none" , np .nan ])
353+ def test_background_not_in_use (tmpdir , monkeypatch , background_name ):
354+ """Not specifying a background must not be an error."""
355+ monkeypatch .chdir (tmpdir )
356+ _write_background_workbook (
357+ "designinput.xlsx" , BACKGROUND_WITH_CORR , None , background_name
358+ )
359+
360+ assert excel_to_dict ("designinput.xlsx" )["background" ] is None
361+
362+
363+ def test_background_correlations_are_read (tmpdir , monkeypatch ):
364+ monkeypatch .chdir (tmpdir )
365+ corr_matrix = pd .DataFrame (
366+ [[1.0 , np .nan ], [0.5 , 1.0 ]],
367+ index = ["PARAM_A" , "PARAM_B" ],
368+ columns = ["PARAM_A" , "PARAM_B" ],
369+ )
370+ _write_background_workbook (
371+ "designinput.xlsx" , BACKGROUND_WITH_CORR , corr_matrix , "backgroundsheet"
372+ )
373+
374+ background = excel_to_dict ("designinput.xlsx" )["background" ]
375+ assert background ["correlations" ]["sheetnames" ] == ["bgcorr" ]
376+ assert list (background ["parameters" ]) == ["PARAM_A" , "PARAM_B" ]
377+
378+
231379def test_assert_no_merged_cells (tmpdir , monkeypatch ):
232380 """Test that assert_no_merged_cells detects merged cells"""
233381 monkeypatch .chdir (tmpdir )
0 commit comments