Skip to content

Commit

Permalink
Equality of opportunity using fa*ir implementation #80
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamedloghmani committed Aug 23, 2023
1 parent 09b5ce4 commit c1dc2e8
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,18 @@ def rerank(preds, labels, output, ratios, algorithm: str = 'det_greedy', k_max:
fair_docs.append([FairScoreDoc(m[0], m[2], not m[1]) for m in member_popularity_probs])

if eq_op:
fair_teams = list()
start_time = perf_counter()
for i, team in enumerate(fair_docs):
r = {True: 1 - ratios[i], False: ratios[i]}
# it is inside the loop because ratio varies for every team in equality of opportunity
fair = fsc.Fair(k_max, r[False], alpha)
if fair.is_fair(team[:k_max]):
fair_teams.append(team[:k_max])
else:
reranked = fair.re_rank(team)
fair_teams.append(reranked[:k_max])
finish_time = perf_counter()
#TODO implement eq_op for fa*ir
else:
fair = fsc.Fair(k_max, ratios[False], alpha)
fair_teams = list()
Expand All @@ -108,13 +117,14 @@ def rerank(preds, labels, output, ratios, algorithm: str = 'det_greedy', k_max:
reranked = fair.re_rank(team)
fair_teams.append(reranked[:k_max])
finish_time = perf_counter()
idx, probs, protected = list(), list(), list()

# Creating required values to return from fairdoc objects
for fair_team in fair_teams:
idx.append([x.id for x in fair_team])
probs.append([x.score for x in fair_team])
idx, probs, protected = list(), list(), list()

# Creating required values to return from fairdoc objects
for fair_team in fair_teams:
idx.append([x.id for x in fair_team])
probs.append([x.score for x in fair_team])
pd.DataFrame({'reranked_idx': idx, 'reranked_probs': probs}).to_csv(f'{output}.{algorithm}.{k_max}.rerank.csv', index=False)

elif algorithm in ['det_greedy', 'det_relaxed', 'det_cons']:
if eq_op:
Expand Down Expand Up @@ -142,8 +152,7 @@ def rerank(preds, labels, output, ratios, algorithm: str = 'det_greedy', k_max:
idx.append(reranked_idx)
probs.append(reranked_probs)
finish_time = perf_counter()
pd.DataFrame({'reranked_idx': idx, 'reranked_probs': probs}).to_csv(
f'{output}.{algorithm}.{k_max}.rerank.csv', index=False)
pd.DataFrame({'reranked_idx': idx, 'reranked_probs': probs}).to_csv(f'{output}.{algorithm}.{k_max}.rerank.csv', index=False)
else:
raise ValueError('chosen reranking algorithm is not valid')

Expand Down

0 comments on commit c1dc2e8

Please sign in to comment.