Table of Contents

Class NpzFile

Namespace
NumSharp.IO
Assembly
NumSharp.dll

A lazily-loaded .npz archive — NumPy's NpzFile.

public sealed class NpzFile : IReadOnlyDictionary<string, NDArray>, IReadOnlyCollection<KeyValuePair<string, NDArray>>, IEnumerable<KeyValuePair<string, NDArray>>, IEnumerable, IDisposable
Inheritance
NpzFile
Implements
Inherited Members
Extension Methods

Remarks

A .npz is a ZIP archive of .npy members. Nothing is decoded until a key is accessed, and each array is cached from then on, so a huge archive costs only what is read out of it.

Keys work with or without the .npy suffix — npz["weights"] and npz["weights.npy"] are the same member — while Files reports the stripped names, matching NumPy.

The archive holds an open file handle, so dispose it:

using var npz = np.load_npz("model.npz");
NDArray w = npz["weights"];
NDArray b = npz.f.biases;   // dot access, NumPy's BagObj

Constructors

NpzFile(Stream, bool, bool, long)

Open an archive over a stream.

public NpzFile(Stream stream, bool ownStream = false, bool allowPickle = false, long maxHeaderSize = 10000)

Parameters

stream Stream

A readable, seekable stream holding the ZIP archive.

ownStream bool

When true, disposing this also disposes stream.

allowPickle bool

Whether members are trusted; see ReadArray(Stream, bool, long).

maxHeaderSize long

Per-member header size cap.

NpzFile(string, bool, long)

Open an archive from a file path. The file handle is owned and closed on dispose.

public NpzFile(string path, bool allowPickle = false, long maxHeaderSize = 10000)

Parameters

path string
allowPickle bool
maxHeaderSize long

Properties

AllowPickle

Whether members are loaded as trusted input.

public bool AllowPickle { get; }

Property Value

bool

Count

Number of members.

public int Count { get; }

Property Value

int

F

Dot-notation access to members — NumPy's npz.f.weights. Requires a dynamic receiver: NDArray w = npz.f.weights;.

public dynamic F { get; }

Property Value

dynamic

Files

The array names in the archive, with .npy stripped — NumPy's .files.

public IReadOnlyList<string> Files { get; }

Property Value

IReadOnlyList<string>

this[string]

The array stored under key, with or without the .npy suffix. Loaded on first access and cached.

public NDArray this[string key] { get; }

Parameters

key string

Property Value

NDArray

Exceptions

KeyNotFoundException

No such member.

FormatException

The member is not a .npy file — use GetRawBytes(string).

Keys

The member names — same as Files.

public IEnumerable<string> Keys { get; }

Property Value

IEnumerable<string>

MaxHeaderSize

The per-member header size cap.

public long MaxHeaderSize { get; }

Property Value

long

Values

Every member's array. Enumerating this loads and caches all of them.

public IEnumerable<NDArray> Values { get; }

Property Value

IEnumerable<NDArray>

Zip

The underlying archive, for callers that need entry metadata.

public ZipArchive Zip { get; }

Property Value

ZipArchive

f

Lower-case alias of F, spelled as NumPy spells it.

public dynamic f { get; }

Property Value

dynamic

Methods

Close()

Close the archive and release the file handle — NumPy's close().

public void Close()

ContainsKey(string)

Whether the archive has this member (with or without the .npy suffix).

public bool ContainsKey(string key)

Parameters

key string

Returns

bool

Dispose()

Close the archive and release the file handle — NumPy's close().

public void Dispose()

GetEnumerator()

Enumerate every member as a name/array pair, loading each in turn.

public IEnumerator<KeyValuePair<string, NDArray>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, NDArray>>

GetRawBytes(string)

A member's raw bytes, whatever it holds. NumPy returns these from its indexer for non-.npy members; for a .npy member this is the encoded file itself.

public byte[] GetRawBytes(string key)

Parameters

key string

Returns

byte[]

Remarks

Capped at ~2 GB by Array itself. A member larger than that can still be read as an array through the indexer, which streams instead of buffering.

Exceptions

KeyNotFoundException

No such member.

IsArray(string)

Whether key names a member that holds a .npy array.

public bool IsArray(string key)

Parameters

key string

Returns

bool

ToString()

Formatted as NumPy's repr: NpzFile 'model.npz' with keys: a, b, c.

public override string ToString()

Returns

string

TryGetValue(string, out NDArray)

The array under key, or false if there is no such member.

public bool TryGetValue(string key, out NDArray value)

Parameters

key string
value NDArray

Returns

bool