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/bc #96

Merged
merged 29 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
efcf50e
added bias module with a place for bias transformation functions and …
grantbuster Sep 20, 2022
115e9c2
feature specific bias correction
grantbuster Sep 20, 2022
c184917
removed warnings about excessive padding - not a bad thing
grantbuster Sep 20, 2022
1d823e3
added a site-by-site linear bias correction calculation method
grantbuster Sep 21, 2022
8b7f25b
bug fix and logging
grantbuster Sep 21, 2022
56148db
bias calc mods and new functions
grantbuster Sep 22, 2022
50c3692
added bias calc cli
grantbuster Sep 22, 2022
2f7d4f7
added bias calc to main cli
grantbuster Sep 22, 2022
c132b81
make bias out dir
grantbuster Sep 22, 2022
915edd5
bug fixes and minor refactor to run on eagle
grantbuster Sep 22, 2022
7b28943
added local linear bias correct to forward pass bc options
grantbuster Sep 22, 2022
0cee671
added option to smooth spatial bias correction factors outside of the…
grantbuster Sep 23, 2022
aa0a040
better enumerated progress logging for fwp
grantbuster Sep 23, 2022
240a0d6
added bias correction option to QA
grantbuster Sep 23, 2022
9ac0905
minor refactor to bias correct u and v instead of windspeed and direc…
grantbuster Sep 23, 2022
d2fb1e2
fixed up the u/v QA with bias correction
grantbuster Sep 27, 2022
01fbeda
added meta data to bc h5 output attrs
grantbuster Sep 27, 2022
b370b9f
more bc convenience functions
grantbuster Sep 28, 2022
24c40b6
added monthly bias correction
grantbuster Sep 28, 2022
0deab95
added montly bias correction data transformation method and integrate…
grantbuster Sep 29, 2022
1f28ccc
fixed collection logic for undefined mask meta variable when file is …
grantbuster Oct 2, 2022
5d93283
added bias correction calc tests
grantbuster Oct 3, 2022
0638d7b
added bias transform calcs
grantbuster Oct 3, 2022
6cc6ced
added fwp+bc integration test
grantbuster Oct 3, 2022
ca24793
added qa+bc integration test
grantbuster Oct 3, 2022
b75b3fc
added version record to bias calc output files and incremented versio…
grantbuster Oct 4, 2022
b0a2c49
simplify qa test and pylint issue
grantbuster Oct 4, 2022
7b9c88f
fixed test on h5 meta attrs dtype and docstrings
grantbuster Oct 4, 2022
2ea15e3
serial data handling for QA+BC bug
grantbuster Oct 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added local linear bias correct to forward pass bc options
  • Loading branch information
grantbuster committed Oct 4, 2022
commit 7b28943dd9338df42445e5c72dc8ec7f775b708b
2 changes: 1 addition & 1 deletion sup3r/bias/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-
"""Bias calculation and correction modules."""
from .bias_transforms import bc_scalar_adder
from .bias_transforms import global_linear_bc, local_linear_bc
4 changes: 2 additions & 2 deletions sup3r/bias/bias_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def get_node_cmd(cls, config):
"t_elap = time.time() - t0;\n")

job_name = config.get('job_name', None)
if job_name is not None:
status_dir = config.get('status_dir', None)
status_dir = config.get('status_dir', None)
if job_name is not None and status_dir is not None:
status_file_arg_str = f'"{status_dir}", '
status_file_arg_str += f'module="{ModuleName.BIAS_CALC}", '
status_file_arg_str += f'job_name="{job_name}", '
Expand Down
1 change: 1 addition & 0 deletions sup3r/bias/bias_calc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def from_config(ctx, config_file, verbose):
jobs = config['jobs']
for i_node, job in enumerate(jobs):
node_config = copy.deepcopy(job)
node_config['status_dir'] = status_dir
node_config['log_file'] = (
log_pattern if log_pattern is None
else os.path.normpath(log_pattern.format(node_index=i_node)))
Expand Down
67 changes: 65 additions & 2 deletions sup3r/bias/bias_transforms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
"""Bias correction transformation functions."""
import numpy as np
from rex import Resource


def bc_scalar_adder(input, scalar, adder):
def global_linear_bc(input, scalar, adder, out_range=None):
"""Bias correct data using a simple global *scalar +adder method.

Parameters
Expand All @@ -13,10 +15,71 @@ def bc_scalar_adder(input, scalar, adder):
Scalar (multiplicative) value to apply to input data.
adder : float
Adder value to apply to input data.
out_range : None | tuple
Option to set floor/ceiling values on the output data.

Returns
-------
out : np.ndarray
out = input * scalar + adder
"""
return input * scalar + adder
out = input * scalar + adder
if out_range is not None:
out = np.maximum(out, np.min(out_range))
out = np.minimum(out, np.max(out_range))
return out


def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
out_range=None):
"""Bias correct data using a simple global *scalar +adder method.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit for local method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like site-by-site, right? yes, good catch


Parameters
----------
input : np.ndarray
Any data to be bias corrected
feature_name : str
Name of feature that is being corrected. Datasets with names
"{feature_name}_scalar" and "{feature_name}_adder" will be retrieved
from bias_fp.
bias_fp : str
Filepath to bias correction file from the bias calc module. Must have
datasets "{feature_name}_scalar" and "{feature_name}_adder" that are
the full low-resolution shape of the forward pass input that will be
sliced using lr_padded_slice for the current chunk.
lr_padded_slice : tuple
Tuple of length four that slices (spatial_1, spatial_2, temporal,
features) where each tuple entry is a slice object for that axes.
Note that if this method is called as part of a sup3r forward pass, the
lr_padded_slice will be included in the kwargs for the active chunk.
out_range : None | tuple
Option to set floor/ceiling values on the output data.

Returns
-------
out : np.ndarray
out = input * scalar + adder
"""

scalar = f'{feature_name}_scalar'
adder = f'{feature_name}_adder'
with Resource(bias_fp) as res:
scalar = res[scalar]
adder = res[adder]

spatial_slice = (lr_padded_slice[0], lr_padded_slice[1])
scalar = scalar[spatial_slice]
adder = adder[spatial_slice]

scalar = np.expand_dims(scalar, axis=-1)
adder = np.expand_dims(adder, axis=-1)

scalar = np.repeat(scalar, input.shape[-1], axis=-1)
adder = np.repeat(adder, input.shape[-1], axis=-1)

out = input * scalar + adder
if out_range is not None:
out = np.maximum(out, np.min(out_range))
out = np.minimum(out, np.max(out_range))

return out
60 changes: 44 additions & 16 deletions sup3r/pipeline/forward_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import copy
from datetime import datetime as dt
import psutil
from inspect import signature

from rex.utilities.fun_utils import get_fun_call_str
from rex.utilities.execution import SpawnProcessPool
Expand Down Expand Up @@ -1138,17 +1139,18 @@ def __init__(self, strategy, chunk_index=0, node_index=0):
input_handler_kwargs.update(self.strategy._input_handler_kwargs)
self.data_handler = self.input_handler_class(**input_handler_kwargs)
self.data_handler.load_cached_data()
self.input_data = self.data_handler.data

self.input_data = self.bias_correct_source_data(self.input_data)

exo_s_en = self.exo_kwargs.get('s_enhancements', None)
out = self.pad_source_data(self.data_handler.data,
out = self.pad_source_data(self.input_data,
self.pad_s1_start, self.pad_s1_end,
self.pad_s2_start, self.pad_s2_end,
self.pad_t_start, self.pad_t_end,
self.exogenous_data, exo_s_en)
self.input_data, self.exogenous_data = out

self.input_data = self.bias_correct_source_data(self.input_data)

@property
def file_paths(self):
"""Get a list of source filepaths to get data from. This list is
Expand All @@ -1168,17 +1170,31 @@ def temporal_pad_slice(self):

return ti_pad_slice

@property
def lr_padded_slice(self):
"""Get the padded slice argument that can be used to slice the full
domain source low res data to return just the extent used for the
current chunk.

Returns
-------
lr_padded_slice : tuple
Tuple of length four that slices (spatial_1, spatial_2, temporal,
features) where each tuple entry is a slice object for that axes.
"""
return self.strategy.lr_pad_slices[self.spatial_chunk_index]

@property
def target(self):
"""Get target for current spatial chunk"""
lr_slice = self.strategy.lr_pad_slices[self.spatial_chunk_index]
return self.strategy.lr_lat_lon[lr_slice[0], lr_slice[1]][-1, 0]
spatial_slice = self.lr_padded_slice[0], self.lr_padded_slice[1]
return self.strategy.lr_lat_lon[spatial_slice][-1, 0]

@property
def shape(self):
"""Get shape for current spatial chunk"""
lr_slice = self.strategy.lr_pad_slices[self.spatial_chunk_index]
return self.strategy.lr_lat_lon[lr_slice[0], lr_slice[1]].shape[:-1]
spatial_slice = self.lr_padded_slice[0], self.lr_padded_slice[1]
return self.strategy.lr_lat_lon[spatial_slice].shape[:-1]

@property
def chunks(self):
Expand Down Expand Up @@ -1382,11 +1398,16 @@ def bias_correct_source_data(self, data):
logger.info('Running bias correction with: {}'.format(method))
for feature, feature_kwargs in kwargs.items():
idf = self.data_handler.features.index(feature)
data[..., idf] = method(data[..., idf], **feature_kwargs)
logger.debug('Bias corrected feature "{}" at axis index {} '

if 'lr_padded_slice' in signature(method).parameters:
feature_kwargs['lr_padded_slice'] = self.lr_padded_slice

logger.debug('Bias correcting feature "{}" at axis index {} '
'using function: {} with kwargs: {}'
.format(feature, idf, method, feature_kwargs))

data[..., idf] = method(data[..., idf], **feature_kwargs)

return data

def _prep_exogenous_input(self, chunk_shape):
Expand Down Expand Up @@ -1623,11 +1644,17 @@ def incremental_check_run(cls, strategy, node_index, chunk_index):
logger.info('Not running chunk index {}, output file '
'exists: {}'.format(chunk_index, out_file))
else:
fwp = cls(strategy, chunk_index, node_index)
logger.info(f'Running forward pass for chunk_index={chunk_index}, '
f'node_index={node_index}, '
f'file_paths={fwp.file_paths}')
fwp.run_chunk()
try:
fwp = cls(strategy, chunk_index, node_index)
logger.info(f'Running forward pass for '
f'chunk_index={chunk_index}, '
f'node_index={node_index}, '
f'file_paths={fwp.file_paths}')
fwp.run_chunk()
except Exception as e:
msg = ('Sup3r ForwardPass chunk failed!')
logger.exception(msg)
raise RuntimeError(msg) from e

@classmethod
def run(cls, strategy, node_index):
Expand Down Expand Up @@ -1667,9 +1694,10 @@ def run(cls, strategy, node_index):
for chunk_index in strategy.node_chunks[node_index]:
future = exe.submit(cls.incremental_check_run,
strategy=strategy,
chunk_index=chunk_index,
node_index=node_index)
node_index=node_index,
chunk_index=chunk_index)
futures[future] = chunk_index

logger.info(f'Started {len(futures)} forward passes '
f'in {dt.now() - now}.')

Expand Down