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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
"""
This utils script contains PORTAGE of wai-core core methods for MapAnything.
"""

import logging
import re
from pathlib import Path
from typing import Any

import numpy as np
import torch

from mapanything.utils.wai.camera import (
    CAMERA_KEYS,
    convert_camera_coeffs_to_pinhole_matrix,
    interpolate_extrinsics,
    interpolate_intrinsics,
)
from mapanything.utils.wai.io import _get_method, _load_scene_meta
from mapanything.utils.wai.ops import crop

logger = logging.getLogger(__name__)

WAI_COLORMAP_PATH = Path(__file__).parent / "colormaps"


def load_data(fname: str | Path, format_type: str | None = None, **kwargs) -> Any:
    """
    Loads data from a file using the appropriate method based on the file format.

    Args:
        fname (str or Path): The filename or path to load data from.
        format_type (str, optional): The format type of the data. If None, it will be inferred from the file extension if possible.
            Supported formats include: 'readable', 'scalar', 'image', 'binary', 'depth', 'normals',
            'numpy', 'ptz', 'mmap', 'scene_meta', 'labeled_image', 'mesh', 'labeled_mesh', 'caption', "latents".
        **kwargs: Additional keyword arguments to pass to the loading method.

    Returns:
        The loaded data in the format returned by the specific loading method.

    Raises:
        ValueError: If the format cannot be inferred from the file extension.
        NotImplementedError: If the specified format is not supported.
        FileExistsError: If the file does not exist.
    """
    load_method = _get_method(fname, format_type, load=True)
    return load_method(fname, **kwargs)


def store_data(
    fname: str | Path,
    data: Any,
    format_type: str | None = None,
    **kwargs,
) -> Any:
    """
    Stores data to a file using the appropriate method based on the file format.

    Args:
        fname (str or Path): The filename or path to store data to.
        data: The data to be stored.
        format_type (str, optional): The format type of the data. If None, it will be inferred from the file extension.
        **kwargs: Additional keyword arguments to pass to the storing method.

    Returns:
        The result of the storing method, which may vary depending on the method used.
    """
    store_method = _get_method(fname, format_type, load=False)
    Path(fname).parent.mkdir(parents=True, exist_ok=True)
    return store_method(fname, data, **kwargs)


def get_frame(
    scene_meta: dict[str, Any],
    frame_key: int | str | float,
) -> dict[str, Any]:
    """
    Get a frame from scene_meta based on name or index.

    Args:
        scene_meta: Dictionary containing scene metadata
        frame_key: Either a string (frame name) or integer (frame index) or float (video timestamp)

    Returns:
        The frame data (dict)
    """
    frame_idx = get_frame_index(scene_meta, frame_key)
    if isinstance(frame_idx, int):
        frame = scene_meta["frames"][frame_idx]
        frame["_is_interpolated"] = False
    else:
        frame = {}
        frame["frame_name"] = frame_key
        left = int(frame_idx)  # it's floor operation
        assert left >= 0 and left < (len(scene_meta["frames"]) - 1), "Wrong index"
        frame_left = scene_meta["frames"][left]
        frame_right = scene_meta["frames"][left + 1]
        # Interpolate intrinsics and extrinsics
        frame["transform_matrix"] = interpolate_extrinsics(
            frame_left["transform_matrix"],
            frame_right["transform_matrix"],
            frame_idx - left,
        )
        frame.update(
            interpolate_intrinsics(
                frame_left,
                frame_right,
                frame_idx - left,
            )
        )
        frame["_is_interpolated"] = True
    return frame


def get_intrinsics(
    scene_meta,
    frame_key,
    fmt: str = "torch",
) -> torch.Tensor | np.ndarray | list:
    frame = get_frame(scene_meta, frame_key)
    return convert_camera_coeffs_to_pinhole_matrix(scene_meta, frame, fmt=fmt)


def get_extrinsics(
    scene_meta,
    frame_key,
    fmt: str = "torch",
) -> torch.Tensor | np.ndarray | list | None:
    frame = get_frame(scene_meta, frame_key)
    if "transform_matrix" in frame:
        if fmt == "torch":
            return torch.tensor(frame["transform_matrix"]).reshape(4, 4).float()
        elif fmt == "np":
            return np.array(frame["transform_matrix"]).reshape(4, 4)
        return frame["transform_matrix"]
    else:
        # TODO: should not happen if we enable interpolation
        return None


def get_frame_index(
    scene_meta: dict[str, Any],
    frame_key: int | str | float,
    frame_index_threshold_sec: float = 1e-4,
    distance_threshold_sec: float = 2.0,
) -> int | float:
    """
    Returns the frame index from scene_meta based on name (str) or index (int) or sub-frame index (float).

    Args:
        scene_meta: Dictionary containing scene metadata
        frame_key: Either a string (frame name) or integer (frame index) or float (sub-frame index)
        frame_index_threshold_sec: A threshold for nearest neighbor clipping for indexes (in seconds).
                                   Default is 1e-4, which is 10000 fps.
        distance_th: A threshold for maximum distance between interpolated frames (in seconds).

    Returns:
        Frame index (int)

    Raises:
        ValueError: If frame_key is not a string or integer or float
    """
    if isinstance(frame_key, str):
        try:
            return scene_meta["frame_names"][frame_key]
        except KeyError as err:
            error_message = (
                f"Frame name not found: {frame_key} - "
                f"Please verify scene_meta.json of scene: {scene_meta['dataset_name']}/{scene_meta['scene_name']}"
            )
            logger.error(error_message)
            raise KeyError(error_message) from err

    if isinstance(frame_key, int):
        return frame_key

    if isinstance(frame_key, float):
        # If exact hit
        if frame_key in scene_meta["frame_names"]:
            return scene_meta["frame_names"][frame_key]

        frame_names = sorted(list(scene_meta["frame_names"].keys()))
        distances = np.array([frm - frame_key for frm in frame_names])
        left = int(np.nonzero(distances <= 0)[0][-1])
        right = left + 1

        # The last frame or rounding errors
        if (
            left == distances.shape[0] - 1
            or abs(distances[left]) < frame_index_threshold_sec
        ):
            return scene_meta["frame_names"][frame_names[int(left)]]
        if abs(distances[right]) < frame_index_threshold_sec:
            return scene_meta["frame_names"][frame_names[int(right)]]

        interpolation_distance = distances[right] - distances[left]
        if interpolation_distance > distance_threshold_sec:
            raise ValueError(
                f"Frame interpolation is forbidden for distances larger than {distance_threshold_sec}."
            )
        alpha = -distances[left] / interpolation_distance

        return scene_meta["frame_names"][frame_names[int(left)]] + alpha

    raise ValueError(f"Frame key type not supported: {frame_key} ({type(frame_key)}).")


def load_modality_data(
    scene_root: Path | str,
    results: dict[str, Any],
    modality_dict: dict[str, Any],
    modality: str,
    frame: dict[str, Any] | None = None,
    fmt: str = "torch",
) -> dict[str, Any]:
    """
    Processes a modality by loading data from a specified path and updating the results dictionary.
    This function extracts the format and path from the given modality dictionary, loads the data
    from the specified path, and updates the results dictionary with the loaded data.

    Args:
        scene_root (str or Path): The root directory of the scene where the data is located.
        results (dict): A dictionary to store the loaded modality data and optional frame path.
        modality_dict (dict): A dictionary containing the modality information, including 'format'
            and the path to the data.
        modality (str): The key under which the loaded modality data will be stored in the results.
        frame (dict, optional): A dictionary containing frame information. If provided, that means we are loading
        frame modalities, otherwise it is scene modalities.

    Returns:
        dict: The updated results dictionary containing the loaded modality data.
    """
    modality_format = modality_dict["format"]

    # The modality is stored as a video
    if "video" in modality_format:
        assert isinstance(frame["frame_name"], float), "frame_name should be float"
        video_file = None
        if "chunks" in modality_dict:
            video_list = modality_dict["chunks"]
            # Get the correct chunk of the video
            for video_chunk in video_list:
                if video_chunk["start"] <= frame["frame_name"] <= video_chunk["end"]:
                    video_file = video_chunk
                    break
        else:
            # There is only one video (no chunks)
            video_file = modality_dict
            if "start" not in video_file:
                video_file["start"] = 0
            if "end" not in video_file:
                video_file["end"] = float("inf")
            if not (video_file["start"] <= frame["frame_name"] <= video_file["end"]):
                video_file = None

        # This timestamp is not available in any of the chunks
        if video_file is None:
            frame_name = frame["frame_name"]
            logger.warning(
                f"Modality {modality} ({modality_format}) is not available at time {frame_name}"
            )
            return results

        # Load the modality from the video
        loaded_modality = load_data(
            Path(scene_root, video_file["file"]),
            modality_format,
            frame_key=frame["frame_name"] - video_file["start"],
        )

        if "bbox" in video_file:
            loaded_modality = crop(loaded_modality, video_file["bbox"])

        if loaded_modality is not None:
            results[modality] = loaded_modality

        if frame:
            results[f"{modality}_fname"] = video_file["file"]
    else:
        modality_path = [v for k, v in modality_dict.items() if k != "format"][0]
        if frame:
            if modality_path in frame:
                fname = frame[modality_path]
            else:
                fname = None
        else:
            fname = modality_path
        if fname is not None:
            loaded_modality = load_data(
                Path(scene_root, fname),
                modality_format,
                frame_key=frame["frame_name"] if frame else None,
                fmt=fmt,
            )
            results[modality] = loaded_modality
            if frame:
                results[f"{modality}_fname"] = frame[modality_path]
    return results


def load_modality(
    scene_root: Path | str,
    modality_meta: dict[str, Any],
    modality: str,
    frame: dict[str, Any] | None = None,
    fmt: str = "torch",
) -> dict[str, Any]:
    """
    Loads modality data based on the provided metadata and updates the results dictionary.
    This function navigates through the modality metadata to find the specified modality,
    then loads the data for each modality found.

    Args:
        scene_root (str or Path): The root directory of the scene where the data is located.
        modality_meta (dict): A nested dictionary containing metadata for various modalities.
        modality (str): A string representing the path to the desired modality within the metadata,
            using '/' as a separator for nested keys.
        frame (dict, optional): A dictionary containing frame information. If provided, we are operating
        on frame modalities, otherwise it is scene modalities.

    Returns:
        dict: A dictionary containing the loaded modality data.
    """
    results = {}
    # support for nested modalities like "pred_depth/metric3dv2"
    modality_keys = modality.split("/")
    current_modality = modality_meta
    for key in modality_keys:
        try:
            current_modality = current_modality[key]
        except KeyError as err:
            error_message = (
                f"Modality '{err.args[0]}' not found in modalities metadata. "
                f"Please verify the scene_meta.json and the provided modalities in {scene_root}."
            )
            logger.error(error_message)
            raise KeyError(error_message) from err
    if "format" in current_modality:
        results = load_modality_data(
            scene_root, results, current_modality, modality, frame, fmt=fmt
        )
    else:
        # nested modality, return last by default
        logger.warning("Nested modality, returning last by default")
        key = next(reversed(current_modality.keys()))
        results = load_modality_data(
            scene_root, results, current_modality[key], modality, frame, fmt=fmt
        )
    return results


def load_frame(
    scene_root: Path | str,
    frame_key: int | str | float,
    modalities: str | list[str] | None = None,
    scene_meta: dict[str, Any] | None = None,
    load_intrinsics: bool = True,
    load_extrinsics: bool = True,
    fmt: str = "torch",
    interpolate: bool = False,
) -> dict[str, Any]:
    """
    Load a single frame from a scene with specified modalities.

    Args:
        scene_root (str or Path): The root directory of the scene where the data is located.
        frame_key (int or str or float): Either a string (frame name) or integer (frame index) or float (video timestamp).
        modalities (str or list[str], optional): The modality or list of modalities to load.
            If None, only basic frame information is loaded.
        scene_meta (dict, optional): Dictionary containing scene metadata. If None, it will be loaded
            from scene_meta.json in the scene_root.
        interpolate (bool, optional): Allow interpolating frames?

    Returns:
        dict: A dictionary containing the loaded frame data with the requested modalities.
    """
    scene_root = Path(scene_root)
    if scene_meta is None:
        scene_meta = _load_scene_meta(scene_root / "scene_meta.json")
    frame = get_frame(scene_meta, frame_key)
    # compact, standarized frame representation
    wai_frame = {}
    if load_extrinsics:
        extrinsics = get_extrinsics(
            scene_meta,
            frame_key,
            fmt=fmt,
        )
        if extrinsics is not None:
            wai_frame["extrinsics"] = extrinsics
    if load_intrinsics:
        camera_model = frame.get("camera_model", scene_meta.get("camera_model"))
        wai_frame["camera_model"] = camera_model
        if camera_model == "PINHOLE":
            wai_frame["intrinsics"] = get_intrinsics(scene_meta, frame_key, fmt=fmt)
        elif camera_model in ["OPENCV", "OPENCV_FISHEYE"]:
            # optional per-frame intrinsics
            for camera_key in CAMERA_KEYS:
                if camera_key in frame:
                    wai_frame[camera_key] = float(frame[camera_key])
                elif camera_key in scene_meta:
                    wai_frame[camera_key] = float(scene_meta[camera_key])
        else:
            error_message = (
                f"Camera model not supported: {camera_model} - "
                f"Please verify scene_meta.json of scene: {scene_meta['dataset_name']}/{scene_meta['scene_name']}"
            )
            logger.error(error_message)
            raise NotImplementedError(error_message)
    wai_frame["w"] = frame.get("w", scene_meta["w"] if "w" in scene_meta else None)
    wai_frame["h"] = frame.get("h", scene_meta["h"] if "h" in scene_meta else None)
    wai_frame["frame_name"] = frame["frame_name"]
    wai_frame["frame_idx"] = get_frame_index(scene_meta, frame_key)
    wai_frame["_is_interpolated"] = frame["_is_interpolated"]

    if modalities is not None:
        if isinstance(modalities, str):
            modalities = [modalities]
        for modality in modalities:
            # Handle regex patterns in modality
            if any(char in modality for char in ".|*+?()[]{}^$\\"):
                # This is a regex pattern
                pattern = re.compile(modality)
                matching_modalities = [
                    m for m in scene_meta["frame_modalities"] if pattern.match(m)
                ]
                if not matching_modalities:
                    raise ValueError(
                        f"No modalities match the pattern: {modality} in scene: {scene_root}"
                    )
                # Use the first matching modality
                modality = matching_modalities[0]
            current_modalities = load_modality(
                scene_root, scene_meta["frame_modalities"], modality, frame, fmt=fmt
            )
            wai_frame.update(current_modalities)

    return wai_frame


def set_frame(
    scene_meta: dict[str, Any],
    frame_key: int | str,
    new_frame: dict[str, Any],
    sort: bool = False,
) -> dict[str, Any]:
    """
    Replace a frame in scene_meta with a new frame.

    Args:
        scene_meta: Dictionary containing scene metadata.
        frame_key: Either a string (frame name) or integer (frame index).
        new_frame: New frame data to replace the existing frame.
        sort: If True, sort the keys in the new_frame dictionary.

    Returns:
        Updated scene_meta dictionary.
    """
    frame_idx = get_frame_index(scene_meta, frame_key)
    if isinstance(frame_idx, float):
        raise ValueError(
            f"Setting frame for sub-frame frame_key is not supported: {frame_key} ({type(frame_key)})."
        )
    if sort:
        new_frame = {k: new_frame[k] for k in sorted(new_frame)}
    scene_meta["frames"][frame_idx] = new_frame
    return scene_meta


def nest_modality(
    frame_modalities: dict[str, Any],
    modality_name: str,
) -> dict[str, Any]:
    """
    Converts a flat modality structure into a nested one based on the modality name.

    Args:
        frame_modalities (dict): Dictionary containing frame modalities.
        modality_name (str): The name of the modality to nest.

    Returns:
        dict: A dictionary with the nested modality structure.
    """
    frame_modality = {}
    if modality_name in frame_modalities:
        frame_modality = frame_modalities[modality_name]
        if "frame_key" in frame_modality:
            # required for backwards compatibility
            # converting non-nested format into nested one based on name
            modality_name = frame_modality["frame_key"].split("_")[0]
            frame_modality = {modality_name: frame_modality}
    return frame_modality