File size: 4,091 Bytes
bc88015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pin official source metadata without bulk acquisition."""

from __future__ import annotations

import json
from pathlib import Path

import requests

DANDI_COHORTS = {
    "000541": "EY",
    "000692": "KK",
    "000565": "SK1",
    "000472": "SK2",
    "000776": "SF",
    "000715": "NP",
    "000714": "HL",
    "001623": "Dunn",
    "000981": "Chemosensory",
}
ZENODO = {"17561700": "ASCENT", "10818810": "ZephIR", "10008744": "Targettrack"}
WORMND_COMMIT = "0c74e9ddffee138cfc6330ab65c0e690e8e199dd"


def inventory(workspace):
    """Save release metadata, immutable asset IDs/checksums, and the original split CSV.

    Re-running an inventory captures the current latest upstream release. Assembly
    uses saved snapshots, never silently updates an existing experiment. Original
    data licenses are taken from release metadata, separately from code licenses.
    """
    out = Path(workspace) / "audit"
    out.mkdir(parents=True, exist_ok=True)
    asset_provenance = {}
    for identifier in DANDI_COHORTS:
        base = f"https://api.dandiarchive.org/api/dandisets/{identifier}/versions/"
        versions = requests.get(base, timeout=60)
        versions.raise_for_status()
        versions = versions.json()
        versions = versions.get("results", versions) if isinstance(versions, dict) else versions
        published = [v["version"] for v in versions if v["version"] != "draft"]
        version = max(published) if published else "draft"
        metadata = requests.get(base + version + "/", timeout=60)
        metadata.raise_for_status()
        (out / f"dandi-{identifier}-pinned.json").write_text(json.dumps(metadata.json(), indent=2))
        assets = []
        url = base + version + "/assets/?page_size=1000"
        while url:
            response = requests.get(url, timeout=60)
            response.raise_for_status()
            page = response.json()
            assets.extend(page["results"])
            url = page.get("next")
        (out / f"dandi-{identifier}-assets.json").write_text(
            json.dumps({"version": version, "assets": assets}, indent=2)
        )
        for asset in assets:
            response = requests.get(
                f"https://api.dandiarchive.org/api/assets/{asset['asset_id']}/", timeout=60
            )
            response.raise_for_status()
            value = response.json()
            asset_provenance[asset["asset_id"]] = {
                key: value.get(key)
                for key in ["digest", "wasAttributedTo", "wasGeneratedBy", "datePublished"]
            }
    (out / "dandi-asset-provenance.json").write_text(json.dumps(asset_provenance, indent=2))
    for identifier in ZENODO:
        response = requests.get(f"https://zenodo.org/api/records/{identifier}", timeout=60)
        response.raise_for_status()
        (out / f"zenodo-{identifier}.json").write_text(json.dumps(response.json(), indent=2))
    url = (
        f"https://raw.githubusercontent.com/focolab/WormND/{WORMND_COMMIT}"
        "/neuron_tracking/3deecelltracker/dataset_split.csv"
    )
    response = requests.get(url, timeout=60)
    response.raise_for_status()
    (out / "wormid-tracking-split.csv").write_text(response.text)


def snapshot_hub(workspace, repo_id="pytc/trackingBench"):
    """Record the immutable Hub state before edits; does not publish or expose tokens."""
    from huggingface_hub import HfApi, hf_hub_download

    api = HfApi()
    info = api.dataset_info(repo_id, files_metadata=True)
    readme = Path(
        hf_hub_download(repo_id, "README.md", repo_type="dataset", revision=info.sha)
    ).read_text()
    snapshot = {
        "repo_id": repo_id,
        "sha": info.sha,
        "private": info.private,
        "card": info.card_data.to_dict() if info.card_data else {},
        "readme": readme,
        "files": [
            {"path": f.rfilename, "size": f.size, "blob_id": f.blob_id} for f in info.siblings
        ],
    }
    Path(workspace).mkdir(parents=True, exist_ok=True)
    (Path(workspace) / "hub-before.json").write_text(json.dumps(snapshot, indent=2))
    return snapshot