Upload compress_to_h5.py with huggingface_hub
Browse files- compress_to_h5.py +64 -0
compress_to_h5.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import h5py
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Directory containing your .npz files
|
| 6 |
+
input_directory = "task_ABC_D/training"
|
| 7 |
+
output_h5_file = "training_abc_d.h5"
|
| 8 |
+
error_log_file = "error_log.txt" # New text file to store error filenames
|
| 9 |
+
|
| 10 |
+
# Function to get list of already processed files
|
| 11 |
+
def get_processed_files(h5_file):
|
| 12 |
+
processed_files = set()
|
| 13 |
+
if os.path.exists(h5_file):
|
| 14 |
+
with h5py.File(h5_file, "r") as h5f:
|
| 15 |
+
processed_files = set(h5f.keys())
|
| 16 |
+
return processed_files
|
| 17 |
+
|
| 18 |
+
# Get list of already processed files
|
| 19 |
+
processed_files = get_processed_files(output_h5_file)
|
| 20 |
+
|
| 21 |
+
# Create an HDF5 file
|
| 22 |
+
with h5py.File(output_h5_file, "a") as h5f:
|
| 23 |
+
# Loop through all files in the directory
|
| 24 |
+
for filename in os.listdir(input_directory):
|
| 25 |
+
if filename.endswith('.npz'): # Only process .npz files
|
| 26 |
+
if filename in processed_files:
|
| 27 |
+
print("Skipping already processed file:", filename)
|
| 28 |
+
continue
|
| 29 |
+
filepath = os.path.join(input_directory, filename)
|
| 30 |
+
print(filename)
|
| 31 |
+
|
| 32 |
+
# Load the .npz file
|
| 33 |
+
try:
|
| 34 |
+
npz_data = np.load(filepath)
|
| 35 |
+
# Create a group for this .npz file
|
| 36 |
+
file_group = h5f.create_group(filename)
|
| 37 |
+
|
| 38 |
+
# Store each array from the .npz file as a separate dataset
|
| 39 |
+
for array_name in npz_data.files:
|
| 40 |
+
# Create dataset with compression
|
| 41 |
+
file_group.create_dataset(
|
| 42 |
+
array_name,
|
| 43 |
+
data=npz_data[array_name],
|
| 44 |
+
compression="lzf",
|
| 45 |
+
# compression_opts=4
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Close the npz file
|
| 49 |
+
npz_data.close()
|
| 50 |
+
except Exception as e:
|
| 51 |
+
error_message = "Error processing " + filename
|
| 52 |
+
print(error_message)
|
| 53 |
+
# Append the filename to the error log file
|
| 54 |
+
with open(error_log_file, "a") as error_log:
|
| 55 |
+
error_log.write(filename + "\n")
|
| 56 |
+
|
| 57 |
+
print('done')
|
| 58 |
+
|
| 59 |
+
# with h5py.File("combined_npz_files.h5", "r") as h5f:
|
| 60 |
+
# for group_name in h5f.keys():
|
| 61 |
+
# print(f"Group: {group_name}")
|
| 62 |
+
# group = h5f[group_name]
|
| 63 |
+
# for dataset_name in group.keys():
|
| 64 |
+
# print(f" Dataset: {dataset_name}, Shape: {group[dataset_name].shape}, Dtype: {group[dataset_name].dtype}")
|