Skip to content

Commit

Permalink
array_from_hist
Browse files Browse the repository at this point in the history
  • Loading branch information
fraterenz committed Nov 27, 2023
1 parent ea8de84 commit f06148d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/futils/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
Histogram = NewType("Histogram", Dict[int, int])


def array_from_hist(my_hist: Histogram) -> np.ndarray:
"""
>>> from src.futils import snapshot
>>> my_keys = [1, 0, 3]
>>> my_values = [2, 2, 1]
>>> histogram = snapshot.histogram_from_dict({k: ele for k, ele in zip(my_keys, my_values)})
>>> snapshot.array_from_hist(histogram)
array([0, 0, 1, 1, 3])
"""
return np.asarray([i for k, val in my_hist.items() for i in [k] * val], dtype=int)


def histogram_from_dict(my_dict: Dict[int, int]) -> Histogram:
return Histogram({int(x): int(y) for x, y in sorted(my_dict.items())})

Expand Down

0 comments on commit f06148d

Please sign in to comment.