Skip to content

Commit

Permalink
Stats argsort (#521)
Browse files Browse the repository at this point in the history
* added argsort to stats

* added unit tests
  • Loading branch information
nwlandry authored Mar 4, 2024
1 parent faacbfe commit 20fa11f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
10 changes: 5 additions & 5 deletions docs/source/api/recipes/recipes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@
"m, n = 10, 20\n",
"p = 0.5\n",
"\n",
"G = nx.triangular_lattice_graph(m,n, with_positions=True)\n",
"pos = nx.get_node_attributes(G, 'pos')\n",
"G = nx.triangular_lattice_graph(m, n, with_positions=True)\n",
"pos = nx.get_node_attributes(G, \"pos\")\n",
"\n",
"mapping = {i: list(G.nodes)[i] for i in range(0, len(list(G.nodes)))}\n",
"inv_mapping = {v: k for k, v in mapping.items()}\n",
Expand Down Expand Up @@ -783,12 +783,12 @@
" lens += tup[1].values()\n",
"\n",
"# remove lengths 0 for self-loops\n",
"lens = [i for i in lens if i!=0] \n",
"lens = [i for i in lens if i != 0]\n",
"\n",
"# replace inf by 0 for disconnected nodes\n",
"lens = [0 if i==np.inf else i for i in lens] \n",
"lens = [0 if i == np.inf else i for i in lens]\n",
"\n",
"avg_shortest_path = np.sum(lens) / (N * (N-1))\n",
"avg_shortest_path = np.sum(lens) / (N * (N - 1))\n",
"print(\"The average shortest path length is\", avg_shortest_path)"
]
}
Expand Down
4 changes: 3 additions & 1 deletion tests/drawing/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ def test_issue_515(edgelist8):

with warnings.catch_warnings():
warnings.simplefilter("error")
ax, (node_coll, edge_coll) = xgi.draw_multilayer(H, node_fc=["black"] * H.num_nodes)
ax, (node_coll, edge_coll) = xgi.draw_multilayer(
H, node_fc=["black"] * H.num_nodes
)

plt.close("all")
16 changes: 16 additions & 0 deletions tests/stats/test_nodestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ def test_aggregates(edgelist1, edgelist2, edgelist8):
assert H.nodes.degree.min() == 1
assert H.nodes.degree.argmax() == 6
assert H.nodes.degree.argmin() == 1
assert H.nodes.degree.argsort() == list(H.nodes.filterby("degree", 1)) + list(
H.nodes.filterby("degree", 2)
)
assert H.nodes.degree.argsort(reverse=True) == list(
H.nodes.filterby("degree", 2)
) + list(H.nodes.filterby("degree", 1))
assert H.nodes.degree.sum() == 9
assert round(H.nodes.degree.mean(), 3) == 1.125
assert round(H.nodes.degree.std(), 3) == 0.331
Expand All @@ -244,6 +250,9 @@ def test_aggregates(edgelist1, edgelist2, edgelist8):
assert H.nodes.degree.min() == 1
assert H.nodes.degree.argmax() == 4
assert H.nodes.degree.argmin() == 1
assert H.nodes.degree.argsort() == list(H.nodes.filterby("degree", 1)) + list(
H.nodes.filterby("degree", 2)
)
assert H.nodes.degree.sum() == 7
assert round(H.nodes.degree.mean(), 3) == 1.167
assert round(H.nodes.degree.std(), 3) == 0.373
Expand All @@ -254,6 +263,13 @@ def test_aggregates(edgelist1, edgelist2, edgelist8):
assert H.nodes.degree.min() == 2
assert H.nodes.degree.argmax() == 0
assert H.nodes.degree.argmin() == 5
assert H.nodes.degree.argsort() == list(H.nodes.filterby("degree", 2)) + list(
H.nodes.filterby("degree", 3)
) + list(H.nodes.filterby("degree", 4)) + list(
H.nodes.filterby("degree", 5)
) + list(
H.nodes.filterby("degree", 6)
)
assert H.nodes.degree.sum() == 26
assert round(H.nodes.degree.mean(), 3) == 3.714
assert round(H.nodes.degree.std(), 3) == 1.385
Expand Down
19 changes: 19 additions & 0 deletions xgi/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,25 @@ def argmax(self):
d = self.asdict()
return max(d, key=d.get)

def argsort(self, reverse=False):
"""Get the list of IDs sorted by stat value.
When values are not unique, the order of the IDs
is preserved.
Parameters
----------
reverse : bool
Whether the sorting should be ascending or descending.
Returns
-------
list
The IDs sorted in ascending or descending order.
"""
d = self.asdict()
return sorted(d, key=d.get, reverse=reverse)


class NodeStat(IDStat):
"""An arbitrary node-quantity mapping.
Expand Down

0 comments on commit 20fa11f

Please sign in to comment.