Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] seed: use Ellipsis to indicate keeping current seed #269

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nosetests.xml
coverage.xml
*.cover
.hypothesis/
cov.xml

# Translations
*.mo
Expand Down
8 changes: 5 additions & 3 deletions src/gstools/field/cond_srf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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 @@ -80,8 +80,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 current seed will be kept. Default: :any:`Ellipsis`
mesh_type : :class:`str`
'structured' / 'unstructured'
post_process : :class:`bool`, optional
Expand Down
41 changes: 19 additions & 22 deletions src/gstools/field/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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 @@ -61,10 +61,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 current seed will be kept. Default: :any:`Ellipsis`
"""

@abstractmethod
Expand Down Expand Up @@ -235,7 +235,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 @@ -244,33 +244,30 @@ 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 current 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):
self.reset_seed(seed)
else:
self.reset_seed(self._seed)
self.reset_seed(self._seed if seed is Ellipsis else seed)
# just update the seed, if its a new one
elif seed is None or not np.isnan(seed):
elif 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 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):
if not (
elif model is None and seed is Ellipsis:
if (
isinstance(self._model, CovModel)
and self._z_1 is not None
and self._z_2 is not None
Expand All @@ -287,22 +284,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 current 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 not Ellipsis:
self._seed = seed
self._rng = RNG(self._seed)
# normal distributed samples for randmeth
Expand Down Expand Up @@ -351,7 +348,7 @@ def seed(self):

@seed.setter
def seed(self, new_seed):
if new_seed is not self._seed:
if new_seed != self._seed:
self.reset_seed(new_seed)

@property
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 @@ -103,7 +103,7 @@ def __init__(
def __call__(
self,
pos=None,
seed=np.nan,
seed=...,
point_volumes=0.0,
mesh_type="unstructured",
post_process=True,
Expand All @@ -118,8 +118,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 current 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
2 changes: 1 addition & 1 deletion src/gstools/krige/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def __call__(
ext_drift = self._pre_ext_drift(pnt_cnt, ext_drift)
# iterate chunks
for i in range(chunk_no):
# get chunk slice for actual chunk
# get chunk slice for current chunk
chunk_slice = (
i * chunk_size,
min(pnt_cnt, (i + 1) * chunk_size),
Expand Down
Loading