Skip to main content

NumpyFile

Base class: Object

Represents a NumPy .npy or .npz file handle.

Use .npy files for a single direct array value and .npz files for named arrays.

Example

NumpyFile("values.npy", "w").write([1.0, 2.0, 3.0])
var values = NumpyFile("values.npy").read(as_real | as_list)
NumpyFile("archive.npz", "w").write("samples", [1, 2, 3])

Constructors

NumpyFile(const string path) -> NumpyFile

Prepares a NumPy file in read/write mode, creating it on first write if needed.

Parameters

  • path: File path. Missing extensions default to .npy.

Example

var f = NumpyFile("values.npy")

NumpyFile(const string path, const string mode) -> NumpyFile

Prepares a NumPy file with an explicit mode.

Modes:

  • "r": read-only, file must exist
  • "r+": read/write, file must exist
  • "w": read/write, create or truncate on first write
  • "w-" or "x": create on first write, fail if it exists
  • "a": read/write, create on first write if missing (default)

Parameters

  • path: File path.
  • mode: File open mode.

Example

var f = NumpyFile("archive.npz", "w")

Members

NameDescription
dtypeReturns the dtype descriptor of a named .npz array.
existsChecks whether a named array exists in a .npz archive.
is_npyReturns whether this handle targets a .npy file.
is_npzReturns whether this handle targets a .npz file.
list_datasetsLists named arrays in a .npz archive.
pathReturns the resolved file path.
rankReturns the rank of a named .npz array.
readReads a named complex sparse matrix from a .npz archive.
shapeReturns the shape of a .npy array.
sizeReturns the element count of a .npy array.
writeWrites a sparse matrix densely to a .npy file and returns the file for chaining.