{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import math\n", "import pickle as pkl\n", "\n", "from torch_geometric.data import Data\n", "import torch\n", "import tqdm" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_5327/3476166533.py:2: DtypeWarning: Columns (2,3,4,6,7,8,9,14,17,18,21) have mixed types. Specify dtype option on import or set low_memory=False.\n", " df = pd.read_csv(\"reddit.csv\")\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "(1070077, 22)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_5327/3476166533.py:18: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_graph.rename(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "(256388, 8)\n" ] } ], "source": [ "## Preprocessing\n", "df = pd.read_csv(\"reddit.csv\")\n", "print(df.shape)\n", "\n", "# select columns\n", "df_graph = df[\n", " [\n", " \"subreddit_id\",\n", " \"subreddit\",\n", " \"name\",\n", " \"body\",\n", " \"score\",\n", " \"author\",\n", " \"author_flair_text\",\n", " \"distinguished\",\n", " ]\n", "]\n", "df_graph.rename(\n", " columns={\n", " \"name\": \"post_id\",\n", " \"body\": \"post\",\n", " \"author\": \"user\",\n", " \"author_flair_text\": \"user_flair\",\n", " },\n", " inplace=True,\n", " errors=\"raise\",\n", ")\n", "\n", "# drop na, duplicates and deleted post\n", "df_graph = df_graph.drop_duplicates()\n", "df_graph = df_graph[df_graph[\"post\"] != \"[deleted]\"]\n", "df_graph = df_graph.dropna(subset=[\"post_id\"])\n", "df_graph = df_graph.dropna(subset=[\"user_flair\"])\n", "df_graph = df_graph.dropna(subset=[\"subreddit\"])\n", "df_graph = df_graph.dropna(subset=[\"post\"])\n", "print(df_graph.shape)\n", "\n", "df_graph[\"distinguished\"] = df_graph[\"distinguished\"].apply(\n", " lambda x: \"ordinary\" if pd.isna(x) else \"distinguished\"\n", ")\n", "df_graph[\"user_flair\"] = df_graph[\"user_flair\"].apply(lambda x: \"\" if pd.isna(x) else x)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
subreddit_idsubredditpost_idpostscoreuseruser_flairdistinguished
3t5_2qhoncomicbookst1_cqug9dkIt's not contradictory. Snyder's rendition of ...1.0eskimo_brosLuke Cageordinary
\n", "
" ], "text/plain": [ " subreddit_id subreddit post_id \\\n", "3 t5_2qhon comicbooks t1_cqug9dk \n", "\n", " post score user \\\n", "3 It's not contradictory. Snyder's rendition of ... 1.0 eskimo_bros \n", "\n", " user_flair distinguished \n", "3 Luke Cage ordinary " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = df_graph\n", "df.head(1)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "256388it [00:15, 17023.98it/s]\n" ] } ], "source": [ "text_nodes = []\n", "node_labels = []\n", "sub_id2idx = {}\n", "sub_nodes = []\n", "user_id2idx = {} \n", "user_nodes = []\n", "count = 0\n", "text_edges = []\n", "for _, row in tqdm.tqdm(df.iterrows()):\n", " sub_id = str(row[\"subreddit\"])\n", " user_id = str(row['user'])\n", "\n", " if sub_id not in sub_id2idx:\n", " sub_id2idx[sub_id] = count\n", " sub_nodes.append(count)\n", " count += 1\n", " else:\n", " sub_nodes.append(sub_id2idx[sub_id])\n", " text_nodes.append(f\"subreddit {sub_id}\")\n", " node_labels.append(-1)\n", "\n", " if user_id not in user_id2idx:\n", " user_id2idx[user_id] = count\n", " user_nodes.append(count)\n", " count += 1\n", " else:\n", " user_nodes.append(user_id2idx[user_id])\n", " text_nodes.append(f\"user {user_id} has flair {row['user_flair']}\")\n", " node_labels.append(row['distinguished'])\n", " text_edges.append(str(row['post']))\n", " \n", "\n", "## Save it as torch data\n", "graph = Data(\n", " text_nodes=text_nodes,\n", " text_edges=text_edges,\n", " node_labels=node_labels,\n", " edge_index=torch.tensor([user_nodes, sub_nodes], dtype=torch.long),\n", ")\n", "\n", "with open(\"../processed/reddit.pkl\", \"wb\") as file:\n", " pkl.dump(graph, file)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }