Skip to content

Commit

Permalink
change parsing to determine expected type ahead of looping user dict,…
Browse files Browse the repository at this point in the history
… and convert single arguments to list to type check against lists, this allows multiple types to be specified as acceptable arguments.
  • Loading branch information
amoodie committed May 7, 2020
1 parent 246068e commit 45d3dba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 46 deletions.
56 changes: 28 additions & 28 deletions pyDeltaRCM/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ verbose:
type: 'int'
default: 0
seed:
type: 'int'
default: -1
type: ['int', 'None']
default: null
Length:
type: 'float'
type: ['float', 'int']
default: 1000.
Width:
type: 'float'
type: ['float', 'int']
default: 2000.
dx:
type: 'float'
type: ['float', 'int']
default: 50.
L0_meters:
type: 'float'
type: ['float', 'int']
default: 150.0
S0:
type: 'float'
type: ['float', 'int']
default: 0.0002
itermax:
type: 'int'
Expand All @@ -35,40 +35,40 @@ Np_water:
type: 'int'
default: 2000
u0:
type: 'float'
type: ['float', 'int']
default: 1.0
N0_meters:
type: 'float'
type: ['float', 'int']
default: 200.0
h0:
type: 'float'
type: ['float', 'int']
default: 5.0
H_SL:
type: 'float'
type: ['float', 'int']
default: 0.0
SLR:
type: 'float'
type: ['float', 'int']
default: 0.0
Np_sed:
type: 'int'
default: 1000
f_bedload:
type: 'float'
type: ['float', 'int']
default: 0.5
C0_percent:
type: 'float'
type: ['float', 'int']
default: 0.1
Csmooth:
type: 'float'
type: ['float', 'int']
default: 0.9
toggle_subsidence:
type: 'bool'
default: False
sigma_max:
type: 'float'
type: ['float', 'int']
default: 0.000825
start_subsidence:
type: 'float'
type: ['float', 'int']
default: 2.5
save_eta_figs:
type: 'bool'
Expand Down Expand Up @@ -101,44 +101,44 @@ save_velocity_grids:
type: 'bool'
default: True
save_dt:
type: 'float'
type: 'int'
default: 50
save_strata:
type: 'bool'
default: True
omega_sfc:
type: 'float'
type: ['float', 'int']
default: 0.1
omega_flow:
type: 'float'
type: ['float', 'int']
default: 0.9
Nsmooth:
type: 'int'
default: 5
theta_water:
type: 'float'
type: ['float', 'int']
default: 1.0
coeff_theta_sand:
type: 'float'
type: ['float', 'int']
default: 2.0
coeff_theta_mud:
type: 'float'
type: ['float', 'int']
default: 1.0
beta:
type: 'int'
default: 3
sed_lag:
type: 'float'
type: ['float', 'int']
default: 1.0
coeff_U_dep_mud:
type: 'float'
type: ['float', 'int']
default: 0.3
coeff_U_ero_mud:
type: 'float'
type: ['float', 'int']
default: 1.5
coeff_U_ero_sand:
type: 'float'
type: ['float', 'int']
default: 1.05
alpha:
type: 'float'
type: ['float', 'int']
default: 0.1
47 changes: 29 additions & 18 deletions pyDeltaRCM/init_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ def import_files(self):
input_file_vars = dict()

# Open and access both yaml files --> put in dictionaries
# only access the user input file if provided.
default_file = open(self.default_file, mode = 'r')
default_dict = yaml.load(default_file, Loader = yaml.FullLoader)
# parse default yaml and find expected types
default_file = open(self.default_file, mode='r')
default_dict = yaml.load(default_file, Loader=yaml.FullLoader)
default_file.close()
if self.input_file: # and os.path.exists(self.input_file):
for k, v in default_dict.items():
if not type(v['type']) is list:
default_dict[k]['type'] = [eval(v['type'])]
else:
default_dict[k]['type'] = [eval(_v) for _v in v['type']]

# only access the user input file if provided.
if self.input_file:
try:
user_file = open(self.input_file, mode = 'r')
user_dict = yaml.load(user_file, Loader = yaml.FullLoader)
user_file = open(self.input_file, mode='r')
user_dict = yaml.load(user_file, Loader=yaml.FullLoader)
user_file.close()
except ValueError as e:
raise e
Expand All @@ -67,25 +74,29 @@ def import_files(self):

# go through and populate input vars with user and default values,
# checking user values for correct type.
for oo in default_dict.keys():
if oo in user_dict:
expected_type = eval(default_dict[oo]['type'])
if type(user_dict[oo]) is expected_type:
input_file_vars[oo] = user_dict[oo]
for k, v in default_dict.items():
if k in user_dict:
expected_type = v['type']
if type(user_dict[k]) in expected_type:
input_file_vars[k] = user_dict[k]
else:
raise TypeError('Input for "{_oo}" not of the right type. '
'Input type was {_wastype}, '
'but needs to be {_exptype}.'
.format(_oo = str(oo), _wastype = type(oo),
_exptype = expected_type))
raise TypeError('Input for "{_k}" not of the right type '
'in yaml configuration file "{_file}". '
'Input type was "{_wastype}", '
'but needs to be "{_exptype}".'
.format(_k=str(k), _file=self.input_file,
_wastype=type(k).__name__,
_exptype=expected_type))
else:
input_file_vars[oo] = default_dict[oo]['default']
input_file_vars[k] = default_dict[k]['default']

# now make each one an attribute of the model object.
for k, v in list(input_file_vars.items()):
setattr(self, k, v)

if self.seed >= 0:
if self.seed is not None:
if self.verbose >= 2:
print("setting random seed to %s " % str(self.seed))
np.random.seed(self.seed)

def set_constants(self):
Expand Down

0 comments on commit 45d3dba

Please sign in to comment.