user friendly interface for NumPy structured arrays
Project description
np-struct
np-struct
extends structured arrays in NumPy to be a bit more user friendly and intuitive, with added support for transferring structured arrays across serial or socket interfaces.
Structured arrays are built to mirror the struct typedef in C/C++, but can be used for any complicated data structure. They behave similar to standard arrays, but support mixed data types, labeling, and unequal length arrays. Arrays are easily written or loaded from disk in the standard .npy
binary format.
Installation
pip install np-struct
Usage
import np_struct
Create a c-style structure with Numpy types.
from np_struct import Struct
import numpy as np
class example(Struct):
data1 = np.uint32()
data2 = np.complex128([0]*3)
Structures can be initialized with arbitrary shapes by using the shape
kwarg:
ex = example(shape = (3,), byte_order='>')
ex[0].data2 = 1 + 2j
>>> ex
Struct example: (3,)
[
psize: uint16[0]
data1: uint32[0]
data2: complex128[1.+2.j 1.+2.j 1.+2.j]
]
...
[
psize: uint16[0]
data1: uint32[0]
data2: complex128[0.+0.j 0.+0.j 0.+0.j]
]
Members can also be initialized by passing in their name to the constructor with an inital value.
>> example(data2 = np.zeros(shape=(3,2)))
Struct example:
psize: uint16[0]
data1: uint32[0]
data2: complex128[[0.+0.j 0.+0.j]
[0.+0.j 0.+0.j]
[0.+0.j 0.+0.j]]
The structure inherits from np.ndarray and supports all math functions that a normal structured array does. To cast as a standard numpy array:
>> ex2.view(np.ndarray)
array([([0], [0], [[0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]])],
dtype=[('psize', '<u2', (1,)), ('data1', '<u4', (1,)), ('data2', '<c16', (3, 2))])
Nested structures are also supported:
class nested(Struct):
field1 = example()
field2 = example()
n = nested()
n.field1.data2 += j*np.pi
>> n
Struct nested:
field1: Struct example:
psize: uint16[0]
data1: uint32[0]
data2: complex128[0.+3.14159265j 0.+3.14159265j 0.+3.14159265j]
field2: Struct example:
psize: uint16[0]
data1: uint32[0]
data2: complex128[0.+0.j 0.+0.j 0.+0.j]
Examples:
Transfering structures over an interface
Labeled arrays
License
np-struct is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file np-struct-0.0.1.tar.gz
.
File metadata
- Download URL: np-struct-0.0.1.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1c2efa22afccd177ba3848cda595ea83239bdedcde4dde76b376cf1062c1456 |
|
MD5 | 3ba58d01b380aaca6fab45a4e4d0f122 |
|
BLAKE2b-256 | e1657d8c4d4ce2a3b10965ebf776d98e637df41e9407220b73f9cd2d33ad768c |
File details
Details for the file np_struct-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: np_struct-0.0.1-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5840cb0716cd4bc4b69e8b45f3c3d8c22d6d8e786fb84fb7b81fa2bad55080b |
|
MD5 | f6c258e2a1cc9b95f960237a1419f35f |
|
BLAKE2b-256 | 2012be303196010f7593142ae4f7894af26bdfb9291ae3609ebd7483a3be6ba5 |