Spaces:
Sleeping
Sleeping
File size: 2,647 Bytes
02c6351 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import numpy as np
import os
import sys
def read_npy_file(file_path):
"""
Read a .npy file and return the numpy array.
Args:
file_path (str): Path to the .npy file
Returns:
numpy.ndarray: The loaded numpy array
Raises:
FileNotFoundError: If the file doesn't exist
ValueError: If the file is not a valid .npy file
"""
try:
# Check if file exists
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
# Check if file has .npy extension
if not file_path.lower().endswith('.npy'):
print(f"Warning: File {file_path} doesn't have .npy extension")
# Load the numpy array
array = np.load(file_path)
print(f"Successfully loaded {file_path}")
print(f"Array shape: {array.shape}")
print(f"Array dtype: {array.dtype}")
print(f"Array size: {array.size}")
return array
except Exception as e:
raise ValueError(f"Error reading {file_path}: {str(e)}")
def print_array_info(array, show_data=False, max_elements=10):
"""
Print detailed information about a numpy array.
Args:
array (numpy.ndarray): The numpy array to analyze
show_data (bool): Whether to show actual data values
max_elements (int): Maximum number of elements to display
"""
print("\n" + "="*50)
print("ARRAY INFORMATION")
print("="*50)
print(f"Shape: {array.shape}")
print(f"Dtype: {array.dtype}")
print(f"Size: {array.size}")
print(f"Dimensions: {array.ndim}")
print(f"Memory usage: {array.nbytes} bytes")
if array.size > 0:
print(f"Min value: {np.min(array)}")
print(f"Max value: {np.max(array)}")
print(f"Mean value: {np.mean(array)}")
print(f"Std deviation: {np.std(array)}")
if show_data:
print("\nArray data:")
if array.size <= max_elements:
print(array)
else:
print(f"First {max_elements} elements:")
print(array.flat[:max_elements])
print("...")
def main():
"""
Main function to demonstrate usage.
"""
file_path = "/home/alienware3/Documents/diamond/low_res.npy"
try:
# Read the NPY file
array = read_npy_file(file_path)
# Print detailed information
print_array_info(array, show_data=True, max_elements=20)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
|