{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "bfa8dc49-e3e5-4c07-9ce8-576617bba299", "metadata": {}, "outputs": [], "source": [ "# Importing relevant libraries\n", "import pandas as pd\n", "import numpy as np\n", "import json" ] }, { "cell_type": "code", "execution_count": 20, "id": "faeb31dd-97d3-46e8-b956-2fac4ee6d463", "metadata": {}, "outputs": [], "source": [ "# Fixing and cleaning up the new categories mapping\n", "with open(r\"./dataset/categories.txt\")as f:\n", " CATEGORIES = json.load(f)\n", "\n", "CATEGORIES = set(CATEGORIES.keys())\n", "\n", "IGNORED_CATEGOREIES = set([\n", " \"atom-ph\", \"bayes-an\", \"chao-dyn\", \"chem-ph\", \"cmp-lg\", \"comp-gas\", \"cond-mat\", \"dg-ga\", \"funct-an\",\n", " \"mtrl-th\", \"patt-sol\", \"plasm-ph\", \"q-alg\", \"q-bio\", \"solv-int\", \"supr-con\", \"test\", \"test.dis-nn\", \n", " \"test.mes-hall\", \"test.mtrl-sci\", \"test.soft\", \"test.stat-mech\", \"test.str-el\", \"test.supr-con\"\n", "])\n", "\n", "CATEGORY_ALIASES = {\n", " 'math.MP': 'math-ph',\n", " 'stat.TH': 'math.ST',\n", " 'math.IT': 'cs.IT',\n", " 'q-fin.EC': 'econ.GN',\n", " 'cs.SY': 'eess.SY',\n", " 'cs.NA': 'math.NA'\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "cd452c2f-6b3b-4ea0-812b-2886b33bf09b", "metadata": {}, "outputs": [], "source": [ "for I_CAT in IGNORED_CATEGOREIES:\n", " if I_CAT in CATEGORIES:\n", " CATEGORIES.remove(I_CAT)\n", "\n", "for C_ALIAS in CATEGORY_ALIASES:\n", " if C_ALIAS in CATEGORIES:\n", " CATEGORIES.remove(C_ALIAS)\n", " CATEGORIES.add(CATEGORY_ALIASES[C_ALIAS])\n", "\n", "CATEGORIES = sorted(list(CATEGORIES))\n", "CATEGORIES = {CATEGORIES[i]:i for i in range(len(CATEGORIES))}\n", "with open(r\"./dataset/categories.txt\", \"w\")as f:\n", " json.dump(CATEGORIES, f)" ] }, { "cell_type": "markdown", "id": "eff91d4f-a44c-4d2b-a96f-0849354c8273", "metadata": {}, "source": [ "#### For now only keep version 1" ] }, { "cell_type": "code", "execution_count": 5, "id": "db881114-a624-456b-a4ab-d4ec8a53ad60", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Cleaning the dataset, ie. removing duplicates, verifying none of the ignored tags are there and replace category aliases\n", "test_minor_path = r\"dataset/full_text_sample/test_minor_cats_full.json\"\n", "train_minor_path = r\"dataset/full_text_sample/train_minor_cats_full.json\"" ] }, { "cell_type": "code", "execution_count": 41, "id": "e3bbe301-4e28-4a03-bd3c-1bbc99b43a0e", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Fixing test\n", "pd_test_minor = pd.read_json(test_minor_path, lines=True)\n", "\n", "pd_test_minor.rename(columns={'major_category':'field','prime_category':'primary_subfield','abs_categories':'secondary_subfield'}, inplace=True)\n", "pd_test_minor = pd_test_minor[~pd_test_minor['field'].isin(IGNORED_CATEGOREIES)]\n", "pd_test_minor = pd_test_minor[(pd_test_minor['version'] == 1)]\n", "if pd_test_minor['primary_subfield'].isin(CATEGORY_ALIASES.keys()).sum():\n", " ValueError(\"IMPLEMENT CODE TO CHANGE CATEGORY_ALIASES\")\n", " \n", "pd_test_minor[['primary_subfield', \"secondary_subfield\"]].to_csv(\"dataset/baseline_datasets/test_field.csv\")\n", "test_fields[\"Usage\"] = \"Public\"\n", "pd_test_minor.drop(columns=['field', 'primary_subfield', \"secondary_subfield\"]).to_csv(r\"dataset/baseline_datasets/test_papers.csv\")" ] }, { "cell_type": "code", "execution_count": 43, "id": "5e0c00ef-29c5-44e3-a73e-e3417e07d53c", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Chunk 1\n", "Chunk 2\n", "Chunk 3\n", "Chunk 4\n", "Chunk 5\n", "Chunk 6\n", "Chunk 7\n", "Chunk 8\n", "Chunk 9\n", "Chunk 10\n", "Chunk 11\n", "Chunk 12\n" ] } ], "source": [ "# Fixing train\n", "chunksize = 10**4\n", "for i, pd_train_minor in enumerate(pd.read_json(train_minor_path, lines=True, chunksize=chunksize)):\n", " print(f\"Chunk {i+1}\")\n", " pd_train_minor.rename(columns={'major_category':'field','prime_category':'primary_subfield','abs_categories':'secondary_subfield'}, inplace=True)\n", " pd_train_minor = pd_train_minor[~pd_train_minor['field'].isin(IGNORED_CATEGOREIES)]\n", " pd_train_minor = pd_train_minor[(pd_train_minor['version'] == 1)]\n", " if pd_train_minor['primary_subfield'].isin(CATEGORY_ALIASES.keys()).sum():\n", " ValueError(\"IMPLEMENT CODE TO CHANGE CATEGORY_ALIASES \")\n", "\n", " if i==0:\n", " pd_train_minor.to_csv(r\"dataset/baseline_datasets/train.csv\")\n", " else:\n", " pd_train_minor.to_csv(r\"dataset/baseline_datasets/train.csv\", header=False, mode='a')" ] }, { "cell_type": "markdown", "id": "e7e5c112-eb78-4fb9-ad12-c3c4a90339a5", "metadata": {}, "source": [ "#### Removing using titles" ] }, { "cell_type": "code", "execution_count": 123, "id": "e968294d-a933-4dba-8142-cc593f9cfbf9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
paper_idversionyymmcreatedtitlesecondary_subfieldabstractprimary_subfieldfieldfulltext
38881006.1164210062010-06-10T05:56:49Non-perturbative renormalization of quark mass...hep-latWe present an evaluation of the quark mass ren...hep-lathep-latUTHEP-609\\nUTCCS-P-59\\n\\nNon-perturbative reno...
39571011.0157110112010-10-31T14:09:45Non-perturbative renormalization of quark mass...hep-latWe present an evaluation of the quark mass ren...hep-lathep-latNon-perturbative renormalization of quark mass...
\n", "
" ], "text/plain": [ " paper_id version yymm created \\\n", "3888 1006.1164 2 1006 2010-06-10T05:56:49 \n", "3957 1011.0157 1 1011 2010-10-31T14:09:45 \n", "\n", " title secondary_subfield \\\n", "3888 Non-perturbative renormalization of quark mass... hep-lat \n", "3957 Non-perturbative renormalization of quark mass... hep-lat \n", "\n", " abstract primary_subfield \\\n", "3888 We present an evaluation of the quark mass ren... hep-lat \n", "3957 We present an evaluation of the quark mass ren... hep-lat \n", "\n", " field fulltext \n", "3888 hep-lat UTHEP-609\\nUTCCS-P-59\\n\\nNon-perturbative reno... \n", "3957 hep-lat Non-perturbative renormalization of quark mass... " ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# TBD whether we do this or not\n", "duplicates = pd_test_minor[pd_test_minor.duplicated(subset=['title'], keep=False)]\n", "duplicates" ] }, { "cell_type": "code", "execution_count": null, "id": "2a5ea0fe-cec7-4f76-891c-fb80852c75a3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "cbc215aa-3b3d-4fbe-ae85-30ca8b9568e2", "metadata": {}, "source": [ "#### Score metrics used on Kaggle" ] }, { "cell_type": "code", "execution_count": 66, "id": "88a5a364-f8f6-4817-aaf7-4e6ce43f5090", "metadata": {}, "outputs": [], "source": [ "def score(solution: pd.DataFrame, submission: pd.DataFrame, row_id_column_name: str) -> float:\n", "\n", " try:\n", " # First calculate the primary subfield accuracy which is relatively simple\n", " res = solution[\"primary_subfield\"]==submission[\"primary_subfield\"]\n", " primary_field_acc = res.sum()/len(res)\n", "\n", " # We then calculate the secondary field accuracy\n", " sol_sec_list = solution[\"secondary_subfield\"].tolist()\n", " sub_sec_list = solution[\"secondary_subfield\"].tolist()\n", "\n", " secondary_field_acc = 0\n", " for i in range(len(sol_sec_list)):\n", " paper_secondary_field_pred = sub_sec_list[i].split(\" \")\n", " paper_secondary_field_sol = sol_sec_list[i].split(\" \")\n", "\n", " diff = len(paper_secondary_field_pred)-len(paper_secondary_field_sol)\n", "\n", " if diff<0:\n", " tot = 0\n", " for sf_sol in paper_secondary_field_sol:\n", " if sf_sol in paper_secondary_field_pred:\n", " tot+=1\n", " tot /= len(paper_secondary_field_sol)\n", " else:\n", " tot = 0\n", " for sf_pred in paper_secondary_field_pred:\n", " if sf_pred in paper_secondary_field_sol:\n", " tot+=1\n", "\n", " tot /= len(paper_secondary_field_pred)\n", "\n", " secondary_field_acc += tot\n", "\n", " loss = secondary_field_acc/len(sol_sec_list) + primary_field_acc\n", " \n", " return loss\n", "\n", " except Exception as e:\n", " \n", " raise ParticipantVisibleError(f\"Error occurred: {str(e)}\")" ] }, { "cell_type": "code", "execution_count": 67, "id": "0497a438-13b7-47d9-a493-bd7c9fc1892d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "score(pd_sol, pd_sol, \"id\")" ] }, { "cell_type": "code", "execution_count": null, "id": "38a4812b-26ce-4063-b6a7-245544e8e02a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 5 }