From 4b6643cd435c12b49ab5d8b89ef0a3bff8633a0f Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 12 Jan 2023 15:28:10 -0700 Subject: [PATCH 01/12] added special Sup3rCC TRH loss --- sup3r/utilities/loss_metrics.py | 38 ++++++++++++++++++++++++++++++++- tests/test_custom_loss.py | 25 +++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/sup3r/utilities/loss_metrics.py b/sup3r/utilities/loss_metrics.py index 5a4034a25..263b139d2 100644 --- a/sup3r/utilities/loss_metrics.py +++ b/sup3r/utilities/loss_metrics.py @@ -1,6 +1,6 @@ """Loss metrics for Sup3r""" -from tensorflow.keras.losses import MeanSquaredError +from tensorflow.keras.losses import MeanSquaredError, MeanAbsoluteError import tensorflow as tf @@ -171,3 +171,39 @@ def __call__(self, x1, x2): x1_coarse = tf.reduce_mean(x1, axis=(1, 2)) x2_coarse = tf.reduce_mean(x2, axis=(1, 2)) return self.MSE_LOSS(x1_coarse, x2_coarse) + + +class TRHLoss(tf.keras.losses.Loss): + """Loss class for Temperature and Relative Humidity Sup3rCC GAN that + encourages accuracy of the min/max values in the timeseries""" + + MAE_LOSS = MeanAbsoluteError() + + def __call__(self, x1, x2): + """Custom Sup3rCC content loss function for Temp + RH + + Parameters + ---------- + x1 : tf.tensor + synthetic generator output + (n_observations, spatial_1, spatial_2, temporal, features) + x2 : tf.tensor + high resolution data + (n_observations, spatial_1, spatial_2, temporal, features) + + Returns + ------- + tf.tensor + 0D tensor with loss value + """ + x1_min = tf.reduce_min(x1, axis=3) + x2_min = tf.reduce_min(x2, axis=3) + + x1_max = tf.reduce_max(x1, axis=3) + x2_max = tf.reduce_max(x2, axis=3) + + mae = self.MAE_LOSS(x1, x2) + mae_min = self.MAE_LOSS(x1_min, x2_min) + mae_max = self.MAE_LOSS(x1_max, x2_max) + + return mae + mae_min + mae_max diff --git a/tests/test_custom_loss.py b/tests/test_custom_loss.py index e334eaee9..c4f11867f 100644 --- a/tests/test_custom_loss.py +++ b/tests/test_custom_loss.py @@ -3,7 +3,7 @@ import numpy as np import tensorflow as tf -from sup3r.utilities.loss_metrics import MmdMseLoss, CoarseMseLoss +from sup3r.utilities.loss_metrics import MmdMseLoss, CoarseMseLoss, TRHLoss def test_mmd_loss(): @@ -52,3 +52,26 @@ def test_coarse_mse_loss(): assert mse.numpy().size == 1 assert coarse_mse.numpy().size == 1 assert mse.numpy() > 10 * coarse_mse.numpy() + + +def test_trh_loss(): + """Test custom Sup3rCC Temp + RH loss function that looks at min/max values + in the timeseries.""" + trh_loss = TRHLoss() + + x = np.zeros((1, 1, 1, 72, 1)) + y = np.zeros((1, 1, 1, 72, 1)) + + # loss should be dominated by special min/max values + x[..., 24, 0] = 20 + y[..., 25, 0] = 25 + loss = trh_loss(x, y) + assert loss.numpy() > 5 + assert loss.numpy() < 6 + + # loss should be dominated by special min/max values + x[..., 24, 0] = -10 + y[..., 25, 0] = -15 + loss = trh_loss(x, y) + assert loss.numpy() > 5 + assert loss.numpy() < 6 From 8250110dcfcae68f39b942b614018c3c5a03b080 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 12 Jan 2023 15:40:16 -0700 Subject: [PATCH 02/12] fixed bad inequality --- sup3r/preprocessing/data_handling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sup3r/preprocessing/data_handling.py b/sup3r/preprocessing/data_handling.py index d73e38569..363be6de8 100644 --- a/sup3r/preprocessing/data_handling.py +++ b/sup3r/preprocessing/data_handling.py @@ -1106,7 +1106,7 @@ def preflight(self): msg = (f'sample_shape[2] ({self.sample_shape[2]}) cannot be larger ' 'than the number of time steps in the raw data ' f'({len(self.raw_time_index)}).') - if len(self.raw_time_index) >= self.sample_shape[2]: + if len(self.raw_time_index) < self.sample_shape[2]: logger.warning(msg) warnings.warn(msg) From ea31c8585aa84d4b1ac96f846bbb05afb6116024 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Fri, 13 Jan 2023 08:34:22 -0700 Subject: [PATCH 03/12] renamed overly specific loss function --- sup3r/utilities/loss_metrics.py | 8 ++++---- tests/test_custom_loss.py | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sup3r/utilities/loss_metrics.py b/sup3r/utilities/loss_metrics.py index 263b139d2..312397f8c 100644 --- a/sup3r/utilities/loss_metrics.py +++ b/sup3r/utilities/loss_metrics.py @@ -173,14 +173,14 @@ def __call__(self, x1, x2): return self.MSE_LOSS(x1_coarse, x2_coarse) -class TRHLoss(tf.keras.losses.Loss): - """Loss class for Temperature and Relative Humidity Sup3rCC GAN that - encourages accuracy of the min/max values in the timeseries""" +class TemporalExtremesLoss(tf.keras.losses.Loss): + """Loss class that encourages accuracy of the min/max values in the + timeseries""" MAE_LOSS = MeanAbsoluteError() def __call__(self, x1, x2): - """Custom Sup3rCC content loss function for Temp + RH + """Custom content loss that encourages temporal min/max accuracy Parameters ---------- diff --git a/tests/test_custom_loss.py b/tests/test_custom_loss.py index c4f11867f..124d630a9 100644 --- a/tests/test_custom_loss.py +++ b/tests/test_custom_loss.py @@ -3,7 +3,8 @@ import numpy as np import tensorflow as tf -from sup3r.utilities.loss_metrics import MmdMseLoss, CoarseMseLoss, TRHLoss +from sup3r.utilities.loss_metrics import (MmdMseLoss, CoarseMseLoss, + TemporalExtremesLoss) def test_mmd_loss(): @@ -54,10 +55,10 @@ def test_coarse_mse_loss(): assert mse.numpy() > 10 * coarse_mse.numpy() -def test_trh_loss(): - """Test custom Sup3rCC Temp + RH loss function that looks at min/max values +def test_tex_loss(): + """Test custom TemporalExtremesLoss function that looks at min/max values in the timeseries.""" - trh_loss = TRHLoss() + loss_obj = TemporalExtremesLoss() x = np.zeros((1, 1, 1, 72, 1)) y = np.zeros((1, 1, 1, 72, 1)) @@ -65,13 +66,13 @@ def test_trh_loss(): # loss should be dominated by special min/max values x[..., 24, 0] = 20 y[..., 25, 0] = 25 - loss = trh_loss(x, y) + loss = loss_obj(x, y) assert loss.numpy() > 5 assert loss.numpy() < 6 # loss should be dominated by special min/max values x[..., 24, 0] = -10 y[..., 25, 0] = -15 - loss = trh_loss(x, y) + loss = loss_obj(x, y) assert loss.numpy() > 5 assert loss.numpy() < 6 From 3bd93335598055c8cbdf6c36cf55d6d0015a0629 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Fri, 13 Jan 2023 11:40:17 -0700 Subject: [PATCH 04/12] added experimental method to fix any bias in the spatial lapse rate for surface met variables --- sup3r/models/surface.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/sup3r/models/surface.py b/sup3r/models/surface.py index 6bd5abfcf..4dd95b77f 100644 --- a/sup3r/models/surface.py +++ b/sup3r/models/surface.py @@ -227,6 +227,37 @@ def _get_temp_rh_ind(self, idf_rh): return idf_temp + def _fix_downscaled_bias(self, single_lr, single_hr, + method=Image.Resampling.BILINEAR): + """Fix any bias introduced by the spatial downscaling with lapse rate. + + Parameters + ---------- + single_lr : np.ndarray + Single timestep raster data with shape + (lat, lon) matching the low-resolution input data. + single_hr : np.ndarray + Single timestep downscaled raster data with shape + (lat, lon) matching the high-resolution input data. + method : Image.Resampling.BILINEAR + An Image.Resampling method (NEAREST, BILINEAR, BICUBIC, LANCZOS). + NEAREST enforces zero bias but makes slightly more spatial seams. + + Returns + ------- + single_hr : np.ndarray + Single timestep downscaled raster data with shape + (lat, lon) matching the high-resolution input data. + """ + + re_coarse = spatial_coarsening(np.expand_dims(single_hr, axis=-1), + s_enhance=self._s_enhance, + obs_axis=False)[..., 0] + bias = re_coarse - single_lr + bc = self.downscale_arr(bias, s_enhance=self._s_enhance, method=method) + single_hr -= bc + return single_hr + @staticmethod def downscale_arr(arr, s_enhance, method=Image.Resampling.BILINEAR): """Downscale a 2D array of data Image.resize() method @@ -286,6 +317,7 @@ def downscale_temp(self, single_lr_temp, topo_lr, topo_hr): lower_data = single_lr_temp.copy() + topo_lr * self._temp_lapse hi_res_temp = self.downscale_arr(lower_data, self._s_enhance) hi_res_temp -= topo_hr * self._temp_lapse + hi_res_temp = self._fix_downscaled_bias(single_lr_temp, hi_res_temp) return hi_res_temp @@ -347,6 +379,8 @@ def downscale_rh(self, single_lr_rh, single_lr_temp, single_hr_temp, + self._w_delta_temp * delta_temp + self._w_delta_topo * delta_topo) + hi_res_rh = self._fix_downscaled_bias(single_lr_rh, hi_res_rh) + return hi_res_rh def downscale_pres(self, single_lr_pres, topo_lr, topo_hr): @@ -403,6 +437,8 @@ def downscale_pres(self, single_lr_pres, topo_lr, topo_hr): const = 101325 * (1 - (1 - topo_hr / self._pres_div)**self._pres_exp) hi_res_pres -= const + hi_res_pres = self._fix_downscaled_bias(single_lr_pres, hi_res_pres) + if np.min(hi_res_pres) < 0.0: msg = ('Spatial interpolation of surface pressure ' 'resulted in negative values. Incorrectly ' From ec45cc787e4a3aea3516608ae0cfa33c4a25fd93 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Mon, 16 Jan 2023 13:55:42 -0700 Subject: [PATCH 05/12] add flag for surface interpolation bias correction --- sup3r/models/surface.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/sup3r/models/surface.py b/sup3r/models/surface.py index 4dd95b77f..d75145d94 100644 --- a/sup3r/models/surface.py +++ b/sup3r/models/surface.py @@ -43,7 +43,7 @@ class SurfaceSpatialMetModel(AbstractInterface): def __init__(self, features, s_enhance, noise_adders=None, temp_lapse=None, w_delta_temp=None, w_delta_topo=None, - pres_div=None, pres_exp=None): + pres_div=None, pres_exp=None, fix_bias=True): """ Parameters ---------- @@ -85,6 +85,10 @@ def __init__(self, features, s_enhance, noise_adders=None, pres_div : None | float Exponential factor in the pressure scale height equation. Defaults to the cls.PRES_EXP attribute. + fix_bias : bool + Some local bias can be introduced by the bilinear interp + lapse + rate, this flag will attempt to correct that bias by using the + low-resolution deviation from the input data """ self._features = features @@ -95,6 +99,7 @@ def __init__(self, features, s_enhance, noise_adders=None, self._w_delta_topo = w_delta_topo or self.W_DELTA_TOPO self._pres_div = pres_div or self.PRES_DIV self._pres_exp = pres_exp or self.PRES_EXP + self._fix_bias = fix_bias if isinstance(self._noise_adders, (int, float)): self._noise_adders = [self._noise_adders] * len(self._features) @@ -317,7 +322,10 @@ def downscale_temp(self, single_lr_temp, topo_lr, topo_hr): lower_data = single_lr_temp.copy() + topo_lr * self._temp_lapse hi_res_temp = self.downscale_arr(lower_data, self._s_enhance) hi_res_temp -= topo_hr * self._temp_lapse - hi_res_temp = self._fix_downscaled_bias(single_lr_temp, hi_res_temp) + + if self._fix_bias: + hi_res_temp = self._fix_downscaled_bias(single_lr_temp, + hi_res_temp) return hi_res_temp @@ -379,7 +387,8 @@ def downscale_rh(self, single_lr_rh, single_lr_temp, single_hr_temp, + self._w_delta_temp * delta_temp + self._w_delta_topo * delta_topo) - hi_res_rh = self._fix_downscaled_bias(single_lr_rh, hi_res_rh) + if self._fix_bias: + hi_res_rh = self._fix_downscaled_bias(single_lr_rh, hi_res_rh) return hi_res_rh @@ -437,7 +446,9 @@ def downscale_pres(self, single_lr_pres, topo_lr, topo_hr): const = 101325 * (1 - (1 - topo_hr / self._pres_div)**self._pres_exp) hi_res_pres -= const - hi_res_pres = self._fix_downscaled_bias(single_lr_pres, hi_res_pres) + if self._fix_bias: + hi_res_pres = self._fix_downscaled_bias(single_lr_pres, + hi_res_pres) if np.min(hi_res_pres) < 0.0: msg = ('Spatial interpolation of surface pressure ' From 20a81621a36fd9835bc4d4445b8174d46845ea08 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 09:16:47 -0700 Subject: [PATCH 06/12] updated surface load method to match other load from disk methods, cleaned up surface model meta data --- sup3r/models/linear.py | 13 +++++---- sup3r/models/surface.py | 63 ++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/sup3r/models/linear.py b/sup3r/models/linear.py index 4a5557bd0..f3242085c 100644 --- a/sup3r/models/linear.py +++ b/sup3r/models/linear.py @@ -2,6 +2,7 @@ """Simple models for super resolution such as linear interp models.""" import numpy as np import logging +from inspect import signature import os import json from sup3r.utilities.utilities import st_interp @@ -45,7 +46,8 @@ def load(cls, model_dir, verbose=False): Parameters ---------- model_dir : str - Directory to load LinearInterp model files from. + Directory to load LinearInterp model files from. Must + have a model_params.json file containing all of the init args. verbose : bool Flag to log information about the loaded model. @@ -59,11 +61,10 @@ def load(cls, model_dir, verbose=False): with open(fp_params, 'r') as f: params = json.load(f) - meta = params.get('meta', {'class': 'Sup3rGan'}) - model = cls(features=meta['training_features'], - s_enhance=meta['s_enhance'], - t_enhance=meta['t_enhance'], - t_centered=meta['t_centered']) + meta = params.get('meta', {'class': 'LinearInterp'}) + args = signature(cls.__init__).parameters + kwargs = {k: v for k, v in meta.items() if k in args} + model = cls(**kwargs) if verbose: logger.info('Loading LinearInterp with meta data: {}' diff --git a/sup3r/models/surface.py b/sup3r/models/surface.py index d75145d94..900a22381 100644 --- a/sup3r/models/surface.py +++ b/sup3r/models/surface.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- """Special models for surface meteorological data.""" +import os +import json import logging +from inspect import signature from fnmatch import fnmatch import numpy as np from PIL import Image @@ -43,7 +46,8 @@ class SurfaceSpatialMetModel(AbstractInterface): def __init__(self, features, s_enhance, noise_adders=None, temp_lapse=None, w_delta_temp=None, w_delta_topo=None, - pres_div=None, pres_exp=None, fix_bias=True): + pres_div=None, pres_exp=None, interp_method='BILINEAR', + fix_bias=True): """ Parameters ---------- @@ -85,6 +89,9 @@ def __init__(self, features, s_enhance, noise_adders=None, pres_div : None | float Exponential factor in the pressure scale height equation. Defaults to the cls.PRES_EXP attribute. + interp_method : str + Name of the interpolation method to use from PIL.Image.Resampling + (NEAREST, BILINEAR, BICUBIC, LANCZOS) fix_bias : bool Some local bias can be introduced by the bilinear interp + lapse rate, this flag will attempt to correct that bias by using the @@ -100,6 +107,7 @@ def __init__(self, features, s_enhance, noise_adders=None, self._pres_div = pres_div or self.PRES_DIV self._pres_exp = pres_exp or self.PRES_EXP self._fix_bias = fix_bias + self._interp_method = getattr(Image.Resampling, interp_method) if isinstance(self._noise_adders, (int, float)): self._noise_adders = [self._noise_adders] * len(self._features) @@ -109,26 +117,17 @@ def __len__(self): return 1 @classmethod - def load(cls, features, s_enhance, verbose=False, **kwargs): + def load(cls, model_dir, verbose=False): """Load the GAN with its sub-networks from a previously saved-to output directory. Parameters ---------- - 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: - temperature_*m, relativehumidity_*m, and pressure_*m. The list can - contain multiple instances of each variable at different heights. - relativehumidity_*m entries must have corresponding temperature_*m - entires at the same hub height. - s_enhance : int - Integer factor by which the spatial axes are to be enhanced. + model_dir : str + Directory to load SurfaceSpatialMetModel model files from. Must + have a model_params.json file containing all of the init args. verbose : bool Flag to log information about the loaded model. - kwargs : None | dict - Optional kwargs to initialize SurfaceSpatialMetModel Returns ------- @@ -136,7 +135,15 @@ def load(cls, features, s_enhance, verbose=False, **kwargs): Returns an initialized SurfaceSpatialMetModel """ - model = cls(features, s_enhance, **kwargs) + fp_params = os.path.join(model_dir, 'model_params.json') + assert os.path.exists(fp_params), f'Could not find: {fp_params}' + with open(fp_params, 'r') as f: + params = json.load(f) + + meta = params.get('meta', {'class': 'SurfaceSpatialMetModel'}) + args = signature(cls.__init__).parameters + kwargs = {k: v for k, v in meta.items() if k in args} + model = cls(**kwargs) if verbose: logger.info('Loading SurfaceSpatialMetModel with meta data: {}' @@ -320,12 +327,14 @@ def downscale_temp(self, single_lr_temp, topo_lr, topo_hr): assert len(topo_hr.shape) == 2, 'Bad shape for topo_hr' lower_data = single_lr_temp.copy() + topo_lr * self._temp_lapse - hi_res_temp = self.downscale_arr(lower_data, self._s_enhance) + hi_res_temp = self.downscale_arr(lower_data, self._s_enhance, + method=self._interp_method) hi_res_temp -= topo_hr * self._temp_lapse if self._fix_bias: hi_res_temp = self._fix_downscaled_bias(single_lr_temp, - hi_res_temp) + hi_res_temp, + method=self._interp_method) return hi_res_temp @@ -376,9 +385,12 @@ def downscale_rh(self, single_lr_rh, single_lr_temp, single_hr_temp, assert len(topo_lr.shape) == 2, 'Bad shape for topo_lr' assert len(topo_hr.shape) == 2, 'Bad shape for topo_hr' - interp_rh = self.downscale_arr(single_lr_rh, self._s_enhance) - interp_temp = self.downscale_arr(single_lr_temp, self._s_enhance) - interp_topo = self.downscale_arr(topo_lr, self._s_enhance) + interp_rh = self.downscale_arr(single_lr_rh, self._s_enhance, + method=self._interp_method) + interp_temp = self.downscale_arr(single_lr_temp, self._s_enhance, + method=self._interp_method) + interp_topo = self.downscale_arr(topo_lr, self._s_enhance, + method=self._interp_method) delta_temp = single_hr_temp - interp_temp delta_topo = topo_hr - interp_topo @@ -388,7 +400,8 @@ def downscale_rh(self, single_lr_rh, single_lr_temp, single_hr_temp, + self._w_delta_topo * delta_topo) if self._fix_bias: - hi_res_rh = self._fix_downscaled_bias(single_lr_rh, hi_res_rh) + hi_res_rh = self._fix_downscaled_bias(single_lr_rh, hi_res_rh, + method=self._interp_method) return hi_res_rh @@ -441,14 +454,16 @@ def downscale_pres(self, single_lr_pres, topo_lr, topo_hr): logger.error(msg) raise ValueError(msg) - hi_res_pres = self.downscale_arr(single_lr_pres, self._s_enhance) + hi_res_pres = self.downscale_arr(single_lr_pres, self._s_enhance, + method=self._interp_method) const = 101325 * (1 - (1 - topo_hr / self._pres_div)**self._pres_exp) hi_res_pres -= const if self._fix_bias: hi_res_pres = self._fix_downscaled_bias(single_lr_pres, - hi_res_pres) + hi_res_pres, + method=self._interp_method) if np.min(hi_res_pres) < 0.0: msg = ('Spatial interpolation of surface pressure ' @@ -571,6 +586,8 @@ def meta(self): 'pressure_exponent': self._pres_exp, 'training_features': self.training_features, 'output_features': self.output_features, + 'interp_method': str(self._interp_method), + 'fix_bias': self._fix_bias, 'class': self.__class__.__name__, } From 3c94c62c635e9d11ce8d01a5e5bb022564c165ad Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 09:17:23 -0700 Subject: [PATCH 07/12] incremented version for new loss for trh extreme values --- sup3r/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sup3r/version.py b/sup3r/version.py index eb9b139c7..ae5c54150 100644 --- a/sup3r/version.py +++ b/sup3r/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- """SUP3R Version""" -__version__ = '0.0.8' +__version__ = '0.0.9' From cbaa67a1b06f8085316e47ba54f32ad863ebb3b7 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 09:35:07 -0700 Subject: [PATCH 08/12] fixed tests --- sup3r/models/linear.py | 5 +++-- sup3r/models/surface.py | 14 ++++++++------ tests/test_surface_model.py | 29 ++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/sup3r/models/linear.py b/sup3r/models/linear.py index f3242085c..2e4d41882 100644 --- a/sup3r/models/linear.py +++ b/sup3r/models/linear.py @@ -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. @@ -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) diff --git a/sup3r/models/surface.py b/sup3r/models/surface.py index 900a22381..8c34c069b 100644 --- a/sup3r/models/surface.py +++ b/sup3r/models/surface.py @@ -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. @@ -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) @@ -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) diff --git a/tests/test_surface_model.py b/tests/test_surface_model.py index 18ded9e9c..222a1ca9e 100644 --- a/tests/test_surface_model.py +++ b/tests/test_surface_model.py @@ -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 @@ -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 @@ -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) From ed9603838e8888c50754f7b7689a949115318dc7 Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 10:59:48 -0700 Subject: [PATCH 09/12] updated default surface spatial model to use lanczos downscaling. more random error but less systematic error --- sup3r/models/surface.py | 14 ++++++++------ tests/test_surface_model.py | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/sup3r/models/surface.py b/sup3r/models/surface.py index 8c34c069b..39a32ac3a 100644 --- a/sup3r/models/surface.py +++ b/sup3r/models/surface.py @@ -46,7 +46,7 @@ class SurfaceSpatialMetModel(AbstractInterface): def __init__(self, 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='BILINEAR', + pres_div=None, pres_exp=None, interp_method='LANCZOS', fix_bias=True): """ Parameters @@ -92,6 +92,8 @@ def __init__(self, features, s_enhance, noise_adders=None, interp_method : str Name of the interpolation method to use from PIL.Image.Resampling (NEAREST, BILINEAR, BICUBIC, LANCZOS) + LANCZOS is default and has been tested to work best for + SurfaceSpatialMetModel. fix_bias : bool Some local bias can be introduced by the bilinear interp + lapse rate, this flag will attempt to correct that bias by using the @@ -241,7 +243,7 @@ def _get_temp_rh_ind(self, idf_rh): return idf_temp def _fix_downscaled_bias(self, single_lr, single_hr, - method=Image.Resampling.BILINEAR): + method=Image.Resampling.LANCZOS): """Fix any bias introduced by the spatial downscaling with lapse rate. Parameters @@ -252,7 +254,7 @@ def _fix_downscaled_bias(self, single_lr, single_hr, single_hr : np.ndarray Single timestep downscaled raster data with shape (lat, lon) matching the high-resolution input data. - method : Image.Resampling.BILINEAR + method : Image.Resampling.LANCZOS An Image.Resampling method (NEAREST, BILINEAR, BICUBIC, LANCZOS). NEAREST enforces zero bias but makes slightly more spatial seams. @@ -272,7 +274,7 @@ def _fix_downscaled_bias(self, single_lr, single_hr, return single_hr @staticmethod - def downscale_arr(arr, s_enhance, method=Image.Resampling.BILINEAR): + def downscale_arr(arr, s_enhance, method=Image.Resampling.LANCZOS): """Downscale a 2D array of data Image.resize() method Parameters @@ -282,9 +284,9 @@ def downscale_arr(arr, s_enhance, method=Image.Resampling.BILINEAR): (lat, lon) s_enhance : int Integer factor by which the spatial axes are to be enhanced. - method : Image.Resampling.BILINEAR + method : Image.Resampling.LANCZOS An Image.Resampling method (NEAREST, BILINEAR, BICUBIC, LANCZOS). - BILINEAR is default and has been tested to work best for + LANCZOS is default and has been tested to work best for SurfaceSpatialMetModel. """ im = Image.fromarray(arr) diff --git a/tests/test_surface_model.py b/tests/test_surface_model.py index 222a1ca9e..0373394e2 100644 --- a/tests/test_surface_model.py +++ b/tests/test_surface_model.py @@ -67,11 +67,11 @@ def test_surface_model(s_enhance=5): diff = true_hi_res - hi_res # high res temperature should have very low bias and MAE < 1C - assert np.abs(diff[..., 0].mean()) < 1e-6 + assert np.abs(diff[..., 0].mean()) < 1e-4 assert np.abs(diff[..., 0]).mean() < 5 # high res relative humidity should have very low bias and MAE < 3% - assert np.abs(diff[..., 1].mean()) < 1e-6 + assert np.abs(diff[..., 1].mean()) < 1e-4 assert np.abs(diff[..., 1]).mean() < 2 # high res pressure should have very low bias and MAE < 200 Pa From 6bea8454002764f02017f5b87bf7e4792d6fa44d Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 13:08:42 -0700 Subject: [PATCH 10/12] fixed linear model meta, relaxed multi step model test --- sup3r/models/linear.py | 3 ++- tests/test_multi_step.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sup3r/models/linear.py b/sup3r/models/linear.py index 2e4d41882..e049a3efd 100644 --- a/sup3r/models/linear.py +++ b/sup3r/models/linear.py @@ -76,7 +76,8 @@ class init args. @property def meta(self): """Get meta data dictionary that defines the model params""" - return {'s_enhance': self._s_enhance, + return {'features': self._features, + 's_enhance': self._s_enhance, 't_enhance': self._t_enhance, 't_centered': self._t_centered, 'training_features': self.training_features, diff --git a/tests/test_multi_step.py b/tests/test_multi_step.py index 81caa03f6..c0d9a6258 100644 --- a/tests/test_multi_step.py +++ b/tests/test_multi_step.py @@ -95,7 +95,8 @@ def test_multi_step_norm(norm_option): out1 = model1.generate(x) out2 = model2.generate(out1) out3 = model3.generate(out2) - assert np.allclose(out, out3, atol=5e-4) + rdiff = np.abs(out - out3) / np.abs(out.mean()) + assert rdiff.max() < 1e-4 def test_spatial_then_temporal_gan(): From 5dcaa7db8590a639289bca20a91ae7e4811a976a Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 13:55:18 -0700 Subject: [PATCH 11/12] added linear->surface model inheritance bc surface is basically fancy linear interp --- sup3r/models/surface.py | 56 ++--------------------------------------- 1 file changed, 2 insertions(+), 54 deletions(-) diff --git a/sup3r/models/surface.py b/sup3r/models/surface.py index 39a32ac3a..8d8e4a508 100644 --- a/sup3r/models/surface.py +++ b/sup3r/models/surface.py @@ -10,13 +10,13 @@ from sklearn import linear_model from warnings import warn -from sup3r.models.abstract import AbstractInterface +from sup3r.models.linear import LinearInterp from sup3r.utilities.utilities import spatial_coarsening logger = logging.getLogger(__name__) -class SurfaceSpatialMetModel(AbstractInterface): +class SurfaceSpatialMetModel(LinearInterp): """Model to spatially downscale daily-average near-surface temperature, relative humidity, and pressure @@ -118,42 +118,6 @@ def __len__(self): """Get number of model steps (match interface of MultiStepGan)""" return 1 - @classmethod - def load(cls, model_dir, verbose=False): - """Load the GAN with its sub-networks from a previously saved-to output - directory. - - Parameters - ---------- - model_dir : str - Directory to load SurfaceSpatialMetModel model files from. Must - 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. - - Returns - ------- - out : SurfaceSpatialMetModel - Returns an initialized SurfaceSpatialMetModel - """ - - fp_params = os.path.join(model_dir, 'model_params.json') - assert os.path.exists(fp_params), f'Could not find: {fp_params}' - with open(fp_params, 'r') as f: - params = json.load(f) - - meta = params['meta'] - args = signature(cls.__init__).parameters - kwargs = {k: v for k, v in meta.items() if k in args} - model = cls(**kwargs) - - if verbose: - logger.info('Loading SurfaceSpatialMetModel with meta data: {}' - .format(model.meta)) - - return model - @staticmethod def _get_s_enhance(topo_lr, topo_hr): """Get the spatial enhancement factor given low-res and high-res @@ -595,22 +559,6 @@ def meta(self): 'class': self.__class__.__name__, } - @property - def training_features(self): - """Get the list of input feature names that the generative model was - trained on. - - Note that topography needs to be passed into generate() as an exogenous - data input. - """ - return self._features - - @property - def output_features(self): - """Get the list of output feature names that the generative model - outputs""" - return self._features - def train(self, true_hr_temp, true_hr_rh, true_hr_topo): """This method trains the relative humidity linear model. The temperature and surface lapse rate models are parameterizations taken From 2ff434eb241e6db299e06a35b2d2db09190e81cc Mon Sep 17 00:00:00 2001 From: grantbuster Date: Thu, 19 Jan 2023 13:55:37 -0700 Subject: [PATCH 12/12] made the multi step norm test more accomodating --- tests/test_multi_step.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_multi_step.py b/tests/test_multi_step.py index c0d9a6258..6c9901e71 100644 --- a/tests/test_multi_step.py +++ b/tests/test_multi_step.py @@ -95,8 +95,8 @@ def test_multi_step_norm(norm_option): out1 = model1.generate(x) out2 = model2.generate(out1) out3 = model3.generate(out2) - rdiff = np.abs(out - out3) / np.abs(out.mean()) - assert rdiff.max() < 1e-4 + err = np.abs(out - out3).mean() / np.abs(out.mean()) + assert err < 1e4 def test_spatial_then_temporal_gan():