File size: 33,330 Bytes
85f2567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
"""INCYDE loader for the cropped INSAT-3D North Indian Ocean set.

Images come from the Zenodo 8015544 file ``Cropped_cyclone_data.zip``
(MD5 ``ed5dac254c689e5a327dee7580324347``). This module never downloads
that zip. Point ``paths.incyde_root`` at an extracted copy.

Filename times are IST (UTC+05:30). ``Sequence`` in the labels CSV is the
storm key. The trailing ``obj`` index is not a storm id. The default split
and the exclusion list are ``incyde/audit/storm_split.csv`` and
``incyde/audit/exclusions.csv``.

``on_fix``, ``distance_to_land_km``, and ``near_land`` are joined from
``incyde/audit/frame_flags.csv`` on the source filename. A ``rot_180``
copy inherits that source row. A filename the table does not list stays
unknown: ``on_fix`` is ``unknown``, the distance is null, and
``near_land`` is ``unknown``. Those gaps are not filled with false or
zero. The labels are already 3-minute winds, so the join does not
rescale them. ``basin`` value ``LAND`` is the IMD basin of a land
depression, not a frame landfall.

Only original frames and ``rot_180`` copies are kept. ``rot_180`` is
synthetic, stays with its source storm, and is train-only. Flips reverse
cyclone spin. ``rot_90`` and ``rot_270`` add black bars. Both are dropped.
"""

from __future__ import annotations

import argparse
import hashlib
import re
from datetime import datetime, timedelta, timezone
from pathlib import Path

import numpy as np
import pandas as pd

from src.schema import require_valid
from src.splits import LeakageError

ZIP_MD5 = "ed5dac254c689e5a327dee7580324347"
WIND_AVERAGING = "3min"
WIND_AVERAGING_S = 180
CV_SEED = 42
YEAR_HOLDOUT_NOTE = (
    "indicative only: different training years and images from "
    "Agrawal et al. 2024, arXiv:2410.08218. Landfall frames are kept; "
    "the audit outputs have no landfall flag."
)
KEPT_AUG = frozenset({"", "rot_180"})
DROPPED_AUG = frozenset({"flip_h", "flip_v", "rot_90", "rot_270"})
_IST = timezone(timedelta(hours=5, minutes=30))
_MONTHS = {
    name: index
    for index, name in enumerate(
        ("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"),
        start=1,
    )
}
_FILENAME = re.compile(
    r"^(?P<day>\d{2})(?P<mon>[A-Za-z]{3})(?P<year>\d{4})_"
    r"(?P<hour>\d{2})(?P<minute>\d{2})_(?P<obj>\d+)"
    r"(?:_(?P<aug>flip_h|flip_v|rot_90|rot_180|rot_270))?\.jpe?g$",
    re.IGNORECASE,
)
_AUG_SUFFIX = re.compile(
    r"_(?:flip_h|flip_v|rot_90|rot_180|rot_270)(?=\.jpe?g$)",
    re.IGNORECASE,
)
_STORM_ID = re.compile(r"^INCYDE-S\d+_IMD-(?P<year>\d{4})-\d+$")
_AUDIT_DIR = Path(__file__).resolve().parents[1] / "incyde" / "audit"
DEFAULT_STORM_SPLIT = _AUDIT_DIR / "storm_split.csv"
DEFAULT_EXCLUSIONS = _AUDIT_DIR / "exclusions.csv"
DEFAULT_FRAME_FLAGS = _AUDIT_DIR / "frame_flags.csv"
_FRAME_FLAG_COLUMNS = ("filename", "on_fix", "distance_to_land_km", "near_land")


def imd_class_of(wind_kt: float) -> str:
    """IMD class on a 3-minute wind, in knots.

    D ≤27, DD ≤33, CS ≤47, SCS ≤63, VSCS ≤89, ESCS ≤119, SuCS ≥120.
    Below 17 kt is ``<17``. These are not the project's 17/34/48/64/91 bins.
    """
    wind = float(wind_kt)
    if wind < 17:
        return "<17"
    if wind <= 27:
        return "D"
    if wind <= 33:
        return "DD"
    if wind <= 47:
        return "CS"
    if wind <= 63:
        return "SCS"
    if wind <= 89:
        return "VSCS"
    if wind <= 119:
        return "ESCS"
    return "SuCS"


def storm_year(storm_id: object) -> int:
    """Year from ``INCYDE-Sxx_IMD-yyyy-nnn``. Filename dates are not used."""
    match = _STORM_ID.match(str(storm_id).strip())
    if match is None:
        raise ValueError(f"storm_id {storm_id!r} is not INCYDE-Sxx_IMD-yyyy-nnn")
    return int(match.group("year"))


def assert_one_storm_year(frame: pd.DataFrame) -> pd.Series:
    """Each storm id maps to one IMD year. A filename that crosses 1 January does not add a second year."""
    years = frame["storm_id"].map(storm_year)
    counts = pd.DataFrame({"storm_id": frame["storm_id"].astype(str), "year": years}).groupby("storm_id")["year"].nunique()
    if bool((counts > 1).any()):
        raise LeakageError("a storm_id maps to more than one year")
    return years


def archive_md5(path: str | Path) -> str:
    """MD5 of a local file. Does not download."""
    digest = hashlib.md5(usedforsecurity=False)
    with Path(path).open("rb") as handle:
        for chunk in iter(lambda: handle.read(1 << 20), b""):
            digest.update(chunk)
    return digest.hexdigest()


def assert_archive_md5(path: str | Path) -> None:
    """Refuse a cropped-zip copy whose MD5 is not the Zenodo value."""
    found = archive_md5(path)
    if found != ZIP_MD5:
        raise ValueError(f"INCYDE archive md5 is {found}, expected {ZIP_MD5}")


def _optional_path(value: object, default: Path) -> Path:
    if value is None:
        return default
    text = str(value).strip()
    if text.lower() in {"", "none", "null"}:
        return default
    return Path(text)


def read_storm_split(path: str | Path | None = None) -> pd.DataFrame:
    """One row per Sequence. ``proposed_split`` is Bean's grouped split."""
    table = pd.read_csv(_optional_path(path, DEFAULT_STORM_SPLIT))
    table.columns = [str(column).strip() for column in table.columns]
    required = {"Sequence", "storm_id", "proposed_split", "cooccurrence_group"}
    missing = sorted(required - set(table.columns))
    if missing:
        raise ValueError(f"storm_split.csv missing {missing}")
    out = table.copy()
    out["Sequence"] = pd.to_numeric(out["Sequence"], errors="raise").astype(int)
    out["storm_id"] = out["storm_id"].astype(str).str.strip()
    out["proposed_split"] = out["proposed_split"].astype(str).str.strip()
    out["cooccurrence_group"] = pd.to_numeric(out["cooccurrence_group"], errors="raise").astype(int)
    if out["storm_id"].duplicated().any() or out["Sequence"].duplicated().any():
        raise ValueError("storm_split.csv repeats a storm")
    bad = sorted(set(out["proposed_split"]) - {"train", "val", "test"})
    if bad:
        raise ValueError(f"storm_split.csv has unknown splits {bad}")
    assert_one_storm_year(out)
    return out


def read_exclusions(path: str | Path | None = None) -> pd.DataFrame:
    table = pd.read_csv(_optional_path(path, DEFAULT_EXCLUSIONS))
    table.columns = [str(column).strip() for column in table.columns]
    required = {"filename", "qc_flag", "default_exclude"}
    missing = sorted(required - set(table.columns))
    if missing:
        raise ValueError(f"exclusions.csv missing {missing}")
    out = table.copy()
    out["filename"] = out["filename"].astype(str).map(lambda name: Path(name).name)
    out["qc_flag"] = out["qc_flag"].fillna("").astype(str)
    out["default_exclude"] = [_is_true(value) for value in out["default_exclude"].tolist()]
    if out["filename"].duplicated().any():
        raise ValueError("exclusions.csv repeats a filename")
    return out


def _is_true(value: object) -> bool:
    if isinstance(value, (bool, np.bool_)):
        return bool(value)
    return str(value).strip().lower() in {"1", "true", "yes", "y", "t"}


def _blank_flag(value: object) -> bool:
    if value is None:
        return True
    try:
        if pd.isna(value):
            return True
    except (TypeError, ValueError):
        return False
    return str(value).strip().lower() in {"", "nan", "none", "null", "<na>"}


def _on_fix_value(value: object) -> bool | str:
    """Explicit bool, or ``unknown`` when the audit did not decide.

    ``unknown`` is not stored as false. A later on-fix metric uses only
    the true rows.
    """
    if isinstance(value, (bool, np.bool_)):
        return bool(value)
    if _blank_flag(value):
        return "unknown"
    text = str(value).strip().lower()
    if text in {"1", "true", "yes", "y", "t"}:
        return True
    if text in {"0", "false", "no", "n", "f"}:
        return False
    if text in {"unknown", "na", "n/a"}:
        return "unknown"
    raise ValueError(f"on_fix value {value!r} is not true, false, or unknown")


def _near_land_token(value: object) -> str:
    """``true``, ``false``, or ``unknown``. Blank stays unknown."""
    if isinstance(value, (bool, np.bool_)):
        return "true" if bool(value) else "false"
    if _blank_flag(value):
        return "unknown"
    text = str(value).strip().lower()
    if text in {"1", "true", "yes", "y", "t"}:
        return "true"
    if text in {"0", "false", "no", "n", "f"}:
        return "false"
    if text in {"unknown", "na", "n/a"}:
        return "unknown"
    raise ValueError(f"near_land value {value!r} is not true, false, or unknown")


def _distance_km(value: object) -> float:
    """Kilometres, or NaN when the audit left the cell blank."""
    if _blank_flag(value):
        return float("nan")
    try:
        number = float(value)
    except (TypeError, ValueError) as exc:
        raise ValueError(f"distance_to_land_km value {value!r} is not a distance") from exc
    if not np.isfinite(number):
        return float("nan")
    return number


def read_frame_flags(path: str | Path | None = None) -> pd.DataFrame:
    """Original-frame flags. Augmented filenames are not required in the file."""
    table = pd.read_csv(_optional_path(path, DEFAULT_FRAME_FLAGS))
    table.columns = [str(column).strip() for column in table.columns]
    missing = [column for column in _FRAME_FLAG_COLUMNS if column not in table.columns]
    if missing:
        raise ValueError(f"frame_flags.csv missing {missing}")
    on_fix = [_on_fix_value(value) for value in table["on_fix"].tolist()]
    out = pd.DataFrame(
        {
            "filename": table["filename"].astype(str).map(lambda name: Path(name).name),
            "distance_to_land_km": [_distance_km(value) for value in table["distance_to_land_km"].tolist()],
            "near_land": [_near_land_token(value) for value in table["near_land"].tolist()],
        }
    )
    # Object dtype so a homogeneous true/false column stays a Python bool
    # and an unknown cell is not coerced to false.
    out["on_fix"] = pd.Series(on_fix, index=out.index, dtype=object)
    if out["filename"].duplicated().any():
        raise ValueError("frame_flags.csv repeats a filename")
    return out


def _flag_key(frame: pd.DataFrame) -> pd.Series:
    """Source filename. Augmented copies inherit; they are not their own key."""
    if "source_frame_id" in frame.columns:
        return frame["source_frame_id"].astype(str).map(lambda name: Path(name).name)
    if "filename" in frame.columns:
        return frame["filename"].astype(str).map(lambda name: Path(_AUG_SUFFIX.sub("", name)).name)
    raise ValueError("frame flags need source_frame_id or filename")


def attach_frame_flags(frame: pd.DataFrame, flags: pd.DataFrame) -> pd.DataFrame:
    """Add ``on_fix``, ``distance_to_land_km``, and ``near_land``.

    The lookup key is the source filename, so a ``rot_180`` copy receives
    the original frame's row. A miss stays unknown. Distance is not filled
    with 0, and ``on_fix`` is not filled with false.
    """
    lookup = flags.drop_duplicates("filename").set_index("filename")
    on_fix: list[object] = []
    distance: list[float] = []
    near: list[str] = []
    for key in _flag_key(frame).tolist():
        if key not in lookup.index:
            on_fix.append("unknown")
            distance.append(float("nan"))
            near.append("unknown")
            continue
        row = lookup.loc[key]
        if isinstance(row, pd.DataFrame):
            raise ValueError(f"frame_flags.csv repeats {key}")
        on_fix.append(_on_fix_value(row["on_fix"]))
        distance.append(_distance_km(row["distance_to_land_km"]))
        near.append(_near_land_token(row["near_land"]))
    out = frame.copy()
    out["on_fix"] = pd.Series(on_fix, index=out.index, dtype=object)
    out["distance_to_land_km"] = np.asarray(distance, dtype=float)
    out["near_land"] = near
    return out


def parse_filename(filename: str) -> dict[str, object]:
    """Parse ``DDMONYYYY_HHMM_<obj>[_aug].jpg``. ``obj`` is not a storm id."""
    name = Path(str(filename)).name
    match = _FILENAME.match(name)
    if match is None:
        raise ValueError(
            f"INCYDE filename {name} does not match DDMONYYYY_HHMM_<obj>[_aug].jpg"
        )
    parts = match.groupdict()
    month = _MONTHS.get(parts["mon"].upper())
    if month is None:
        raise ValueError(f"INCYDE filename {name} has an unknown month")
    ist = datetime(
        int(parts["year"]),
        month,
        int(parts["day"]),
        int(parts["hour"]),
        int(parts["minute"]),
        tzinfo=_IST,
    )
    utc = ist.astimezone(timezone.utc)
    aug = "" if not parts["aug"] else parts["aug"].lower()
    return {
        "filename": name,
        "time_ist": ist.isoformat(),
        "time_utc": utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
        "object_index": int(parts["obj"]),
        "aug_type": aug,
        "source_frame_id": _AUG_SUFFIX.sub("", name),
    }


def _source_table(frame: pd.DataFrame) -> pd.DataFrame:
    """Labels CSV or the audit manifest, reduced to one column set."""
    columns = {str(column).strip(): column for column in frame.columns}
    renamed = frame.rename(columns={raw: clean for clean, raw in columns.items()})
    if "Sequence" in renamed.columns and ("MSWS kts" in renamed.columns or "label_kt" in renamed.columns):
        wind_column = "MSWS kts" if "MSWS kts" in renamed.columns else "label_kt"
        filename = renamed["filename"] if "filename" in renamed.columns else renamed["file"]
        out = pd.DataFrame(
            {
                "sequence": pd.to_numeric(renamed["Sequence"], errors="raise").astype(int),
                "filename": filename.astype(str),
                "wind_kt": pd.to_numeric(renamed[wind_column], errors="coerce"),
                "source_category": renamed["Category"].astype(str) if "Category" in renamed.columns else "",
                "incyde_set": renamed["Set"].astype(str) if "Set" in renamed.columns else "",
                "given_storm_id": renamed["storm_id"].astype(str) if "storm_id" in renamed.columns else pd.NA,
            }
        )
        return out
    if "storm_id" in renamed.columns and "label_kt" in renamed.columns:
        filename = renamed["filename"] if "filename" in renamed.columns else renamed["file"]
        sequence = renamed["incyde_sequence"] if "incyde_sequence" in renamed.columns else pd.Series(pd.NA, index=renamed.index)
        category = renamed["label_class_imd"] if "label_class_imd" in renamed.columns else ""
        return pd.DataFrame(
            {
                "sequence": pd.to_numeric(sequence, errors="coerce"),
                "filename": filename.astype(str),
                "wind_kt": pd.to_numeric(renamed["label_kt"], errors="coerce"),
                "source_category": category.astype(str) if not isinstance(category, str) else category,
                "incyde_set": renamed["incyde_set"].astype(str) if "incyde_set" in renamed.columns else "",
                "given_storm_id": renamed["storm_id"].astype(str),
            }
        )
    raise ValueError(
        "INCYDE manifest needs the labels columns (Sequence, MSWS kts, filename) "
        "or the audit columns (storm_id, label_kt, file)"
    )


def _locate_labels(root: Path) -> Path:
    candidates = [
        root / "Cropped_cyclone_labels.csv",
        root / "Cropped_cyclone_data" / "Cropped_cyclone_labels.csv",
        root / "labels.csv",
    ]
    for path in candidates:
        if path.is_file():
            return path
    raise FileNotFoundError(
        f"No INCYDE labels CSV under {root}. This loader does not download imagery."
    )


def locate_image(root: Path, filename: str) -> Path:
    name = Path(filename).name
    candidates = [
        root / name,
        root / "Cropped_cyclone_images" / name,
        root / "Cropped_cyclone_data" / "Cropped_cyclone_images" / name,
        root / "Cropped_cyclone_data" / name,
    ]
    for path in candidates:
        if path.is_file():
            return path
    raise FileNotFoundError(
        f"INCYDE image {name} is missing under {root}. This loader does not download imagery."
    )


def _qc_tokens(flag: object) -> set[str]:
    text = "" if flag is None or (isinstance(flag, float) and np.isnan(flag)) else str(flag)
    return {part.strip() for part in text.split(";") if part.strip()}


def _drop_reason(aug: str, exclude: bool, qc_flag: str, drop_qc_flags: set[str]) -> str | None:
    if exclude:
        return "default_exclude"
    if aug in DROPPED_AUG:
        return f"dropped aug {aug}"
    if aug not in KEPT_AUG:
        return f"unknown aug {aug or 'blank'}"
    if drop_qc_flags and _qc_tokens(qc_flag) & drop_qc_flags:
        return "drop_qc_flags"
    return None


def read_rows(
    root: str | Path,
    *,
    manifest_csv: str | Path | None = None,
    storm_split_csv: str | Path | None = None,
    exclusions_csv: str | Path | None = None,
    frame_flags_csv: str | Path | None = None,
    drop_qc_flags: list[str] | tuple[str, ...] | None = None,
    check_images: bool = True,
) -> tuple[pd.DataFrame, int]:
    """Frame rows before the final split. ``rot_180`` is still present.

    Flips, ``rot_90``, ``rot_270``, and ``default_exclude`` rows are gone.
    The returned count is how many source rows were dropped here.
    """
    root = Path(root)
    if not root.is_dir():
        raise FileNotFoundError(
            f"INCYDE root {root} is missing. Set paths.incyde_root to the extracted "
            "Cropped_cyclone_data directory. This entrypoint does not download imagery."
        )
    label_path = Path(manifest_csv) if manifest_csv not in (None, "", "null") else _locate_labels(root)
    if not label_path.is_file():
        raise FileNotFoundError(f"INCYDE manifest {label_path} is missing. Nothing is downloaded.")
    storms = read_storm_split(storm_split_csv)
    exclusions = read_exclusions(exclusions_csv)
    source = _source_table(pd.read_csv(label_path, encoding="utf-8-sig"))
    parsed = [parse_filename(name) for name in source["filename"].tolist()]
    parsed_frame = pd.DataFrame(parsed).drop(columns=["filename"])
    work = pd.concat([source.reset_index(drop=True), parsed_frame], axis=1)
    work["filename"] = work["filename"].map(lambda name: Path(str(name)).name)
    by_sequence = storms.set_index("Sequence")
    by_storm = storms.drop_duplicates("storm_id").set_index("storm_id")
    storm_ids: list[str] = []
    sequences: list[int] = []
    proposed: list[str] = []
    groups: list[int] = []
    names: list[str] = []
    basins: list[str] = []
    for sequence, given in zip(work["sequence"].tolist(), work["given_storm_id"].tolist(), strict=True):
        row = None
        if pd.notna(sequence) and int(sequence) in by_sequence.index:
            row = by_sequence.loc[int(sequence)]
        elif isinstance(given, str) and given in by_storm.index:
            row = by_storm.loc[given]
        if row is None:
            raise ValueError(f"no storm_split row for sequence {sequence!r} storm {given!r}")
        if isinstance(row, pd.DataFrame):
            raise ValueError(f"storm_split matched more than one row for sequence {sequence}")
        storm_id = str(row["storm_id"]) if "storm_id" in row.index else str(row.name)
        sequence_id = int(row["Sequence"]) if "Sequence" in row.index else int(row.name)
        if isinstance(given, str) and given not in {"", "nan", "<NA>"} and given != storm_id:
            raise ValueError(f"manifest storm_id {given} does not match storm_split.csv ({storm_id})")
        storm_ids.append(storm_id)
        sequences.append(sequence_id)
        proposed.append(str(row["proposed_split"]))
        groups.append(int(row["cooccurrence_group"]))
        names.append("" if "name" not in row.index or pd.isna(row["name"]) else str(row["name"]))
        basins.append("" if "basin" not in row.index or pd.isna(row["basin"]) else str(row["basin"]))
    work["storm_id"] = storm_ids
    work["sequence"] = sequences
    work["proposed_split"] = proposed
    work["cooccurrence_group"] = groups
    work["name"] = names
    work["basin"] = basins
    qc = exclusions.set_index("filename")
    flags = []
    exclude = []
    for filename in work["filename"].tolist():
        if filename in qc.index:
            flags.append(str(qc.at[filename, "qc_flag"]))
            exclude.append(bool(qc.at[filename, "default_exclude"]))
        else:
            flags.append("")
            exclude.append(False)
    work["qc_flag"] = flags
    drop_flags = {str(flag).strip() for flag in (drop_qc_flags or []) if str(flag).strip()}
    reasons = [
        _drop_reason(aug, dropped, flag, drop_flags)
        for aug, dropped, flag in zip(work["aug_type"], exclude, work["qc_flag"], strict=True)
    ]
    dropped_count = sum(reason is not None for reason in reasons)
    kept = work.loc[[reason is None for reason in reasons]].reset_index(drop=True)
    if kept.empty:
        raise ValueError("INCYDE filters removed every row")
    if kept["filename"].duplicated().any():
        raise ValueError("INCYDE labels repeat a filename")
    years = assert_one_storm_year(kept)
    image_paths: list[str] = []
    heights: list[int] = []
    widths: list[int] = []
    if check_images:
        from PIL import Image

        for filename in kept["filename"].tolist():
            path = locate_image(root, filename)
            image_paths.append(path.resolve().as_posix())
            with Image.open(path) as image:
                widths.append(int(image.width))
                heights.append(int(image.height))
    else:
        image_paths = kept["filename"].tolist()
        heights = [0] * len(kept)
        widths = [0] * len(kept)
    frame = pd.DataFrame(
        {
            "sample_id": [Path(name).stem for name in kept["filename"].tolist()],
            "image_path": image_paths,
            "storm_id": kept["storm_id"].astype(str),
            "wind_speed_kt": kept["wind_kt"].to_numpy(dtype=float),
            "wind_kt": kept["wind_kt"].to_numpy(dtype=float),
            "wind_averaging": WIND_AVERAGING,
            "wind_averaging_s": WIND_AVERAGING_S,
            "is_synthetic": [aug == "rot_180" for aug in kept["aug_type"].tolist()],
            "imd_class": [imd_class_of(wind) for wind in kept["wind_kt"].tolist()],
            "split": kept["proposed_split"].astype(str),
            "proposed_split": kept["proposed_split"].astype(str),
            "cooccurrence_group": kept["cooccurrence_group"].astype(int),
            "time_utc": kept["time_utc"].astype(str),
            "time_ist": kept["time_ist"].astype(str),
            "storm_year": years.to_numpy(dtype=int),
            "aug_type": kept["aug_type"].astype(str),
            "source_frame_id": kept["source_frame_id"].astype(str),
            "object_index": kept["object_index"].astype(int),
            "qc_flag": kept["qc_flag"].astype(str),
            "incyde_sequence": kept["sequence"].astype(int),
            "incyde_set": kept["incyde_set"].astype(str),
            "source_category": kept["source_category"].astype(str),
            "name": kept["name"].astype(str),
            "basin": kept["basin"].astype(str),
            "native_height": heights,
            "native_width": widths,
            "orig_size": [height if height == width else -1 for height, width in zip(heights, widths, strict=True)],
            "sensor": "INSAT-3D",
            "channel": "IR",
            "pixel_units": "jpeg_uncalibrated",
            "label_source": "incyde_imd_3min",
            "center_source": "dataset_precentered",
        }
    )
    # Join after the row filter so dropped augs are not looked up. The
    # source filename is the key; unknown audit cells stay unknown.
    flagged = attach_frame_flags(frame, read_frame_flags(frame_flags_csv))
    return require_valid(flagged), int(dropped_count)


def build_cv_folds(storms: pd.DataFrame, n_folds: int, seed: int) -> pd.DataFrame:
    """Storm-grouped folds over train+val only. Test storms are absent.

    Groups are shuffled with ``seed`` and dealt round-robin, so every storm
    in one ``cooccurrence_group`` shares a fold. The training path passes
    ``incyde.cv_seed`` (42 in the YAML).
    """
    folds = int(n_folds)
    if folds < 2:
        raise ValueError("incyde.n_folds must be at least 2")
    pool = storms.loc[storms["proposed_split"].astype(str).isin(["train", "val"])].drop_duplicates("storm_id")
    if pool.empty:
        raise ValueError("storm_cv needs train and val storms")
    groups = sorted({int(group) for group in pool["cooccurrence_group"].tolist()})
    order = np.random.default_rng(int(seed)).permutation(len(groups))
    shuffled = [groups[int(index)] for index in order]
    group_fold = {group: index % folds for index, group in enumerate(shuffled)}
    out = pd.DataFrame(
        {
            "storm_id": pool["storm_id"].astype(str).tolist(),
            "fold": [int(group_fold[int(group)]) for group in pool["cooccurrence_group"].tolist()],
        }
    )
    return out.sort_values(["fold", "storm_id"]).reset_index(drop=True)


def read_cv_folds(path: str | Path) -> pd.DataFrame:
    """Use a folds file verbatim. Columns are ``storm_id`` and ``fold``."""
    table = pd.read_csv(path)
    table.columns = [str(column).strip() for column in table.columns]
    if "storm_id" not in table.columns or "fold" not in table.columns:
        raise ValueError("cv folds csv needs columns storm_id and fold")
    out = pd.DataFrame(
        {
            "storm_id": table["storm_id"].astype(str).str.strip(),
            "fold": pd.to_numeric(table["fold"], errors="raise").astype(int),
        }
    )
    if out["storm_id"].duplicated().any():
        raise ValueError("cv folds csv repeats a storm_id")
    return out.reset_index(drop=True)


def validate_cv_folds(folds: pd.DataFrame, storms: pd.DataFrame) -> None:
    """No test storm, and no cooccurrence group spread across folds."""
    test_ids = set(storms.loc[storms["proposed_split"].astype(str) == "test", "storm_id"].astype(str))
    listed = set(folds["storm_id"].astype(str))
    leaked = sorted(listed & test_ids)
    if leaked:
        raise LeakageError(f"cv folds include test storms {leaked[:8]}")
    pool = storms.loc[storms["proposed_split"].astype(str).isin(["train", "val"])].drop_duplicates("storm_id")
    pool_ids = set(pool["storm_id"].astype(str))
    missing = sorted(pool_ids - listed)
    extra = sorted(listed - pool_ids)
    if missing:
        raise ValueError(f"cv folds omit train/val storms {missing[:8]}")
    if extra:
        raise ValueError(f"cv folds name storms that are not train/val {extra[:8]}")
    merged = pool.assign(storm_id=pool["storm_id"].astype(str)).merge(folds, on="storm_id", how="left")
    spread = merged.groupby("cooccurrence_group")["fold"].nunique()
    if bool((spread > 1).any()):
        groups = [int(group) for group in spread.index[spread > 1].tolist()]
        raise LeakageError(f"cooccurrence_group spans folds {groups[:8]}")


def _folds_for(storms: pd.DataFrame, n_folds: int, seed: int, cv_folds_csv: str | Path | None) -> pd.DataFrame:
    if cv_folds_csv not in (None, "", "null"):
        folds = read_cv_folds(cv_folds_csv)
    else:
        folds = build_cv_folds(storms, n_folds, seed)
    validate_cv_folds(folds, storms)
    return folds


def _assert_storms(frame: pd.DataFrame) -> None:
    per_storm = frame.groupby(frame["storm_id"].astype(str))["split"].nunique()
    if bool((per_storm > 1).any()):
        raise LeakageError("a storm is in more than one split")
    synthetic = frame["is_synthetic"].astype(bool) & frame["split"].astype(str).ne("train")
    if bool(synthetic.any()):
        raise LeakageError("synthetic INCYDE rows are train-only")
    bad_aug = sorted(set(frame["aug_type"].astype(str)) - KEPT_AUG)
    if bad_aug:
        raise LeakageError(f"INCYDE kept dropped augmentations {bad_aug}")


def _assert_groups(frame: pd.DataFrame) -> None:
    spread = frame.groupby("cooccurrence_group")["split"].nunique()
    if bool((spread > 1).any()):
        raise LeakageError("a cooccurrence_group is in more than one split")


def assign_incyde_split(
    frame: pd.DataFrame,
    strategy: str,
    *,
    n_folds: int = 5,
    cv_val_fold: int = 0,
    cv_seed: int = CV_SEED,
    cv_folds_csv: str | Path | None = None,
) -> tuple[pd.DataFrame, int]:
    """Write ``split`` and drop synthetic rows that are not in train.

    ``storm_wise`` uses ``proposed_split`` (60/14/17 on the audit file).
    ``storm_cv`` keeps test storms out of the folds. ``year_holdout_2019``
    puts 2019 in test and 2021 in val. The year comes from ``storm_id``.
    """
    name = str(strategy).strip().lower()
    work = frame.copy()
    if "proposed_split" not in work.columns:
        raise ValueError("INCYDE rows need proposed_split from storm_split.csv")
    storms = work.groupby("storm_id", as_index=False).agg(
        proposed_split=("proposed_split", "first"),
        cooccurrence_group=("cooccurrence_group", "first"),
    )
    assert_one_storm_year(work)
    if name in {"storm_wise", "proposed", "bean"}:
        work["split"] = work["proposed_split"].astype(str)
        _assert_groups(work)
    elif name == "storm_cv":
        using_file = cv_folds_csv not in (None, "", "null")
        folds = _folds_for(storms, n_folds, cv_seed, cv_folds_csv)
        chosen = int(cv_val_fold)
        if not using_file and (chosen < 0 or chosen >= int(n_folds)):
            raise ValueError("incyde.cv_val_fold is outside 0 .. n_folds-1")
        fold_of = dict(zip(folds["storm_id"].astype(str), folds["fold"].astype(int), strict=True))
        splits = []
        for storm_id, proposed in zip(work["storm_id"].astype(str), work["proposed_split"].astype(str), strict=True):
            if proposed == "test":
                splits.append("test")
            else:
                splits.append("val" if fold_of[storm_id] == chosen else "train")
        work["split"] = splits
        work["cv_fold"] = work["storm_id"].astype(str).map(lambda storm_id: fold_of.get(storm_id, pd.NA))
        _assert_groups(work)
    elif name == "year_holdout_2019":
        years = work["storm_id"].map(storm_year)
        work["split"] = np.where(years.eq(2019), "test", np.where(years.eq(2021), "val", "train"))
    else:
        raise LeakageError(f"unknown INCYDE split strategy {strategy}")
    synthetic_eval = work["is_synthetic"].astype(bool) & work["split"].astype(str).ne("train")
    dropped = int(synthetic_eval.sum())
    work = work.loc[~synthetic_eval].reset_index(drop=True)
    _assert_storms(work)
    for split_name in ("train", "val", "test"):
        if split_name not in set(work["split"].astype(str)):
            raise LeakageError(f"INCYDE split {name} produced no {split_name} rows")
    return work, dropped


def load_frame(config: dict) -> tuple[pd.DataFrame, int]:
    """Rows for ``python -m src.train --dataset incyde``. Nothing is downloaded."""
    paths = config["paths"]
    block = config["incyde"]
    archive = block["archive"]
    if archive not in (None, "", "null"):
        assert_archive_md5(archive)
    frame, dropped = read_rows(
        paths["incyde_root"],
        manifest_csv=block["manifest_csv"],
        storm_split_csv=block["storm_split_csv"],
        exclusions_csv=block["exclusions_csv"],
        frame_flags_csv=block.get("frame_flags_csv"),
        drop_qc_flags=list(block["drop_qc_flags"] or []),
        check_images=True,
    )
    assigned, synthetic_dropped = assign_incyde_split(
        frame,
        str(config["split"]["strategy"]),
        n_folds=int(block["n_folds"]),
        cv_val_fold=int(block["cv_val_fold"]),
        cv_seed=int(block["cv_seed"]),
        cv_folds_csv=block["cv_folds_csv"],
    )
    return assigned, int(dropped) + int(synthetic_dropped)


def main(argv: list[str] | None = None) -> Path:
    """Write a manifest from a local extracted tree. Does not download."""
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("--root", type=Path, required=True)
    parser.add_argument("--manifest-csv", type=Path, default=None)
    parser.add_argument("--storm-split", type=Path, default=None)
    parser.add_argument("--exclusions", type=Path, default=None)
    parser.add_argument("--frame-flags", type=Path, default=None)
    parser.add_argument("--out", type=Path, required=True)
    parser.add_argument("--strategy", default="storm_wise")
    parser.add_argument("--archive", type=Path, default=None)
    args = parser.parse_args(argv)
    if args.archive is not None:
        assert_archive_md5(args.archive)
    frame, dropped = read_rows(
        args.root,
        manifest_csv=args.manifest_csv,
        storm_split_csv=args.storm_split,
        exclusions_csv=args.exclusions,
        frame_flags_csv=args.frame_flags,
    )
    assigned, synthetic_dropped = assign_incyde_split(frame, args.strategy)
    args.out.parent.mkdir(parents=True, exist_ok=True)
    assigned.to_csv(args.out, index=False)
    print(f"Wrote {args.out} ({len(assigned)} rows, dropped {dropped + synthetic_dropped}). Not a forecast.")
    return args.out


if __name__ == "__main__":
    main()