Skip to content

Commit 3d43d40

Browse files
committed
Optimization result method to tretrieve names of parameters adjusted from metadata
1 parent 6ecd846 commit 3d43d40

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

prms_python/optimizer.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ def monte_carlo(self, reference_path, param_names, statvar_name, \
108108
'''
109109
if '_' in stage:
110110
raise ValueError('stage name cannot contain an underscore')
111-
# assign the optimization object a copy of measured srad for plots
112-
self.measured_arb = pd.Series.from_csv(
113-
reference_path, parse_dates=True
114-
)
111+
# assign the optimization object a copy of measured data for plots
112+
self.measured_arb = pd.Series.from_csv(reference_path,\
113+
parse_dates=True)
115114
# statistical variable output name
116115
self.statvar_name = statvar_name
117116

@@ -149,7 +148,7 @@ def monte_carlo(self, reference_path, param_names, statvar_name, \
149148
end_time = dt.datetime.now()
150149
end_time = end_time.replace(second=0, microsecond=0)
151150

152-
# json metadata for Monte Carlo method
151+
# json metadata for Monte Carlo run
153152
meta = { 'params_adjusted' : param_names,
154153
'statvar_name' : self.statvar_name,
155154
'optimization_title' : self.title,
@@ -424,7 +423,7 @@ def resample_param(params, param_name, how='uniform', noise_factor=0.1):
424423
nhru == params.dimensions[dimnames[0]]):
425424
dim_case = 'nhru_nmonths'
426425
elif not dim_case:
427-
raise ValueError('The {} parameter should not be resampled'.\
426+
raise ValueError('The {} parameter is not set for resampling'.\
428427
format(param_name))
429428
# #testing purposes
430429
# print('name: ', param_name)
@@ -499,8 +498,8 @@ def __init__(self, working_dir, stage):
499498
self.working_dir = working_dir
500499
self.stage = stage
501500
self.metadata_json_paths = self._get_optr_jsons(working_dir, stage)
502-
self.statvar_name = self.get_statvar_name(stage)
503-
self.measured = self.get_measured(stage)
501+
self.statvar_name = self._get_statvar_name(stage)
502+
self.measured = self._get_measured(stage)
504503
self.input_dir = self._get_input_dir(stage)
505504
self.input_params = self._get_input_params(stage)
506505

@@ -556,7 +555,7 @@ def _get_input_params(self, stage):
556555
param_paths.append(meta_dic['original_params'])
557556
return list(set(param_paths))
558557

559-
def get_sim_dirs(self, stage):
558+
def _get_sim_dirs(self, stage):
560559
jsons = self.metadata_json_paths[stage]
561560
json_files = []
562561
sim_dirs = []
@@ -568,23 +567,23 @@ def get_sim_dirs(self, stage):
568567
# list of all simulation directory paths for stage
569568
return sim_dirs
570569

571-
def get_measured(self, stage):
570+
def _get_measured(self, stage):
572571
# only need to open one json file to get this information
573572
if not self.metadata_json_paths.get(stage):
574573
return # no optimization json files exist for given stage
575574
first_json = self.metadata_json_paths[stage][0]
576575
with open(first_json) as json_file:
577576
json_data = json.load(json_file)
578577
measured_series = pd.Series.from_csv(json_data.get('measured'),\
579-
parse_dates=True)
578+
parse_dates=True)
580579
return measured_series
581580

582-
def get_statvar_name(self, stage):
581+
def _get_statvar_name(self, stage):
583582
# only need to open one json file to get this information
584583
try:
585584
first_json = self.metadata_json_paths[stage][0]
586585
except:
587-
raise ValueError("""No optimizatin has been run for
586+
raise ValueError("""No optimization has been run for
588587
stage: {}""".format(stage))
589588
with open(first_json) as json_file:
590589
json_data = json.load(json_file)
@@ -593,13 +592,13 @@ def get_statvar_name(self, stage):
593592
return var_name
594593

595594
def result_table(self, freq='daily', top_n=5, latex=False):
596-
##TODO: add stats for freq options monthly, annual (means or sum)
595+
##TODO: add stats for freq options annual (means or sum)
597596

598-
sim_dirs = self.get_sim_dirs(self.stage)
597+
sim_dirs = self._get_sim_dirs(self.stage)
599598
if top_n >= len(sim_dirs): top_n = len(sim_dirs)
600599
sim_names = [path.split(os.sep)[-1] for path in sim_dirs]
601-
meas_var = self.get_measured(self.stage)
602-
statvar_name = self.get_statvar_name(self.stage)
600+
meas_var = self._get_measured(self.stage)
601+
statvar_name = self._get_statvar_name(self.stage)
603602
result_df = pd.DataFrame(columns=\
604603
['NSE','RMSE','PBIAS','COEF_DET','ABS(PBIAS)'])
605604
for i, sim in enumerate(sim_dirs):
@@ -634,21 +633,30 @@ def result_table(self, freq='daily', top_n=5, latex=False):
634633
else: return sorted_result[:top_n]
635634

636635
def get_top_ranked_sims(self, sorted_df):
637-
## use result table to make dic with best param and statvar paths
636+
# use result table to make dic with best param and statvar paths
638637
# index of table is the simulation directory names
639638
ret = {
640639
'dir_name' : [],
641640
'param_path' : [],
642-
'statvar_path' : []
641+
'statvar_path' : [],
642+
'params_adjusted' : []
643643
}
644-
644+
645+
json_paths = self.metadata_json_paths[self.stage]
646+
645647
for i,el in enumerate(sorted_df.index):
646648
ret['dir_name'].append(el)
647649
ret['param_path'].append(OPJ(self.working_dir,el,'inputs',\
648650
'parameters'))
649651
ret['statvar_path'].append(OPJ(self.working_dir,el,'outputs',\
650652
'statvar.dat'))
651-
653+
for f in json_paths:
654+
with open(f) as fh:
655+
json_data = json.load(fh)
656+
if OPJ(self.working_dir, el) in json_data.get('sim_dirs'):
657+
ret['params_adjusted'].append(\
658+
json_data.get('params_adjusted'))
659+
652660
return ret
653661

654662

prms_python/simulation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import print_function
22
import glob
33
import multiprocessing as mp
4-
#import multiprocess as mp
54
import os
65
import shutil
76
import subprocess

0 commit comments

Comments
 (0)