-
Notifications
You must be signed in to change notification settings - Fork 10
[ana6, com8]: Add ana6Optimisation Module, apply changes in com8MoTPSA #1245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
15f5438
e41ff1c
a7f0a5e
3f4d1e5
97fd380
7c32be0
81b2d68
76b0dc8
3d831a7
8843c22
51ac8a1
0f4e968
4d36ca4
0e7b446
67a5886
da3f6db
20b8cd0
9c1940a
e0c0307
9c6432c
39b8dbe
4085755
a28eb40
c317e0e
385742a
7ebb68e
b475c60
c41d53b
6b03d6e
1762499
d002640
67fe320
6efedeb
b9bf7cf
9820d98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import logging | ||
| import numpy as np | ||
| import pathlib | ||
| import pandas as pd | ||
|
|
||
| # Local imports | ||
| from avaframe.in2Trans import shpConversion | ||
|
|
@@ -16,7 +17,7 @@ | |
| from avaframe.in3Utils import geoTrans | ||
| from avaframe.com1DFA import com1DFA | ||
| import avaframe.in3Utils.fileHandlerUtils as fU | ||
|
|
||
| import avaframe.in3Utils.cfgUtils as cfgUtils | ||
|
|
||
| # create local logger | ||
| log = logging.getLogger(__name__) | ||
|
|
@@ -762,3 +763,205 @@ def addSLToParticles(avaDir, cfgAimec, demFileName, particlesList, saveToPickle= | |
| rasterTransfo['demHeader'] = dem['header'] | ||
|
|
||
| return particlesList, rasterTransfo, dem | ||
|
|
||
|
|
||
| def initialAimecRunoutDiffSetup(cfgAIMEC, avalancheDir, simName, comModule): | ||
| """Setup thalweg following coordinate system using a reference simulation and DEM based on a runoutResType | ||
| and analyze reference data from e.g. observations | ||
| If only certain results of the aimec analysis workflow are required this provided info for domain transfo | ||
|
|
||
| Parameters | ||
| ----------- | ||
| cfgAIMEC: configparser object | ||
| aimec configuration settings | ||
| avalancheDir: str or pathlib.Path | ||
| path to avalanche directory | ||
| simName: str | ||
| full simulation name or path thereof | ||
| comModule: str | ||
| name of computational module that has been used to run simulations | ||
|
|
||
| Returns | ||
| ---------- | ||
| aimecInfo: dict | ||
| dictionary including: | ||
| rasterTransfo dict (info about domain transformation) | ||
| newRasters dict (info on transformed DEM) | ||
| refDataTransformed dict (info on reference dataset (observations) transformed) | ||
| pathDict dict (info on all required input file paths, project name, etc.) | ||
| cfgAIMEC (aimec configuration settings) | ||
| """ | ||
|
|
||
| cfgSetup = cfgAIMEC["AIMECSETUP"] | ||
| # create data frame that lists all available simulations and path to their result type result files | ||
| inputsDF, resTypeList = fU.makeSimFromResDF(avalancheDir, comModule, inputDir="", simName=simName) | ||
|
|
||
| # use first simulation as reference | ||
| refSimRowHash = inputsDF.index[0] | ||
| refSimName = inputsDF.loc[refSimRowHash, "simName"] | ||
|
|
||
| # initialize pathDict | ||
| pathDict = { | ||
| "refSimRowHash": refSimRowHash, | ||
| "refSimName": refSimName, | ||
| "compType": ["singleModule", comModule], | ||
| "colorParameter": False, | ||
| "resTypeList": resTypeList, | ||
| "valRef": "", | ||
| "demFileName": "", | ||
| } | ||
| pathDict = aimecTools.readAIMECinputs( | ||
| avalancheDir, pathDict, cfgSetup.getboolean("defineRunoutArea"), dirName=comModule | ||
| ) | ||
| pathDict = aimecTools.checkAIMECinputs(cfgSetup, pathDict, inputsDF) | ||
|
|
||
| # Extract input config parameters | ||
| interpMethod = cfgSetup["interpMethod"] | ||
| # Read input dem | ||
| demSource = pathDict["demSource"] | ||
| dem = IOf.readRaster(demSource) | ||
| # read reference file and raster and config | ||
| runoutResType = pathDict["runoutResType"] | ||
| runoutLayer = pathDict.get("runoutLayer", "") | ||
| refResTypeCol = aimecTools.resolveResTypeColumn(inputsDF.loc[refSimRowHash], runoutResType, runoutLayer) | ||
| refResultSource = inputsDF.loc[refSimRowHash, refResTypeCol] | ||
| refRaster = IOf.readRaster(refResultSource) | ||
| refHeader = refRaster["header"] | ||
|
|
||
| # Make domain transformation | ||
| rasterTransfo = aimecTools.makeDomainTransfo(pathDict, dem, refHeader["cellsize"], cfgSetup) | ||
| rasterTransfo["avaDir"] = pathDict["avalancheDir"] | ||
|
|
||
| # #################################################### | ||
| # visualisation | ||
| # TODO: needs to be moved somewhere else | ||
| newRasters = {} | ||
| log.debug("Assigning dem data to deskewed raster") | ||
| newRasters["newRasterDEM"] = aimecTools.transform(dem, demSource, rasterTransfo, interpMethod, dem=True) | ||
|
|
||
| # if includeReference add reference data to resAnalysisDF | ||
| if cfgSetup.getboolean("includeReference"): | ||
| referenceDF = aimecTools.createReferenceDF(pathDict) | ||
| refDataTransformed, referenceDF = postProcessReference( | ||
| cfgAIMEC, rasterTransfo, pathDict, referenceDF, newRasters | ||
| ) | ||
| # save resultsDF to file | ||
| referenceDFPath = pathlib.Path(pathDict["pathResult"], "referenceDF.csv") | ||
| referenceDF.to_csv(referenceDFPath) | ||
| else: | ||
| refDataTransformed = {} | ||
|
|
||
| aimecInfo = { | ||
| "rasterTransfo": rasterTransfo, | ||
| "newRasters": newRasters, | ||
| "refDataTransformed": refDataTransformed, | ||
| "pathDict": pathDict, | ||
| "cfgAIMEC": cfgAIMEC, | ||
| } | ||
| # initialize an empty dataframe to collect runoutLineDiff_RMSE values for all simulations | ||
| aimecInfo["resAnalysisDFFull"] = pd.DataFrame() | ||
|
|
||
| return aimecInfo | ||
|
|
||
|
|
||
| def addSimToResAnalysisDFForRunoutComparison(avalancheDir, simName, comModule, aimecInfo): | ||
| """analyze simulation in thalweg following coordinate system derive runout line and compare runout line to | ||
| reference rounout line | ||
|
|
||
| Parameters | ||
| ----------- | ||
| avalancheDir : str or pathlib.Path | ||
| path to avalanche directory | ||
| simName : str | ||
| simulation name or part thereof as e.g. simHash | ||
| comModule : str | ||
| name of computational module that was used to run simulation | ||
| aimecInfo : dict | ||
| dictionary with info on domain transformation output from initialAimecRunoutDiffSetup() | ||
|
|
||
| Returns | ||
| ------- | ||
| resAnalysisDF: pandas dataFrame | ||
| dataFrame with one row per simulation, so here only 1 row with runout line difference analysis | ||
| and simulation configuration | ||
|
|
||
| """ | ||
|
|
||
| # initialize required inputs and parameters | ||
| cfgSetup = aimecInfo["cfgAIMEC"]["AIMECSETUP"] | ||
| rasterTransfo = aimecInfo["rasterTransfo"] | ||
| newRasters = aimecInfo["newRasters"] | ||
| refDataTransformed = aimecInfo["refDataTransformed"] | ||
| pathDict = aimecInfo["pathDict"] | ||
|
|
||
| # create data frame that lists all available simulations and path to their result type result files | ||
| inputsDF, resTypeList = fU.makeSimFromResDF(avalancheDir, comModule, inputDir="", simName=simName) | ||
| # look for a configuration | ||
| try: | ||
| # load dataFrame for all configurations | ||
| configurationDF = cfgUtils.createConfigurationInfo(avalancheDir, comModule=comModule) | ||
| # Merge inputsDF with the configurationDF. Make sure to keep the indexing from inputs and to merge on 'simName' | ||
| inputsDF = ( | ||
| inputsDF.reset_index().merge(configurationDF, on=["simName", "modelType"]).set_index("index") | ||
| ) | ||
| configFound = True | ||
| except (NotADirectoryError, FileNotFoundError) as e: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| configFound = False | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| refSimRowHash = inputsDF.index[0] | ||
| refSimName = inputsDF.loc[refSimRowHash, "simName"] | ||
|
|
||
| # update pathDict | ||
| pathDict["refSimRowHash"] = (refSimRowHash,) | ||
| pathDict["refSimName"] = (refSimName,) | ||
|
|
||
| # Extract input config parameters | ||
| interpMethod = cfgSetup["interpMethod"] | ||
| # add fields that will be filled in analysis | ||
| resAnalysisDF = aimecTools.addFieldsToDF(inputsDF) | ||
| # read reference file and raster and config | ||
| runoutResType = pathDict["runoutResType"] | ||
| runoutLayer = pathDict.get("runoutLayer", "") | ||
| resTypeCol = aimecTools.resolveResTypeColumn( | ||
| resAnalysisDF.loc[refSimRowHash], runoutResType, runoutLayer | ||
| ) | ||
| inputFiles = resAnalysisDF.loc[refSimRowHash, resTypeCol] | ||
| if isinstance(inputFiles, pathlib.PurePath): | ||
| rasterData = IOf.readRaster(inputFiles) | ||
| newRaster = aimecTools.transform(rasterData, inputFiles, rasterTransfo, interpMethod) | ||
| newRasters["newRaster" + runoutResType.upper()] = newRaster | ||
| if cfgSetup.getboolean("includeReference"): | ||
| resAnalysisDF["runoutLineDiff_line"] = np.nan | ||
| resAnalysisDF["runoutLineDiff_line"] = resAnalysisDF["runoutLineDiff_line"].astype(object) | ||
| resAnalysisDF["runoutLineDiff_poly"] = np.nan | ||
| resAnalysisDF["runoutLineDiff_poly"] = resAnalysisDF["runoutLineDiff_line"].astype(object) | ||
|
|
||
| runoutLine = aimecTools.computeRunoutLine( | ||
| cfgSetup, | ||
| rasterTransfo, | ||
| newRasters, | ||
| refSimRowHash, | ||
| "simulation", | ||
| name="", | ||
| runoutResType=runoutResType, | ||
| ) | ||
|
|
||
| # plot comparison between runout lines | ||
| outAimec.compareRunoutLines( | ||
| cfgSetup, | ||
| refDataTransformed, | ||
| newRasters["newRaster" + runoutResType.upper()], | ||
| runoutLine, | ||
| rasterTransfo, | ||
| resAnalysisDF.loc[refSimRowHash], | ||
| pathDict, | ||
| ) | ||
|
|
||
| # analyze distribution of diffs between runout lines | ||
| resAnalysisDF = aimecTools.analyzeDiffsRunoutLines( | ||
| cfgSetup, runoutLine, refDataTransformed, resAnalysisDF, refSimRowHash, pathDict | ||
| ) | ||
| resAnalysisDFPath = pathlib.Path(pathDict["pathResult"], "resAnalysisDF_%s.csv" % simName) | ||
| resAnalysisDF.to_csv(resAnalysisDFPath) | ||
|
|
||
| return resAnalysisDF | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Local variable
eis assigned to but never used [ruff:F841]