{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-24T17:15:59.331167Z","iopub.execute_input":"2023-04-24T17:15:59.332245Z","iopub.status.idle":"2023-04-24T17:15:59.381787Z","shell.execute_reply.started":"2023-04-24T17:15:59.332201Z","shell.execute_reply":"2023-04-24T17:15:59.380654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_terms=pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv', sep='\\t')\nprint('Total proteins:',len(train_terms.EntryID.unique()))\nprint('Total terms used:',len(train_terms.term.unique()))\nprint('Biological process terms used:',len(train_terms[train_terms.aspect=='BPO'].term.unique()))\nprint('Molecular function terms used:',len(train_terms[train_terms.aspect=='MFO'].term.unique()))\nprint('Cellular component terms used:',len(train_terms[train_terms.aspect=='CCO'].term.unique()))\ntrain_terms.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:01.51761Z","iopub.execute_input":"2023-04-24T17:16:01.518749Z","iopub.status.idle":"2023-04-24T17:16:07.602687Z","shell.execute_reply.started":"2023-04-24T17:16:01.518701Z","shell.execute_reply":"2023-04-24T17:16:07.601618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create a simple function to calculate knn","metadata":{}},{"cell_type":"code","source":"!pip install pykeops==1.5","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:12.993113Z","iopub.execute_input":"2023-04-24T17:16:12.993728Z","iopub.status.idle":"2023-04-24T17:16:26.179342Z","shell.execute_reply.started":"2023-04-24T17:16:12.993688Z","shell.execute_reply":"2023-04-24T17:16:26.178079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pykeops.numpy import LazyTensor\n\ndef knn_keops(x, y, k=5):\n    x_i = LazyTensor(x[:, None, :])\n    y_j = LazyTensor(y[None, :, :])\n\n    pairwise_distance_ij = ((x_i - y_j) ** 2).sum(-1)\n\n    idx = pairwise_distance_ij.argKmin(K=k, axis=1)  # (N, K)\n\n    return idx","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:26.183534Z","iopub.execute_input":"2023-04-24T17:16:26.18387Z","iopub.status.idle":"2023-04-24T17:16:26.264844Z","shell.execute_reply.started":"2023-04-24T17:16:26.183836Z","shell.execute_reply":"2023-04-24T17:16:26.263878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we use protein embedding calculated by T5 protein language model from the Rost Lab (https://www.kaggle.com/datasets/sergeifironov/t5embeds)","metadata":{}},{"cell_type":"code","source":"train_ids=np.load('/kaggle/input/t5embeds/train_ids.npy')\ntrain_emb=np.load('/kaggle/input/t5embeds/train_embeds.npy')\ntest_ids=np.load('/kaggle/input/t5embeds/test_ids.npy')\ntest_emb=np.load('/kaggle/input/t5embeds/test_embeds.npy')\n","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:26.266437Z","iopub.execute_input":"2023-04-24T17:16:26.26685Z","iopub.status.idle":"2023-04-24T17:16:44.954625Z","shell.execute_reply.started":"2023-04-24T17:16:26.266813Z","shell.execute_reply":"2023-04-24T17:16:44.953524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train set size:', train_ids.shape[0])\nprint('Test set size:', test_ids.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:44.957415Z","iopub.execute_input":"2023-04-24T17:16:44.957822Z","iopub.status.idle":"2023-04-24T17:16:44.966169Z","shell.execute_reply.started":"2023-04-24T17:16:44.957766Z","shell.execute_reply":"2023-04-24T17:16:44.964845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k=16","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:44.968003Z","iopub.execute_input":"2023-04-24T17:16:44.968694Z","iopub.status.idle":"2023-04-24T17:16:44.974279Z","shell.execute_reply.started":"2023-04-24T17:16:44.968589Z","shell.execute_reply":"2023-04-24T17:16:44.972835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Calculate nearest neighbours from train set for each test sample","metadata":{}},{"cell_type":"code","source":"kmins=knn_keops(test_emb,train_emb, k=k)\nkmins.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:16:44.976035Z","iopub.execute_input":"2023-04-24T17:16:44.976565Z","iopub.status.idle":"2023-04-24T17:18:23.9143Z","shell.execute_reply.started":"2023-04-24T17:16:44.976528Z","shell.execute_reply":"2023-04-24T17:18:23.913144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arrayofgos=train_terms.groupby('EntryID').term.apply(lambda x: np.array(x)).loc[train_ids].values\ntest_df=pd.DataFrame(index=test_ids,data=arrayofgos[kmins])\ntest_df=test_df.apply(lambda x: np.concatenate(x), axis=1).explode().reset_index()\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:24:37.962567Z","iopub.execute_input":"2023-04-24T17:24:37.963351Z","iopub.status.idle":"2023-04-24T17:24:48.072309Z","shell.execute_reply.started":"2023-04-24T17:24:37.963311Z","shell.execute_reply":"2023-04-24T17:24:48.071086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[1]=1/k\ntest_df=test_df.groupby(['index', 0]).sum().round(3).reset_index()\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:24:48.075226Z","iopub.execute_input":"2023-04-24T17:24:48.076158Z","iopub.status.idle":"2023-04-24T17:25:15.319833Z","shell.execute_reply.started":"2023-04-24T17:24:48.076122Z","shell.execute_reply":"2023-04-24T17:25:15.318798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv(\"submission.tsv\",header=False, index=False, sep=\"\\t\")","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:25:24.600332Z","iopub.execute_input":"2023-04-24T17:25:24.60071Z","iopub.status.idle":"2023-04-24T17:25:47.400891Z","shell.execute_reply.started":"2023-04-24T17:25:24.600676Z","shell.execute_reply":"2023-04-24T17:25:47.399286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('submission.tsv','r') as f:\n    for i in range(10):\n        print(f.readline().strip())","metadata":{"execution":{"iopub.status.busy":"2023-04-24T17:26:40.699241Z","iopub.execute_input":"2023-04-24T17:26:40.699662Z","iopub.status.idle":"2023-04-24T17:26:40.707406Z","shell.execute_reply.started":"2023-04-24T17:26:40.699624Z","shell.execute_reply":"2023-04-24T17:26:40.706007Z"},"trusted":true},"execution_count":null,"outputs":[]}]}