1. import numpy as np
  2. import argparse
  3. import os
  4. def restore_arrays(data_dir, array_names, output_file, overwrite=False):
  5. """
  6. Restores data arrays from a directory.
  7. Args:
  8. data_dir (str): Path to the directory containing the array data.
  9. array_names (list): List of array names to restore.
  10. output_file (str): Path to the output file for saving the restored arrays.
  11. overwrite (bool): Whether to overwrite existing arrays in the output file.
  12. """
  13. try:
  14. with np.nditer(data_dir) as f: # Iterate through the data directory
  15. for name in array_names:
  16. filepath = os.path.join(data_dir, name)
  17. if not os.path.exists(filepath):
  18. print(f"Warning: File not found: {filepath}")
  19. continue
  20. try:
  21. array = np.load(filepath) # Load the array
  22. if overwrite and os.path.exists(output_file):
  23. np.save(output_file, array) # Overwrite if specified
  24. else:
  25. np.save(output_file, array)
  26. print(f"Restored and saved: {name}")
  27. except Exception as e:
  28. print(f"Error restoring {name}: {e}")
  29. except Exception as e:
  30. print(f"Error processing data directory: {e}")
  31. if __name__ == '__main__':
  32. parser = argparse.ArgumentParser(description='Restore data arrays from a directory.')
  33. parser.add_argument('data_dir', type=str, help='Path to the directory containing the array data.')
  34. parser.add_argument('array_names', nargs='+', type=str, help='List of array names to restore.')
  35. parser.add_argument('output_file', type=str, help='Path to the output file for saving the restored arrays.')
  36. parser.add_argument('--overwrite', action='store_true', help='Overwrite existing arrays in the output file.')
  37. args = parser.parse_args()
  38. restore_arrays(args.data_dir, args.array_names, args.output_file, args.overwrite)

Add your comment