Datasets:
Upload /process_causaldynamics.py with huggingface_hub
Browse files- process_causaldynamics.py +49 -0
process_causaldynamics.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
import tarfile
|
| 4 |
+
|
| 5 |
+
def extract_archives(shards_dir: str, target_base: str):
|
| 6 |
+
"""
|
| 7 |
+
Extract all .tar.gz archives in `shards_dir` into `target_base`, preserving internal paths.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
shards_dir (str): Directory containing .tar.gz files (e.g., "inputs" or "outputs").
|
| 11 |
+
target_base (str): Base directory to extract archives into (e.g., "data").
|
| 12 |
+
"""
|
| 13 |
+
if not os.path.isdir(shards_dir):
|
| 14 |
+
print(f"Directory '{shards_dir}' not found. Skipping.")
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
archives = sorted(glob.glob(os.path.join(shards_dir, "*.tar.gz")))
|
| 18 |
+
if not archives:
|
| 19 |
+
print(f"No archives found in '{shards_dir}'.")
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
for arch in archives:
|
| 23 |
+
print(f"Extracting '{arch}' into '{target_base}'...")
|
| 24 |
+
with tarfile.open(arch, 'r:gz') as tar:
|
| 25 |
+
tar.extractall(path=target_base)
|
| 26 |
+
print(f"Done extracting archives from '{shards_dir}'.\n")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
"""
|
| 31 |
+
Download and extract CausalDynamics data from HuggingFace
|
| 32 |
+
|
| 33 |
+
Example usage:
|
| 34 |
+
python process_causaldynamics.py
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
# Base directory for downloaded shards
|
| 38 |
+
base_dir = os.getcwd()
|
| 39 |
+
# Directory to place extracted files
|
| 40 |
+
target_base = os.path.join(base_dir, "data")
|
| 41 |
+
os.makedirs(target_base, exist_ok=True)
|
| 42 |
+
|
| 43 |
+
# Extract data tarballs (inputs)
|
| 44 |
+
extract_archives(os.path.join(base_dir, "inputs"), target_base)
|
| 45 |
+
# Extract eval tarballs (outputs)
|
| 46 |
+
extract_archives(os.path.join(base_dir, "outputs"), target_base)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
main()
|