snpl.image module

I/O interface for a generic multilayer array collection

class snpl.image.NpzImage(fp=None)[source]

Bases: object

I/O interface for NpzImage file.

NpzImage is a convenient file format to store multi-layered multi-dimensional arrays with a metadata header. Multiple numpy.ndarray objects with the same shape can be stored as “layers”, which can be specified by a “key”. All layers should have identical dimension and shape, but data type may be varied.

Parameters:

fp (str or file-like) – Path or file-like object of the source file. If None, an empty object is created.

Examples

Creation

>>> im = snpl.image.NpzImage()

Adding headers

>>> im.h["number"] = 100.0
>>> im.h["string"] = "wow",
>>> im.h["bool"] = True
>>> im.h["list"] = [1.0, 2.0]

Adding layers

>>> im.append_layer("one", np.array( [[1.0, 2.0], [3.0, 4.0]] ) )
>>> im.append_layer("two", np.array( [[5.0, 6.0], [7.0, 8.0]] ) )

Save to a file

>>> im.save("npzimage.npz")

Load from a file

>>> im2 = snpl.image.NpzImage("npzimage.npz")
>>> print(im2.h)
{'number': 100.0, 'string': 'wow', 'bool': True, 'list': [1.0, 2.0], 'version': '0.3.0'}
>>> print(im2.layers["one"])
[[1. 2.]
[3. 4.]]
>>> print(im2.layers["two"])
[[5. 6.]
[7. 8.]]
>>> print(im2.h["string"])
wow
append_history(string)[source]
append_layer(key, arr)[source]
get_layer(key)[source]
pop_layer(key)[source]
save(fp, compress=False)[source]