stplot 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.
- stplot/__init__.py +21 -0
- stplot/config/__init__.py +33 -0
- stplot/config/loader.py +106 -0
- stplot/config/models.py +328 -0
- stplot/config/presets/__init__.py +1 -0
- stplot/config/presets/default.yaml +208 -0
- stplot/config/presets/horizons_one_plan.yaml +59 -0
- stplot/io/__init__.py +6 -0
- stplot/io/spatial.py +72 -0
- stplot/io/tabular.py +154 -0
- stplot/plots/__init__.py +122 -0
- stplot/plots/barbell.py +400 -0
- stplot/plots/batch.py +135 -0
- stplot/plots/domains.py +1143 -0
- stplot/plots/executive.py +474 -0
- stplot/plots/export.py +74 -0
- stplot/plots/infographics.py +1118 -0
- stplot/plots/lollipop.py +285 -0
- stplot/plots/maps.py +738 -0
- stplot/plots/matrix.py +190 -0
- stplot/plots/one_plan.py +642 -0
- stplot/plots/paired.py +437 -0
- stplot/plots/scorecards.py +1940 -0
- stplot/plots/state.py +373 -0
- stplot/plots/state_trends.py +1413 -0
- stplot/plots/tables.py +107 -0
- stplot/plots/timeseries.py +607 -0
- stplot/plots/trends.py +305 -0
- stplot/reporting/__init__.py +24 -0
- stplot/reporting/captions.py +177 -0
- stplot/reporting/html_companion.py +339 -0
- stplot/reporting/templates.py +485 -0
- stplot/reporting/word.py +369 -0
- stplot/theme/__init__.py +25 -0
- stplot/theme/folium.py +62 -0
- stplot/theme/mpl.py +181 -0
- stplot/theme/plotly.py +130 -0
- stplot/transform/__init__.py +60 -0
- stplot/transform/categories.py +51 -0
- stplot/transform/columns.py +103 -0
- stplot/transform/filters.py +385 -0
- stplot/transform/merge.py +173 -0
- stplot/transform/pairing.py +154 -0
- stplot/transform/qa.py +297 -0
- stplot/transform/tables.py +573 -0
- stplot/transform/zones.py +354 -0
- stplot-0.1.0.dist-info/METADATA +235 -0
- stplot-0.1.0.dist-info/RECORD +49 -0
- stplot-0.1.0.dist-info/WHEEL +4 -0
stplot/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""stplot: Post-processing and templated plotting helpers for state and trends results."""
|
|
2
|
+
|
|
3
|
+
from stplot import config, io, plots, reporting, theme, transform
|
|
4
|
+
from stplot.config import Config, list_presets, load_config
|
|
5
|
+
from stplot.plots import save_figure
|
|
6
|
+
|
|
7
|
+
__version__ = "0.1.0"
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Config",
|
|
11
|
+
"__version__",
|
|
12
|
+
"config",
|
|
13
|
+
"io",
|
|
14
|
+
"list_presets",
|
|
15
|
+
"load_config",
|
|
16
|
+
"plots",
|
|
17
|
+
"reporting",
|
|
18
|
+
"save_figure",
|
|
19
|
+
"theme",
|
|
20
|
+
"transform",
|
|
21
|
+
]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Configuration models, preset loading, and parameter resolution helpers."""
|
|
2
|
+
|
|
3
|
+
from stplot.config.loader import ensure_config, list_presets, load_config
|
|
4
|
+
from stplot.config.models import (
|
|
5
|
+
Config,
|
|
6
|
+
ExportConfig,
|
|
7
|
+
ThemeConfig,
|
|
8
|
+
is_lower_better,
|
|
9
|
+
merge_config_dicts,
|
|
10
|
+
resolve_category_order,
|
|
11
|
+
resolve_column,
|
|
12
|
+
resolve_indicator_direction,
|
|
13
|
+
resolve_label,
|
|
14
|
+
resolve_palette,
|
|
15
|
+
resolve_spatial_source,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"Config",
|
|
20
|
+
"ExportConfig",
|
|
21
|
+
"ThemeConfig",
|
|
22
|
+
"ensure_config",
|
|
23
|
+
"is_lower_better",
|
|
24
|
+
"list_presets",
|
|
25
|
+
"load_config",
|
|
26
|
+
"merge_config_dicts",
|
|
27
|
+
"resolve_category_order",
|
|
28
|
+
"resolve_column",
|
|
29
|
+
"resolve_indicator_direction",
|
|
30
|
+
"resolve_label",
|
|
31
|
+
"resolve_palette",
|
|
32
|
+
"resolve_spatial_source",
|
|
33
|
+
]
|
stplot/config/loader.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Configuration loaders for YAML/TOML files and built-in presets."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib import resources
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import yaml
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
import tomllib
|
|
13
|
+
except ModuleNotFoundError: # pragma: no cover - Python 3.10 fallback
|
|
14
|
+
import tomli as tomllib # type: ignore[no-redef]
|
|
15
|
+
|
|
16
|
+
from stplot.config.models import Config, merge_config_dicts
|
|
17
|
+
|
|
18
|
+
_PRESET_ALIASES: dict[str, str] = {
|
|
19
|
+
"default": "default.yaml",
|
|
20
|
+
"npsfm_lawa": "default.yaml",
|
|
21
|
+
"horizons_one_plan": "horizons_one_plan.yaml",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def list_presets() -> list[str]:
|
|
26
|
+
"""Return sorted names of built-in configuration presets."""
|
|
27
|
+
return sorted(_PRESET_ALIASES.keys())
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _read_yaml_text(text: str) -> dict[str, Any]:
|
|
31
|
+
parsed = yaml.safe_load(text)
|
|
32
|
+
if parsed is None:
|
|
33
|
+
return {}
|
|
34
|
+
if not isinstance(parsed, dict):
|
|
35
|
+
raise ValueError("Configuration file must contain a top-level mapping.")
|
|
36
|
+
return parsed
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _load_preset_dict(preset_name: str) -> dict[str, Any]:
|
|
40
|
+
filename = _PRESET_ALIASES[preset_name]
|
|
41
|
+
preset_file = resources.files("stplot.config.presets").joinpath(filename)
|
|
42
|
+
return _read_yaml_text(preset_file.read_text(encoding="utf-8"))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _load_file_dict(path: Path) -> dict[str, Any]:
|
|
46
|
+
if not path.exists():
|
|
47
|
+
raise FileNotFoundError(f"Configuration file not found: {path}")
|
|
48
|
+
suffix = path.suffix.lower()
|
|
49
|
+
if suffix in {".yaml", ".yml"}:
|
|
50
|
+
return _read_yaml_text(path.read_text(encoding="utf-8"))
|
|
51
|
+
if suffix == ".toml":
|
|
52
|
+
with path.open("rb") as f:
|
|
53
|
+
parsed = tomllib.load(f)
|
|
54
|
+
if not isinstance(parsed, dict):
|
|
55
|
+
raise ValueError("TOML configuration file must contain a top-level table.")
|
|
56
|
+
return parsed
|
|
57
|
+
raise ValueError(
|
|
58
|
+
f"Unsupported configuration file extension '{suffix}' for {path}. "
|
|
59
|
+
"Supported formats: .yaml, .yml, .toml."
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def load_config(
|
|
64
|
+
source: str | Path | Config | None = None,
|
|
65
|
+
*,
|
|
66
|
+
base: str = "default",
|
|
67
|
+
) -> Config:
|
|
68
|
+
"""Load an stplot Config from a preset name, YAML/TOML file path, or existing Config.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
source: One of:
|
|
72
|
+
- ``None``: Load the default preset (``"npsfm_lawa"``).
|
|
73
|
+
- ``Config``: Returned as-is.
|
|
74
|
+
- Built-in preset name (see :func:`list_presets`).
|
|
75
|
+
- Path (``str`` or ``Path``) to a ``.yaml``, ``.yml``, or ``.toml`` file.
|
|
76
|
+
base: Built-in preset name used as the fallback base when merging overrides.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
Resolved :class:`Config` instance.
|
|
80
|
+
"""
|
|
81
|
+
if isinstance(source, Config):
|
|
82
|
+
return source
|
|
83
|
+
|
|
84
|
+
if base not in _PRESET_ALIASES:
|
|
85
|
+
raise ValueError(
|
|
86
|
+
f"Unknown base preset '{base}'. Available presets: {list_presets()}."
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
base_dict = _load_preset_dict(base)
|
|
90
|
+
|
|
91
|
+
if source is None:
|
|
92
|
+
return Config.from_dict(base_dict)
|
|
93
|
+
|
|
94
|
+
if isinstance(source, str) and source in _PRESET_ALIASES:
|
|
95
|
+
if source == base:
|
|
96
|
+
return Config.from_dict(base_dict)
|
|
97
|
+
preset_dict = _load_preset_dict(source)
|
|
98
|
+
return Config.from_dict(merge_config_dicts(base_dict, preset_dict))
|
|
99
|
+
|
|
100
|
+
file_dict = _load_file_dict(Path(source))
|
|
101
|
+
return Config.from_dict(merge_config_dicts(base_dict, file_dict))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def ensure_config(config: str | Path | Config | None = None) -> Config:
|
|
105
|
+
"""Normalize an optional config argument (None, path, preset, or Config) into a Config."""
|
|
106
|
+
return load_config(config)
|
stplot/config/models.py
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"""Configuration data models and resolution helpers for stplot."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class ThemeConfig:
|
|
11
|
+
"""Shared visual theme tokens applied across plotting backends."""
|
|
12
|
+
|
|
13
|
+
font_family: str = "DejaVu Sans"
|
|
14
|
+
title_size: float = 14.0
|
|
15
|
+
subtitle_size: float = 11.0
|
|
16
|
+
label_size: float = 11.0
|
|
17
|
+
tick_size: float = 10.0
|
|
18
|
+
legend_size: float = 10.0
|
|
19
|
+
caption_size: float = 9.0
|
|
20
|
+
figure_dpi: int = 300
|
|
21
|
+
default_figsize: tuple[float, float] = (10.0, 6.0)
|
|
22
|
+
background_color: str = "#ffffff"
|
|
23
|
+
axes_facecolor: str = "#ffffff"
|
|
24
|
+
grid_color: str = "#e5e5e5"
|
|
25
|
+
grid_width: float = 0.8
|
|
26
|
+
text_color: str = "#222222"
|
|
27
|
+
caption_color: str = "#555555"
|
|
28
|
+
spine_color: str = "#cccccc"
|
|
29
|
+
accent_color: str = "#009ec2"
|
|
30
|
+
title_weight: str = "bold"
|
|
31
|
+
show_grid: bool = True
|
|
32
|
+
despine: bool = True
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def from_dict(cls, data: dict[str, Any]) -> ThemeConfig:
|
|
36
|
+
"""Build a ThemeConfig from a plain dictionary."""
|
|
37
|
+
kwargs = dict(data)
|
|
38
|
+
if "default_figsize" in kwargs and kwargs["default_figsize"] is not None:
|
|
39
|
+
figsize = kwargs["default_figsize"]
|
|
40
|
+
kwargs["default_figsize"] = (float(figsize[0]), float(figsize[1]))
|
|
41
|
+
return cls(**kwargs)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class ExportConfig:
|
|
46
|
+
"""Default figure export settings."""
|
|
47
|
+
|
|
48
|
+
dpi: int = 300
|
|
49
|
+
bbox_inches: str = "tight"
|
|
50
|
+
transparent: bool = False
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def from_dict(cls, data: dict[str, Any]) -> ExportConfig:
|
|
54
|
+
"""Build an ExportConfig from a plain dictionary."""
|
|
55
|
+
return cls(**data)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class Config:
|
|
60
|
+
"""Top-level stplot configuration holding column mappings, palettes, and theme settings."""
|
|
61
|
+
|
|
62
|
+
name: str = "npsfm_lawa"
|
|
63
|
+
description: str = ""
|
|
64
|
+
columns: dict[str, str] = field(default_factory=dict)
|
|
65
|
+
category_orders: dict[str, list[str]] = field(default_factory=dict)
|
|
66
|
+
palettes: dict[str, dict[str, str]] = field(default_factory=dict)
|
|
67
|
+
spatial_sources: dict[str, str] = field(default_factory=dict)
|
|
68
|
+
labels: dict[str, str] = field(default_factory=dict)
|
|
69
|
+
indicator_directions: dict[str, str] = field(default_factory=dict)
|
|
70
|
+
figure_sizes: dict[str, tuple[float, float]] = field(default_factory=dict)
|
|
71
|
+
theme: ThemeConfig = field(default_factory=ThemeConfig)
|
|
72
|
+
export: ExportConfig = field(default_factory=ExportConfig)
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def from_dict(cls, data: dict[str, Any]) -> Config:
|
|
76
|
+
"""Construct a Config instance from a parsed YAML/TOML dictionary."""
|
|
77
|
+
raw_sizes = data.get("figure_sizes", {})
|
|
78
|
+
parsed_sizes = {k: (float(v[0]), float(v[1])) for k, v in raw_sizes.items()}
|
|
79
|
+
return cls(
|
|
80
|
+
name=str(data.get("name", "custom")),
|
|
81
|
+
description=str(data.get("description", "")),
|
|
82
|
+
columns=dict(data.get("columns", {})),
|
|
83
|
+
category_orders={
|
|
84
|
+
k: list(v) for k, v in data.get("category_orders", {}).items()
|
|
85
|
+
},
|
|
86
|
+
palettes={k: dict(v) for k, v in data.get("palettes", {}).items()},
|
|
87
|
+
spatial_sources=dict(data.get("spatial_sources", {})),
|
|
88
|
+
labels=dict(data.get("labels", {})),
|
|
89
|
+
indicator_directions=dict(data.get("indicator_directions", {})),
|
|
90
|
+
figure_sizes=parsed_sizes,
|
|
91
|
+
theme=ThemeConfig.from_dict(data.get("theme", {})),
|
|
92
|
+
export=ExportConfig.from_dict(data.get("export", {})),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def merge_config_dicts(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
|
|
97
|
+
"""Merge an override config dict onto a base config dict.
|
|
98
|
+
|
|
99
|
+
- ``columns``, ``spatial_sources``, ``labels``, ``indicator_directions``,
|
|
100
|
+
``figure_sizes``, ``theme``, and ``export`` merge key-by-key so unspecified
|
|
101
|
+
keys are inherited.
|
|
102
|
+
- ``palettes`` and ``category_orders`` merge at the named-palette/order level, replacing
|
|
103
|
+
an individual palette or category order completely when specified in ``override``.
|
|
104
|
+
"""
|
|
105
|
+
merged: dict[str, Any] = {
|
|
106
|
+
"name": override.get("name", base.get("name", "custom")),
|
|
107
|
+
"description": override.get("description", base.get("description", "")),
|
|
108
|
+
"columns": {**base.get("columns", {}), **override.get("columns", {})},
|
|
109
|
+
"category_orders": {
|
|
110
|
+
**{k: list(v) for k, v in base.get("category_orders", {}).items()},
|
|
111
|
+
**{k: list(v) for k, v in override.get("category_orders", {}).items()},
|
|
112
|
+
},
|
|
113
|
+
"palettes": {
|
|
114
|
+
**{k: dict(v) for k, v in base.get("palettes", {}).items()},
|
|
115
|
+
**{k: dict(v) for k, v in override.get("palettes", {}).items()},
|
|
116
|
+
},
|
|
117
|
+
"spatial_sources": {
|
|
118
|
+
**base.get("spatial_sources", {}),
|
|
119
|
+
**override.get("spatial_sources", {}),
|
|
120
|
+
},
|
|
121
|
+
"labels": {**base.get("labels", {}), **override.get("labels", {})},
|
|
122
|
+
"indicator_directions": {
|
|
123
|
+
**base.get("indicator_directions", {}),
|
|
124
|
+
**override.get("indicator_directions", {}),
|
|
125
|
+
},
|
|
126
|
+
"figure_sizes": {**base.get("figure_sizes", {}), **override.get("figure_sizes", {})},
|
|
127
|
+
"theme": {**base.get("theme", {}), **override.get("theme", {})},
|
|
128
|
+
"export": {**base.get("export", {}), **override.get("export", {})},
|
|
129
|
+
}
|
|
130
|
+
return merged
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def resolve_column(
|
|
134
|
+
override: str | None,
|
|
135
|
+
config: Config,
|
|
136
|
+
key: str,
|
|
137
|
+
) -> str:
|
|
138
|
+
"""Resolve a column name using precedence: explicit override > config.columns[key].
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
override: Explicit column name passed to a function, or None.
|
|
142
|
+
config: Loaded stplot Config object.
|
|
143
|
+
key: Logical column key in ``config.columns`` (e.g., ``"site_id"``, ``"state_band"``).
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
Resolved column name string.
|
|
147
|
+
|
|
148
|
+
Raises:
|
|
149
|
+
KeyError: If ``override`` is None and ``key`` is not defined in ``config.columns``.
|
|
150
|
+
"""
|
|
151
|
+
if override is not None:
|
|
152
|
+
return override
|
|
153
|
+
if key in config.columns:
|
|
154
|
+
return config.columns[key]
|
|
155
|
+
available = ", ".join(sorted(config.columns.keys()))
|
|
156
|
+
raise KeyError(
|
|
157
|
+
f"Column key '{key}' not found in config '{config.name}'. "
|
|
158
|
+
f"Available column keys: [{available}]. "
|
|
159
|
+
f"Pass an explicit column name or add '{key}' to config.columns."
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def resolve_palette(
|
|
164
|
+
override: dict[str, str] | None,
|
|
165
|
+
config: Config,
|
|
166
|
+
key: str,
|
|
167
|
+
) -> dict[str, str]:
|
|
168
|
+
"""Resolve a semantic color palette: explicit override > config.palettes[key].
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
override: Explicit category-to-hex color mapping, or None.
|
|
172
|
+
config: Loaded stplot Config object.
|
|
173
|
+
key: Palette key in ``config.palettes`` (e.g., ``"state_bands"``).
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
Dictionary mapping category names to color strings.
|
|
177
|
+
|
|
178
|
+
Raises:
|
|
179
|
+
KeyError: If ``override`` is None and ``key`` is not defined in ``config.palettes``.
|
|
180
|
+
"""
|
|
181
|
+
if override is not None:
|
|
182
|
+
return dict(override)
|
|
183
|
+
if key in config.palettes:
|
|
184
|
+
return dict(config.palettes[key])
|
|
185
|
+
available = ", ".join(sorted(config.palettes.keys()))
|
|
186
|
+
raise KeyError(
|
|
187
|
+
f"Palette '{key}' not found in config '{config.name}'. "
|
|
188
|
+
f"Available palettes: [{available}]."
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def resolve_category_order(
|
|
193
|
+
override: list[str] | None,
|
|
194
|
+
config: Config,
|
|
195
|
+
key: str,
|
|
196
|
+
) -> list[str]:
|
|
197
|
+
"""Resolve a canonical category ordering: explicit override > config.category_orders[key].
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
override: Explicit ordered list of category labels, or None.
|
|
201
|
+
config: Loaded stplot Config object.
|
|
202
|
+
key: Order key in ``config.category_orders`` (e.g., ``"state_bands"``).
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
Ordered list of category names.
|
|
206
|
+
|
|
207
|
+
Raises:
|
|
208
|
+
KeyError: If ``override`` is None and ``key`` is not defined in ``config.category_orders``.
|
|
209
|
+
"""
|
|
210
|
+
if override is not None:
|
|
211
|
+
return list(override)
|
|
212
|
+
if key in config.category_orders:
|
|
213
|
+
return list(config.category_orders[key])
|
|
214
|
+
available = ", ".join(sorted(config.category_orders.keys()))
|
|
215
|
+
raise KeyError(
|
|
216
|
+
f"Category order '{key}' not found in config '{config.name}'. "
|
|
217
|
+
f"Available category orders: [{available}]."
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def resolve_spatial_source(
|
|
222
|
+
override: str | None,
|
|
223
|
+
config: Config,
|
|
224
|
+
key: str,
|
|
225
|
+
) -> str:
|
|
226
|
+
"""Resolve a spatial layer path or REST URL: explicit override > config.spatial_sources[key].
|
|
227
|
+
|
|
228
|
+
Args:
|
|
229
|
+
override: Explicit file path or URL override, or None.
|
|
230
|
+
config: Loaded stplot Config object.
|
|
231
|
+
key: Spatial source key in ``config.spatial_sources`` (e.g., ``"horizons_fmu"``).
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
Resolved path or URL string.
|
|
235
|
+
|
|
236
|
+
Raises:
|
|
237
|
+
KeyError: If ``override`` is None and ``key`` is not defined in ``config.spatial_sources``.
|
|
238
|
+
"""
|
|
239
|
+
if override is not None:
|
|
240
|
+
return override
|
|
241
|
+
if key in config.spatial_sources:
|
|
242
|
+
return config.spatial_sources[key]
|
|
243
|
+
available = ", ".join(sorted(config.spatial_sources.keys()))
|
|
244
|
+
raise KeyError(
|
|
245
|
+
f"Spatial source '{key}' not found in config '{config.name}'. "
|
|
246
|
+
f"Available spatial sources: [{available}]."
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def resolve_label(
|
|
251
|
+
value: str,
|
|
252
|
+
config: Config,
|
|
253
|
+
overrides: dict[str, str] | None = None,
|
|
254
|
+
) -> str:
|
|
255
|
+
"""Resolve a human-readable display label (including macrons) for a raw code/name."""
|
|
256
|
+
if overrides and value in overrides:
|
|
257
|
+
return overrides[value]
|
|
258
|
+
return config.labels.get(value, value)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
_VALID_DIRECTIONS = {"higher_is_better", "lower_is_better"}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def resolve_indicator_direction(
|
|
265
|
+
indicator: str | None,
|
|
266
|
+
config: Config,
|
|
267
|
+
*,
|
|
268
|
+
override: str | None = None,
|
|
269
|
+
overrides: dict[str, str] | None = None,
|
|
270
|
+
default: str = "lower_is_better",
|
|
271
|
+
) -> str:
|
|
272
|
+
"""Resolve whether higher or lower values represent improvement for an indicator.
|
|
273
|
+
|
|
274
|
+
Precedence:
|
|
275
|
+
1. Explicit ``override`` (``"higher_is_better"`` or ``"lower_is_better"``)
|
|
276
|
+
2. Explicit ``overrides`` mapping (per-call dict)
|
|
277
|
+
3. ``config.indicator_directions`` (exact or case-insensitive lookup)
|
|
278
|
+
4. ``default`` (``"lower_is_better"``)
|
|
279
|
+
|
|
280
|
+
Raises:
|
|
281
|
+
ValueError: If the resolved direction is not ``"higher_is_better"`` or
|
|
282
|
+
``"lower_is_better"``.
|
|
283
|
+
"""
|
|
284
|
+
|
|
285
|
+
def _validate(val: str) -> str:
|
|
286
|
+
norm = str(val).strip().lower()
|
|
287
|
+
if norm not in _VALID_DIRECTIONS:
|
|
288
|
+
raise ValueError(
|
|
289
|
+
f"Invalid indicator direction '{val}'. "
|
|
290
|
+
f"Expected one of {sorted(_VALID_DIRECTIONS)}."
|
|
291
|
+
)
|
|
292
|
+
return norm
|
|
293
|
+
|
|
294
|
+
if override is not None:
|
|
295
|
+
return _validate(override)
|
|
296
|
+
|
|
297
|
+
if indicator is not None:
|
|
298
|
+
key = str(indicator).strip()
|
|
299
|
+
key_lower = key.lower()
|
|
300
|
+
for mapping in (overrides or {}, config.indicator_directions):
|
|
301
|
+
if key in mapping:
|
|
302
|
+
return _validate(mapping[key])
|
|
303
|
+
for m_key, m_val in mapping.items():
|
|
304
|
+
if str(m_key).strip().lower() == key_lower:
|
|
305
|
+
return _validate(m_val)
|
|
306
|
+
|
|
307
|
+
return _validate(default)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def is_lower_better(
|
|
311
|
+
indicator: str | None,
|
|
312
|
+
config: Config,
|
|
313
|
+
*,
|
|
314
|
+
lower_is_better: bool | None = None,
|
|
315
|
+
overrides: dict[str, str] | None = None,
|
|
316
|
+
default: bool = True,
|
|
317
|
+
) -> bool:
|
|
318
|
+
"""Return True if lower numeric values are better for ``indicator``."""
|
|
319
|
+
if lower_is_better is not None:
|
|
320
|
+
return bool(lower_is_better)
|
|
321
|
+
default_dir = "lower_is_better" if default else "higher_is_better"
|
|
322
|
+
resolved = resolve_indicator_direction(
|
|
323
|
+
indicator,
|
|
324
|
+
config,
|
|
325
|
+
overrides=overrides,
|
|
326
|
+
default=default_dir,
|
|
327
|
+
)
|
|
328
|
+
return resolved == "lower_is_better"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Packaged YAML configuration presets for stplot."""
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# Default stplot configuration: NZ NPS-FM / LAWA preset
|
|
2
|
+
# Tailored to LWP / TimeTrends NPS-FM State and Trends output schemas.
|
|
3
|
+
# Override any field via a custom YAML/TOML file or explicit function parameters.
|
|
4
|
+
|
|
5
|
+
name: "npsfm_lawa"
|
|
6
|
+
description: "NZ NPS-FM / LAWA state bands (NOF_Grade) and IPCC/LWP trend likelihoods."
|
|
7
|
+
|
|
8
|
+
columns:
|
|
9
|
+
site_id: "sID"
|
|
10
|
+
indicator: "npID"
|
|
11
|
+
attribute: "Attr"
|
|
12
|
+
state_band: "NOF_Grade"
|
|
13
|
+
state_value: "Median"
|
|
14
|
+
period: "yearz"
|
|
15
|
+
trend_period: "Trend_Period"
|
|
16
|
+
trend_direction: "TrendDirection"
|
|
17
|
+
trend_confidence: "DirectionConf"
|
|
18
|
+
trend_category: "Improving_Confidence"
|
|
19
|
+
sen_slope: "AnnualSenSlope"
|
|
20
|
+
pct_annual_change: "Percent.annual.change"
|
|
21
|
+
p_value: "p"
|
|
22
|
+
filter_ok: "FilterOK"
|
|
23
|
+
zone: "Freshwater.Management.Unit.FMU"
|
|
24
|
+
fmu: "Freshwater.Management.Unit.FMU"
|
|
25
|
+
subzone: "Water.management.subzone.One.Plan"
|
|
26
|
+
catchment: "CatName"
|
|
27
|
+
site_type: "SiteType"
|
|
28
|
+
site_status: "Status"
|
|
29
|
+
x: "NZTM.X"
|
|
30
|
+
y: "NZTM.Y"
|
|
31
|
+
crs: "EPSG:2193"
|
|
32
|
+
|
|
33
|
+
category_orders:
|
|
34
|
+
state_bands:
|
|
35
|
+
- "A"
|
|
36
|
+
- "B"
|
|
37
|
+
- "C"
|
|
38
|
+
- "D"
|
|
39
|
+
- "E"
|
|
40
|
+
- "Not assessed"
|
|
41
|
+
trend_confidence:
|
|
42
|
+
- "Virtually certain"
|
|
43
|
+
- "Extremely likely"
|
|
44
|
+
- "Very likely"
|
|
45
|
+
- "Likely"
|
|
46
|
+
- "As likely as not"
|
|
47
|
+
- "Unlikely"
|
|
48
|
+
- "Very unlikely"
|
|
49
|
+
- "Extremely unlikely"
|
|
50
|
+
- "Exceptionally unlikely"
|
|
51
|
+
- "Not Analysed"
|
|
52
|
+
trend_categories:
|
|
53
|
+
- "Virtually certain improving"
|
|
54
|
+
- "Extremely likely improving"
|
|
55
|
+
- "Very likely improving"
|
|
56
|
+
- "Likely improving"
|
|
57
|
+
- "As likely as not improving"
|
|
58
|
+
- "Unlikely improving"
|
|
59
|
+
- "Very unlikely improving"
|
|
60
|
+
- "Extremely unlikely improving"
|
|
61
|
+
- "Exceptionally unlikely improving"
|
|
62
|
+
- "Not Analysed improving"
|
|
63
|
+
trend_directions:
|
|
64
|
+
- "Improving"
|
|
65
|
+
- "Indeterminate"
|
|
66
|
+
- "Degrading"
|
|
67
|
+
- "Not Analysed"
|
|
68
|
+
|
|
69
|
+
palettes:
|
|
70
|
+
state_bands:
|
|
71
|
+
A: "#29b6f6" # LAWA / NPS-FM Band A (Sky Blue)
|
|
72
|
+
B: "#9ccc65" # Band B (Light Green)
|
|
73
|
+
C: "#e6e600" # Band C (Yellow)
|
|
74
|
+
D: "#ffa726" # Band D (Orange)
|
|
75
|
+
E: "#ff6666" # Band E (Coral Red)
|
|
76
|
+
Not assessed: "#bdbdbd" # Neutral grey
|
|
77
|
+
trend_confidence:
|
|
78
|
+
Virtually certain: "#08306b"
|
|
79
|
+
Extremely likely: "#08519c"
|
|
80
|
+
Very likely: "#2171b5"
|
|
81
|
+
Likely: "#6baed6"
|
|
82
|
+
As likely as not: "#bdbdbd"
|
|
83
|
+
Unlikely: "#fcae91"
|
|
84
|
+
Very unlikely: "#fb6a4a"
|
|
85
|
+
Extremely unlikely: "#cb181d"
|
|
86
|
+
Exceptionally unlikely: "#67000d"
|
|
87
|
+
Not Analysed: "#e0e0e0"
|
|
88
|
+
trend_categories:
|
|
89
|
+
Virtually certain improving: "#08306b"
|
|
90
|
+
Extremely likely improving: "#08519c"
|
|
91
|
+
Very likely improving: "#2171b5"
|
|
92
|
+
Likely improving: "#6baed6"
|
|
93
|
+
As likely as not improving: "#bdbdbd"
|
|
94
|
+
Unlikely improving: "#fcae91"
|
|
95
|
+
Very unlikely improving: "#fb6a4a"
|
|
96
|
+
Extremely unlikely improving: "#cb181d"
|
|
97
|
+
Exceptionally unlikely improving: "#67000d"
|
|
98
|
+
Not Analysed improving: "#e0e0e0"
|
|
99
|
+
trend_directions:
|
|
100
|
+
Improving: "#29b6f6"
|
|
101
|
+
Indeterminate: "#e6e600"
|
|
102
|
+
Degrading: "#ff6666"
|
|
103
|
+
Increasing: "#29b6f6"
|
|
104
|
+
Decreasing: "#ff6666"
|
|
105
|
+
Not Analysed: "#e0e0e0"
|
|
106
|
+
recreational_swim:
|
|
107
|
+
Green: "#3f915b"
|
|
108
|
+
Amber: "#f9a81d"
|
|
109
|
+
Red: "#ba461e"
|
|
110
|
+
Suitable: "#3f915b"
|
|
111
|
+
Caution: "#f9a81d"
|
|
112
|
+
Unsuitable: "#ba461e"
|
|
113
|
+
|
|
114
|
+
spatial_sources:
|
|
115
|
+
nz_regional_councils: "https://services.arcgis.com/XTtANUDT8Va4DLwI/arcgis/rest/services/nz_regional_councils_2021/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=geojson"
|
|
116
|
+
horizons_fmu: "https://services1.arcgis.com/VuN78wcRdq1Oj69W/arcgis/rest/services/FMU_20210122/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=geojson"
|
|
117
|
+
horizons_wms: "https://maps.horizons.govt.nz/arcgis/rest/services/LocalMapsPublic/Public_OnePlan/MapServer/3/query?where=1=1&outFields=*&returnGeometry=true&outSR=4326&f=geojson"
|
|
118
|
+
|
|
119
|
+
theme:
|
|
120
|
+
font_family: "DejaVu Sans"
|
|
121
|
+
title_size: 14
|
|
122
|
+
subtitle_size: 11
|
|
123
|
+
label_size: 11
|
|
124
|
+
tick_size: 10
|
|
125
|
+
legend_size: 10
|
|
126
|
+
caption_size: 9
|
|
127
|
+
figure_dpi: 300
|
|
128
|
+
default_figsize: [10.0, 6.0]
|
|
129
|
+
background_color: "#ffffff"
|
|
130
|
+
axes_facecolor: "#f3f5f7"
|
|
131
|
+
grid_color: "#ffffff"
|
|
132
|
+
grid_width: 1.2
|
|
133
|
+
text_color: "#1e2a38"
|
|
134
|
+
caption_color: "#52606d"
|
|
135
|
+
spine_color: "#cbd2d9"
|
|
136
|
+
accent_color: "#29b6f6"
|
|
137
|
+
title_weight: "bold"
|
|
138
|
+
show_grid: true
|
|
139
|
+
despine: true
|
|
140
|
+
|
|
141
|
+
labels:
|
|
142
|
+
DRP: "Dissolved Reactive Phosphorus"
|
|
143
|
+
ECOLI: "E. coli"
|
|
144
|
+
Ecoli: "E. coli"
|
|
145
|
+
CLAR: "Visual Clarity"
|
|
146
|
+
NO3: "Nitrate-N"
|
|
147
|
+
SIN: "Soluble Inorganic Nitrogen"
|
|
148
|
+
NH4N_adj: "Ammoniacal-N"
|
|
149
|
+
NH4-N_raw: "Ammoniacal-N"
|
|
150
|
+
TN: "Total Nitrogen"
|
|
151
|
+
TP: "Total Phosphorus"
|
|
152
|
+
TURB: "Turbidity"
|
|
153
|
+
DO_Conc: "Dissolved Oxygen"
|
|
154
|
+
DO_Sat: "Dissolved Oxygen Saturation"
|
|
155
|
+
MCI: "MCI"
|
|
156
|
+
Manawatu: "Manawatū"
|
|
157
|
+
Rangitikei: "Rangitīkei"
|
|
158
|
+
Rangitikei/Turakina: "Rangitīkei/Turakina"
|
|
159
|
+
Akitio: "Ākitio"
|
|
160
|
+
Oroua: "Ōroua"
|
|
161
|
+
Ohau: "Ōhau"
|
|
162
|
+
Taupo: "Taupō"
|
|
163
|
+
|
|
164
|
+
indicator_directions:
|
|
165
|
+
# Indicators where INCREASING values mean IMPROVING environmental state
|
|
166
|
+
CLAR: "higher_is_better"
|
|
167
|
+
Clarity: "higher_is_better"
|
|
168
|
+
Visual Clarity: "higher_is_better"
|
|
169
|
+
BDISC: "higher_is_better"
|
|
170
|
+
MCI: "higher_is_better"
|
|
171
|
+
QMCI: "higher_is_better"
|
|
172
|
+
SQMCI: "higher_is_better"
|
|
173
|
+
ASPM: "higher_is_better"
|
|
174
|
+
DO: "higher_is_better"
|
|
175
|
+
DO_Conc: "higher_is_better"
|
|
176
|
+
DO_Sat: "higher_is_better"
|
|
177
|
+
Dissolved Oxygen: "higher_is_better"
|
|
178
|
+
# Indicators where DECREASING values mean IMPROVING environmental state
|
|
179
|
+
DRP: "lower_is_better"
|
|
180
|
+
ECOLI: "lower_is_better"
|
|
181
|
+
Ecoli: "lower_is_better"
|
|
182
|
+
E. coli: "lower_is_better"
|
|
183
|
+
NO3: "lower_is_better"
|
|
184
|
+
NO3N: "lower_is_better"
|
|
185
|
+
SIN: "lower_is_better"
|
|
186
|
+
NH4N: "lower_is_better"
|
|
187
|
+
NH4N_adj: "lower_is_better"
|
|
188
|
+
NH4-N_raw: "lower_is_better"
|
|
189
|
+
TN: "lower_is_better"
|
|
190
|
+
TP: "lower_is_better"
|
|
191
|
+
TURB: "lower_is_better"
|
|
192
|
+
Turbidity: "lower_is_better"
|
|
193
|
+
SUSP: "lower_is_better"
|
|
194
|
+
PERI: "lower_is_better"
|
|
195
|
+
CHLA: "lower_is_better"
|
|
196
|
+
ChlA: "lower_is_better"
|
|
197
|
+
|
|
198
|
+
figure_sizes:
|
|
199
|
+
full_width: [11.0, 5.8]
|
|
200
|
+
half_width: [6.0, 4.5]
|
|
201
|
+
square: [7.5, 7.5]
|
|
202
|
+
tall: [8.0, 11.0]
|
|
203
|
+
a4_poster: [8.27, 11.69]
|
|
204
|
+
|
|
205
|
+
export:
|
|
206
|
+
dpi: 300
|
|
207
|
+
bbox_inches: "tight"
|
|
208
|
+
transparent: false
|