@@ -108,10 +108,9 @@ def monte_carlo(self, reference_path, param_names, statvar_name, \ |
108 | 108 | ''' |
109 | 109 | if '_' in stage: |
110 | 110 | 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) |
115 | 114 | # statistical variable output name |
116 | 115 | self.statvar_name = statvar_name |
117 | 116 |
|
@@ -149,7 +148,7 @@ def monte_carlo(self, reference_path, param_names, statvar_name, \ |
149 | 148 | end_time = dt.datetime.now() |
150 | 149 | end_time = end_time.replace(second=0, microsecond=0) |
151 | 150 |
|
152 | | - # json metadata for Monte Carlo method |
| 151 | + # json metadata for Monte Carlo run |
153 | 152 | meta = { 'params_adjusted' : param_names, |
154 | 153 | 'statvar_name' : self.statvar_name, |
155 | 154 | 'optimization_title' : self.title, |
@@ -424,7 +423,7 @@ def resample_param(params, param_name, how='uniform', noise_factor=0.1): |
424 | 423 | nhru == params.dimensions[dimnames[0]]): |
425 | 424 | dim_case = 'nhru_nmonths' |
426 | 425 | elif not dim_case: |
427 | | - raise ValueError('The {} parameter should not be resampled'.\ |
| 426 | + raise ValueError('The {} parameter is not set for resampling'.\ |
428 | 427 | format(param_name)) |
429 | 428 | # #testing purposes |
430 | 429 | # print('name: ', param_name) |
@@ -499,8 +498,8 @@ def __init__(self, working_dir, stage): |
499 | 498 | self.working_dir = working_dir |
500 | 499 | self.stage = stage |
501 | 500 | 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) |
504 | 503 | self.input_dir = self._get_input_dir(stage) |
505 | 504 | self.input_params = self._get_input_params(stage) |
506 | 505 |
|
@@ -556,7 +555,7 @@ def _get_input_params(self, stage): |
556 | 555 | param_paths.append(meta_dic['original_params']) |
557 | 556 | return list(set(param_paths)) |
558 | 557 |
|
559 | | - def get_sim_dirs(self, stage): |
| 558 | + def _get_sim_dirs(self, stage): |
560 | 559 | jsons = self.metadata_json_paths[stage] |
561 | 560 | json_files = [] |
562 | 561 | sim_dirs = [] |
@@ -568,23 +567,23 @@ def get_sim_dirs(self, stage): |
568 | 567 | # list of all simulation directory paths for stage |
569 | 568 | return sim_dirs |
570 | 569 |
|
571 | | - def get_measured(self, stage): |
| 570 | + def _get_measured(self, stage): |
572 | 571 | # only need to open one json file to get this information |
573 | 572 | if not self.metadata_json_paths.get(stage): |
574 | 573 | return # no optimization json files exist for given stage |
575 | 574 | first_json = self.metadata_json_paths[stage][0] |
576 | 575 | with open(first_json) as json_file: |
577 | 576 | json_data = json.load(json_file) |
578 | 577 | measured_series = pd.Series.from_csv(json_data.get('measured'),\ |
579 | | - parse_dates=True) |
| 578 | + parse_dates=True) |
580 | 579 | return measured_series |
581 | 580 |
|
582 | | - def get_statvar_name(self, stage): |
| 581 | + def _get_statvar_name(self, stage): |
583 | 582 | # only need to open one json file to get this information |
584 | 583 | try: |
585 | 584 | first_json = self.metadata_json_paths[stage][0] |
586 | 585 | except: |
587 | | - raise ValueError("""No optimizatin has been run for |
| 586 | + raise ValueError("""No optimization has been run for |
588 | 587 | stage: {}""".format(stage)) |
589 | 588 | with open(first_json) as json_file: |
590 | 589 | json_data = json.load(json_file) |
@@ -593,13 +592,13 @@ def get_statvar_name(self, stage): |
593 | 592 | return var_name |
594 | 593 |
|
595 | 594 | 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) |
597 | 596 |
|
598 | | - sim_dirs = self.get_sim_dirs(self.stage) |
| 597 | + sim_dirs = self._get_sim_dirs(self.stage) |
599 | 598 | if top_n >= len(sim_dirs): top_n = len(sim_dirs) |
600 | 599 | 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) |
603 | 602 | result_df = pd.DataFrame(columns=\ |
604 | 603 | ['NSE','RMSE','PBIAS','COEF_DET','ABS(PBIAS)']) |
605 | 604 | for i, sim in enumerate(sim_dirs): |
@@ -634,21 +633,30 @@ def result_table(self, freq='daily', top_n=5, latex=False): |
634 | 633 | else: return sorted_result[:top_n] |
635 | 634 |
|
636 | 635 | 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 |
638 | 637 | # index of table is the simulation directory names |
639 | 638 | ret = { |
640 | 639 | 'dir_name' : [], |
641 | 640 | 'param_path' : [], |
642 | | - 'statvar_path' : [] |
| 641 | + 'statvar_path' : [], |
| 642 | + 'params_adjusted' : [] |
643 | 643 | } |
644 | | - |
| 644 | + |
| 645 | + json_paths = self.metadata_json_paths[self.stage] |
| 646 | + |
645 | 647 | for i,el in enumerate(sorted_df.index): |
646 | 648 | ret['dir_name'].append(el) |
647 | 649 | ret['param_path'].append(OPJ(self.working_dir,el,'inputs',\ |
648 | 650 | 'parameters')) |
649 | 651 | ret['statvar_path'].append(OPJ(self.working_dir,el,'outputs',\ |
650 | 652 | '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 | + |
652 | 660 | return ret |
653 | 661 |
|
654 | 662 |
|