Skip to content

Commit 5b9ede7

Browse files
committed
Update: refactoring
1 parent 99bc5d0 commit 5b9ede7

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

kaleidoscope/algorithms/randomize.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,6 @@
1515
from ..interface.generating import Normal
1616

1717

18-
def _chlorophyll(
19-
seed: np.ndarray, x: np.ndarray, u: np.ndarray
20-
) -> np.ndarray:
21-
"""
22-
Returns randomized values for ESA CCI ocean colour chlorophyll.
23-
24-
Uses ESA CCI OC PUG (Equation 2.10).
25-
"""
26-
return _lognormal(
27-
seed, x, x * np.sqrt(np.exp(np.square(np.log(10.0) * u)) - 1.0)
28-
)
29-
30-
31-
def _lognormal(seed: np.ndarray, x: np.ndarray, u: np.ndarray) -> np.ndarray:
32-
"""Returns randomized values for log-normally distributed errors."""
33-
v = np.log(1.0 + np.square(u / x))
34-
m = np.log(x) - 0.5 * v
35-
return np.exp(_normal(seed, m, np.sqrt(v)))
36-
37-
38-
def _normal(seed: np.ndarray, x: np.ndarray, u: np.ndarray) -> np.ndarray:
39-
"""Returns randomized values for normally distributed errors."""
40-
z: Normal = DefaultNormal(seed)
41-
return x + u * z.randoms(np.empty(x.shape, x.dtype))
42-
43-
4418
class Randomize(InformedBlockAlgorithm):
4519
"""
4620
The algorithm to randomize data.
@@ -113,11 +87,11 @@ def randomize(
11387
u = u * x
11488
match self._dist:
11589
case "normal":
116-
y = _normal(seed, x, u)
90+
y = self._normal(seed, x, u)
11791
case "lognormal":
118-
y = _lognormal(seed, x, u)
92+
y = self._lognormal(seed, x, u)
11993
case "chlorophyll":
120-
y = _chlorophyll(seed, x, u)
94+
y = self._chlorophyll(seed, x, u)
12195
case _:
12296
y = x
12397
if clip is not None:
@@ -130,6 +104,38 @@ def block_seed(self, block_id: tuple[int, ...]) -> np.ndarray:
130104
"""Returns the block seed."""
131105
return np.array([i for i in block_id] + [i for i in self._root_seed])
132106

107+
def _chlorophyll(
108+
self, seed: np.ndarray, x: np.ndarray, u: np.ndarray
109+
) -> np.ndarray:
110+
"""
111+
Returns randomized values for ESA CCI ocean colour chlorophyll.
112+
113+
Uses ESA CCI OC PUG (Equation 2.10).
114+
"""
115+
return self._lognormal(
116+
seed, x, x * np.sqrt(np.exp(np.square(np.log(10.0) * u)) - 1.0)
117+
)
118+
119+
def _lognormal(
120+
self, seed: np.ndarray, x: np.ndarray, u: np.ndarray
121+
) -> np.ndarray:
122+
"""
123+
Returns randomized values for log-normally distributed errors.
124+
"""
125+
v = np.log(1.0 + np.square(u / x))
126+
m = np.log(x) - 0.5 * v
127+
return np.exp(self._normal(seed, m, np.sqrt(v)))
128+
129+
# noinspection PyMethodMayBeStatic
130+
def _normal(
131+
self, seed: np.ndarray, x: np.ndarray, u: np.ndarray
132+
) -> np.ndarray:
133+
"""
134+
Returns randomized values for normally distributed errors.
135+
"""
136+
z: Normal = DefaultNormal(seed)
137+
return x + u * z.randoms(np.empty(x.shape, x.dtype))
138+
133139
@property
134140
def name(self) -> str:
135141
return "randomize"

0 commit comments

Comments
 (0)