def dfaComp2Aimec(avaDir, cfg, rel, simType): """ Create a pathDict where the paths to all the files required by aimec are saved for two modules - in order to compare always two simulations at a time for now matching simulations are identified via releaseScenario and simType Parameters ----------- avaDir: str path to avalanche directory cfgSetup: configParser object configuration for aimec rel: str releaseScenario simType: str simulation type (null, ent, entres, ..) Returns -------- pathDict: dict dictionary with paths to result and optionally mass files for matching simulations from two modules - matching in terms of releaseScenario and simType """ cfgSetup = cfg['AIMECSETUP'] # initialise empty pathDict for all required files pathDict = {'ppr': [], 'pfd': [], 'pfv': [], 'massBal': []} # get directories where simulation results can be found for both modules inputDirRef, inputDirComp, pathDict, refModule = getCompDirs(avaDir, cfgSetup, pathDict) # Load all infos on reference simulations if refModule == 'benchmarkReference': refData = fU.makeSimDict(inputDirRef) else: refData = fU.makeSimDict(inputDirRef) # Load all infos on comparison module simulations compData = fU.makeSimDict(inputDirComp) # identify names of simulations that match criteria for both cases simSearch = True for countRef, simNameShort in enumerate(refData['simName']): for countComp, simNameComp in enumerate(compData['simName']): if simSearch: if refData['releaseArea'][countRef] == compData['releaseArea'][countComp] == rel and refData['simType'][countRef] == compData['simType'][countComp] == simType: refSimName = refData['simName'][countRef] compSimName = compData['simName'][countComp] log.info('Reference simulation: %s and to comparison simulation: %s ' % (refSimName, compSimName)) simSearch = False if simSearch == True: message = 'No matching simulations found for reference and comparison simulation for releaseScenario: %s and simType: %s' % (rel, simType) log.error(message) raise FileNotFoundError(message) # fill pathDict pathDict = getPathsFromSimName(pathDict, avaDir, cfg, inputDirRef, refSimName, inputDirComp, compSimName) return pathDict
def test_makeSimDict(): """ Test if simulation dictionary is generated correctly """ # Test function dirPath = os.path.dirname(__file__) inputDir = os.path.join(dirPath, 'data', 'testSim') cfg = configparser.ConfigParser() cfg = {'varPar': 'test'} data = fU.makeSimDict(inputDir, cfg['varPar']) assert data['names'][0] == 'releaseTest1_entres_dfa_0.888_ppr' assert data['releaseArea'][0] == 'releaseTest1' assert data['simType'][0] == 'entres' assert data['resType'][0] == 'ppr' assert data['cellSize'][0] == 5.0 assert data['test'][0] == '0.888'
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) # make comparison plots damBreak.plotComparison(dataComSol, hL, xR, hR, uR, dtAnalysis, cfgMain)
def extractMaxValues(inputDir, cfgMain, avaDir, nameScenario=''): """ Extract max values of result parameters and save to dictionary Parameters ----------- inputDir: str path to directoy where peak files can be found cfgMain: dict configuration used to perform simulations avaDir: str path to avalanche directoy Returns -------- peakValues: dict dictionary that contain max values for all result parameters for each simulation """ # load simulation results and info varPar = cfgMain['PARAMETERVAR']['varPar'] peakFiles = fU.makeSimDict(inputDir, varPar, avaDir) nSims = len(peakFiles['simName']) # load parameter variation values to check if they include default value itemsRaw = fU.splitIniValueToArraySteps( cfgMain['PARAMETERVAR']['varParValues']) dVal = float(cfgMain['DEFVALUES'][varPar]) flagValue = False if dVal in itemsRaw: flagValue = True # initialize dictionary to save values, if default value not in parameter variation, exclude this value peakValues = {} count = 0 for simName in peakFiles['simName']: if flagValue == False: if peakFiles[varPar][count] != dVal: peakValues[simName] = {} else: peakValues[simName] = {} count = count + 1 # Loop through peakFiles and compute probability for m in range(nSims): # Load peak fields # be aware of the standard simulation - so if default value should not be part of the analysis if flagValue == True: log.debug('Simulation parameter %s= %s' % (varPar, peakFiles[varPar][m])) # Load data fileName = peakFiles['files'][m] simName = peakFiles['simName'][m] data = np.loadtxt(fileName, skiprows=6) # compute max max = np.amax(data) # add statistical measures peakValues[simName].update({peakFiles['resType'][m]: max}) peakValues[simName].update({'varPar': float(peakFiles[varPar][m])}) if nameScenario != '': peakValues[simName].update({'scenario': nameScenario}) else: if peakFiles[varPar][m] != dVal: log.debug('Simulation parameter %s= %s' % (varPar, peakFiles[varPar][m])) # Load data fileName = peakFiles['files'][m] simName = peakFiles['simName'][m] data = np.loadtxt(fileName, skiprows=6) # compute max max = np.amax(data) # add statistical measures peakValues[simName].update({peakFiles['resType'][m]: max}) peakValues[simName].update( {'varPar': float(peakFiles[varPar][m])}) if nameScenario != '': peakValues[simName].update({'scenario': nameScenario}) return peakValues
def quickPlot(avaDir, testDir, suffix, val, parameter, cfg, cfgPlot, rel='', simType='null', comModule='com1DFA', comModule2=''): """ Plot simulation result and compare to reference solution (two raster datasets of identical dimension) and save to Outputs/out3Plot within avalanche directoy figure 1: plot raster data for dataset1, dataset2 and their difference, including a histogram and the cumulative density function of the differences figure 2: plot cross and longprofiles for both datasets (ny_loc and nx_loc define location of profiles) -plots are saved to Outputs/out3Plot Parameters ---------- avaDir : str path to avalanche directory suffix : str result parameter abbreviation (e.g. 'ppr') val : str value of parameter parameter : str parameter that is used to filter simulation results within folder, for example, symType, parameter variation, etc. cfg : dict global configuration settings cfgPlot : dict configuration settings for plots, required for flag if plots shall be shown or only saved rel : str optional - name of release area scenarios simType : str optional - simulation type null or entres Returns ------- plotList : list list of plot dictionaries (path to plots, min, mean and max difference between plotted datasets, max and mean value of reference dataset ) """ # Create required directories workDir = os.path.join(avaDir, 'Work', 'out3Plot') fU.makeADir(workDir) outDir = os.path.join(avaDir, 'Outputs', 'out3Plot') fU.makeADir(outDir) # Initialise plotDictList plotList = [] # Setup input from com1DFA fU.getDFAData(avaDir, workDir, suffix, comModule=comModule) if comModule2 == '': # Get data from reference run fU.getRefData(testDir, workDir, suffix) else: fU.getDFAData(avaDir, workDir, suffix, comModule=comModule2) # prepare data if parameter == 'Mu' or parameter == 'RelTh': data = fU.makeSimDict(workDir, parameter, avaDir) else: data = fU.makeSimDict(workDir, '', avaDir) cellSize = data['cellSize'][0] unit = pU.cfgPlotUtils['unit%s' % suffix] # check if release Area and simType area provided if rel != '': relAreas = [rel] else: # Count the number of release areas relAreas = set(data['releaseArea']) if parameter == 'simType': simType = val for rel in relAreas: # Initialise plotList plotDict = {'relArea': rel, 'plots': [], 'difference': [], 'stats': []} # get list of indices of files that are of correct simulation type and result paramete indSuffix = [-9999, -9999] findComp = True for m in range(len(data['files'])): if data['resType'][m] == suffix and data['releaseArea'][ m] == rel and data[parameter][m] == val and data[ 'simType'][m] == simType: if (data['modelType'][m] == 'dfa') and findComp: indSuffix[0] = m findComp = False elif data['modelType'][m] == cfgPlot['PLOT']['refModel']: indSuffix[1] = m if findComp: log.error('No matching files found') # Load data raster = IOf.readRaster(data['files'][indSuffix[0]]) rasterRef = IOf.readRaster(data['files'][indSuffix[1]]) data1, data2 = geoTrans.resizeData(raster, rasterRef) log.debug('dataset1: %s' % data['files'][indSuffix[0]]) log.debug('dataset2: %s' % data['files'][indSuffix[1]]) # Get name of Avalanche avaName = data['avaName'][indSuffix[0]] # Create dataDict to be passed to generatePlot dataDict = { 'data1': data1, 'data2': data2, 'name1': data['names'][indSuffix[0]], 'name2': data['names'][indSuffix[1]], 'compareType': 'compToRef', 'simName': data['simName'][indSuffix[0]], 'suffix': suffix, 'cellSize': cellSize, 'unit': unit } # Create Plots plotDictNew = generatePlot(dataDict, avaName, outDir, cfg, plotDict) plotList.append(plotDictNew) return plotList
def plotAllPeakFields(avaDir, cfg, cfgFLAGS, modName): """ Plot all peak fields and return dictionary with paths to plots Parameters ---------- avaDir : str path to avalanche directoy cfg : dict configuration used to perform simulations cfgFLAGS : str general configuration, required to define if plots saved to reports directoy modName : str name of module that has been used to produce data to be plotted Returns ------- plotDict : dict dictionary with info on plots, like path to plot """ # Load all infos on simulations inputDir = os.path.join(avaDir, 'Outputs', modName, 'peakFiles') peakFiles = fU.makeSimDict(inputDir, '', avaDir) demFile = gI.getDEMPath(avaDir) demData = IOf.readRaster(demFile) demField = demData['rasterData'] # Output directory if cfgFLAGS.getboolean('ReportDir'): outDir = os.path.join(avaDir, 'Outputs', modName, 'reports') fU.makeADir(outDir) else: outDir = os.path.join(avaDir, 'Outputs', 'out1Peak') fU.makeADir(outDir) # Initialise plot dictionary with simulation names plotDict = {} for sName in peakFiles['simName']: plotDict[sName] = {} # Loop through peakFiles and generate plot for m in range(len(peakFiles['names'])): # Load names and paths of peakFiles name = peakFiles['names'][m] fileName = peakFiles['files'][m] avaName = peakFiles['avaName'][m] log.debug('now plot %s:' % (fileName)) # Load data raster = IOf.readRaster(fileName) data = raster['rasterData'] # constrain data to where there is data cellSize = peakFiles['cellSize'][m] rowsMin, rowsMax, colsMin, colsMax = pU.constrainPlotsToData( data, cellSize) dataConstrained = data[rowsMin:rowsMax + 1, colsMin:colsMax + 1] demConstrained = demField[rowsMin:rowsMax + 1, colsMin:colsMax + 1] data = np.ma.masked_where(dataConstrained == 0.0, dataConstrained) unit = pU.cfgPlotUtils['unit%s' % peakFiles['resType'][m]] # Set extent of peak file ny = data.shape[0] nx = data.shape[1] Ly = ny * cellSize Lx = nx * cellSize # Figure shows the result parameter data fig = plt.figure(figsize=(pU.figW, pU.figH)) fig, ax = plt.subplots() # choose colormap cmap, _, _, norm, ticks = makePalette.makeColorMap( pU.cmapPres, np.amin(data), np.amax(data), continuous=pU.contCmap) cmap.set_bad(alpha=0) rowsMinPlot = rowsMin * cellSize rowsMaxPlot = (rowsMax + 1) * cellSize colsMinPlot = colsMin * cellSize colsMaxPlot = (colsMax + 1) * cellSize im0 = ax.imshow( demConstrained, cmap='Greys', extent=[colsMinPlot, colsMaxPlot, rowsMinPlot, rowsMaxPlot], origin='lower', aspect='equal') im1 = ax.imshow( data, cmap=cmap, extent=[colsMinPlot, colsMaxPlot, rowsMinPlot, rowsMaxPlot], origin='lower', aspect='equal') pU.addColorBar(im1, ax, ticks, unit) title = str('%s' % name) ax.set_title(title) ax.set_xlabel('x [m]') ax.set_ylabel('y [m]') plotName = os.path.join(outDir, '%s.%s' % (name, pU.outputFormat)) pU.putAvaNameOnPlot(ax, avaDir) fig.savefig(plotName) if cfgFLAGS.getboolean('showPlot'): plt.show() plotPath = os.path.join(os.getcwd(), plotName) plotDict[peakFiles['simName'][m]].update( {peakFiles['resType'][m]: plotPath}) plt.close('all') return plotDict
def probAnalysis(avaDir, cfg, cfgMain='', inputDir=''): """ Compute propability map of a given set of simulation result exceeding a particular threshold and save to outDir Parameters ---------- avaDir: str path to avalanche directory cfg : dict configuration read from ini file of probAna function cfgMain : dict configuration read from ini file that has been used for the com1DFA simulation inputDir : str optional - path to directory where data that should be analysed can be found, required if not in com1DFA results outDir : str optional - path to directory where results shall be saved to """ # Set input and output directory and Load all infos on simulations flagStandard = False if inputDir == '': inputDir = os.path.join(avaDir, 'Outputs', 'com1DFA', 'peakFiles') flagStandard = True peakFiles = fU.makeSimDict(inputDir, cfgMain['PARAMETERVAR']['varPar'], avaDir=avaDir) else: peakFiles = fU.makeSimDict(inputDir, avaDir=avaDir) outDir = os.path.join(avaDir, 'Outputs', 'ana4Stats') fU.makeADir(outDir) # get header info from peak files header = IOf.readASCheader(peakFiles['files'][0]) cellSize = header.cellsize nRows = header.nrows nCols = header.ncols xllcenter = header.xllcenter yllcenter = header.yllcenter noDataValue = header.noDataValue # Initialise array for computations probSum = np.zeros((nRows, nCols)) count = 0 # Loop through peakFiles and compute probability for m in range(len(peakFiles['names'])): # Load peak field for desired peak field parameter # be aware of the standard simulation - so if default value should not be part of the analysis if peakFiles['resType'][m] == cfg['GENERAL']['peakVar']: log.info('Simulation parameter %s ' % ( cfg['GENERAL']['peakVar'])) if flagStandard: if peakFiles[cfgMain['PARAMETERVAR']['varPar']][m] != cfgMain['DEFVALUES'][cfgMain['PARAMETERVAR']['varPar']]: log.info('Simulation parameter %s= %s ' % (cfgMain['PARAMETERVAR']['varPar'], peakFiles[cfgMain['PARAMETERVAR']['varPar']][m])) # Load data fileName = peakFiles['files'][m] data = np.loadtxt(fileName, skiprows=6) dataLim = np.zeros((nRows, nCols)) log.debug('File name is %s' % fileName) # Check if peak values exceed desired threshold dataLim[data>float(cfg['GENERAL']['peakLim'])] = 1.0 probSum = probSum + dataLim count = count + 1 else: # Load data fileName = peakFiles['files'][m] data = np.loadtxt(fileName, skiprows=6) dataLim = np.zeros((nRows, nCols)) log.debug('File name is %s' % fileName) # Check if peak values exceed desired threshold dataLim[data>float(cfg['GENERAL']['peakLim'])] = 1.0 probSum = probSum + dataLim count = count + 1 # Create probability map ranging from 0-1 probMap = probSum / count unit = pU.cfgPlotUtils['unit%s' % cfg['GENERAL']['peakVar']] log.info('probability analysis performed for peak parameter: %s and a peak value threshold of: %s %s' % (cfg['GENERAL']['peakVar'], cfg['GENERAL']['peakLim'], unit)) log.info('%s peak fields added to analysis' % count) # # Save to .asc file avaName = os.path.basename(avaDir) outFile = os.path.join(outDir, '%s_probMap%s.asc' % (avaName, cfg['GENERAL']['peakLim'])) IOf.writeResultToAsc(header, probMap, outFile)