dustalov commited on
Commit
1a4fad6
1 Parent(s): ed19e77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -15,7 +15,7 @@
15
  __author__ = 'Dmitry Ustalov'
16
  __license__ = 'Apache 2.0'
17
 
18
- from typing import IO, Tuple, List, cast, Dict, Set
19
 
20
  import gradio as gr
21
  import networkx as nx
@@ -68,19 +68,30 @@ def bradley_terry(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
68
  return p
69
 
70
 
71
- def pagerank(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
72
- seed: int = 0, tolerance: float = 10e-6, limit: int = 100) -> npt.NDArray[np.float64]:
 
73
  A = wins + .5 * ties
74
 
75
  G = nx.from_numpy_array(A, create_using=nx.DiGraph)
76
 
77
- scores: Dict[int, float] = nx.algorithms.pagerank(G, max_iter=limit, tol=tolerance)
78
 
79
  p = np.array([scores[i] for i in range(len(G))])
80
 
81
  return p
82
 
83
 
 
 
 
 
 
 
 
 
 
 
84
  # https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-newman-py
85
  def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
86
  seed: int = 0, tolerance: float = 10e-6, limit: int = 20) -> npt.NDArray[np.float64]:
@@ -131,6 +142,7 @@ def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
131
 
132
  ALGORITHMS = {
133
  'Bradley-Terry (1952)': bradley_terry,
 
134
  'PageRank (1998)': pagerank,
135
  'Newman (2023)': newman,
136
  }
@@ -232,7 +244,8 @@ def main() -> None:
232
  gr.Checkbox(
233
  value=False,
234
  label='Largest SCC',
235
- info='Bradley-Terry and Newman algorithms require the comparison graph to be strongly-connected. '
 
236
  'This option keeps only the largest strongly-connected component (SCC) of the input graph. '
237
  'Some items might be missing as a result of this filtering.'
238
  ),
@@ -252,6 +265,7 @@ def main() -> None:
252
  ],
253
  examples=[
254
  ['food.csv', 'Bradley-Terry (1952)', False],
 
255
  ['food.csv', 'PageRank (1998)', False],
256
  ['food.csv', 'Newman (2023)', False]
257
  ],
 
15
  __author__ = 'Dmitry Ustalov'
16
  __license__ = 'Apache 2.0'
17
 
18
+ from typing import IO, Tuple, List, cast, Dict, Set, Callable
19
 
20
  import gradio as gr
21
  import networkx as nx
 
68
  return p
69
 
70
 
71
+ def centrality(algorithm: Callable[..., Dict[int, float]],
72
+ wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
73
+ tolerance: float = 10e-6, limit: int = 100) -> npt.NDArray[np.float64]:
74
  A = wins + .5 * ties
75
 
76
  G = nx.from_numpy_array(A, create_using=nx.DiGraph)
77
 
78
+ scores: Dict[int, float] = algorithm(G, max_iter=limit, tol=tolerance)
79
 
80
  p = np.array([scores[i] for i in range(len(G))])
81
 
82
  return p
83
 
84
 
85
+ def eigen(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
86
+ seed: int = 0, tolerance: float = 10e-6, limit: int = 100) -> npt.NDArray[np.float64]:
87
+ return centrality(nx.algorithms.eigenvector_centrality_numpy, wins, ties, tolerance, limit)
88
+
89
+
90
+ def pagerank(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
91
+ seed: int = 0, tolerance: float = 10e-6, limit: int = 100) -> npt.NDArray[np.float64]:
92
+ return centrality(nx.algorithms.pagerank, wins, ties, tolerance, limit)
93
+
94
+
95
  # https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-newman-py
96
  def newman(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
97
  seed: int = 0, tolerance: float = 10e-6, limit: int = 20) -> npt.NDArray[np.float64]:
 
142
 
143
  ALGORITHMS = {
144
  'Bradley-Terry (1952)': bradley_terry,
145
+ 'Eigenvector (1986)': eigen,
146
  'PageRank (1998)': pagerank,
147
  'Newman (2023)': newman,
148
  }
 
244
  gr.Checkbox(
245
  value=False,
246
  label='Largest SCC',
247
+ info='Bradley-Terry, Eigenvector, and Newman algorithms require the comparison graph '
248
+ 'to be strongly-connected. '
249
  'This option keeps only the largest strongly-connected component (SCC) of the input graph. '
250
  'Some items might be missing as a result of this filtering.'
251
  ),
 
265
  ],
266
  examples=[
267
  ['food.csv', 'Bradley-Terry (1952)', False],
268
+ ['food.csv', 'Eigenvector (1986)', False],
269
  ['food.csv', 'PageRank (1998)', False],
270
  ['food.csv', 'Newman (2023)', False]
271
  ],