Beautify with yapf

master
Dan Howe 3 years ago
parent bb78c46ee7
commit 640b0d8061

@ -51,10 +51,11 @@ def parse_args():
the parsed input arguments in a dict the parsed input arguments in a dict
""" """
parser = argparse.ArgumentParser(usage=__doc__) parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument( parser.add_argument('-f',
'-f', '--file', help='name of parameter file', default=None) '--file',
parser.add_argument( help='name of parameter file',
'-a', default=None)
parser.add_argument('-a',
'--all', '--all',
help='process all *.yaml files in folder', help='process all *.yaml files in folder',
action='store_true') action='store_true')
@ -189,21 +190,21 @@ def get_ongoing_recession(n_runs, start_year, end_year, sea_level_rise,
n_years = len(years) n_years = len(years)
# Interpolate sea level rise projections (m) # Interpolate sea level rise projections (m)
slr_mode = np.interp( slr_mode = np.interp(years,
years, xp=sea_level_rise['year'], xp=sea_level_rise['year'],
fp=sea_level_rise['mode'])[:, np.newaxis] fp=sea_level_rise['mode'])[:, np.newaxis]
try: try:
slr_min = np.interp( slr_min = np.interp(years,
years, xp=sea_level_rise['year'], xp=sea_level_rise['year'],
fp=sea_level_rise['min'])[:, np.newaxis] fp=sea_level_rise['min'])[:, np.newaxis]
except ValueError: except ValueError:
# Use mode for deterministic beaches # Use mode for deterministic beaches
slr_min = slr_mode slr_min = slr_mode
try: try:
slr_max = np.interp( slr_max = np.interp(years,
years, xp=sea_level_rise['year'], xp=sea_level_rise['year'],
fp=sea_level_rise['max'])[:, np.newaxis] fp=sea_level_rise['max'])[:, np.newaxis]
except ValueError: except ValueError:
# Use mode for deterministic beaches # Use mode for deterministic beaches
@ -215,8 +216,7 @@ def get_ongoing_recession(n_runs, start_year, end_year, sea_level_rise,
for i in range(n_years): for i in range(n_years):
# Use triangular distribution for SLR in each year (m) # Use triangular distribution for SLR in each year (m)
try: try:
slr[i, :] = np.random.triangular( slr[i, :] = np.random.triangular(left=slr_min[i],
left=slr_min[i],
mode=slr_mode[i], mode=slr_mode[i],
right=slr_max[i], right=slr_max[i],
size=n_runs) size=n_runs)
@ -236,8 +236,7 @@ def get_ongoing_recession(n_runs, start_year, end_year, sea_level_rise,
# Simulate probabilistic Bruun factors (-) # Simulate probabilistic Bruun factors (-)
if (bruun_factor['min'] < bruun_factor['mode'] < bruun_factor['max']): if (bruun_factor['min'] < bruun_factor['mode'] < bruun_factor['max']):
# Use probabilistic method if min and max are provided # Use probabilistic method if min and max are provided
bruun_factor = np.random.triangular( bruun_factor = np.random.triangular(left=bruun_factor['min'],
left=bruun_factor['min'],
mode=bruun_factor['mode'], mode=bruun_factor['mode'],
right=bruun_factor['max'], right=bruun_factor['max'],
size=n_runs) size=n_runs)
@ -412,8 +411,10 @@ def process(beach_name, beach_scenario, n_runs, start_year, end_year,
if probabilistic: if probabilistic:
for i in range(len(years)): for i in range(len(years)):
# Generate synthetic storm demands for each year # Generate synthetic storm demands for each year
storm_demand_volume[i, :] = get_storm_demand_volume( storm_demand_volume[i, :] = get_storm_demand_volume(ref_aep,
ref_aep, ref_vol, n=n_runs, mode='fit') ref_vol,
n=n_runs,
mode='fit')
else: else:
# Get storm demand for 1% AEP event # Get storm demand for 1% AEP event
sort_idx = np.argsort(ref_aep) sort_idx = np.argsort(ref_aep)
@ -434,20 +435,19 @@ def process(beach_name, beach_scenario, n_runs, start_year, end_year,
col_names = [c for c in df_in.columns if c.isdigit()] col_names = [c for c in df_in.columns if c.isdigit()]
# Loop through profiles # Loop through profiles
pbar_profile = tqdm( pbar_profile = tqdm(df_in[df_in['beach'] == beach_name].iterrows(),
df_in[df_in['beach'] == beach_name].iterrows(),
total=df_in[df_in['beach'] == beach_name].shape[0]) total=df_in[df_in['beach'] == beach_name].shape[0])
for i, prof in pbar_profile: for i, prof in pbar_profile:
pbar_profile.set_description(('Block: {}, profile: {}'.format( pbar_profile.set_description(
prof['block'], prof['profile']))) ('Block: {}, profile: {}'.format(prof['block'],
prof['profile'])))
# Convert storm demand volume to a profile chainage (m) # Convert storm demand volume to a profile chainage (m)
profile_volume = np.array([int(c) for c in col_names]) profile_volume = np.array([int(c) for c in col_names])
profile_chainage = np.array(prof[col_names], dtype=float) profile_chainage = np.array(prof[col_names], dtype=float)
valid_idx = np.isfinite(profile_chainage) valid_idx = np.isfinite(profile_chainage)
storm_demand_chainage = np.interp( storm_demand_chainage = np.interp(storm_demand_volume,
storm_demand_volume,
xp=profile_volume[valid_idx], xp=profile_volume[valid_idx],
fp=profile_chainage[valid_idx]) fp=profile_chainage[valid_idx])
@ -465,31 +465,31 @@ def process(beach_name, beach_scenario, n_runs, start_year, end_year,
for b, p, ch in zip(min_chainage['block'], for b, p, ch in zip(min_chainage['block'],
min_chainage['profile'], min_chainage['profile'],
min_chainage['chainage']): min_chainage['chainage']):
idx = (df_out['block'] == str(b)) & ( idx = (df_out['block'] == str(b)) & (df_out['profile']
df_out['profile'] == p) == p)
df_out.loc[idx, 'min_chainage'] = ch df_out.loc[idx, 'min_chainage'] = ch
# Specify which segments to break # Specify which segments to break
df_out = df_out.assign(segment_gaps=False) df_out = df_out.assign(segment_gaps=False)
for b, p, in zip(segment_gaps['block'], for b, p, in zip(segment_gaps['block'],
segment_gaps['profile']): segment_gaps['profile']):
idx = (df_out['block'] == str(b)) & ( idx = (df_out['block'] == str(b)) & (df_out['profile']
df_out['profile'] == p) == p)
df_out.loc[idx, 'segment_gaps'] = True df_out.loc[idx, 'segment_gaps'] = True
# Specify which profiles to plot # Specify which profiles to plot
df_out = df_out.assign(plot_stats=False) df_out = df_out.assign(plot_stats=False)
for b, p in zip(plot_stats['block'], plot_stats['profile']): for b, p in zip(plot_stats['block'], plot_stats['profile']):
idx = (df_out['block'] == str(b)) & ( idx = (df_out['block'] == str(b)) & (df_out['profile']
df_out['profile'] == p) == p)
df_out.loc[idx, 'plot_stats'] = True df_out.loc[idx, 'plot_stats'] = True
# Specify which profiles to omit from shapefiles # Specify which profiles to omit from shapefiles
df_out = df_out.assign(omit_from_shp=False) df_out = df_out.assign(omit_from_shp=False)
for b, p in zip(omit_from_shp['block'], for b, p in zip(omit_from_shp['block'],
omit_from_shp['profile']): omit_from_shp['profile']):
idx = (df_out['block'] == str(b)) & ( idx = (df_out['block'] == str(b)) & (df_out['profile']
df_out['profile'] == p) == p)
df_out.loc[idx, 'omit_from_shp'] = True df_out.loc[idx, 'omit_from_shp'] = True
# Specify additional points to be included in shapefiles # Specify additional points to be included in shapefiles
@ -497,9 +497,9 @@ def process(beach_name, beach_scenario, n_runs, start_year, end_year,
for b, p, x, y in zip(insert_points['block'], for b, p, x, y in zip(insert_points['block'],
insert_points['profile'], insert_points['profile'],
insert_points['x'], insert_points['y']): insert_points['x'], insert_points['y']):
idx = np.where((df_out['block'] == str(b)) & ( idx = np.where((df_out['block'] == str(b))
df_out['profile'] == p) & ( & (df_out['profile'] == p)
df_out['beach'] == beach_name))[0][0] & (df_out['beach'] == beach_name))[0][0]
if not df_out.loc[idx, 'insert_points']: if not df_out.loc[idx, 'insert_points']:
df_out.loc[idx, 'insert_points'] = [] df_out.loc[idx, 'insert_points'] = []
@ -509,9 +509,9 @@ def process(beach_name, beach_scenario, n_runs, start_year, end_year,
for b, p, x, y in zip(append_points['block'], for b, p, x, y in zip(append_points['block'],
append_points['profile'], append_points['profile'],
append_points['x'], append_points['y']): append_points['x'], append_points['y']):
idx = np.where((df_out['block'] == str(b)) & ( idx = np.where((df_out['block'] == str(b))
df_out['profile'] == p) & ( & (df_out['profile'] == p)
df_out['beach'] == beach_name))[0][0] & (df_out['beach'] == beach_name))[0][0]
if not df_out.loc[idx, 'append_points']: if not df_out.loc[idx, 'append_points']:
df_out.loc[idx, 'append_points'] = [] df_out.loc[idx, 'append_points'] = []
@ -568,16 +568,16 @@ def process(beach_name, beach_scenario, n_runs, start_year, end_year,
df_csv = df_out[df_out['beach'] == beach_name] df_csv = df_out[df_out['beach'] == beach_name]
# Save values for current beach # Save values for current beach
csv_name = os.path.join(output_folder, '{} {} {}.csv'.format( csv_name = os.path.join(
beach_scenario, year, profile_type)) output_folder,
'{} {} {}.csv'.format(beach_scenario, year, profile_type))
# Write header for csv file on first iteration # Write header for csv file on first iteration
if df_out[df_out['beach'] == beach_name].index[0] == i: if df_out[df_out['beach'] == beach_name].index[0] == i:
df_csv.loc[[], :].to_csv(csv_name, index=False) df_csv.loc[[], :].to_csv(csv_name, index=False)
# Append data for current profile # Append data for current profile
df_csv[df_csv.index == i].to_csv( df_csv[df_csv.index == i].to_csv(csv_name,
csv_name,
mode='a', mode='a',
index=False, index=False,
header=False, header=False,

Loading…
Cancel
Save