Skip to content

Commit

Permalink
Passed tests primitives load_save and state
Browse files Browse the repository at this point in the history
Next is generative_model and pipeline
  • Loading branch information
lenhoanglnh committed Jan 7, 2025
1 parent aebd607 commit 003025e
Show file tree
Hide file tree
Showing 18 changed files with 1,475 additions and 1,334 deletions.
2 changes: 1 addition & 1 deletion solidago/src/solidago/_state/_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __call__(self, entities: Union["Entity", "Entities"]) -> MultiScore:
from solidago._state._entities import Entities
if isinstance(entities, Entities):
return MultiScore(
{ entity: self(entity) for entity in entities },
{ str(entity): self(entity) for entity in entities },
key_names=["entity_name", "criterion"]
)
entity = entities
Expand Down
10 changes: 5 additions & 5 deletions solidago/src/solidago/_state/_models/post_processed.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def post_process(self, score: Union[Score, MultiScore]) -> Union[Score, MultiSco
value = self.post_process_fn(score.value)
extremes = [self.post_process_fn(score.max), self.post_process_fn(score.min)]
return Score(value, value - min(extremes), max(extremes) - value)
else:
return MultiScore({
criterion: self.process(criterion_score)
for criterion, criterion_score in score
})
assert isinstance(score, MultiScore)
return MultiScore({
criterion: self.post_process(criterion_score)
for criterion, criterion_score in score
})


class SquashedModel(PostProcessedModel):
Expand Down
42 changes: 39 additions & 3 deletions solidago/src/solidago/_state/_models/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self,
values = value.value, value.left_unc, value.right_unc
elif isinstance(value, (dict, Series)):
assert left_unc is None and right_unc is None
values = value["value"], value["left_unc"], value["right_unc"]
values = value["score"], value["left_unc"], value["right_unc"]
elif isinstance(value, (list, tuple)):
assert left_unc is None and right_unc is None
values = value
Expand Down Expand Up @@ -59,23 +59,59 @@ def to_dict(self) -> dict[str, float]:

def average_uncertainty(self) -> float:
return (self.left_unc + self.right_unc) / 2

def __eq__(self, score: Union[int, float, "Score"]) -> bool:
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return self.to_triplet() == score.to_triplet()

def __neq__(self, score: Union[int, float, "Score"]) -> bool:
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return self.to_triplet() != score.to_triplet()

def __lt__(self, score: Union[int, float, "Score"]) -> bool:
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return self.max < score.min

def __gt__(self, score: Union[int, float, "Score"]) -> bool:
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return self.min > score.max

def __le__(self, score: Union[int, float, "Score"]) -> bool:
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return self.min <= score.max

def __ge__(self, score: Union[int, float, "Score"]) -> bool:
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return self.max >= score.min

def __neg__(self) -> "Score":
return Score(- self.value, self.right_unc, self.left_unc)

def __add__(self, score: "Score") -> "Score":
def __add__(self, score: Union[int, float, "Score"]) -> "Score":
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
return Score(
self.value + score.value,
self.left_unc + score.left_unc,
self.right_unc + score.right_unc
)

def __mul__(self, s: "Score") -> "Score":
def __mul__(self, s: Union[int, float, "Score"]) -> "Score":
if isinstance(s, (int, float)):
s = Score(score, 0, 0)
value = self.value * s.value
extremes = [ self.min * s.min, self.min * s.max, self.max * s.min, self.max * s.max ]
return Score(value, value - min(extremes), max(extremes) - value)

def __truediv__(self, s: "Score") -> "Score":
if isinstance(score, (int, float)):
score = Score(score, 0, 0)
if 0 in self or 0 in s:
return Score.nan()
value = self.value / s.value
Expand Down
5 changes: 4 additions & 1 deletion solidago/src/solidago/_state/_models/user_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def __init__(self, *args, default_model_cls: type=DirectScoring, **kwargs):

def default_value(self) -> ScoringModel:
return self.default_model_cls()


def __call__(self, entity: Union[str, "Entity", "Entities"]) -> MultiScore:
return self.score(entity)

def score(self, entity: Union[str, "Entity", "Entities"]) -> MultiScore:
from solidago._state._entities import Entity, Entities
if isinstance(entity, (str, Entity)):
Expand Down
2 changes: 1 addition & 1 deletion solidago/src/solidago/_state/tournesol.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def load(filename, columns):
"criteria": "criterion",
"video": "entity_name",
"public_username": "username",
"uncertainty": "left_unc",
"uncertainty": "left_unc",
})

vouches["kind"] = "Personhood"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(self, *args, save_filename: Optional[Union[str, Path]]=None, **kwar
super().__init__(*args, **kwargs)
self.meta = SimpleNamespace()
self.meta.save_filename = save_filename
# if len(self.columns) == 1:
# self.rename(columns={ self.columns[0]: self.index_name }, inplace=True)
if self.index.name is None and len(self.columns) == 1 and self.columns[0] == 0:
self.rename(columns={ self.columns[0]: self.index_name }, inplace=True)
if self.index_name in self.columns:
self.set_index(self.index_name, inplace=True)
self.index = [str(name) for name in self.index]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def init_dict(self, d: Union["NestedDict", dict, DataFrame]) -> None:
if isinstance(d, NestedDict):
self._dict |= d._dict
elif isinstance(d, dict) and len(self.key_names) == 1:
self._dict |= d
self._dict |= { key: self.sanitize(value) for key, value in d.items() }
elif isinstance(d, dict):
self._dict |= {
key: type(self)(d=value, key_names=self.key_names[1:])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def __init__(self,
self.value_names = value_names
super().__init__(d=d, key_names=key_names, save_filename=save_filename)

def add_row(self, keys: list[str], row: Union[dict, Series]) -> None:
self[keys] = tuple([row[name] for name in self.value_names])
def add_row(self, keys: Union[str, list[str]], row: Union[dict, Series]) -> None:
self[keys] = self.sanitize(row)

def sanitize(self, value: Any) -> tuple:
if isinstance(value, (dict, Series)):
return [value[name] for name in self.value_names]
return tuple(value)

def get_set(self, key_name: str, default_value: Optional[str]=None) -> set:
Expand Down
180 changes: 97 additions & 83 deletions solidago/tests/load_save/generated_state/assessments.csv
Original file line number Diff line number Diff line change
@@ -1,85 +1,99 @@
username,criterion,entity_name,assessment,assessment_min,assessment_max
user_0,default,entity_0,-3.084032146485425,-inf,inf
user_0,default,entity_16,-4.0878539983787,-inf,inf
user_0,default,entity_15,-1.490202061094308,-inf,inf
user_0,default,entity_18,-1.0038588539696902,-inf,inf
user_0,default,entity_9,0.15862347293943002,-inf,inf
user_0,default,entity_13,0.6825297084581012,-inf,inf
user_0,default,entity_19,-1.1330286216672032,-inf,inf
user_0,default,entity_5,1.5277158309506345,-inf,inf
user_0,default,entity_4,3.4585969415576345,-inf,inf
user_1,default,entity_3,1.667515895613929,-inf,inf
user_1,default,entity_14,1.006242789479527,-inf,inf
user_1,default,entity_7,2.664075720463755,-inf,inf
user_1,default,entity_18,-0.8301415440372613,-inf,inf
user_1,default,entity_8,-0.1742090239878929,-inf,inf
user_1,default,entity_10,-1.1835582791092611,-inf,inf
user_1,default,entity_6,-1.1948480725179846,-inf,inf
user_1,default,entity_16,-0.9539951632086847,-inf,inf
user_1,default,entity_12,-2.1802861964071387,-inf,inf
user_1,default,entity_2,-0.42652666498132275,-inf,inf
user_2,default,entity_11,-1.387325030216633,-inf,inf
user_2,default,entity_9,-0.9284619449437295,-inf,inf
user_2,default,entity_18,-0.48508960064422346,-inf,inf
user_2,default,entity_14,-0.5877998959704127,-inf,inf
user_2,default,entity_17,-0.8219599742190199,-inf,inf
user_2,default,entity_0,-0.7750717258171584,-inf,inf
user_2,default,entity_4,-0.7272800370864463,-inf,inf
user_2,default,entity_13,-0.33567087330509393,-inf,inf
user_2,default,entity_5,1.0737649403543752,-inf,inf
user_2,default,entity_1,1.3235236743850212,-inf,inf
user_2,default,entity_16,2.548852058606136,-inf,inf
user_3,default,entity_19,0.9453108117051138,-inf,inf
user_3,default,entity_1,2.2805036607813913,-inf,inf
user_3,default,entity_13,-1.5769377112488974,-inf,inf
user_3,default,entity_16,1.5165254267520498,-inf,inf
user_3,default,entity_10,2.072081238298041,-inf,inf
user_3,default,entity_3,-1.1848498093087512,-inf,inf
user_3,default,entity_7,1.3271180042064845,-inf,inf
user_3,default,entity_18,-0.25013042087067283,-inf,inf
user_3,default,entity_12,-1.021665684122971,-inf,inf
user_3,default,entity_6,-1.0810028748325473,-inf,inf
user_3,default,entity_2,1.203173894386199,-inf,inf
user_4,default,entity_19,0.2639409528014471,-inf,inf
user_4,default,entity_8,0.440512579577302,-inf,inf
user_4,default,entity_2,-0.43449183897222454,-inf,inf
user_4,default,entity_17,0.6740247220648662,-inf,inf
user_4,default,entity_18,1.2408002798143847,-inf,inf
user_4,default,entity_3,0.482883923065351,-inf,inf
user_4,default,entity_15,0.8328232652751022,-inf,inf
user_4,default,entity_12,-0.4929965759725601,-inf,inf
user_4,default,entity_14,-0.5966527881357854,-inf,inf
user_4,default,entity_7,0.11951141508835739,-inf,inf
user_4,default,entity_16,-0.5090007368392502,-inf,inf
user_5,default,entity_6,-0.21462643170061727,-inf,inf
user_0,default,entity_16,-2.0749057611785386,-inf,inf
user_0,default,entity_15,-2.39370352625087,-inf,inf
user_0,default,entity_11,-1.5992836978625538,-inf,inf
user_0,default,entity_18,-2.619031854481638,-inf,inf
user_0,default,entity_6,-1.2481876740748055,-inf,inf
user_0,default,entity_17,-0.8195927297894479,-inf,inf
user_0,default,entity_14,-1.2667207574469999,-inf,inf
user_0,default,entity_19,0.8509795934379412,-inf,inf
user_0,default,entity_4,3.053938668091461,-inf,inf
user_1,default,entity_10,1.666205471029322,-inf,inf
user_1,default,entity_15,1.3982102010500272,-inf,inf
user_1,default,entity_7,0.31743364012724,-inf,inf
user_1,default,entity_2,2.0757722679531048,-inf,inf
user_1,default,entity_1,0.24777798376771804,-inf,inf
user_1,default,entity_17,1.2172607977951482,-inf,inf
user_1,default,entity_14,0.7587810143743123,-inf,inf
user_1,default,entity_16,-0.45322198998446633,-inf,inf
user_1,default,entity_3,-0.6293299841653897,-inf,inf
user_1,default,entity_12,-2.053760441767217,-inf,inf
user_1,default,entity_18,-0.9601193237500203,-inf,inf
user_1,default,entity_11,-0.7764255417543154,-inf,inf
user_2,default,entity_8,-3.1840313925258865,-inf,inf
user_2,default,entity_12,-1.3582673358306874,-inf,inf
user_2,default,entity_4,-0.3920343878647034,-inf,inf
user_2,default,entity_13,-1.0307685022464559,-inf,inf
user_2,default,entity_19,1.2140338299536473,-inf,inf
user_2,default,entity_1,0.34488131370003594,-inf,inf
user_2,default,entity_5,0.4963366907015018,-inf,inf
user_2,default,entity_16,1.3239011724971825,-inf,inf
user_3,default,entity_1,1.7159560105752791,-inf,inf
user_3,default,entity_2,0.900609937541631,-inf,inf
user_3,default,entity_10,0.010155796202459877,-inf,inf
user_3,default,entity_19,1.9939222430641497,-inf,inf
user_3,default,entity_18,-0.2487610576189404,-inf,inf
user_3,default,entity_6,-1.201379294506745,-inf,inf
user_3,default,entity_8,-1.989535014457278,-inf,inf
user_3,default,entity_14,-1.421354527295856,-inf,inf
user_3,default,entity_5,-0.32127542248226026,-inf,inf
user_4,default,entity_8,0.28435161869848846,-inf,inf
user_4,default,entity_6,2.4003825078321817,-inf,inf
user_4,default,entity_19,0.42259228898088025,-inf,inf
user_4,default,entity_11,0.055986495346031506,-inf,inf
user_4,default,entity_17,0.2099984004735524,-inf,inf
user_4,default,entity_13,0.4161872688546774,-inf,inf
user_4,default,entity_9,1.6036019683037805,-inf,inf
user_4,default,entity_4,-0.18158399657965602,-inf,inf
user_4,default,entity_2,2.0454369805034163,-inf,inf
user_4,default,entity_0,0.3854155927801495,-inf,inf
user_4,default,entity_15,-0.14632039422818038,-inf,inf
user_4,default,entity_5,-2.022605231132655,-inf,inf
user_4,default,entity_18,-0.872532836583685,-inf,inf
user_4,default,entity_3,-0.3827672112907701,-inf,inf
user_4,default,entity_12,0.6017254822778608,-inf,inf
user_4,default,entity_7,-2.9180266496509044,-inf,inf
user_4,default,entity_16,-0.7599310550355487,-inf,inf
user_5,default,entity_3,-2.1101274264627663,-inf,inf
user_5,default,entity_14,-2.762781599519231,-inf,inf
user_5,default,entity_2,-0.5100542990101968,-inf,inf
user_5,default,entity_17,0.05710431263487662,-inf,inf
user_6,default,entity_13,-0.6865530798790695,-inf,inf
user_6,default,entity_16,0.27755782227896963,-inf,inf
user_6,default,entity_4,-0.6587271432487042,-inf,inf
user_6,default,entity_8,-0.5583548331157635,-inf,inf
user_6,default,entity_18,1.9506928061461883,-inf,inf
user_6,default,entity_1,0.49050679907782496,-inf,inf
user_6,default,entity_2,-1.3252639653101215,-inf,inf
user_6,default,entity_0,-0.3233871818836943,-inf,inf
user_7,default,entity_4,0.18732339474126747,-inf,inf
user_7,default,entity_8,1.900874305115553,-inf,inf
user_7,default,entity_19,0.0966132005912641,-inf,inf
user_7,default,entity_3,0.9342862554614455,-inf,inf
user_7,default,entity_11,1.6668385201561917,-inf,inf
user_7,default,entity_5,0.7660559749152163,-inf,inf
user_7,default,entity_10,-1.394815185465011,-inf,inf
user_7,default,entity_2,-0.5664965551290375,-inf,inf
user_7,default,entity_14,3.0454263676652222,-inf,inf
user_8,default,entity_8,2.4404515835847844,-inf,inf
user_8,default,entity_4,2.1409310166269293,-inf,inf
user_8,default,entity_11,0.8428911156251948,-inf,inf
user_8,default,entity_14,0.11540882613427472,-inf,inf
user_8,default,entity_6,0.45813136118948833,-inf,inf
user_8,default,entity_2,0.31277617960683546,-inf,inf
user_9,default,entity_3,-1.9624260627998813,-inf,inf
user_9,default,entity_12,-0.6855884162192276,-inf,inf
user_9,default,entity_2,-0.8853080326940073,-inf,inf
user_9,default,entity_14,0.5609584765999138,-inf,inf
user_9,default,entity_9,-0.023301797645628702,-inf,inf
user_9,default,entity_5,-0.779672768494564,-inf,inf
user_9,default,entity_1,-2.172493689682739,-inf,inf
user_6,default,entity_16,-0.06305869415402687,-inf,inf
user_6,default,entity_6,-0.7677539107375391,-inf,inf
user_6,default,entity_3,-2.8643609493870237,-inf,inf
user_6,default,entity_11,-2.1819602163698275,-inf,inf
user_6,default,entity_4,-0.43687483459641485,-inf,inf
user_6,default,entity_9,-0.7354562201117167,-inf,inf
user_6,default,entity_13,-1.6808275477504329,-inf,inf
user_6,default,entity_8,0.9663711729296698,-inf,inf
user_6,default,entity_14,0.621626369112255,-inf,inf
user_6,default,entity_2,-0.35611410923840825,-inf,inf
user_6,default,entity_10,1.408450182481708,-inf,inf
user_6,default,entity_5,0.5043003970870001,-inf,inf
user_6,default,entity_7,2.0966207883047416,-inf,inf
user_7,default,entity_15,-0.5655431776581341,-inf,inf
user_7,default,entity_12,-0.18604211953544048,-inf,inf
user_7,default,entity_8,-0.719456200755322,-inf,inf
user_7,default,entity_10,-0.824253089052783,-inf,inf
user_7,default,entity_1,-1.1268342583835582,-inf,inf
user_7,default,entity_3,1.6072561062359942,-inf,inf
user_7,default,entity_18,0.43807952480223084,-inf,inf
user_7,default,entity_2,1.1942940555235904,-inf,inf
user_7,default,entity_4,-0.4542680865093558,-inf,inf
user_7,default,entity_14,2.5219389650833213,-inf,inf
user_7,default,entity_19,-0.6650799718202678,-inf,inf
user_7,default,entity_9,-0.13580933330195982,-inf,inf
user_7,default,entity_11,-0.4812107308028669,-inf,inf
user_7,default,entity_17,1.96090138537457,-inf,inf
user_8,default,entity_8,2.1127574448260513,-inf,inf
user_8,default,entity_4,2.642406825318763,-inf,inf
user_8,default,entity_9,0.5444086059726662,-inf,inf
user_8,default,entity_11,0.5846999636827297,-inf,inf
user_8,default,entity_2,0.7698291989015807,-inf,inf
user_8,default,entity_18,1.3419798902453495,-inf,inf
user_8,default,entity_5,1.7797980695330238,-inf,inf
user_9,default,entity_3,-0.01253707482118327,-inf,inf
user_9,default,entity_13,1.3216410202684816,-inf,inf
user_9,default,entity_11,1.694370071188104,-inf,inf
user_9,default,entity_14,1.5236251305880073,-inf,inf
user_9,default,entity_9,2.4498918215182184,-inf,inf
Loading

0 comments on commit 003025e

Please sign in to comment.