Class NpzFile
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
streamStreamA readable, seekable stream holding the ZIP archive.
ownStreamboolWhen true, disposing this also disposes
stream.allowPickleboolWhether members are trusted; see ReadArray(Stream, bool, long).
maxHeaderSizelongPer-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
Properties
AllowPickle
Whether members are loaded as trusted input.
public bool AllowPickle { get; }
Property Value
Count
Number of members.
public int Count { get; }
Property Value
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
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
keystring
Property Value
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
MaxHeaderSize
The per-member header size cap.
public long MaxHeaderSize { get; }
Property Value
Values
Every member's array. Enumerating this loads and caches all of them.
public IEnumerable<NDArray> Values { get; }
Property Value
Zip
The underlying archive, for callers that need entry metadata.
public ZipArchive Zip { get; }
Property Value
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
keystring
Returns
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
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
keystring
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
keystring
Returns
ToString()
Formatted as NumPy's repr: NpzFile 'model.npz' with keys: a, b, c.
public override string ToString()
Returns
TryGetValue(string, out NDArray)
The array under key, or false if there is no such member.
public bool TryGetValue(string key, out NDArray value)