def main2(): seims_cfg = ParseSEIMSConfig() seims_cfg.db_name = 'youwuzhen10m_longterm_model' thresholds = {18: [18, 72, 288, 0.1, -0.1]} fuzzyslppos_fnames = [(1, 'summit', 'rdgInf'), (4, 'backslope', 'bksInf'), (16, 'valley', 'vlyInf')] outfname = 'SLPPOS_UNITS_10121314' hillslp_data = DelinateSlopePositionByThreshold(seims_cfg, thresholds, fuzzyslppos_fnames, outfname) print(hillslp_data)
def __init__(self, cf, method='nsga2'): # type: (ConfigParser, str) -> None """Initialization.""" # 1. SEIMS model related self.model = ParseSEIMSConfig(cf) # 2. Common settings of auto-calibration if 'CALI_Settings' not in cf.sections(): raise ValueError("[CALI_Settings] section MUST be existed in *.ini file.") self.param_range_def = 'cali_param_rng.def' if cf.has_option('CALI_Settings', 'paramrngdef'): self.param_range_def = cf.get('CALI_Settings', 'paramrngdef') self.param_range_def = self.model.model_dir + os.path.sep + self.param_range_def if not FileClass.is_file_exists(self.param_range_def): raise IOError('Ranges of parameters MUST be provided!') # UTCTIME of calibration and validation (optional) periods if not (cf.has_option('CALI_Settings', 'cali_time_start') and cf.has_option('CALI_Settings', 'cali_time_end')): raise ValueError("Start and end time of Calibration " "MUST be specified in [CALI_Settings].") self.cali_stime = parse_datetime_from_ini(cf, 'CALI_Settings', 'cali_time_start') self.cali_etime = parse_datetime_from_ini(cf, 'CALI_Settings', 'cali_time_end') self.vali_stime = parse_datetime_from_ini(cf, 'CALI_Settings', 'vali_time_start') self.vali_etime = parse_datetime_from_ini(cf, 'CALI_Settings', 'vali_time_end') self.calc_validation = True if self.vali_stime and self.vali_etime else False if self.cali_stime >= self.cali_etime or (self.calc_validation and self.vali_stime >= self.vali_etime): raise ValueError("Wrong time settings in [CALI_Settings]!") # 3. Parameters settings for specific optimization algorithm self.opt_mtd = method self.opt = None if self.opt_mtd == 'nsga2': self.opt = ParseNSGA2Config(cf, self.model.model_dir, 'CALI_NSGA2_Gen_%d_Pop_%d')
def __init__(self, cf, method='morris'): """Initialization.""" self.method = method # 1. SEIMS model related self.model = ParseSEIMSConfig(cf) # 2. Common settings of parameters sensitivity analysis if 'PSA_Settings' not in cf.sections(): raise ValueError( "[PSA_Settings] section MUST be existed in *.ini file.") self.evaluate_params = list() if cf.has_option('PSA_Settings', 'evaluateparam'): eva_str = cf.get('PSA_Settings', 'evaluateparam') self.evaluate_params = StringClass.split_string(eva_str, ',') else: self.evaluate_params = ['Q'] # Default self.param_range_def = 'morris_param_rng.def' # Default if cf.has_option('PSA_Settings', 'paramrngdef'): self.param_range_def = cf.get('PSA_Settings', 'paramrngdef') self.param_range_def = self.model.model_dir + os.path.sep + self.param_range_def if not FileClass.is_file_exists(self.param_range_def): raise IOError('Ranges of parameters MUST be provided!') if not (cf.has_option('PSA_Settings', 'psa_time_start') and cf.has_option('PSA_Settings', 'psa_time_end')): raise ValueError( "Start and end time of PSA MUST be specified in [PSA_Settings]." ) try: # UTCTIME tstart = cf.get('PSA_Settings', 'psa_time_start') tend = cf.get('PSA_Settings', 'psa_time_end') self.psa_stime = StringClass.get_datetime(tstart) self.psa_etime = StringClass.get_datetime(tend) except ValueError: raise ValueError('The time format MUST be"YYYY-MM-DD HH:MM:SS".') if self.psa_stime >= self.psa_etime: raise ValueError("Wrong time settings in [PSA_Settings]!") # 3. Parameters settings for specific sensitivity analysis methods self.morris = None self.fast = None if self.method == 'fast': self.fast = FASTConfig(cf) self.psa_outpath = '%s/PSA_FAST_N%dM%d' % ( self.model.model_dir, self.fast.N, self.fast.M) elif self.method == 'morris': self.morris = MorrisConfig(cf) self.psa_outpath = '%s/PSA_Morris_N%dL%d' % ( self.model.model_dir, self.morris.N, self.morris.num_levels) # 4. (Optional) Plot settings for matplotlib self.plot_cfg = PlotConfig(cf) # Do not remove psa_outpath if already existed UtilClass.mkdir(self.psa_outpath) self.outfiles = PSAOutputs(self.psa_outpath)
def write_runmodel_config_file(mpaths, org_file_name): org_cfg_file = mpaths.cfg_dir + os.path.sep + org_file_name runmodel_cfg_file = mpaths.workspace + os.path.sep + org_file_name cfg_items = list() with open(org_cfg_file, 'r', encoding='utf-8') as f: for line in f.readlines(): cfg_items.append(line.strip()) cfg_items.append('MODEL_DIR = %s' % mpaths.model_dir) cfg_items.append('BIN_DIR = %s' % mpaths.bin_dir) with open(runmodel_cfg_file, 'w', encoding='utf-8') as f: for item in cfg_items: f.write('%s\n' % item) cf = ConfigParser() cf.read(runmodel_cfg_file) return ParseSEIMSConfig(cf)
def __init__(self, cf, method='nsga2'): """Initialization.""" # 1. SEIMS model related self.model = ParseSEIMSConfig(cf) # 2. Common settings of auto-calibration if 'CALI_Settings' not in cf.sections(): raise ValueError( "[CALI_Settings] section MUST be existed in *.ini file.") self.param_range_def = 'cali_param_rng.def' if cf.has_option('CALI_Settings', 'paramrngdef'): self.param_range_def = cf.get('CALI_Settings', 'paramrngdef') self.param_range_def = self.model.model_dir + os.path.sep + self.param_range_def if not FileClass.is_file_exists(self.param_range_def): raise IOError('Ranges of parameters MUST be provided!') if not (cf.has_option('CALI_Settings', 'cali_time_start') and cf.has_option('CALI_Settings', 'cali_time_end')): raise ValueError("Start and end time of Calibration " "MUST be specified in [CALI_Settings].") try: # UTCTIME tstart = cf.get('CALI_Settings', 'cali_time_start') tend = cf.get('CALI_Settings', 'cali_time_end') self.cali_stime = StringClass.get_datetime(tstart) self.cali_etime = StringClass.get_datetime(tend) self.calc_validation = False if cf.has_option('CALI_Settings', 'vali_time_start') and \ cf.has_option('CALI_Settings', 'vali_time_end'): tstart = cf.get('CALI_Settings', 'vali_time_start') tend = cf.get('CALI_Settings', 'vali_time_end') self.vali_stime = StringClass.get_datetime(tstart) self.vali_etime = StringClass.get_datetime(tend) self.calc_validation = True except ValueError: raise ValueError( 'The time format MUST be "YYYY-MM-DD" or "YYYY-MM-DD HH:MM:SS".' ) if self.cali_stime >= self.cali_etime or ( self.calc_validation and self.vali_stime >= self.vali_etime): raise ValueError("Wrong time setted in [CALI_Settings]!") # 3. Parameters settings for specific optimization algorithm self.opt_mtd = method self.opt = None if self.opt_mtd == 'nsga2': self.opt = ParseNSGA2Config(cf, self.model.model_dir)
def __init__(self, cf): """Initialization.""" # 1. SEIMS model related self.model_cfg = ParseSEIMSConfig(cf) # 2. Parameters self.plt_subbsnid = -1 self.plt_vars = list() if 'PARAMETERS' in cf.sections(): self.plt_subbsnid = cf.getint('PARAMETERS', 'plot_subbasinid') plt_vars_str = cf.get('PARAMETERS', 'plot_variables') else: raise ValueError( "[PARAMETERS] section MUST be existed in *.ini file.") if self.plt_subbsnid < 0: raise ValueError( "PLOT_SUBBASINID must be greater or equal than 0.") if plt_vars_str != '': self.plt_vars = StringClass.split_string(plt_vars_str) else: raise ValueError("PLOT_VARIABLES illegal defined in [PARAMETERS]!") # 3. Optional_Parameters if 'OPTIONAL_PARAMETERS' not in cf.sections(): raise ValueError( "[OPTIONAL_PARAMETERS] section MUST be existed in *.ini file.") # UTCTIME self.cali_stime = parse_datetime_from_ini(cf, 'OPTIONAL_PARAMETERS', 'cali_time_start') self.cali_etime = parse_datetime_from_ini(cf, 'OPTIONAL_PARAMETERS', 'cali_time_end') self.vali_stime = parse_datetime_from_ini(cf, 'OPTIONAL_PARAMETERS', 'vali_time_start') self.vali_etime = parse_datetime_from_ini(cf, 'OPTIONAL_PARAMETERS', 'vali_time_end') if not self.cali_stime or not self.cali_etime or self.cali_stime >= self.cali_etime: raise ValueError( "Wrong time settings of calibration in [OPTIONAL_PARAMETERS]!") if self.vali_stime and self.vali_etime and self.vali_stime >= self.vali_etime: raise ValueError( "Wrong time settings of validation in [OPTIONAL_PARAMETERS]!") # 4. Switches self.lang_cn = False if 'SWITCH' in cf.sections(): self.lang_cn = cf.getboolean('SWITCH', 'lang_cn')
def __init__(self, cf, method='nsga2'): # type: (ConfigParser, str) -> None """Initialization.""" # 1. SEIMS model related self.model = ParseSEIMSConfig(cf) # type: ParseSEIMSConfig # 2. Common settings of BMPs scenario self.eval_stime = None # type: Optional[datetime] self.eval_etime = None # type: Optional[datetime] self.worst_econ = 0. self.worst_env = 0. self.runtime_years = 0. self.export_sce_txt = False self.export_sce_tif = False if 'Scenario_Common' not in cf.sections(): raise ValueError( '[Scenario_Common] section MUST be existed in *.ini file.') self.eval_stime = parse_datetime_from_ini(cf, 'Scenario_Common', 'eval_time_start') self.eval_etime = parse_datetime_from_ini(cf, 'Scenario_Common', 'eval_time_end') self.worst_econ = cf.getfloat('Scenario_Common', 'worst_economy') self.worst_env = cf.getfloat('Scenario_Common', 'worst_environment') self.runtime_years = cf.getfloat('Scenario_Common', 'runtime_years') if cf.has_option('Scenario_Common', 'export_scenario_txt'): self.export_sce_txt = cf.getboolean('Scenario_Common', 'export_scenario_txt') if cf.has_option('Scenario_Common', 'export_scenario_tif'): self.export_sce_tif = cf.getboolean('Scenario_Common', 'export_scenario_tif') # 3. Application specific setting section [BMPs] # Selected BMPs, the key is BMPID, and value is the BMP information dict self.bmps_info = dict( ) # type: Dict[int, Dict[AnyStr, Union[int, float, AnyStr, List[Union[int, float, AnyStr]]]]] # BMPs to be constant for generated scenarios during optimization, same format with bmps_info self.bmps_retain = dict( ) # type: Dict[int, Dict[AnyStr, Union[int, float, AnyStr, List[Union[int, float, AnyStr]]]]] self.eval_info = dict( ) # type: Dict[AnyStr, Union[int, float, AnyStr]] self.bmps_cfg_unit = 'CONNFIELD' # type: AnyStr self.bmps_cfg_method = 'RAND' # type: AnyStr if 'BMPs' not in cf.sections(): raise ValueError( '[BMPs] section MUST be existed for specific scenario analysis.' ) bmpsinfostr = cf.get('BMPs', 'bmps_info') self.bmps_info = UtilClass.decode_strs_in_dict(json.loads(bmpsinfostr)) if cf.has_option('BMPs', 'bmps_retain'): bmpsretainstr = cf.get('BMPs', 'bmps_retain') self.bmps_retain = json.loads(bmpsretainstr) self.bmps_retain = UtilClass.decode_strs_in_dict(self.bmps_retain) evalinfostr = cf.get('BMPs', 'eval_info') self.eval_info = UtilClass.decode_strs_in_dict(json.loads(evalinfostr)) bmpscfgunitstr = cf.get('BMPs', 'bmps_cfg_units') bmpscfgunitdict = UtilClass.decode_strs_in_dict( json.loads(bmpscfgunitstr)) for unitname, unitcfg in viewitems(bmpscfgunitdict): self.bmps_cfg_unit = unitname if self.bmps_cfg_unit not in BMPS_CFG_UNITS: raise ValueError('BMPs configuration unit MUST be ' 'one of %s' % BMPS_CFG_UNITS.__str__()) if not isinstance(unitcfg, dict): raise ValueError( 'The value of BMPs configuration unit MUST be dict value!') for cfgname, cfgvalue in viewitems(unitcfg): for bmpid, bmpdict in viewitems(self.bmps_info): if cfgname in bmpdict: continue self.bmps_info[bmpid][cfgname] = cfgvalue break if cf.has_option('BMPs', 'bmps_cfg_method'): self.bmps_cfg_method = cf.get('BMPs', 'bmps_cfg_method') if self.bmps_cfg_method not in BMPS_CFG_METHODS: print('BMPs configuration method MUST be one of %s' % BMPS_CFG_METHODS.__str__()) self.bmps_cfg_method = 'RAND' # Check the validation of configuration unit and method if self.bmps_cfg_method not in BMPS_CFG_PAIR.get(self.bmps_cfg_unit): raise ValueError('BMPs configuration method %s ' 'is not supported on unit %s' % (self.bmps_cfg_method, self.bmps_cfg_unit)) # Optimize boundary of BMP configuration unit self.boundary_adaptive = False self.boundary_adaptive_threshs = None if cf.has_option('BMPs', 'bmps_cfg_units_opt'): self.boundary_adaptive = cf.getboolean('BMPs', 'bmps_cfg_units_opt') if cf.has_option('BMPs', 'boundary_adaptive_threshold'): tstr = cf.get('BMPs', 'boundary_adaptive_threshold') self.boundary_adaptive_threshs = StringClass.extract_numeric_values_from_string( tstr) if 0 not in self.boundary_adaptive_threshs: self.boundary_adaptive_threshs.append( 0) # 0 means no adjustment of boundary for tmp_thresh in self.boundary_adaptive_threshs: if -1 * tmp_thresh not in self.boundary_adaptive_threshs: self.boundary_adaptive_threshs.append(-1 * tmp_thresh) # 4. Parameters settings for specific optimization algorithm self.opt_mtd = method self.opt = None # type: Union[ParseNSGA2Config, None] if self.opt_mtd == 'nsga2': self.opt = ParseNSGA2Config( cf, self.model.model_dir, 'SA_NSGA2_%s_%s' % (self.bmps_cfg_unit, self.bmps_cfg_method)) # Using the existed population derived from previous scenario optimization self.initial_byinput = cf.getboolean(self.opt_mtd.upper(), 'inputpopulation') if \ cf.has_option(self.opt_mtd.upper(), 'inputpopulation') else False self.input_pareto_file = None self.input_pareto_gen = -1 if cf.has_option(self.opt_mtd.upper(), 'paretofrontsfile'): self.input_pareto_file = cf.get(self.opt_mtd.upper(), 'paretofrontsfile') if cf.has_option(self.opt_mtd.upper(), 'generationselected'): self.input_pareto_gen = cf.getint(self.opt_mtd.upper(), 'generationselected') self.scenario_dir = self.opt.out_dir + os.path.sep + 'Scenarios' UtilClass.rmmkdir(self.scenario_dir) # 5. (Optional) Plot settings for matplotlib self.plot_cfg = PlotConfig(cf)
def __init__(self, cf, method='nsga2'): # type: (ConfigParser, str) -> None """Initialization.""" # 1. SEIMS model related self.model = ParseSEIMSConfig(cf) # 2. Common settings of BMPs scenario self.eval_stime = None self.eval_etime = None self.worst_econ = 0. self.worst_env = 0. self.runtime_years = 0. self.export_sce_txt = False self.export_sce_tif = False if 'Scenario_Common' not in cf.sections(): raise ValueError( '[Scenario_Common] section MUST be existed in *.ini file.') self.eval_stime = parse_datetime_from_ini(cf, 'Scenario_Common', 'eval_time_start') self.eval_etime = parse_datetime_from_ini(cf, 'Scenario_Common', 'eval_time_end') self.worst_econ = cf.getfloat('Scenario_Common', 'worst_economy') self.worst_env = cf.getfloat('Scenario_Common', 'worst_environment') self.runtime_years = cf.getfloat('Scenario_Common', 'runtime_years') if cf.has_option('Scenario_Common', 'export_scenario_txt'): self.export_sce_txt = cf.getboolean('Scenario_Common', 'export_scenario_txt') if cf.has_option('Scenario_Common', 'export_scenario_tif'): self.export_sce_tif = cf.getboolean('Scenario_Common', 'export_scenario_tif') # 3. Application specific setting section [BMPs] self.bmps_info = dict() # BMP to be evaluated, JSON format self.bmps_retain = dict() # BMPs to be constant, JSON format self.bmps_cfg_unit = 'SLPPOS' self.bmps_cfg_method = 'RDM' if 'BMPs' not in cf.sections(): raise ValueError( '[BMPs] section MUST be existed for specific scenario analysis.' ) bmpsinfostr = cf.get('BMPs', 'bmps_info') self.bmps_info = UtilClass.decode_strs_in_dict(json.loads(bmpsinfostr)) if cf.has_option('BMPs', 'bmps_retain'): bmpsretainstr = cf.get('BMPs', 'bmps_retain') self.bmps_retain = json.loads(bmpsretainstr) self.bmps_retain = UtilClass.decode_strs_in_dict(self.bmps_retain) bmpscfgunitstr = cf.get('BMPs', 'bmps_cfg_units') bmpscfgunitdict = UtilClass.decode_strs_in_dict( json.loads(bmpscfgunitstr)) for unitname, unitcfg in viewitems(bmpscfgunitdict): self.bmps_cfg_unit = unitname if self.bmps_cfg_unit not in BMPS_CFG_UNITS: raise ValueError('BMPs configuration unit MUST be ' 'one of %s' % BMPS_CFG_UNITS.__str__()) if not isinstance(unitcfg, dict): raise ValueError( 'The value of BMPs configuration unit MUST be dict value!') for cfgname, cfgvalue in viewitems(unitcfg): self.bmps_info[cfgname] = cfgvalue break if cf.has_option('BMPs', 'bmps_cfg_method'): self.bmps_cfg_method = cf.get('BMPs', 'bmps_cfg_method') if self.bmps_cfg_method not in BMPS_CFG_METHODS: print('BMPs configuration method MUST be one of %s' % BMPS_CFG_METHODS.__str__()) self.bmps_cfg_method = 'RDM' # Check the validation of configuration unit and method if self.bmps_cfg_method not in BMPS_CFG_PAIR.get(self.bmps_cfg_unit): raise ValueError('BMPs configuration method %s ' 'is not supported on unit %s' % (self.bmps_cfg_method, self.bmps_cfg_unit)) # 4. Parameters settings for specific optimization algorithm self.opt_mtd = method self.opt = None # type: Union[ParseNSGA2Config] if self.opt_mtd == 'nsga2': self.opt = ParseNSGA2Config( cf, self.model.model_dir, 'SA_NSGA2_%s_%s' % (self.bmps_cfg_unit, self.bmps_cfg_method)) self.scenario_dir = self.opt.out_dir + os.path.sep + 'Scenarios' UtilClass.rmmkdir(self.scenario_dir)