bilalahmad176176 commited on
Commit
4bd15dd
·
verified ·
1 Parent(s): ada0be5

Upload scripts/upload_hf_dataset.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/upload_hf_dataset.py +158 -0
scripts/upload_hf_dataset.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Upload Golden-0-to-25 + Golden-25plus raw NIfTI datasets to Hugging Face.
2
+
3
+ Uploads manifests first, then NIfTI files folder-by-folder with progress.
4
+ Resumable: HF Hub skips files already uploaded (content-addressed by SHA).
5
+ """
6
+ from __future__ import annotations
7
+ import os, sys, time
8
+ from pathlib import Path
9
+ from huggingface_hub import HfApi, CommitOperationAdd
10
+
11
+ TOKEN = "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"
12
+ REPO_ID = "bilalahmad176176/BrainAge-Golden-Raw"
13
+ REPO_TYPE = "dataset"
14
+
15
+ SPLITS = [
16
+ Path("/home/MRI-DataSet/Golden-0-to-25"),
17
+ Path("/home/MRI-DataSet/Golden-25plus"),
18
+ ]
19
+
20
+ BATCH_SIZE = 80 # files per commit (keeps commit payloads manageable)
21
+
22
+
23
+ def collect_files(root: Path) -> list[tuple[Path, str]]:
24
+ """Return (local_path, repo_path) pairs for all files under root."""
25
+ pairs = []
26
+ for p in sorted(root.rglob("*")):
27
+ if p.is_file():
28
+ rel = p.relative_to(root.parent)
29
+ pairs.append((p, str(rel)))
30
+ return pairs
31
+
32
+
33
+ def upload_split(api: HfApi, root: Path):
34
+ pairs = collect_files(root)
35
+ total = len(pairs)
36
+ print(f"\n{'='*60}")
37
+ print(f"Uploading {root.name}: {total} files")
38
+ print(f"{'='*60}")
39
+
40
+ for i in range(0, total, BATCH_SIZE):
41
+ batch = pairs[i : i + BATCH_SIZE]
42
+ ops = []
43
+ for local, repo_path in batch:
44
+ ops.append(CommitOperationAdd(
45
+ path_in_repo=repo_path,
46
+ path_or_fileobj=str(local),
47
+ ))
48
+ n = min(i + BATCH_SIZE, total)
49
+ msg = f"Add {root.name} files {i+1}–{n} of {total}"
50
+ print(f" [{n}/{total}] committing batch … ", end="", flush=True)
51
+ t0 = time.time()
52
+ try:
53
+ api.create_commit(
54
+ repo_id=REPO_ID,
55
+ repo_type=REPO_TYPE,
56
+ operations=ops,
57
+ commit_message=msg,
58
+ )
59
+ print(f"done ({time.time()-t0:.0f}s)")
60
+ except Exception as e:
61
+ print(f"ERROR: {e}")
62
+ print(" (will retry this batch once)")
63
+ time.sleep(5)
64
+ try:
65
+ api.create_commit(
66
+ repo_id=REPO_ID,
67
+ repo_type=REPO_TYPE,
68
+ operations=ops,
69
+ commit_message=msg + " (retry)",
70
+ )
71
+ print(f" retry succeeded")
72
+ except Exception as e2:
73
+ print(f" retry also failed: {e2}")
74
+ print(f" skipping batch, re-run script to resume.")
75
+
76
+
77
+ def upload_readme(api: HfApi):
78
+ readme = """---
79
+ license: cc-by-nc-4.0
80
+ task_categories:
81
+ - image-classification
82
+ - other
83
+ task_ids:
84
+ - brain-age-prediction
85
+ language:
86
+ - en
87
+ pretty_name: BrainAge Golden Raw MRI Dataset
88
+ size_categories:
89
+ - 1K<n<10K
90
+ tags:
91
+ - neuroimaging
92
+ - mri
93
+ - brain-age
94
+ - t1w
95
+ - nifti
96
+ - pediatric
97
+ - adult
98
+ ---
99
+
100
+ # BrainAge Golden Raw MRI Dataset
101
+
102
+ Curated collection of **6,152 healthy-brain T1-weighted MRI scans** spanning
103
+ ages 0–86 years, assembled from 12 public neuroimaging datasets.
104
+
105
+ ## Splits
106
+
107
+ | Split | Subjects | Age range | Size |
108
+ |-------|----------|-----------|------|
109
+ | `Golden-0-to-25/` | 4,782 | 0 – 25 y | ~42 GB |
110
+ | `Golden-25plus/` | 1,370 | 25 – 86 y | ~13 GB |
111
+
112
+ ## Source datasets
113
+
114
+ BCP, Calgary, ds002726, ds000248, PTBP, IXI, MPI-Leipzig,
115
+ AOMIC-ID1000, NKI-Rockland, ABIDE-I, ABIDE-II, ADHD-200.
116
+
117
+ ## File format
118
+
119
+ Each scan is a `.nii.gz` NIfTI file (native space, T1w).
120
+ Manifests (`manifest.csv`) list subject IDs, dataset of origin,
121
+ chronological age, sex, split, and file paths.
122
+
123
+ ## Intended use
124
+
125
+ Training and evaluating brain-age prediction models on healthy controls.
126
+
127
+ ## Citation
128
+
129
+ If you use this dataset, please cite the original source studies
130
+ (listed in each manifest row under the `dataset` column).
131
+ """
132
+ api.upload_file(
133
+ path_or_fileobj=readme.encode(),
134
+ path_in_repo="README.md",
135
+ repo_id=REPO_ID,
136
+ repo_type=REPO_TYPE,
137
+ commit_message="Add dataset card",
138
+ )
139
+ print("README.md uploaded.")
140
+
141
+
142
+ def main():
143
+ api = HfApi(token=TOKEN)
144
+ print(f"Authenticated as: {api.whoami()['name']}")
145
+ print(f"Target repo: https://huggingface.co/datasets/{REPO_ID}")
146
+
147
+ upload_readme(api)
148
+
149
+ for split_dir in SPLITS:
150
+ upload_split(api, split_dir)
151
+
152
+ print(f"\n{'='*60}")
153
+ print(f"DONE — https://huggingface.co/datasets/{REPO_ID}")
154
+ print(f"{'='*60}")
155
+
156
+
157
+ if __name__ == "__main__":
158
+ main()