npdict 0.0.1__py3-none-any.whl

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.
npdict/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+
2
+ from .wrap import NumpyNDArrayWrappedDict
npdict/utils.py ADDED
@@ -0,0 +1,18 @@
1
+
2
+
3
+ from typing import Tuple
4
+
5
+
6
+ class DuplicatedKeyError(Exception):
7
+ def __init__(self):
8
+ self.message = "Duplicated keys!"
9
+
10
+
11
+ class WrongArrayDimensionException(Exception):
12
+ def __init__(self, expected_dimension: int, given_dimensions: int):
13
+ self.message = f"Expected dimension: {expected_dimension}, but {given_dimensions} dimensions are given!"
14
+
15
+
16
+ class WrongArrayShapeException(Exception):
17
+ def __init__(self, expected_shape: Tuple[int, ...], given_shape: Tuple[int, ...]):
18
+ self.message = f"Expected shape: {', '.join(str(dim_len) for dim_len in expected_shape)}, but the given array shape is {', '.join(str(dim_len) for dim_len in given_shape)}!"
npdict/wrap.py ADDED
@@ -0,0 +1,143 @@
1
+
2
+ from typing import Tuple, Generator
3
+ import sys
4
+ from itertools import product
5
+ from functools import reduce
6
+
7
+ import numpy as np
8
+
9
+ if sys.version_info < (3, 11):
10
+ from typing_extensions import Self
11
+ else:
12
+ from typing import Self
13
+
14
+ from .utils import DuplicatedKeyError, WrongArrayDimensionException, WrongArrayShapeException
15
+
16
+
17
+ class NumpyNDArrayWrappedDict(dict):
18
+ def __init__(
19
+ self,
20
+ lists_keystrings: list[list[str]],
21
+ default_initial_value: float=0.0
22
+ ):
23
+ super(dict, self).__init__()
24
+ for list_keystrings in lists_keystrings:
25
+ if (len(list_keystrings)) != len(set(list_keystrings)):
26
+ raise DuplicatedKeyError()
27
+ self._lists_keystrings = lists_keystrings
28
+ self._keystrings_to_indices = [
29
+ {
30
+ keyword: idx for idx, keyword in enumerate(list_keystrings)
31
+ }
32
+ for list_keystrings in self._lists_keystrings
33
+ ]
34
+
35
+ self._tensor_dimensions = len(self._lists_keystrings)
36
+ self._dimension_sizes = [len(l) for l in self._lists_keystrings]
37
+ self._total_size = reduce(lambda a, b: a*b, self._dimension_sizes)
38
+
39
+ self._numpyarray = np.empty(tuple(len(l) for l in self._lists_keystrings))
40
+ self._numpyarray.fill(default_initial_value)
41
+
42
+ def _get_indices(self, item: Tuple[str, ...]) -> list[int]:
43
+ return [
44
+ mapping[keyword]
45
+ for mapping, keyword in zip(self._keystrings_to_indices, item)
46
+ ]
47
+
48
+ def __getitem__(self, item: Tuple[str, ...]) -> float:
49
+ if len(item) != self.tensor_dimensions:
50
+ raise WrongArrayDimensionException(self.tensor_dimensions, len(item))
51
+ indices = self._get_indices(item)
52
+ return self._numpyarray[tuple(indices)]
53
+
54
+ def __setitem__(self, key: Tuple[str, ...], value: float) -> None:
55
+ if len(key) != self.tensor_dimensions:
56
+ raise WrongArrayDimensionException(self.tensor_dimensions, len(key))
57
+ indices = self._get_indices(key)
58
+ self._numpyarray[tuple(indices)] = value
59
+
60
+ def update(self, new_dict: dict):
61
+ raise TypeError("We cannot update this kind of dict this way!")
62
+
63
+ def __iter__(self) -> Generator[Tuple[str, ...], None, None]:
64
+ for keywords_tuple in product(*self._lists_keystrings):
65
+ yield keywords_tuple
66
+
67
+ def keys(self):
68
+ return list(self.__iter__())
69
+
70
+ def values(self):
71
+ return [self.__getitem__(keywords_tuple) for keywords_tuple in self.__iter__()]
72
+
73
+ def items(self):
74
+ return [
75
+ (keywords_tuple, self.__getitem__(keywords_tuple))
76
+ for keywords_tuple in self.__iter__()
77
+ ]
78
+
79
+ def to_numpy(self) -> np.ndarray:
80
+ return self._numpyarray
81
+
82
+ def generate_dict(self, nparray: np.ndarray) -> Self:
83
+ if len(nparray.shape) != self.tensor_dimensions:
84
+ raise WrongArrayDimensionException(self.tensor_dimensions, len(nparray.shape))
85
+ if nparray.shape != self._numpyarray.shape:
86
+ raise WrongArrayShapeException(self._numpyarray.shape, nparray.shape)
87
+ wrapped_dict = NumpyNDArrayWrappedDict(self._lists_keystrings)
88
+ wrapped_dict._numpyarray = nparray
89
+ return wrapped_dict
90
+
91
+ def __repr__(self) -> str:
92
+ return f"<NumpyNDArrayWrappedDict: dimensions ({', '.join(map(str, self.dimension_sizes))})>"
93
+
94
+ def __str__(self) -> str:
95
+ return self.__repr__()
96
+
97
+ def __len__(self) -> int:
98
+ return self._total_size
99
+
100
+ def to_dict(self) -> dict[Tuple[str, ...], float]:
101
+ return {
102
+ keywords_tuple: value for keywords_tuple, value in self.items()
103
+ }
104
+
105
+ @classmethod
106
+ def from_dict_given_keywords(
107
+ cls,
108
+ lists_keywords: list[list[str]],
109
+ oridict: dict[Tuple[str, ...], float],
110
+ default_initial_value: float = 0.0
111
+ ) -> Self:
112
+ wrapped_dict = NumpyNDArrayWrappedDict(
113
+ lists_keywords,
114
+ default_initial_value=default_initial_value
115
+ )
116
+ for keywords_tuple in product(*lists_keywords):
117
+ wrapped_dict[keywords_tuple] = oridict.get(keywords_tuple, default_initial_value)
118
+ return wrapped_dict
119
+
120
+ @classmethod
121
+ def from_dict(
122
+ cls,
123
+ oridict: dict[Tuple[str, ...], float],
124
+ default_initial_value: float = 0.0
125
+ ) -> Self:
126
+ nbdims = len(next(iter(oridict)))
127
+ lists_keystrings = [
128
+ list(set(keystring[i] for keystring in oridict.keys()))
129
+ for i in range(nbdims)
130
+ ]
131
+ return cls.from_dict_given_keywords(
132
+ lists_keystrings,
133
+ oridict,
134
+ default_initial_value=default_initial_value
135
+ )
136
+
137
+ @property
138
+ def tensor_dimensions(self) -> int:
139
+ return self._tensor_dimensions
140
+
141
+ @property
142
+ def dimension_sizes(self) -> list[int]:
143
+ return self._dimension_sizes
@@ -0,0 +1,171 @@
1
+ Metadata-Version: 2.4
2
+ Name: npdict
3
+ Version: 0.0.1
4
+ Summary: A Python dictionary wrapper for numpy arrays
5
+ Author-email: Kwan Yuet Stephen Ho <stephenhky@yahoo.com.hk>
6
+ License: MIT
7
+ Project-URL: Repository, https://github.com/stephenhky/npdict
8
+ Project-URL: Issues, https://github.com/stephenhky/npdict/issues
9
+ Keywords: dictionary,numpy
10
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
13
+ Classifier: Topic :: Software Development :: Version Control :: Git
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Intended Audience :: Science/Research
20
+ Classifier: Intended Audience :: Developers
21
+ Requires-Python: >=3.9
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: numpy
25
+ Requires-Dist: typing-extensions
26
+ Provides-Extra: test
27
+ Requires-Dist: pytest; extra == "test"
28
+ Dynamic: license-file
29
+
30
+
31
+ [![GitHub release](https://img.shields.io/github/release/stephenhky/npdict.svg?maxAge=3600)](https://github.com/stephenhky/npdict/releases)
32
+ [![pypi](https://img.shields.io/pypi/v/npdict.svg?maxAge=3600)](https://pypi.org/project/npdict/)
33
+ [![download](https://img.shields.io/pypi/dm/npdict.svg?maxAge=2592000&label=installs&color=%2327B1FF)](https://pypi.org/project/npdict/)
34
+
35
+ # `npdict`: Python Package for Dictionary Wrappers for Numpy Arrays
36
+
37
+ This Python package, `npdict`, aims at facilitating holding numerical
38
+ values in a Python dictionary, but at the same time retaining the
39
+ ultra-high performance supported by NumPy. It supports an object
40
+ which is a Python dictionary on the surface, but numpy behind the
41
+ back, facilitating fast assignment and retrieval of values
42
+ and fast computation of numpy arrays.
43
+
44
+ ## Installation
45
+
46
+ To install, in your terminal, simply enter:
47
+
48
+ ```
49
+ pip install npdict
50
+ ```
51
+
52
+ ## Quickstart
53
+
54
+ ### Instantiation
55
+
56
+ Suppose you are doing a similarity dictionary between two sets of words.
57
+ And each of these sets have words:
58
+
59
+ ```
60
+ document1 = ['president', 'computer', 'tree']
61
+ document2 = ['chairman', 'abacus', 'trees']
62
+ ```
63
+
64
+ And you can build a dictionary like this:
65
+
66
+ ```
67
+ import numpy as np
68
+ from npdict import NumpyNDArrayWrappedDict
69
+
70
+ similarity_dict = NumpyNDArrayWrappedDict([document1, document2])
71
+ ```
72
+
73
+ An `npdict.NumpyNDArrayWrappedDict` instance is instantiated. It is
74
+ a Python dict:
75
+
76
+ ```
77
+ isinstance(similarity_dict, dict) # which gives `True`
78
+ ```
79
+
80
+ It has a matrix inside with default value 0.0 (and the initial default value can
81
+ be changed to other values when the instance is instantiated.)
82
+
83
+ ```
84
+ similarity_dict.to_numpy()
85
+ ```
86
+ giving
87
+ ```
88
+ array([[0., 0., 0.],
89
+ [0., 0., 0.],
90
+ [0., 0., 0.]])
91
+ ```
92
+
93
+ ### Value Assignments
94
+
95
+ Now you can assign values just like what you do to a Python dictionary:
96
+
97
+ ```
98
+ similarity_dict['president', 'chairman'] = 0.9
99
+ similarity_dict['computer', 'abacus'] = 0.7
100
+ similarity_dict['tree', 'trees'] = 0.95
101
+ ```
102
+
103
+ And it has changed the inside numpy array to be:
104
+
105
+ ```
106
+ array([[0.9 , 0. , 0. ],
107
+ [0. , 0.7 , 0. ],
108
+ [0. , 0. , 0.95]])
109
+ ```
110
+
111
+ ### Generation of New Object from the Old One
112
+
113
+ If you want to create another dict using the same words, but
114
+ a manipulation of the original value, 25 percent discount
115
+ of the original one for example, you can do something like this:
116
+
117
+ ```
118
+ new_similarity_dict = similarity_dict.generate_dict(similarity_dict.to_numpy()*0.75)
119
+ ```
120
+
121
+ And you got a new dictionary with numpy array to be:
122
+
123
+ ```
124
+ new_similarity_dict.to_numpy()
125
+ ```
126
+ giving
127
+ ```
128
+ array([[0.675 , 0. , 0. ],
129
+ [0. , 0.525 , 0. ],
130
+ [0. , 0. , 0.7125]])
131
+ ```
132
+
133
+ This is a simple operation. But the design of this wrapped Python
134
+ dictionary is that you can perform any fast or optimized operation
135
+ on your numpy array (using numba or Cython, for examples),
136
+ while retaining the keywords as your dictionary.
137
+
138
+ ### Retrieval of Values
139
+
140
+ At the same time, you can set new values just like above, or retrieve
141
+ values as if it is a Python dictionary:
142
+
143
+ ```
144
+ similarity_dict['president', 'chairman']
145
+ ```
146
+
147
+ ### Conversion to a Python Dictionary
148
+
149
+ You can also convert this to an ordinary Python dictionary:
150
+
151
+ ```
152
+ raw_similarity_dict = similarity_dict.to_dict()
153
+ ```
154
+
155
+ ### Instantiation from a Python Dictionary
156
+
157
+ And you can convert a Python dictionary of this type back to
158
+ `npdict.NumpyNDArrayWrappedDict` by (recommended)
159
+
160
+ ```
161
+ new_similarity_dict_2 = NumpyNDArrayWrappedDict.from_dict_given_keywords([document1, document2], raw_similarity_dict)
162
+ ```
163
+
164
+ Or you can even do this (not recommended):
165
+
166
+ ```
167
+ new_similarity_dict_3 = NumpyNDArrayWrappedDict.from_dict(raw_similarity_dict)
168
+ ```
169
+
170
+ It is not recommended because the order of the keys are not retained in this way.
171
+ Use it with caution.
@@ -0,0 +1,8 @@
1
+ npdict/__init__.py,sha256=ux0JiBWDLIy_L-ecnTyQggbvqyDWGnA8ugoaux-ToG8,43
2
+ npdict/utils.py,sha256=qKyXJuO3Nm1oLjX4tSbvWzqMkCieDH6sIC-ZO_CD0E4,680
3
+ npdict/wrap.py,sha256=74seK4p5VDl73N3wG_6fqEd6BQO86eB4tQZWPw7m9rM,4902
4
+ npdict-0.0.1.dist-info/licenses/LICENSE,sha256=ufwgIO2ky6BJ35g3fjy2fgU7-tkFlYu9C5K3WaYaPaA,1063
5
+ npdict-0.0.1.dist-info/METADATA,sha256=lWFxoUEdOnZbYPaDI2wsLyLOm0EHcyzp_cSLgEb_7cg,4905
6
+ npdict-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ npdict-0.0.1.dist-info/top_level.txt,sha256=6UZJ6RoYitrWz9KNuPH5SwIKL83tz5A0AluK29GoOD8,7
8
+ npdict-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2025 Kwan Yuet Stephen Ho
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ npdict