Skip to content

Commit b1e277e

Browse files
committed
Update: refactoring
1 parent 3ce2598 commit b1e277e

File tree

10 files changed

+88
-224
lines changed

10 files changed

+88
-224
lines changed

kaleidoscope/algorithms/codec.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
from ..interface.algorithm import BlockAlgorithm
1414

1515

16+
def decode(x: da.Array, a: dict[str:Any]) -> da.Array:
17+
"""Returns decoded data."""
18+
f = Decode(np.single if x.dtype == np.single else np.double, x.ndim)
19+
y = f.apply_to(
20+
x,
21+
add_offset=a.get("add_offset", None),
22+
scale_factor=a.get("scale_factor", None),
23+
fill_value=a.get("_FillValue", None),
24+
valid_min=a.get("valid_min", None),
25+
valid_max=a.get("valid_max", None),
26+
)
27+
return y
28+
29+
30+
def encode(x: da.Array, a: dict[str:Any], dtype: np.dtype) -> da.Array:
31+
"""Returns encoded data."""
32+
f = Encode(dtype, x.ndim)
33+
y = f.apply_to(
34+
x,
35+
add_offset=a.get("add_offset", None),
36+
scale_factor=a.get("scale_factor", None),
37+
fill_value=a.get("_FillValue", None),
38+
valid_min=a.get("valid_min", None),
39+
valid_max=a.get("valid_max", None),
40+
)
41+
return y
42+
43+
1644
class Decode(BlockAlgorithm):
1745
"""
1846
The algorithm to decode data according to CF conventions.
Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# License: MIT
44

55
"""
6-
This module provides a main function.
6+
This module provides the Kaleidoscope collect main function
7+
to produce uncertainties from a given Monte Carlo ensemble.
78
"""
89

910
import json
@@ -17,7 +18,6 @@
1718
from importlib import resources
1819
from pathlib import Path
1920
from typing import Any
20-
from typing import Literal
2121
from typing import TextIO
2222

2323
import yaml
@@ -29,7 +29,7 @@
2929
from kaleidoscope.interface.reading import Reading
3030
from kaleidoscope.interface.writing import Writing
3131
from kaleidoscope.logger import get_logger
32-
from kaleidoscope.operators.derandop import DerandOp
32+
from kaleidoscope.operators.collectop import CollectOp
3333
from kaleidoscope.readerfactory import ReaderFactory
3434
from kaleidoscope.runner import Runner
3535
from kaleidoscope.signalhandler import AbortHandler
@@ -59,9 +59,9 @@ def create() -> ArgumentParser:
5959
:return: The parser.
6060
"""
6161
parser = _ArgumentParser(
62-
prog=__name__[::-1],
62+
prog=f"{__name__}-collect",
6363
description="This scientific processor computes standard "
64-
"uncertainty from simulated measurement errors.",
64+
"uncertainty from a given Monte Carlo ensemble.",
6565
epilog="Copyright (c) Brockmann Consult GmbH, 2025. "
6666
"License: MIT",
6767
exit_on_error=False,
@@ -87,7 +87,7 @@ def _add_arguments(parser):
8787
parser.add_argument(
8888
"target_file",
8989
help="the file path of the target dataset.",
90-
type=Parser.FileType("w"),
90+
type=Path,
9191
)
9292

9393
@staticmethod
@@ -140,27 +140,13 @@ def _add_options(parser):
140140
required=False,
141141
dest="progress",
142142
)
143-
parser.add_argument(
144-
"--no-progress",
145-
help="disable progress bar display.",
146-
action="store_false",
147-
required=False,
148-
dest="progress",
149-
)
150143
parser.add_argument(
151144
"--stack-traces",
152145
help="enable Python stack traces.",
153146
action="store_true",
154147
required=False,
155148
dest="stack_traces",
156149
)
157-
parser.add_argument(
158-
"--no-stack-traces",
159-
help="disable Python stack traces.",
160-
action="store_false",
161-
required=False,
162-
dest="stack_traces",
163-
)
164150

165151
@staticmethod
166152
def _add_version(parser):
@@ -172,39 +158,9 @@ def _add_version(parser):
172158
version=f"%(prog)s {__version__}",
173159
)
174160

175-
class FileType:
176-
"""! Callable to convert an argument into a file path."""
177-
178-
def __init__(self, mode: Literal["r", "w"] = "r"):
179-
"""
180-
Creates a new instance of this class.
181-
182-
@param mode The file mode required.
183-
"""
184-
self._mode = mode
185-
186-
def __call__(self, arg: str):
187-
"""Converts an argument into a file path."""
188-
path = Path(arg)
189-
if self._mode == "r":
190-
if not path.is_file():
191-
raise TypeError(
192-
f"Path {path} does not refer to an existing file"
193-
)
194-
if self._mode == "w":
195-
if path.is_dir() or not path.parent.is_dir():
196-
raise TypeError(
197-
f"Path {path} does not refer to a writeable file"
198-
)
199-
try:
200-
path = path.resolve()
201-
except RuntimeError:
202-
raise TypeError(f"File path {path} cannot be resolved")
203-
return path
204-
205161

206162
class Processor(Processing):
207-
"""! The Epocsodielak processor."""
163+
"""! The Kaleidoscope reduce processor."""
208164

209165
def __init__(self, config_package: str = "kaleidoscope.config"):
210166
"""
@@ -229,7 +185,7 @@ def get_default_config(self) -> dict[str:Any]: # noqa: D102
229185
return config
230186

231187
def get_name(self): # noqa: D102
232-
return __name__[::-1]
188+
return f"{__name__}-collect"
233189

234190
def get_version(self): # noqa: D102
235191
return __version__
@@ -271,7 +227,7 @@ def run(self, args: Namespace): # noqa: D102
271227
def get_result( # noqa: D102
272228
self, args: Namespace, *inputs: Dataset
273229
) -> Dataset:
274-
return DerandOp(args).run(inputs[0])
230+
return CollectOp(args).run(inputs[0])
275231

276232
def _create_reader(self, args) -> Reading:
277233
"""This method does not belong to public API."""
Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# License: MIT
44

55
"""
6-
This module provides a main function.
6+
This module provides the Kaleidoscope scatter main function
7+
to produce a Monte Carlo ensemble from given uncertainties.
78
"""
89

910
import json
@@ -17,7 +18,6 @@
1718
from importlib import resources
1819
from pathlib import Path
1920
from typing import Any
20-
from typing import Literal
2121
from typing import TextIO
2222

2323
import yaml
@@ -29,7 +29,7 @@
2929
from kaleidoscope.interface.reading import Reading
3030
from kaleidoscope.interface.writing import Writing
3131
from kaleidoscope.logger import get_logger
32-
from kaleidoscope.operators.randomop import RandomOp
32+
from kaleidoscope.operators.scatterop import ScatterOp
3333
from kaleidoscope.readerfactory import ReaderFactory
3434
from kaleidoscope.runner import Runner
3535
from kaleidoscope.signalhandler import AbortHandler
@@ -59,9 +59,9 @@ def create() -> ArgumentParser:
5959
:return: The parser.
6060
"""
6161
parser = _ArgumentParser(
62-
prog=f"{__name__}",
63-
description="This scientific processor simulates measurement "
64-
"errors.",
62+
prog=f"{__name__}-scatter",
63+
description="This scientific processor produces a Monte Carlo "
64+
"ensemble from given uncertainties.",
6565
epilog="Copyright (c) Brockmann Consult GmbH, 2025. "
6666
"License: MIT",
6767
exit_on_error=False,
@@ -78,12 +78,12 @@ def _add_arguments(parser):
7878
parser.add_argument(
7979
"source_file",
8080
help="the file path of the source dataset.",
81-
type=Parser.FileType("r"),
81+
type=Path,
8282
)
8383
parser.add_argument(
8484
"target_file",
8585
help="the file path of the target dataset.",
86-
type=Parser.FileType("w"),
86+
type=Path,
8787
)
8888

8989
@staticmethod
@@ -106,7 +106,8 @@ def _add_options(parser):
106106
"--selector",
107107
help="the Monte Carlo stream selector. An integral number which "
108108
"must not be negative.",
109-
type=Parser.IntType(0),
109+
choices=[i for i in range(101)],
110+
type=int,
110111
required=True,
111112
dest="selector",
112113
)
@@ -157,34 +158,13 @@ def _add_options(parser):
157158
required=False,
158159
dest="progress",
159160
)
160-
parser.add_argument(
161-
"--no-progress",
162-
help="disable progress bar display.",
163-
action="store_false",
164-
required=False,
165-
dest="progress",
166-
)
167161
parser.add_argument(
168162
"--stack-traces",
169163
help="enable Python stack traces.",
170164
action="store_true",
171165
required=False,
172166
dest="stack_traces",
173167
)
174-
parser.add_argument(
175-
"--no-stack-traces",
176-
help="disable Python stack traces.",
177-
action="store_false",
178-
required=False,
179-
dest="stack_traces",
180-
)
181-
parser.add_argument(
182-
"--tmpdir",
183-
help="specify the path to the temporary directory.",
184-
type=Parser.DirType(),
185-
required=False,
186-
dest="tmpdir",
187-
)
188168

189169
@staticmethod
190170
def _add_version(parser):
@@ -220,50 +200,6 @@ def __call__(self, arg: str) -> int:
220200
raise TypeError("Argument is not a valid integer value")
221201
return i
222202

223-
class DirType:
224-
"""Callable to convert an argument into a directory path."""
225-
226-
def __call__(self, arg: str) -> Path:
227-
"""Converts an argument into a directory path."""
228-
path = Path(arg)
229-
if not path.is_dir():
230-
raise TypeError("Path does not refer to a directory")
231-
try:
232-
path = path.resolve()
233-
except RuntimeError:
234-
raise TypeError(f"Directory path {path} cannot be resolved")
235-
return path
236-
237-
class FileType:
238-
"""! Callable to convert an argument into a file path."""
239-
240-
def __init__(self, mode: Literal["r", "w"] = "r"):
241-
"""
242-
Creates a new instance of this class.
243-
244-
@param mode The file mode required.
245-
"""
246-
self._mode = mode
247-
248-
def __call__(self, arg: str):
249-
"""Converts an argument into a file path."""
250-
path = Path(arg)
251-
if self._mode == "r":
252-
if not path.is_file():
253-
raise TypeError(
254-
f"Path {path} does not refer to an existing file"
255-
)
256-
if self._mode == "w":
257-
if path.is_dir() or not path.parent.is_dir():
258-
raise TypeError(
259-
f"Path {path} does not refer to a writeable file"
260-
)
261-
try:
262-
path = path.resolve()
263-
except RuntimeError:
264-
raise TypeError(f"File path {path} cannot be resolved")
265-
return path
266-
267203

268204
class Processor(Processing):
269205
"""! The Kaleidoscope processor."""
@@ -291,7 +227,7 @@ def get_default_config(self) -> dict[str:Any]: # noqa: D102
291227
return config
292228

293229
def get_name(self): # noqa: D102
294-
return __name__
230+
return f"{__name__}-scatter"
295231

296232
def get_version(self): # noqa: D102
297233
return __version__
@@ -333,7 +269,7 @@ def run(self, args: Namespace): # noqa: D102
333269
def get_result( # noqa: D102
334270
self, args: Namespace, *inputs: Dataset
335271
) -> Dataset:
336-
return RandomOp(args).run(inputs[0])
272+
return ScatterOp(args).run(inputs[0])
337273

338274
def _create_reader(self, args) -> Reading:
339275
"""This method does not belong to public API."""

0 commit comments

Comments
 (0)