Skip to content

Commit

Permalink
linear and spatial model features->lr_features
Browse files Browse the repository at this point in the history
  • Loading branch information
grantbuster committed Nov 21, 2023
1 parent b69302d commit ac662da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
13 changes: 6 additions & 7 deletions sup3r/models/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class LinearInterp(AbstractInterface):
"""Simple model to do linear interpolation on the spatial and temporal axes
"""

def __init__(self, features, s_enhance, t_enhance, t_centered=False):
def __init__(self, lr_features, s_enhance, t_enhance, t_centered=False):
"""
Parameters
----------
features : list
lr_features : list
List of feature names that this model will operate on for both
input and output. This must match the feature axis ordering in the
array input to generate().
Expand All @@ -35,7 +35,7 @@ def __init__(self, features, s_enhance, t_enhance, t_centered=False):
time-centered (e.g. interp 01:00 02:00 to 00:45 01:15 01:45 02:15)
"""

self._features = features
self._lr_features = lr_features
self._s_enhance = s_enhance
self._t_enhance = t_enhance
self._t_centered = t_centered
Expand Down Expand Up @@ -78,11 +78,10 @@ class init args.
@property
def meta(self):
"""Get meta data dictionary that defines the model params"""
return {'features': self._features,
return {'lr_features': self._lr_features,
's_enhance': self._s_enhance,
't_enhance': self._t_enhance,
't_centered': self._t_centered,
'lr_features': self.lr_features,
'hr_out_features': self.hr_out_features,
'class': self.__class__.__name__,
}
Expand All @@ -92,13 +91,13 @@ def lr_features(self):
"""Get the list of input feature names that the generative model was
trained on.
"""
return self._features
return self._lr_features

@property
def hr_out_features(self):
"""Get the list of output feature names that the generative model
outputs"""
return self._features
return self._lr_features

@property
def hr_exo_features(self):
Expand Down
36 changes: 18 additions & 18 deletions sup3r/models/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class SurfaceSpatialMetModel(LinearInterp):
"""Weight for the delta-topography feature for the relative humidity linear
regression model."""

def __init__(self, features, s_enhance, noise_adders=None,
def __init__(self, lr_features, s_enhance, noise_adders=None,
temp_lapse=None, w_delta_temp=None, w_delta_topo=None,
pres_div=None, pres_exp=None, interp_method='LANCZOS',
fix_bias=True):
"""
Parameters
----------
features : list
lr_features : list
List of feature names that this model will operate on for both
input and output. This must match the feature axis ordering in the
array input to generate(). Typically this is a list containing:
Expand All @@ -63,11 +63,11 @@ def __init__(self, features, s_enhance, noise_adders=None,
Option to add gaussian noise to spatial model output. Noise will be
normally distributed with mean of 0 and standard deviation =
noise_adders. noise_adders can be a single value or a list
corresponding to the features list. None is no noise. The addition
of noise has been shown to help downstream temporal-only models
produce diurnal cycles in regions where there is minimal change in
topography. A noise_adders around 0.07C (temperature) and 0.1%
(relative humidity) have been shown to be effective. This is
corresponding to the lr_features list. None is no noise. The
addition of noise has been shown to help downstream temporal-only
models produce diurnal cycles in regions where there is minimal
change in topography. A noise_adders around 0.07C (temperature) and
0.1% (relative humidity) have been shown to be effective. This is
unnecessary if daily min/max temperatures are provided as low res
training features.
temp_lapse : None | float
Expand Down Expand Up @@ -98,7 +98,7 @@ def __init__(self, features, s_enhance, noise_adders=None,
low-resolution deviation from the input data
"""

self._features = features
self._lr_features = lr_features
self._s_enhance = s_enhance
self._noise_adders = noise_adders
self._temp_lapse = temp_lapse or self.TEMP_LAPSE
Expand All @@ -111,7 +111,7 @@ def __init__(self, features, s_enhance, noise_adders=None,
self._interp_method = getattr(Image.Resampling, interp_method)

if isinstance(self._noise_adders, (int, float)):
self._noise_adders = [self._noise_adders] * len(self._features)
self._noise_adders = [self._noise_adders] * len(self._lr_features)

def __len__(self):
"""Get number of model steps (match interface of MultiStepGan)"""
Expand Down Expand Up @@ -162,21 +162,21 @@ def input_dims(self):
@property
def feature_inds_temp(self):
"""Get the feature index values for the temperature features."""
inds = [i for i, name in enumerate(self._features)
inds = [i for i, name in enumerate(self._lr_features)
if fnmatch(name, 'temperature_*')]
return inds

@property
def feature_inds_pres(self):
"""Get the feature index values for the pressure features."""
inds = [i for i, name in enumerate(self._features)
inds = [i for i, name in enumerate(self._lr_features)
if fnmatch(name, 'pressure_*')]
return inds

@property
def feature_inds_rh(self):
"""Get the feature index values for the relative humidity features."""
inds = [i for i, name in enumerate(self._features)
inds = [i for i, name in enumerate(self._lr_features)
if fnmatch(name, 'relativehumidity_*')]
return inds

Expand All @@ -195,22 +195,23 @@ def _get_temp_rh_ind(self, idf_rh):
Index in the feature list for a temperature_*m feature with the
same hub height as the idf_rh input.
"""
name_rh = self._features[idf_rh]
name_rh = self._lr_features[idf_rh]
hh_suffix = name_rh.split('_')[-1]
idf_temp = None
for i in self.feature_inds_temp:
same_hh = self._features[i].endswith(hh_suffix)
same_hh = self._lr_features[i].endswith(hh_suffix)
not_minmax = not any(mm in name_rh for mm in ('_min_', '_max_'))
both_mins = '_min_' in name_rh and '_min_' in self._features[i]
both_maxs = '_max_' in name_rh and '_max_' in self._features[i]
both_mins = '_min_' in name_rh and '_min_' in self._lr_features[i]
both_maxs = '_max_' in name_rh and '_max_' in self._lr_features[i]

if same_hh and (not_minmax or both_mins or both_maxs):
idf_temp = i
break

if idf_temp is None:
msg = ('Could not find temperature feature corresponding to '
'"{}" in feature list: {}'.format(name_rh, self._features))
'"{}" in feature list: {}'
.format(name_rh, self._lr_features))
logger.error(msg)
raise KeyError(msg)

Expand Down Expand Up @@ -567,7 +568,6 @@ def meta(self):
'weight_for_delta_topo': self._w_delta_topo,
'pressure_divisor': self._pres_div,
'pressure_exponent': self._pres_exp,
'features': self._features,
'lr_features': self.lr_features,
'hr_out_features': self.hr_out_features,
'interp_method': str(self._interp_method),
Expand Down
2 changes: 1 addition & 1 deletion tests/forward_pass/test_forward_pass_exo.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def test_fwp_wind_hi_res_topo_plus_linear():
_ = s_model.generate(np.ones((4, 10, 10, 3)),
exogenous_data=exo_tmp)

t_model = LinearInterp(features=['U_100m', 'V_100m'],
t_model = LinearInterp(lr_features=['U_100m', 'V_100m'],
s_enhance=1,
t_enhance=4)
t_model.meta['input_resolution'] = {'spatial': '4km',
Expand Down
2 changes: 1 addition & 1 deletion tests/forward_pass/test_multi_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_spatial_gan_then_linear_interp():
model1 = Sup3rGan(fp_gen, fp_disc)
_ = model1.generate(np.ones((4, 10, 10, len(FEATURES))))

model2 = LinearInterp(features=FEATURES, s_enhance=3, t_enhance=4)
model2 = LinearInterp(lr_features=FEATURES, s_enhance=3, t_enhance=4)

model1.set_norm_stats({'U_100m': 0.1, 'V_100m': 0.2},
{'U_100m': 0.04, 'V_100m': 0.02})
Expand Down
7 changes: 3 additions & 4 deletions tests/forward_pass/test_surface_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def test_surface_model(s_enhance=5):

low_res, true_hi_res, topo_lr, topo_hr = get_inputs(s_enhance)

kwargs = {'meta': {'features': FEATURES, 'lr_features': FEATURES,
'hr_out_features': FEATURES, 's_enhance': s_enhance}}
kwargs = {'meta': {'lr_features': FEATURES, 'hr_out_features': FEATURES,
's_enhance': s_enhance}}
with tempfile.TemporaryDirectory() as td:
fp_params = os.path.join(td, 'model_params.json')
with open(fp_params, 'w') as f:
Expand Down Expand Up @@ -136,8 +136,7 @@ def test_multi_step_surface(s_enhance=2, t_enhance=2):
temporal_dir = os.path.join(td, 'model')
model.save(temporal_dir)

surface_model_kwargs = {'meta': {'features': FEATURES,
'lr_features': FEATURES,
surface_model_kwargs = {'meta': {'lr_features': FEATURES,
'hr_out_features': FEATURES,
's_enhance': s_enhance}}

Expand Down

0 comments on commit ac662da

Please sign in to comment.