Download scripts/copy_tcga_cache.py from raylim/mosaic: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/raylim/mosaic/resolve/main/scripts/copy_tcga_cache.py
- Command line
-
hf download hf://spaces/raylim/mosaic/scripts/copy_tcga_cache.py
-
curl -L -o copy_tcga_cache.py https://huggingface.co/spaces/raylim/mosaic/resolve/main/scripts/copy_tcga_cache.py
2.78 kB
| #!/usr/bin/env python3 | |
| """Copy TCGA cached results between HF dataset caches (e.g. staging -> production). | |
| uv run python scripts/copy_tcga_cache.py \\ | |
| --src raylim/mosaic-tcga-cache-staging --dst PDM-Group/mosaic-tcga-cache \\ | |
| TCGA-86-8074-01Z-00-DX1 ... # barcodes or file UUIDs (default: UI examples) | |
| Only adds files that are missing in the destination; never overwrites. Result | |
| folders are keyed by a hash of the settings, cache version and pinned model | |
| revisions, so results computed by the same code on staging are exactly what | |
| production looks up. metadata.json is skipped when the destination already | |
| has one (the app refreshes stale metadata itself). | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import os | |
| import sys | |
| import tempfile | |
| from pathlib import Path | |
| from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from warm_tcga_examples import EXAMPLE_SLIDES # noqa: E402 | |
| def main(argv=None) -> int: | |
| parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) | |
| parser.add_argument("--src", required=True) | |
| parser.add_argument("--dst", required=True) | |
| parser.add_argument("--dry-run", action="store_true") | |
| parser.add_argument("slides", nargs="*", default=EXAMPLE_SLIDES) | |
| args = parser.parse_args(argv) | |
| from mosaic.tcga import is_valid_uuid, query_slide_uuid | |
| api = HfApi(token=os.environ.get("HF_TOKEN")) | |
| src_files = set(api.list_repo_files(args.src, repo_type="dataset")) | |
| dst_files = set(api.list_repo_files(args.dst, repo_type="dataset")) | |
| to_copy = [] | |
| for slide in args.slides: | |
| uuid = slide if is_valid_uuid(slide) else query_slide_uuid(slide) | |
| mine = sorted(f for f in src_files if f.startswith(f"{uuid}/")) | |
| new = [f for f in mine if f not in dst_files] # never overwrite | |
| print(f"{slide} ({uuid}): {len(mine)} files on src, {len(new)} new") | |
| to_copy += new | |
| if not to_copy: | |
| print("Nothing to copy.") | |
| return 0 | |
| for f in to_copy: | |
| print(" +", f) | |
| if args.dry_run: | |
| return 0 | |
| with tempfile.TemporaryDirectory() as tmp: | |
| ops = [ | |
| CommitOperationAdd( | |
| path_in_repo=f, | |
| path_or_fileobj=hf_hub_download( | |
| args.src, f, repo_type="dataset", local_dir=tmp | |
| ), | |
| ) | |
| for f in to_copy | |
| ] | |
| api.create_commit( | |
| args.dst, | |
| repo_type="dataset", | |
| operations=ops, | |
| commit_message=f"Copy {len(ops)} cached result files from {args.src}", | |
| ) | |
| print(f"Copied {len(to_copy)} files to {args.dst}") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |