{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":1.290929,"end_time":"2023-04-28T16:11:40.612349","exception":false,"start_time":"2023-04-28T16:11:39.32142","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:10:59.232348Z","iopub.execute_input":"2023-06-08T14:10:59.232759Z","iopub.status.idle":"2023-06-08T14:10:59.240306Z","shell.execute_reply.started":"2023-06-08T14:10:59.232727Z","shell.execute_reply":"2023-06-08T14:10:59.239073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Assigning labels","metadata":{"papermill":{"duration":0.004422,"end_time":"2023-04-28T16:11:40.621902","exception":false,"start_time":"2023-04-28T16:11:40.61748","status":"completed"},"tags":[]}},{"cell_type":"code","source":"DATA_DIR = '/kaggle/input/cafa-5-protein-function-prediction'\nMAX_LABELS = 1048","metadata":{"papermill":{"duration":0.01492,"end_time":"2023-04-28T16:11:40.6416","exception":false,"start_time":"2023-04-28T16:11:40.62668","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:11:04.878067Z","iopub.execute_input":"2023-06-08T14:11:04.878465Z","iopub.status.idle":"2023-06-08T14:11:04.884043Z","shell.execute_reply.started":"2023-06-08T14:11:04.878434Z","shell.execute_reply":"2023-06-08T14:11:04.882655Z"},"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":{"papermill":{"duration":5.386139,"end_time":"2023-04-28T16:11:46.032438","exception":false,"start_time":"2023-04-28T16:11:40.646299","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:11:05.211468Z","iopub.execute_input":"2023-06-08T14:11:05.211877Z","iopub.status.idle":"2023-06-08T14:11:11.274965Z","shell.execute_reply.started":"2023-06-08T14:11:05.21184Z","shell.execute_reply":"2023-06-08T14:11:11.273563Z"},"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":{"papermill":{"duration":0.07447,"end_time":"2023-04-28T16:11:46.112388","exception":false,"start_time":"2023-04-28T16:11:46.037918","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:11:11.276965Z","iopub.execute_input":"2023-06-08T14:11:11.277345Z","iopub.status.idle":"2023-06-08T14:11:11.346638Z","shell.execute_reply.started":"2023-06-08T14:11:11.277306Z","shell.execute_reply":"2023-06-08T14:11:11.345356Z"},"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":{"papermill":{"duration":141.942432,"end_time":"2023-04-28T16:14:08.08098","exception":false,"start_time":"2023-04-28T16:11:46.138548","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:11:11.348511Z","iopub.execute_input":"2023-06-08T14:11:11.349106Z","iopub.status.idle":"2023-06-08T14:13:32.510653Z","shell.execute_reply.started":"2023-06-08T14:11:11.349075Z","shell.execute_reply":"2023-06-08T14:13:32.509308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading train embeddings","metadata":{"papermill":{"duration":0.082986,"end_time":"2023-04-28T16:14:08.247278","exception":false,"start_time":"2023-04-28T16:14:08.164292","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":20.577499,"end_time":"2023-04-28T16:14:28.907666","exception":false,"start_time":"2023-04-28T16:14:08.330167","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:13:32.513335Z","iopub.execute_input":"2023-06-08T14:13:32.513744Z","iopub.status.idle":"2023-06-08T14:13:56.440118Z","shell.execute_reply.started":"2023-06-08T14:13:32.513712Z","shell.execute_reply":"2023-06-08T14:13:56.438824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{"papermill":{"duration":0.083305,"end_time":"2023-04-28T16:14:29.076391","exception":false,"start_time":"2023-04-28T16:14:28.993086","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import sys\nsys.path.append('../input/iterativestratification')\nfrom iterstrat.ml_stratifiers import MultilabelStratifiedKFold\n\nFOLDS = 10\n# make a 5-fold split, measure time it takes to make the split\nstart = time.time()\nmsss = MultilabelStratifiedKFold(n_splits=FOLDS, random_state=0, shuffle=True)\nsplits = msss.split(x, y)\n\nfor fold, (train_idx, valid_idx) in enumerate(splits):\n    if fold == 0: \n        end = time.time()\n        print(f'Time to make {FOLDS}-fold split: {end - start:.2f} seconds')\n    x_tr, x_val = x[train_idx], x[valid_idx]\n    y_tr, y_val = y[train_idx], y[valid_idx]\n    print(f'Fold {fold + 1}')\n    print(f'Train: {x_tr.shape}, {y_tr.shape}')\n    print(f'Valid: {x_val.shape}, {y_val.shape}')\n    print()\n    model = Ridge()\n    model.fit(x_tr, y_tr)\n    y_hat = model.predict(x_val)\n\n    scores = pd.DataFrame(columns=list(selected_terms), index=['roc_auc'])\n\n    for i, term in enumerate(list(selected_terms)):\n        score = roc_auc_score(y_val[:, i], y_hat[:, i])\n        scores[term] = score\n\n    scores = scores.mean(axis=1)\n    print(scores)\n\n    # save the model in a pickle file\n    with open(f'ridge_model_{fold}.pkl', 'wb') as f:\n        pickle.dump(model, f)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:19:05.899954Z","iopub.execute_input":"2023-06-08T14:19:05.901238Z","iopub.status.idle":"2023-06-08T14:23:40.85589Z","shell.execute_reply.started":"2023-06-08T14:19:05.901187Z","shell.execute_reply":"2023-06-08T14:23:40.854688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"papermill":{"duration":0.08303,"end_time":"2023-04-28T16:14:57.422033","exception":false,"start_time":"2023-04-28T16:14:57.339003","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":6.254099,"end_time":"2023-04-28T16:15:03.759386","exception":false,"start_time":"2023-04-28T16:14:57.505287","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:23:40.858032Z","iopub.execute_input":"2023-06-08T14:23:40.858385Z","iopub.status.idle":"2023-06-08T14:23:41.394093Z","shell.execute_reply.started":"2023-06-08T14:23:40.858355Z","shell.execute_reply":"2023-06-08T14:23:41.392761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict for each fold and average the predictions\npredictions = np.zeros((len(test_ids), len(selected_terms)))\nfor fold in range(FOLDS):\n    print(f'Fold {fold + 1}')\n    with open(f'ridge_model_{fold}.pkl', 'rb') as f:\n        model = pickle.load(f)\n    predictions += model.predict(x_test) / FOLDS","metadata":{"papermill":{"duration":48.344071,"end_time":"2023-04-28T16:15:52.186228","exception":false,"start_time":"2023-04-28T16:15:03.842157","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:23:41.397773Z","iopub.execute_input":"2023-06-08T14:23:41.398617Z","iopub.status.idle":"2023-06-08T14:24:41.938776Z","shell.execute_reply.started":"2023-06-08T14:23:41.398553Z","shell.execute_reply":"2023-06-08T14:24:41.937523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:24:41.941276Z","iopub.execute_input":"2023-06-08T14:24:41.941651Z","iopub.status.idle":"2023-06-08T14:24:41.94996Z","shell.execute_reply.started":"2023-06-08T14:24:41.94162Z","shell.execute_reply":"2023-06-08T14:24:41.948766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Define chunk size\nchunk_size = 10_000  # adjust this value accordingly\n\n# Calculate the number of chunks\nnum_chunks = int(np.ceil(len(predictions) / chunk_size))\nprint(\"num_chunks\", num_chunks)\n\nfor i in range(num_chunks):\n    print(\"chunk\", i+1)\n    start = i * chunk_size\n    end = (i + 1) * chunk_size\n    \n    # Create chunk DataFrame\n    chunk_predictions = predictions[start:end]\n    chunk_test_ids = test_ids[start:end]\n    \n    sub_chunk = pd.DataFrame(data=chunk_predictions, \n                             columns=list(selected_terms), \n                             index=chunk_test_ids)\n    \n    # Transpose, unstack, reset index and filter\n    sub_chunk = sub_chunk.T.unstack().reset_index(name='prediction')\n    sub_chunk = sub_chunk[sub_chunk['prediction'] > 0]\n    \n    # Write the chunk to a CSV file, appending if the file already exists\n    if i == 0:\n        sub_chunk.to_csv('output.csv', index=False)\n    else:\n        sub_chunk.to_csv('output.csv', mode='a', header=False, index=False)\n\n# Now, 'output.csv' holds the data.\nprint(\"Done!\")\nsub = pd.read_csv('output.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-08T14:24:41.951309Z","iopub.execute_input":"2023-06-08T14:24:41.951681Z","iopub.status.idle":"2023-06-08T14:34:54.451336Z","shell.execute_reply.started":"2023-06-08T14:24:41.951652Z","shell.execute_reply":"2023-06-08T14:34:54.449861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.tsv', sep='\\t', index=False, header=False)","metadata":{"papermill":{"duration":296.790114,"end_time":"2023-04-28T16:20:49.060174","exception":false,"start_time":"2023-04-28T16:15:52.27006","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-08T14:34:54.453528Z","iopub.execute_input":"2023-06-08T14:34:54.454035Z","iopub.status.idle":"2023-06-08T14:43:15.915558Z","shell.execute_reply.started":"2023-06-08T14:34:54.453989Z","shell.execute_reply":"2023-06-08T14:43:15.914288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}