relib 1.0.4__py3-none-any.whl → 1.0.6__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.
relib/__init__.py CHANGED
@@ -2,12 +2,14 @@ from .utils import (
2
2
  list_split,
3
3
  drop_none,
4
4
  distinct,
5
- find,
5
+ first,
6
+ move_value,
6
7
  transpose_dict,
7
8
  make_combinations_by_dict,
8
9
  merge_dicts,
9
10
  intersect,
10
11
  ensure_tuple,
12
+ key_of,
11
13
  omit,
12
14
  pick,
13
15
  dict_by,
relib/utils.py CHANGED
@@ -22,9 +22,14 @@ def drop_none(l: Iterable[Union[T, None]]) -> list[T]:
22
22
  def distinct(items: Iterable[T]) -> list[T]:
23
23
  return list(set(items))
24
24
 
25
- def find(iterable: Iterable[T]) -> Union[T, None]:
25
+ def first(iterable: Iterable[T]) -> Union[T, None]:
26
26
  return next(iter(iterable), None)
27
27
 
28
+ def move_value(l: Iterable[T], from_i: int, to_i: int) -> list[T]:
29
+ l = list(l)
30
+ l.insert(to_i, l.pop(from_i))
31
+ return l
32
+
28
33
  def transpose_dict(des):
29
34
  if isinstance(des, list):
30
35
  keys = list(des[0].keys()) if des else []
@@ -55,10 +60,10 @@ def make_combinations_by_dict(des, keys=None, pairs=[]):
55
60
  ])
56
61
 
57
62
  def merge_dicts(*dicts: dict[K, T]) -> dict[K, T]:
58
- result = {}
59
- for dictionary in dicts:
60
- result.update(dictionary)
61
- return result
63
+ output = {}
64
+ for d in dicts:
65
+ output.update(d)
66
+ return output
62
67
 
63
68
  def intersect(*lists: Iterable[T]) -> list[T]:
64
69
  return list(set.intersection(*map(set, lists)))
@@ -66,6 +71,9 @@ def intersect(*lists: Iterable[T]) -> list[T]:
66
71
  def ensure_tuple(value: Union[T, tuple[T, ...]]) -> tuple[T, ...]:
67
72
  return value if isinstance(value, tuple) else (value,)
68
73
 
74
+ def key_of(dicts: Iterable[dict[T, U]], key: T) -> list[U]:
75
+ return [d[key] for d in dicts]
76
+
69
77
  def omit(d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
70
78
  if keys:
71
79
  d = dict(d)
@@ -83,29 +91,27 @@ def tuple_by(d: dict[K, T], keys: Iterable[K]) -> tuple[T, ...]:
83
91
  return tuple(d[key] for key in keys)
84
92
 
85
93
  def flatten(l: Iterable[Iterable[T]]) -> list[T]:
86
- # TODO: compare performance and ensure all types of iterables works
87
94
  return list(chain.from_iterable(l))
88
- return [value for inner_list in l for value in inner_list]
89
95
 
90
96
  def transpose(tuples, default_num_returns=0):
91
- result = tuple(zip(*tuples))
92
- if not result:
97
+ output = tuple(zip(*tuples))
98
+ if not output:
93
99
  return ([],) * default_num_returns
94
- return tuple(map(list, result))
100
+ return tuple(map(list, output))
95
101
 
96
102
  def map_dict(fn: Callable[[T], U], d: dict[K, T]) -> dict[K, U]:
97
103
  return {key: fn(value) for key, value in d.items()}
98
104
 
99
105
  def deepen_dict(d):
100
- result = {}
106
+ output = {}
101
107
  for (*tail, head), value in d.items():
102
- curr = result
108
+ curr = output
103
109
  for key in tail:
104
110
  if key not in curr:
105
111
  curr[key] = {}
106
112
  curr = curr[key]
107
113
  curr[head] = value
108
- return result
114
+ return output
109
115
 
110
116
  def group(pairs: Iterable[tuple[K, T]]) -> dict[K, list[T]]:
111
117
  values_by_key = {}
@@ -141,24 +147,23 @@ def _cat_tile(cats, n_tile):
141
147
  def df_from_array(
142
148
  value_cols: dict[str, np.ndarray],
143
149
  dim_labels: list[tuple[str, list[Union[str, int, float]]]],
144
- set_index=True,
150
+ indexed=False,
145
151
  ):
146
152
  import pandas as pd
147
153
  dim_names = [name for name, _ in dim_labels]
148
154
  dim_sizes = np.array([len(labels) for _, labels in dim_labels])
149
- repeats_tiles = [
155
+ assert all(array.shape == tuple(dim_sizes) for array in value_cols.values())
156
+ array_offsets = [
150
157
  (dim_sizes[i + 1:].prod(), dim_sizes[:i].prod())
151
158
  for i in range(len(dim_sizes))
152
159
  ]
153
- label_arrays = [
154
- _cat_tile(pd.Categorical(labels).repeat(repeats), tiles)
155
- for labels, (repeats, tiles) in zip(dim_labels, repeats_tiles)
156
- ]
157
- df = pd.DataFrame(dict(zip(dim_names, label_arrays)))
158
- for col_name, array in value_cols.items():
159
- assert array.shape == tuple(dim_sizes)
160
- df[col_name] = array.reshape(-1)
161
- if set_index:
160
+ category_cols = {
161
+ dim: _cat_tile(pd.Categorical(labels).repeat(repeats), tiles)
162
+ for dim, labels, (repeats, tiles) in zip(dim_names, dim_labels, array_offsets)
163
+ }
164
+ value_cols = {name: array.reshape(-1) for name, array in value_cols.items()}
165
+ df = pd.DataFrame({**category_cols, **value_cols}, copy=False)
166
+ if indexed:
162
167
  df = df.set_index(dim_names)
163
168
  return df
164
169
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: relib
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Home-page: https://github.com/Reddan/relib
5
5
  Author: Hampus Hallman
6
6
  Author-email: me@hampushallman.com
@@ -0,0 +1,10 @@
1
+ relib/__init__.py,sha256=4yr8xPi3VMbiFlApussB4OXU_U6wzhje06qD0Ad3Gq4,471
2
+ relib/hashing.py,sha256=6iAPRiJI_4jaSooZRFJnqK2limXqTmErzcwpd050LAA,8943
3
+ relib/measure_duration.py,sha256=jJa5Kh5FxaBysS__nkwqcnTt8Uc2niLucXfTzFE0j-E,555
4
+ relib/raypipe.py,sha256=ynEoXs1dnD-360_uQC8v89xjiilt3knpocXpFaQ3plA,1905
5
+ relib/utils.py,sha256=JM68czDzWLOa2ADbBEdAb7j4Zv3RFYMws5sy0ghxbW0,5353
6
+ relib-1.0.6.dist-info/LICENSE.txt,sha256=t9LfkVbmcvZjP0x3Sq-jR38UfTNbNtRQvc0Q8HWmLak,1054
7
+ relib-1.0.6.dist-info/METADATA,sha256=NVAMcPbnJrlRd_W8QRuX93F-gHSg47L9dGBf5ap7tnw,260
8
+ relib-1.0.6.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
9
+ relib-1.0.6.dist-info/top_level.txt,sha256=Yc96FwkbRYj4AQVatga8uK4hH9ATKI9XIyEH_1ba6KQ,6
10
+ relib-1.0.6.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.40.0)
2
+ Generator: bdist_wheel (0.42.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,10 +0,0 @@
1
- relib/__init__.py,sha256=d7vbtHV079on1ZRQeT5SAxNHtfSd14JDN1fHCGbbsjE,446
2
- relib/hashing.py,sha256=6iAPRiJI_4jaSooZRFJnqK2limXqTmErzcwpd050LAA,8943
3
- relib/measure_duration.py,sha256=jJa5Kh5FxaBysS__nkwqcnTt8Uc2niLucXfTzFE0j-E,555
4
- relib/raypipe.py,sha256=ynEoXs1dnD-360_uQC8v89xjiilt3knpocXpFaQ3plA,1905
5
- relib/utils.py,sha256=V5pdaO2qZWjOkr__eMbFfF2X4mdk9b2THQ8Ve26UJcU,5221
6
- relib-1.0.4.dist-info/LICENSE.txt,sha256=t9LfkVbmcvZjP0x3Sq-jR38UfTNbNtRQvc0Q8HWmLak,1054
7
- relib-1.0.4.dist-info/METADATA,sha256=lzPhG9j-DBJ_X6-camK9N7pAoTp8dLJ6V-e3GQWsPpI,260
8
- relib-1.0.4.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
9
- relib-1.0.4.dist-info/top_level.txt,sha256=Yc96FwkbRYj4AQVatga8uK4hH9ATKI9XIyEH_1ba6KQ,6
10
- relib-1.0.4.dist-info/RECORD,,