Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gb/trh loss #142

Merged
merged 12 commits into from
Jan 20, 2023
Merged
Prev Previous commit
Next Next commit
fixed tests
  • Loading branch information
grantbuster committed Jan 19, 2023
commit cbaa67a1b06f8085316e47ba54f32ad863ebb3b7
5 changes: 3 additions & 2 deletions sup3r/models/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def load(cls, model_dir, verbose=False):
----------
model_dir : str
Directory to load LinearInterp model files from. Must
have a model_params.json file containing all of the init args.
have a model_params.json file containing "meta" key with all of the
class init args.
verbose : bool
Flag to log information about the loaded model.

Expand All @@ -61,7 +62,7 @@ def load(cls, model_dir, verbose=False):
with open(fp_params, 'r') as f:
params = json.load(f)

meta = params.get('meta', {'class': 'LinearInterp'})
meta = params['meta']
args = signature(cls.__init__).parameters
kwargs = {k: v for k, v in meta.items() if k in args}
model = cls(**kwargs)
Expand Down
14 changes: 8 additions & 6 deletions sup3r/models/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def load(cls, model_dir, verbose=False):
----------
model_dir : str
Directory to load SurfaceSpatialMetModel model files from. Must
have a model_params.json file containing all of the init args.
have a model_params.json file containing "meta" key with all of the
class init args.
verbose : bool
Flag to log information about the loaded model.

Expand All @@ -140,7 +141,7 @@ def load(cls, model_dir, verbose=False):
with open(fp_params, 'r') as f:
params = json.load(f)

meta = params.get('meta', {'class': 'SurfaceSpatialMetModel'})
meta = params['meta']
args = signature(cls.__init__).parameters
kwargs = {k: v for k, v in meta.items() if k in args}
model = cls(**kwargs)
Expand Down Expand Up @@ -444,17 +445,18 @@ def downscale_pres(self, single_lr_pres, topo_lr, topo_hr):
warn(msg)

const = 101325 * (1 - (1 - topo_lr / self._pres_div)**self._pres_exp)
single_lr_pres = single_lr_pres.copy() + const
lr_pres_adj = single_lr_pres.copy() + const

if np.min(single_lr_pres) < 0.0:
if np.min(lr_pres_adj) < 0.0:
msg = ('Spatial interpolation of surface pressure '
'resulted in negative values. Incorrectly '
'scaled/unscaled values or incorrect units are '
'the most likely causes.')
'the most likely causes. All pressure data should be '
'in Pascals.')
logger.error(msg)
raise ValueError(msg)

hi_res_pres = self.downscale_arr(single_lr_pres, self._s_enhance,
hi_res_pres = self.downscale_arr(lr_pres_adj, self._s_enhance,
method=self._interp_method)

const = 101325 * (1 - (1 - topo_hr / self._pres_div)**self._pres_exp)
Expand Down
29 changes: 22 additions & 7 deletions tests/test_surface_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
"""Test the temperature and relative humidity scaling relationships of the
SurfaceSpatialMetModel"""
import json
import os
import tempfile
import pytest
Expand Down Expand Up @@ -53,7 +54,14 @@ def test_surface_model(s_enhance=5):

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

model = SurfaceSpatialMetModel.load(features=FEATURES, s_enhance=s_enhance)
kwargs = {'meta': {'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:
json.dump(kwargs, f)

model = SurfaceSpatialMetModel.load(model_dir=td)

hi_res = model.generate(low_res, exogenous_data=[topo_lr, topo_hr])

diff = true_hi_res - hi_res
Expand Down Expand Up @@ -116,14 +124,21 @@ def test_multi_step_surface(s_enhance=2, t_enhance=2):
t_enhance=t_enhance)

with tempfile.TemporaryDirectory() as td:
fp = os.path.join(td, 'model')
model.save(fp)
temporal_dir = os.path.join(td, 'model')
model.save(temporal_dir)

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

surface_dir = os.path.join(td, 'surface/')
os.makedirs(surface_dir)
fp_params = os.path.join(surface_dir, 'model_params.json')
with open(fp_params, 'w') as f:
json.dump(surface_model_kwargs, f)

surface_model_kwargs = {'features': FEATURES, 's_enhance': s_enhance}
temporal_model_kwargs = {'model_dirs': fp}
ms_model = MultiStepSurfaceMetGan.load(
surface_model_kwargs=surface_model_kwargs,
temporal_model_kwargs=temporal_model_kwargs)
surface_model_kwargs={'model_dir': surface_dir},
temporal_model_kwargs={'model_dirs': temporal_dir})

for model in ms_model.models:
assert isinstance(model.s_enhance, int)
Expand Down