relib 1.0.9__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.
- relib-1.0.9/.gitignore +3 -0
- relib-1.0.9/LICENSE +7 -0
- relib-1.0.9/PKG-INFO +16 -0
- relib-1.0.9/README.md +0 -0
- relib-1.0.9/pyproject.toml +24 -0
- relib-1.0.9/relib/__init__.py +31 -0
- relib-1.0.9/relib/hashing.py +255 -0
- relib-1.0.9/relib/measure_duration.py +20 -0
- relib-1.0.9/relib/raypipe.py +60 -0
- relib-1.0.9/relib/utils.py +208 -0
- relib-1.0.9/uv.lock +62 -0
relib-1.0.9/.gitignore
ADDED
relib-1.0.9/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2023 Hampus Hallman
|
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.
|
relib-1.0.9/PKG-INFO
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Metadata-Version: 2.3
|
2
|
+
Name: relib
|
3
|
+
Version: 1.0.9
|
4
|
+
Project-URL: Repository, https://github.com/Reddan/relib.git
|
5
|
+
Author: Hampus Hallman
|
6
|
+
License: Copyright 2023 Hampus Hallman
|
7
|
+
|
8
|
+
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:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
11
|
+
|
12
|
+
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.
|
13
|
+
License-File: LICENSE
|
14
|
+
Requires-Python: >=3.12
|
15
|
+
Requires-Dist: numpy>=2.1.0
|
16
|
+
Requires-Dist: termcolor>=2.4.0
|
relib-1.0.9/README.md
ADDED
File without changes
|
@@ -0,0 +1,24 @@
|
|
1
|
+
[project]
|
2
|
+
name = "relib"
|
3
|
+
version = "1.0.9"
|
4
|
+
requires-python = ">=3.12"
|
5
|
+
dependencies = [
|
6
|
+
"numpy>=2.1.0",
|
7
|
+
"termcolor>=2.4.0",
|
8
|
+
]
|
9
|
+
authors = [
|
10
|
+
{name = "Hampus Hallman"}
|
11
|
+
]
|
12
|
+
# description = ""
|
13
|
+
readme = "README.md"
|
14
|
+
license = {file = "LICENSE"}
|
15
|
+
|
16
|
+
[project.urls]
|
17
|
+
Repository = "https://github.com/Reddan/relib.git"
|
18
|
+
|
19
|
+
[build-system]
|
20
|
+
requires = ["hatchling"]
|
21
|
+
build-backend = "hatchling.build"
|
22
|
+
|
23
|
+
[tool.hatch.build.targets.wheel]
|
24
|
+
packages = ["relib"]
|
@@ -0,0 +1,31 @@
|
|
1
|
+
from .utils import (
|
2
|
+
non_none,
|
3
|
+
list_split,
|
4
|
+
drop_none,
|
5
|
+
distinct,
|
6
|
+
first,
|
7
|
+
move_value,
|
8
|
+
transpose_dict,
|
9
|
+
make_combinations_by_dict,
|
10
|
+
merge_dicts,
|
11
|
+
intersect,
|
12
|
+
ensure_tuple,
|
13
|
+
key_of,
|
14
|
+
omit,
|
15
|
+
pick,
|
16
|
+
dict_by,
|
17
|
+
tuple_by,
|
18
|
+
flatten,
|
19
|
+
transpose,
|
20
|
+
map_dict,
|
21
|
+
deepen_dict,
|
22
|
+
group,
|
23
|
+
sized_partitions,
|
24
|
+
num_partitions,
|
25
|
+
df_from_array,
|
26
|
+
StrFilter,
|
27
|
+
str_filterer,
|
28
|
+
)
|
29
|
+
from .raypipe import raypipe
|
30
|
+
from .hashing import hash
|
31
|
+
from .measure_duration import measure_duration
|
@@ -0,0 +1,255 @@
|
|
1
|
+
"""
|
2
|
+
Fast cryptographic hash of Python objects, with a special case for fast
|
3
|
+
hashing of numpy arrays.
|
4
|
+
"""
|
5
|
+
|
6
|
+
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
|
7
|
+
# Copyright (c) 2009 Gael Varoquaux
|
8
|
+
# License: BSD Style, 3 clauses.
|
9
|
+
|
10
|
+
import pickle
|
11
|
+
import hashlib
|
12
|
+
import sys
|
13
|
+
import types
|
14
|
+
import struct
|
15
|
+
import io
|
16
|
+
import decimal
|
17
|
+
|
18
|
+
Pickler = pickle._Pickler
|
19
|
+
_bytes_or_unicode = (bytes, str)
|
20
|
+
|
21
|
+
|
22
|
+
class _ConsistentSet(object):
|
23
|
+
""" Class used to ensure the hash of Sets is preserved
|
24
|
+
whatever the order of its items.
|
25
|
+
"""
|
26
|
+
def __init__(self, set_sequence):
|
27
|
+
# Forces order of elements in set to ensure consistent hash.
|
28
|
+
try:
|
29
|
+
# Trying first to order the set assuming the type of elements is
|
30
|
+
# consistent and orderable.
|
31
|
+
# This fails on python 3 when elements are unorderable
|
32
|
+
# but we keep it in a try as it's faster.
|
33
|
+
self._sequence = sorted(set_sequence)
|
34
|
+
except (TypeError, decimal.InvalidOperation):
|
35
|
+
# If elements are unorderable, sorting them using their hash.
|
36
|
+
# This is slower but works in any case.
|
37
|
+
self._sequence = sorted((hash(e) for e in set_sequence))
|
38
|
+
|
39
|
+
|
40
|
+
class _MyHash(object):
|
41
|
+
""" Class used to hash objects that won't normally pickle """
|
42
|
+
|
43
|
+
def __init__(self, *args):
|
44
|
+
self.args = args
|
45
|
+
|
46
|
+
|
47
|
+
class Hasher(Pickler):
|
48
|
+
""" A subclass of pickler, to do cryptographic hashing, rather than
|
49
|
+
pickling.
|
50
|
+
"""
|
51
|
+
|
52
|
+
def __init__(self, hash_name='md5'):
|
53
|
+
self.stream = io.BytesIO()
|
54
|
+
# By default we want a pickle protocol that only changes with
|
55
|
+
# the major python version and not the minor one
|
56
|
+
protocol = pickle.HIGHEST_PROTOCOL
|
57
|
+
Pickler.__init__(self, self.stream, protocol=protocol)
|
58
|
+
# Initialise the hash obj
|
59
|
+
self._hash = hashlib.new(hash_name)
|
60
|
+
|
61
|
+
def hash(self, obj, return_digest=True):
|
62
|
+
try:
|
63
|
+
self.dump(obj)
|
64
|
+
except pickle.PicklingError as e:
|
65
|
+
e.args += ('PicklingError while hashing %r: %r' % (obj, e),)
|
66
|
+
raise
|
67
|
+
dumps = self.stream.getvalue()
|
68
|
+
self._hash.update(dumps)
|
69
|
+
if return_digest:
|
70
|
+
return self._hash.hexdigest()
|
71
|
+
|
72
|
+
def save(self, obj):
|
73
|
+
if isinstance(obj, (types.MethodType, type({}.pop))):
|
74
|
+
# the Pickler cannot pickle instance methods; here we decompose
|
75
|
+
# them into components that make them uniquely identifiable
|
76
|
+
if hasattr(obj, '__func__'):
|
77
|
+
func_name = obj.__func__.__name__
|
78
|
+
else:
|
79
|
+
func_name = obj.__name__
|
80
|
+
inst = obj.__self__
|
81
|
+
if type(inst) == type(pickle):
|
82
|
+
obj = _MyHash(func_name, inst.__name__)
|
83
|
+
elif inst is None:
|
84
|
+
# type(None) or type(module) do not pickle
|
85
|
+
obj = _MyHash(func_name, inst)
|
86
|
+
else:
|
87
|
+
cls = obj.__self__.__class__
|
88
|
+
obj = _MyHash(func_name, inst, cls)
|
89
|
+
Pickler.save(self, obj)
|
90
|
+
|
91
|
+
def memoize(self, obj):
|
92
|
+
# We want hashing to be sensitive to value instead of reference.
|
93
|
+
# For example we want ['aa', 'aa'] and ['aa', 'aaZ'[:2]]
|
94
|
+
# to hash to the same value and that's why we disable memoization
|
95
|
+
# for strings
|
96
|
+
if isinstance(obj, _bytes_or_unicode):
|
97
|
+
return
|
98
|
+
Pickler.memoize(self, obj)
|
99
|
+
|
100
|
+
# The dispatch table of the pickler is not accessible in Python
|
101
|
+
# 3, as these lines are only bugware for IPython, we skip them.
|
102
|
+
def save_global(self, obj, name=None, pack=struct.pack):
|
103
|
+
# We have to override this method in order to deal with objects
|
104
|
+
# defined interactively in IPython that are not injected in
|
105
|
+
# __main__
|
106
|
+
kwargs = dict(name=name, pack=pack)
|
107
|
+
if sys.version_info >= (3, 4):
|
108
|
+
del kwargs['pack']
|
109
|
+
try:
|
110
|
+
Pickler.save_global(self, obj, **kwargs)
|
111
|
+
except pickle.PicklingError:
|
112
|
+
Pickler.save_global(self, obj, **kwargs)
|
113
|
+
module = getattr(obj, "__module__", None)
|
114
|
+
if module == '__main__':
|
115
|
+
my_name = name
|
116
|
+
if my_name is None:
|
117
|
+
my_name = obj.__name__
|
118
|
+
mod = sys.modules[module]
|
119
|
+
if not hasattr(mod, my_name):
|
120
|
+
# IPython doesn't inject the variables define
|
121
|
+
# interactively in __main__
|
122
|
+
setattr(mod, my_name, obj)
|
123
|
+
|
124
|
+
dispatch = Pickler.dispatch.copy()
|
125
|
+
# builtin
|
126
|
+
dispatch[type(len)] = save_global
|
127
|
+
# type
|
128
|
+
dispatch[type(object)] = save_global
|
129
|
+
# classobj
|
130
|
+
dispatch[type(Pickler)] = save_global
|
131
|
+
# function
|
132
|
+
dispatch[type(pickle.dump)] = save_global
|
133
|
+
|
134
|
+
def _batch_setitems(self, items):
|
135
|
+
# forces order of keys in dict to ensure consistent hash.
|
136
|
+
try:
|
137
|
+
# Trying first to compare dict assuming the type of keys is
|
138
|
+
# consistent and orderable.
|
139
|
+
# This fails on python 3 when keys are unorderable
|
140
|
+
# but we keep it in a try as it's faster.
|
141
|
+
Pickler._batch_setitems(self, iter(sorted(items)))
|
142
|
+
except TypeError:
|
143
|
+
# If keys are unorderable, sorting them using their hash. This is
|
144
|
+
# slower but works in any case.
|
145
|
+
Pickler._batch_setitems(self, iter(sorted((hash(k), v)
|
146
|
+
for k, v in items)))
|
147
|
+
|
148
|
+
def save_set(self, set_items):
|
149
|
+
# forces order of items in Set to ensure consistent hash
|
150
|
+
Pickler.save(self, _ConsistentSet(set_items))
|
151
|
+
|
152
|
+
dispatch[type(set())] = save_set
|
153
|
+
|
154
|
+
|
155
|
+
class NumpyHasher(Hasher):
|
156
|
+
""" Special case the hasher for when numpy is loaded.
|
157
|
+
"""
|
158
|
+
|
159
|
+
def __init__(self, hash_name='md5', coerce_mmap=False):
|
160
|
+
"""
|
161
|
+
Parameters
|
162
|
+
----------
|
163
|
+
hash_name: string
|
164
|
+
The hash algorithm to be used
|
165
|
+
coerce_mmap: boolean
|
166
|
+
Make no difference between np.memmap and np.ndarray
|
167
|
+
objects.
|
168
|
+
"""
|
169
|
+
self.coerce_mmap = coerce_mmap
|
170
|
+
Hasher.__init__(self, hash_name=hash_name)
|
171
|
+
# delayed import of numpy, to avoid tight coupling
|
172
|
+
import numpy as np
|
173
|
+
self.np = np
|
174
|
+
if hasattr(np, 'getbuffer'):
|
175
|
+
self._getbuffer = np.getbuffer
|
176
|
+
else:
|
177
|
+
self._getbuffer = memoryview
|
178
|
+
|
179
|
+
def save(self, obj):
|
180
|
+
""" Subclass the save method, to hash ndarray subclass, rather
|
181
|
+
than pickling them. Off course, this is a total abuse of
|
182
|
+
the Pickler class.
|
183
|
+
"""
|
184
|
+
if isinstance(obj, self.np.ndarray) and not obj.dtype.hasobject:
|
185
|
+
# Compute a hash of the object
|
186
|
+
# The update function of the hash requires a c_contiguous buffer.
|
187
|
+
if obj.shape == ():
|
188
|
+
# 0d arrays need to be flattened because viewing them as bytes
|
189
|
+
# raises a ValueError exception.
|
190
|
+
obj_c_contiguous = obj.flatten()
|
191
|
+
elif obj.flags.c_contiguous:
|
192
|
+
obj_c_contiguous = obj
|
193
|
+
elif obj.flags.f_contiguous:
|
194
|
+
obj_c_contiguous = obj.T
|
195
|
+
else:
|
196
|
+
# Cater for non-single-segment arrays: this creates a
|
197
|
+
# copy, and thus aleviates this issue.
|
198
|
+
# XXX: There might be a more efficient way of doing this
|
199
|
+
obj_c_contiguous = obj.flatten()
|
200
|
+
|
201
|
+
# memoryview is not supported for some dtypes, e.g. datetime64, see
|
202
|
+
# https://github.com/numpy/numpy/issues/4983. The
|
203
|
+
# workaround is to view the array as bytes before
|
204
|
+
# taking the memoryview.
|
205
|
+
self._hash.update(
|
206
|
+
self._getbuffer(obj_c_contiguous.view(self.np.uint8)))
|
207
|
+
|
208
|
+
# We store the class, to be able to distinguish between
|
209
|
+
# Objects with the same binary content, but different
|
210
|
+
# classes.
|
211
|
+
if self.coerce_mmap and isinstance(obj, self.np.memmap):
|
212
|
+
# We don't make the difference between memmap and
|
213
|
+
# normal ndarrays, to be able to reload previously
|
214
|
+
# computed results with memmap.
|
215
|
+
klass = self.np.ndarray
|
216
|
+
else:
|
217
|
+
klass = obj.__class__
|
218
|
+
# We also return the dtype and the shape, to distinguish
|
219
|
+
# different views on the same data with different dtypes.
|
220
|
+
|
221
|
+
# The object will be pickled by the pickler hashed at the end.
|
222
|
+
obj = (klass, ('HASHED', obj.dtype, obj.shape, obj.strides))
|
223
|
+
elif isinstance(obj, self.np.dtype):
|
224
|
+
# Atomic dtype objects are interned by their default constructor:
|
225
|
+
# np.dtype('f8') is np.dtype('f8')
|
226
|
+
# This interning is not maintained by a
|
227
|
+
# pickle.loads + pickle.dumps cycle, because __reduce__
|
228
|
+
# uses copy=True in the dtype constructor. This
|
229
|
+
# non-deterministic behavior causes the internal memoizer
|
230
|
+
# of the hasher to generate different hash values
|
231
|
+
# depending on the history of the dtype object.
|
232
|
+
# To prevent the hash from being sensitive to this, we use
|
233
|
+
# .descr which is a full (and never interned) description of
|
234
|
+
# the array dtype according to the numpy doc.
|
235
|
+
klass = obj.__class__
|
236
|
+
obj = (klass, ('HASHED', obj.descr))
|
237
|
+
Hasher.save(self, obj)
|
238
|
+
|
239
|
+
|
240
|
+
def hash(obj, hash_name='md5', coerce_mmap=False):
|
241
|
+
""" Quick calculation of a hash to identify uniquely Python objects
|
242
|
+
containing numpy arrays.
|
243
|
+
Parameters
|
244
|
+
-----------
|
245
|
+
hash_name: 'md5' or 'sha1'
|
246
|
+
Hashing algorithm used. sha1 is supposedly safer, but md5 is
|
247
|
+
faster.
|
248
|
+
coerce_mmap: boolean
|
249
|
+
Make no difference between np.memmap and np.ndarray
|
250
|
+
"""
|
251
|
+
if 'numpy' in sys.modules:
|
252
|
+
hasher = NumpyHasher(hash_name=hash_name, coerce_mmap=coerce_mmap)
|
253
|
+
else:
|
254
|
+
hasher = Hasher(hash_name=hash_name)
|
255
|
+
return hasher.hash(obj)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
from time import time
|
2
|
+
from termcolor import colored
|
3
|
+
|
4
|
+
active_mds = []
|
5
|
+
|
6
|
+
class measure_duration:
|
7
|
+
def __init__(self, name):
|
8
|
+
self.name = name
|
9
|
+
active_mds.append(self)
|
10
|
+
|
11
|
+
def __enter__(self):
|
12
|
+
self.start = time()
|
13
|
+
|
14
|
+
def __exit__(self, *_):
|
15
|
+
duration = round(time() - self.start, 4)
|
16
|
+
depth = len(active_mds) - 1
|
17
|
+
indent = ('──' * depth) + (' ' * (depth > 0))
|
18
|
+
text = '{}: {} seconds'.format(self.name, duration)
|
19
|
+
print(colored(indent + text, attrs=['dark']))
|
20
|
+
active_mds.remove(self)
|
@@ -0,0 +1,60 @@
|
|
1
|
+
import numpy as np
|
2
|
+
|
3
|
+
class Raypipe():
|
4
|
+
def __init__(self, handlers=[]):
|
5
|
+
self.handlers = handlers
|
6
|
+
|
7
|
+
def __add_to_pipeline(self, handler_type, fn, kwargs={}):
|
8
|
+
handler = (handler_type, fn, kwargs)
|
9
|
+
return Raypipe(self.handlers + [handler])
|
10
|
+
|
11
|
+
def map(self, fn):
|
12
|
+
return self.__add_to_pipeline('map', fn)
|
13
|
+
|
14
|
+
def flatten(self):
|
15
|
+
return self.__add_to_pipeline('flatten', None)
|
16
|
+
|
17
|
+
def flat_map(self, fn):
|
18
|
+
return self.map(fn).flatten()
|
19
|
+
|
20
|
+
def filter(self, fn):
|
21
|
+
return self.__add_to_pipeline('filter', fn)
|
22
|
+
|
23
|
+
def sort(self, fn=None, reverse=False):
|
24
|
+
return self.__add_to_pipeline('sort', fn, dict(reverse=reverse))
|
25
|
+
|
26
|
+
def distinct(self):
|
27
|
+
return self.__add_to_pipeline('distinct', None)
|
28
|
+
|
29
|
+
def sort_distinct(self, fn=None, reverse=False):
|
30
|
+
return self.distinct().sort(fn, reverse=reverse)
|
31
|
+
|
32
|
+
def do(self, fn):
|
33
|
+
return self.__add_to_pipeline('do', fn)
|
34
|
+
|
35
|
+
def shuffle(self, random_state=42):
|
36
|
+
return self.__add_to_pipeline('shuffle', None, dict(random_state=random_state))
|
37
|
+
|
38
|
+
def to_numpy(self):
|
39
|
+
return self.__add_to_pipeline('do', np.array)
|
40
|
+
|
41
|
+
def compute(self, values):
|
42
|
+
for handler_type, handler_fn, handler_kwargs in self.handlers:
|
43
|
+
if handler_type == 'map':
|
44
|
+
values = [handler_fn(val) for val in values]
|
45
|
+
elif handler_type == 'flatten':
|
46
|
+
values = [item for sublist in values for item in sublist]
|
47
|
+
elif handler_type == 'filter':
|
48
|
+
values = [val for val in values if handler_fn(val)]
|
49
|
+
elif handler_type == 'sort':
|
50
|
+
values.sort(key=handler_fn, reverse=handler_kwargs['reverse'])
|
51
|
+
elif handler_type == 'distinct':
|
52
|
+
values = list(set(values))
|
53
|
+
elif handler_type == 'do':
|
54
|
+
values = handler_fn(values)
|
55
|
+
elif handler_type == 'shuffle':
|
56
|
+
from sklearn.utils import shuffle
|
57
|
+
values = shuffle(values, random_state=handler_kwargs['random_state'])
|
58
|
+
return values
|
59
|
+
|
60
|
+
raypipe = Raypipe()
|
@@ -0,0 +1,208 @@
|
|
1
|
+
from typing import TypeVar, Iterable, Callable, Any, cast, overload
|
2
|
+
from itertools import chain
|
3
|
+
import numpy as np
|
4
|
+
import re
|
5
|
+
|
6
|
+
T = TypeVar('T')
|
7
|
+
U = TypeVar('U')
|
8
|
+
K = TypeVar('K')
|
9
|
+
K1, K2, K3, K4, K5, K6 = TypeVar('K1'), TypeVar('K2'), TypeVar('K3'), TypeVar('K4'), TypeVar('K5'), TypeVar('K6')
|
10
|
+
|
11
|
+
def non_none(obj: T | None) -> T:
|
12
|
+
assert obj is not None
|
13
|
+
return obj
|
14
|
+
|
15
|
+
def list_split(l: list[T], sep: T) -> list[list[T]]:
|
16
|
+
l = [sep, *l, sep]
|
17
|
+
split_at = [i for i, x in enumerate(l) if x is sep]
|
18
|
+
ranges = list(zip(split_at[0:-1], split_at[1:]))
|
19
|
+
return [
|
20
|
+
l[start + 1:end]
|
21
|
+
for start, end in ranges
|
22
|
+
]
|
23
|
+
|
24
|
+
def drop_none(l: Iterable[T | None]) -> list[T]:
|
25
|
+
return [x for x in l if x is not None]
|
26
|
+
|
27
|
+
def distinct(items: Iterable[T]) -> list[T]:
|
28
|
+
return list(set(items))
|
29
|
+
|
30
|
+
def first(iterable: Iterable[T]) -> T | None:
|
31
|
+
return next(iter(iterable), None)
|
32
|
+
|
33
|
+
def move_value(l: Iterable[T], from_i: int, to_i: int) -> list[T]:
|
34
|
+
l = list(l)
|
35
|
+
l.insert(to_i, l.pop(from_i))
|
36
|
+
return l
|
37
|
+
|
38
|
+
def transpose_dict(des):
|
39
|
+
if isinstance(des, list):
|
40
|
+
keys = list(des[0].keys()) if des else []
|
41
|
+
length = len(des)
|
42
|
+
return {
|
43
|
+
key: [des[i][key] for i in range(length)]
|
44
|
+
for key in keys
|
45
|
+
}
|
46
|
+
elif isinstance(des, dict):
|
47
|
+
keys = list(des.keys())
|
48
|
+
length = len(des[keys[0]]) if keys else 0
|
49
|
+
return [
|
50
|
+
{key: des[key][i] for key in keys}
|
51
|
+
for i in range(length)
|
52
|
+
]
|
53
|
+
raise ValueError('transpose_dict only accepts dict or list')
|
54
|
+
|
55
|
+
def make_combinations_by_dict(des, keys=None, pairs=[]):
|
56
|
+
keys = sorted(des.keys()) if keys == None else keys
|
57
|
+
if len(keys) == 0:
|
58
|
+
return [dict(pairs)]
|
59
|
+
key = keys[0]
|
60
|
+
remaining_keys = keys[1:]
|
61
|
+
new_pairs = [(key, val) for val in des[key]]
|
62
|
+
return flatten([
|
63
|
+
make_combinations_by_dict(des, remaining_keys, [pair] + pairs)
|
64
|
+
for pair in new_pairs
|
65
|
+
])
|
66
|
+
|
67
|
+
def merge_dicts(*dicts: dict[K, T]) -> dict[K, T]:
|
68
|
+
if len(dicts) == 1:
|
69
|
+
return dicts[0]
|
70
|
+
result = {}
|
71
|
+
for d in dicts:
|
72
|
+
result.update(d)
|
73
|
+
return result
|
74
|
+
|
75
|
+
def intersect(*lists: Iterable[T]) -> list[T]:
|
76
|
+
return list(set.intersection(*map(set, lists)))
|
77
|
+
|
78
|
+
def ensure_tuple(value: T | tuple[T, ...]) -> tuple[T, ...]:
|
79
|
+
return value if isinstance(value, tuple) else (value,)
|
80
|
+
|
81
|
+
def key_of(dicts: Iterable[dict[T, U]], key: T) -> list[U]:
|
82
|
+
return [d[key] for d in dicts]
|
83
|
+
|
84
|
+
def omit(d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
|
85
|
+
if keys:
|
86
|
+
d = dict(d)
|
87
|
+
for key in keys:
|
88
|
+
del d[key]
|
89
|
+
return d
|
90
|
+
|
91
|
+
def pick(d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
|
92
|
+
return {key: d[key] for key in keys}
|
93
|
+
|
94
|
+
def dict_by(keys: Iterable[K], values: Iterable[T]) -> dict[K, T]:
|
95
|
+
return dict(zip(keys, values))
|
96
|
+
|
97
|
+
def tuple_by(d: dict[K, T], keys: Iterable[K]) -> tuple[T, ...]:
|
98
|
+
return tuple(d[key] for key in keys)
|
99
|
+
|
100
|
+
def flatten(l: Iterable[Iterable[T]]) -> list[T]:
|
101
|
+
return list(chain.from_iterable(l))
|
102
|
+
|
103
|
+
def transpose(tuples, default_num_returns=0):
|
104
|
+
output = tuple(zip(*tuples))
|
105
|
+
if not output:
|
106
|
+
return ([],) * default_num_returns
|
107
|
+
return tuple(map(list, output))
|
108
|
+
|
109
|
+
def map_dict(fn: Callable[[T], U], d: dict[K, T]) -> dict[K, U]:
|
110
|
+
return {key: fn(value) for key, value in d.items()}
|
111
|
+
|
112
|
+
@overload
|
113
|
+
def deepen_dict(d: dict[tuple[K1], U]) -> dict[K1, U]: ...
|
114
|
+
|
115
|
+
@overload
|
116
|
+
def deepen_dict(d: dict[tuple[K1, K2], U]) -> dict[K1, dict[K2, U]]: ...
|
117
|
+
|
118
|
+
@overload
|
119
|
+
def deepen_dict(d: dict[tuple[K1, K2, K3], U]) -> dict[K1, dict[K2, dict[K3, U]]]: ...
|
120
|
+
|
121
|
+
@overload
|
122
|
+
def deepen_dict(d: dict[tuple[K1, K2, K3, K4], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, U]]]]: ...
|
123
|
+
|
124
|
+
@overload
|
125
|
+
def deepen_dict(d: dict[tuple[K1, K2, K3, K4, K5], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, dict[K5, U]]]]]: ...
|
126
|
+
|
127
|
+
@overload
|
128
|
+
def deepen_dict(d: dict[tuple[K1, K2, K3, K4, K5, K6], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, dict[K5, dict[K6, U]]]]]]: ...
|
129
|
+
|
130
|
+
def deepen_dict(d: dict[tuple[Any, ...], Any]) -> dict:
|
131
|
+
output = {}
|
132
|
+
for (*tail, head), value in d.items():
|
133
|
+
curr = output
|
134
|
+
for key in tail:
|
135
|
+
if key not in curr:
|
136
|
+
curr[key] = {}
|
137
|
+
curr = curr[key]
|
138
|
+
curr[head] = value
|
139
|
+
return output
|
140
|
+
|
141
|
+
def group(pairs: Iterable[tuple[K, T]]) -> dict[K, list[T]]:
|
142
|
+
values_by_key = {}
|
143
|
+
for key, value in pairs:
|
144
|
+
if key not in values_by_key:
|
145
|
+
values_by_key[key] = []
|
146
|
+
values_by_key[key].append(value)
|
147
|
+
return values_by_key
|
148
|
+
|
149
|
+
def get_at(d: dict, keys: Iterable[Any], default: T) -> T:
|
150
|
+
try:
|
151
|
+
for key in keys:
|
152
|
+
d = d[key]
|
153
|
+
except KeyError:
|
154
|
+
return default
|
155
|
+
return cast(Any, d)
|
156
|
+
|
157
|
+
def sized_partitions(values: Iterable[T], part_size: int) -> list[list[T]]:
|
158
|
+
# "chunk"
|
159
|
+
if not isinstance(values, list):
|
160
|
+
values = list(values)
|
161
|
+
num_parts = (len(values) / part_size).__ceil__()
|
162
|
+
return [values[i * part_size:(i + 1) * part_size] for i in range(num_parts)]
|
163
|
+
|
164
|
+
def num_partitions(values: Iterable[T], num_parts: int) -> list[list[T]]:
|
165
|
+
if not isinstance(values, list):
|
166
|
+
values = list(values)
|
167
|
+
part_size = (len(values) / num_parts).__ceil__()
|
168
|
+
return [values[i * part_size:(i + 1) * part_size] for i in range(num_parts)]
|
169
|
+
|
170
|
+
def _cat_tile(cats, n_tile):
|
171
|
+
return cats[np.tile(np.arange(len(cats)), n_tile)]
|
172
|
+
|
173
|
+
def df_from_array(
|
174
|
+
value_cols: dict[str, np.ndarray],
|
175
|
+
dim_labels: list[tuple[str, list[str | int | float]]],
|
176
|
+
indexed=False,
|
177
|
+
):
|
178
|
+
import pandas as pd
|
179
|
+
dim_sizes = np.array([len(labels) for _, labels in dim_labels])
|
180
|
+
assert all(array.shape == tuple(dim_sizes) for array in value_cols.values())
|
181
|
+
array_offsets = [
|
182
|
+
(dim_sizes[i + 1:].prod(), dim_sizes[:i].prod())
|
183
|
+
for i in range(len(dim_sizes))
|
184
|
+
]
|
185
|
+
category_cols = {
|
186
|
+
dim: _cat_tile(pd.Categorical(labels).repeat(repeats), tiles)
|
187
|
+
for (dim, labels), (repeats, tiles) in zip(dim_labels, array_offsets)
|
188
|
+
}
|
189
|
+
value_cols = {name: array.reshape(-1) for name, array in value_cols.items()}
|
190
|
+
df = pd.DataFrame({**category_cols, **value_cols}, copy=False)
|
191
|
+
if indexed:
|
192
|
+
df = df.set_index([name for name, _ in dim_labels])
|
193
|
+
return df
|
194
|
+
|
195
|
+
StrFilter = Callable[[str], bool]
|
196
|
+
|
197
|
+
def str_filterer(
|
198
|
+
include_patterns: list[re.Pattern[str]] = [],
|
199
|
+
exclude_patterns: list[re.Pattern[str]] = [],
|
200
|
+
) -> StrFilter:
|
201
|
+
def str_filter(string: str) -> bool:
|
202
|
+
if any(pattern.search(string) for pattern in exclude_patterns):
|
203
|
+
return False
|
204
|
+
if not include_patterns:
|
205
|
+
return True
|
206
|
+
return any(pattern.search(string) for pattern in include_patterns)
|
207
|
+
|
208
|
+
return str_filter
|
relib-1.0.9/uv.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
version = 1
|
2
|
+
requires-python = ">=3.12"
|
3
|
+
|
4
|
+
[[package]]
|
5
|
+
name = "numpy"
|
6
|
+
version = "2.1.0"
|
7
|
+
source = { registry = "https://pypi.org/simple" }
|
8
|
+
sdist = { url = "https://files.pythonhosted.org/packages/54/a4/f8188c4f3e07f7737683588210c073478abcb542048cf4ab6fedad0b458a/numpy-2.1.0.tar.gz", hash = "sha256:7dc90da0081f7e1da49ec4e398ede6a8e9cc4f5ebe5f9e06b443ed889ee9aaa2", size = 18868922 }
|
9
|
+
wheels = [
|
10
|
+
{ url = "https://files.pythonhosted.org/packages/eb/f5/a06a231cbeea4aff841ff744a12e4bf4d4407f2c753d13ce4563aa126c90/numpy-2.1.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:fe76d75b345dc045acdbc006adcb197cc680754afd6c259de60d358d60c93736", size = 20882951 },
|
11
|
+
{ url = "https://files.pythonhosted.org/packages/70/1d/4ad38e3a1840f72c29595c06b103ecd9119f260e897ff7e88a74adb0ca14/numpy-2.1.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f358ea9e47eb3c2d6eba121ab512dfff38a88db719c38d1e67349af210bc7529", size = 13491878 },
|
12
|
+
{ url = "https://files.pythonhosted.org/packages/b4/3b/569055d01ed80634d6be6ceef8fb28eb0866e4f98c2d97667dcf9fae3e22/numpy-2.1.0-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:dd94ce596bda40a9618324547cfaaf6650b1a24f5390350142499aa4e34e53d1", size = 5087346 },
|
13
|
+
{ url = "https://files.pythonhosted.org/packages/24/37/212dd6fbd298c467b80d4d6217b2bc902b520e96a967b59f72603bf1142f/numpy-2.1.0-cp312-cp312-macosx_14_0_x86_64.whl", hash = "sha256:b47c551c6724960479cefd7353656498b86e7232429e3a41ab83be4da1b109e8", size = 6618269 },
|
14
|
+
{ url = "https://files.pythonhosted.org/packages/33/4d/435c143c06e16c8bfccbfd9af252b0a8ac7897e0c0e36e539d75a75e91b4/numpy-2.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0756a179afa766ad7cb6f036de622e8a8f16ffdd55aa31f296c870b5679d745", size = 13695244 },
|
15
|
+
{ url = "https://files.pythonhosted.org/packages/48/3e/bf807eb050abc23adc556f34fcf931ca2d67ad8dfc9c17fcd9332c01347f/numpy-2.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:24003ba8ff22ea29a8c306e61d316ac74111cebf942afbf692df65509a05f111", size = 16040181 },
|
16
|
+
{ url = "https://files.pythonhosted.org/packages/cd/a9/40dc96b5d43076836d82d1e84a3a4a6a4c2925a53ec0b7f31271434ff02c/numpy-2.1.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:b34fa5e3b5d6dc7e0a4243fa0f81367027cb6f4a7215a17852979634b5544ee0", size = 16407920 },
|
17
|
+
{ url = "https://files.pythonhosted.org/packages/cc/77/39e44cf0a6eb0f93b18ffb00f1964b2c471b1df5605aee486c221b06a8e4/numpy-2.1.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:c4f982715e65036c34897eb598d64aef15150c447be2cfc6643ec7a11af06574", size = 14170943 },
|
18
|
+
{ url = "https://files.pythonhosted.org/packages/54/02/f0a3c2ec1622dc4346bd126e2578948c7192b3838c893a3d215738fb367b/numpy-2.1.0-cp312-cp312-win32.whl", hash = "sha256:c4cd94dfefbefec3f8b544f61286584292d740e6e9d4677769bc76b8f41deb02", size = 6235947 },
|
19
|
+
{ url = "https://files.pythonhosted.org/packages/8c/bf/d9d214a9dff020ad1663f1536f45d34e052e4c7f630c46cd363e785e3231/numpy-2.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:a0cdef204199278f5c461a0bed6ed2e052998276e6d8ab2963d5b5c39a0500bc", size = 12566546 },
|
20
|
+
{ url = "https://files.pythonhosted.org/packages/c3/16/6b536e1b67624178e3631a3fa60c9c1b5ee7cda2fa9492c4f2de01bfcb06/numpy-2.1.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:8ab81ccd753859ab89e67199b9da62c543850f819993761c1e94a75a814ed667", size = 20833354 },
|
21
|
+
{ url = "https://files.pythonhosted.org/packages/52/87/130e95aa8a6383fc3de4fdaf7adc629289b79b88548fb6e35e9d924697d7/numpy-2.1.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:442596f01913656d579309edcd179a2a2f9977d9a14ff41d042475280fc7f34e", size = 13506169 },
|
22
|
+
{ url = "https://files.pythonhosted.org/packages/d9/c2/0fcf68c67681f9ad9d76156b4606f60b48748ead76d4ba19b90aecd4b626/numpy-2.1.0-cp313-cp313-macosx_14_0_arm64.whl", hash = "sha256:848c6b5cad9898e4b9ef251b6f934fa34630371f2e916261070a4eb9092ffd33", size = 5072908 },
|
23
|
+
{ url = "https://files.pythonhosted.org/packages/72/40/e21bbbfae665ef5fa1dfd7eae1c5dc93ba9d3b36e39d2d38789dd8c22d56/numpy-2.1.0-cp313-cp313-macosx_14_0_x86_64.whl", hash = "sha256:54c6a63e9d81efe64bfb7bcb0ec64332a87d0b87575f6009c8ba67ea6374770b", size = 6604906 },
|
24
|
+
{ url = "https://files.pythonhosted.org/packages/0e/ce/848967516bf8dd4f769886a883a4852dbc62e9b63b1137d2b9900f595222/numpy-2.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:652e92fc409e278abdd61e9505649e3938f6d04ce7ef1953f2ec598a50e7c195", size = 13690864 },
|
25
|
+
{ url = "https://files.pythonhosted.org/packages/15/72/2cebe04758e1123f625ed3221cb3c48602175ad619dd9b47de69689b4656/numpy-2.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0ab32eb9170bf8ffcbb14f11613f4a0b108d3ffee0832457c5d4808233ba8977", size = 16036272 },
|
26
|
+
{ url = "https://files.pythonhosted.org/packages/a7/b7/ae34ced7864b551e0ea01ce4e7acbe7ddf5946afb623dea39760b19bc8b0/numpy-2.1.0-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:8fb49a0ba4d8f41198ae2d52118b050fd34dace4b8f3fb0ee34e23eb4ae775b1", size = 16408978 },
|
27
|
+
{ url = "https://files.pythonhosted.org/packages/4d/22/c9d696b87c5ce25e857d7745fe4f090373a2daf8c26f5e15b32b5db7bff7/numpy-2.1.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:44e44973262dc3ae79e9063a1284a73e09d01b894b534a769732ccd46c28cc62", size = 14168398 },
|
28
|
+
{ url = "https://files.pythonhosted.org/packages/9e/8b/63f74dccf86d4832d593bdbe06544f4a0a1b7e18e86e0db1e8231bf47c49/numpy-2.1.0-cp313-cp313-win32.whl", hash = "sha256:ab83adc099ec62e044b1fbb3a05499fa1e99f6d53a1dde102b2d85eff66ed324", size = 6232743 },
|
29
|
+
{ url = "https://files.pythonhosted.org/packages/23/4b/e30a3132478c69df3e3e587fa87dcbf2660455daec92d8d52e7028a92554/numpy-2.1.0-cp313-cp313-win_amd64.whl", hash = "sha256:de844aaa4815b78f6023832590d77da0e3b6805c644c33ce94a1e449f16d6ab5", size = 12560212 },
|
30
|
+
{ url = "https://files.pythonhosted.org/packages/5a/1b/40e881a3a272c4861de1e43a3e7ee1559988dd12187463726d3b395a8874/numpy-2.1.0-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:343e3e152bf5a087511cd325e3b7ecfd5b92d369e80e74c12cd87826e263ec06", size = 20840821 },
|
31
|
+
{ url = "https://files.pythonhosted.org/packages/d0/8e/5b7c08f9238f6cc18037f6fd92f83feaa8c19e9decb6bd075cad81f71fae/numpy-2.1.0-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:f07fa2f15dabe91259828ce7d71b5ca9e2eb7c8c26baa822c825ce43552f4883", size = 13500478 },
|
32
|
+
{ url = "https://files.pythonhosted.org/packages/65/32/bf9df25ef50761fcb3e089c745d2e195b35cc6506d032f12bb5cc28f6c43/numpy-2.1.0-cp313-cp313t-macosx_14_0_arm64.whl", hash = "sha256:5474dad8c86ee9ba9bb776f4b99ef2d41b3b8f4e0d199d4f7304728ed34d0300", size = 5095825 },
|
33
|
+
{ url = "https://files.pythonhosted.org/packages/50/34/d18c95bc5981ea3bb8e6f896aad12159a37dcc67b22cd9464fe3899612f7/numpy-2.1.0-cp313-cp313t-macosx_14_0_x86_64.whl", hash = "sha256:1f817c71683fd1bb5cff1529a1d085a57f02ccd2ebc5cd2c566f9a01118e3b7d", size = 6611470 },
|
34
|
+
{ url = "https://files.pythonhosted.org/packages/b4/4f/27d56e9f6222419951bfeef54bc0a71dc40c0ebeb248e1aa85655da6fa11/numpy-2.1.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3a3336fbfa0d38d3deacd3fe7f3d07e13597f29c13abf4d15c3b6dc2291cbbdd", size = 13647061 },
|
35
|
+
{ url = "https://files.pythonhosted.org/packages/f9/e0/ae6e12a157c4ab415b380d0f3596cb9090a0c4acf48cd8cd7bc6d6b93d24/numpy-2.1.0-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7a894c51fd8c4e834f00ac742abad73fc485df1062f1b875661a3c1e1fb1c2f6", size = 16006479 },
|
36
|
+
{ url = "https://files.pythonhosted.org/packages/ab/da/b746668c7303bd73af262208abbfa8b1c86be12e9eccb0d3021ed8a58873/numpy-2.1.0-cp313-cp313t-musllinux_1_1_x86_64.whl", hash = "sha256:9156ca1f79fc4acc226696e95bfcc2b486f165a6a59ebe22b2c1f82ab190384a", size = 16383064 },
|
37
|
+
{ url = "https://files.pythonhosted.org/packages/f4/51/c0dcadea0c281be5db32b29f7b977b17bdb53b7dbfcbc3b4f49288de8696/numpy-2.1.0-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:624884b572dff8ca8f60fab591413f077471de64e376b17d291b19f56504b2bb", size = 14135556 },
|
38
|
+
]
|
39
|
+
|
40
|
+
[[package]]
|
41
|
+
name = "relib"
|
42
|
+
version = "1.0.9"
|
43
|
+
source = { editable = "." }
|
44
|
+
dependencies = [
|
45
|
+
{ name = "numpy" },
|
46
|
+
{ name = "termcolor" },
|
47
|
+
]
|
48
|
+
|
49
|
+
[package.metadata]
|
50
|
+
requires-dist = [
|
51
|
+
{ name = "numpy" },
|
52
|
+
{ name = "termcolor" },
|
53
|
+
]
|
54
|
+
|
55
|
+
[[package]]
|
56
|
+
name = "termcolor"
|
57
|
+
version = "2.4.0"
|
58
|
+
source = { registry = "https://pypi.org/simple" }
|
59
|
+
sdist = { url = "https://files.pythonhosted.org/packages/10/56/d7d66a84f96d804155f6ff2873d065368b25a07222a6fd51c4f24ef6d764/termcolor-2.4.0.tar.gz", hash = "sha256:aab9e56047c8ac41ed798fa36d892a37aca6b3e9159f3e0c24bc64a9b3ac7b7a", size = 12664 }
|
60
|
+
wheels = [
|
61
|
+
{ url = "https://files.pythonhosted.org/packages/d9/5f/8c716e47b3a50cbd7c146f45881e11d9414def768b7cd9c5e6650ec2a80a/termcolor-2.4.0-py3-none-any.whl", hash = "sha256:9297c0df9c99445c2412e832e882a7884038a25617c60cea2ad69488d4040d63", size = 7719 },
|
62
|
+
]
|