|
| 1 | +''' |
| 2 | +optimizer.py -- Optimization routines for PRMS parameters and data. |
| 3 | +''' |
| 4 | +import pandas as pd |
| 5 | +import numpy as np |
| 6 | +import os |
| 7 | + |
| 8 | +from .data import Data |
| 9 | +from .parameters import Parameters |
| 10 | +from .scenario import ScenarioSeries |
| 11 | + |
| 12 | + |
| 13 | +class Optimizer: |
| 14 | + ''' |
| 15 | + Container for a PRMS parameter optimization routine consisting of the |
| 16 | + four stages as described in Hay, et al, 2006 |
| 17 | + (ftp://brrftp.cr.usgs.gov/pub/mows/software/luca_s/jawraHay.pdf). |
| 18 | +
|
| 19 | + Example: |
| 20 | +
|
| 21 | + >>> from prms_python import Data, Optimizer, Parameters |
| 22 | + >>> params = Parameters('path/to/parameters') |
| 23 | + >>> data = Data('path/to/data') |
| 24 | + >>> optr = Optimizer(params, data, title='the title', description='desc') |
| 25 | + >>> optr.srad('path/to/reference_data/measured_srad.csv') |
| 26 | +
|
| 27 | + ''' |
| 28 | + |
| 29 | + def __init__(self, parameters, data, working_dir, |
| 30 | + title=None, description=None): |
| 31 | + |
| 32 | + if isinstance(parameters, Parameters): |
| 33 | + self.parameters = parameters |
| 34 | + else: |
| 35 | + raise TypeError('parameters must be instance of Parameters') |
| 36 | + |
| 37 | + if isinstance(data, Data): |
| 38 | + self.data = data |
| 39 | + else: |
| 40 | + raise TypeError('data must be instance of Data') |
| 41 | + |
| 42 | + self.working_dir = working_dir |
| 43 | + self.title = title |
| 44 | + self.description = description |
| 45 | + |
| 46 | + def srad(self, reference_srad_path, station_nhru, method='', |
| 47 | + dday_intcp_range=None, dday_slope_range=None, |
| 48 | + intcp_delta=None, slope_delta=None): |
| 49 | + ''' |
| 50 | + Optimize the monthly dday_intcp and dday_slope parameters by one of |
| 51 | + two methods: 'uniform' or 'random' for uniform sampling |
| 52 | +
|
| 53 | + Args: |
| 54 | + reference_srad_path (str): path to measured solar radiation data |
| 55 | + Kwargs: |
| 56 | + method (str): 'uniform' or 'random'; if 'random', |
| 57 | + intcp_delta and slope_delta are ignored, if provided |
| 58 | + dday_intcp_range ((float, float)): two-tuple of minimum and |
| 59 | + maximum value to consider for the dday_intcp parameter |
| 60 | + dday_slope_range ((float, float)): two-tuple of minimum and |
| 61 | + maximum value to consider for the dday_slope parameter |
| 62 | + intcp_delta (float): resolution of grid to test in intcp dimension |
| 63 | + slope_delta (float): resolution of grid to test in slope dimension |
| 64 | +
|
| 65 | + Returns: |
| 66 | + (SradOptimizationResult) |
| 67 | + ''' |
| 68 | + if dday_intcp_range is None: |
| 69 | + dday_intcp_range = (-60.0, 10.0) |
| 70 | + intcp_delta = 10.0 |
| 71 | + elif intcp_delta is None: |
| 72 | + intcp_delta = (dday_intcp_range[1] - dday_intcp_range[0]) / 4.0 |
| 73 | + |
| 74 | + if dday_slope_range is None: |
| 75 | + dday_slope_range = (0.2, 0.9) |
| 76 | + slope_delta = .05 |
| 77 | + elif slope_delta is None: |
| 78 | + slope_delta = (dday_slope_range[1] - dday_slope_range[0]) / 4.0 |
| 79 | + |
| 80 | + # create parameters |
| 81 | + ir = dday_intcp_range |
| 82 | + sr = dday_slope_range |
| 83 | + |
| 84 | + intcps = np.arange(ir[0], ir[1], intcp_delta) |
| 85 | + slopes = np.arange(sr[0], sr[1], slope_delta) |
| 86 | + |
| 87 | + param_grid = np.meshgrid(intcps, slopes) |
| 88 | + |
| 89 | + def _mod_params(parameters, month, intcp, slope): |
| 90 | + |
| 91 | + parameters['dday_intcp'][month] = intcp |
| 92 | + parameters['dday_slope'][month] = slope |
| 93 | + |
| 94 | + parameters_iter = ( |
| 95 | + { |
| 96 | + 'parameters': |
| 97 | + _mod_params(self.parameters, month, intcp, slope), |
| 98 | + |
| 99 | + 'title': '"month":{0},"dday_intcp":{1:.3f},' |
| 100 | + '"dday_slope":{2:.3f}'.format(month, intcp, slope), |
| 101 | + } |
| 102 | + for month in range(12) |
| 103 | + for intcp, slope in param_grid |
| 104 | + ) |
| 105 | + |
| 106 | + # create ScenarioSeries from parameters |
| 107 | + |
| 108 | + # XXX TODO XXX TODO |
| 109 | + series = ScenarioSeries.from_params_iter( |
| 110 | + self.working_dir, |
| 111 | + parameters_iter=parameters_iter, |
| 112 | + title=self.title, |
| 113 | + description=self.description |
| 114 | + ) |
| 115 | + |
| 116 | + # run all scenarios |
| 117 | + series.run() |
| 118 | + |
| 119 | + def _error(x, y): |
| 120 | + return float(abs(x - y))/float(len(x)) |
| 121 | + |
| 122 | + # calculate the top performing |
| 123 | + modeled_srads = ( |
| 124 | + (output['title'], output['statvar']['swrad_' + str(station_nhru)]) |
| 125 | + |
| 126 | + # XXX TODO XXX TODO |
| 127 | + for output in series.outputs |
| 128 | + ) |
| 129 | + |
| 130 | + measured_srad = pd.read_csv(reference_srad_path, parse_dates=True) |
| 131 | + |
| 132 | + errors = ( |
| 133 | + (modeled_srads[0], _error(measured_srad, modeled_srads[1])) |
| 134 | + for modeled_srad in modeled_srads |
| 135 | + ) |
| 136 | + rankings = list(sorted(errors, key=lambda x: x[1])) |
| 137 | + |
| 138 | + # update internal parameters |
| 139 | + self.parameters = series.outputs[rankings[0]]['parameters'] |
| 140 | + |
| 141 | + return rankings |
| 142 | + |
| 143 | + |
| 144 | +class OptimizationResult: |
| 145 | + |
| 146 | + pass |
| 147 | + |
| 148 | + |
| 149 | +class SradOptimizationResult(OptimizationResult): |
| 150 | + |
| 151 | + pass |
0 commit comments