# (C) Copyright 2025 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
from earthkit.utils.array.namespace.unknown import UnknownPatchedNamespace
from earthkit.utils.decorators import thread_safe_cached_property
[docs]
class PatchedNumpyNamespace(UnknownPatchedNamespace):
[docs]
def __init__(self):
super().__init__(None)
@thread_safe_cached_property
def xp(self):
import array_api_compat.numpy as np
return np
@property
def _earthkit_array_namespace_name(self):
return "numpy"
[docs]
def polyval(self, *args, **kwargs):
from numpy.polynomial.polynomial import polyval
return polyval(*args, **kwargs)
[docs]
def percentile(self, a, q, axis=None):
return self.xp.percentile(a, q, axis=axis)
[docs]
def quantile(self, a, q, axis=None):
return self.xp.quantile(a, q, axis=axis)
[docs]
def histogram2d(self, x, y, *, bins=10):
return self.xp.histogram2d(x, y, bins=bins)
[docs]
def histogramdd(self, x, *, bins=10):
return self.xp.histogramdd(x, bins=bins)
[docs]
def isclose(self, x, y, *, rtol=1e-5, atol=1e-8, equal_nan=False):
return self.xp.isclose(x, y, rtol=rtol, atol=atol, equal_nan=equal_nan)
[docs]
def allclose(self, x, y, *, rtol=1e-5, atol=1e-8, equal_nan=False):
return self.xp.allclose(x, y, rtol=rtol, atol=atol, equal_nan=equal_nan)
[docs]
def rad2deg(self, x):
return self.xp.rad2deg(x)
[docs]
def deg2rad(self, x):
return self.xp.deg2rad(x)
[docs]
def choice(self, a, size, replace=True, generator=None):
rng = self.xp.random.default_rng() if generator is None else generator
return rng.choice(a, size=size, replace=replace)