Update app.py
Browse files
app.py
CHANGED
@@ -29,6 +29,8 @@ import pandas as pd
|
|
29 |
import plotly.express as px
|
30 |
from plotly.graph_objects import Figure
|
31 |
|
|
|
|
|
32 |
|
33 |
def visualize(df_pairwise: pd.DataFrame) -> Figure:
|
34 |
fig = px.imshow(df_pairwise, color_continuous_scale='RdBu', text_auto='.2f')
|
@@ -38,8 +40,7 @@ def visualize(df_pairwise: pd.DataFrame) -> Figure:
|
|
38 |
|
39 |
|
40 |
# https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-bradley_terry-py
|
41 |
-
def bradley_terry(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]
|
42 |
-
seed: int = 0, tolerance: float = 10e-6, limit: int = 20) -> npt.NDArray[np.float64]:
|
43 |
M = wins + .5 * ties
|
44 |
|
45 |
T = M.T + M
|
@@ -65,7 +66,7 @@ def bradley_terry(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
|
|
65 |
p_new /= Z.sum(axis=0)
|
66 |
p_new /= p_new.sum()
|
67 |
|
68 |
-
converged = bool(np.linalg.norm(p_new - p) <
|
69 |
|
70 |
p[:] = p_new
|
71 |
|
@@ -85,30 +86,26 @@ def centrality(algorithm: Callable[[nx.DiGraph], dict[int, float]],
|
|
85 |
return p
|
86 |
|
87 |
|
88 |
-
def counting(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]
|
89 |
-
seed: int = 0, tolerance: float = 10e-6, limit: int = 100) -> npt.NDArray[np.float64]:
|
90 |
M = wins + .5 * ties
|
91 |
|
92 |
return cast(npt.NDArray[np.float64], M.sum(axis=1))
|
93 |
|
94 |
|
95 |
-
def eigen(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]
|
96 |
-
|
97 |
-
algorithm = partial(nx.algorithms.eigenvector_centrality_numpy, max_iter=limit, tol=tolerance, weight='weight')
|
98 |
|
99 |
return centrality(algorithm, wins, ties)
|
100 |
|
101 |
|
102 |
-
def pagerank(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]
|
103 |
-
|
104 |
-
algorithm = partial(nx.algorithms.pagerank, max_iter=limit, tol=tolerance, weight='weight')
|
105 |
|
106 |
return centrality(algorithm, wins, ties)
|
107 |
|
108 |
|
109 |
# https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-newman-py
|
110 |
-
def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]
|
111 |
-
seed: int = 0, tolerance: float = 10e-6, limit: int = 20) -> npt.NDArray[np.float64]:
|
112 |
pi, v = np.ones(wins.shape[0]), .5
|
113 |
|
114 |
converged, iterations = False, 0
|
@@ -127,7 +124,7 @@ def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
|
|
127 |
)
|
128 |
|
129 |
v = v_numerator / v_denominator
|
130 |
-
v = np.nan_to_num(v, nan=
|
131 |
|
132 |
pi_old = pi.copy()
|
133 |
|
@@ -144,10 +141,10 @@ def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
|
|
144 |
)
|
145 |
|
146 |
pi = pi_numerator / pi_denominator
|
147 |
-
pi = np.nan_to_num(pi, nan=
|
148 |
|
149 |
converged = np.allclose(pi / (pi + 1), pi_old / (pi_old + 1),
|
150 |
-
rtol=
|
151 |
|
152 |
return pi
|
153 |
|
@@ -219,7 +216,7 @@ def handler(file: BinaryIO, algorithm: str, filtered: bool, truncated: bool, see
|
|
219 |
|
220 |
assert wins.shape == ties.shape, 'wins and ties shapes are different'
|
221 |
|
222 |
-
scores = ALGORITHMS[algorithm](wins, ties
|
223 |
|
224 |
df_result = pd.DataFrame(data={'score': scores}, index=index)
|
225 |
|
|
|
29 |
import plotly.express as px
|
30 |
from plotly.graph_objects import Figure
|
31 |
|
32 |
+
TOLERANCE, LIMIT = 1e-16, 1000
|
33 |
+
|
34 |
|
35 |
def visualize(df_pairwise: pd.DataFrame) -> Figure:
|
36 |
fig = px.imshow(df_pairwise, color_continuous_scale='RdBu', text_auto='.2f')
|
|
|
40 |
|
41 |
|
42 |
# https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-bradley_terry-py
|
43 |
+
def bradley_terry(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]) -> npt.NDArray[np.float64]:
|
|
|
44 |
M = wins + .5 * ties
|
45 |
|
46 |
T = M.T + M
|
|
|
66 |
p_new /= Z.sum(axis=0)
|
67 |
p_new /= p_new.sum()
|
68 |
|
69 |
+
converged = bool(np.linalg.norm(p_new - p) < TOLERANCE) or (iterations >= LIMIT)
|
70 |
|
71 |
p[:] = p_new
|
72 |
|
|
|
86 |
return p
|
87 |
|
88 |
|
89 |
+
def counting(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]) -> npt.NDArray[np.float64]:
|
|
|
90 |
M = wins + .5 * ties
|
91 |
|
92 |
return cast(npt.NDArray[np.float64], M.sum(axis=1))
|
93 |
|
94 |
|
95 |
+
def eigen(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]) -> npt.NDArray[np.float64]:
|
96 |
+
algorithm = partial(nx.algorithms.eigenvector_centrality_numpy, max_iter=LIMIT, tol=TOLERANCE, weight='weight')
|
|
|
97 |
|
98 |
return centrality(algorithm, wins, ties)
|
99 |
|
100 |
|
101 |
+
def pagerank(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]) -> npt.NDArray[np.float64]:
|
102 |
+
algorithm = partial(nx.algorithms.pagerank, max_iter=LIMIT, tol=TOLERANCE, weight='weight')
|
|
|
103 |
|
104 |
return centrality(algorithm, wins, ties)
|
105 |
|
106 |
|
107 |
# https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-newman-py
|
108 |
+
def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64]) -> npt.NDArray[np.float64]:
|
|
|
109 |
pi, v = np.ones(wins.shape[0]), .5
|
110 |
|
111 |
converged, iterations = False, 0
|
|
|
124 |
)
|
125 |
|
126 |
v = v_numerator / v_denominator
|
127 |
+
v = np.nan_to_num(v, nan=TOLERANCE)
|
128 |
|
129 |
pi_old = pi.copy()
|
130 |
|
|
|
141 |
)
|
142 |
|
143 |
pi = pi_numerator / pi_denominator
|
144 |
+
pi = np.nan_to_num(pi, nan=TOLERANCE)
|
145 |
|
146 |
converged = np.allclose(pi / (pi + 1), pi_old / (pi_old + 1),
|
147 |
+
rtol=TOLERANCE, atol=TOLERANCE) or (iterations >= LIMIT)
|
148 |
|
149 |
return pi
|
150 |
|
|
|
216 |
|
217 |
assert wins.shape == ties.shape, 'wins and ties shapes are different'
|
218 |
|
219 |
+
scores = ALGORITHMS[algorithm](wins, ties)
|
220 |
|
221 |
df_result = pd.DataFrame(data={'score': scores}, index=index)
|
222 |
|