diff --git a/docs/source/api/recipes/recipes.ipynb b/docs/source/api/recipes/recipes.ipynb index 477cd35c0..b5dd5fc4e 100644 --- a/docs/source/api/recipes/recipes.ipynb +++ b/docs/source/api/recipes/recipes.ipynb @@ -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": {