-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathfield.py
446 lines (368 loc) · 13.1 KB
/
field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# -*- coding: utf-8 -*-
"""
GStools subpackage providing field transformations.
.. currentmodule:: gstools.transform.field
The following functions are provided
.. autosummary::
binary
boxcox
zinnharvey
normal_force_moments
normal_to_lognormal
normal_to_uniform
normal_to_arcsin
normal_to_uquad
"""
# pylint: disable=C0103, E1101
from warnings import warn
import numpy as np
from scipy.special import erf, erfinv
__all__ = [
"binary",
"boxcox",
"zinnharvey",
"normal_force_moments",
"normal_to_lognormal",
"normal_to_uniform",
"normal_to_arcsin",
"normal_to_uquad",
]
def binary(fld, divide=None, upper=None, lower=None):
"""
Binary transformation.
After this transformation, the field only has two values.
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
divide : :class:`float`, optional
The dividing value.
Default: ``fld.mean``
upper : :class:`float`, optional
The resulting upper value of the field.
Default: ``mean + sqrt(fld.model.sill)``
lower : :class:`float`, optional
The resulting lower value of the field.
Default: ``mean - sqrt(fld.model.sill)``
"""
if fld.field is None:
print("binary: no field stored in SRF class.")
else:
divide = fld.mean if divide is None else divide
upper = fld.mean + np.sqrt(fld.model.sill) if upper is None else upper
lower = fld.mean - np.sqrt(fld.model.sill) if lower is None else lower
fld.field[fld.field > divide] = upper
fld.field[fld.field <= divide] = lower
def boxcox(fld, lmbda=1, shift=0):
"""
Box-Cox transformation.
After this transformation, the again Box-Cox transformed field is normal
distributed.
See: https://en.wikipedia.org/wiki/Power_transform#Box%E2%80%93Cox_transformation
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
lmbda : :class:`float`, optional
The lambda parameter of the Box-Cox transformation.
For ``lmbda=0`` one obtains the log-normal transformation.
Default: ``1``
shift : :class:`float`, optional
The shift parameter from the two-parametric Box-Cox transformation.
The field will be shifted by that value before transformation.
Default: ``0``
"""
if fld.field is None:
print("Box-Cox: no field stored in SRF class.")
else:
fld.mean += shift
fld.field += shift
if np.isclose(lmbda, 0):
normal_to_lognormal(fld)
if np.min(fld.field) < -1 / lmbda:
warn("Box-Cox: Some values will be cut off!")
fld.field = (np.maximum(lmbda * fld.field + 1, 0)) ** (1 / lmbda)
def zinnharvey(fld, conn="high"):
"""
Zinn and Harvey transformation to connect low or high values.
After this transformation, the field is still normal distributed.
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
conn : :class:`str`, optional
Desired connectivity. Either "low" or "high".
Default: "high"
"""
if fld.field is None:
print("zinnharvey: no field stored in SRF class.")
else:
fld.field = _zinnharvey(fld.field, conn, fld.mean, fld.model.sill)
def normal_force_moments(fld):
"""
Force moments of a normal distributed field.
After this transformation, the field is still normal distributed.
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
"""
if fld.field is None:
print("normal_force_moments: no field stored in SRF class.")
else:
fld.field = _normal_force_moments(fld.field, fld.mean, fld.model.sill)
def normal_to_lognormal(fld):
"""
Transform normal distribution to log-normal distribution.
After this transformation, the field is log-normal distributed.
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
"""
if fld.field is None:
print("normal_to_lognormal: no field stored in SRF class.")
else:
fld.field = _normal_to_lognormal(fld.field)
def normal_to_uniform(fld):
"""
Transform normal distribution to uniform distribution on [0, 1].
After this transformation, the field is uniformly distributed on [0, 1].
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
"""
if fld.field is None:
print("normal_to_uniform: no field stored in SRF class.")
else:
fld.field = _normal_to_uniform(fld.field, fld.mean, fld.model.sill)
def normal_to_arcsin(fld, a=None, b=None):
"""
Transform normal distribution to the bimodal arcsin distribution.
See: https://en.wikipedia.org/wiki/Arcsine_distribution
After this transformation, the field is arcsin-distributed on [a, b].
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
a : :class:`float`, optional
Parameter a of the arcsin distribution (lower bound).
Default: keep mean and variance
b : :class:`float`, optional
Parameter b of the arcsin distribution (upper bound).
Default: keep mean and variance
"""
if fld.field is None:
print("normal_to_arcsin: no field stored in SRF class.")
else:
a = fld.mean - np.sqrt(2.0 * fld.model.sill) if a is None else a
b = fld.mean + np.sqrt(2.0 * fld.model.sill) if b is None else b
fld.field = _normal_to_arcsin(
fld.field, fld.mean, fld.model.sill, a, b
)
fld.mean = (a + b) / 2.0
def normal_to_uquad(fld, a=None, b=None):
"""
Transform normal distribution to U-quadratic distribution.
See: https://en.wikipedia.org/wiki/U-quadratic_distribution
After this transformation, the field is U-quadratic-distributed on [a, b].
Parameters
----------
fld : :any:`Field`
Spatial Random Field class containing a generated field.
Field will be transformed inplace.
a : :class:`float`, optional
Parameter a of the U-quadratic distribution (lower bound).
Default: keep mean and variance
b : :class:`float`, optional
Parameter b of the U-quadratic distribution (upper bound).
Default: keep mean and variance
"""
if fld.field is None:
print("normal_to_uquad: no field stored in SRF class.")
else:
a = fld.mean - np.sqrt(5.0 / 3.0 * fld.model.sill) if a is None else a
b = fld.mean + np.sqrt(5.0 / 3.0 * fld.model.sill) if b is None else b
fld.field = _normal_to_uquad(fld.field, fld.mean, fld.model.sill, a, b)
fld.mean = (a + b) / 2.0
# low level functions
def _zinnharvey(field, conn="high", mean=None, var=None):
"""
Zinn and Harvey transformation to connect low or high values.
Parameters
----------
field : :class:`numpy.ndarray`
Spatial Random Field with normal distributed values.
As returned by SRF.
conn : :class:`str`, optional
Desired connectivity. Either "low" or "high".
Default: "high"
mean : :class:`float` or :any:`None`, optional
Mean of the given field. If None is given, the mean will be calculated.
Default: :any:`None`
var : :class:`float` or :any:`None`, optional
Variance of the given field.
If None is given, the mean will be calculated.
Default: :any:`None`
Returns
-------
:class:`numpy.ndarray`
Transformed field.
"""
if mean is None:
mean = np.mean(field)
if var is None:
var = np.var(field)
field = np.abs((field - mean) / var)
field = 2 * erf(field / np.sqrt(2)) - 1
field = np.sqrt(2) * erfinv(field)
if conn == "high":
field = -field
return field * var + mean
def _normal_force_moments(field, mean=0, var=1):
"""
Force moments of a normal distributed field.
Parameters
----------
field : :class:`numpy.ndarray`
Spatial Random Field with normal distributed values.
As returned by SRF.
mean : :class:`float`, optional
Desired mean of the field.
Default: 0
var : :class:`float` or :any:`None`, optional
Desired variance of the field.
Default: 1
Returns
-------
:class:`numpy.ndarray`
Transformed field.
"""
var_in = np.var(field)
mean_in = np.mean(field)
rescale = np.sqrt(var / var_in)
return rescale * (field - mean_in) + mean
def _normal_to_lognormal(field):
"""
Transform normal distribution to log-normal distribution.
Parameters
----------
field : :class:`numpy.ndarray`
Spatial Random Field with normal distributed values.
As returned by SRF.
Returns
-------
:class:`numpy.ndarray`
Transformed field.
"""
return np.exp(field)
def _normal_to_uniform(field, mean=None, var=None):
"""
Transform normal distribution to uniform distribution on [0, 1].
Parameters
----------
field : :class:`numpy.ndarray`
Spatial Random Field with normal distributed values.
As returned by SRF.
mean : :class:`float` or :any:`None`, optional
Mean of the given field. If None is given, the mean will be calculated.
Default: :any:`None`
var : :class:`float` or :any:`None`, optional
Variance of the given field.
If None is given, the mean will be calculated.
Default: :any:`None`
Returns
-------
:class:`numpy.ndarray`
Transformed field.
"""
if mean is None:
mean = np.mean(field)
if var is None:
var = np.var(field)
return 0.5 * (1 + erf((field - mean) / np.sqrt(2 * var)))
def _normal_to_arcsin(field, mean=None, var=None, a=0, b=1):
"""
Transform normal distribution to arcsin distribution.
See: https://en.wikipedia.org/wiki/Arcsine_distribution
Parameters
----------
field : :class:`numpy.ndarray`
Spatial Random Field with normal distributed values.
As returned by SRF.
mean : :class:`float` or :any:`None`, optional
Mean of the given field. If None is given, the mean will be calculated.
Default: :any:`None`
var : :class:`float` or :any:`None`, optional
Variance of the given field.
If None is given, the mean will be calculated.
Default: :any:`None`
a : :class:`float`, optional
Parameter a of the arcsin distribution. Default: 0
b : :class:`float`, optional
Parameter b of the arcsin distribution. Default: 1
Returns
-------
:class:`numpy.ndarray`
Transformed field.
"""
return _uniform_to_arcsin(_normal_to_uniform(field, mean, var), a, b)
def _normal_to_uquad(field, mean=None, var=None, a=0, b=1):
"""
Transform normal distribution to U-quadratic distribution.
See: https://en.wikipedia.org/wiki/U-quadratic_distribution
Parameters
----------
field : :class:`numpy.ndarray`
Spatial Random Field with normal distributed values.
As returned by SRF.
mean : :class:`float` or :any:`None`, optional
Mean of the given field. If None is given, the mean will be calculated.
Default: :any:`None`
var : :class:`float` or :any:`None`, optional
Variance of the given field.
If None is given, the mean will be calculated.
Default: :any:`None`
a : :class:`float`, optional
Parameter a of the U-quadratic distribution. Default: 0
b : :class:`float`, optional
Parameter b of the U-quadratic distribution. Default: 1
Returns
-------
:class:`numpy.ndarray`
Transformed field.
"""
return _uniform_to_uquad(_normal_to_uniform(field, mean, var), a, b)
def _uniform_to_arcsin(field, a=0, b=1):
"""
PPF of your desired distribution.
The PPF is the inverse of the CDF and is used to sample a distribution
from uniform distributed values on [0, 1]
in this case: the arcsin distribution
See: https://en.wikipedia.org/wiki/Arcsine_distribution
"""
return (b - a) * np.sin(np.pi * 0.5 * field) ** 2 + a
def _uniform_to_uquad(field, a=0, b=1):
"""
PPF of your desired distribution.
The PPF is the inverse of the CDF and is used to sample a distribution
from uniform distributed values on [0, 1]
in this case: the U-quadratic distribution
See: https://en.wikipedia.org/wiki/U-quadratic_distribution
"""
al = 12 / (b - a) ** 3
be = (a + b) / 2
ga = (a - b) ** 3 / 8
y_raw = 3 * field / al + ga
out = np.zeros_like(y_raw)
out[y_raw > 0] = y_raw[y_raw > 0] ** (1 / 3)
out[y_raw < 0] = -((-y_raw[y_raw < 0]) ** (1 / 3))
return out + be