Skip to content

Commit

Permalink
CovModel: add 'needs_fourier_transform' attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerSeb committed Jan 1, 2025
1 parent 0e35a22 commit 2ded2e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
35 changes: 22 additions & 13 deletions src/gstools/covmodel/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.. autosummary::
CovModel
SumModel
"""

# pylint: disable=C0103, R0201, E1101, C0302, W0613, W0231
Expand Down Expand Up @@ -495,6 +496,13 @@ def percentile_scale(self, per=0.9):

# spectrum methods (can be overridden for speedup)

@property
def needs_fourier_transform(self):
"""Whether the model doesn't implement an analytical spectral density."""
base_method = getattr(CovModel, "spectral_density")
instance_method = getattr(self.__class__, "spectral_density")
return base_method == instance_method

def spectrum(self, k):
r"""
Spectrum of the covariance model.
Expand Down Expand Up @@ -1030,12 +1038,13 @@ def hankel_kw(self):

@hankel_kw.setter
def hankel_kw(self, hankel_kw):
if self._hankel_kw is None or hankel_kw is None:
self._hankel_kw = copy.copy(HANKEL_DEFAULT)
if hankel_kw is not None:
self._hankel_kw.update(hankel_kw)
if self.dim is not None:
self._sft = SFT(ndim=self.dim, **self.hankel_kw)
if self.needs_fourier_transform:
if self._hankel_kw is None or hankel_kw is None:
self._hankel_kw = copy.copy(HANKEL_DEFAULT)
if hankel_kw is not None:
self._hankel_kw.update(hankel_kw)
if self.dim is not None:
self._sft = SFT(ndim=self.dim, **self.hankel_kw)

@property
def dist_func(self):
Expand Down Expand Up @@ -1214,7 +1223,7 @@ class SumModel(CovModel):
len_scales : :class:`list` of :class:`float`, optional
length scale of the models. Will reset present length scales.
nugget : :class:`float`, optional
nugget of the sum-model. All summed models will a nugget of zero.
nugget of the sum-model. All summed models will have a nugget of zero.
Default: ``0.0``
anis : :class:`float` or :class:`list`, optional
anisotropy ratios in the transversal directions [e_y, e_z].
Expand Down Expand Up @@ -1355,7 +1364,7 @@ def __init__(self, *models, **kwargs):
if spatial_dim is not None:
kwargs["dim"] = spatial_dim + int(self.temporal)
self._dim = None
self._hankel_kw = {}
self._hankel_kw = None
self._sft = None
self.dim = kwargs.get("dim", self.models[0].dim if self.models else 3)
# prepare parameters (they are checked in dim setting)
Expand Down Expand Up @@ -1461,11 +1470,6 @@ def set_norm_len_ratios(self, ratios, skip=None, len_scale=None):
"""
sum_set_norm_len_ratios(self, ratios, skip, len_scale)

@property
def hankel_kw(self):
""":class:`dict`: :any:`hankel.SymmetricFourierTransform` kwargs."""
return self._hankel_kw

@property
def models(self):
""":class:`tuple`: The summed models."""
Expand Down Expand Up @@ -1623,6 +1627,11 @@ def calc_integral_scale(self):
0.0,
)

@property
def needs_fourier_transform(self):
"""Whether the model doesn't implement an analytical spectral density."""
return False

def spectral_density(self, k):
return sum(
(
Expand Down
3 changes: 2 additions & 1 deletion src/gstools/covmodel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ def set_dim(model, dim):
)
model._dim = int(dim)
# create fourier transform just once (recreate for dim change)
model._sft = SFT(ndim=model.dim, **model.hankel_kw)
if model.needs_fourier_transform:
model._sft = SFT(ndim=model.dim, **model.hankel_kw)
# recalculate dimension related parameters (if model initialized)
if model._init:
model.len_scale, model.anis = set_len_anis(
Expand Down

0 comments on commit 2ded2e0

Please sign in to comment.