File size: 12,163 Bytes
3656aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Manifest schema and row-level quality gates.

Required columns are ``sample_id``, ``image_path``, ``storm_id``, and
``wind_speed_kt``. ``require_valid`` raises ``SchemaError`` on a failed
gate. It does not warn and continue.

``wind_averaging`` must record one period for the whole manifest, such as
``1-min``, ``3-min``, or ``10-min``. Mixed or blank periods are rejected.
``is_synthetic`` must be an explicit true or false on every row so a
synthetic sample cannot be read as real.

Image statistics are pixel stats. This schema has no brightness-temperature
columns. Raw digital numbers are not named ``tb``. Those names are used
only after a brightness-temperature calibration table exists.

The 17/34/48/64/91 kt bins are this project's grouping, not official IMD
classes, and they are not a warning product. ``imd5_class`` stores that
project group.
"""

from __future__ import annotations

from dataclasses import dataclass

import pandas as pd

REQUIRED_COLUMNS = ("sample_id", "image_path", "storm_id", "wind_speed_kt")
FORBIDDEN_FEATURES = frozenset(
    {"wind_speed_kt", "r34_nmi", "rmw_nmi", "imd5_class", "intensity_bin"}
)
# Project metric labels, not an IMD warning. Five names group D+DD and
# ESCS+SuCS. The edges 17/34/48/64/91 kt are this project's own grouping.
# IMD operational winds are 3-minute averages and are not these bins.
# NASA HURDAT2 labels are 1-minute winds; config data.wind_averaging is
# 1min for that dataset. Keep these tuples equal to data.imd5_edges_kt
# and data.imd5_names. The July 2024 IMD 3-minute sheet starts Extremely
# Severe at 90 kt, so 90 kt is VSCS here.
DEFAULT_EDGES_KT = (17.0, 34.0, 48.0, 64.0, 91.0)
DEFAULT_NAMES = ("D_DD", "CS", "SCS", "VSCS", "ESCS_SUCS")
DEFAULT_WIND_MIN_KT = 0.0
DEFAULT_WIND_MAX_KT = 200.0

_TRUE_TEXT = frozenset({"1", "true", "yes", "y", "t"})
_FALSE_TEXT = frozenset({"0", "false", "no", "n", "f"})
_MISSING_TEXT = frozenset({"", "nan", "none", "<na>", "nat", "null"})


class SchemaError(ValueError):
    """A manifest failed a quality gate."""


@dataclass(frozen=True)
class QualityReport:
    n_rows: int
    n_storms: int
    errors: tuple[str, ...]

    @property
    def ok(self) -> bool:
        return not self.errors


def wind_to_imd5(
    wind_kt: float,
    edges: tuple[float, ...] = DEFAULT_EDGES_KT,
    names: tuple[str, ...] = DEFAULT_NAMES,
) -> str | None:
    """Return this project's intensity group, or None below the lowest edge.

    The names and edges are this project's grouping. They are not official IMD classes, IMD 3-minute categories, or a warning product.
    """
    if len(edges) != len(names):
        raise ValueError("edges and names must have the same length")
    if pd.isna(wind_kt) or float(wind_kt) < edges[0]:
        return None
    value = float(wind_kt)
    for lower, upper, name in zip(edges, edges[1:], names):
        if lower <= value < upper:
            return name
    return names[-1]


def _unpack_scalar(value: object) -> object:
    """Return a Python scalar when ``value`` is a numpy scalar."""
    if isinstance(value, (str, bytes, bytearray, bool)):
        return value
    item = getattr(value, "item", None)
    if not callable(item):
        return value
    try:
        unpacked = item()
    except (TypeError, ValueError):
        return value
    if type(unpacked) is type(value):
        return value
    return unpacked


def _is_missing(value: object) -> bool:
    if value is None:
        return True
    try:
        missing = pd.isna(value)
    except (TypeError, ValueError):
        return False
    if missing is True:
        return True
    return False


def _sample_key(value: object) -> str:
    if isinstance(value, bool):
        return "True" if value else "False"
    if _is_missing(value):
        return ""
    if isinstance(value, str):
        return value.strip()
    value = _unpack_scalar(value)
    if isinstance(value, bool):
        return "True" if value else "False"
    if _is_missing(value):
        return ""
    if isinstance(value, str):
        return value.strip()
    if isinstance(value, int) and not isinstance(value, bool):
        return str(value)
    if isinstance(value, float):
        if value != value:
            return ""
        if value.is_integer():
            return str(int(value))
    return str(value).strip()


def parse_is_synthetic(value: object) -> bool:
    """Return an explicit synthetic flag. Ambiguous values raise.

    Null, blank, and unrecognized values are not treated as real data.
    """
    if isinstance(value, bool):
        return value
    if isinstance(value, str):
        text = value.strip().lower()
        if text in _TRUE_TEXT:
            return True
        if text in _FALSE_TEXT:
            return False
        if text in _MISSING_TEXT:
            raise SchemaError("is_synthetic is null; synthetic rows must be explicit")
        try:
            number = float(text)
        except ValueError:
            raise SchemaError(
                f"is_synthetic value {value!r} is not an explicit true or false"
            ) from None
        return parse_is_synthetic(number)
    value = _unpack_scalar(value)
    if isinstance(value, bool):
        return value
    if _is_missing(value):
        raise SchemaError("is_synthetic is null; synthetic rows must be explicit")
    if isinstance(value, str):
        return parse_is_synthetic(value)
    if isinstance(value, int) and not isinstance(value, bool):
        if value in (0, 1):
            return bool(value)
        raise SchemaError(
            f"is_synthetic value {value!r} is not an explicit true or false"
        )
    if isinstance(value, float):
        if value != value or value in (float("inf"), float("-inf")):
            raise SchemaError("is_synthetic is null; synthetic rows must be explicit")
        if value == 1.0:
            return True
        if value == 0.0:
            return False
    raise SchemaError(f"is_synthetic value {value!r} is not an explicit true or false")


def recorded_wind_averaging(frame: pd.DataFrame) -> str:
    """Return the single averaging period recorded on this manifest.

    The column is required. A blank row or two different periods raises.
    """
    if "wind_averaging" not in frame.columns:
        raise SchemaError(
            "wind_averaging is required; record one period for the manifest "
            "(for example 1-min, 3-min, or 10-min)"
        )
    if len(frame) == 0:
        raise SchemaError(
            "wind_averaging is required; record one period for the manifest"
        )
    periods: list[str] = []
    for value in frame["wind_averaging"].tolist():
        if isinstance(value, str):
            text = value.strip()
        elif _is_missing(value):
            text = ""
        else:
            unpacked = _unpack_scalar(value)
            if _is_missing(unpacked):
                text = ""
            else:
                text = str(unpacked).strip()
        if text.lower() in _MISSING_TEXT:
            raise SchemaError(
                "wind_averaging has nulls; record one period for every row"
            )
        periods.append(text)
    unique = sorted(set(periods))
    if len(unique) != 1:
        raise SchemaError(
            f"mixed wind_averaging periods {unique}; split or convert explicitly"
        )
    return unique[0]


# The label's duration in seconds. This is not a wind conversion factor.
_AVERAGING_SECONDS = {
    "1min": 60,
    "1-min": 60,
    "1minute": 60,
    "1-minute": 60,
    "3min": 180,
    "3-min": 180,
    "3minute": 180,
    "3-minute": 180,
    "10min": 600,
    "10-min": 600,
    "10minute": 600,
    "10-minute": 600,
}


def wind_averaging_seconds(period: object) -> int | None:
    """Seconds for a 1, 3, or 10 minute label. Other labels have no duration.

    ``1min`` is 60, ``3-min`` is 180, and ``10min`` is 600. Synthetic and
    unknown text stay null. This does not rescale a wind speed.
    """
    if period is None:
        return None
    text = str(period).strip().lower().replace(" ", "").replace("_", "-")
    if not text:
        return None
    return _AVERAGING_SECONDS.get(text)


def split_wind_averaging(frame: pd.DataFrame) -> dict[str, dict[str, list]]:
    """``wind_averaging`` and ``wind_averaging_s`` for train, val, and test.

    Each split lists the set of labels on its rows. Seconds follow the
    label. A label with no known duration stores null and is not converted.
    """
    if "split" not in frame.columns:
        raise SchemaError("split is required to record wind_averaging by split")
    if "wind_averaging" not in frame.columns:
        raise SchemaError("wind_averaging is required")
    report: dict[str, dict[str, list]] = {}
    for name in ("train", "val", "test"):
        rows = frame.loc[frame["split"].astype(str) == name, "wind_averaging"]
        labels = sorted({str(value).strip() for value in rows.tolist() if str(value).strip()})
        report[name] = {
            "wind_averaging": labels,
            "wind_averaging_s": [wind_averaging_seconds(label) for label in labels],
        }
    return report


def _synthetic_flags(frame: pd.DataFrame) -> list[bool]:
    if "is_synthetic" not in frame.columns:
        raise SchemaError(
            "is_synthetic is required so a synthetic row cannot be read as real"
        )
    return [parse_is_synthetic(value) for value in frame["is_synthetic"].tolist()]


def validate_manifest(
    frame: pd.DataFrame,
    wind_min_kt: float = DEFAULT_WIND_MIN_KT,
    wind_max_kt: float = DEFAULT_WIND_MAX_KT,
) -> QualityReport:
    """Collect quality-gate failures. Callers that load data use ``require_valid``."""
    errors: list[str] = []
    missing = [column for column in REQUIRED_COLUMNS if column not in frame.columns]
    if missing:
        return QualityReport(len(frame), 0, (f"missing required columns: {missing}",))

    keys = frame["sample_id"].map(_sample_key)
    null_id = frame["sample_id"].isna() | keys.eq("") | keys.str.lower().isin(_MISSING_TEXT)
    if bool(null_id.any()):
        errors.append("sample_id has nulls")
    if bool(keys.duplicated().any()):
        examples = keys.loc[keys.duplicated()].head(5).tolist()
        errors.append(f"duplicate sample_id, examples: {examples}")
    blank_storm = frame["storm_id"].astype(str).str.strip().str.len() == 0
    if frame["storm_id"].isna().any() or blank_storm.any():
        errors.append("storm_id has nulls")
    if frame["image_path"].isna().any() or (
        frame["image_path"].astype(str).str.strip().str.len() == 0
    ).any():
        errors.append("image_path has nulls")

    winds = pd.to_numeric(frame["wind_speed_kt"], errors="coerce")
    if winds.isna().any():
        errors.append("wind_speed_kt has non-numeric values")
    else:
        bad = (winds < wind_min_kt) | (winds > wind_max_kt)
        if bad.any():
            errors.append(
                f"{int(bad.sum())} wind values outside [{wind_min_kt}, {wind_max_kt}] kt"
            )

    try:
        recorded_wind_averaging(frame)
    except SchemaError as exc:
        errors.append(str(exc))
    try:
        _synthetic_flags(frame)
    except SchemaError as exc:
        errors.append(str(exc))

    n_storms = int(frame["storm_id"].nunique(dropna=True))
    return QualityReport(len(frame), n_storms, tuple(errors))


def require_valid(frame: pd.DataFrame, **kwargs: float) -> pd.DataFrame:
    """Return a copy that passed every manifest gate. Raises on failure."""
    report = validate_manifest(frame, **kwargs)
    if not report.ok:
        raise SchemaError("; ".join(report.errors))
    out = frame.copy()
    out["storm_id"] = out["storm_id"].astype(str).str.strip()
    out["sample_id"] = out["sample_id"].map(_sample_key).astype(str)
    out["image_path"] = out["image_path"].astype(str).str.strip()
    out["wind_speed_kt"] = pd.to_numeric(out["wind_speed_kt"])
    out["wind_averaging"] = recorded_wind_averaging(out)
    out["is_synthetic"] = _synthetic_flags(out)
    # Project grouping stored on imd5_class. Not an official IMD class code.
    out["imd5_class"] = out["wind_speed_kt"].map(wind_to_imd5)
    return out