def test_getModuleConfig(): '''Test for module getModuleConfig''' avalancheDir = os.path.dirname(__file__) # test with both a default and local .ini file cfg = cfgUtils.getModuleConfig(test_logUtils) sections = cfg.sections() assert sections == ['GENERAL', 'FLAGS', 'GOODSECTION1', 'GOODSECTION2', 'BADSECTION1'] assert cfg['GENERAL']['inputDir'] == 'path/to/avalanche' assert cfg['GENERAL']['fullLog'] == 'True' assert cfg['GOODSECTION1']['goodKey1'] == '1' assert cfg['GOODSECTION1']['goodKey2'] == 'False' hasKey = cfg.has_option('GOODSECTION1', 'badKey1') assert hasKey == False # test reading a different file filename = os.path.join(avalancheDir, 'local_test_logUtilsCfg.ini') cfg = cfgUtils.getModuleConfig(test_logUtils, fileOverride=filename) sections = cfg.sections() assert sections == ['GENERAL', 'FLAGS', 'GOODSECTION1', 'GOODSECTION2', 'BADSECTION1'] assert sections != ['GENERAL', 'FLAGS', 'GOODSECTION1', 'BADSECTION1'] assert cfg['GENERAL']['inputDir'] == 'path/to/avalanche' assert cfg['GENERAL']['fullLog'] == 'True' assert cfg['GOODSECTION1']['goodKey1'] == '1' assert cfg['GOODSECTION1']['goodKey2'] == 'False' assert cfg['GOODSECTION2']['goodKey3'] == '0' assert cfg['GOODSECTION2']['goodKey4'] == 'False'
def test_com1DFAMain(tmp_path, testName): """ test call to com1DFA module """ # get path to executable cfgCom1DFA = cfgUtils.getModuleConfig(com1DFA) com1Exe = cfgCom1DFA['GENERAL']['com1Exe'] # load all benchmark info as dictionaries from description files testDictList = tU.readAllBenchmarkDesDicts(info=False) for test in testDictList: if test['NAME'] == testName: testAva = test avaTestName = testAva['NAME'] avaName = testAva['AVANAME'] # get input data dirPath = os.path.dirname(__file__) avaDir = os.path.join(tmp_path, avaName) avaInputs = os.path.join(avaDir, 'Inputs') avaData = os.path.join(dirPath, '..', 'data', avaName, 'Inputs') testCfg = os.path.join(dirPath, '..', '..', 'benchmarks', avaTestName, '%s_com1DFACfg.ini' % avaName) shutil.copytree(avaData, avaInputs) # get configuration cfg = cfgUtils.getModuleConfig(com1DFA, testCfg) cfg['GENERAL']['com1Exe'] = com1Exe # Run Standalone DFA reportDictList = com1DFA.com1DFAMain(cfg, avaDir) reportD = reportDictList[0] pprCom1DFA = np.loadtxt(os.path.join(avaDir, 'Outputs', 'com1DFA', 'peakFiles', reportD['simName']['name'] + '_ppr.asc'), skiprows=6) # Fetch simulation info from benchmark results benchDict = simParametersDict.fetchBenchParameters(avaTestName) simBench = benchDict['simName']['name'].replace('dfa', 'ref') pprBench = np.loadtxt(os.path.join(dirPath, '..', '..', 'benchmarks', avaTestName, simBench + '_ppr.asc'), skiprows=6) # Compare result to reference solution testRes = np.allclose(pprCom1DFA, pprBench, atol=1.e-12) # Test module assert reportD['simName']['name'] == benchDict['simName']['name'] assert reportD['Simulation Parameters']['Release Area'] == benchDict['Simulation Parameters']['Release Area'] assert reportD['Simulation Parameters']['Entrainment'] == benchDict['Simulation Parameters']['Entrainment'] assert reportD['Simulation Parameters']['Resistance'] == benchDict['Simulation Parameters']['Resistance'] assert reportD['Simulation Parameters']['Mu'] == benchDict['Simulation Parameters']['Mu'] assert reportD['Simulation Parameters']['Parameter value'] == '' assert reportD['Simulation Parameters']['Release thickness [m]'] == benchDict['Simulation Parameters']['Release thickness [m]'] assert testRes == True
def test_com2ABMain(capfd): '''Simple test for function com2ABMain''' # load and prepare Inputs listNames = ['avaHockeySmall', 'avaHockeyChannel', 'avaBowl'] dirname = os.path.dirname(__file__) for name in listNames: avalancheDir = os.path.join(dirname, '..', 'data', name) saveOutPathRef = os.path.join(dirname, '..', '..', 'benchmarks', name + 'ABPytest') cfg = cfgUtils.getModuleConfig(com2AB) flags = cfg['FLAGS'] # run main routine resAB = com2AB.com2ABMain(cfg, avalancheDir) eqParams = resAB['eqParams'] eqOut = resAB[name] # open ref data flags['fullOut'] = 'True' eqParamsRef, eqOutRef = outAB.readABresults(saveOutPathRef, name, flags) for key in eqParamsRef.keys(): assert eqParamsRef[key] == eqParams[key] atol = 1e-10 assert (np.allclose( eqOutRef['x'], eqOut['x'], atol=atol)) and (np.allclose( eqOutRef['y'], eqOut['y'], atol=atol)) and (np.allclose( eqOutRef['z'], eqOut['z'], atol=atol)) and (np.allclose( eqOutRef['s'], eqOut['s'], atol=atol)) assert (np.allclose( eqOutRef['alpha'], eqOut['alpha'], atol=atol)) and (np.allclose( eqOutRef['alphaSD'], eqOut['alphaSD'], atol=atol))
def test_getModuleConfig(capfd): '''Simple test for module getModuleConfig''' dirname = os.path.dirname(__file__) avalancheDir = dirname cfg = cfgUtils.getModuleConfig(test_logUtils) assert cfg['DICT1']['parameter1'] == '600' assert cfg['DICT1']['parameter2'] == 'None' assert cfg['DICT2']['parameter5'] == 'False'
def test_tmp1ExMain(capfd): '''Simple test for module template main''' cfg = cfgUtils.getModuleConfig(tmp1Ex) tmp1Ex.tmp1ExMain(cfg) out, err = capfd.readouterr() assert out == 'In tmp1Example\n'
def test_calcAB(capfd): '''Simple test for function calcAB''' cfg = cfgUtils.getModuleConfig(com2AB) # Make a reference quadratic profile B = -np.tan(np.deg2rad(45)) A = -B / 4000 C = 1000 N = 1000 s = np.linspace(0.0, -B / (2 * A), num=N) z = np.empty(np.shape(s)) for i in range(N): if (s[i] < (-B / (2 * A))): z[i] = 1 * (A * s[i] * s[i] + B * s[i] + C) else: z[i] = 1 * (-B * B / (4 * A) + C) thetaBeta = 10 xBeta = (-np.tan(np.deg2rad(thetaBeta)) - B) / (2 * A) yBeta = A * xBeta * xBeta + B * xBeta + C beta = np.rad2deg(np.arctan2((C - yBeta), xBeta)) # use standard coeef k1 = 1.05 k2 = -3130.0 k3 = 0.0 k4 = -2.38 SD = 1.25 alpharef = k1 * beta + k2 * 2 * A + k3 * B * B / (2 * A) + k4 SDs = [SD, -1 * SD, -2 * SD] alphaSDref = k1 * beta + k2 * 2 * A + k3 * B * B / (2 * A) + k4 + SDs # Using com2AB.calcAB to get the solution eqIn = {} eqIn['s'] = s # curvilinear coordinate (of the x, y path) eqIn['x'] = [] # x coordinate of the path eqIn['y'] = [] # y coordinate of the path eqIn['z'] = z # z coordinate of the path (projection of x,y on the raster) eqIn['indSplit'] = 2 # index of split point eqParams = com2AB.setEqParameters(cfg, smallAva=False) eqOut = com2AB.calcAB(eqIn, eqParams, 30) alpha = eqOut['alpha'] alphaSD = eqOut['alphaSD'] # compare results with a relative tolerance of tol tol = 0.002 # here 0.1% relative diff assert (alpha == pytest.approx( alpharef, rel=tol)) and (alphaSD[0] == pytest.approx( alphaSDref[0], rel=tol)) and (alphaSD[1] == pytest.approx( alphaSDref[1], rel=tol)) and (alphaSD[2] == pytest.approx( alphaSDref[2], rel=tol))
def test_QGISAB(capfd): '''Compare com2ABMain results to QGIS AB results for avaSlide''' # load and prepare Inputs avaName = 'avaSlide' dirname = os.path.dirname(__file__) avalancheDir = os.path.join(dirname, '..', 'data', avaName) cfg = cfgUtils.getModuleConfig(com2AB) # run main routine resAB = com2AB.com2ABMain(cfg, avalancheDir) # process data to get results AvaPath = resAB['AvaPath'] NameAva = AvaPath['Name'] for i in range(len(NameAva)): name = NameAva[i] resAB = outAB.processABresults(resAB, name) beta = resAB[name]['beta'] alpha = resAB[name]['alpha'] alphaSD = resAB[name]['alphaSD'] s = resAB[name]['s'] ids_alpha = resAB[name]['ids_alpha'] ids10Point = resAB[name]['ids10Point'] ids_alphaP1SD = resAB[name]['ids_alphaP1SD'] ids_alphaM1SD = resAB[name]['ids_alphaM1SD'] ids_alphaM2SD = resAB[name]['ids_alphaM2SD'] # get ref results nameRef = name + '_AB_QGIS.txt' nameRefpath = os.path.join(dirname, '..', '..', 'benchmarks', avaName + 'ABPytest', nameRef) data = np.loadtxt(nameRefpath, skiprows=4, delimiter=',') tolDist = 10 tolAngle = 0.12 assert (alpha == pytest.approx( data[0, 4], abs=tolAngle)) and (beta == pytest.approx( data[1, 4], rel=tolAngle)) and (alphaSD[1] == pytest.approx( data[2, 4], rel=tolAngle)) and (alphaSD[2] == pytest.approx( data[3, 4], rel=tolAngle)) and (alphaSD[0] == pytest.approx( data[4, 4], rel=tolAngle)) assert (s[ids_alpha] == pytest.approx( data[0, 3], abs=tolDist)) and (s[ids10Point] == pytest.approx( data[1, 3], rel=tolDist)) and (s[ids_alphaM1SD] == pytest.approx( data[2, 3], rel=tolDist)) and (s[ids_alphaM2SD] == pytest.approx( data[3, 3], rel=tolDist)) and (s[ids_alphaP1SD] == pytest.approx( data[4, 3], rel=tolDist))
def test_writeCfg2Log(tmp_path): '''Simple test for module writeCfg2Log''' dirname = os.path.dirname(__file__) avalancheDir = dirname logName = 'testCFG' logUtils.initiateLogger(tmp_path, logName) cfg = cfgUtils.getModuleConfig(test_logUtils) logFileName = os.path.join(tmp_path, 'testCFG.log') logFileNameRef = os.path.join(avalancheDir, 'data', 'testCFGRef.tog') f = open(logFileName).readlines() for i in range(3): firstLine = f.pop(0) fref = open(logFileNameRef).readlines() assert f == fref
def test_setEqParameters(capfd): '''Simple test for module setEqParameters''' cfg = cfgUtils.getModuleConfig(com2AB) # small avalanche eqParamRef = {} eqParamRef['ParameterSet'] = 'Small avalanches' eqParamRef['k1'] = 0.933 eqParamRef['k2'] = 0.0 eqParamRef['k3'] = 0.0088 eqParamRef['k4'] = -5.02 eqParamRef['SD'] = 2.36 eqParams = com2AB.setEqParameters(cfg, smallAva=True) for key in eqParamRef.keys(): assert eqParamRef[key] == eqParams[key] eqParamRef = {} eqParamRef['ParameterSet'] = 'Standard' eqParamRef['k1'] = 1.05 eqParamRef['k2'] = -3130.0 eqParamRef['k3'] = 0.0 eqParamRef['k4'] = -2.38 eqParamRef['SD'] = 1.25 eqParams = com2AB.setEqParameters(cfg, smallAva=False) for key in eqParamRef.keys(): assert eqParamRef[key] == eqParams[key] cfg['ABSETUP']['customParam'] = 'True' cfg['ABSETUP']['k1'] = '1' cfg['ABSETUP']['k2'] = '2' cfg['ABSETUP']['k3'] = '3' cfg['ABSETUP']['k4'] = '4' cfg['ABSETUP']['SD'] = '5' eqParamRef = {} eqParamRef['ParameterSet'] = 'Custom' eqParamRef['k1'] = cfg.getfloat('ABSETUP', 'k1') eqParamRef['k2'] = cfg.getfloat('ABSETUP', 'k2') eqParamRef['k3'] = cfg.getfloat('ABSETUP', 'k3') eqParamRef['k4'] = cfg.getfloat('ABSETUP', 'k4') eqParamRef['SD'] = cfg.getfloat('ABSETUP', 'SD') eqParams = com2AB.setEqParameters(cfg, smallAva=False) for key in eqParamRef.keys(): assert eqParamRef[key] == eqParams[key]
def test_cfgHash(): '''Test for the uid hash generation ''' avalancheDir = os.path.dirname(__file__) filename = os.path.join(avalancheDir, 'local_test_logUtilsCfg.ini') cfg = cfgUtils.getModuleConfig(test_logUtils, fileOverride=filename) uid = cfgUtils.cfgHash(cfg) # test for the correct uid assert uid == 'bcc6c69699' # change and test again cfg['GOODSECTION1']['goodKey1'] = '1.5' uid = cfgUtils.cfgHash(cfg) # make sure it is not the same hash assert uid != 'bcc6c69699'
def test_probAna(tmp_path): """ test probAna function to compute mask for parameter exceeding threshold """ # set input directory avaName = 'avaParabola' avaTestDir = 'avaParabolaStatsTest' dirPath = os.path.dirname(__file__) avaDir = os.path.join(dirPath, '..', '..', 'benchmarks', avaTestDir) avaDir2 = os.path.join(dirPath, '..', '..', 'benchmarks', avaName) avaDirtmp = os.path.join(tmp_path, avaName) inputDir = os.path.join(tmp_path, avaName, 'ana4Stats') inputDir1 = os.path.join(avaDir, 'ana4Stats') shutil.copytree(inputDir1, inputDir) # set configurations testCfg = os.path.join(inputDir, '%sProbAna_com1DFACfg.ini' % avaName) cfgMain = cfgUtils.getModuleConfig(com1DFA, testCfg) # Initialise input in correct format cfg = configparser.ConfigParser() cfg['GENERAL'] = {'peakLim': 10.0, 'peakVar': 'ppr'} # call function to test pA.probAnalysis(avaDirtmp, cfg, cfgMain, inputDir) probTest = np.loadtxt(os.path.join(avaDirtmp, 'Outputs', 'ana4Stats', 'avaParabola_probMap10.0.asc'), skiprows=6) # Load reference solution probSol = np.loadtxt(os.path.join(inputDir1, 'avaParabola_probMap10.0.txt'), skiprows=6) # Compare result to reference solution testRes = np.allclose(probTest, probSol, atol=1.e-6) # Test assert (testRes == True)
# Initialize project initializeProject.initializeFolderStruct(avalancheDir) logOrigin = os.path.join('.', logName + '.log') logDest = os.path.join(avalancheDir, logName + '.log') shutil.move(logOrigin, logDest) # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') # Load input parameters from configuration file cfgT = cfgUtils.getModuleConfig(gT) cfgR = cfgUtils.getModuleConfig(gR) # Call main function to generate DEMs [z, name_ext, outDir] = gT.generateTopo(cfgT, avalancheDir) # Plot new topogrpahy oT.plotDEM(z, name_ext, cfgT, outDir) # Initialise DEM_type DEM_type = cfgT['TOPO']['DEM_type'] if DEM_type == 'HX' or DEM_type == 'BL': log.warning('There is no release area available for this DEM type') else: # Make release area
from avaframe.in3Utils import logUtils # log file name; leave empty to use default runLog.log logName = 'runSampleFromDist' # Load avalanche directory from general configuration file cfgAva = cfgUtils.getGeneralConfig() avalancheDir = cfgAva['MAIN']['avalancheDir'] # set working directory workingDir = os.path.join(avalancheDir, 'Outputs', 'in1Data') fU.makeADir(workingDir) # Load input parameters from configuration file cfgMain = cfgUtils.getGeneralConfig() cfg = cfgUtils.getModuleConfig(cF) cfgGen = cfg['GENERAL'] # log file name; leave empty to use default runLog.log logName = 'runSampleFromDist%s' % cfgGen['distType'] # Start logging log = logUtils.initiateLogger(workingDir, logName) log.info('MAIN SCRIPT') # load parameters required to compute specific distribution a = float(cfgGen['a']) b = float(cfgGen['b']) c = float(cfgGen['c']) steps = int(cfgGen['support'])
def test_makeDomainTransfo(capfd): '''Simple test for module makeDomainTransfo''' # Extract input file locations cfgPath = {} dir = os.path.dirname(__file__) dirname = os.path.join(dir, 'data', 'testAna3Aimec') pathData = os.path.join(dirname, 'data') profileLayer = glob.glob(os.path.join(dirname, 'LINES', '*aimec*.shp')) cfgPath['profileLayer'] = ''.join(profileLayer) splitPointLayer = glob.glob(os.path.join(dirname, 'POINTS', '*.shp')) cfgPath['splitPointSource'] = ''.join(splitPointLayer) demSource = glob.glob(os.path.join(dirname, '*.asc')) cfgPath['demSource'] = ''.join(demSource) cfgPath['ppr'] = [ os.path.join(pathData, 'testAimec_0.asc'), os.path.join(pathData, 'testAimec_1.asc'), os.path.join(pathData, 'testAimec_2.asc'), os.path.join(pathData, 'testAimec_3.asc'), os.path.join(pathData, 'testAimec_4.asc') ] cfgPath['pfd'] = [ os.path.join(pathData, 'testAimec_0.asc'), os.path.join(pathData, 'testAimec_1.asc'), os.path.join(pathData, 'testAimec_2.asc'), os.path.join(pathData, 'testAimec_3.asc'), os.path.join(pathData, 'testAimec_4.asc') ] cfgPath['pfv'] = [ os.path.join(pathData, 'testAimec_0.asc'), os.path.join(pathData, 'testAimec_1.asc'), os.path.join(pathData, 'testAimec_2.asc'), os.path.join(pathData, 'testAimec_3.asc'), os.path.join(pathData, 'testAimec_4.asc') ] cfgPath['massBal'] = [os.path.join(dirname, '000001.txt')] * 5 cfgPath['contCmap'] = True pathResult = os.path.join(dirname, 'results') cfgPath['pathResult'] = pathResult cfgPath['projectName'] = 'testAna3Aimec' pathName = os.path.basename(profileLayer[0]) cfgPath['pathName'] = pathName cfgPath['dirName'] = 'com1DFA' cfgPath['referenceFile'] = 0 cfgPath['compType'] = ['singleModule', 'com1DFA'] cfg = cfgUtils.getModuleConfig(ana3AIMEC) cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] cfgFlags['savePlot'] = 'False' cfgSetup['startOfRunoutAreaAngle'] = '0' cfgSetup['domainWidth'] = '160' cfgSetup['resType'] = 'ppr' cfgSetup['thresholdValue'] = '0.9' cfgSetup['contourLevels'] = '0.1|0.5|1' cfgPath['numSim'] = 5 rasterTransfo = aT.makeDomainTransfo(cfgPath, cfgSetup) assert rasterTransfo['gridx'][-1, 0] == 60 assert rasterTransfo['gridx'][-1, -1] == 220 assert rasterTransfo['gridy'][0, 0] == 180 assert rasterTransfo['gridy'][0, -1] == 20 assert rasterTransfo['gridy'][-1, -1] == 258 # transform pressure_data, depth_data and speed_data in new raster newRasters = {} # assign pressure data interpMethod = cfgSetup['interpMethod'] newRasters['newRasterPPR'] = aT.assignData(cfgPath['ppr'], rasterTransfo, interpMethod) newRasters['newRasterPFD'] = newRasters['newRasterPPR'] newRasters['newRasterPFV'] = newRasters['newRasterPPR'] newRasterDEM = aT.assignData([cfgPath['demSource']], rasterTransfo, interpMethod) newRasters['newRasterDEM'] = newRasterDEM[0] # Analyze data resAnalysis = ana3AIMEC.postProcessAIMEC(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) for i in range(5): rasterSource = cfgPath['ppr'][i] sourceData = IOf.readRaster(rasterSource) rasterdata = sourceData['rasterData'] error = (resAnalysis['TP'][i] + resAnalysis['FP'][i] - np.nansum(rasterdata)) / (np.nansum(rasterdata) * 100) assert error < 0.4 assert np.abs(resAnalysis['runout'][0, i] - (240 + 10 * (i + 1))) < 5
def runCom1DFA(avaDir='', cfgFile='', relThField='', variationDict=''): """ run com1DFA module """ # +++++++++SETUP CONFIGURATION++++++++++++++++++++++++ # log file name; leave empty to use default runLog.log logName = 'runCom1DFA' # Load avalanche directory from general configuration file cfgMain = cfgUtils.getGeneralConfig() if avaDir != '': avalancheDir = avaDir else: avalancheDir = cfgMain['MAIN']['avalancheDir'] # set module name, reqiured as long we are in dev phase # - because need to create e.g. Output folder for com1DFA to distinguish from # current com1DFA modName = 'com1DFA' # Clean input directory(ies) of old work and output files # initProj.cleanSingleAvaDir(avalancheDir, keep=logName, deleteOutput=False) initProj.cleanModuleFiles(avalancheDir, com1DFA, modName) # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # Create output and work directories # - because need to create e.g. Output folder for com1DFA to distinguish from workDir, outDir = inDirs.initialiseRunDirs(avalancheDir, modName) # generate list of simulations from desired configuration if variationDict == '': # Load full configuration modCfg, modInfo = cfgUtils.getModuleConfig(com1DFA, fileOverride=cfgFile, modInfo=True) variationDict = dP.getVariationDict(avalancheDir, modCfg, modInfo) else: # check if variationDict items exist and are provided in correct format # Load standard/ default configuration modCfg = cfgUtils.getDefaultModuleConfig(com1DFA) variationDict = dP.validateVarDict(variationDict, modCfg) log.info('Variations are performed for:') for key in variationDict: log.info('%s: %s' % (key, variationDict[key])) # add avalanche directory info to cfg modCfg['GENERAL']['avalancheDir'] = avalancheDir # fetch input data - dem, release-, entrainment- and resistance areas inputSimFiles = gI.getInputDataCom1DFAPy(avalancheDir, modCfg['FLAGS']) # write full configuration file to file cfgUtils.writeCfgFile(avalancheDir, com1DFA, modCfg, fileName='sourceConfiguration') # create a list of simulations # if need to reproduce exactely the hash - need to be strings with exactely the same number of digits!! simDict = com1DFA.prepareVarSimDict(modCfg, inputSimFiles, variationDict) log.info('The following simulations will be performed') for key in simDict: log.info('Simulation: %s' % key) reportDictList = [] # loop over all simulations for cuSim in simDict: # load configuration dictionary for cuSim cfg = simDict[cuSim]['cfgSim'] # save configuration settings for each simulation simHash = simDict[cuSim]['simHash'] cfgUtils.writeCfgFile(avalancheDir, com1DFA, cfg, fileName=cuSim) # log simulation name log.info('Run simulation: %s' % cuSim) # set release area scenario inputSimFiles['releaseScenario'] = simDict[cuSim]['relFile'] # +++++++++++++++++++++++++++++++++ # ------------------------ particlesList, fieldsList, Tsave, dem, reportDict, cfgFinal = com1DFA.com1DFAMain( cfg, avalancheDir, cuSim, inputSimFiles, outDir, relThField) # +++++++++EXPORT RESULTS AND PLOTS++++++++++++++++++++++++ # Generate plots for all peakFiles plotDict = oP.plotAllPeakFields(avalancheDir, cfg, cfgMain['FLAGS'], modName) reportDictList.append(reportDict) # export for visulation if cfg['VISUALISATION'].getboolean('writePartToCSV'): outDir = os.path.join(avalancheDir, 'Outputs', modName) com1DFA.savePartToCsv(cfg['VISUALISATION']['particleProperties'], particlesList, outDir) # create hash to check if config didnt change simHashFinal = cfgUtils.cfgHash(cfgFinal) if simHashFinal != simHash: log.warning( 'simulation configuration has been changed since start') cfgUtils.writeCfgFile(avalancheDir, com1DFA, cfg, fileName='%s_butModified' % simHash) # Set directory for report reportDir = os.path.join(avalancheDir, 'Outputs', 'com1DFA', 'reports') # write report gR.writeReport(reportDir, reportDictList, cfgMain['FLAGS'], plotDict) # read all simulation configuration files and return dataFrame and write to csv standardCfg = cfgUtils.getDefaultModuleConfig(com1DFA) simDF = cfgUtils.createConfigurationInfo(avalancheDir, standardCfg, writeCSV=True) return particlesList, fieldsList, Tsave, dem, plotDict, reportDictList
from avaframe.log2Report import generateReport as gR from avaframe.out1Peak import outPlotAllPeak as oP from avaframe.out3Plot import plotUtils from avaframe.in3Utils import cfgUtils from avaframe.in3Utils import logUtils # log file name; leave empty to use default runLog.log logName = 'runGetStats' modName = 'com1DFAOrig' # Load general configuration filee cfgMain = cfgUtils.getGeneralConfig() flagShow = cfgMain['FLAGS'].getboolean('showPlot') # get path to executable cfgCom1DFA = cfgUtils.getModuleConfig(com1DFAOrig) com1Exe = cfgCom1DFA['GENERAL']['com1Exe'] peakDictList = [] avalancheDirs = ['data/avaHockeyChannel','data/avaHockeyChannel'] cfgFull = cfgUtils.getModuleConfig(getStats) cfg = cfgFull['GENERAL'] # set output directory, first ava in list outDir = os.path.join(avalancheDirs[0], 'Outputs', 'ana4Stats') cfg['outDir'] = outDir # Specify where you want the results to be stored fU.makeADir(outDir) # Start logging log = logUtils.initiateLogger(outDir, logName)
def test_analyzeArea(capfd): '''Simple test for module analyzeArea''' # get input data dirname = os.path.dirname(__file__) dataRef = os.path.join(dirname, 'data', 'refTestAimecTopo.asc') dataSim = os.path.join(dirname, 'data', 'simTestAimecTopo.asc') dataMass = os.path.join(dirname, 'data', '000000.txt') dataMass1 = os.path.join(dirname, 'data', '000001.txt') cfgPath = {} cfgPath['projectName'] = 'NameOfAvalanche' cfgPath['ppr'] = [dataRef, dataSim] cfgPath['massBal'] = [dataMass, dataMass1] pathResult = os.path.join(dirname, 'data') cfgPath['pathResult'] = pathResult cfgPath['dirName'] = 'testAIMEC' cfgPath['referenceFile'] = 0 cfgPath['compType'] = ['singleModule', 'com1DFA'] cfgPath['numSim'] = 2 cfgPath['contCmap'] = True cfg = cfgUtils.getModuleConfig(ana3AIMEC) cfgSetup = cfg['AIMECSETUP'] cfgFlags = cfg['FLAGS'] cfgFlags['savePlot'] = 'True' cfgSetup['resType'] = 'ppr' cfgSetup['thresholdValue'] = '0.9' cfgSetup['contourLevels'] = '0.1|0.5|1' cfgSetup['domainWidth'] = '600' cfgSetup['startOfRunoutAreaAngle'] = '10' avalData = np.array(([None] * 2)) data = IOf.readRaster(dataRef) avalData[0] = np.transpose(data['rasterData']) data = IOf.readRaster(dataSim) avalData[1] = np.transpose(data['rasterData']) newRasters = {} newRasters['newRasterPPR'] = avalData newRasters['newRasterPFD'] = avalData newRasters['newRasterPFV'] = avalData newRasters['newRasterDEM'] = np.transpose(data['rasterData']) rasterTransfo = {} rasterTransfo['s'] = np.linspace(0, 499, 500) rasterTransfo['l'] = np.linspace(0, 99, 100) rasterTransfo['x'] = rasterTransfo['s'] rasterTransfo['y'] = 50 * np.ones(np.shape(rasterTransfo['s'])) rasterTransfo['rasterArea'] = np.ones((500, 100)) rasterTransfo['indStartOfRunout'] = 400 rasterTransfo['startOfRunoutAreaAngle'] = 10 # testing analyzeFields function resAnalysis = ana3AIMEC.postProcessAIMEC(rasterTransfo, newRasters, cfgSetup, cfgPath, cfgFlags) assert (resAnalysis['runout'][0][0] == 449) and (resAnalysis['runout'][1][1] == 419) and (resAnalysis['runout'][2][0] == 50) and (resAnalysis['MMPPR'][1] == 1) assert (resAnalysis['TP'][1] == 800) and (resAnalysis['FN'][1] == 1700) and ( resAnalysis['FP'][1] == 200) and (resAnalysis['TN'][1] == 7300)
log.error('more than one matching simulation found for criteria! ') else: simNameRef = simNameRef[0] for dict in reportDictList: if simNameComp in dict['simName']['name']: reportD = dict log.info('Reference simulation %s and comparison simulation %s ' % (simNameRef, simNameComp)) # +++++++Aimec analysis # load configuration aimecCfg = os.path.join('..', 'benchmarks', test['NAME'], '%s_AIMECPyCfg.ini' % test['AVANAME']) cfgAimec = cfgUtils.getModuleConfig(ana3AIMEC, aimecCfg) cfgAimec['AIMECSETUP']['resType'] = 'ppr' cfgAimec['AIMECSETUP']['thresholdValue'] = '1' cfgAimec['AIMECSETUP']['diffLim'] = '5' cfgAimec['AIMECSETUP']['contourLevels'] = '1|3|5|10' cfgAimec['FLAGS']['flagMass'] = 'False' cfgAimec['AIMECSETUP']['comModules'] = 'benchmarkReference|com1DFAPy' cfgAimec['AIMECSETUP']['testName'] = test['NAME'] # Setup input from com1DFA and reference pathDict = [] pathDict = dfa2Aimec.dfaBench2Aimec(avaDir, cfgAimec, simNameRef, simNameComp) pathDict['numSim'] = len(pathDict['ppr']) log.info('reference file comes from: %s' % pathDict['compType'][1])
# Load avalanche directory from general configuration file cfgMain = cfgUtils.getGeneralConfig() avalancheDir = 'data/avaFPtest' modName = 'com1DFA' # Clean input directory(ies) of old work and output files initProj.cleanModuleFiles(avalancheDir, com1DFA, modName) # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # Load configuration FPCfg = os.path.join(avalancheDir, 'Inputs', 'FlatPlane_com1DFACfg.ini') cfg = cfgUtils.getModuleConfig(com1DFA, FPCfg) cfgGen = cfg['GENERAL'] cfgFP = cfg['FPSOL'] # for timing the sims startTime = time.time() # create output directory for test result plots outDirTest = os.path.join(avalancheDir, 'Outputs', 'ana1Tests') fU.makeADir(outDirTest) # Define release thickness distribution demFile, relFiles, entFiles, resFile, flagEntRes = gI.getInputData( avalancheDir, cfg['FLAGS']) relDict = FPtest.getReleaseThickness(avalancheDir, cfg, demFile) relTh = relDict['relTh']
# Start logging avaDir = test['AVADIR'] # Fetch benchmark test info benchDict = simParametersVar.fetchBenchParameters(test['NAME']) simNameRef = test['simNameRef'] simNameRefTest = simNameRef.replace('ref', 'dfa') refDir = pathlib.Path('..', 'benchmarks', test['NAME']) simType = benchDict['simType'] rel = benchDict['Simulation Parameters']['Release Area Scenario'] # Clean input directory(ies) of old work and output files initProj.cleanSingleAvaDir(avaDir, keep=logName) # get path to executable cfgCom1DFA = cfgUtils.getModuleConfig(com1DFA) com1Exe = cfgCom1DFA['GENERAL']['com1Exe'] # Load input parameters from configuration file for standard tests # write config to log file avaName = os.path.basename(avaDir) standardCfg = os.path.join('..', 'benchmarks', test['NAME'], '%sVarPar_com1DFACfg.ini' % test['AVANAME']) cfg = cfgUtils.getModuleConfig(com1DFA, standardCfg) cfg['GENERAL']['com1Exe'] = com1Exe # Set timing startTime = time.time() # Run Standalone DFA reportDictList = com1DFA.com1DFAMain(cfg, avaDir)
# Load settings from general configuration file cfgMain = cfgUtils.getGeneralConfig() avaDir = 'data/avaDamBreak' cfgMain['MAIN']['avalancheDir'] = avaDir # Clean input directory(ies) of old work and output files initProj.cleanModuleFiles(avaDir, com1DFA, 'com1DFA') # Start logging log = logUtils.initiateLogger(avaDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avaDir) # Load configuration damBreakCfg = os.path.join(avaDir, 'Inputs', 'damBreak_com1DFACfg.ini') cfg = cfgUtils.getModuleConfig(com1DFA, damBreakCfg) cfgGen = cfg['GENERAL'] # Load flow depth from analytical solution hL, hR, uR, phi, xR = damBreak.damBreakSol(avaDir, cfgMain, cfg) xR = xR * np.cos(phi) # projected on the horizontal plane dtAnalysis = cfg['DAMBREAK'].getfloat('dtStep') # call com1DFAPy to perform simulation - provide configuration file and release thickness function Particles, Fields, Tsave, dem, plotDict, reportDictList = runCom1DFA.runCom1DFA( avaDir=avaDir, cfgFile=damBreakCfg) # create simDict of results inputDir = pathlib.Path(avaDir, 'Outputs', 'com1DFA', 'peakFiles', 'timeSteps') dataComSol = fU.makeSimDict(inputDir, avaDir=avaDir)
# Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # ---------------- # Load input parameters from configuration files # ---------------- # Clean input directory(ies) of old work and output files initProj.cleanSingleAvaDir(avalancheDir, keep=logName) # ---------------- # Run dense flow cfg = cfgUtils.getModuleConfig(com1DFA) reportDictList = com1DFA.com1DFAMain(cfg, avalancheDir) # ---------------- # Run Alpha Beta cfgAB = cfgUtils.getModuleConfig(com2AB) resAB = com2AB.com2ABMain(cfgAB, avalancheDir) # ---------------- # Collect results/plots/report to a single directory # make simple plots (com1DFA, com2AB) # peak file plot # Generata plots for all peakFiles plotDict = oP.plotAllPeakFields(avalancheDir, cfg, cfgMain['FLAGS']) reportDictList, _, _ = outAB.writeABpostOut(resAB, cfgAB, reportDictList)
import numpy as np import time import os import math import matplotlib.pyplot as plt # Local imports import avaframe.in2Trans.ascUtils as IOf import avaframe.out3Plot.plotUtils as pU import avaframe.com1DFA.DFAtools as DFAtls import avaframe.com1DFA.com1DFA as com1DFA # import avaframe.com1DFA.SPHfunctions as SPH from avaframe.in3Utils import cfgUtils import avaframe.com1DFA.DFAfunctionsCython as DFAfunC cfg = cfgUtils.getModuleConfig(com1DFA)['GENERAL'] cfgFull = cfgUtils.getModuleConfig(com1DFA) ######################################################################## # CHOOSE YOUR SETUP ########################################################################## # Choose the snow depth you want to use (h function) def Hfunction(x, y, z): h = np.ones(np.shape(x)) GHx = np.zeros(np.shape(x)) GHy = np.zeros(np.shape(x)) # h = x*x*y/10000 + 1 GHx = -4 / (Lx * Lx) * (2 * x - Lx) * np.sin(math.pi * y / Lx) GHy = -4 / (Lx * Lx) * (x - Lx) * x * np.cos(
import pathlib from matplotlib.image import NonUniformImage from matplotlib import pyplot as plt import cmocean import logging from avaframe.in3Utils import cfgUtils from avaframe.out3Plot import makePalette from avaframe.out3Plot import plotUtils # create local logger log = logging.getLogger(__name__) # Load all input Parameters from config file # get the configuration of an already imported module cfg = cfgUtils.getModuleConfig(plotUtils) cfgPlotUtils = cfg['UNITS'] cfg = cfg['MAIN'] # define seaborn style and color maps sns.set(font_scale=1) sns.set_style( "ticks", { 'axes.linewidth': 1, 'axes.edgecolor': 'black', 'font.family': [cfg['fontFamily']] }) # define figure dimentions figW = float(cfg['figW']) figH = float(cfg['figH'])
logName = 'runAna3AIMEC' # --------------------------------------------- # Load avalanche directory from general configuration file cfgMain = cfgUtils.getGeneralConfig() avalancheDir = cfgMain['MAIN']['avalancheDir'] # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # Load all input Parameters from config file # get the configuration of an already imported module # write config to log file cfg = cfgUtils.getModuleConfig(ana3AIMEC) iP.cleanModuleFiles(avalancheDir, ana3AIMEC) # write configuration to file cfgUtils.writeCfgFile(avalancheDir, ana3AIMEC, cfg) cfgSetup = cfg['AIMECSETUP'] anaMod = cfgSetup['anaMod'] # set parameter for standard aimec analysis with mass analysis cfgSetup['resType'] = 'ppr' cfgSetup['thresholdValue'] = '1' cfgSetup['diffLim'] = '5' cfgSetup['contourLevels'] = '1|3|5|10' cfg['FLAGS']['flagMass'] = 'True'
# Load general configuration cfgMain = cfgUtils.getGeneralConfig() avalancheDir = 'data/avaSimilaritySol' # Clean input directory(ies) of old work and output files initProj.cleanSingleAvaDir(avalancheDir, keep=logName) # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # Load configuration for similarity solution test simiSolCfg = os.path.join(avalancheDir, 'Inputs', 'simiSol_com1DFACfg.ini') cfg = cfgUtils.getModuleConfig(com1DFA, simiSolCfg) # for timing the sims startTime = time.time() # create output directory for test result plots outDirTest = os.path.join(avalancheDir, 'Outputs', 'ana1Tests') fU.makeADir(outDirTest) # Define release thickness distribution demFile, relFiles, entFiles, resFile, flagEntRes = gI.getInputData( avalancheDir, cfg['FLAGS']) relDict = simiSol.getReleaseThickness(avalancheDir, cfg, demFile) relTh = relDict['relTh'] # call com1DFA to perform simulation - provide configuration file and release thickness function
import os # Local imports from avaframe.out3Plot import outQuickPlot from avaframe.in3Utils import cfgUtils from avaframe.in3Utils import logUtils # log file name; leave empty to use default runLog.log logName = 'runQuickPlotOne' # Load avalanche directory from general configuration file cfgMain = cfgUtils.getGeneralConfig() avalancheDir = cfgMain['MAIN']['avalancheDir'] # load configuration for plot generation cfg = cfgUtils.getModuleConfig(outQuickPlot) cfgPlot = cfg['ONEPLOT'] # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # Set directory where input files is located if cfgPlot.getboolean('flagInput') == True: inputDir = cfgPlot['inputDir'] else: inputDir = os.path.join(avalancheDir, 'Work', 'simplePlot') # parameters to be set location = float(cfgPlot['location'])
"""Run script for module template""" # Local imports from avaframe.tmp1Ex import tmp1Ex from avaframe.in3Utils import cfgUtils from avaframe.in3Utils import logUtils # log file name; leave empty to use default runLog.log logName = 'runTmp1Ex' # Load avalanche directory from general configuration file cfgMain = cfgUtils.getGeneralConfig() avalancheDir = cfgMain['MAIN']['avalancheDir'] # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.info('MAIN SCRIPT') log.info('Current avalanche: %s', avalancheDir) # Load all input Parameters from config file # get the configuration of an already imported module # Write config to log file cfg = cfgUtils.getModuleConfig(tmp1Ex) # Different ways to call functions tmp1Ex.tmp1ExMain(cfg)
# log file name; leave empty to use default runLog.log logName = 'runCom1DFA' modName = 'com1DFAOrigOrig' # Load avalanche directory from general configuration file cfgMain = cfgUtils.getGeneralConfig() avalancheDir = cfgMain['MAIN']['avalancheDir'] # Start logging log = logUtils.initiateLogger(avalancheDir, logName) log.debug('MAIN SCRIPT') log.debug('Current avalanche: %s', avalancheDir) # Load input parameters from configuration file # write config to log file cfg = cfgUtils.getModuleConfig(com1DFAOrig) startTime = time.time() # Clean input directory(ies) of old work and output files initProj.cleanSingleAvaDir(avalancheDir, keep=logName) # Run Standalone DFA reportDictList = com1DFAOrig.com1DFAOrigMain(cfg, avalancheDir) # Print time needed endTime = time.time() log.info(('Took %s seconds to calculate.' % (endTime - startTime))) # append parameters from logFile for reportD in reportDictList:
for avaName in testList: avaDir = 'data' + os.sep + avaName # Start logging log = logUtils.initiateLogger(avaDir, logName) log.info('Current avalanche: %s', avaDir) outDir = os.path.join(avaDir, 'Outputs') # Clean input directory(ies) of old work and output files initProj.cleanSingleAvaDir(avaDir, keep=logName) ##################################################################### # ######################### Run com1DFAOrig ############################## # get module configuration (path to executable...) cfgCom1DFAOrig = cfgUtils.getModuleConfig(com1DFAOrig) # Run Standalone DFA reportDictListcom1DFAOrig = com1DFAOrig.com1DFAOrigMain( cfgCom1DFAOrig, avaDir) for reportD1 in reportDictListcom1DFAOrig: simName1 = reportD1['simName']['name'] parameterDict = fU.extractParameterInfo(avaDir, simName1, reportD1) # Generata plots for all peakFiles modNameOrig = 'com1DFAOrig' plotDictcom1DFAOrig = oP.plotAllPeakFields(avaDir, cfgCom1DFAOrig, cfgMain['FLAGS'], modNameOrig) # Set directory for com1DFA report reportDirOrig = os.path.join(outDir, 'com1DFAOrig', 'reports')