Skip to content

Commit

Permalink
seed: use Ellipsis to indicate keeping the seed
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerSeb committed Oct 19, 2022
1 parent 231284f commit 8a0d1d5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/gstools/field/cond_srf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, krige, generator="RandMeth", **generator_kwargs):
def __call__(
self,
pos=None,
seed=np.nan,
seed=...,
mesh_type="unstructured",
post_process=True,
store=True,
Expand All @@ -81,8 +81,10 @@ def __call__(
pos : :class:`list`, optional
the position tuple, containing main direction and transversal
directions
seed : :class:`int`, optional
seed for RNG for resetting. Default: keep seed from generator
seed : :class:`int` or :any:`None` or :any:`Ellipsis`, optional
the seed of the random number generator.
If :any:`None`, a random seed is used. If :any:`Ellipsis`,
the actual seed will be kept. Default: :any:`Ellipsis`
mesh_type : :class:`str`
'structured' / 'unstructured'
post_process : :class:`bool`, optional
Expand Down
34 changes: 17 additions & 17 deletions src/gstools/field/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, model, **kwargs):
pass

@abstractmethod
def update(self, model=None, seed=np.nan):
def update(self, model=None, seed=...):
"""Update the model and the seed.
If model and seed are not different, nothing will be done.
Expand All @@ -60,10 +60,10 @@ def update(self, model=None, seed=np.nan):
----------
model : :any:`CovModel` or :any:`None`, optional
covariance model. Default: :any:`None`
seed : :class:`int` or :any:`None` or :any:`numpy.nan`, optional
seed : :class:`int` or :any:`None` or :any:`Ellipsis`, optional
the seed of the random number generator.
If :any:`None`, a random seed is used. If :any:`numpy.nan`,
the actual seed will be kept. Default: :any:`numpy.nan`
If :any:`None`, a random seed is used. If :any:`Ellipsis`,
the actual seed will be kept. Default: :any:`Ellipsis`
"""

@abstractmethod
Expand Down Expand Up @@ -238,7 +238,7 @@ def get_nugget(self, shape):
nugget = 0.0
return nugget

def update(self, model=None, seed=np.nan):
def update(self, model=None, seed=...):
"""Update the model and the seed.
If model and seed are not different, nothing will be done.
Expand All @@ -247,32 +247,32 @@ def update(self, model=None, seed=np.nan):
----------
model : :any:`CovModel` or :any:`None`, optional
covariance model. Default: :any:`None`
seed : :class:`int` or :any:`None` or :any:`numpy.nan`, optional
seed : :class:`int` or :any:`None` or :any:`Ellipsis`, optional
the seed of the random number generator.
If :any:`None`, a random seed is used. If :any:`numpy.nan`,
the actual seed will be kept. Default: :any:`numpy.nan`
If :any:`None`, a random seed is used. If :any:`Ellipsis`,
the actual seed will be kept. Default: :any:`Ellipsis`
"""
# check if a new model is given
if isinstance(model, CovModel):
if self.model != model:
self._model = dcp(model)
if seed is None or not np.isnan(seed):
if seed is None or seed is not Ellipsis:
self.reset_seed(seed)
else:
self.reset_seed(self._seed)
# just update the seed, if its a new one
elif seed is None or not np.isnan(seed):
elif seed is None or seed is not Ellipsis:
self.seed = seed
# or just update the seed, when no model is given
elif model is None and (seed is None or not np.isnan(seed)):
elif model is None and (seed is None or seed is not Ellipsis):
if isinstance(self._model, CovModel):
self.seed = seed
else:
raise ValueError(
"gstools.field.generator.RandMeth: no 'model' given"
)
# if the user tries to trick us, we beat him!
elif model is None and np.isnan(seed):
elif model is None and seed is Ellipsis:
if (
isinstance(self._model, CovModel)
and self._z_1 is not None
Expand All @@ -293,22 +293,22 @@ def update(self, model=None, seed=np.nan):
"instance of 'gstools.CovModel'"
)

def reset_seed(self, seed=np.nan):
def reset_seed(self, seed=...):
"""
Recalculate the random amplitudes and wave numbers with the given seed.
Parameters
----------
seed : :class:`int` or :any:`None` or :any:`numpy.nan`, optional
seed : :class:`int` or :any:`None` or :any:`Ellipsis`, optional
the seed of the random number generator.
If :any:`None`, a random seed is used. If :any:`numpy.nan`,
the actual seed will be kept. Default: :any:`numpy.nan`
If :any:`None`, a random seed is used. If :any:`Ellipsis`,
the actual seed will be kept. Default: :any:`Ellipsis`
Notes
-----
Even if the given seed is the present one, modes will be recalculated.
"""
if seed is None or not np.isnan(seed):
if seed is None or seed is not Ellipsis:
self._seed = seed
self._rng = RNG(self._seed)
# normal distributed samples for randmeth
Expand Down
8 changes: 5 additions & 3 deletions src/gstools/field/srf.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(
def __call__(
self,
pos=None,
seed=np.nan,
seed=...,
point_volumes=0.0,
mesh_type="unstructured",
post_process=True,
Expand All @@ -119,8 +119,10 @@ def __call__(
pos : :class:`list`, optional
the position tuple, containing main direction and transversal
directions
seed : :class:`int`, optional
seed for RNG for resetting. Default: keep seed from generator
seed : :class:`int` or :any:`None` or :any:`Ellipsis`, optional
the seed of the random number generator.
If :any:`None`, a random seed is used. If :any:`Ellipsis`,
the actual seed will be kept. Default: :any:`Ellipsis`
point_volumes : :class:`float` or :class:`numpy.ndarray`
If your evaluation points for the field are coming from a mesh,
they are probably representing a certain element volume.
Expand Down

0 comments on commit 8a0d1d5

Please sign in to comment.