{"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":"import os\nimport gc\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import Ridge\nfrom sklearn.metrics import roc_auc_score\n\nimport numpy as np\nimport pandas as pd\n\nfrom tqdm import tqdm\ntqdm.pandas()\n\nimport pickle\nimport time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-04T17:53:00.165616Z","iopub.execute_input":"2023-06-04T17:53:00.166106Z","iopub.status.idle":"2023-06-04T17:53:01.889773Z","shell.execute_reply.started":"2023-06-04T17:53:00.166066Z","shell.execute_reply":"2023-06-04T17:53:01.888698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = '/kaggle/input/cafa-5-protein-function-prediction'\nMAX_LABELS = 1024","metadata":{"execution":{"iopub.status.busy":"2023-06-04T17:53:01.891774Z","iopub.execute_input":"2023-06-04T17:53:01.892313Z","iopub.status.idle":"2023-06-04T17:53:01.898228Z","shell.execute_reply.started":"2023-06-04T17:53:01.892281Z","shell.execute_reply":"2023-06-04T17:53:01.8968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_terms = pd.read_csv(os.path.join(DATA_DIR, 'Train', 'train_terms.tsv'), sep='\\t')\n\nterms = train_terms.groupby(['aspect', 'term'])['term'].count().reset_index(name='frequency')\nprint(terms.groupby('aspect')['term'].nunique())","metadata":{"execution":{"iopub.status.busy":"2023-06-04T17:53:06.545062Z","iopub.execute_input":"2023-06-04T17:53:06.545435Z","iopub.status.idle":"2023-06-04T17:53:10.716985Z","shell.execute_reply.started":"2023-06-04T17:53:06.545409Z","shell.execute_reply":"2023-06-04T17:53:10.715705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fractions = (terms.groupby('aspect')['term'].nunique() / terms['term'].nunique() * MAX_LABELS).apply(round)\nprint(fractions)\n\nselected_terms = set()\nfor aspect, number in fractions.items():\n    selection = terms.loc[(terms.aspect == aspect)]\n    selection = selection.nlargest(number, columns='frequency', keep='first')\n    selected_terms.update(selection.term.to_list())","metadata":{"execution":{"iopub.status.busy":"2023-06-04T17:53:10.71952Z","iopub.execute_input":"2023-06-04T17:53:10.719983Z","iopub.status.idle":"2023-06-04T17:53:10.768496Z","shell.execute_reply.started":"2023-06-04T17:53:10.719949Z","shell.execute_reply":"2023-06-04T17:53:10.767368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def assign_labels(annotations, selected_terms=selected_terms):\n    \n    intersection = selected_terms.intersection(annotations)\n    labels = np.isin(np.array(list(selected_terms)), np.array(list(intersection)))\n    \n    return list(labels.astype('int'))\n\nannotations = train_terms.groupby('EntryID')['term'].apply(set)\nlabels = annotations.progress_apply(assign_labels)\n\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-04T17:53:10.76987Z","iopub.execute_input":"2023-06-04T17:53:10.770178Z","iopub.status.idle":"2023-06-04T17:54:31.792178Z","shell.execute_reply.started":"2023-06-04T17:53:10.77015Z","shell.execute_reply":"2023-06-04T17:54:31.790828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids = np.load('/kaggle/input/t5embeds/train_ids.npy')\n\nx = np.load('/kaggle/input/t5embeds/train_embeds.npy')\ny = np.array(labels[train_ids].to_list())","metadata":{"execution":{"iopub.status.busy":"2023-06-04T17:54:31.794998Z","iopub.execute_input":"2023-06-04T17:54:31.795501Z","iopub.status.idle":"2023-06-04T17:54:49.874177Z","shell.execute_reply.started":"2023-06-04T17:54:31.795461Z","shell.execute_reply":"2023-06-04T17:54:49.872846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check pytorch version\nimport torch\nprint(torch.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T17:55:42.554247Z","iopub.execute_input":"2023-06-04T17:55:42.554721Z","iopub.status.idle":"2023-06-04T17:55:45.636295Z","shell.execute_reply.started":"2023-06-04T17:55:42.554686Z","shell.execute_reply":"2023-06-04T17:55:45.634837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_valid, y_train, y_valid = train_test_split(x, y, shuffle=True, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:07:50.117363Z","iopub.execute_input":"2023-06-04T18:07:50.118332Z","iopub.status.idle":"2023-06-04T18:07:50.596158Z","shell.execute_reply.started":"2023-06-04T18:07:50.118279Z","shell.execute_reply":"2023-06-04T18:07:50.594129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\n# measure roc auc score metric \nfrom tensorflow.keras.metrics import AUC\n\n# build a simple MLP model in Keras with ReLU activation and nothing else\nnfeats = x_train.shape[1]\nnlabels = y_train.shape[1]\nmodel = Sequential()\nmodel.add(Dense(256, activation='relu', input_dim=nfeats))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(nlabels, activation='sigmoid'))\nmodel.compile(loss='binary_crossentropy',\n                optimizer='adam',\n                metrics=[AUC()])","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:09:39.986585Z","iopub.execute_input":"2023-06-04T18:09:39.987022Z","iopub.status.idle":"2023-06-04T18:09:40.076732Z","shell.execute_reply.started":"2023-06-04T18:09:39.986989Z","shell.execute_reply":"2023-06-04T18:09:40.074403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=15, batch_size=64, validation_data=(x_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:09:49.201728Z","iopub.execute_input":"2023-06-04T18:09:49.202153Z","iopub.status.idle":"2023-06-04T18:16:14.078638Z","shell.execute_reply.started":"2023-06-04T18:09:49.202119Z","shell.execute_reply":"2023-06-04T18:16:14.076572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = model.predict(x_valid)\n\nscores = pd.DataFrame(columns=list(selected_terms), index=['roc_auc'])\n\nfor i, term in enumerate(selected_terms):\n    score = roc_auc_score(y_valid[:, i], y_hat[:, i])\n    scores[term] = score\n\nscores.mean(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:16:14.081402Z","iopub.execute_input":"2023-06-04T18:16:14.082726Z","iopub.status.idle":"2023-06-04T18:16:32.376702Z","shell.execute_reply.started":"2023-06-04T18:16:14.082675Z","shell.execute_reply":"2023-06-04T18:16:32.375912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = np.load('/kaggle/input/t5embeds/test_ids.npy')\nx_test = np.load('/kaggle/input/t5embeds/test_embeds.npy')","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:16:32.377843Z","iopub.execute_input":"2023-06-04T18:16:32.378753Z","iopub.status.idle":"2023-06-04T18:16:42.966649Z","shell.execute_reply.started":"2023-06-04T18:16:32.378721Z","shell.execute_reply":"2023-06-04T18:16:42.96506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del x_train, y_train, x_valid, y_valid, labels\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:16:42.968713Z","iopub.execute_input":"2023-06-04T18:16:42.969839Z","iopub.status.idle":"2023-06-04T18:16:44.582508Z","shell.execute_reply.started":"2023-06-04T18:16:42.969806Z","shell.execute_reply":"2023-06-04T18:16:44.581004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test)\ndel x_test\ngc.collect()\n\nchunk_size = 5_000\nchunks = [range(i, min(i + chunk_size, len(predictions))) for i in range(0, len(predictions), chunk_size)]\n\nfinal_sub = pd.DataFrame()  # Create an empty DataFrame to hold the final result\n\nprint(f\"processing {len(chunks)} chunks of {chunk_size} predictions each\")\n\nfor chunk in chunks:\n    print(f\"processing chunk {chunk}\")\n    sub = pd.DataFrame(data=predictions[chunk], columns=list(selected_terms), index=test_ids[chunk])\n    sub = sub.T.unstack().reset_index(name='prediction')\n    sub = sub.loc[sub['prediction'] > 0]\n    final_sub = pd.concat([final_sub, sub])  # Concatenate current chunk DataFrame to the final DataFrame\n\nfinal_sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:16:44.584043Z","iopub.execute_input":"2023-06-04T18:16:44.584405Z","iopub.status.idle":"2023-06-04T18:18:18.5463Z","shell.execute_reply.started":"2023-06-04T18:16:44.584377Z","shell.execute_reply":"2023-06-04T18:18:18.544775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_sub.to_csv('submission.tsv', sep='\\t', index=False, header=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-04T18:18:18.547875Z","iopub.execute_input":"2023-06-04T18:18:18.548217Z","iopub.status.idle":"2023-06-04T18:23:21.754121Z","shell.execute_reply.started":"2023-06-04T18:18:18.548189Z","shell.execute_reply":"2023-06-04T18:23:21.752766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}