from pathlib import Path
import numpy as np
from epymorph.adrio.numpy import NPY, NPZ
from epymorph.error import DataResourceError
Numpy
Description
The Numpy file ADRIO loads data from any provided .npy (a NumPy array file) or .npz (a zipped archive of .npy files) files. In addition, you can provide a slice of data that you would like to specifically extract and load from the given files.
Numpy ADRIO
Simple NumPy Array Example
- Use necessary imports for manipulating numpy arrays and using the Numpy ADRIO
- Provide or make a .npy file to use.
= np.array([1, 2, 3])
array "./scratch/npy_test.npy", arr=array) np.save(
- Use the NPY ADRIO with the path to the .npy file and evaluate to load the data from that file.
= NPY(Path("./scratch/npy_test.npy"))
adrio adrio.evaluate()
array([1, 2, 3])
NPZ File Example
- Provide or make any NPZ files, which can be made by saving arrays and using savez()
= np.array(
array 7.7, 8.8, 9.9)],
[(=[("data", np.float64), ("data2", np.float64), ("data3", np.float64)],
dtype
)= np.array([4.4, 5.5, 6.6])
array2 "./scratch/npz_test.npz", arr=array, arr2=array2) np.savez(
- Use the NPZ call with the path to the .npz file and the array you want to load
"./scratch/npz_test.npz"), "arr").evaluate() NPZ(Path(
array([(7.7, 8.8, 9.9)],
dtype=[('data', '<f8'), ('data2', '<f8'), ('data3', '<f8')])
- Use a slice to exclude or include any specific elements of an array
"./scratch/npz_test.npz"), "arr2", np.s_[1:3,]).evaluate() NPZ(Path(
array([5.5, 6.6])
Slicing Arrays
The following are examples on different ways arrays from NPZ files can be sliced.
Each Array Axis
# save and load a multidimensional array
= np.array(
array "This", "is", "an"], ["array", "of", "strings"], ["for", "testing", "."]]
[[
)"./scratch/npz_test2.npz", array)
np.savez(
# slice each axis of the array individually
"./scratch/npz_test2.npz"), "arr_0", (slice(0, 2), slice(1, 3))).evaluate() NPZ(Path(
array([['is', 'an'],
['of', 'strings']], dtype='<U7')
Single Slice
"./scratch/npz_test2.npz"), "arr_0", slice(0, None, 2)).evaluate() NPZ(Path(
array([['This', 'is', 'an'],
['for', 'testing', '.']], dtype='<U7')
Using np.s_ to Slice Arrays
"./scratch/npz_test2.npz"), "arr_0", np.s_[0:2, ::2]).evaluate() NPZ(Path(
array([['This', 'an'],
['array', 'strings']], dtype='<U7')
Ellipsis Slice
"./scratch/npz_test2.npz"), "arr_0", np.s_[...]).evaluate() NPZ(Path(
array([['This', 'is', 'an'],
['array', 'of', 'strings'],
['for', 'testing', '.']], dtype='<U7')
Using np.index_exp to Slice Arrays
"./scratch/npz_test2.npz"), "arr_0", np.index_exp[...]).evaluate() NPZ(Path(
array([['This', 'is', 'an'],
['array', 'of', 'strings'],
['for', 'testing', '.']], dtype='<U7')
Error Examples
Below are some examples of how the ADRIO may not output an expected output. These issues can occur when there are more indices provided to slice than the array actually has or if an incorrect file type is given to the call.
# ERROR: too many indices
try:
= (
adrio "./scratch/npz_test2.npz"), "arr_0",
NPZ(Path(1:3, 1:3, 1:3])
np.index_exp[
)
adrio.evaluate()except DataResourceError as e:
print(repr(e))
DataResourceError('Specified array slice is invalid for the shape of this data.')
# ERROR: wrong file type
try:
= NPY(Path("./scratch/npz_test2.npz"))
adrio except DataResourceError as e:
print(repr(e))
DataResourceError('Incorrect file type. Only .npy files can be loaded through NPY ADRIOs.')