anoscore 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anoscore/__init__.py +84 -0
- anoscore/__main__.py +16 -0
- anoscore/adapters.py +27 -0
- anoscore/calibration.py +46 -0
- anoscore/cli/__init__.py +11 -0
- anoscore/cli/_app.py +196 -0
- anoscore/cli/_commands.py +153 -0
- anoscore/cli/_io.py +200 -0
- anoscore/compat/__init__.py +11 -0
- anoscore/core/__init__.py +471 -0
- anoscore/core/adapters.py +428 -0
- anoscore/core/calibration/__init__.py +46 -0
- anoscore/core/calibration/base.py +1749 -0
- anoscore/core/compat/__init__.py +10 -0
- anoscore/core/compat/base.py +294 -0
- anoscore/core/detect.py +1302 -0
- anoscore/core/diagnostics.py +367 -0
- anoscore/core/limits.py +295 -0
- anoscore/core/proactive/__init__.py +36 -0
- anoscore/core/proactive/base.py +911 -0
- anoscore/core/reactive/__init__.py +53 -0
- anoscore/core/reactive/base.py +950 -0
- anoscore/core/reactive/sequential.py +699 -0
- anoscore/core/reference.py +112 -0
- anoscore/core/score_all.py +337 -0
- anoscore/core/tails/__init__.py +26 -0
- anoscore/core/tails/base.py +633 -0
- anoscore/detect.py +26 -0
- anoscore/diagnostics.py +10 -0
- anoscore/emit/__init__.py +10 -0
- anoscore/emit/base.py +65 -0
- anoscore/limits.py +10 -0
- anoscore/policy.py +172 -0
- anoscore/proactive.py +36 -0
- anoscore/py.typed +0 -0
- anoscore/reactive.py +50 -0
- anoscore/reference.py +10 -0
- anoscore/score_all.py +11 -0
- anoscore/tails.py +27 -0
- anoscore-0.1.0.dist-info/METADATA +178 -0
- anoscore-0.1.0.dist-info/RECORD +44 -0
- anoscore-0.1.0.dist-info/WHEEL +4 -0
- anoscore-0.1.0.dist-info/entry_points.txt +3 -0
- anoscore-0.1.0.dist-info/licenses/LICENSE +21 -0
anoscore/__init__.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""anoscore: quantile-forecast anomaly scoring.
|
|
2
|
+
|
|
3
|
+
The UX reaches the package as ``ans``: scorers through ``ans.reactive`` and
|
|
4
|
+
``ans.proactive``, gridded results through ``ans.diagnostics``, ``ans.adapters``
|
|
5
|
+
and ``ans.reference``, and the pipeline verbs ``ans.calibrate``,
|
|
6
|
+
``ans.aggregate``, ``ans.detect`` and ``ans.score_all``. The latter two are the
|
|
7
|
+
callable functions at the package root (per UX.md); their modules stay
|
|
8
|
+
importable as ``from anoscore.detect import detect`` and
|
|
9
|
+
``importlib.import_module("anoscore.detect")``. Every public module is
|
|
10
|
+
NumPy-free at its own scope and re-exports its implementation from
|
|
11
|
+
``anoscore.core``.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from anoscore import (
|
|
15
|
+
adapters,
|
|
16
|
+
calibration,
|
|
17
|
+
compat,
|
|
18
|
+
diagnostics,
|
|
19
|
+
emit,
|
|
20
|
+
limits,
|
|
21
|
+
policy,
|
|
22
|
+
proactive,
|
|
23
|
+
reactive,
|
|
24
|
+
reference,
|
|
25
|
+
tails,
|
|
26
|
+
)
|
|
27
|
+
from anoscore.calibration import Calibration, calibrate
|
|
28
|
+
from anoscore.core.adapters import LEVELS_9, LEVELS_21
|
|
29
|
+
from anoscore.detect import (
|
|
30
|
+
AlarmPolicy,
|
|
31
|
+
AnomalyEvent,
|
|
32
|
+
EventSet,
|
|
33
|
+
aggregate,
|
|
34
|
+
detect,
|
|
35
|
+
reduce_horizon,
|
|
36
|
+
)
|
|
37
|
+
from anoscore.diagnostics import DiagnosticsReport
|
|
38
|
+
from anoscore.emit import register_format
|
|
39
|
+
from anoscore.limits import Limits
|
|
40
|
+
from anoscore.policy import AnoscoreWarning, get_policy, set_policy
|
|
41
|
+
from anoscore.reference import NominalGrid
|
|
42
|
+
from anoscore.score_all import ScoreSet, score_all
|
|
43
|
+
from anoscore.tails import GPD, Clip, Empirical, Exponential, Gaussian
|
|
44
|
+
|
|
45
|
+
__version__ = "0.1.0"
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"GPD",
|
|
49
|
+
"LEVELS_9",
|
|
50
|
+
"LEVELS_21",
|
|
51
|
+
"AlarmPolicy",
|
|
52
|
+
"AnomalyEvent",
|
|
53
|
+
"AnoscoreWarning",
|
|
54
|
+
"Calibration",
|
|
55
|
+
"Clip",
|
|
56
|
+
"DiagnosticsReport",
|
|
57
|
+
"Empirical",
|
|
58
|
+
"EventSet",
|
|
59
|
+
"Exponential",
|
|
60
|
+
"Gaussian",
|
|
61
|
+
"Limits",
|
|
62
|
+
"NominalGrid",
|
|
63
|
+
"ScoreSet",
|
|
64
|
+
"__version__",
|
|
65
|
+
"adapters",
|
|
66
|
+
"aggregate",
|
|
67
|
+
"calibrate",
|
|
68
|
+
"calibration",
|
|
69
|
+
"compat",
|
|
70
|
+
"detect",
|
|
71
|
+
"diagnostics",
|
|
72
|
+
"emit",
|
|
73
|
+
"get_policy",
|
|
74
|
+
"limits",
|
|
75
|
+
"policy",
|
|
76
|
+
"proactive",
|
|
77
|
+
"reactive",
|
|
78
|
+
"reduce_horizon",
|
|
79
|
+
"reference",
|
|
80
|
+
"register_format",
|
|
81
|
+
"score_all",
|
|
82
|
+
"set_policy",
|
|
83
|
+
"tails",
|
|
84
|
+
]
|
anoscore/__main__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
|
|
4
|
+
from anoscore.cli import _main
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
11
|
+
"""Dispatch to the CLI and return its exit code."""
|
|
12
|
+
return _main(argv)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
if __name__ == "__main__":
|
|
16
|
+
sys.exit(main())
|
anoscore/adapters.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Public adapter shim.
|
|
2
|
+
|
|
3
|
+
Re-exports the adapter functions and level constants from
|
|
4
|
+
``anoscore.core.adapters``. This module imports no NumPy and no model package,
|
|
5
|
+
so ``import anoscore.adapters`` succeeds with no optional dependencies
|
|
6
|
+
installed (conformance item 92).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from anoscore.core.adapters import (
|
|
10
|
+
LEVELS_9,
|
|
11
|
+
LEVELS_21,
|
|
12
|
+
from_array,
|
|
13
|
+
from_chronos2,
|
|
14
|
+
from_chronos2_df,
|
|
15
|
+
from_timesfm3,
|
|
16
|
+
from_toto2,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"LEVELS_9",
|
|
21
|
+
"LEVELS_21",
|
|
22
|
+
"from_array",
|
|
23
|
+
"from_chronos2",
|
|
24
|
+
"from_chronos2_df",
|
|
25
|
+
"from_timesfm3",
|
|
26
|
+
"from_toto2",
|
|
27
|
+
]
|
anoscore/calibration.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Public calibration shim.
|
|
2
|
+
|
|
3
|
+
Re-exports the calibration artifact and estimators from
|
|
4
|
+
``anoscore.core.calibration``. This module imports no NumPy, so the public
|
|
5
|
+
import surface stays free of the numerical dependency.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from anoscore.core.calibration import (
|
|
9
|
+
Calibration,
|
|
10
|
+
ReachSample,
|
|
11
|
+
calibrate,
|
|
12
|
+
conformal_p,
|
|
13
|
+
conformal_p_weighted,
|
|
14
|
+
conformal_score,
|
|
15
|
+
exp_threshold,
|
|
16
|
+
fdr_threshold,
|
|
17
|
+
fit_covariance,
|
|
18
|
+
fit_empirical_threshold,
|
|
19
|
+
fit_gpd_shape,
|
|
20
|
+
fit_pit_map,
|
|
21
|
+
fit_pot,
|
|
22
|
+
fit_step_dependence,
|
|
23
|
+
threshold_for_rate,
|
|
24
|
+
thresholds_for_rate,
|
|
25
|
+
to_normal_score,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"Calibration",
|
|
30
|
+
"ReachSample",
|
|
31
|
+
"calibrate",
|
|
32
|
+
"conformal_p",
|
|
33
|
+
"conformal_p_weighted",
|
|
34
|
+
"conformal_score",
|
|
35
|
+
"exp_threshold",
|
|
36
|
+
"fdr_threshold",
|
|
37
|
+
"fit_covariance",
|
|
38
|
+
"fit_empirical_threshold",
|
|
39
|
+
"fit_gpd_shape",
|
|
40
|
+
"fit_pit_map",
|
|
41
|
+
"fit_pot",
|
|
42
|
+
"fit_step_dependence",
|
|
43
|
+
"threshold_for_rate",
|
|
44
|
+
"thresholds_for_rate",
|
|
45
|
+
"to_normal_score",
|
|
46
|
+
]
|
anoscore/cli/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Command-line interface package.
|
|
2
|
+
|
|
3
|
+
Conformance items 104-108. :mod:`anoscore.cli._app` builds the parser and
|
|
4
|
+
dispatches the five documented commands; ``__main__`` owns process exit. The
|
|
5
|
+
package imports no dataframe or Arrow package, so the CLI adds no hard
|
|
6
|
+
dependency to the library.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from anoscore.cli._app import _build_parser, _main
|
|
10
|
+
|
|
11
|
+
__all__ = ["_build_parser", "_main"]
|
anoscore/cli/_app.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Command-line dispatch: five subcommands over the public functions.
|
|
2
|
+
|
|
3
|
+
Conformance items 104-108. The parser is built with the standard library only
|
|
4
|
+
and mirrors the documented flags: every data or behaviour flag maps to a
|
|
5
|
+
parameter of the library function the command wraps, so the CLI has no
|
|
6
|
+
behaviour the library lacks. ``--format`` resolves the registered event
|
|
7
|
+
emitters. ``_main`` returns a non-zero code when a raise-level policy condition
|
|
8
|
+
fires, so a cron entry fails loudly instead of writing a bad artifact.
|
|
9
|
+
|
|
10
|
+
``sys.stdout.write`` is used for output (``print`` is banned outside
|
|
11
|
+
``__main__``) and ``__main__`` owns process exit.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import argparse
|
|
15
|
+
import sys
|
|
16
|
+
from typing import TYPE_CHECKING, Any
|
|
17
|
+
|
|
18
|
+
from anoscore import __version__
|
|
19
|
+
from anoscore.cli import _commands
|
|
20
|
+
from anoscore.cli._io import _floats, _load_toml
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from collections.abc import Mapping, Sequence
|
|
24
|
+
|
|
25
|
+
__all__ = []
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _add_forecast_arg(parser: argparse.ArgumentParser) -> None:
|
|
29
|
+
"""Add the canonical-forecast JSON input every command but ``detect`` uses."""
|
|
30
|
+
parser.add_argument("--fc", required=True, help="forecast JSON file")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _add_obs_arg(
|
|
34
|
+
parser: argparse.ArgumentParser,
|
|
35
|
+
*,
|
|
36
|
+
required: bool,
|
|
37
|
+
) -> None:
|
|
38
|
+
"""Add the observations JSON input, mapping ``--obs`` to the ``y`` parameter."""
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
"--obs",
|
|
41
|
+
dest="y",
|
|
42
|
+
required=required,
|
|
43
|
+
default=None,
|
|
44
|
+
help="observations JSON file",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _add_out_arg(
|
|
49
|
+
parser: argparse.ArgumentParser,
|
|
50
|
+
*,
|
|
51
|
+
required: bool = False,
|
|
52
|
+
) -> None:
|
|
53
|
+
"""Add the output path; absent, output goes to stdout."""
|
|
54
|
+
parser.add_argument(
|
|
55
|
+
"--out",
|
|
56
|
+
required=required,
|
|
57
|
+
default=None,
|
|
58
|
+
help="write output here instead of standard output",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _add_diagnose_parser(
|
|
63
|
+
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
|
64
|
+
defaults: Mapping[str, Any],
|
|
65
|
+
) -> None:
|
|
66
|
+
parser = subparsers.add_parser("diagnose", help="grid-quality diagnostics")
|
|
67
|
+
_add_forecast_arg(parser)
|
|
68
|
+
_add_obs_arg(parser, required=False)
|
|
69
|
+
_add_out_arg(parser)
|
|
70
|
+
parser.set_defaults(func=_commands._diagnose, **defaults)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _add_calibrate_parser(
|
|
74
|
+
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
|
75
|
+
defaults: Mapping[str, Any],
|
|
76
|
+
) -> None:
|
|
77
|
+
parser = subparsers.add_parser("calibrate", help="fit a calibration artifact")
|
|
78
|
+
_add_forecast_arg(parser)
|
|
79
|
+
_add_obs_arg(parser, required=True)
|
|
80
|
+
parser.add_argument("--tail", default="clip", help="tail model name")
|
|
81
|
+
parser.add_argument("--reference", default=None, help="reference grid JSON")
|
|
82
|
+
parser.add_argument("--limits", default=None, help="limits JSON")
|
|
83
|
+
parser.add_argument(
|
|
84
|
+
"--sentinel", type=_floats, default=None, help="sentinel values"
|
|
85
|
+
)
|
|
86
|
+
_add_out_arg(parser, required=True)
|
|
87
|
+
parser.set_defaults(func=_commands._calibrate, **defaults)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _add_score_parser(
|
|
91
|
+
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
|
92
|
+
defaults: Mapping[str, Any],
|
|
93
|
+
) -> None:
|
|
94
|
+
parser = subparsers.add_parser("score", help="map every applicable scorer")
|
|
95
|
+
_add_forecast_arg(parser)
|
|
96
|
+
_add_obs_arg(parser, required=False)
|
|
97
|
+
parser.add_argument("--cal", default=None, help="calibration artifact JSON")
|
|
98
|
+
parser.add_argument("--limits", default=None, help="limits JSON")
|
|
99
|
+
parser.add_argument("--reference", default=None, help="reference grid JSON")
|
|
100
|
+
parser.add_argument("--scorers", default=None, help="comma-separated scorer names")
|
|
101
|
+
_add_out_arg(parser)
|
|
102
|
+
parser.set_defaults(func=_commands._score, **defaults)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _add_detect_parser(
|
|
106
|
+
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
|
107
|
+
defaults: Mapping[str, Any],
|
|
108
|
+
) -> None:
|
|
109
|
+
parser = subparsers.add_parser("detect", help="threshold a score into events")
|
|
110
|
+
parser.add_argument("--score", dest="s", required=True, help="score JSON file")
|
|
111
|
+
parser.add_argument(
|
|
112
|
+
"--scorer", default=None, help="scorer to read from a score set"
|
|
113
|
+
)
|
|
114
|
+
cut = parser.add_mutually_exclusive_group()
|
|
115
|
+
cut.add_argument("--rate", default=None, help="per-sample rate or '1/day'")
|
|
116
|
+
cut.add_argument("--threshold", type=float, default=None, help="absolute cut")
|
|
117
|
+
parser.add_argument("--alarm", default=None, help="alarm policy TOML")
|
|
118
|
+
parser.add_argument("--format", default="dict", help="registered emitter name")
|
|
119
|
+
_add_out_arg(parser)
|
|
120
|
+
parser.set_defaults(func=_commands._detect, **defaults)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _add_adapt_parser(
|
|
124
|
+
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
|
125
|
+
defaults: Mapping[str, Any],
|
|
126
|
+
) -> None:
|
|
127
|
+
parser = subparsers.add_parser("adapt", help="adapt raw model output to a forecast")
|
|
128
|
+
parser.add_argument(
|
|
129
|
+
"--model",
|
|
130
|
+
required=True,
|
|
131
|
+
choices=("array", "chronos2", "toto2", "timesfm3"),
|
|
132
|
+
help="which adapter to apply",
|
|
133
|
+
)
|
|
134
|
+
parser.add_argument("--input", required=True, help="raw model output JSON")
|
|
135
|
+
parser.add_argument("--step", default=None, help="sample spacing, e.g. '1s'")
|
|
136
|
+
parser.add_argument(
|
|
137
|
+
"--sentinel", type=_floats, default=None, help="sentinel values"
|
|
138
|
+
)
|
|
139
|
+
parser.add_argument(
|
|
140
|
+
"--decode-block-size",
|
|
141
|
+
dest="decode_block_size",
|
|
142
|
+
type=int,
|
|
143
|
+
default=None,
|
|
144
|
+
help="Toto-2 decode block size",
|
|
145
|
+
)
|
|
146
|
+
_add_out_arg(parser)
|
|
147
|
+
parser.set_defaults(func=_commands._adapt, **defaults)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _build_parser(
|
|
151
|
+
defaults: Mapping[str, Any] | None = None,
|
|
152
|
+
) -> argparse.ArgumentParser:
|
|
153
|
+
"""Build the argument parser, applying config-file defaults when given."""
|
|
154
|
+
config: dict[str, Any] = {} if defaults is None else dict(defaults)
|
|
155
|
+
config.pop("func", None)
|
|
156
|
+
config.pop("command", None)
|
|
157
|
+
parser = argparse.ArgumentParser(
|
|
158
|
+
prog="anoscore", description="anoscore command line"
|
|
159
|
+
)
|
|
160
|
+
parser.add_argument(
|
|
161
|
+
"--version",
|
|
162
|
+
action="version",
|
|
163
|
+
version=f"anoscore {__version__}",
|
|
164
|
+
)
|
|
165
|
+
parser.add_argument("--config", default=None, help="TOML file mirroring the flags")
|
|
166
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
167
|
+
_add_diagnose_parser(subparsers, config)
|
|
168
|
+
_add_calibrate_parser(subparsers, config)
|
|
169
|
+
_add_score_parser(subparsers, config)
|
|
170
|
+
_add_detect_parser(subparsers, config)
|
|
171
|
+
_add_adapt_parser(subparsers, config)
|
|
172
|
+
return parser
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _config_path(argv: Sequence[str]) -> str | None:
|
|
176
|
+
"""The ``--config`` value from raw arguments, before the parser runs."""
|
|
177
|
+
for index, item in enumerate(argv):
|
|
178
|
+
if item == "--config":
|
|
179
|
+
return argv[index + 1] if index + 1 < len(argv) else None
|
|
180
|
+
if item.startswith("--config="):
|
|
181
|
+
return item.partition("=")[2]
|
|
182
|
+
return None
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _main(argv: Sequence[str] | None = None) -> int:
|
|
186
|
+
"""Parse arguments, run the selected command and return the exit code."""
|
|
187
|
+
arguments = list(sys.argv[1:] if argv is None else argv)
|
|
188
|
+
path = _config_path(arguments)
|
|
189
|
+
defaults = {} if path is None else _load_toml(path)
|
|
190
|
+
parser = _build_parser(defaults)
|
|
191
|
+
args = parser.parse_args(arguments)
|
|
192
|
+
try:
|
|
193
|
+
return int(args.func(args))
|
|
194
|
+
except (TypeError, ValueError, KeyError) as exc:
|
|
195
|
+
sys.stderr.write(f"anoscore: {exc}\n")
|
|
196
|
+
return 2
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""The five CLI commands, each a thin caller of one public function.
|
|
2
|
+
|
|
3
|
+
Conformance items 104-108. ``diagnose`` calls :func:`anoscore.diagnostics.report`,
|
|
4
|
+
``calibrate`` calls :func:`anoscore.calibration.calibrate`, ``score`` calls
|
|
5
|
+
:func:`anoscore.score_all.score_all`, ``detect`` calls
|
|
6
|
+
:func:`anoscore.detect.detect` and emits through the registered emitter, and
|
|
7
|
+
``adapt`` calls one of the adapter entry points. No command adds behaviour the
|
|
8
|
+
library lacks; each returns a process exit code and leaves printing to ``_io``.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
from typing import TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
from anoscore.adapters import from_array, from_chronos2, from_timesfm3, from_toto2
|
|
15
|
+
from anoscore.calibration import Calibration, calibrate
|
|
16
|
+
from anoscore.cli._io import (
|
|
17
|
+
_alarm,
|
|
18
|
+
_events_ndjson,
|
|
19
|
+
_forecast,
|
|
20
|
+
_forecast_payload,
|
|
21
|
+
_limits,
|
|
22
|
+
_load_array,
|
|
23
|
+
_load_json,
|
|
24
|
+
_load_score,
|
|
25
|
+
_reference,
|
|
26
|
+
_score_payload,
|
|
27
|
+
_sentinel,
|
|
28
|
+
_timesfm_entries,
|
|
29
|
+
_write,
|
|
30
|
+
)
|
|
31
|
+
from anoscore.detect import detect
|
|
32
|
+
from anoscore.diagnostics import report
|
|
33
|
+
from anoscore.score_all import score_all
|
|
34
|
+
|
|
35
|
+
if TYPE_CHECKING:
|
|
36
|
+
import argparse
|
|
37
|
+
|
|
38
|
+
from anoscore.core import Score
|
|
39
|
+
from anoscore.score_all import ScoreSet
|
|
40
|
+
|
|
41
|
+
__all__ = []
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _diagnose(args: argparse.Namespace) -> int:
|
|
45
|
+
"""Report grid quality for a forecast, optionally against observations."""
|
|
46
|
+
fc = _forecast(_load_json(args.fc))
|
|
47
|
+
y = None if args.y is None else _load_array(args.y)
|
|
48
|
+
_write(f"{report(fc, y)!r}\n", args.out)
|
|
49
|
+
return 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _calibrate(args: argparse.Namespace) -> int:
|
|
53
|
+
"""Fit the deployment artifact and save it to ``--out``."""
|
|
54
|
+
fc = _forecast(_load_json(args.fc))
|
|
55
|
+
reference = (
|
|
56
|
+
None if args.reference is None else _reference(_load_json(args.reference))
|
|
57
|
+
)
|
|
58
|
+
limits = None if args.limits is None else _limits(_load_json(args.limits))
|
|
59
|
+
cal = calibrate(
|
|
60
|
+
fc,
|
|
61
|
+
_load_array(args.y),
|
|
62
|
+
tail=args.tail,
|
|
63
|
+
reference=reference,
|
|
64
|
+
limits=limits,
|
|
65
|
+
sentinel=_sentinel(args.sentinel),
|
|
66
|
+
)
|
|
67
|
+
cal.save(args.out)
|
|
68
|
+
return 0
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _select(
|
|
72
|
+
result: ScoreSet, names: list[str] | None
|
|
73
|
+
) -> tuple[dict[str, Score], dict[str, str]]:
|
|
74
|
+
"""Pick the requested scorers from a ``ScoreSet``, else keep every result."""
|
|
75
|
+
if names is None:
|
|
76
|
+
return dict(result.data), dict(result.skipped)
|
|
77
|
+
selected = {name: result.data[name] for name in names if name in result.data}
|
|
78
|
+
missing = [
|
|
79
|
+
name for name in names if name not in result.data and name not in result.skipped
|
|
80
|
+
]
|
|
81
|
+
if missing:
|
|
82
|
+
msg = f"unknown scorers {missing}."
|
|
83
|
+
raise ValueError(msg)
|
|
84
|
+
return selected, {
|
|
85
|
+
name: result.skipped[name] for name in names if name in result.skipped
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _score(args: argparse.Namespace) -> int:
|
|
90
|
+
"""Map every applicable scorer over a forecast and write the result."""
|
|
91
|
+
fc = _forecast(_load_json(args.fc))
|
|
92
|
+
y = None if args.y is None else _load_array(args.y)
|
|
93
|
+
cal = None if args.cal is None else Calibration.load(args.cal)
|
|
94
|
+
limits = None if args.limits is None else _limits(_load_json(args.limits))
|
|
95
|
+
reference = (
|
|
96
|
+
None if args.reference is None else _reference(_load_json(args.reference))
|
|
97
|
+
)
|
|
98
|
+
result = score_all(fc, y=y, cal=cal, limits=limits, reference=reference)
|
|
99
|
+
names = (
|
|
100
|
+
None if args.scorers is None else [n.strip() for n in args.scorers.split(",")]
|
|
101
|
+
)
|
|
102
|
+
data, skipped = _select(result, None if names is None else [n for n in names if n])
|
|
103
|
+
payload = {
|
|
104
|
+
"scores": {name: _score_payload(score) for name, score in data.items()},
|
|
105
|
+
"skipped": skipped,
|
|
106
|
+
}
|
|
107
|
+
_write(f"{json.dumps(payload, indent=2, default=str)}\n", args.out)
|
|
108
|
+
return 0
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _detect(args: argparse.Namespace) -> int:
|
|
112
|
+
"""Threshold a score into events and emit them in the requested format."""
|
|
113
|
+
score = _load_score(args.s, args.scorer)
|
|
114
|
+
events = detect(
|
|
115
|
+
score,
|
|
116
|
+
rate=args.rate,
|
|
117
|
+
threshold=args.threshold,
|
|
118
|
+
alarm=_alarm(args.alarm),
|
|
119
|
+
)
|
|
120
|
+
_write(_events_ndjson(events, args.format), args.out)
|
|
121
|
+
return 0
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _adapt(args: argparse.Namespace) -> int:
|
|
125
|
+
"""Convert a model's raw output to a canonical forecast file."""
|
|
126
|
+
payload = _load_json(args.input)
|
|
127
|
+
sentinel = _sentinel(args.sentinel)
|
|
128
|
+
if args.model == "array":
|
|
129
|
+
fc = from_array(
|
|
130
|
+
levels=payload["levels"],
|
|
131
|
+
values=payload["values"],
|
|
132
|
+
step=args.step,
|
|
133
|
+
sentinel=sentinel,
|
|
134
|
+
variate_names=payload.get("variate_names"),
|
|
135
|
+
unit=payload.get("unit"),
|
|
136
|
+
)
|
|
137
|
+
elif args.model == "chronos2":
|
|
138
|
+
fc = from_chronos2(
|
|
139
|
+
payload["quantiles"],
|
|
140
|
+
levels=payload["levels"],
|
|
141
|
+
step=args.step,
|
|
142
|
+
)
|
|
143
|
+
elif args.model == "toto2":
|
|
144
|
+
fc = from_toto2(
|
|
145
|
+
payload["quantiles"],
|
|
146
|
+
mask=payload.get("mask"),
|
|
147
|
+
step=args.step,
|
|
148
|
+
decode_block_size=args.decode_block_size,
|
|
149
|
+
)
|
|
150
|
+
else:
|
|
151
|
+
fc = from_timesfm3(_timesfm_entries(payload["out"]), step=args.step)
|
|
152
|
+
_write(f"{json.dumps(_forecast_payload(fc), indent=2, default=str)}\n", args.out)
|
|
153
|
+
return 0
|