File size: 2,979 Bytes
a4ba047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check that each file of the hmc/reference folder falls under exactly one licence row.

The rows are the rows of the README table "Licences", in the same order. Each row is a
list of regular expressions on the path relative to the folder (full match). The script
prints the file count of each row and fails when a file matches no row or more than one.

Usage: python -I check_licence_rows.py REFERENCE_DIR
"""

import re
import sys
from pathlib import Path

RUNS_Q = r"qsiprep-(?:r1|e1|e2|e3)"
ROWS = {
    "CC0: ds005577 input files": [
        r"input/(?!input-facts\.json$)[^/]+",
    ],
    "MIT: MindGrab mask": [
        r"mask/sub-107_dir-AP_desc-mindgrab_mask\.nii\.gz",
    ],
    "BSD-3-Clause: QSIPrep outputs": [
        r"qsiprep/input/split_dwis/vol\.\d{4}\.nii\.gz",
        rf"{RUNS_Q}/qsiprep\.log\.gz",
        rf"{RUNS_Q}/images/[^/]+",
        rf"{RUNS_Q}/out/[^/]+",
        (
            rf"{RUNS_Q}/collected/(?!manifest-host-paths\.json$)"
            r"(?!mask-dice-vs-mindgrab\.json$).+"
        ),
    ],
    "Apache-2.0: NiFreeze and ANTs outputs": [
        r"nifreeze-[^/]+/work/[^/]+",
        r"nifreeze-[^/]+/out/[^/]+",
        r"nifreeze-[^/]+/nifreeze\.log",
        r"nifreeze-gp/bzero\.nii\.gz",
    ],
    "Apache-2.0: NiFreeze patch": [
        r"patches/nifreeze-keepdir-float\.diff",
    ],
    "BSD-3-Clause: scikit-learn patch": [
        r"patches/sklearn-1\.9\.1-gpr-gradient-memory\.diff",
    ],
    "Apache-2.0: scripts and the files they wrote": [
        r"(?:scripts|compare|qc|q53-b0|env)/.+",
        r"qsiprep/spread-[^/]+\.json",
        r"(?:input|mask)/[^/]+-facts\.json",
        r"mask/sub-107_dir-AP_desc-meanb0_dwiref\.nii\.gz",
        r"mask/mask-overlay\.png",
        (
            r"nifreeze-[^/]+/(?:command\.txt|timing\.txt|motion\.tsv|motion_affines\.tsv"
            r"|motion-check\.json|clip-facts\.json)"
        ),
        rf"{RUNS_Q}/(?:command\.txt|timing\.txt)",
        rf"{RUNS_Q}/collected/(?:manifest-host-paths|mask-dice-vs-mindgrab)\.json",
        r"README\.md|SHA256SUMS|index\.json",
    ],
}
COMPILED = {k: [re.compile(p) for p in v] for k, v in ROWS.items()}


def rows_of(path: str) -> list[str]:
    return [k for k, ps in COMPILED.items() if any(p.fullmatch(path) for p in ps)]


def main(root: Path) -> int:
    files = sorted(str(p.relative_to(root)) for p in root.rglob("*") if p.is_file())
    count = dict.fromkeys(ROWS, 0)
    bad = []
    for f in files:
        hit = rows_of(f)
        if len(hit) != 1:
            bad.append((f, hit))
            continue
        count[hit[0]] += 1
    for k, n in count.items():
        print(f"{n:5d}  {k}")
    print(f"{len(files):5d}  files")
    for f, hit in bad:
        print(f"FAIL {f}: {len(hit)} rows {hit}")
    empty = [k for k, n in count.items() if n == 0]
    for k in empty:
        print(f"FAIL row with no file: {k}")
    return 1 if bad or empty else 0


if __name__ == "__main__":
    sys.exit(main(Path(sys.argv[1])))