def jenkinsTests(): try: isHPCC = PathFinder.isHPCCRun() # Run just the matching cases for now testNames = ['steady', 'open_base', 'open_counter', 'closed_base', 'closed_counter'] for o in testNames: if o == 'steady': scenario = Scenario(ModelTester.test_params).currentPolicy().steady() elif o == 'open_base': scenario = Scenario(ModelTester.test_params).currentPolicy().open() elif o == 'open_counter': scenario = Scenario(ModelTester.test_params).open() elif o == 'closed_base': scenario = Scenario(ModelTester.test_params).currentPolicy().closed() elif o == 'closed_counter': scenario = Scenario(ModelTester.test_params).closed() else: scenario = [] typeDeviation = ModelTester.testOutput( scenario, o, 0 ) if typeDeviation != ModelTester.DEVIATION_NONE: if typeDeviation == ModelTester.DEVIATION_TINY and isHPCC: continue else: exit(1) # Test writing the 'series' interface with the last scenario # Requires that 'baseline' scenario exists PathFinder.setToTestingMode() print( 'TESTING OutputWriter.writeScenarios\n' ) ModelSolver.solve( scenario.baseline() ) OutputWriter.writeScenarios( [scenario] ) PathFinder.setToDevelopmentMode() print( 'ALL TESTS PASSED.\n' ) exit(0) except: exit(1)
def testOutput(scenario, testName, isInteractive): # Set to testing environment PathFinder.setToTestingMode() # Clear the old results and solve ModelSolver.removeCached(scenario) taggedDir = ModelSolver.solve(scenario) cacheDir = PathFinder.getCacheDir(scenario) # Set to development environment # TBD: Set back to original environment? PathFinder.setToDevelopmentMode() # testSet depends on type of scenario if( scenario.isSteady() ): setNames = ['market', 'dynamics'] elif( scenario.isCurrentPolicy() ): setNames = ['market', 'dynamics' ] else: setNames = ['market', 'dynamics', 'statics'] # Load target values targetfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ModelTester.pkl') with open(targetfile, 'rb') as handle: s = pickle.load(handle) target = s.target # Initialize match flag typeDeviation = ModelTester.DEVIATION_NONE # Define function to flag issues # NOTE: Relies on severity of deviation to be increasing def flag(str, deviation): print('\t%-15s%-20s%s\n' % (setname, valuename, str)) global typeDeviation if deviation > typeDeviation: typeDeviation = deviation print('\n[Test results]\n') for i in range(len(setNames)): # Extract output and target values by set setname = setNames[i] output = {} with open(os.path.join(cacheDir, ('%s.pkl' % setname)), 'rb') as handle: output[testName][setname] = pickle.load(handle) outputset = output[testName][setname] targetset = target[testName][setname] # Iterate over target values targetvaluenames = targetset.keys() for j in range(len(targetvaluenames)): valuename = targetvaluenames[j] if not valuename in outputset.keys(): # Flag missing value flag('Not found', ModelTester.DEVIATION_FATAL) continue if isinstance(outputset[valuename], dict): # Skip checking of structs -- it is currently just # priceindex which does not need to be checked print('\tSkipping %s because it is a struct.\n' % valuename) continue if np.any(np.isnan(outputset[valuename][:])): # Flag NaN value flag('NaN value', ModelTester.DEVIATION_FATAL) continue if np.any(outputset[valuename].shape != targetset[valuename].shape): # Flag for size mismatch flag('Size mismatch', ModelTester.DEVIATION_FATAL) continue # Classify deviation deviation = ModelTester.calculateDeviation(outputset[valuename][:], targetset[valuename][:]) if deviation > 0: if (deviation < 1e-6): msg = 'TINY : %06.16f%% deviation' % deviation*100 flag(msg, ModelTester.DEVIATION_TINY) elif deviation < 1e-4: msg = 'SMALL: %06.16f%% deviation' % deviation*100 flag( msg, ModelTester.DEVIATION_SMALL ) else: msg = 'LARGE: %06.4f%% deviation' % deviation*100 flag( msg, ModelTester.DEVIATION_FATAL ) # Identify new values, if any outputvaluenames = outputset.keys() for j in range(len(outputvaluenames)): valuename = outputvaluenames[j] if not valuename in targetset.keys(): flag('New', ModelTester.DEVIATION_FATAL) # Check for match if typeDeviation == ModelTester.DEVIATION_NONE: print('\tTarget matched.\n\n') else: if not isInteractive: print( '\tTarget not matched.\n\n' ) return # Query user for target update ans = input('\n\tUpdate test target with new values? Y/[N]: ') if ans == 'Y': target[testName] = output[testName] with open(targetfile) as f: pickle.dump(target, f) print('\tTarget updated.\n\n') else: print('\tTarget retained.\n\n') return typeDeviation