Skip to content

Experimental/safe test parameters from excel files#473

Merged
justin-ven merged 4 commits into
developfrom
experimental/safe-test-parameters-from-excel-files
May 26, 2026
Merged

Experimental/safe test parameters from excel files#473
justin-ven merged 4 commits into
developfrom
experimental/safe-test-parameters-from-excel-files

Conversation

@andrewbaxter439
Copy link
Copy Markdown
Collaborator

What

  • A proposed potential 'safety test' of regressors as files are read-in

Why

  • When reading in a regression file which should contain findable enums this should check that they all load at the very start of the model running
  • Otherwise it will crash much later with non-traceable messages
  • So this would save time and prompt user to check specified row/columns in reg sheets before proceeding with any part of the run

Before merging

  • sense-check whether this is a good change and whether the safeReadExcel wrapper should replace all ExcelAssistant.loadCoefficientMap reads for all regression files?

Copy link
Copy Markdown
Collaborator Author

@andrewbaxter439 andrewbaxter439 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have noted some things that might be worth looking at specifically!

Comment on lines +1143 to 1144
validateRegressors(coeffCovarianceHM2CaseMales, "reg_health_mental.xlsx", "HM2_Males_C");

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should come out as was put in to test. Will be run automatically if safeReadExcel is implemented.

Suggested change
validateRegressors(coeffCovarianceHM2CaseMales, "reg_health_mental.xlsx", "HM2_Males_C");

Comment on lines +3249 to +3254
try {
Person.DoublesVariables.valueOf(keyName);
} catch (IllegalArgumentException e) {
try {
BenefitUnit.Regressors.valueOf(keyName);
} catch (IllegalArgumentException e2) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hacky test - checking whether looking up both Person and BenefitUnit enums errors out. The error IllegalArgumentException isn't the best fit, perhaps needs to go deeper to get a better error in each of these cases, or do a more literal check of what enum values are present in each set?

Or leave as-is if checking valueOf and returning IllegalArgumentException is a good standard way of testing?

Comment on lines +18 to +20
String[] badValueVector = new String[] {"Dag", "Not_a_valid_value"};
String[] goodValueVector = new String[] {"Dag", "D_Home_owner", "PovertyToNonPoverty"};
String[] keyVector = new String[] {"REGRESSOR"};
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'good' values depend on stable enums being in Person.java (e.g. D_Home_owner). If this changes, the test will fail, though the method would still work correctly.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an early “safety check” for regression Excel inputs by validating that regressor names loaded into MultiKeyCoefficientMap correspond to known model enums, aiming to fail fast with clearer diagnostics.

Changes:

  • Added Parameters.validateRegressors(...) to verify regressor keys against Person.DoublesVariables and BenefitUnit.Regressors.
  • Added Parameters.safeReadExcel(...) wrappers that load coefficient maps and immediately validate regressors.
  • Added a new unit test covering valid vs invalid regressor names.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/main/java/simpaths/data/Parameters.java Introduces regressor validation + “safe” Excel read wrappers and applies validation to one loaded regression map.
src/test/java/simpaths/data/ParametersTest.java Adds a unit test for the new regressor validation logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

coeffCovarianceHM2CaseMales = ExcelAssistant.loadCoefficientMap(Parameters.getInputDirectory() + "reg_health_mental.xlsx", countryString + "_HM2_Males_C", 1);
coeffCovarianceHM2CaseFemales = ExcelAssistant.loadCoefficientMap(Parameters.getInputDirectory() + "reg_health_mental.xlsx", countryString + "_HM2_Females_C", 1);

validateRegressors(coeffCovarianceHM2CaseMales, "reg_health_mental.xlsx", "HM2_Males_C");
// Check across all
for (Object regressor : regressorNames) {
if (regressor instanceof MultiKey mk) {
String keyName = mk.getKey(0).toString();
Comment on lines +3256 to +3259
// This fires if the string isn't in the Enum
throw new RuntimeException("Validation failed for " + excelFileName + " in " + sheetName +
": Regressor '" + keyName + "' not found in Person.DoublesVariables. " +
"Check for typos in Excel or missing Enums in Person.java.");
Comment on lines +6 to +7
import java.util.Arrays;

Comment on lines +18 to +29
String[] badValueVector = new String[] {"Dag", "Not_a_valid_value"};
String[] goodValueVector = new String[] {"Dag", "D_Home_owner", "PovertyToNonPoverty"};
String[] keyVector = new String[] {"REGRESSOR"};

MultiKeyCoefficientMap badMap = new MultiKeyCoefficientMap(keyVector, badValueVector);
for (String badValue : badValueVector) {badMap.putValue(badValue, 0);}

MultiKeyCoefficientMap goodMap = new MultiKeyCoefficientMap(keyVector, goodValueVector);
for (String goodValue : goodValueVector) {goodMap.putValue(goodValue, 0);}

assertThrows(RuntimeException.class, () -> Parameters.validateRegressors(badMap, "A map designed to contain invalid values", "Sheet1"));
assertDoesNotThrow(() -> Parameters.validateRegressors(goodMap, "A map designed to contain valid values", "Sheet1"));
@justin-ven
Copy link
Copy Markdown
Contributor

Looks like a really useful addition

@justin-ven justin-ven merged commit f2693b1 into develop May 26, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants