Datasets:
Upload decompress_useful.py with huggingface_hub
Browse files- decompress_useful.py +71 -0
decompress_useful.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
import subprocess
|
| 4 |
+
import argparse
|
| 5 |
+
|
| 6 |
+
def restore_dataset(input_path="shards", target_path="samples", sensors_to_extract=None):
|
| 7 |
+
"""
|
| 8 |
+
Restores the dataset from shards.
|
| 9 |
+
:param sensors_to_extract: Optional list of strings.
|
| 10 |
+
Example: ["LIDAR_TOP", "CAM_FRONT"]
|
| 11 |
+
"""
|
| 12 |
+
# Ensure the target directory exists
|
| 13 |
+
if not os.path.exists(target_path):
|
| 14 |
+
os.makedirs(target_path)
|
| 15 |
+
|
| 16 |
+
# Find all unique base names (e.g., "LIDAR_TOP.tar" from "LIDAR_TOP.tar.aa")
|
| 17 |
+
all_shards = glob.glob(f"{input_path}/*.tar.*")
|
| 18 |
+
tar_groups = sorted(set(f.split(".tar.")[0] for f in all_shards))
|
| 19 |
+
|
| 20 |
+
for base in tar_groups:
|
| 21 |
+
sensor_name = os.path.basename(base)
|
| 22 |
+
|
| 23 |
+
# 🟢 FILTER LOGIC:
|
| 24 |
+
# If the user provided a list, skip sensors not in that list
|
| 25 |
+
if sensors_to_extract:
|
| 26 |
+
if not any(requested in sensor_name for requested in sensors_to_extract):
|
| 27 |
+
continue
|
| 28 |
+
|
| 29 |
+
md5_file = f"{base}.md5"
|
| 30 |
+
|
| 31 |
+
# 1. Verify MD5 integrity if the checksum file exists
|
| 32 |
+
if os.path.exists(md5_file):
|
| 33 |
+
print(f"🔍 Verifying integrity for {sensor_name}...")
|
| 34 |
+
try:
|
| 35 |
+
# We use shell=True to handle the redirection in the md5sum command
|
| 36 |
+
subprocess.run(f"md5sum -c {md5_file}", shell=True, check=True)
|
| 37 |
+
print(f"✨ Checksum verified for {sensor_name}.")
|
| 38 |
+
except subprocess.CalledProcessError:
|
| 39 |
+
print(f"⚠️ WARNING: {sensor_name} shards are corrupted! Skipping.")
|
| 40 |
+
continue
|
| 41 |
+
else:
|
| 42 |
+
print(f"ℹ️ No MD5 file found for {sensor_name}, skipping verification.")
|
| 43 |
+
|
| 44 |
+
# 2. Reconstruct and extract
|
| 45 |
+
print(f"🔄 Restoring {sensor_name} to {target_path}...")
|
| 46 |
+
|
| 47 |
+
# Check if the shards are gzipped (ending in .tar.gz.aa) or just tar (.tar.aa)
|
| 48 |
+
# If 'gz' is in the filename, we add the 'z' flag to tar
|
| 49 |
+
is_gz = ".gz" in base
|
| 50 |
+
tar_flags = "-xzf" if is_gz else "-xf"
|
| 51 |
+
|
| 52 |
+
# Command: concatenate all parts and pipe to tar
|
| 53 |
+
cmd_extract = f"cat {base}.tar.* | tar {tar_flags} - -C {target_path}"
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
subprocess.run(cmd_extract, shell=True, check=True)
|
| 57 |
+
print(f"✅ {sensor_name} successfully restored.")
|
| 58 |
+
except subprocess.CalledProcessError as e:
|
| 59 |
+
print(f"❌ Error extracting {sensor_name}: {e}")
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
# Example usage:
|
| 63 |
+
# To extract everything: restore_dataset()
|
| 64 |
+
# To extract only LiDAR: restore_dataset(sensors_to_extract=["LIDAR"])
|
| 65 |
+
|
| 66 |
+
# Optional: Integration with command line arguments
|
| 67 |
+
parser = argparse.ArgumentParser(description="Restore dataset from shards.")
|
| 68 |
+
parser.add_argument("--sensors", nargs="+", help="List of sensors to extract (e.g. LIDAR NARROW)")
|
| 69 |
+
args = parser.parse_args()
|
| 70 |
+
|
| 71 |
+
restore_dataset(sensors_to_extract=args.sensors)
|