{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Directed Measures\n" ], "id": "ca4b3e01" }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. admonition:: Definition\n", "\n", " Directed hypergraphs distinguish source and target node sets for each hyperedge.\n" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. admonition:: You will learn\n", "\n", " Compute directed measures and compare in- vs out-structure effects.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "- Compute directed hypergraph measures and interpret in/out effects.\n", "- Compare directed vs undirected behavior on examples.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "import matplotlib as mpl\n", "\n", "mpl.rcParams.update({\n", " \"figure.figsize\": (6, 4),\n", " \"figure.dpi\": 120,\n", " \"savefig.dpi\": 150,\n", "})\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import hypergraphx as hgx" ], "id": "39767ae3" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "edges = [\n", " ((1,), (2,)), # Edge 1: Simple directed edge from 1 to 2\n", " ((2,), (1,)), # Edge 2: Simple directed edge from 2 to 1 (reciprocal with Edge 1)\n", " ((2, 3), (4,)), # Edge 2: Directed hyperedge with sources 2, 3 and target 4\n", " ((4,), (2, 3)), # Edge 3: Directed hyperedge from 4 to targets 2, 5\n", " ((5,), (1,)), # Edge 4: Directed edge from 5 to 1 (reciprocal with Edge 1)\n", " ((6,), (3, 4)), # Edge 5: Directed hyperedge from 6 to targets 3 and 4\n", " ((7,), (2, 6)), # Edge 6: Directed hyperedge from 7 to targets 2 and 6\n", " ((3, 8), (5,)), # Edge 7: Directed hyperedge with sources 3, 8 and target 5\n", " ((4, 5), (7, 8)) # Edge 8: Directed hyperedge with sources 4, 5 and targets 7, 8\n", "]" ], "id": "6216e269" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "h = hgx.DirectedHypergraph(edges)" ], "id": "848b7e77" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from hypergraphx.measures.directed import in_degree, out_degree, in_degree_sequence, out_degree_sequence" ], "id": "f0f5cf4d" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In-degree of node 1: 1\n", "In-degree of node 2: 2\n", "Out-degree of node 1: 2\n", "Out-degree of node 2: 3\n" ] } ], "source": [ "print(\"In-degree of node 1:\", in_degree(h, 1))\n", "print(\"In-degree of node 2:\", in_degree(h, 2))\n", "print(\"Out-degree of node 1:\", out_degree(h, 1))\n", "print(\"Out-degree of node 2:\", out_degree(h, 2))" ], "id": "efa51b6b" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In-degree sequence: {1: 1, 2: 2, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1, 8: 1}\n", "Out-degree sequence: {1: 2, 2: 3, 3: 2, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1}\n" ] } ], "source": [ "print(\"In-degree sequence: \", in_degree_sequence(h))\n", "print(\"Out-degree sequence: \", out_degree_sequence(h))" ], "id": "c4b4188d" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from hypergraphx.measures.directed import hyperedge_signature_vector" ], "id": "e6e00205" }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hyperedge signature vector: [3. 3. 0. 2. 1. 0. 0. 0. 0.]\n" ] } ], "source": [ "v = hyperedge_signature_vector(h)\n", "print(\"Hyperedge signature vector: \", v)" ], "id": "d03d625a" }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from hypergraphx.measures.directed import exact_reciprocity, strong_reciprocity, weak_reciprocity" ], "id": "dc085cec" }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exact reciprocity: {2: 0.6666666666666666, 3: 0.4, 4: 0.0}\n", "Strong reciprocity: {2: 0.6666666666666666, 3: 0.4, 4: 0.0}\n", "Weak reciprocity: {2: 0.6666666666666666, 3: 0.6, 4: 1.0}\n" ] } ], "source": [ "print(\"Exact reciprocity: \", exact_reciprocity(h, max_hyperedge_size=4))\n", "print(\"Strong reciprocity: \", strong_reciprocity(h, max_hyperedge_size=4))\n", "print(\"Weak reciprocity: \", weak_reciprocity(h, max_hyperedge_size=4))" ], "id": "ff750517" }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from hypergraphx.motifs.directed_motifs import *" ], "id": "d9118e9e" }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Computing observed motifs of order 3...\n" ] } ], "source": [ "m = compute_directed_motifs(h, order=3, runs_config_model=0)" ], "id": "ffbe991c" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[((((1,), (2, 3)),), 2),\n", " ((((1,), (2, 3)), ((2, 3), (1,))), 1),\n", " ((((1, 2), (3,)),), 1)]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m['observed']" ], "id": "79919a5f" } ], "metadata": { "kernelspec": { "display_name": "hgx-installation", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.17" } }, "nbformat": 4, "nbformat_minor": 5 }