Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,6 @@ src/mypy.txt

# Ignore .DS_Store files
.DS_Store

.ruff_cache/
temp_dir/
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


"""
A sophisticated [Transform][pyearthtools.data.transforms.Transform] to normalise and unnormalise data.
A sophisticated [Transform][pyearthtools.data.transforms.Transform] to normalise and denormalise data.

## Methods
| Name | Description |
Expand All @@ -30,10 +30,10 @@
## Transforms
[Normalise][pyearthtools.data.transforms.normalisation.normalise] provides the Transforms to normalise incoming data

[UnNormalise][pyearthtools.data.transforms.normalisation.unnormalise] provides the Transforms to unnormalise incoming data
[Denormalise][pyearthtools.data.transforms.normalisation.denormalise] provides the Transforms to denormalise incoming data

"""

from pyearthtools.data.transforms.normalisation import _utils
from pyearthtools.data.transforms.normalisation.normalise import Normalise
from pyearthtools.data.transforms.normalisation.unnormalise import Unnormalise
from pyearthtools.data.transforms.normalisation.denormalise import Denormalise
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def __init__(
"""
Base Normalise Class

Setup Transformer Class to normalise and unnormalise data
Setup Transformer Class to normalise and denormalise data

Can't be used directly, see `Normalise`, & `Unnormalise`.
Can't be used directly, see `Normalise`, & `Denormalise`.

Anomaly, Range, Deviation all require `start`, `end`, and `interval` to be given or `file`.

Expand Down Expand Up @@ -122,14 +122,14 @@ def __init__(
kwargs.update(skip_invalid=True)
self.retrieval_arguments = dict(start=start, end=end, interval=interval, verbose=verbose, **kwargs)

# TODO - Consider wrapping in a try/except block to catch if TemporaryDirectory() fails
if cache == "temp":
temp_dir = tempfile.TemporaryDirectory()
cache = temp_dir.name
self.temp_dir = temp_dir

self.cache_dir = cache
self.index = index

self._function = function
self.override = override
self.verbose = verbose
Expand All @@ -156,7 +156,7 @@ def check_init_args(self):
for arg in ["start", "end", "interval"]:
if not self.retrieval_arguments[arg]:
raise RuntimeError(
f"`override`, or (`start`, `end` and `interval`) was not given in `__init__`."
"`override`, or (`start`, `end` and `interval`) was not given in `__init__`."
"These must be given in order to find the normalisation values."
)
return True
Expand Down Expand Up @@ -356,7 +356,7 @@ def as_float(**kwargs):
return {variable_name: range[variable_name]}

def _find_user_normaliser(self, key: str):
if not "Normaliser" in key:
if "Normaliser" not in key:
raise AttributeError(f"{key!r} does not contain 'Normaliser', so is being ignored")
try:
return dynamic_import(key)(self)
Expand Down Expand Up @@ -480,7 +480,7 @@ def apply(self, dataset: xr.Dataset):

def __call__(self, method: str | dict | tuple, default: str | None = None) -> Transform:
"""
Get Transform to Normaliser or UnNormalise based on provided method
Get Transform to Normaliser or Denormalise based on provided method

Args:
method (dict): Dictionary assigning variable names to normalisation methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,53 @@
xr.set_options(keep_attrs=True)


class Unnormalise(Normaliser):
"""Unnormalise Incoming Data"""
class Denormalise(Normaliser):
"""Denormalise Incoming Data"""

@functools.wraps(Normaliser)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def __getattr__(self, key: str):
function = self._find_user_normaliser(key).unnormalise
function = self._find_user_normaliser(key).denormalise
if isinstance(function, Transform):
return function
return FunctionTransform(function)

@property
def anomaly(normalise_self):
"""
UnNormalise using anomalies
Denormalise using anomalies
"""

class AnomalyUnNormaliser(Transform):
class AnomalyDenormaliser(Transform):
@property
def _info_(self):
return normalise_self._info_

def apply(self, dataset: xr.Dataset):
"""UnNormalise Dataset by creating anomalies"""
"""Denormalise Dataset by creating anomalies"""
dataset = xr.Dataset(dataset)
for variable_name in dataset:
average = normalise_self.get_anomaly(variable_name)
dataset[variable_name] = dataset[variable_name] + average[variable_name]
return dataset

return AnomalyUnNormaliser()
return AnomalyDenormaliser()

@property
def range(normalise_self):
"""
UnNormalise using range
Denormalise using range
"""

class RangeUnNormaliser(Transform):
class RangeDenormaliser(Transform):
@property
def _info_(self):
return normalise_self._info_

def apply(self, dataset: xr.Dataset):
"""UnNormalise between 0-1 with ranges"""
"""Denormalise between 0-1 with ranges"""
dataset = xr.Dataset(dataset)
for variable_name in dataset:
range = normalise_self.get_range(variable_name)
Expand All @@ -85,7 +85,7 @@ def apply(self, dataset: xr.Dataset):

return dataset

return RangeUnNormaliser()
return RangeDenormaliser()

def manual_range(normalise_self, min: float, max: float):
warnings.warn(
Expand Down Expand Up @@ -116,16 +116,16 @@ def apply(self, dataset: xr.Dataset):
@property
def deviation(normalise_self):
"""
UnNormalise using mean & standard deviation
Denormalise using mean & standard deviation
"""

class DeviationUnNormaliser(Transform):
class DeviationDenormaliser(Transform):
@property
def _info_(self):
return normalise_self._info_

def apply(self, dataset: xr.Dataset):
"""UnNormalise Dataset using mean & standard deviation"""
"""Denormalise Dataset using mean & standard deviation"""
dataset = xr.Dataset(dataset)
for variable_name in dataset:
average, deviation = normalise_self.get_deviation(variable_name)
Expand All @@ -135,15 +135,15 @@ def apply(self, dataset: xr.Dataset):
]
return dataset

return DeviationUnNormaliser()
return DeviationDenormaliser()

@property
def temporal_difference(normalise_self):
"""
Normalise datasets using mean & standard deviation
"""

class TemporalDifferenceUnNormaliser(Transform):
class TemporalDifferenceDenormaliser(Transform):
"""Normalise Dataset using mean & standard deviation"""

@property
Expand All @@ -159,21 +159,21 @@ def apply(self, dataset: xr.Dataset):

return dataset

return TemporalDifferenceUnNormaliser()
return TemporalDifferenceDenormaliser()

@property
def deviation_spatial(normalise_self):
"""
UnNormalise using mean & standard deviation
Denormalise using mean & standard deviation
"""

class DeviationUnNormaliser(Transform):
class DeviationDenormaliser(Transform):
@property
def _info_(self):
return normalise_self._info_

def apply(self, dataset: xr.Dataset):
"""UnNormalise Dataset using mean & standard deviation spatially"""
"""Denormalise Dataset using mean & standard deviation spatially"""
dataset = xr.Dataset(dataset)
for variable_name in dataset:
average, deviation = normalise_self.get_deviation(variable_name, spatial=True)
Expand All @@ -183,27 +183,27 @@ def apply(self, dataset: xr.Dataset):
]
return dataset

return DeviationUnNormaliser()
return DeviationDenormaliser()

@property
def log(normalise_self):
"""
UnNormalise using exp
Denormalise using exp
"""

class LogUnNormaliser(Transform):
class LogDenormaliser(Transform):
@property
def _info_(self):
return normalise_self._info_

def apply(self, dataset: xr.Dataset):
"""UnNormalise Dataset with exp"""
"""Denormalise Dataset with exp"""
dataset = xr.Dataset(dataset)
for variable_name in dataset:
dataset[variable_name] = np.exp(dataset[variable_name])
return dataset

return LogUnNormaliser()
return LogDenormaliser()

@property
def function(self) -> FunctionTransform:
Expand All @@ -215,4 +215,4 @@ def function(self) -> FunctionTransform:
"""
if self._function is None:
raise ValueError("Cannot use function transform without a given function.\nTry giving `function` to init")
return FunctionTransform(self._function.unnormalise)
return FunctionTransform(self._function.denormalise)
1 change: 1 addition & 0 deletions packages/data/tests/download/test_arcoera5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pyearthtools.data.download import arcoera5

# FIXME: Skip slow downloads unless chosen specifically

def _load_sample(variables, levels, sample_time):
arco = arcoera5.ARCOERA5(variables, levels=levels)
Expand Down
Loading
Loading