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 bias correction option to QA
  • Loading branch information
grantbuster committed Oct 4, 2022
commit 240a0d69d14f7f3a07f537190823402cbc7e6cab
11 changes: 7 additions & 4 deletions sup3r/bias/bias_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
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
lr_padded_slice : tuple | None
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.
If this is None, no slicing will be done and the full bias correction
source shape will be used.
out_range : None | tuple
Option to set floor/ceiling values on the output data.

Expand All @@ -67,9 +69,10 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
scalar = res[scalar]
adder = res[adder]

spatial_slice = (lr_padded_slice[0], lr_padded_slice[1])
scalar = scalar[spatial_slice]
adder = adder[spatial_slice]
if lr_padded_slice is not None:
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)
Expand Down
69 changes: 68 additions & 1 deletion sup3r/qa/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
from rex import Resource
from rex.utilities.fun_utils import get_fun_call_str
import sup3r.bias
from sup3r.postprocessing.file_handling import RexOutputs, H5_ATTRS
from sup3r.preprocessing.feature_handling import Feature
from sup3r.utilities import ModuleName
Expand All @@ -32,6 +33,8 @@ def __init__(self, source_file_paths, out_file_path, s_enhance, t_enhance,
shape=None,
raster_file=None,
qa_fp=None,
bias_correct_method=None,
bias_correct_kwargs=None,
save_sources=True,
time_chunk_size=None,
cache_pattern=None,
Expand Down Expand Up @@ -92,6 +95,18 @@ def __init__(self, source_file_paths, out_file_path, s_enhance, t_enhance,
qa_fp : str | None
Optional filepath to output QA file when you call Sup3rQa.run()
(only .h5 is supported)
bias_correct_method : str | None
Optional bias correction function name that can be imported from
the sup3r.bias module. This will transform the source data
according to some predefined bias correction transformation along
with the bias_correct_kwargs. As the first argument, this method
must receive a generic numpy array of data to be bias corrected
bias_correct_kwargs : dict | None
Optional namespace of kwargs to provide to bias_correct_method.
If this is provided, it must be a dictionary where each key is a
feature name and each value is a dictionary of kwargs to correct
that feature. You can bias correct only certain input features by
only including those feature names in this dict.
save_sources : bool
Flag to save re-coarsened synthetic data and true low-res data to
qa_fp in addition to the error dataset
Expand Down Expand Up @@ -145,6 +160,9 @@ def __init__(self, source_file_paths, out_file_path, s_enhance, t_enhance,
self.save_sources = save_sources
self.output_handler = self.output_handler_class(self._out_fp)

self.bias_correct_method = bias_correct_method
self.bias_correct_kwargs = bias_correct_kwargs or {}

HandlerClass = get_input_handler_class(source_file_paths,
input_handler)
self.source_handler = HandlerClass(source_file_paths,
Expand Down Expand Up @@ -272,6 +290,55 @@ def output_handler_class(self):
elif self.output_type == 'h5':
return Resource

def bias_correct_source_data(self, data, feature):
"""Bias correct data using a method defined by the bias_correct_method
input to ForwardPassStrategy

Parameters
----------
data : np.ndarray
Any source data to be bias corrected, with the feature channel in
the last axis.

Returns
-------
data : np.ndarray
Data corrected by the bias_correct_method ready for input to the
forward pass through the generative model.
"""
method = self.bias_correct_method
kwargs = self.bias_correct_kwargs
if method is not None:
method = getattr(sup3r.bias, method)
logger.info('Running bias correction with: {}'.format(method))
feature_kwargs = kwargs[feature]
logger.debug('Bias correcting feature "{}" using function: {} '
'with kwargs: {}'
.format(feature, method, feature_kwargs))

data = method(data, **feature_kwargs)

return data

def get_source_dset(self, idf, feature):
"""Get source low res input data including optional bias correction

Parameters
----------
idf : int
Feature index in axis=-1 of the source data handler.
feature : str
Feature name

Returns
-------
data_true : np.array
Low-res source input data including optional bias correction
"""
data_true = self.source_handler.data[..., idf]
data_true = self.bias_correct_source_data(data_true, feature)
return data_true

def get_dset_out(self, name):
"""Get an output dataset from the forward pass output file.

Expand Down Expand Up @@ -446,7 +513,7 @@ def run(self):
.format(idf + 1, len(self.features), feature))
data_syn = self.get_dset_out(feature)
data_syn = self.coarsen_data(data_syn)
data_true = self.source_handler.data[..., idf]
data_true = self.get_source_dset(idf, feature)

if data_syn.shape != data_true.shape:
msg = ('Sup3rQa failed while trying to inspect the "{}" '
Expand Down