def testMissingMain(self): # make sure it gracefully fails when a template key is missing test_input = ["-c", MISSING_MAIN_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("missing the required 'main' section" in output)
def testBasinHopNonFloatMax(self): # test catching min greater than max test_input = ["-c", CONV_HOP_NONFLOAT_MAX_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("could not convert string" in output)
def testTplKeyInMain(self): # aim for a helpful message if the template key is in main test_input = ["-c", TPL_KEY_IN_MAIN_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue('Unexpected key' in output)
def testValBeforeSection(self): # make sure it gracefully fails when a template key is missing test_input = ["-c", VAL_BEFORE_SECTION_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue('must start with' in output)
def testBasinHopMaxMin(self): # test catching min greater than max test_input = ["-c", CONV_HOP_MAX_MIN_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("is not less than" in output)
def testExtraSection(self): # catch an error if the program finds an unexpected section test_input = ["-c", EXTRA_SEC_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue('not one of the valid section names' in output)
def testMissingFilledTplName(self): # new file name not specified by either config file or command line test_input = ["-c", MISSING_PAR_NAME] main(test_input) if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue(PAR_FILE_NAME in output)
def testHelp(self): test_input = ['-h'] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertFalse(output) with capture_stdout(main, test_input) as output: self.assertTrue("optional arguments" in output)
def testMissingTrialNameKey(self): test_input = ["-c", MISSING_TRIAL_NAME_KEY_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue(TRIAL_NAME in output) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE) silent_remove(PAR_OUT, disable=DISABLE_REMOVE)
def testMaxMin(self): # Stop based on step size try: test_input = ["-c", MAX_MIN_INI] main(test_input) self.assertFalse(diff_lines(PAR_OUT, GOOD_MAX_MIN_PAR_OUT)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testNoTrialName(self): # catch an error if the program finds an unexpected section test_input = ["-c", MISSING_TRIAL_NAME_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("Missing key name 'trial_name'" in output) silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testBasinHop(self): # Try hopping + minimization try: test_input = ["-c", CONV_HOP_INI] silent_remove(BIN_HOP_RESULT_SUM) silent_remove(SCRIPT_OUT) main(test_input) self.assertFalse(diff_lines(BIN_HOP_RESULT_SUM, GOOD_BIN_HOP_RESULT_SUM)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE) silent_remove(BIN_HOP_RESULT_SUM, disable=DISABLE_REMOVE)
def testMakeParStartLow(self): # For this test, there is exactly one value provided for each parameter, and x_0 is too low silent_remove(PAR_OUT) silent_remove(COPY_PAR) try: main(["-c", CONV_INI]) self.assertFalse(diff_lines(PAR_OUT, GOOD_PAR_OUT2)) self.assertFalse(diff_lines(COPY_PAR, GOOD_PAR_OUT2)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(COPY_PAR, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testNonTestedMethod(self): # Try alternate minimization method try: test_input = ["-c", CONV_NOT_TESTED_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) silent_remove(SCRIPT_OUT) with capture_stderr(main, test_input) as output: self.assertTrue("Only the following optimization methods" in output) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testBasinHopBounds(self): # Try hopping + minimization try: silent_remove(SCRIPT_OUT) test_input = ["-c", CONV_HOP_MIN_MAX_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) silent_remove(SCRIPT_OUT) with capture_stdout(main, test_input) as output: self.assertTrue("success condition satisfied. Number of function calls: 83" in output) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testMakeParStartHigh(self): # Testing that starting from an x_0 too high still ends at the same answer # Also check specifying par file name in command line; should overwrite what is in the config file try: silent_remove(ALT_PAR_FNAME) main(["-c", CONV_ALT_INI, "-f", ALT_PAR_FNAME]) self.assertFalse(diff_lines(ALT_PAR_FNAME, GOOD_PAR_OUT2)) self.assertFalse(diff_lines(RESID_PAR_OUT, GOOD_PAR_OUT2)) finally: silent_remove(ALT_PAR_FNAME, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE) silent_remove(RESID_PAR_OUT, disable=DISABLE_REMOVE) for file_name in OTHER_RESID_NAMES: silent_remove(os.path.join(DATA_DIR, file_name), disable=DISABLE_REMOVE)
def testNelderMead(self): # Try alternate minimization method try: silent_remove(SCRIPT_OUT) test_input = ["-c", CONV_NM_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) silent_remove(SCRIPT_OUT) with capture_stdout(main, test_input) as output: self.assertTrue("Function evaluations: 24" in output) self.assertTrue("vii_0: 0.000000" in output) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testMaxIterNum(self): # Specified a small number of iterations test_input = ["-c", CONV_MAX_ITER_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) try: with capture_stdout(main, test_input) as output: self.assertTrue("Maximum number of function evaluations has been exceeded" in output) diffs = diff_lines(PAR_OUT, GOOD_PAR_OUT2) self.assertEqual(len(diffs), 2) self.assertEqual('- -0.000000 : constant Vii', diffs[0]) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testNoOpt(self): # Testing that will run without any params specified to be optimized silent_remove(PAR_OUT) test_input = ["-c", PAR_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) try: with capture_stderr(main, test_input) as output: self.assertTrue("No parameters will be optimized" in output) self.assertFalse(diff_lines(PAR_OUT, GOOD_PAR_OUT2)) self.assertFalse(diff_lines(COPY_PAR, GOOD_PAR_OUT2)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(COPY_PAR, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testInitialDirections(self): # Start multi-variable test_input = ["-c", DIRS_INI] try: silent_remove(BEST_PARAMS) if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stdout(main, test_input) as output: # this option reduced the function calls by 1 (19 to 18) self.assertTrue("Function evaluations: 223" in output) self.assertFalse(diff_lines(BEST_PARAMS, GOOD_BEST_PARAMS)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE) silent_remove(BEST_PARAMS, disable=DISABLE_REMOVE)
def testCopyOutput(self): # Stop based on step size; multiple variables try: # since we backup RESULT_SUM, start by removing it so we don't accidentally make a copy silent_remove(RESULT_SUM) test_input = ["-c", COPY_OUTPUT_INI] main(test_input) self.assertFalse(diff_lines(SCRIPT_OUT, GOOD_SCRIPT_OUT)) self.assertFalse(diff_lines(SCRIPT_COPY_OUT, GOOD_SCRIPT_OUT)) self.assertFalse(diff_lines(RESULT_SUM, GOOD_RESULT_SUM)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(COPY_PAR, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_COPY_OUT, disable=DISABLE_REMOVE) silent_remove(RESULT_SUM, disable=DISABLE_REMOVE)
def testTriangleMin(self): # Test stepwise minimization with multiple minimization steps test_input = ["-c", TRIANGLE_INI] try: silent_remove(SCRIPT_OUT) if logger.isEnabledFor(logging.DEBUG): main(test_input) silent_remove(SCRIPT_OUT) with capture_stdout(main, test_input) as output: self.assertTrue("Resid: 34.416667 for parameters: 0.500000, 1.833333\n" in output) self.assertTrue("Resid: 4.496540 for parameters: 0.853871, 1.414121, -3.133996, " " 4.085879, 2.381966\n" in output) self.assertTrue("Function evaluations: 35\nOptimization terminated successfully. " "Completed 2 of 2 minimization cycles" in output) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testRepeatMin(self): # Test repeating minimization and removing duplicate opt_params test_input = ["-c", REPEAT_INI] try: silent_remove(BEST_PARAMS) silent_remove(SCRIPT_OUT) silent_remove(TEST_OUT) if logger.isEnabledFor(logging.DEBUG): main(test_input) silent_remove(BEST_PARAMS) silent_remove(SCRIPT_OUT) silent_remove(TEST_OUT) with capture_stdout(main, test_input) as output: self.assertTrue("Function evaluations: 25\nOptimization terminated successfully. " "Completed 2 of 3 minimization cycles" in output) self.assertFalse(diff_lines(BEST_PARAMS, GOOD_BEST_PARAMS)) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE) silent_remove(BEST_PARAMS, disable=DISABLE_REMOVE) silent_remove(TEST_OUT, disable=DISABLE_REMOVE)
def testNelderMeadMultiVar(self): # Try alternate minimization method for multiple variable. Did worse than Powell for multiple functions # Results from Powell are: # Current function value: 0.000000 # Iterations: 10 # Function evaluations: 242 # Optimized parameters: # vii_0 = 2.000000 # vij_0 = 0.000000 # gamma = -2.000000 try: test_input = ["-c", CONV_NM_MULTI_INI] silent_remove(SCRIPT_OUT) if logger.isEnabledFor(logging.DEBUG): main(test_input) silent_remove(SCRIPT_OUT) with capture_stdout(main, test_input) as output: self.assertTrue("Current function value: 6.192328" in output) self.assertTrue("Iterations: 29" in output) self.assertTrue("Function evaluations: 56" in output) finally: silent_remove(PAR_OUT, disable=DISABLE_REMOVE) silent_remove(SCRIPT_OUT, disable=DISABLE_REMOVE)
def testTwoParamVals(self): test_input = ["-c", TOO_MANY_PARAM_VALS_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("3 values were found" in output)
def testMissingBashScript(self): test_input = ["-c", MISSING_BASH_SCRIPT_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("Missing file" in output)
def testMissingResultFileName(self): test_input = ["-c", MISSING_RESULT_FNAME_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("bash driver output" in output)
def testNonfloatMin(self): test_input = ["-c", NON_FLOAT_MIN_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("convert string" in output)
def testTooManyMax(self): test_input = ["-c", TOO_MANY_MAX_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("Expected" in output)
def testNonFloatDir(self): test_input = ["-c", NON_FLOAT_DIR_INI] if logger.isEnabledFor(logging.DEBUG): main(test_input) with capture_stderr(main, test_input) as output: self.assertTrue("float" in output)