{"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":"\nimport os\nimport torch\nimport numpy as np  \nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport scipy \nimport sklearn\nfrom scipy import stats\nimport statsmodels.api as sm\nimport torch.nn as nn\nimport torch.nn.functional as F\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-07T14:36:25.304103Z","iopub.execute_input":"2023-08-07T14:36:25.304925Z","iopub.status.idle":"2023-08-07T14:36:25.316647Z","shell.execute_reply.started":"2023-08-07T14:36:25.304873Z","shell.execute_reply":"2023-08-07T14:36:25.315189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**GETTING ALL TRAIN AND TEST DATA**","metadata":{}},{"cell_type":"code","source":"###GETTING DATA FROM GIVEN DATA SET\n\ntrain_taxonomy = pd.read_csv(\"/kaggle/input/cafa-5-protein-function-prediction/Train/train_taxonomy.tsv\", sep='\\t')\ntrain_terms = pd.read_csv(\"/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv\",sep=\"\\t\")\nprint(train_taxonomy.head())\nprint(train_terms.head())\nprint(train_taxonomy.shape)\nprint(train_terms.shape)\n\nprint(train_terms['term'].nunique())","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:25.335351Z","iopub.execute_input":"2023-08-07T14:36:25.335825Z","iopub.status.idle":"2023-08-07T14:36:29.362442Z","shell.execute_reply.started":"2023-08-07T14:36:25.335791Z","shell.execute_reply":"2023-08-07T14:36:29.361274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****IMPORTING t5embeds by Sergei Fironov ****","metadata":{}},{"cell_type":"code","source":"train_protein_ids = np.load('/kaggle/input/t5embeds/train_ids.npy')\nprint(train_protein_ids.shape)\nprint(train_protein_ids[:5])","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:29.364992Z","iopub.execute_input":"2023-08-07T14:36:29.365393Z","iopub.status.idle":"2023-08-07T14:36:29.418009Z","shell.execute_reply.started":"2023-08-07T14:36:29.365357Z","shell.execute_reply":"2023-08-07T14:36:29.417088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_embeddings=np.load('/kaggle/input/t5embeds/train_embeds.npy')\ntrain= pd.DataFrame(train_embeddings, columns=[\"Column_\" + str(i) for i in range(1, train_embeddings.shape[1] + 1)])\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:29.419344Z","iopub.execute_input":"2023-08-07T14:36:29.419781Z","iopub.status.idle":"2023-08-07T14:36:39.283116Z","shell.execute_reply.started":"2023-08-07T14:36:29.419747Z","shell.execute_reply":"2023-08-07T14:36:39.282179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_embeddings=np.load('/kaggle/input/t5embeds/test_embeds.npy')\ntest = pd.DataFrame(test_embeddings,columns=[\"column_\" + str(i) for i in range(1,train_embeddings.shape[1] + 1)])\ntest.head()\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:39.284484Z","iopub.execute_input":"2023-08-07T14:36:39.286501Z","iopub.status.idle":"2023-08-07T14:36:49.057693Z","shell.execute_reply.started":"2023-08-07T14:36:39.286475Z","shell.execute_reply":"2023-08-07T14:36:49.055746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**PREPARING THE DATASET**","metadata":{}},{"cell_type":"markdown","source":"DISTINCT **TRAIN (*GO TERM ID*)** IN THE SET IS 31,466 , HENCE WE ARE CHOOSING JUST 2000 VALUES FOR TRAINING \n\nTHESE 2000 WILL BE THE MOST FREQUENT ONCE TO GET MORE ACCURACY","metadata":{}},{"cell_type":"code","source":"plot = train_terms['term'].value_counts().iloc[:50]\n\nfig,axis=plt.subplots(1,1,figsize=(20,20))\nbp=sns.barplot(x=plot.index,y=plot.values)\nfor index, value in enumerate(plot.values):\n    bp.text(index, value, str(value), ha='center', va='bottom',rotation=90, fontsize=12)\nbp.set_xticklabels(bp.get_xticklabels(),rotation=90,size=12)\nbp.set_xlabel(\"go term IDS\",size=25)\nbp.set_ylabel(\"count\",size=25)\nbp.set_title(\"top 50 GO term ID\",size=20)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:49.060929Z","iopub.execute_input":"2023-08-07T14:36:49.061684Z","iopub.status.idle":"2023-08-07T14:36:51.188602Z","shell.execute_reply.started":"2023-08-07T14:36:49.061656Z","shell.execute_reply":"2023-08-07T14:36:51.187643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AS WE CAN SEE FROM THE GRAPH, TOP 50 OF (ID) HAS A VERY HUGE COUNT AND HENCE TAKING A SMALL SMAPLE OF 1500 WILL HAVE A GOOD ACCURACY FOR PREDICTIONS","metadata":{}},{"cell_type":"code","source":"### SELECTING TOP 2000 ID'S\n\nno_of_label = 2000\nlabel = train_terms['term'].value_counts().index[:no_of_label].tolist()\nlen(label)\n\ndef get_label_train_terms(df):\n    labels=df['term'].value_counts().index[:no_of_label].tolist()\n    train_terms_updated=df.loc[df['term'].isin(labels)]\n    return labels,train_terms_updated\n\nlabels_count,train_terms_updated=get_label_train_terms(train_terms)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:51.189619Z","iopub.execute_input":"2023-08-07T14:36:51.189932Z","iopub.status.idle":"2023-08-07T14:36:53.522702Z","shell.execute_reply.started":"2023-08-07T14:36:51.189901Z","shell.execute_reply":"2023-08-07T14:36:53.521671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###NOW FETCHING ALL DATA FOR THIS 2000 AND STORING IT FOR FURTHER MODELING\n\ntrain_updated = train_terms.loc[train_terms['term'].isin(label)]\ntrain_updated.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:53.524360Z","iopub.execute_input":"2023-08-07T14:36:53.524723Z","iopub.status.idle":"2023-08-07T14:36:54.137483Z","shell.execute_reply.started":"2023-08-07T14:36:53.524688Z","shell.execute_reply":"2023-08-07T14:36:54.136367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_df = train_updated['aspect'].value_counts()\npalette_color = sns.color_palette('dark')\nplt.pie(pie_df.values, labels=np.array(pie_df.index), colors=palette_color, autopct='%.0f%%')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:54.139078Z","iopub.execute_input":"2023-08-07T14:36:54.139455Z","iopub.status.idle":"2023-08-07T14:36:54.919702Z","shell.execute_reply.started":"2023-08-07T14:36:54.139423Z","shell.execute_reply":"2023-08-07T14:36:54.918710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"FROM ABOVE IT CAN BE CONCLUDED THAT MAJORITY OF ***(GO TERM ID)*** HAVE BPO***(BIOLOGICAL PROCESS ONTOLGY)***","metadata":{}},{"cell_type":"markdown","source":"NOW, LETS MARK ALL ***(GO TERM ID)*** PRESENT IN EACH PROTIEN\n\nSINCE WE LIMIT TO 2000 IT WILL SHOW ONLY TILL 2000 TERMS","metadata":{}},{"cell_type":"markdown","source":"The groupby function is used to efficiently group rows in the train_terms DataFrame by the unique GO terms (labels) present in the dataset. By doing this, we can easily extract the proteins related to each unique term without the need for explicit nested loops.\n\n","metadata":{}},{"cell_type":"code","source":"# Create an empty DataFrame to store the binary-encoded labels\ntrain_labels_df = pd.DataFrame(0, index=train_protein_ids, columns=label)\n\n# Group the train_terms DataFrame by 'term' and aggregate the 'EntryID' as a list\ngrouped_terms = train_terms.groupby('term')['EntryID'].apply(list)\n\n# Loop through each label (GO term) and mark the proteins related to the label as 1\nfor i in label:\n    if i in grouped_terms.index:\n        label_related_proteins = grouped_terms[i]\n        train_labels_df.loc[label_related_proteins, i] = 1\n\n# Convert train_labels_df to a numpy array (if needed)\ntrain_labels = train_labels_df.values\n\n# Convert train_labels numpy into pandas dataframe\nlabels_df = pd.DataFrame(data=train_labels, columns=label)\nprint(labels_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:36:54.921157Z","iopub.execute_input":"2023-08-07T14:36:54.922150Z","iopub.status.idle":"2023-08-07T14:37:03.427742Z","shell.execute_reply.started":"2023-08-07T14:36:54.922096Z","shell.execute_reply":"2023-08-07T14:37:03.426791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:03.429253Z","iopub.execute_input":"2023-08-07T14:37:03.429620Z","iopub.status.idle":"2023-08-07T14:37:03.448940Z","shell.execute_reply.started":"2023-08-07T14:37:03.429587Z","shell.execute_reply":"2023-08-07T14:37:03.447605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TRAINING THE DATA**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(train,labels_df,shuffle=True,test_size=0.5,random_state=50)\nprint(x_train.shape,y_train.shape,x_test.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:03.450506Z","iopub.execute_input":"2023-08-07T14:37:03.450867Z","iopub.status.idle":"2023-08-07T14:37:06.029765Z","shell.execute_reply.started":"2023-08-07T14:37:03.450835Z","shell.execute_reply":"2023-08-07T14:37:06.028155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\nif torch.cuda.is_available():\n    device = torch.device(\"cuda\")  # CUDA available, use GPU\nelse:\n    device = torch.device(\"cpu\")   # No CUDA, use CPU\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:06.031206Z","iopub.execute_input":"2023-08-07T14:37:06.031749Z","iopub.status.idle":"2023-08-07T14:37:06.107343Z","shell.execute_reply.started":"2023-08-07T14:37:06.031714Z","shell.execute_reply":"2023-08-07T14:37:06.104552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\n\nclass FluxData(Dataset):\n    def __init__(self, x_data, y_data):\n        self.X_data = x_data\n        self.y_data = y_data\n        \n    def __getitem__(self, index):\n        return self.X_data[index], self.y_data[index]\n        \n    def __len__(self):\n        return len(self.X_data)\n\nx_train = torch.from_numpy(x_train.values).float().to(device)\ny_train= torch.from_numpy(y_train.values).float().to(device)\nx_test = torch.from_numpy(x_test.values).float().to(device)\ny_test = torch.from_numpy(y_test.values).float().to(device)\ntrain_data = FluxData(x_train, y_train)\ntest_data = FluxData(x_test, y_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:06.108736Z","iopub.execute_input":"2023-08-07T14:37:06.109322Z","iopub.status.idle":"2023-08-07T14:37:11.073728Z","shell.execute_reply.started":"2023-08-07T14:37:06.109289Z","shell.execute_reply":"2023-08-07T14:37:11.072666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nclass CAFA5NNetBase(torch.nn.Module):\n    def training_steps(self, batch):\n        features, label = batch\n        out = self(features)\n        loss = F.binary_cross_entropy(out, label)\n        return loss\n    \n    def validation_step(self, batch):\n        features, label = batch\n        out = self(features)\n        loss = F.binary_cross_entropy(out, label).detach()\n        acc = aurocs(out, label)\n        return {'validation_loss': loss, 'validation_acc': acc}\n    \n    def validation_epoch_end(self, outputs):\n        batch_losses = [x['validation_loss'] for x in outputs]\n        epoch_loss = torch.stack(batch_losses).mean()\n        batch_accs = [x['validation_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()\n        return {'validation_loss': epoch_loss.item(), 'validation_acc': epoch_acc.item()}\n    \n    def epoch_end(self, epoch, result):\n        print(\"epoch[{}], train_loss: {:.4f}, validation_loss: {:.4f}, validation_acc: {:.4f}\".format(\n            epoch, result['train_loss'], result['validation_loss'], result['validation_acc']\n        ))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:11.079829Z","iopub.execute_input":"2023-08-07T14:37:11.080138Z","iopub.status.idle":"2023-08-07T14:37:11.089352Z","shell.execute_reply.started":"2023-08-07T14:37:11.080103Z","shell.execute_reply":"2023-08-07T14:37:11.088202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CAFA5NNet(CAFA5NNetBase):\n    def __init__(self, input_features, output_features):\n        super(CAFA5NNet, self).__init__()\n        \n        self.activation = nn.PReLU()\n        \n        self.bn1 = nn.BatchNorm1d(input_features)\n        self.fc1 = nn.Linear(input_features, 800)\n        self.ln1 = nn.LayerNorm(800, elementwise_affine=True)\n        \n        self.bn2 = nn.BatchNorm1d(800)\n        self.fc2 = nn.Linear(800, 600)\n        self.ln2 = nn.LayerNorm(600, elementwise_affine=True)\n        \n        self.bn3 = nn.BatchNorm1d(600)\n        self.fc3 = nn.Linear(600, 400)\n        self.ln3 = nn.LayerNorm(400, elementwise_affine=True)\n        \n        self.bn4 = nn.BatchNorm1d(1200)\n        self.fc4 = nn.Linear(1200, output_features)\n        self.ln4 = nn.LayerNorm(output_features, elementwise_affine=True)\n        \n        self.sigm = nn.Sigmoid()\n\n    def forward(self, inputs):\n        fc1_out = self.bn1(inputs)\n        fc1_out = self.ln1(self.fc1(inputs))\n        fc1_out = self.activation(fc1_out)\n        \n        x = self.bn2(fc1_out)\n        \n        x = self.ln2(self.fc2(x))\n        x = self.activation(x)\n        \n        x = self.bn3(x)\n        \n        x = self.ln3(self.fc3(x))\n        x = self.activation(x)\n        \n        x = torch.cat([x, fc1_out], axis=-1)\n        \n        x = self.bn4(x)\n        \n        x = self.ln4(self.fc4(x))\n        out = self.sigm(x)\n        return out\n\nmodel = CAFA5NNet(x_train.shape[1], y_train.shape[1])\nmodel.to(device)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:11.091038Z","iopub.execute_input":"2023-08-07T14:37:11.091420Z","iopub.status.idle":"2023-08-07T14:37:11.180412Z","shell.execute_reply.started":"2023-08-07T14:37:11.091386Z","shell.execute_reply":"2023-08-07T14:37:11.179379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:11.181902Z","iopub.execute_input":"2023-08-07T14:37:11.182259Z","iopub.status.idle":"2023-08-07T14:37:11.187206Z","shell.execute_reply.started":"2023-08-07T14:37:11.182225Z","shell.execute_reply":"2023-08-07T14:37:11.186166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install Sophia\n!git clone https://github.com/kyegomez/Sophia.git\n! python Sophia/setup.py install\n!rm Sophia/Sophia/__init__.py\nfrom Sophia.Sophia.Sophia import SophiaG\nBATCH_SIZE = 128\nEPOCHS = 39\nLEARNING_RATE =0.001\nMOMENTUM = 0.9\nOPT_FUNC = SophiaG\n","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:11.188764Z","iopub.execute_input":"2023-08-07T14:37:11.189123Z","iopub.status.idle":"2023-08-07T14:37:27.053901Z","shell.execute_reply.started":"2023-08-07T14:37:11.189091Z","shell.execute_reply":"2023-08-07T14:37:27.052633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dataloader(dataset_type,batch,shuffle):\n    if shuffle:\n        return DataLoader(dataset=dataset_type,batch_size=batch,shuffle=True)\n    else:\n        return DataLoader(dataset=dataset_type,batch_size=batch,shuffle=False)\n\ntrain_dl = get_dataloader(train_data,BATCH_SIZE,True)\ntest_dl = get_dataloader(test_data,BATCH_SIZE,True)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:27.056594Z","iopub.execute_input":"2023-08-07T14:37:27.057333Z","iopub.status.idle":"2023-08-07T14:37:27.064567Z","shell.execute_reply.started":"2023-08-07T14:37:27.057294Z","shell.execute_reply":"2023-08-07T14:37:27.063601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tqdm.notebook as tqdm\nimport torch\nfrom torchmetrics import AUROC\ndef aurocs(outputs, labels):\n    aurocs = AUROC(task=\"binary\")\n    return aurocs(outputs, labels)\n\n  \n@torch.no_grad()\ndef evaluate(model, val_loader):\n    model.eval()\n    outputs = [model.validation_step(batch) for batch in val_loader]\n    return model.validation_epoch_end(outputs)\n\n  \ndef fit(epochs, lr, model, train_loader, val_loader, opt_func = OPT_FUNC):\n    \n    history = []\n    optimizer = opt_func(model.parameters(),lr, betas=(0.965, 0.99), rho = 0.01, weight_decay=1e-1)\n\n    lr_sched = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=2, eta_min=0.001, last_epoch=-1)\n\n    for epoch in tqdm.tqdm(range(epochs)):\n        \n        model.train()\n        train_losses = []\n        for batch in train_loader:\n            loss = model.training_steps(batch)\n            train_losses.append(loss)\n            loss.backward()\n            optimizer.step()\n            lr_sched.step()\n            optimizer.zero_grad()\n            \n            \n        result = evaluate(model, val_loader)\n        result['train_loss'] = torch.stack(train_losses).mean().item()\n        model.epoch_end(epoch, result)\n        history.append(result)\n    \n    return history","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:27.066862Z","iopub.execute_input":"2023-08-07T14:37:27.067619Z","iopub.status.idle":"2023-08-07T14:37:36.969115Z","shell.execute_reply.started":"2023-08-07T14:37:27.067585Z","shell.execute_reply":"2023-08-07T14:37:36.968186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(EPOCHS,LEARNING_RATE,model,train_dl,test_dl,OPT_FUNC)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:37:36.970539Z","iopub.execute_input":"2023-08-07T14:37:36.970883Z","iopub.status.idle":"2023-08-07T14:41:01.816826Z","shell.execute_reply.started":"2023-08-07T14:37:36.970848Z","shell.execute_reply":"2023-08-07T14:41:01.815674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_accur (histoy):\n    accuracy = [x['validation_acc'] for x in history]\n    plt.plot(accuracy,'-x')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.title('Accuracy vs Epoch')\n\nplot_accur(history)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:01.821875Z","iopub.execute_input":"2023-08-07T14:41:01.822229Z","iopub.status.idle":"2023-08-07T14:41:02.203107Z","shell.execute_reply.started":"2023-08-07T14:41:01.822190Z","shell.execute_reply":"2023-08-07T14:41:02.202070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_losses(history):\n    \"\"\" Plot the losses in each epoch\"\"\"\n    train_losses = [x.get('train_loss') for x in history]\n    val_losses = [x['validation_loss'] for x in history]\n    plt.plot(train_losses, '-bx')\n    plt.plot(val_losses, '-rx')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend(['Training', 'Validation'])\n    plt.title('Loss vs. No. of epochs');\n\nplot_losses(history)\n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:02.204620Z","iopub.execute_input":"2023-08-07T14:41:02.205004Z","iopub.status.idle":"2023-08-07T14:41:02.544971Z","shell.execute_reply.started":"2023-08-07T14:41:02.204968Z","shell.execute_reply":"2023-08-07T14:41:02.544033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train,x_test,y_test,x_train,y_train,train_data,test_data,train_dl,test_dl,train_protein_ids\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:02.546279Z","iopub.execute_input":"2023-08-07T14:41:02.546720Z","iopub.status.idle":"2023-08-07T14:41:03.093862Z","shell.execute_reply.started":"2023-08-07T14:41:02.546686Z","shell.execute_reply":"2023-08-07T14:41:03.092768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:03.095705Z","iopub.execute_input":"2023-08-07T14:41:03.096153Z","iopub.status.idle":"2023-08-07T14:41:03.103647Z","shell.execute_reply.started":"2023-08-07T14:41:03.096104Z","shell.execute_reply":"2023-08-07T14:41:03.101603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CAFA5TestData(Dataset):\n    \n    def __init__(self, X_test_data):\n        self.X_test_data = X_test_data\n        \n    def __getitem__(self, index):\n        return self.X_test_data[index]\n        \n    def __len__ (self):\n        return len(self.X_test_data)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:03.105073Z","iopub.execute_input":"2023-08-07T14:41:03.105366Z","iopub.status.idle":"2023-08-07T14:41:03.112762Z","shell.execute_reply.started":"2023-08-07T14:41:03.105343Z","shell.execute_reply":"2023-08-07T14:41:03.111710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = CAFA5TestData(torch.from_numpy(test.values).float().to(device))\ntest_data_loader = DataLoader(dataset=test_data, batch_size=test.shape[0])\n\ndef eval_test_data(model,testing_data_dl):\n    model.eval()\n    with torch.no_grad():\n        for X_batch_test in testing_data_dl:\n            X_batch_test = X_batch_test.to(device)\n            predictions = model(X_batch_test)\n            prediction_target=predictions.detach().cpu().numpy()\n\n    return prediction_target\n\nprediction_target = eval_test_data(model,test_data_loader)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:03.114322Z","iopub.execute_input":"2023-08-07T14:41:03.115384Z","iopub.status.idle":"2023-08-07T14:41:05.356059Z","shell.execute_reply.started":"2023-08-07T14:41:03.115351Z","shell.execute_reply":"2023-08-07T14:41:05.355044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(prediction_target):\n#     test_protein_ids = np.load('/kaggle/input/4637427/test_ids_esm2_t36_3B_UR50D.npy')\n    test_protein_ids = np.load('/kaggle/input/t5embeds/test_ids.npy')\n#     test_protein_ids = np.load('/kaggle/input/t5embeds/test_ids.npy')\n    protein_list = []\n    for k in list(test_protein_ids):\n        protein_list += [k] * prediction_target.shape[1] \n    return protein_list\n\nprotein_list=make_predictions(prediction_target)      \n# del labels\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:05.357504Z","iopub.execute_input":"2023-08-07T14:41:05.357962Z","iopub.status.idle":"2023-08-07T14:41:09.837752Z","shell.execute_reply.started":"2023-08-07T14:41:05.357927Z","shell.execute_reply":"2023-08-07T14:41:09.836571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def submit(labels_count, protein_list, prediction_target):\n    labels_count = labels_count * prediction_target.shape[0]  # List of labels\n\n    with open(\"submission.tsv\", \"w\") as file:\n#         file.write(f\"Protein Id\\tGO Term Id\\tPrediction\\n\")\n        idx = 0\n        for row in prediction_target:\n            for element in row:\n                file.write(f\"{protein_list[idx]}\\t{labels_count[idx]}\\t{element}\\n\")\n                idx += 1\n                if idx %3000000 == 0:\n                    print(f'{idx} passed')\n                \nsubmit(labels_count, protein_list, prediction_target)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T14:41:09.839451Z","iopub.execute_input":"2023-08-07T14:41:09.839846Z","iopub.status.idle":"2023-08-07T14:59:20.602955Z","shell.execute_reply.started":"2023-08-07T14:41:09.839796Z","shell.execute_reply":"2023-08-07T14:59:20.601718Z"},"trusted":true},"execution_count":null,"outputs":[]}]}