-
Notifications
You must be signed in to change notification settings - Fork 5
Multiple cfgs #739
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
Multiple cfgs #739
Changes from all commits
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 |
|---|---|---|
|
|
@@ -14,12 +14,14 @@ | |
| # import config and init tools | ||
| import avaframe.in2Trans.shpConversion as shpConv | ||
| from avaframe.in3Utils import cfgUtils | ||
| from avaframe.in3Utils import fileHandlerUtils as fU | ||
| from avaframe.in1Data import getInput | ||
|
|
||
| # import computation modules | ||
| from avaframe.com1DFA import com1DFA, particleTools | ||
| import avaframe.ana5Utils.DFAPathGeneration as DFAPath | ||
| from avaframe.com2AB import com2AB | ||
| from avaframe.com3Hybrid import com3Hybrid | ||
|
|
||
| # import analysis tools | ||
| from avaframe.out3Plot import outAB | ||
|
|
@@ -41,8 +43,15 @@ def maincom3Hybrid(cfgMain, cfgHybrid): | |
| """ | ||
| avalancheDir = cfgMain['MAIN']['avalancheDir'] | ||
| demOri = getInput.readDEM(avalancheDir) | ||
| # get comDFA configuration path for hybrid model | ||
| hybridModelDFACfg = pathlib.Path('com3Hybrid', 'hybridModel_com1DFACfg.ini') | ||
|
|
||
| # setup outputs folder | ||
| oPath = pathlib.Path(avalancheDir, 'Outputs', 'com3Hybrid') | ||
| fU.makeADir(oPath) | ||
|
|
||
| # get comDFA configuration and save to file | ||
| hybridModelDFACfg = cfgUtils.getModuleConfig(com1DFA, fileOverride='', modInfo=False, toPrint=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. it is maybe a bit confusing to have both a fileOverride and a overrideConfig... Maybe we could rename this a bit
Contributor
Author
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. what is your suggestion? I think it is actually pretty accurate the one thing is a file path that you have to provide the other is configparser object that has the override parameters in it.. |
||
| onlyDefault=cfgHybrid['com1DFA_override']['defaultConfig'], overrideConfig=cfgHybrid) | ||
| hybridModelDFACfgFile = cfgUtils.writeCfgFile(avalancheDir, com1DFA, hybridModelDFACfg, fileName='com1DFA_settings', filePath=oPath) | ||
|
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. why write this one and not also the AB and pathGeneration cfgs to file? |
||
|
|
||
| # get initial mu value | ||
| muArray = np.array([cfgHybrid.getfloat('DFA', 'mu')]) | ||
|
|
@@ -54,21 +63,25 @@ def maincom3Hybrid(cfgMain, cfgHybrid): | |
| iterate = True | ||
| resultsHybrid = {} | ||
| while iteration < nIterMax and iterate: | ||
| # update the com1DFA mu value | ||
| # update the com1DFA mu value in configuration file | ||
| updater = ConfigUpdater() | ||
| updater.read(hybridModelDFACfg) | ||
| updater.read(hybridModelDFACfgFile) | ||
| updater['GENERAL']['mu'].value = ('%.4f' % muArray[-1]) | ||
| updater.update_file() | ||
|
|
||
| # Run dense flow with coulomb friction | ||
| dem, _, _, simDF = com1DFA.com1DFAMain(avalancheDir, cfgMain, cfgFile=hybridModelDFACfg) | ||
| dem, _, _, simDF = com1DFA.com1DFAMain(avalancheDir, cfgMain, cfgFile=hybridModelDFACfgFile) | ||
| simID = simDF.index[0] | ||
| particlesList, timeStepInfo = particleTools.readPartFromPickle(avalancheDir, simName=simID, flagAvaDir=True, | ||
| comModule='com1DFA') | ||
|
|
||
| # fetch configuration for DFAPathGeneration | ||
| hybridModelPathCfg= cfgUtils.getModuleConfig(DFAPath, fileOverride='', modInfo=False, toPrint=False, | ||
|
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. Similar blocks of code found in 3 locations. Consider refactoring.
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. we need to come up with a good way to name these cfg objects. DFAPathcom3HybridCfg and com1DFAcom3HybridCfg and com2ABcom3HybridCfg or so no? And we always use the same method to name these files
Contributor
Author
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. this is a good point! |
||
| onlyDefault=cfgHybrid['DFAPathGeneration_override']['defaultConfig'], overrideConfig=cfgHybrid) | ||
| # postprocess to extract path and energy line | ||
| avaProfileMass = DFAPath.getDFAPathFromPart(particlesList, addVelocityInfo=True) | ||
| # make a copy because extendDFAPathKernel might modify avaProfileMass | ||
| avaProfileMassExt = DFAPath.extendDFAPath(cfgHybrid['PATH'], avaProfileMass.copy(), dem, particlesList[0]) | ||
| avaProfileMassExt = DFAPath.extendDFAPath(hybridModelPathCfg['PATH'], avaProfileMass.copy(), dem, particlesList[0]) | ||
| avaProfileMassExtOri = copy.deepcopy(avaProfileMassExt) | ||
| avaProfileMassExtOri['x'] = avaProfileMassExtOri['x'] + demOri['header']['xllcenter'] | ||
| avaProfileMassExtOri['y'] = avaProfileMassExtOri['y'] + demOri['header']['yllcenter'] | ||
|
|
@@ -78,14 +91,15 @@ def maincom3Hybrid(cfgMain, cfgHybrid): | |
| shpConv.writeLine2SHPfile(avaProfileMassExtOri, name, pathAB) | ||
|
|
||
| # Run Alpha Beta | ||
| hybridModelABCfg = pathlib.Path('com3Hybrid', 'hybridModel_com2ABCfg.ini') | ||
| cfgAB = cfgUtils.getModuleConfig(com2AB, fileOverride=hybridModelABCfg) | ||
| cfgAB['ABSETUP']['path2Line'] = str(pathAB) + '.shp' | ||
| # first create configuration object for com2AB | ||
| hybridModelcom2ABCfg = cfgUtils.getModuleConfig(com2AB, fileOverride='', modInfo=False, toPrint=False, | ||
fso42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| onlyDefault=cfgHybrid['com1DFA_override']['defaultConfig'], overrideConfig=cfgHybrid) | ||
| hybridModelcom2ABCfg['ABSETUP']['path2Line'] = str(pathAB) + '.shp' | ||
| # take the path extracted from the DFA model as input | ||
| pathDict, demAB, splitPoint, eqParams, resAB = com2AB.com2ABMain(cfgAB, avalancheDir) | ||
| pathDict, demAB, splitPoint, eqParams, resAB = com2AB.com2ABMain(hybridModelcom2ABCfg, avalancheDir) | ||
| # make AB plot | ||
| reportDictList = [] | ||
| _, plotFile, writeFile = outAB.writeABpostOut(pathDict, demAB, splitPoint, eqParams, resAB, cfgAB, reportDictList) | ||
| _, plotFile, writeFile = outAB.writeABpostOut(pathDict, demAB, splitPoint, eqParams, resAB, hybridModelcom2ABCfg, reportDictList) | ||
|
|
||
| # make custom com3 profile plot | ||
| alpha = resAB[name]['alpha'] | ||
|
|
@@ -113,6 +127,8 @@ def maincom3Hybrid(cfgMain, cfgHybrid): | |
| outCom3Plots.hybridProfilePlot(avalancheDir, resultsHybrid) | ||
| outCom3Plots.hybridPathPlot(avalancheDir, dem, resultsHybrid, fields[0], particlesList[-1], muArray) | ||
|
|
||
| hybridModelCfgFile = cfgUtils.writeCfgFile(avalancheDir, com3Hybrid, cfgHybrid, fileName='com3Hybrid_settings', filePath=oPath) | ||
|
|
||
| log.debug('Alpha array is %s' % alphaArray) | ||
| log.debug('mu array is %s' % muArray) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ def getGeneralConfig(): | |
| return cfg | ||
|
|
||
|
|
||
| def getModuleConfig(module, fileOverride='', modInfo=False, toPrint=True, onlyDefault=False): | ||
| def getModuleConfig(module, fileOverride='', modInfo=False, toPrint=True, onlyDefault=False, overrideConfig=''): | ||
| ''' Returns the configuration for a given module | ||
| returns a configParser object | ||
|
|
||
|
|
@@ -65,6 +65,8 @@ def getModuleConfig(module, fileOverride='', modInfo=False, toPrint=True, onlyDe | |
| true if dictionary with info on differences to standard config | ||
| onlyDefault: bool | ||
| if True, only use the default configuration | ||
| overrideConfig: cfg | ||
| if not empty, override configuration parameters with override parameters | ||
|
Comment on lines
+68
to
+69
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. back to my other comment, the naming is confusing, we have two override files given in input. Also update the doc string so it is clear what is happening here |
||
|
|
||
| Order is as follows: | ||
| fileOverride -> local_MODULECfg.ini -> MODULECfg.ini | ||
|
|
@@ -106,12 +108,49 @@ def getModuleConfig(module, fileOverride='', modInfo=False, toPrint=True, onlyDe | |
| # Finally read it | ||
| cfg, modDict = compareConfig(iniFile, modName, compare, modInfo, toPrint) | ||
|
|
||
| # override parameters with override information | ||
| if overrideConfig != '': | ||
| log.info('adding overrides now!') | ||
| cfg = getOverrideConfiguration(cfg, overrideConfig['%s_override' % modName]) | ||
|
|
||
| if modInfo: | ||
| return cfg, modDict | ||
|
|
||
| return cfg | ||
|
|
||
|
|
||
| def getOverrideConfiguration(cfg, overrideParameters): | ||
fso42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ override configuration parameter values with the values provided in overrideParameters | ||
|
|
||
| Parameters | ||
| ---------- | ||
| cfg: configparer object | ||
| configuration of module | ||
| overrideParameters: configparser object | ||
| section with override parameter information | ||
|
|
||
| Returns | ||
| -------- | ||
| cfg: configparser object | ||
| updated configuration of module | ||
| """ | ||
|
|
||
| # create list with parameters that become overridden | ||
| overrideKeys = [item for item in overrideParameters] | ||
|
|
||
| # loop through sections of the configuration of the module | ||
| for section in cfg.sections(): | ||
| for key in cfg[section]: | ||
| if key in overrideKeys: | ||
| cfg.set(section, key, overrideParameters[key]) | ||
| log.info('Override parameter: %s in section: %s with %s' % (key, section, str(overrideParameters[key]))) | ||
| else: | ||
| overrideParameters[key] = cfg[section][key] | ||
|
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. This updates the overrideParameters (which is the override cfg object) without you returning it as an output... I works but I am not a big fan of something happening without me knowing it. Should we change this? |
||
| log.debug('Added %s: %s to override parameters ' % (key, cfg[section][key])) | ||
|
|
||
| return cfg | ||
|
|
||
|
|
||
| def getDefaultModuleConfig(module, toPrint=True): | ||
| ''' Returns the default configuration for a given module | ||
| returns a configParser object | ||
|
|
@@ -240,7 +279,7 @@ def compareConfig(iniFile, modName, compare, modInfo=False, toPrint=True): | |
| return cfg, modDict | ||
|
|
||
|
|
||
| def writeCfgFile(avaDir, module, cfg, fileName=''): | ||
| def writeCfgFile(avaDir, module, cfg, fileName='', filePath=''): | ||
fso42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ Save configuration used to text file in Outputs as moduleName_settings.ini | ||
| or optional in Outputs/moduleName/configurationFiles/filenName.ini | ||
|
|
||
|
|
@@ -254,6 +293,8 @@ def writeCfgFile(avaDir, module, cfg, fileName=''): | |
| configuration settings | ||
| fileName: str | ||
| name of saved configuration file - optional | ||
| filePath: str or pathlib path | ||
| path where file should be saved to except file name - optional | ||
|
|
||
| """ | ||
|
|
||
|
|
@@ -264,18 +305,31 @@ def writeCfgFile(avaDir, module, cfg, fileName=''): | |
| # write to file | ||
| if fileName != '': | ||
| # set outputs | ||
| outDir = pathlib.Path(avaDir, 'Outputs', modName, 'configurationFiles') | ||
| fU.makeADir(outDir) | ||
| if filePath == '': | ||
| outDir = pathlib.Path(avaDir, 'Outputs', modName, 'configurationFiles') | ||
| fU.makeADir(outDir) | ||
| else: | ||
| if filePath.is_dir(): | ||
fso42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| outDir = pathlib.Path(filePath) | ||
| else: | ||
| message = '%s is not a valid location for saving cfg file' % str(filePath) | ||
| log.error(message) | ||
| raise NotADirectoryError(message) | ||
| cfg.optionxform = str | ||
| with open(pathlib.Path(outDir, '%s.ini' % (fileName)), 'w') as conf: | ||
| pathToFile = pathlib.Path(outDir, '%s.ini' % (fileName)) | ||
| with open(pathToFile, 'w') as conf: | ||
| cfg.write(conf) | ||
| else: | ||
| # set outputs | ||
| outDir = pathlib.Path(avaDir, 'Outputs') | ||
| fU.makeADir(outDir) | ||
| cfg.optionxform = str | ||
| with open(pathlib.Path(outDir, '%s_settings.ini' % (modName)), 'w') as conf: | ||
| pathToFile = pathlib.Path(outDir, '%s_settings.ini' % (modName)) | ||
| with open(pathToFile, 'w') as conf: | ||
| cfg.write(conf) | ||
|
|
||
| return pathToFile | ||
|
|
||
|
|
||
| def readCfgFile(avaDir, module='', fileName=''): | ||
| """ Read configuration from ini file, if module is provided, module configuration is read from Ouputs, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.