File size: 6,802 Bytes
904d76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Conservative filesystem inventory for dataset feasibility audits.

This records observed files only. It inspects verified dataset files when
metadata and loaders are available, while preserving conservative defaults.
"""

from __future__ import annotations

import argparse
import csv
import hashlib
import json
from pathlib import Path
from typing import Iterable

try:
    import scipy.io
    import numpy as np
    HAS_SCIPY = True
except ImportError:
    HAS_SCIPY = False

SUPPORTED = {".dng", ".mat", ".png", ".jpg", ".jpeg", ".tif", ".tiff"}
FIELDS = [
    "path", "sample_id", "capture_id", "scene_id", "camera", "iso", "cfa",
    "width", "height", "bit_depth", "representation", "metadata_present",
    "target_path", "alignment_verified", "split", "license_note", "sha256",
]


def sha256_file(path: Path, chunk_size: int = 1024 * 1024) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as stream:
        for chunk in iter(lambda: stream.read(chunk_size), b""):
            digest.update(chunk)
    return digest.hexdigest()


def classify(path: Path) -> str:
    suffix = path.suffix.lower()
    if suffix == ".dng":
        return "bayer_dng_candidate_metadata_unverified"
    if suffix == ".mat":
        return "mat_contents_uninspected"
    if suffix in {".png", ".jpg", ".jpeg", ".tif", ".tiff"}:
        return "image_target_or_input_unverified"
    return "unknown"


def files_under(root: Path) -> Iterable[Path]:
    for path in sorted(root.rglob("*")):
        if path.is_file() and path.suffix.lower() in SUPPORTED:
            yield path


def parse_sidd_record(path: Path, root: Path) -> dict[str, str] | None:
    """Attempts to inspect and extract verified metadata from a SIDD MAT file."""
    filename = path.name
    parent_name = path.parent.name
    
    parts = parent_name.split("_")
    # Expected: 0017_001_GP_00100_00060_5500_N (7 parts)
    if len(parts) != 7 or not filename.endswith(".MAT"):
        return None

    inst_id, scene_idx, camera, iso_str, shutter, temp, brightness = parts
    
    # Identify type: GT, NOISY, METADATA
    fn_no_ext = filename[:-4]
    fn_parts = fn_no_ext.split("_")
    if len(fn_parts) < 4:
        return None
    type_str = fn_parts[-3]
    idx_str = fn_parts[-1]

    sample_id = f"{parent_name}_{idx_str}"
    capture_id = parent_name
    scene_id = scene_idx
    iso = str(int(iso_str))
    
    meta_path = path.parent / f"{fn_parts[0]}_METADATA_RAW_{idx_str}.MAT"
    cfa = ""
    width = ""
    height = ""
    bit_depth = ""
    metadata_present = "false"

    if HAS_SCIPY and meta_path.exists():
        try:
            m = scipy.io.loadmat(meta_path)
            if "cfa_str" in m:
                cfa_val = m["cfa_str"]
                if isinstance(cfa_val, np.ndarray) and cfa_val.size > 0:
                    cfa = str(cfa_val.item()).upper()
                else:
                    cfa = str(cfa_val).upper()
            if "WhiteLevel" in m:
                wl = float(m["WhiteLevel"].item())
                if wl > 2000:
                    bit_depth = "12"
                elif wl > 500:
                    bit_depth = "10"
                else:
                    bit_depth = "8"
            metadata_present = "true"
        except Exception:
            pass

    target_path = ""
    alignment_verified = "false"
    split = "train"
    license_note = "SIDD Terms of Use (academic research); CVPR 2018 / SIDD-CC 2026"

    if type_str == "NOISY":
        representation = "bayer_raw_noisy_normalized_unclipped_float32"
        gt_file = path.parent / f"{fn_parts[0]}_GT_RAW_{idx_str}.MAT"
        if gt_file.exists():
            target_path = gt_file.relative_to(root).as_posix()
            alignment_verified = "true"
        if HAS_SCIPY:
            try:
                arr = scipy.io.loadmat(path)["data"]
                height, width = str(arr.shape[0]), str(arr.shape[1])
            except Exception:
                pass
    elif type_str == "GT":
        representation = "bayer_raw_groundtruth_normalized_unclipped_float32"
        alignment_verified = "true"
        if HAS_SCIPY:
            try:
                arr = scipy.io.loadmat(path)["data"]
                height, width = str(arr.shape[0]), str(arr.shape[1])
            except Exception:
                pass
    elif type_str == "METADATA":
        representation = "capture_metadata_mat"
        metadata_present = "true"
    else:
        return None

    return {
        "path": path.relative_to(root).as_posix(),
        "sample_id": sample_id,
        "capture_id": capture_id,
        "scene_id": scene_id,
        "camera": camera,
        "iso": iso,
        "cfa": cfa,
        "width": width,
        "height": height,
        "bit_depth": bit_depth,
        "representation": representation,
        "metadata_present": metadata_present,
        "target_path": target_path,
        "alignment_verified": alignment_verified,
        "split": split,
        "license_note": license_note,
        "sha256": sha256_file(path),
    }


def inventory(root: Path) -> list[dict[str, str]]:
    rows = []
    for path in files_under(root):
        sidd_record = parse_sidd_record(path, root)
        if sidd_record is not None:
            rows.append(sidd_record)
        else:
            rows.append({
                "path": path.relative_to(root).as_posix(),
                "sample_id": "", "capture_id": "", "scene_id": "", "camera": "",
                "iso": "", "cfa": "", "width": "", "height": "", "bit_depth": "",
                "representation": classify(path), "metadata_present": "unknown",
                "target_path": "", "alignment_verified": "false", "split": "unassigned",
                "license_note": "unknown; record source/version/license before use",
                "sha256": sha256_file(path),
            })
    return rows


def write_csv(rows: list[dict[str, str]], output: Path) -> None:
    output.parent.mkdir(parents=True, exist_ok=True)
    with output.open("w", newline="", encoding="utf-8") as stream:
        writer = csv.DictWriter(stream, fieldnames=FIELDS)
        writer.writeheader()
        writer.writerows(rows)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("root", type=Path, help="dataset directory to inventory")
    parser.add_argument("--output", type=Path, default=Path("data/manifests/sidd_inventory.csv"))
    args = parser.parse_args()
    if not args.root.is_dir():
        parser.error("root must be an existing directory")
    rows = inventory(args.root.resolve())
    write_csv(rows, args.output)
    summary = {"root": str(args.root.resolve()), "files": len(rows), "output": str(args.output)}
    print(json.dumps(summary, indent=2))


if __name__ == "__main__":
    main()