Skip to content

Commit

Permalink
Merge pull request #45 from andrewbaxter439/bugfix/44_check_policySch…
Browse files Browse the repository at this point in the history
…edule

Don't automatically rebuild EUROMODpolicySchedule file and check for valid file.
  • Loading branch information
pbronka authored Dec 19, 2023
2 parents 256313e + 5891cb6 commit ff15c2f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/SimPathsBuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ jobs:
path: .

- name: Setup run
run: java -jar singlerun.jar -c UK -s 2017 -Setup -g false
run: java -jar singlerun.jar -c UK -s 2017 -Setup -g false --rewrite-policy-schedule

- name: Check input db exists
id: check_file
uses: thebinaryfelix/[email protected]
with:
files: 'input/input.mv.db, input/EUROMODpolicySchedule.xlsx, input/DatabaseCountryYear.xlsx'

- name: Do one run
run: java -jar multirun.jar -p 20000 -s 2017 -e 2020 -r 100 -n 2 -g false
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ To run the SimPathsStart setup phases and set up a population for subsequent mul
- `-c` Country ['UK' or 'IT']
- `-s` Start year
- `-g` [true/false] show/hide gui
- `-r` Re-write policy schedule from detected policy files
- `-Setup` do setup phases (creating input populations database) only

e.g.
Expand All @@ -70,13 +71,15 @@ For multiple runs, `multirun.jar` takes the following options:
- `-s` start year of runs
- `-e` end year of runs
- `-g` [true/false] show/hide gui
- `-f` write console output to file (in 'output/logs/run_[seed].txt')
- `-f` write console output and logs to file (in 'output/logs/run_[seed].txt')

e.g.
```
$ java -jar multirun.jar -r 100 -p 50000 -n 20 -s 2017 -e 2020 -g false -f
```

Run `java -jar singlerun.jar -h` or `java -jar multirun.jar -h` to show these help messages.

### Contributing

1. Create a new branch for your contributions. This will likely be based on either the `main` branch of this repository (if you seek to modify the stable version of the model) or `develop` (if you seek to modify the most recent version of the model). Please see branch naming convention below.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/simpaths/data/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,9 @@ public static TreeMap<Integer, String> calculateEUROMODpolicySchedule(Country co
MultiKey k = (MultiKey)o;
if(k.getKey(0) != null) {
String name = k.getKey(0).toString();
if(name != null) {
if(name != null &&
currentEUROMODpolicySchedule.getValue(name, EUROMODpolicyScheduleHeadingScenarioYearBegins) != null &&
currentEUROMODpolicySchedule.getValue(name, EUROMODpolicyScheduleHeadingScenarioSystemYear) != null) {
String policyStartYearString = currentEUROMODpolicySchedule.getValue(name, EUROMODpolicyScheduleHeadingScenarioYearBegins).toString();
String policySystemYearString = currentEUROMODpolicySchedule.getValue(name, EUROMODpolicyScheduleHeadingScenarioSystemYear).toString();
if(policyStartYearString != null && !policyStartYearString.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/simpaths/experiment/SimPathsMultiRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static boolean parseCommandLineArgs(String[] args) {
} catch (ParseException e) {
System.err.println("Error parsing command line arguments: " + e.getMessage());
formatter.printHelp("SimPathsMultiRun", options);
System.exit(1);
return false;
}

return true;
Expand Down
39 changes: 30 additions & 9 deletions src/main/java/simpaths/experiment/SimPathsStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import java.awt.Dimension;
import org.apache.commons.cli.*;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -67,6 +64,8 @@ public class SimPathsStart implements ExperimentBuilder {

private static boolean setupOnly = false;

private static boolean rewritePolicySchedule = false;


/**
*
Expand All @@ -85,7 +84,12 @@ public static void main(String[] args) {
// display dialog box to allow users to define desired simulation
runGUIdialog();
} else {
runGUIlessSetup(4);
try {
runGUIlessSetup(4);
} catch (FileNotFoundException f) {
System.err.println(f.getMessage());
System.exit(1);
};
}

if (setupOnly) {
Expand Down Expand Up @@ -130,6 +134,9 @@ private static boolean parseCommandLineArgs(String[] args) {
Option setupOption = new Option("Setup", "Setup only");
options.addOption(setupOption);

Option rewritePolicyScheduleOption = new Option("r", "rewrite-policy-schedule",false, "Re-write policy schedule from detected policy files");
options.addOption(rewritePolicyScheduleOption);

Option guiOption = new Option("g", "showGui", true, "Show GUI");
guiOption.setArgName("true/false");
options.addOption(guiOption);
Expand All @@ -154,7 +161,11 @@ private static boolean parseCommandLineArgs(String[] args) {
}

if (cmd.hasOption("c")) {
country = Country.valueOf(cmd.getOptionValue("c"));
try {
country = Country.valueOf(cmd.getOptionValue("c"));
} catch (Exception e) {
throw new IllegalArgumentException("Code '" + cmd.getOptionValue("c") + "' not a valid country.");
}
}

if (cmd.hasOption("s")) {
Expand All @@ -164,10 +175,14 @@ private static boolean parseCommandLineArgs(String[] args) {
if (cmd.hasOption("Setup")) {
setupOnly = true;
}

if (cmd.hasOption("r")) {
rewritePolicySchedule = true;
}
} catch (ParseException | IllegalArgumentException e) {
System.err.println("Error parsing command line arguments: " + e.getMessage());
formatter.printHelp("SimPathsStart", options);
System.exit(1);
return false;
}

return true;
Expand Down Expand Up @@ -205,7 +220,7 @@ public void buildExperiment(SimulationEngine engine) {
model.setCollector(collector);
}

private static void runGUIlessSetup(int option) {
private static void runGUIlessSetup(int option) throws FileNotFoundException {

// Detect if data available; set to testing data if not
Collection<File> testList = FileUtils.listFiles(new File(Parameters.getInputDirectoryInitialPopulations()), new String[]{"csv"}, false);
Expand All @@ -217,7 +232,13 @@ private static void runGUIlessSetup(int option) {
Parameters.setTaxDonorInputFileName(taxDonorInputFilename);

// Create EUROMODPolicySchedule input from files
writePolicyScheduleExcelFile();
if (!rewritePolicySchedule &&
!new File("input" + File.separator + Parameters.EUROMODpolicyScheduleFilename + ".xlsx").exists()) {
throw new FileNotFoundException("Policy Schedule file '"+ File.separator + "input" + File.separator +
Parameters.EUROMODpolicyScheduleFilename + ".xlsx` doesn't exist. " +
"Provide excel file or use `--rewrite-policy-schedule` to re-construct from available policy files.");
};
if (rewritePolicySchedule) writePolicyScheduleExcelFile();
//Save the last selected country and year to Excel to use in the model if GUI launched straight away
String[] columnNames = {"Country", "Year"};
Object[][] data = new Object[1][columnNames.length];
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/simpaths/experiment/SimPathsStartTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void setUpStreams() {
@BeforeEach
public void resetStreams() {
outContent.reset();
// errContent.reset();
errContent.reset();
}

@Test
Expand All @@ -47,5 +47,13 @@ public void testSimPathsStartHelpOptionWithOtherArguments() {
assertEquals("", errContent.toString().trim());
}

// Add more test methods for other scenarios as needed
@Test
public void testBadCountryName() {
String[] args = {"-g", "false", "-s", "2017", "-c", "XX"};

String expectedError = "Code 'XX' not a valid country.";

SimPathsStart.main(args);
assertTrue(errContent.toString().contains(expectedError));
}
}

0 comments on commit ff15c2f

Please sign in to comment.