From b98904e9e5a4ee14c1bcefeef757167dfa4aa165 Mon Sep 17 00:00:00 2001 From: luc Date: Fri, 28 Aug 2026 14:05:29 +0200 Subject: [PATCH] graph-construct/construct-graph.ipynb: add --- graph-construct/construct-graph.ipynb | 241 ++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 graph-construct/construct-graph.ipynb diff --git a/graph-construct/construct-graph.ipynb b/graph-construct/construct-graph.ipynb new file mode 100644 index 0000000..31ea9fb --- /dev/null +++ b/graph-construct/construct-graph.ipynb @@ -0,0 +1,241 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Construct interaction graphs\n", + "\n", + "Constructing interaction graphs and counting the neighbour interactions for each user." + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Import the dataset and create user nodes dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "\n", + "case = '...'\n", + "casegraphs = '...'\n", + "\n", + "df_data = pd.read_csv(f'../datasets/{case}.csv')\n", + "user_nodes = df_data.groupby('user')['id'].apply(list).to_dict()\n", + "\n", + "df_data.info()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Construct and export nearest-neighbour graph and count nearest-neighbours in nearest-neighbour graph." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import networkx as nx\n", + "\n", + "# Define undirected graph\n", + "G = nx.Graph()\n", + "\n", + "# Construct nearest-neighbour graph\n", + "for _, row in df_data.iterrows():\n", + " G.add_node(row['id'], user=row['user'], sentiment=row['s'], text=row['text'])\n", + " if row['parentid'] != 'no_parent':\n", + " G.add_edge(row['id'], row['parentid'])\n", + "\n", + "# Export nearest-neighbour graph\n", + "nx.write_graphml(G, f'../datasets/{casegraphs}-nn-graph.graphml')\n", + "\n", + "# Define nearest-neighbour interaction count dictionary\n", + "nn_count = {}\n", + "\n", + "# Count nearest neighbours for each user\n", + "for node in G.nodes(data=True):\n", + " node_id, attributes = node\n", + " user = attributes['user']\n", + "\n", + " count = len(set(G.neighbors(node_id)) - set(user_nodes[user]))\n", + "\n", + " if user in nn_count:\n", + " nn_count[user] += count\n", + " else:\n", + " nn_count[user] = count" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Construct and export transitive-closure graph and count nearest-neighbours in transitive-closure graph." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Construct transitive-closure graph\n", + "tc_G = nx.transitive_closure(G)\n", + "\n", + "# Export transitive-closure graph\n", + "nx.write_graphml(tc_G, f'../datasets/{casegraphs}-tc-graph.graphml')\n", + "\n", + "# Define transitive-closure interaction count dictionary\n", + "tc_count = {}\n", + "\n", + "# Count neighbours for each user\n", + "for node in tc_G.nodes(data=True):\n", + " node_id, attributes = node\n", + " user = attributes['user']\n", + "\n", + " count = len(set(tc_G.neighbors(node_id)) - set(user_nodes[user]))\n", + "\n", + " if user in tc_count:\n", + " tc_count[user] += count\n", + " else:\n", + " tc_count[user] = count" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Construct and export directed-transitive-closure graph and count nearest-neighbours in directed-transitive-closure graph." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define directed graph\n", + "dG = nx.DiGraph()\n", + "\n", + "# Construct nearest-neighbour directed graph\n", + "for _, row in df_data.iterrows():\n", + " dG.add_node(row['id'], user=row['user'], sentiment=row['s'], text=row['text'])\n", + " if row['parentid'] != 'no_parent':\n", + " dG.add_edge(row['id'], row['parentid'])\n", + "\n", + "nx.write_graphml(dG, f'../datasets/{casegraphs}-nn-dgraph.graphml')\n", + "\n", + "# Construct directed-transitive-closure directed graph\n", + "dtc_dG = nx.transitive_closure(dG)\n", + "\n", + "# Export directed-transitive-closure directed graph\n", + "nx.write_graphml(dtc_dG, f'../datasets/{casegraphs}-dtc-dgraph.graphml')\n", + "\n", + "# Convert to directed-transitive-closure undirected graph\n", + "dtc_G = dtc_dG.to_undirected()\n", + "\n", + "# Export directed-transitive-closure graph\n", + "nx.write_graphml(dtc_G, f'../datasets/{casegraphs}-dtc-graph.graphml')\n", + "\n", + "# Define directed-transitive-closure interaction count dictionary\n", + "dtc_count = {}\n", + "\n", + "# Count neighbours for each user\n", + "for node in dtc_G.nodes(data=True):\n", + " node_id, attributes = node\n", + " user = attributes['user']\n", + "\n", + " count = len(set(dtc_G.neighbors(node_id)) - set(user_nodes[user]))\n", + "\n", + " if user in dtc_count:\n", + " dtc_count[user] += count\n", + " else:\n", + " dtc_count[user] = count" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Construct and export user interaction count dataset." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "user_c = df_data.groupby('user')['id'].count().to_dict()\n", + "user_s = df_data.groupby('user')['s'].mean().to_dict()\n", + "user_stds = df_data.groupby('user')['s'].std().to_dict()\n", + "\n", + "df_users = pd.DataFrame({\n", + " 'nn-count': pd.Series(nn_count), \n", + " 'dtc-count': pd.Series(dtc_count), \n", + " 'tc-count': pd.Series(tc_count),\n", + " 'mean-sentiment': pd.Series(user_s),\n", + " 'std-sentiment': pd.Series(user_stds),\n", + " 'post-count': pd.Series(user_c)\n", + "})\n", + "\n", + "export_dataset = f'../datasets/{case}-users.csv'\n", + "df_users.to_csv(export_dataset, index=False)\n", + "\n", + "df_users.info()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df_users.describe()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.14.6" + }, + "vscode": { + "interpreter": { + "hash": "8c75c0fdd1a718867cdcb84b32adcfdbeaad00b3a4e00a59385211aeed084d4c" + } + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}