Skip to content

Commit

Permalink
fix negative variance for constant variables (ecmwf#148)
Browse files Browse the repository at this point in the history
* variance is negative for constant variables, due to rounding errors. Setting it to zero when it is negative

* Update CHANGELOG.md
  • Loading branch information
floriankrb authored Dec 3, 2024
1 parent ddee516 commit 2c1e6b1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Keep it human-readable, your future self will thank you!
## Changed

- Fix metadata serialization handling of numpy.integer (#140)
- Fix negative variance for constant variables (#148)
- Fix cutout slicing of grid dimension (#145)

### Added
Expand Down
16 changes: 15 additions & 1 deletion src/anemoi/datasets/create/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def assert_is_fieldlist(obj):
def import_function(name, kind):

from anemoi.transform.filters import filter_registry
from anemoi.transform.sources import source_registry

name = name.replace("-", "_")

Expand All @@ -45,7 +46,20 @@ def import_function(name, kind):
if filter_registry.lookup(name, return_none=True):

def proc(context, data, *args, **kwargs):
return filter_registry.create(name, *args, **kwargs)(data)
filter = filter_registry.create(name, *args, **kwargs)
filter.context = context
# filter = filter_registry.create(context, name, *args, **kwargs)
return filter.forward(data)

return proc

if kind == "sources":
if source_registry.lookup(name, return_none=True):

def proc(context, data, *args, **kwargs):
source = source_registry.create(name, *args, **kwargs)
# source = source_registry.create(context, name, *args, **kwargs)
return source.forward(data)

return proc

Expand Down
4 changes: 2 additions & 2 deletions src/anemoi/datasets/create/statistics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def fix_variance(x, name, count, sums, squares):

variances = squares / count - mean * mean
assert variances.shape == squares.shape == mean.shape
if all(variances >= 0):
if np.all(variances >= 0):
LOG.warning(f"All individual variances for {name} are positive, setting variance to 0.")
return 0

Expand All @@ -108,7 +108,7 @@ def fix_variance(x, name, count, sums, squares):
# return 0

LOG.warning(f"ERROR at least one individual variance is negative ({np.nanmin(variances)}).")
return x
return 0


def check_variance(x, variables_names, minimum, maximum, mean, count, sums, squares):
Expand Down

0 comments on commit 2c1e6b1

Please sign in to comment.