{"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":"markdown","source":"# Random Forest Classifier CAFA5 - Protein Function Prediction","metadata":{}},{"cell_type":"markdown","source":"# Import Statements","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport cudf\n# import cuml\n\nfrom sklearn.model_selection import train_test_split\nfrom cuml.ensemble import RandomForestClassifier as CuMLRFClassifier\n\nfrom sklearn.metrics import hamming_loss\n\n!pip install pickle-mixin\n\nfrom tqdm import tqdm\nimport pickle","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:53.823829Z","start_time":"2023-06-07T07:33:47.554045Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:23:28.571401Z","iopub.execute_input":"2023-06-08T17:23:28.57191Z","iopub.status.idle":"2023-06-08T17:23:49.477477Z","shell.execute_reply.started":"2023-06-08T17:23:28.571877Z","shell.execute_reply":"2023-06-08T17:23:49.476197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:53.82718Z","start_time":"2023-06-07T07:33:53.824666Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:12.727883Z","iopub.execute_input":"2023-06-08T17:25:12.728457Z","iopub.status.idle":"2023-06-08T17:25:12.758512Z","shell.execute_reply.started":"2023-06-08T17:25:12.728409Z","shell.execute_reply":"2023-06-08T17:25:12.757379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Paths:\n    train_terms_tsv =  \"/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv\"\n    test_embeds_npy = \"/kaggle/input/t5embeds/test_embeds.npy\"\n    test_ids_npy = \"/kaggle/input/t5embeds/test_ids.npy\"\n    train_embeds_npy = \"/kaggle/input/t5embeds/train_embeds.npy\"\n    train_ids_npy = \"/kaggle/input/t5embeds/train_ids.npy\"\n    train_labels_csv = \"/kaggle/input/train-labels-cafa5/train_labels.csv\"","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T10:04:38.946132Z","start_time":"2023-06-07T10:04:38.94432Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:15.320287Z","iopub.execute_input":"2023-06-08T17:25:15.320982Z","iopub.status.idle":"2023-06-08T17:25:15.326287Z","shell.execute_reply.started":"2023-06-08T17:25:15.320947Z","shell.execute_reply":"2023-06-08T17:25:15.325166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read The Training Dataset","metadata":{}},{"cell_type":"code","source":"# read the training dataset\ntrain_terms = pd.read_csv(Paths.train_terms_tsv,sep=\"\\t\")\nprint(train_terms.shape)","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:55.265845Z","start_time":"2023-06-07T07:33:53.831979Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:20.360516Z","iopub.execute_input":"2023-06-08T17:25:20.360886Z","iopub.status.idle":"2023-06-08T17:25:23.806212Z","shell.execute_reply.started":"2023-06-08T17:25:20.360847Z","shell.execute_reply":"2023-06-08T17:25:23.803624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_labels = 500\nlabels_to_consider = train_terms[\"term\"].value_counts().iloc[:num_of_labels]","metadata":{"execution":{"iopub.status.busy":"2023-06-08T17:25:23.808812Z","iopub.execute_input":"2023-06-08T17:25:23.809196Z","iopub.status.idle":"2023-06-08T17:25:24.604274Z","shell.execute_reply.started":"2023-06-08T17:25:23.809162Z","shell.execute_reply":"2023-06-08T17:25:24.603311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(x= labels_to_consider.index,height =  labels_to_consider,)\nplt.xticks([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-08T17:25:24.606057Z","iopub.execute_input":"2023-06-08T17:25:24.606445Z","iopub.status.idle":"2023-06-08T17:25:25.697249Z","shell.execute_reply.started":"2023-06-08T17:25:24.60641Z","shell.execute_reply":"2023-06-08T17:25:25.696275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"We will choose only top {} frequent labels (in train) and predict only them.\".format(num_of_labels))","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:55.280103Z","start_time":"2023-06-07T07:33:55.277486Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:25.699827Z","iopub.execute_input":"2023-06-08T17:25:25.700598Z","iopub.status.idle":"2023-06-08T17:25:25.706455Z","shell.execute_reply.started":"2023-06-08T17:25:25.70056Z","shell.execute_reply":"2023-06-08T17:25:25.70542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_to_consider = train_terms[\"term\"].value_counts().index[:num_of_labels].tolist()\nprint(\"labels_to_consider\")\nprint(\"First {}\\n {}\".format(10,labels_to_consider[:10]))","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:55.634974Z","start_time":"2023-06-07T07:33:55.629223Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:25.708257Z","iopub.execute_input":"2023-06-08T17:25:25.709211Z","iopub.status.idle":"2023-06-08T17:25:26.49801Z","shell.execute_reply.started":"2023-06-08T17:25:25.709175Z","shell.execute_reply":"2023-06-08T17:25:26.49701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# update the train_terms to hold only those values which have the selected labels\ntrain_terms_updated = train_terms.loc[train_terms.term.isin(labels_to_consider)]\ntrain_terms_updated.shape","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:56.947089Z","start_time":"2023-06-07T07:33:55.899977Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:26.499626Z","iopub.execute_input":"2023-06-08T17:25:26.500297Z","iopub.status.idle":"2023-06-08T17:25:27.099323Z","shell.execute_reply.started":"2023-06-08T17:25:26.500261Z","shell.execute_reply":"2023-06-08T17:25:27.098308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the t5_embeds Data","metadata":{}},{"cell_type":"code","source":"# Load the embedding protien ids\nprot_ids = np.load(Paths.train_ids_npy, allow_pickle=True)\nprot_embeddings = np.load(Paths.train_embeds_npy, allow_pickle=True).astype(np.float32)","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:56.950921Z","start_time":"2023-06-07T07:33:56.945032Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:27.102134Z","iopub.execute_input":"2023-06-08T17:25:27.102825Z","iopub.status.idle":"2023-06-08T17:25:37.804738Z","shell.execute_reply.started":"2023-06-08T17:25:27.102789Z","shell.execute_reply":"2023-06-08T17:25:37.803742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_proteins = len(prot_ids)","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:56.962266Z","start_time":"2023-06-07T07:33:56.94887Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:37.806091Z","iopub.execute_input":"2023-06-08T17:25:37.80652Z","iopub.status.idle":"2023-06-08T17:25:37.812455Z","shell.execute_reply.started":"2023-06-08T17:25:37.806484Z","shell.execute_reply":"2023-06-08T17:25:37.810273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a datafrme of the embeddings\ncolumn_num = prot_embeddings.shape[1]\nprot_df = pd.DataFrame(prot_embeddings, columns = [\"Column_\" + str(i) for i in range(1, column_num+1)], dtype=np.float32)\nprot_df.set_index(prot_ids,inplace=True)\nprot_df.head()","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:56.96274Z","start_time":"2023-06-07T07:33:56.949064Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:37.815154Z","iopub.execute_input":"2023-06-08T17:25:37.815543Z","iopub.status.idle":"2023-06-08T17:25:37.865755Z","shell.execute_reply.started":"2023-06-08T17:25:37.815511Z","shell.execute_reply":"2023-06-08T17:25:37.864693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(prot_df.shape)","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-06-07T07:33:56.963166Z","start_time":"2023-06-07T07:33:56.949499Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-08T17:25:37.867331Z","iopub.execute_input":"2023-06-08T17:25:37.867697Z","iopub.status.idle":"2023-06-08T17:25:37.873527Z","shell.execute_reply.started":"2023-06-08T17:25:37.867666Z","shell.execute_reply":"2023-06-08T17:25:37.872467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a prot_labels DataFrame which contains binary one-hot encoded GO Labels","metadata":{}},{"cell_type":"code","source":"# !pip install progressbar\n# import progressbar\n\n# # Creating a label_df which is a binary representation of which protien is\n# # annotated by which of the chosen top protien labels and which are not.\n\n# bar = progressbar.ProgressBar(maxval=num_of_labels, \\\n#     widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])\n\n# prot_labels = np.zeros((num_of_proteins ,num_of_labels), dtype=np.float32)\n# # convert prot_ids to series to use the .isin method\n# prot_ids_series = pd.Series(prot_ids)\n\n# bar.start()\n# for label_i in range(num_of_labels):\n\n#     label_i_go_id = labels_to_consider[label_i]\n\n#     prots_annotated_by_label = train_terms_updated[train_terms_updated['term'] == label_i_go_id ][\"EntryID\"].unique()\n\n#     prot_labels[:,label_i] = prot_ids_series.isin(prots_annotated_by_label).astype(np.float32)\n\n#     bar.update(label_i+1)\n# bar.finish()\n\n# prot_labels = pd.DataFrame(prot_labels,columns=labels_to_consider, dtype=np.float32)\n# prot_labels.head()","metadata":{"ExecuteTime":{"end_time":"2023-06-07T07:33:56.963292Z","start_time":"2023-06-07T07:33:56.950124Z"},"jupyter":{"outputs_hidden":false},"collapsed":false,"execution":{"iopub.status.busy":"2023-06-08T17:25:53.171478Z","iopub.execute_input":"2023-06-08T17:25:53.171893Z","iopub.status.idle":"2023-06-08T17:25:53.176769Z","shell.execute_reply.started":"2023-06-08T17:25:53.171862Z","shell.execute_reply":"2023-06-08T17:25:53.175857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load pre-created train_labels dataset from private dataset\nprot_labels = pd.read_csv(Paths.train_labels_csv, dtype=np.float32)\nprot_labels.head()","metadata":{"ExecuteTime":{"end_time":"2023-06-07T07:34:10.588094Z","start_time":"2023-06-07T07:33:56.950482Z"},"execution":{"iopub.status.busy":"2023-06-08T17:25:53.565088Z","iopub.execute_input":"2023-06-08T17:25:53.56542Z","iopub.status.idle":"2023-06-08T17:26:34.227577Z","shell.execute_reply.started":"2023-06-08T17:25:53.565393Z","shell.execute_reply":"2023-06-08T17:26:34.226284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we are only using top 500 terms right now and not all 1500 available in the prot_labels\nprot_labels.drop(\"Unnamed: 0\",axis=1,inplace=True)\nprot_labels = prot_labels.iloc[:,:500]\nprot_labels.set_index(prot_ids,inplace=True)\nprot_labels.shape\nprot_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-08T17:26:34.230027Z","iopub.execute_input":"2023-06-08T17:26:34.230456Z","iopub.status.idle":"2023-06-08T17:26:34.526005Z","shell.execute_reply.started":"2023-06-08T17:26:34.230419Z","shell.execute_reply":"2023-06-08T17:26:34.524847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split data into testing and training set","metadata":{}},{"cell_type":"code","source":"train_df, test_df, train_labels, test_labels = train_test_split(prot_df, prot_labels, test_size=0.4, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T17:26:34.527938Z","iopub.execute_input":"2023-06-08T17:26:34.528298Z","iopub.status.idle":"2023-06-08T17:26:35.105514Z","shell.execute_reply.started":"2023-06-08T17:26:34.528266Z","shell.execute_reply":"2023-06-08T17:26:35.104444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Model Using GPU - Acceleration (GPU T4 x2)","metadata":{}},{"cell_type":"code","source":"train_df_cudf = cudf.DataFrame.from_pandas(train_df).astype('float32')\ntrain_labels_cudf = cudf.DataFrame.from_pandas(train_labels).astype('float32')\ntest_df_cudf = cudf.DataFrame.from_pandas(test_df).astype('float32')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T17:26:56.752051Z","iopub.execute_input":"2023-06-08T17:26:56.752779Z","iopub.status.idle":"2023-06-08T17:27:10.212112Z","shell.execute_reply.started":"2023-06-08T17:26:56.752734Z","shell.execute_reply":"2023-06-08T17:27:10.210921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from time import sleep","metadata":{"execution":{"iopub.status.busy":"2023-06-08T17:38:24.595158Z","iopub.execute_input":"2023-06-08T17:38:24.595532Z","iopub.status.idle":"2023-06-08T17:38:24.600666Z","shell.execute_reply.started":"2023-06-08T17:38:24.595502Z","shell.execute_reply":"2023-06-08T17:38:24.599706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels_train_df = pd.DataFrame(index= train_df.index, columns= train_labels.columns, dtype= np.float32).fillna(np.float32(0))","metadata":{"execution":{"iopub.status.busy":"2023-06-08T18:45:04.358471Z","iopub.execute_input":"2023-06-08T18:45:04.358859Z","iopub.status.idle":"2023-06-08T18:45:04.849504Z","shell.execute_reply.started":"2023-06-08T18:45:04.35882Z","shell.execute_reply":"2023-06-08T18:45:04.84853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels_df = pd.DataFrame(index= test_df.index, columns= test_labels.columns, dtype= np.float32).fillna(np.float32(0))","metadata":{"execution":{"iopub.status.busy":"2023-06-08T18:45:04.851932Z","iopub.execute_input":"2023-06-08T18:45:04.852537Z","iopub.status.idle":"2023-06-08T18:45:05.039738Z","shell.execute_reply.started":"2023-06-08T18:45:04.852499Z","shell.execute_reply":"2023-06-08T18:45:05.038639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_columns = 500\n\nwith tqdm(total=n_columns, ncols=100, desc=\"Training\", unit=\"column\") as pbar:\n    for col in range(n_columns):\n        \n        rf_clf = CuMLRFClassifier()\n        rf_clf.fit(train_df_cudf, train_labels_cudf.iloc[:,col])\n        \n        predict_labels = rf_clf.predict(test_df_cudf)\n        predicted_labels_df[train_labels_cudf.columns[col]] = predict_labels.to_numpy()\n        \n        predict_labels_train = rf_clf.predict(train_df_cudf)\n        predicted_labels_train_df[train_labels_cudf.columns[col]] = predict_labels_train.to_numpy()\n        \n        \n        pbar.update(1)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T18:51:41.457132Z","iopub.execute_input":"2023-06-08T18:51:41.457531Z","iopub.status.idle":"2023-06-08T19:16:26.183646Z","shell.execute_reply.started":"2023-06-08T18:51:41.457501Z","shell.execute_reply":"2023-06-08T19:16:26.182667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = hamming_loss(train_labels.iloc[:,:n_columns],predicted_labels_train_df.iloc[:,:n_columns])\nprint(f\"Hamming Loss on the train set itself : {loss}.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-08T19:16:32.238927Z","iopub.execute_input":"2023-06-08T19:16:32.239291Z","iopub.status.idle":"2023-06-08T19:16:37.742466Z","shell.execute_reply.started":"2023-06-08T19:16:32.239261Z","shell.execute_reply":"2023-06-08T19:16:37.741505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = hamming_loss(test_labels.iloc[:,:n_columns],predicted_labels_df.iloc[:,:n_columns])\nprint(f\"Hamming Loss on the test set : {loss}.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-08T19:16:44.339674Z","iopub.execute_input":"2023-06-08T19:16:44.340036Z","iopub.status.idle":"2023-06-08T19:16:47.810534Z","shell.execute_reply.started":"2023-06-08T19:16:44.340008Z","shell.execute_reply":"2023-06-08T19:16:47.809434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels_train_df.to_csv(\"rf_pred_train_upto_500_terms_random_state_42.csv\")\npredicted_labels_df.to_csv(\"rf_pred_upto_500_terms_random_state_42.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-06-08T19:17:31.474317Z","iopub.execute_input":"2023-06-08T19:17:31.474991Z","iopub.status.idle":"2023-06-08T19:18:25.597608Z","shell.execute_reply.started":"2023-06-08T19:17:31.474958Z","shell.execute_reply":"2023-06-08T19:18:25.596388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing evaluating test_labels efficiency of prediction of the model","metadata":{}}]}