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

Update uniform trans #310

Merged
merged 2 commits into from Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/gstools/transform/array.py
Expand Up @@ -215,9 +215,9 @@ def array_to_lognormal(field):
return np.exp(field)


def array_to_uniform(field, mean=None, var=None):
def array_to_uniform(field, mean=None, var=None, low=0.0, high=1.0):
"""
Transform normal distribution to uniform distribution on [0, 1].
Transform normal distribution to uniform distribution on [low, high].

Parameters
----------
Expand All @@ -230,6 +230,12 @@ def array_to_uniform(field, mean=None, var=None):
Variance of the given field.
If None is given, the variance will be calculated.
Default: :any:`None`
low : :class:`float`, optional
Lower bound for the uniform distribution.
Default: 0.0
high : :class:`float`, optional
Upper bound for the uniform distribution.
Default: 1.0

Returns
-------
Expand All @@ -239,7 +245,9 @@ def array_to_uniform(field, mean=None, var=None):
field = np.asarray(field)
mean = np.mean(field) if mean is None else float(mean)
var = np.var(field) if var is None else float(var)
return 0.5 * (1 + erf((field - mean) / np.sqrt(2 * var)))
return (
0.5 * (1 + erf((field - mean) / np.sqrt(2 * var))) * (high - low) + low
)


def array_to_arcsin(field, mean=None, var=None, a=None, b=None):
Expand Down
21 changes: 20 additions & 1 deletion src/gstools/transform/field.py
Expand Up @@ -548,6 +548,8 @@ def normal_to_lognormal(

def normal_to_uniform(
fld,
low=0.0,
high=1.0,
field="field",
store=True,
process=False,
Expand All @@ -562,14 +564,31 @@ def normal_to_uniform(
----------
fld : :any:`Field`
Field class containing a generated field.
low : :class:`float`, optional
Lower bound for the uniform distribution.
Default: 0.0
high : :class:`float`, optional
Upper bound for the uniform distribution.
Default: 1.0
field : :class:`str`, optional
Name of field to be transformed. The default is "field".
store : :class:`str` or :class:`bool`, optional
Whether to store field inplace (True/False) or under a given name.
The default is True.
process : :class:`bool`, optional
Whether to process in/out fields with trend, normalizer and mean
of given Field instance. The default is False.
keep_mean : :class:`bool`, optional
Whether to keep the mean of the field if process=True.
The default is True.
"""
if not process:
_check_for_default_normal(fld)
kw = dict(
mean=0.0 if process and not keep_mean else fld.mean, var=fld.model.sill
mean=0.0 if process and not keep_mean else fld.mean,
var=fld.model.sill,
low=low,
high=high,
)
return apply_function(
fld=fld,
Expand Down