np-struct 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 Rick Lyon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.1
2
+ Name: np-struct
3
+ Version: 0.0.1
4
+ Summary: user friendly interface for NumPy structured arrays
5
+ Author-email: Rick Lyon <rlyon14@gmail.com>
6
+ Project-URL: repository, https://github.com/ricklyon/np_struct
7
+ Keywords: numpy,structured array,struct
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.7
12
+ Description-Content-Type: text/markdown
13
+ Provides-Extra: dev
14
+ License-File: LICENSE.txt
15
+
16
+ # np-struct
17
+
18
+ `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.
19
+
20
+ 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.
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ pip install np-struct
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```python
31
+ import np_struct
32
+ ```
33
+
34
+ Create a c-style structure with Numpy types.
35
+
36
+ ```python
37
+ from np_struct import Struct
38
+ import numpy as np
39
+
40
+ class example(Struct):
41
+ data1 = np.uint32()
42
+ data2 = np.complex128([0]*3)
43
+ ```
44
+
45
+ Structures can be initialized with arbitrary shapes by using the `shape` kwarg:
46
+ ```python
47
+ ex = example(shape = (3,), byte_order='>')
48
+ ex[0].data2 = 1 + 2j
49
+ ```
50
+ ```bash
51
+ >>> ex
52
+ Struct example: (3,)
53
+ [
54
+ psize: uint16[0]
55
+ data1: uint32[0]
56
+ data2: complex128[1.+2.j 1.+2.j 1.+2.j]
57
+ ]
58
+ ...
59
+ [
60
+ psize: uint16[0]
61
+ data1: uint32[0]
62
+ data2: complex128[0.+0.j 0.+0.j 0.+0.j]
63
+ ]
64
+ ```
65
+
66
+ Members can also be initialized by passing in their name to the constructor with an inital value.
67
+ ```bash
68
+ >> example(data2 = np.zeros(shape=(3,2)))
69
+ Struct example:
70
+ psize: uint16[0]
71
+ data1: uint32[0]
72
+ data2: complex128[[0.+0.j 0.+0.j]
73
+ [0.+0.j 0.+0.j]
74
+ [0.+0.j 0.+0.j]]
75
+ ```
76
+
77
+ The structure inherits from np.ndarray and supports all math functions that a normal structured array does.
78
+ To cast as a standard numpy array:
79
+
80
+ ```bash
81
+ >> ex2.view(np.ndarray)
82
+ array([([0], [0], [[0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]])],
83
+ dtype=[('psize', '<u2', (1,)), ('data1', '<u4', (1,)), ('data2', '<c16', (3, 2))])
84
+ ```
85
+
86
+ Nested structures are also supported:
87
+ ```python
88
+ class nested(Struct):
89
+ field1 = example()
90
+ field2 = example()
91
+
92
+ n = nested()
93
+ n.field1.data2 += j*np.pi
94
+ ```
95
+ ```bash
96
+ >> n
97
+ Struct nested:
98
+ field1: Struct example:
99
+ psize: uint16[0]
100
+ data1: uint32[0]
101
+ data2: complex128[0.+3.14159265j 0.+3.14159265j 0.+3.14159265j]
102
+ field2: Struct example:
103
+ psize: uint16[0]
104
+ data1: uint32[0]
105
+ data2: complex128[0.+0.j 0.+0.j 0.+0.j]
106
+ ```
107
+
108
+
109
+ Examples:
110
+
111
+ [Transfering structures over an interface](./examples/structures.ipynb)
112
+ [Labeled arrays](./examples/ldarray.ipynb)
113
+
114
+
115
+ ## License
116
+
117
+ np-struct is licensed under the MIT License.
@@ -0,0 +1,102 @@
1
+ # np-struct
2
+
3
+ `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.
4
+
5
+ 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.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install np-struct
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```python
16
+ import np_struct
17
+ ```
18
+
19
+ Create a c-style structure with Numpy types.
20
+
21
+ ```python
22
+ from np_struct import Struct
23
+ import numpy as np
24
+
25
+ class example(Struct):
26
+ data1 = np.uint32()
27
+ data2 = np.complex128([0]*3)
28
+ ```
29
+
30
+ Structures can be initialized with arbitrary shapes by using the `shape` kwarg:
31
+ ```python
32
+ ex = example(shape = (3,), byte_order='>')
33
+ ex[0].data2 = 1 + 2j
34
+ ```
35
+ ```bash
36
+ >>> ex
37
+ Struct example: (3,)
38
+ [
39
+ psize: uint16[0]
40
+ data1: uint32[0]
41
+ data2: complex128[1.+2.j 1.+2.j 1.+2.j]
42
+ ]
43
+ ...
44
+ [
45
+ psize: uint16[0]
46
+ data1: uint32[0]
47
+ data2: complex128[0.+0.j 0.+0.j 0.+0.j]
48
+ ]
49
+ ```
50
+
51
+ Members can also be initialized by passing in their name to the constructor with an inital value.
52
+ ```bash
53
+ >> example(data2 = np.zeros(shape=(3,2)))
54
+ Struct example:
55
+ psize: uint16[0]
56
+ data1: uint32[0]
57
+ data2: complex128[[0.+0.j 0.+0.j]
58
+ [0.+0.j 0.+0.j]
59
+ [0.+0.j 0.+0.j]]
60
+ ```
61
+
62
+ The structure inherits from np.ndarray and supports all math functions that a normal structured array does.
63
+ To cast as a standard numpy array:
64
+
65
+ ```bash
66
+ >> ex2.view(np.ndarray)
67
+ array([([0], [0], [[0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j]])],
68
+ dtype=[('psize', '<u2', (1,)), ('data1', '<u4', (1,)), ('data2', '<c16', (3, 2))])
69
+ ```
70
+
71
+ Nested structures are also supported:
72
+ ```python
73
+ class nested(Struct):
74
+ field1 = example()
75
+ field2 = example()
76
+
77
+ n = nested()
78
+ n.field1.data2 += j*np.pi
79
+ ```
80
+ ```bash
81
+ >> n
82
+ Struct nested:
83
+ field1: Struct example:
84
+ psize: uint16[0]
85
+ data1: uint32[0]
86
+ data2: complex128[0.+3.14159265j 0.+3.14159265j 0.+3.14159265j]
87
+ field2: Struct example:
88
+ psize: uint16[0]
89
+ data1: uint32[0]
90
+ data2: complex128[0.+0.j 0.+0.j 0.+0.j]
91
+ ```
92
+
93
+
94
+ Examples:
95
+
96
+ [Transfering structures over an interface](./examples/structures.ipynb)
97
+ [Labeled arrays](./examples/ldarray.ipynb)
98
+
99
+
100
+ ## License
101
+
102
+ np-struct is licensed under the MIT License.
@@ -0,0 +1,5 @@
1
+
2
+ from . structures import Struct
3
+ from . ldarray import ldarray, lddim
4
+ from . transfer import Packet
5
+ from . import fields
@@ -0,0 +1,63 @@
1
+
2
+ import numpy as np
3
+ from copy import deepcopy as dcopy
4
+
5
+
6
+ class npfield(np.ndarray):
7
+
8
+ def __new__(cls, input_=None, bits=None, doc=None, dtype=None, enum=None):
9
+
10
+ input_ = 0 if np.all(input_ == None) else input_
11
+
12
+ ## cast single values as arrays
13
+ input_ = [input_] if not isinstance(input_, (tuple, list, np.ndarray)) else input_
14
+
15
+ ## set dtype based on class name
16
+ dtype = np.dtype(cls.__name__.lower())
17
+
18
+ obj = np.asarray(input_, dtype=dtype).view(cls)
19
+
20
+ ## assign member variables
21
+ obj.bits = bits
22
+ obj.doc = doc
23
+ obj.enum = enum
24
+
25
+ return obj
26
+
27
+ def __array_finalize__(self, obj):
28
+ ## required method of subclasses of numpy. Sets unique member variables of new instances
29
+ if obj is None: return
30
+
31
+ self.bits = getattr(obj, 'bits', None)
32
+ self.doc = getattr(obj, 'doc', "")
33
+ self.enum = getattr(obj, 'enum', None)
34
+
35
+ def __array_wrap__(self, out_arr, context=None):
36
+
37
+ dtype = np.dtype(self.__class__.__name__.lower())
38
+
39
+ return out_arr.astype(dtype)
40
+
41
+ class uint8(npfield):
42
+ pass
43
+
44
+ class int8(npfield):
45
+ pass
46
+
47
+ class uint16(npfield):
48
+ pass
49
+
50
+ class int16(npfield):
51
+ pass
52
+
53
+ class uint32(npfield):
54
+ pass
55
+
56
+ class int32(npfield):
57
+ pass
58
+
59
+ class float64(npfield):
60
+ pass
61
+
62
+ class float32(npfield):
63
+ pass