Skip to content

visualdynamics.io.plain_numpy

plain_numpy

Read NumPy files without running anything in them.

A .npy or .npz member with an object array holds a pickle, and unpickling runs whatever the pickle says. A file is something a person was sent and drops on the window, so nothing here hands a file's pickle to pickle.load or allow_pickle=True: an array of objects is refused in words, and the one kind a real file needs, the node id arrays in a geometry's traceline and element (PlainNpz.with_objects), is rebuilt from a pickle that may name nothing but numpy's arrays.

Classes:

Name Description
PlainNpz

An open .npz, used as np.load's result is, whose arrays of

Functions:

Name Description
npy_dtype

What a .npy declares it holds, from its header alone. Raises

read_npy

A .npy of plain data; ValueError for an array of objects, or for

Classes

PlainNpz

PlainNpz(path: str | PathLike)

An open .npz, used as np.load's result is, whose arrays of objects are refused in words.

Methods:

Name Description
with_objects

An array that may hold objects, rebuilt from plain data only.

Source code in src/visualdynamics/io/plain_numpy.py
def __init__(self, path: str | os.PathLike) -> None:
    self._path = path
    try:
        self._npz = np.load(path, allow_pickle=False)
        self.files: list[str] = self._npz.files
    except (ValueError, AttributeError, zipfile.BadZipFile):
        raise ValueError(f'{path} is not an npz archive: it could not be read') from None
Methods:
with_objects
with_objects(name: str) -> ndarray

An array that may hold objects, rebuilt from plain data only.

Source code in src/visualdynamics/io/plain_numpy.py
def with_objects(self, name: str) -> np.ndarray:
    """An array that may hold objects, rebuilt from plain data only."""
    where = f'{self._path}: {name}'
    with self._open(name) as fp:
        try:
            shape, dtype = _header(fp)
        except ValueError:
            raise ValueError(f'{where} is not a NumPy array') from None
        if dtype.hasobject:
            return _read_objects(fp, shape, dtype, where)
    return self._npz[name]

Functions:

npy_dtype

npy_dtype(path: str | PathLike) -> dtype

What a .npy declares it holds, from its header alone. Raises ValueError for a file that is not a NumPy file.

Source code in src/visualdynamics/io/plain_numpy.py
def npy_dtype(path: str | os.PathLike) -> np.dtype:
    """What a `.npy` declares it holds, from its header alone. Raises
    ValueError for a file that is not a NumPy file."""
    with open(path, 'rb') as fp:
        return _header(fp)[1]

read_npy

read_npy(path: str | PathLike) -> ndarray

A .npy of plain data; ValueError for an array of objects, or for a file that is not a NumPy file.

Source code in src/visualdynamics/io/plain_numpy.py
def read_npy(path: str | os.PathLike) -> np.ndarray:
    """A `.npy` of plain data; ValueError for an array of objects, or for
    a file that is not a NumPy file."""
    try:
        dtype = npy_dtype(path)
    except ValueError:
        raise ValueError(f'{path} is not a NumPy file') from None
    if dtype.hasobject:
        raise _holds_objects(path)
    return np.load(path, allow_pickle=False)