Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement set operations for NodeView and EdgeView #208

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tutorial to include set operations
  • Loading branch information
leotrs committed Nov 1, 2022
commit b4c574767588827e6f0b4a1f52952926d7e5fb43
151 changes: 101 additions & 50 deletions tutorials/Tutorial 6 - Statistics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,70 @@
"H.nodes.filterby(\"degree\", H.nodes.degree.max())"
]
},
{
"cell_type": "markdown",
"id": "11548f49",
"metadata": {},
"source": [
"Another way of chaining multiple results of `filterby*` methods is by using set operations. Indeed, chaining two filters is the same as intersecting the results of two separate calls:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "970da532",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 5]\n",
"[2, 5]\n"
]
}
],
"source": [
"print(H.nodes.filterby(\"degree\", 2).filterby_attr(\"color\", \"blue\"))\n",
"print(H.nodes.filterby(\"degree\", 2) & H.nodes.filterby_attr(\"color\", \"blue\"))"
]
},
{
"cell_type": "markdown",
"id": "dc0ff694",
"metadata": {},
"source": [
"Other set operations are also supported."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "65c22a67",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"nodes1 - nodes2 = NodeView((4,))\n",
"nodes2 - nodes1 = NodeView(())\n",
"nodes1 & nodes2 = NodeView((2, 5))\n",
"nodes1 | nodes2 = NodeView((2, 4, 5))\n",
"nodes1 ^ nodes2 = NodeView((4,))\n"
]
}
],
"source": [
"nodes1 = H.nodes.filterby(\"degree\", 2)\n",
"nodes2 = H.nodes.filterby_attr(\"color\", \"blue\")\n",
"print(f\"{nodes1 - nodes2 = }\")\n",
"print(f\"{nodes2 - nodes1 = }\")\n",
"print(f\"{nodes1 & nodes2 = }\")\n",
"print(f\"{nodes1 | nodes2 = }\")\n",
"print(f\"{nodes1 ^ nodes2 = }\")"
]
},
{
"cell_type": "markdown",
"id": "697055d8",
Expand All @@ -760,7 +824,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 28,
"id": "09aeadef",
"metadata": {},
"outputs": [
Expand All @@ -770,7 +834,7 @@
"MultiNodeStat(degree, clustering)"
]
},
"execution_count": 26,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -789,7 +853,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 29,
"id": "31619b17",
"metadata": {
"scrolled": true
Expand All @@ -805,7 +869,7 @@
" 5: {'degree': 2, 'clustering': 3.0}}"
]
},
"execution_count": 27,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -824,7 +888,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 30,
"id": "c29d8461",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -903,7 +967,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 31,
"id": "250d2808",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -971,7 +1035,7 @@
"5 2 3.000000"
]
},
"execution_count": 29,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -991,7 +1055,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 32,
"id": "3c47eac0",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1048,7 +1112,7 @@
"3 1.333333"
]
},
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1067,7 +1131,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 33,
"id": "d28315b8",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1098,7 +1162,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 34,
"id": "a6c92003",
"metadata": {
"scrolled": true
Expand Down Expand Up @@ -1174,7 +1238,7 @@
"5 2 1 blue"
]
},
"execution_count": 32,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1196,12 +1260,12 @@
"id": "29d4aa9f",
"metadata": {},
"source": [
"Every feature showcased above (lazy evaluation, type conversion, filtering, and multi objects) is supported for edge-quantity or edge-attribute mappings, via `EdgeStat` objects."
"Every feature showcased above (lazy evaluation, type conversion, filtering, set operations, and multi objects) is supported for edge-quantity or edge-attribute mappings, via `EdgeStat` objects."
]
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 35,
"id": "3a43de55",
"metadata": {},
"outputs": [
Expand All @@ -1211,7 +1275,7 @@
"EdgeStat('order')"
]
},
"execution_count": 33,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1222,7 +1286,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 36,
"id": "9e708cda",
"metadata": {},
"outputs": [
Expand All @@ -1232,7 +1296,7 @@
"{0: 2, 1: 3, 2: 2}"
]
},
"execution_count": 34,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1243,7 +1307,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 37,
"id": "29154c7e",
"metadata": {},
"outputs": [
Expand All @@ -1253,7 +1317,7 @@
"EdgeView((1,))"
]
},
"execution_count": 35,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1264,7 +1328,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 38,
"id": "6b319bfc",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1320,7 +1384,7 @@
"2 2 3"
]
},
"execution_count": 36,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1349,7 +1413,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 39,
"id": "fec60de5",
"metadata": {},
"outputs": [],
Expand All @@ -1360,9 +1424,17 @@
" return {n: 10 * net.degree(n) for n in bunch}"
]
},
{
"cell_type": "markdown",
"id": "b0be4a0d",
"metadata": {},
"source": [
"Now `user_degree` is a valid stat that can be computed on any hypergraph:"
]
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 40,
"id": "7409ae74",
"metadata": {},
"outputs": [
Expand All @@ -1372,7 +1444,7 @@
"{1: 10, 2: 20, 3: 30, 4: 20, 5: 20}"
]
},
"execution_count": 38,
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1386,12 +1458,12 @@
"id": "65f4b7bf",
"metadata": {},
"source": [
"Now every single feature showcased above is available for use with `user_degree`, including filtering nodes and multi stats objects."
"Every single feature showcased above is available for use with `user_degree`, including filtering nodes and multi stats objects."
]
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 41,
"id": "314751dc",
"metadata": {},
"outputs": [
Expand All @@ -1401,7 +1473,7 @@
"NodeView((2, 4, 5))"
]
},
"execution_count": 39,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1412,7 +1484,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 42,
"id": "7ae354dd",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1480,7 +1552,7 @@
"5 2 20"
]
},
"execution_count": 40,
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1505,27 +1577,6 @@
"User-defined edge statistics can similarly be defined using the `@xgi.edgestat` decorator."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ee7467f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 + 2"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down