Skip to content

Commit

Permalink
Prettify visualize_preferences.py
Browse files Browse the repository at this point in the history
  • Loading branch information
PC committed Sep 18, 2024
2 parents 1772e77 + c22fd90 commit 12b917a
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 21 deletions.
32 changes: 20 additions & 12 deletions calculate_scores.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd

from mock_results import single_preference_var, pair_preference_var, pair_preference_gpt_4o
from mock_results import single_preference_var, pair_preference_var, pair_preference_gpt_4o, pair_preference_gpt_35, triple_preference_gpt_35
import matplotlib.pyplot as plt
from constants import MORAL_VALUES
import numpy as np
Expand All @@ -23,17 +23,17 @@ def get_single_prefs_means_var():


def normalise_pair_prefs(pair_preference_var):
first_comb = pair_preference_var[0]
first_comb = pair_preference_var
num = sum(first_comb[list(first_comb.keys())[0]].values())
# print(num)
normalised_pair_prefs = []
for i, preference_list in enumerate(pair_preference_var):
new_dict = {}
for comb, results in preference_list.items():
new_dict[comb] = {k: int(v * 100 / num) for k, v in results.items()}
normalised_pair_prefs.append(new_dict)
# for i, preference_list in enumerate(pair_preference_var):
new_dict = {}
for comb, results in pair_preference_var.items():
new_dict[comb] = {k: int(v * 100 / num) for k, v in results.items()}
# normalised_pair_prefs.append(new_dict)
# print(normalised_pair_prefs)
return normalised_pair_prefs
return new_dict# normalised_pair_prefs[0]


def pair_preference_matrix(preference_dict):
Expand Down Expand Up @@ -85,18 +85,26 @@ def get_pair_pref_ranking(pair_preferences=pair_preference_var[0]):
matrix, preference_matrix = pair_preference_matrix(pair_preferences)
# best_ranking, min_distance = kemeny_young_method(MORAL_VALUES, pair_preference_var[0])
# print("Kemeny-Young ranking:", best_ranking, min_distance)
# matrix["row_sums"] = matrix.apply(sum, axis=1)
# matrix["row_sums"] = matrix["row_sums"].apply(int)
for col in matrix.columns:
matrix[col] = matrix[col].astype(int)
new_matrix = matrix
new_matrix["row_sums"] = new_matrix.apply(sum, axis=1)
new_matrix["row_sums"] = new_matrix["row_sums"].apply(int)
new_matrix = new_matrix.sort_values(by="row_sums", ascending=False)

matrix = new_matrix[MORAL_VALUES]


rankings = kemeny_young(preference_matrix)

# total_scores = sorted(zip(MORAL_VALUES, rankings, matrix["row_sums"].values), key=lambda x:x[1])
print(matrix)
# print(new_matrix)
print("Kemeny-Young Ranking Scores:", rankings)
# print(matrix["row_sums"].values)
sns.heatmap(matrix, cmap="Blues",annot=True, fmt=".3g")
plt.show()
return matrix, rankings


if __name__ == "__main__":
get_pair_pref_ranking(pair_preference_gpt_4o)
get_pair_pref_ranking(normalise_pair_prefs(pair_preference_gpt_35))
7 changes: 6 additions & 1 deletion check_inconsistencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@


def plot(what):
nodes = [x for x, _ in what]
nodes = [x for x in what]

edges = [(nodes[i], nodes[i + 1]) for i in range(len(nodes) - 1)]
node_coords = {node: (0, 10 * i) for i, node in enumerate(nodes)}
print(edges)
plot_single_graph(edges=edges, node_coords=node_coords)

<<<<<<< HEAD

plot(ordered_pair_prefs_list)
=======
plot(['liberty', 'sanctity', 'loyalty', 'care', 'authority', 'fairness'])
# plot(ordered_total_prefs_list)
>>>>>>> c22fd9004dad46f4af85f6d77864a8ea121fc53e
# check monotonic ordering 1 vs 2

# check monotonic ordering 1 vs all
Expand Down
2 changes: 2 additions & 0 deletions mock_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@

#graph
pair_preference_gpt_35 = {('authority', 'care'): {'authority': 335, 'care': 741, 'neither': 3}, ('authority', 'fairness'): {'authority': 433, 'fairness': 642, 'neither': 4}, ('authority', 'liberty'): {'authority': 545, 'liberty': 525, 'neither': 9}, ('authority', 'loyalty'): {'authority': 733, 'loyalty': 331, 'neither': 15}, ('authority', 'sanctity'): {'authority': 592, 'sanctity': 482, 'neither': 5}, ('care', 'fairness'): {'care': 600, 'fairness': 478, 'neither': 1}, ('care', 'liberty'): {'care': 687, 'liberty': 386, 'neither': 6}, ('care', 'loyalty'): {'care': 900, 'loyalty': 170, 'neither': 9}, ('care', 'sanctity'): {'care': 797, 'sanctity': 276, 'neither': 6}, ('fairness', 'liberty'): {'fairness': 673, 'liberty': 401, 'neither': 5}, ('fairness', 'loyalty'): {'fairness': 790, 'loyalty': 281, 'neither': 8}, ('fairness', 'sanctity'): {'fairness': 674, 'sanctity': 403, 'neither': 2}, ('liberty', 'loyalty'): {'liberty': 717, 'loyalty': 343, 'neither': 19}, ('liberty', 'sanctity'): {'liberty': 606, 'sanctity': 460, 'neither': 13}, ('loyalty', 'sanctity'): {'loyalty': 383, 'sanctity': 678, 'neither': 18}}
triple_preference_gpt_35 = {('authority', 'care', 'fairness'): {'authority': 305, 'care': 427, 'fairness': 345, 'neither': 2}, ('authority', 'care', 'liberty'): {'authority': 332, 'care': 450, 'liberty': 287, 'neither': 10}, ('authority', 'care', 'loyalty'): {'authority': 317, 'care': 468, 'loyalty': 285, 'neither': 9}, ('authority', 'care', 'sanctity'): {'authority': 293, 'care': 474, 'sanctity': 300, 'neither': 12}, ('authority', 'fairness', 'liberty'): {'authority': 344, 'fairness': 389, 'liberty': 341, 'neither': 5}, ('authority', 'fairness', 'loyalty'): {'authority': 331, 'fairness': 427, 'loyalty': 314, 'neither': 7}, ('authority', 'fairness', 'sanctity'): {'authority': 339, 'fairness': 385, 'sanctity': 352, 'neither': 3}, ('authority', 'liberty', 'loyalty'): {'authority': 387, 'liberty': 364, 'loyalty': 312, 'neither': 16}, ('authority', 'liberty', 'sanctity'): {'authority': 407, 'liberty': 333, 'sanctity': 332, 'neither': 7}, ('authority', 'loyalty', 'sanctity'): {'authority': 384, 'loyalty': 300, 'sanctity': 382, 'neither': 13}, ('care', 'fairness', 'liberty'): {'care': 417, 'fairness': 357, 'liberty': 301, 'neither': 4}, ('care', 'fairness', 'loyalty'): {'care': 416, 'fairness': 370, 'loyalty': 285, 'neither': 8}, ('care', 'fairness', 'sanctity'): {'care': 434, 'fairness': 347, 'sanctity': 289, 'neither': 9}, ('care', 'liberty', 'loyalty'): {'care': 463, 'liberty': 278, 'loyalty': 320, 'neither': 18}, ('care', 'liberty', 'sanctity'): {'care': 452, 'liberty': 304, 'sanctity': 314, 'neither': 9}, ('care', 'loyalty', 'sanctity'): {'care': 456, 'loyalty': 277, 'sanctity': 325, 'neither': 21}, ('fairness', 'liberty', 'loyalty'): {'fairness': 416, 'liberty': 344, 'loyalty': 306, 'neither': 13}, ('fairness', 'liberty', 'sanctity'): {'fairness': 418, 'liberty': 312, 'sanctity': 344, 'neither': 5}, ('fairness', 'loyalty', 'sanctity'): {'fairness': 415, 'loyalty': 309, 'sanctity': 339, 'neither': 16}, ('liberty', 'loyalty', 'sanctity'): {'liberty': 360, 'loyalty': 330, 'sanctity': 369, 'neither': 20}}
results_trp = [3., 5. ,4., 1., 0., 2.]

pair_preference_gpt_4o = {('authority', 'care'): {'authority': 168, 'care': 368, 'neither': 4}, ('authority', 'fairness'): {'authority': 229, 'fairness': 310, 'neither': 1}, ('authority', 'liberty'): {'authority': 223, 'liberty': 316, 'neither': 1}, ('authority', 'loyalty'): {'authority': 376, 'loyalty': 157, 'neither': 7}, ('authority', 'sanctity'): {'authority': 287, 'sanctity': 250, 'neither': 3}, ('care', 'fairness'): {'care': 322, 'fairness': 217, 'neither': 1}, ('care', 'liberty'): {'care': 365, 'liberty': 175, 'neither': 0}, ('care', 'loyalty'): {'care': 468, 'loyalty': 68, 'neither': 4}, ('care', 'sanctity'): {'care': 413, 'sanctity': 127, 'neither': 0}, ('fairness', 'liberty'): {'fairness': 299, 'liberty': 241, 'neither': 0}, ('fairness', 'loyalty'): {'fairness': 417, 'loyalty': 121, 'neither': 2}, ('fairness', 'sanctity'): {'fairness': 332, 'sanctity': 207, 'neither': 1}, ('liberty', 'loyalty'): {'liberty': 406, 'loyalty': 128, 'neither': 6}, ('liberty', 'sanctity'): {'liberty': 320, 'sanctity': 218, 'neither': 2}, ('loyalty', 'sanctity'): {'loyalty': 160, 'sanctity': 376, 'neither': 4}}
triple_preference_gpt_4o = {('authority', 'care', 'fairness'): {'authority': 143, 'care': 222, 'fairness': 175, 'neither': 0}, ('authority', 'care', 'liberty'): {'authority': 157, 'care': 231, 'liberty': 152, 'neither': 0}, ('authority', 'care', 'loyalty'): {'authority': 152, 'care': 256, 'loyalty': 132, 'neither': 0}, ('authority', 'care', 'sanctity'): {'authority': 142, 'care': 254, 'sanctity': 143, 'neither': 1}, ('authority', 'fairness', 'liberty'): {'authority': 157, 'fairness': 218, 'liberty': 165, 'neither': 0}, ('authority', 'fairness', 'loyalty'): {'authority': 182, 'fairness': 229, 'loyalty': 128, 'neither': 1}, ('authority', 'fairness', 'sanctity'): {'authority': 163, 'fairness': 220, 'sanctity': 156, 'neither': 1}, ('authority', 'liberty', 'loyalty'): {'authority': 203, 'liberty': 205, 'loyalty': 131, 'neither': 1}, ('authority', 'liberty', 'sanctity'): {'authority': 156, 'liberty': 205, 'sanctity': 178, 'neither': 1}, ('authority', 'loyalty', 'sanctity'): {'authority': 190, 'loyalty': 152, 'sanctity': 196, 'neither': 2}, ('care', 'fairness', 'liberty'): {'care': 201, 'fairness': 193, 'liberty': 146, 'neither': 0}, ('care', 'fairness', 'loyalty'): {'care': 224, 'fairness': 190, 'loyalty': 124, 'neither': 2}, ('care', 'fairness', 'sanctity'): {'care': 204, 'fairness': 196, 'sanctity': 140, 'neither': 0}, ('care', 'liberty', 'loyalty'): {'care': 249, 'liberty': 141, 'loyalty': 146, 'neither': 4}, ('care', 'liberty', 'sanctity'): {'care': 246, 'liberty': 149, 'sanctity': 142, 'neither': 3}, ('care', 'loyalty', 'sanctity'): {'care': 259, 'loyalty': 137, 'sanctity': 143, 'neither': 1}, ('fairness', 'liberty', 'loyalty'): {'fairness': 226, 'liberty': 180, 'loyalty': 133, 'neither': 1}, ('fairness', 'liberty', 'sanctity'): {'fairness': 224, 'liberty': 172, 'sanctity': 143, 'neither': 1}, ('fairness', 'loyalty', 'sanctity'): {'fairness': 238, 'loyalty': 136, 'sanctity': 165, 'neither': 1}, ('liberty', 'loyalty', 'sanctity'): {'liberty': 202, 'loyalty': 150, 'sanctity': 184, 'neither': 4}}
Expand Down
Binary file added report-images/data_div.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/data_q_hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt25_pp_hm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt35_pp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt35_sp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt35_tp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt35_trp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt35_trp_hm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/gpt_35_pp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added report-images/pair_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
Binary file added report-images/single_val_prefs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added report-images/total_prefs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions table.csv

This file was deleted.

32 changes: 30 additions & 2 deletions triple_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import seaborn as sns
import matplotlib.pyplot as plt

from mock_results import triple_preference
from mock_results import triple_preference, triple_preference_gpt_35
from constants import MORAL_VALUES

from calculate_scores import kemeny_young

def tuple_to_pairwise_matrix(triples):
# Get all unique items and map them to indices
Expand Down Expand Up @@ -37,4 +37,32 @@ def triples_to_preference_matrix(triple_preference):
df = pd.DataFrame(pairwise_matrix, columns=MORAL_VALUES, index=MORAL_VALUES)
return df

def get_triple_pref_ranking(preference_matrix):
# best_ranking, min_distance = kemeny_young_method(MORAL_VALUES, pair_preference_var[0])
# print("Kemeny-Young ranking:", best_ranking, min_distance)

matrix = pd.DataFrame(data=preference_matrix, columns=MORAL_VALUES, index=MORAL_VALUES)
matrix["row_sums"] = matrix.apply(sum, axis=1)
matrix["row_sums"] = matrix["row_sums"].apply(int)
matrix = matrix.sort_values(by="row_sums", ascending=False)

matrix = matrix[MORAL_VALUES]
for col in matrix.columns:
matrix[col] = matrix[col].astype(int)
matrix[col] = matrix[col].apply(lambda val: val*100/1079/5)
rankings = kemeny_young(preference_matrix)

# total_scores = sorted(zip(MORAL_VALUES, rankings, matrix["row_sums"].values), key=lambda x:x[1])
print(matrix)
print("Kemeny-Young Ranking Scores:", rankings)
# print(matrix["row_sums"].values)
sns.heatmap(matrix, cmap="Blues",annot=True, fmt=".0f")
plt.show()
return rankings

if __name__ == "__main__":
matrix = triples_to_preference_matrix(triple_preference_gpt_35)
print(matrix)
x= get_triple_pref_ranking(matrix.to_numpy())
# print([MORAL_VALUES[int(i)] for i in x ])

2 changes: 1 addition & 1 deletion visualize_preference_rankings.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def plot_single_graph(edges, node_coords, output_filename="single_graph.png"):
ax.margins(0.20)
plt.axis("off")
plt.show()
plt.close()
# plt.close()
# ax.figure.savefig(output_filename)

def get_counts(new_dict):
Expand Down

0 comments on commit 12b917a

Please sign in to comment.