File size: 3,300 Bytes
18877ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7bc2d8
 
 
 
 
 
 
 
18877ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7bc2d8
 
 
 
 
18877ac
 
c7bc2d8
 
18877ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path
from typing import Dict, Iterator, List, Tuple


COMPLETE_MARKER_NAME = ".complete"


def dataset_dir_for_video(
    video_path: Path,
    home_raw_dir: Path,
    scratch_raw_dir: Path | None,
    home_dataset_dir: Path,
    scratch_dataset_dir: Path | None,
) -> Path:
    if scratch_raw_dir is not None and scratch_dataset_dir is not None:
        try:
            if video_path.parent.resolve() == scratch_raw_dir.resolve():
                return scratch_dataset_dir
        except FileNotFoundError:
            pass
    return home_dataset_dir


def iter_dataset_video_dirs(home_dataset_dir: Path, scratch_dataset_dir: Path | None = None) -> Iterator[Tuple[str, Path]]:
    seen: Dict[str, Path] = {}
    for dataset_dir in [home_dataset_dir, scratch_dataset_dir]:
        if dataset_dir is None or not dataset_dir.exists():
            continue
        for folder_path in sorted(dataset_dir.iterdir()):
            if not folder_path.is_dir():
                continue
            existing = seen.get(folder_path.name)
            if existing is None:
                seen[folder_path.name] = folder_path
                continue
            existing_complete = (existing / "npz" / COMPLETE_MARKER_NAME).exists()
            candidate_complete = (folder_path / "npz" / COMPLETE_MARKER_NAME).exists()
            if candidate_complete and not existing_complete:
                seen[folder_path.name] = folder_path
    for video_id, folder_path in sorted(seen.items()):
        yield video_id, folder_path


def complete_video_ids(home_dataset_dir: Path, scratch_dataset_dir: Path | None = None) -> set[str]:
    complete: set[str] = set()
    for video_id, folder_path in iter_dataset_video_dirs(home_dataset_dir, scratch_dataset_dir):
        if (folder_path / "npz" / COMPLETE_MARKER_NAME).exists():
            complete.add(video_id)
    return complete


def count_complete(home_dataset_dir: Path, scratch_dataset_dir: Path | None = None) -> int:
    return len(complete_video_ids(home_dataset_dir, scratch_dataset_dir))


def find_dataset_video_dir(video_id: str, home_dataset_dir: Path, scratch_dataset_dir: Path | None = None) -> Path:
    home_path = home_dataset_dir / video_id
    scratch_path = scratch_dataset_dir / video_id if scratch_dataset_dir is not None else None
    home_complete = (home_path / "npz" / COMPLETE_MARKER_NAME).exists()
    scratch_complete = bool(scratch_path is not None and (scratch_path / "npz" / COMPLETE_MARKER_NAME).exists())
    if scratch_complete and not home_complete:
        return scratch_path
    if home_path.exists():
        return home_path
    if scratch_path is not None and scratch_path.exists():
        return scratch_path
    return home_path


def list_unuploaded_folder_paths(
    home_dataset_dir: Path,
    scratch_dataset_dir: Path | None,
    uploaded_folders: Dict[str, object],
) -> List[Tuple[str, Path]]:
    folders: List[Tuple[str, Path]] = []
    for video_id, folder_path in iter_dataset_video_dirs(home_dataset_dir, scratch_dataset_dir):
        if video_id in uploaded_folders:
            continue
        if not (folder_path / "npz" / COMPLETE_MARKER_NAME).exists():
            continue
        folders.append((video_id, folder_path))
    return folders