{"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":"%%capture \n!pip install torchmetrics","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:12:09.986882Z","iopub.execute_input":"2023-05-28T22:12:09.987748Z","iopub.status.idle":"2023-05-28T22:12:22.205588Z","shell.execute_reply.started":"2023-05-28T22:12:09.987702Z","shell.execute_reply":"2023-05-28T22:12:22.204352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture \n!pip install lightning","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:12:22.207998Z","iopub.execute_input":"2023-05-28T22:12:22.208373Z","iopub.status.idle":"2023-05-28T22:12:37.2945Z","shell.execute_reply.started":"2023-05-28T22:12:22.208337Z","shell.execute_reply":"2023-05-28T22:12:37.293131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!pip install plotly-express","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:12:37.297317Z","iopub.execute_input":"2023-05-28T22:12:37.298023Z","iopub.status.idle":"2023-05-28T22:12:48.197757Z","shell.execute_reply.started":"2023-05-28T22:12:37.297984Z","shell.execute_reply":"2023-05-28T22:12:48.196574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## https://www.kaggle.com/code/alexandervc/baseline-multilabel-to-multitarget-binary#Load-train-features---precalculated-embeddings-for-the-proteins\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchmetrics import AUROC\nfrom pytorch_lightning import LightningModule, Trainer ,LightningDataModule\nfrom pytorch_lightning.callbacks.progress import TQDMProgressBar\nfrom pytorch_lightning.callbacks.early_stopping import EarlyStopping\nfrom lightning.pytorch.loggers import TensorBoardLogger\nfrom lightning.pytorch.profilers import SimpleProfiler\nimport lightning as L\n\n\nimport warnings\nwarnings.filterwarnings('ignore')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:12:48.200765Z","iopub.execute_input":"2023-05-28T22:12:48.201417Z","iopub.status.idle":"2023-05-28T22:13:07.100751Z","shell.execute_reply.started":"2023-05-28T22:12:48.201381Z","shell.execute_reply":"2023-05-28T22:13:07.099848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dataframe(path):\n    return pd.read_csv(path,sep='\\t')","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:07.102039Z","iopub.execute_input":"2023-05-28T22:13:07.10336Z","iopub.status.idle":"2023-05-28T22:13:07.108724Z","shell.execute_reply.started":"2023-05-28T22:13:07.103326Z","shell.execute_reply":"2023-05-28T22:13:07.107212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_terms = '/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv'\ntrain_taxonomy ='/kaggle/input/cafa-5-protein-function-prediction/Train/train_taxonomy.tsv'","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:07.1112Z","iopub.execute_input":"2023-05-28T22:13:07.111725Z","iopub.status.idle":"2023-05-28T22:13:07.131831Z","shell.execute_reply.started":"2023-05-28T22:13:07.111695Z","shell.execute_reply":"2023-05-28T22:13:07.130856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_dataframe(train_terms).head()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:07.134821Z","iopub.execute_input":"2023-05-28T22:13:07.135086Z","iopub.status.idle":"2023-05-28T22:13:10.316932Z","shell.execute_reply.started":"2023-05-28T22:13:07.135064Z","shell.execute_reply":"2023-05-28T22:13:10.315896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_dataframe(train_taxonomy).head()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:10.318525Z","iopub.execute_input":"2023-05-28T22:13:10.318948Z","iopub.status.idle":"2023-05-28T22:13:10.430947Z","shell.execute_reply.started":"2023-05-28T22:13:10.318893Z","shell.execute_reply":"2023-05-28T22:13:10.429995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.        \n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n        \n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n        else:\n            df[col] = df[col].astype('category')\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:10.432378Z","iopub.execute_input":"2023-05-28T22:13:10.43274Z","iopub.status.idle":"2023-05-28T22:13:10.446117Z","shell.execute_reply.started":"2023-05-28T22:13:10.432706Z","shell.execute_reply":"2023-05-28T22:13:10.444978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.histogram(reduce_mem_usage(get_dataframe(train_terms)), x=\"aspect\")","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:10.451168Z","iopub.execute_input":"2023-05-28T22:13:10.451769Z","iopub.status.idle":"2023-05-28T22:13:38.744913Z","shell.execute_reply.started":"2023-05-28T22:13:10.451729Z","shell.execute_reply":"2023-05-28T22:13:38.74377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_terms=reduce_mem_usage(get_dataframe(train_terms))","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:38.746597Z","iopub.execute_input":"2023-05-28T22:13:38.747057Z","iopub.status.idle":"2023-05-28T22:13:43.49676Z","shell.execute_reply.started":"2023-05-28T22:13:38.747Z","shell.execute_reply":"2023-05-28T22:13:43.495647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    num_of_labels = 1500\n    batch_size = 512\n    learning_rate = 0.0001\n    random_state =42\n    accelerator='cpu'\n    devices=1\n    precision=16\n    max_epochs=100\n    mode=\"max\"\n    patience=10\n\n\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:43.498479Z","iopub.execute_input":"2023-05-28T22:13:43.498839Z","iopub.status.idle":"2023-05-28T22:13:43.505791Z","shell.execute_reply.started":"2023-05-28T22:13:43.498805Z","shell.execute_reply":"2023-05-28T22:13:43.503522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Preprocessing():\n    \n    def get_train_dataset(self):\n        \n        train_protein_ids = np.load('/kaggle/input/t5embeds/train_ids.npy')\n        train_embeddings = np.load('/kaggle/input/t5embeds/train_embeds.npy')\n        column_num = train_embeddings.shape[1]\n        train = pd.DataFrame(train_embeddings, columns = [\"Column_\" + str(i) for i in range(1, column_num+1)])\n        return train,train_protein_ids\n\n    def get_label_train_terms(self,df):\n        labels=df['term'].value_counts().index[:config.num_of_labels].tolist()\n        train_terms_updated=df.loc[df['term'].isin(labels)]\n        return labels,train_terms_updated\n\n    def get_labels(self,train_protein_ids):\n        train_size = train_protein_ids.shape[0] \n        train_labels = np.zeros((train_size ,config.num_of_labels))\n        series_train_protein_ids = pd.Series(train_protein_ids)\n\n        for i in range(config.num_of_labels):\n            n_train_terms = train_terms_updated[train_terms_updated['term'] ==  labels_count[i]]\n            label_related_proteins = n_train_terms['EntryID'].unique()\n            train_labels[:,i] =  series_train_protein_ids.isin(label_related_proteins).astype(float)\n        return train_labels\n    \n    \n    def show_pit_aspects(self):\n        pie_df = train_terms_updated['aspect'].value_counts()\n        palette_color = sns.color_palette('pastel')\n        plt.pie(pie_df.values, labels=np.array(pie_df.index), colors=palette_color, autopct='%.0f%%')\n        plt.show()\n    \n\n    \npreprocess = Preprocessing()\n\ntrain,train_protein_ids = preprocess.get_train_dataset()\nlabels_count,train_terms_updated=preprocess.get_label_train_terms(train_terms)\ntrain_labels=preprocess.get_labels(train_protein_ids)\nlabels = pd.DataFrame(data = train_labels, columns = labels_count)\n\nprint(train.shape,train_protein_ids.shape,labels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:13:43.506902Z","iopub.execute_input":"2023-05-28T22:13:43.507678Z","iopub.status.idle":"2023-05-28T22:14:24.245978Z","shell.execute_reply.started":"2023-05-28T22:13:43.507653Z","shell.execute_reply":"2023-05-28T22:14:24.244992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess.show_pit_aspects()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:24.247357Z","iopub.execute_input":"2023-05-28T22:14:24.247968Z","iopub.status.idle":"2023-05-28T22:14:24.420014Z","shell.execute_reply.started":"2023-05-28T22:14:24.247935Z","shell.execute_reply":"2023-05-28T22:14:24.41882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CAFA5Data(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\n","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:24.425487Z","iopub.execute_input":"2023-05-28T22:14:24.428542Z","iopub.status.idle":"2023-05-28T22:14:24.438876Z","shell.execute_reply.started":"2023-05-28T22:14:24.428493Z","shell.execute_reply":"2023-05-28T22:14:24.437779Z"},"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-05-28T22:14:24.444691Z","iopub.execute_input":"2023-05-28T22:14:24.4474Z","iopub.status.idle":"2023-05-28T22:14:24.457822Z","shell.execute_reply.started":"2023-05-28T22:14:24.447351Z","shell.execute_reply":"2023-05-28T22:14:24.456786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PrepareData(LightningDataModule):\n    \n    def __init__(self,train,labels):\n        self.train = train\n        self.labels = labels\n\n    \n    def setup(self, stage=None):\n        \n        if stage == \"fit\":\n            \n            self.X_train,self.X_val,self.y_train,self.y_val=train_test_split(self.train, self.labels,shuffle=True,random_state=config.random_state)\n            self.X_data = torch.tensor(self.X_train.values,dtype=torch.float32)\n            self.y_data = torch.tensor(self.y_train.values,dtype=torch.float32)\n            self.X_val = torch.tensor(self.X_val.values,dtype=torch.float32)\n            self.y_val = torch.tensor(self.y_val.values,dtype=torch.float32)\n            \n            self.train_data = CAFA5Data(self.X_data,self.y_data)\n            self.val_data = CAFA5Data(self.X_val,self.y_val)\n    \n        if stage == \"test\":\n            self.test_embeddings = np.load('/kaggle/input/t5embeds/test_embeds.npy')\n            self.column_num = self.test_embeddings.shape[1]\n            self.X_test = pd.DataFrame(self.test_embeddings, columns = [\"Column_\" + str(i) for i in range(1, self.column_num+1)])\n            self.X_data_test = torch.tensor(self.X_test.values,dtype=torch.float32)\n            self.test_data = CAFA5TestData(self.X_data_test)\n                \n\n            \n    def get_shape(self):\n        return self.X_train.shape[1],self.y_train.shape[1]\n\n    def train_dataloader(self):\n        return DataLoader(self.train_data, batch_size=config.batch_size,persistent_workers=True,pin_memory=True,num_workers=2)\n\n    def val_dataloader(self):\n        return DataLoader(self.val_data, batch_size=config.batch_size,persistent_workers=True,pin_memory=True,num_workers=2)\n\n    def test_dataloader(self):\n        return DataLoader(self.test_data, batch_size=config.batch_size,persistent_workers=True,pin_memory=True,num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:24.463273Z","iopub.execute_input":"2023-05-28T22:14:24.464182Z","iopub.status.idle":"2023-05-28T22:14:24.490312Z","shell.execute_reply.started":"2023-05-28T22:14:24.464137Z","shell.execute_reply":"2023-05-28T22:14:24.488903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CAFA5Light(LightningModule):\n    \n    def __init__(self,input_features,output_features,learning_rate):\n        super().__init__()\n        self.input_features = input_features\n        self.output_features = output_features\n        self.learning_rate = learning_rate\n        \n        \n        self.network = torch.nn.Sequential(\n        torch.nn.Linear(self.input_features,512),\n        torch.nn.ReLU(),\n        torch.nn.Linear(512,512),    \n        torch.nn.ReLU(),\n        torch.nn.Linear(512,self.output_features),\n        torch.nn.Softmax()\n        )\n        \n        \n    def forward(self,inputs):\n        return self.network(inputs)\n    \n    \n    def accuracy(self,outputs, labels):\n        auroc = AUROC(task=\"binary\")\n        return auroc(outputs, labels)\n            \n    def training_step(self, batch, batch_idx):\n        x, y = batch\n        logits = self(x)\n        loss = F.cross_entropy(logits, y)\n        tensorboard_logs = {\"Traininig_loss\": loss}\n        return {\"loss\": loss, \"log\": tensorboard_logs}\n        #return loss\n\n    def validation_step(self, batch, batch_idx):\n        x, y = batch\n        logits = self(x)\n        loss = F.cross_entropy(logits, y)\n        self.accuracy(x, y)\n        self.log(\"Validation_Loss\", loss, prog_bar=True)\n        #self.log(\"Validation_Acc\", self.accuracy, prog_bar=True)\n        return {\"Validation_loss\": loss}\n        #return loss\n    \n\n    def test_step(self, batch, batch_idx):\n        return self.validation_step(batch, batch_idx)\n    \n   \n\n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate)\n        return optimizer","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:24.491778Z","iopub.execute_input":"2023-05-28T22:14:24.492464Z","iopub.status.idle":"2023-05-28T22:14:24.506694Z","shell.execute_reply.started":"2023-05-28T22:14:24.492432Z","shell.execute_reply":"2023-05-28T22:14:24.505956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_module  = PrepareData(train,labels)\ndata_module.setup('fit')\nmodel = CAFA5Light(*data_module.get_shape(),config.learning_rate)\nprint(model)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:24.508141Z","iopub.execute_input":"2023-05-28T22:14:24.508728Z","iopub.status.idle":"2023-05-28T22:14:26.6503Z","shell.execute_reply.started":"2023-05-28T22:14:24.508692Z","shell.execute_reply":"2023-05-28T22:14:26.646903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor=\"Validation_Loss\", mode=config.mode, patience=config.patience)\nlogger = TensorBoardLogger(\"/kaggle/working/lightning_logs\", name=\"CAFA5Logging\")\nprofiler = SimpleProfiler()\ntrainer = Trainer(accelerator=config.accelerator, devices=config.devices,max_epochs=config.max_epochs,callbacks=[TQDMProgressBar(refresh_rate=10)],logger=logger)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:26.651661Z","iopub.execute_input":"2023-05-28T22:14:26.652187Z","iopub.status.idle":"2023-05-28T22:14:27.409897Z","shell.execute_reply.started":"2023-05-28T22:14:26.652156Z","shell.execute_reply":"2023-05-28T22:14:27.409055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.fit(model,data_module.train_dataloader(),data_module.val_dataloader()) ","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:14:27.413877Z","iopub.execute_input":"2023-05-28T22:14:27.415978Z","iopub.status.idle":"2023-05-28T22:18:16.296327Z","shell.execute_reply.started":"2023-05-28T22:14:27.415941Z","shell.execute_reply":"2023-05-28T22:18:16.295248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_module.setup('test')\npredictions=trainer.predict(model,data_module.test_dataloader())\npredictions = torch.concat(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:18:16.298153Z","iopub.execute_input":"2023-05-28T22:18:16.298525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class predict:\n    \n    def make_predictions(self,prediction_target):\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        test_protein_ids.close()\n    \n\npred = predict()\nprotein_list=pred.make_predictions(predictions)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class submitt_predictions:\n    \n    def submit(self,protein_list,predictions):\n        df_submission = pd.DataFrame(columns = ['Protein Id', 'GO Term Id','Prediction'])\n        df_submission['Protein Id'] = protein_list\n        df_submission['GO Term Id'] = labels_count * predictions.shape[0]\n        df_submission['Prediction'] = predictions.ravel()\n        df_submission.to_csv(\"submission.tsv\",header=False, index=False,sep='\\t')\n        \n        \nsub = submitt_predictions()\nsub.submit(protein_list,predictions)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"     \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}