{"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.10"},"papermill":{"default_parameters":{},"duration":51.862954,"end_time":"2023-05-13T06:43:03.419192","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-05-13T06:42:11.556238","version":"2.4.0"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":75254,"databundleVersionId":8264048,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-05-13T06:42:19.037609Z","iopub.status.busy":"2023-05-13T06:42:19.037200Z","iopub.status.idle":"2023-05-13T06:42:19.048825Z","shell.execute_reply":"2023-05-13T06:42:19.047845Z"},"papermill":{"duration":0.017097,"end_time":"2023-05-13T06:42:19.050636","exception":false,"start_time":"2023-05-13T06:42:19.033539","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nif torch.cuda.is_available():\n# GPU enabled\n    !pip install  dgl -f https://data.dgl.ai/wheels/cu113/repo.html\n    !pip install  dglgo -f https://data.dgl.ai/wheels-test/repo.html\nelse:\n# just use CPU\n    !pip install  dgl -f https://data.dgl.ai/wheels/repo.html\n    !pip install  dglgo -f https://data.dgl.ai/wheels-test/repo.html","metadata":{"execution":{"iopub.execute_input":"2023-05-13T06:42:19.056786Z","iopub.status.busy":"2023-05-13T06:42:19.056098Z","iopub.status.idle":"2023-05-13T06:42:51.531242Z","shell.execute_reply":"2023-05-13T06:42:51.530237Z"},"papermill":{"duration":32.479395,"end_time":"2023-05-13T06:42:51.533138","exception":false,"start_time":"2023-05-13T06:42:19.053743","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pickle as pkl\nimport numpy as np\n\ndef read_txt(file):\n    res_list = list()\n    with open(file, \"r\") as f:\n        line_list = f.readlines()\n    for line in line_list:\n        res_list.append(list(map(int, line.strip().split(' '))))      \n    return res_list\n\nbase_path = \"/kaggle/input/cs3319-01-project-2-recommendation-2024-spring\"\n\ncite_file = \"paper_file_ann.txt\"\ntrain_ref_file = \"bipartite_train_ann.txt\"\ntest_ref_file = \"bipartite_test_ann.txt\"\ncoauthor_file = \"author_file_ann.txt\"\nfeature_file = \"feature.pkl\"\n\ncitation = read_txt(os.path.join(base_path, cite_file))\nexisting_refs = read_txt(os.path.join(base_path, train_ref_file))\nrefs_to_pred = read_txt(os.path.join(base_path, test_ref_file))\ncoauthor = read_txt(os.path.join(base_path, coauthor_file))\n\nfeature_file = os.path.join(base_path, feature_file)\nwith open(feature_file, 'rb') as f:\n      paper_feature = pkl.load(f)\n        \n\n\nprint(\"Number of citation edges: {}\\n\\\nNumber of existing references: {}\\n\\\nNumber of author-paper pairs to predict: {}\\n\\\nNumber of coauthor edges: {}\\n\\\nShape of paper features: {}\"\n.format(len(citation), len(existing_refs), len(refs_to_pred), len(coauthor), paper_feature.shape))","metadata":{"execution":{"iopub.execute_input":"2023-05-13T06:42:51.544450Z","iopub.status.busy":"2023-05-13T06:42:51.543728Z","iopub.status.idle":"2023-05-13T06:43:00.594533Z","shell.execute_reply":"2023-05-13T06:43:00.593271Z"},"papermill":{"duration":9.058563,"end_time":"2023-05-13T06:43:00.596639","exception":false,"start_time":"2023-05-13T06:42:51.538076","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ncite_edges = pd.DataFrame(citation, columns=['source', 'target'])\ncite_edges = cite_edges.set_index(\n    \"c-\" + cite_edges.index.astype(str)\n)\n\nref_edges = pd.DataFrame(existing_refs, columns=['source', 'target'])\nref_edges = ref_edges.set_index(\n    \"r-\" + ref_edges.index.astype(str)\n)\n\ncoauthor_edges = pd.DataFrame(coauthor, columns=['source', 'target'])\ncoauthor_edges = coauthor_edges.set_index(\n    \"a-\" + coauthor_edges.index.astype(str)\n)\n\ncite_edges.head()","metadata":{"execution":{"iopub.execute_input":"2023-05-13T06:43:00.607379Z","iopub.status.busy":"2023-05-13T06:43:00.607060Z","iopub.status.idle":"2023-05-13T06:43:01.828248Z","shell.execute_reply":"2023-05-13T06:43:01.827441Z"},"papermill":{"duration":1.228342,"end_time":"2023-05-13T06:43:01.829853","exception":false,"start_time":"2023-05-13T06:43:00.601511","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"node_tmp = pd.concat([cite_edges.loc[:, 'source'], cite_edges.loc[:, 'target'], ref_edges.loc[:, 'target']])\nnode_papers = pd.DataFrame(index=pd.unique(node_tmp))\n\nnode_tmp = pd.concat([ref_edges['source'], coauthor_edges['source'], coauthor_edges['target']])\nnode_authors = pd.DataFrame(index=pd.unique(node_tmp))\n\nprint(\"Number of paper nodes: {}, number of author nodes: {}\".format(len(node_papers), len(node_authors)))","metadata":{"execution":{"iopub.execute_input":"2023-05-13T06:43:01.840934Z","iopub.status.busy":"2023-05-13T06:43:01.840473Z","iopub.status.idle":"2023-05-13T06:43:01.890526Z","shell.execute_reply":"2023-05-13T06:43:01.888843Z"},"papermill":{"duration":0.057419,"end_time":"2023-05-13T06:43:01.892324","exception":false,"start_time":"2023-05-13T06:43:01.834905","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}