|
import os |
|
import glob |
|
import tarfile |
|
|
|
def extract_archives(shards_dir: str, target_base: str): |
|
""" |
|
Extract all .tar.gz archives in `shards_dir` into `target_base`, preserving internal paths. |
|
|
|
Args: |
|
shards_dir (str): Directory containing .tar.gz files (e.g., "inputs" or "outputs"). |
|
target_base (str): Base directory to extract archives into (e.g., "data"). |
|
""" |
|
if not os.path.isdir(shards_dir): |
|
print(f"Directory '{shards_dir}' not found. Skipping.") |
|
return |
|
|
|
archives = sorted(glob.glob(os.path.join(shards_dir, "*.tar.gz"))) |
|
if not archives: |
|
print(f"No archives found in '{shards_dir}'.") |
|
return |
|
|
|
for arch in archives: |
|
print(f"Extracting '{arch}' into '{target_base}'...") |
|
with tarfile.open(arch, 'r:gz') as tar: |
|
tar.extractall(path=target_base) |
|
print(f"Done extracting archives from '{shards_dir}'.\n") |
|
|
|
|
|
def main(): |
|
""" |
|
Download and extract CausalDynamics data from HuggingFace |
|
|
|
Example usage: |
|
python process_causaldynamics.py |
|
""" |
|
|
|
|
|
base_dir = os.getcwd() |
|
|
|
target_base = os.path.join(base_dir, "data") |
|
os.makedirs(target_base, exist_ok=True) |
|
|
|
|
|
extract_archives(os.path.join(base_dir, "inputs"), target_base) |
|
|
|
extract_archives(os.path.join(base_dir, "outputs"), target_base) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|