np-struct


Namenp-struct JSON
Version 0.0.1 PyPI version JSON
download
home_page
Summaryuser friendly interface for NumPy structured arrays
upload_time2023-08-11 03:51:17
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords numpy structured array struct
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install np-struct
```

## Usage

```python
import np_struct 
```

Create a c-style structure with Numpy types.

```python
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:
```python
ex = example(shape = (3,), byte_order='>')
ex[0].data2 = 1 + 2j
```
```bash
>>> 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. 
```bash
>> 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:

```bash
>> 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:
```python
class nested(Struct):
    field1 = example()
    field2 = example()

n = nested()
n.field1.data2 += j*np.pi
```
```bash
>> 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](./examples/structures.ipynb)  
[Labeled arrays](./examples/ldarray.ipynb)


## License

np-struct is licensed under the MIT License.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "np-struct",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "numpy,structured array,struct",
    "author": "",
    "author_email": "Rick Lyon <rlyon14@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e1/65/7d8c4d4ce2a3b10965ebf776d98e637df41e9407220b73f9cd2d33ad768c/np-struct-0.0.1.tar.gz",
    "platform": null,
    "description": "# np-struct\r\n\r\n`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. \r\n \r\nStructured 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.\r\n\r\n## Installation\r\n\r\n```bash\r\npip install np-struct\r\n```\r\n\r\n## Usage\r\n\r\n```python\r\nimport np_struct \r\n```\r\n\r\nCreate a c-style structure with Numpy types.\r\n\r\n```python\r\nfrom np_struct import Struct\r\nimport numpy as np\r\n\r\nclass example(Struct):\r\n    data1 = np.uint32()\r\n    data2 = np.complex128([0]*3)\r\n```\r\n\r\nStructures can be initialized with arbitrary shapes by using the `shape` kwarg:\r\n```python\r\nex = example(shape = (3,), byte_order='>')\r\nex[0].data2 = 1 + 2j\r\n```\r\n```bash\r\n>>> ex\r\nStruct example: (3,)\r\n[\r\n    psize:  uint16[0]\r\n    data1:  uint32[0]\r\n    data2:  complex128[1.+2.j 1.+2.j 1.+2.j]\r\n]\r\n...\r\n[\r\n    psize:  uint16[0]\r\n    data1:  uint32[0]\r\n    data2:  complex128[0.+0.j 0.+0.j 0.+0.j]\r\n]\r\n```\r\n\r\nMembers can also be initialized by passing in their name to the constructor with an inital value. \r\n```bash\r\n>> example(data2 = np.zeros(shape=(3,2)))\r\nStruct example: \r\n    psize:  uint16[0]\r\n    data1:  uint32[0]\r\n    data2:  complex128[[0.+0.j 0.+0.j]\r\n\t       [0.+0.j 0.+0.j]\r\n\t       [0.+0.j 0.+0.j]]\r\n```\r\n\r\nThe structure inherits from np.ndarray and supports all math functions that a normal structured array does.\r\nTo cast as a standard numpy array:\r\n\r\n```bash\r\n>> ex2.view(np.ndarray)\r\narray([([0], [0], [[0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]])],\r\n      dtype=[('psize', '<u2', (1,)), ('data1', '<u4', (1,)), ('data2', '<c16', (3, 2))])\r\n```\r\n\r\nNested structures are also supported:\r\n```python\r\nclass nested(Struct):\r\n    field1 = example()\r\n    field2 = example()\r\n\r\nn = nested()\r\nn.field1.data2 += j*np.pi\r\n```\r\n```bash\r\n>> n\r\nStruct nested: \r\n    field1:  Struct example: \r\n              psize:  uint16[0]\r\n              data1:  uint32[0]\r\n              data2:  complex128[0.+3.14159265j 0.+3.14159265j 0.+3.14159265j]\r\n    field2:  Struct example: \r\n              psize:  uint16[0]\r\n              data1:  uint32[0]\r\n              data2:  complex128[0.+0.j 0.+0.j 0.+0.j]\r\n```\r\n\r\n\r\nExamples:\r\n\r\n[Transfering structures over an interface](./examples/structures.ipynb)  \r\n[Labeled arrays](./examples/ldarray.ipynb)\r\n\r\n\r\n## License\r\n\r\nnp-struct is licensed under the MIT License.\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "user friendly interface for NumPy structured arrays",
    "version": "0.0.1",
    "project_urls": {
        "repository": "https://github.com/ricklyon/np_struct"
    },
    "split_keywords": [
        "numpy",
        "structured array",
        "struct"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2012be303196010f7593142ae4f7894af26bdfb9291ae3609ebd7483a3be6ba5",
                "md5": "f6c258e2a1cc9b95f960237a1419f35f",
                "sha256": "c5840cb0716cd4bc4b69e8b45f3c3d8c22d6d8e786fb84fb7b81fa2bad55080b"
            },
            "downloads": -1,
            "filename": "np_struct-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f6c258e2a1cc9b95f960237a1419f35f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 18552,
            "upload_time": "2023-08-11T03:51:16",
            "upload_time_iso_8601": "2023-08-11T03:51:16.437233Z",
            "url": "https://files.pythonhosted.org/packages/20/12/be303196010f7593142ae4f7894af26bdfb9291ae3609ebd7483a3be6ba5/np_struct-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e1657d8c4d4ce2a3b10965ebf776d98e637df41e9407220b73f9cd2d33ad768c",
                "md5": "3ba58d01b380aaca6fab45a4e4d0f122",
                "sha256": "b1c2efa22afccd177ba3848cda595ea83239bdedcde4dde76b376cf1062c1456"
            },
            "downloads": -1,
            "filename": "np-struct-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3ba58d01b380aaca6fab45a4e4d0f122",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 19461,
            "upload_time": "2023-08-11T03:51:17",
            "upload_time_iso_8601": "2023-08-11T03:51:17.623468Z",
            "url": "https://files.pythonhosted.org/packages/e1/65/7d8c4d4ce2a3b10965ebf776d98e637df41e9407220b73f9cd2d33ad768c/np-struct-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-11 03:51:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ricklyon",
    "github_project": "np_struct",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "np-struct"
}
        
Elapsed time: 0.45800s