-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
60 lines (45 loc) · 1.41 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import annotations
from typing import Tuple, List, Dict
def ComputeCumulativeProbabilitiesFromDict(inputDict: Dict) -> Tuple[List[float], List[float]]:
"""
Expecting input in format:
{
"0.5": 0.5,
"1": 0.5
}
Outputting array of values, and array of cumulative probabilities
"""
allKeys = list(inputDict.keys())
values = [float(k) for k in allKeys]
probas = [
inputDict[k] for k in allKeys
]
# make sure sum probas == 1
probas = [p / sum(probas) for p in probas]
# cumulative probabilities
cumProbas = []
for idProbas in range(len(probas)):
currCumProba = probas[idProbas]
if idProbas > 0:
currCumProba += cumProbas[-1]
cumProbas.append(currCumProba)
return values, cumProbas
# Emulate List
class CustomList(object):
def __init__(self, elements: List = []):
self.Elements = elements
def __repr__(self):
return self.Elements.__repr__()
def __len__(self):
return len(self.Elements)
def __getitem__(self, item):
return self.Elements[item]
def __add__(self, other):
if type(other) == list:
self.Elements += other
elif self.__class__ is other.__class__:
self.Elements += other.Elements
else:
raise NotImplemented
def append(self, item):
self.Elements.append(item)