Load a 1D NumPy array from a binary file. Args: filename (str): The name of the file to read the array from. Returns: np.ndarray: The loaded 1D array.
(filename)
| 75 | print("Error: Cannot write to file.") |
| 76 | |
| 77 | def load_array_from_bin(filename): |
| 78 | """ |
| 79 | Load a 1D NumPy array from a binary file. |
| 80 | |
| 81 | Args: |
| 82 | filename (str): The name of the file to read the array from. |
| 83 | |
| 84 | Returns: |
| 85 | np.ndarray: The loaded 1D array. |
| 86 | """ |
| 87 | try: |
| 88 | with open(filename, "rb") as file: |
| 89 | # Read the length of the array |
| 90 | length = int.from_bytes(file.read(4), byteorder='little') |
| 91 | |
| 92 | # Read the array data |
| 93 | array = np.fromfile(file, dtype=np.double, count=length) |
| 94 | |
| 95 | print(f"Array loaded from {filename}. Length: {length}") |
| 96 | return array |
| 97 | except IOError: |
| 98 | print("Error: Cannot read file.") |
| 99 | return None |
| 100 | |
| 101 |
nothing calls this directly
no outgoing calls
no test coverage detected