Skip to content

Commit

Permalink
Added recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Mar 9, 2024
1 parent 6ea9df9 commit fea6b81
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions docs/source/api/recipes/recipes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,73 @@
"matching_ids = [k for k, v in H.degree().items() if v == d]\n",
"print(f\"Nodes {', '.join(matching_ids)} have degree {d}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 18. Define a custom filtering function\n",
"\n",
"In addition to the pre-defined filtering functionality, one can also define a custom comparison operator to compare statistics and attributes.\n",
"\n",
"First, we show an example for numerical statistics and second, an example for attributes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Numerical statistics**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import xgi\n",
"\n",
"outsiderange = lambda val, arg: arg[0] >= val or val >= arg[1]\n",
"\n",
"H = xgi.load_xgi_data(\"email-enron\")\n",
"print(H.num_nodes)\n",
"\n",
"# Get all of the dates\n",
"nodes = H.nodes.filterby(\"degree\", [3, 20], mode=outsiderange)\n",
"print(len(nodes))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Attributes**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"\n",
"import xgi\n",
"\n",
"date1 = datetime.datetime(2000, 1, 1)\n",
"date2 = datetime.datetime(2001, 1, 1)\n",
"datecompare = (\n",
" lambda date, arg: arg[0] <= datetime.datetime.fromisoformat(date) <= arg[1]\n",
")\n",
"\n",
"H = xgi.load_xgi_data(\"email-enron\")\n",
"print(H.num_edges)\n",
"\n",
"# Get all of the dates\n",
"e = H.edges.filterby_attr(\"timestamp\", [date1, date2], mode=datecompare)\n",
"print(len(e))"
]
}
],
"metadata": {
Expand Down

0 comments on commit fea6b81

Please sign in to comment.