{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Crawling from paperswithcode.com\n",
"\n",
"[API Documentation](https://paperswithcode.com/api/v1/docs/)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/mnt/nvme01/.venv/lib/python3.11/site-packages/umap/distances.py:1063: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\n",
" @numba.jit()\n",
"/mnt/nvme01/.venv/lib/python3.11/site-packages/umap/distances.py:1071: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\n",
" @numba.jit()\n",
"/mnt/nvme01/.venv/lib/python3.11/site-packages/umap/distances.py:1086: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\n",
" @numba.jit()\n",
"/mnt/nvme01/.venv/lib/python3.11/site-packages/umap/umap_.py:660: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\n",
" @numba.jit()\n",
"2023-07-14 13:12:02.877303: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
"To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2023-07-14 13:12:03.877543: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
]
}
],
"source": [
"import pandas as pd\n",
"import requests\n",
"from tqdm.notebook import trange, tqdm\n",
"import plotly.express as px\n",
"import uuid\n",
"from sentence_transformers import SentenceTransformer\n",
"from umap import UMAP\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# RUN ONLY IF YOU DONT HAVE THE CORRESPONDING JSON FILES AT HAND - it takes around 2 hours to run\n",
"\n",
"ITEMS_PER_PAGE = 100\n",
"\n",
"# Get all areas\n",
"print(\"Getting all areas...\")\n",
"areas = requests.get(\"https://paperswithcode.com/api/v1/areas\").json()\n",
"areas = pd.DataFrame(areas[\"results\"])\n",
"print(\"Done.\")\n",
"\n",
"# Get all tasks from all areas and write them to the datafile\n",
"print(\"Getting all tasks...\")\n",
"# Make a new column tasks\n",
"areas[\"tasks\"] = areas.id.apply(lambda x: [])\n",
"page = 1\n",
"for area_id in tqdm(areas.id):\n",
" # Make a first request to get the count\n",
" count = requests.get(f\"https://paperswithcode.com/api/v1/areas/{area_id}/tasks?items_per_page=1\").json()[\"count\"]\n",
" print(f\"Getting tasks from area {area_id}. Got {count} tasks...\")\n",
" for page in trange(1, int(count / ITEMS_PER_PAGE) + 2):\n",
" try:\n",
" response = requests.get(f\"https://paperswithcode.com/api/v1/areas/{area_id}/tasks?items_per_page={ITEMS_PER_PAGE}&page={page}\").json()\n",
" # Append the results to a list in a new tasks column\n",
" if response[\"results\"] is not None:\n",
" areas.loc[areas.id == area_id, \"tasks\"] = areas.loc[areas.id == area_id, \"tasks\"].apply(lambda x: x + response[\"results\"])\n",
" else:\n",
" print(f\"Error getting tasks from area {area_id}.\")\n",
" break\n",
"\n",
" page += 1\n",
" except Exception as e:\n",
" print(f\"Error getting tasks from area {area_id}.\")\n",
" print(e)\n",
" print(response)\n",
"print(\"Done.\")\n",
"\n",
"# Write areas to json file\n",
"areas.to_json(\"areas.json\", orient=\"records\")\n",
"\n",
"print(f\"Saved {len(areas)} areas with corresponding tasks to areas.json.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# RUN ONLY IF YOU DONT HAVE THE CORRESPONDING JSON FILES AT HAND - it takes around 2 hours to run\n",
"\n",
"# Get areas.json\n",
"areas = pd.read_json(\"areas.json\", orient=\"records\")\n",
"\n",
"# Get all papers from all tasks and write them to the dataframe\n",
"print(\"Getting all papers...\")\n",
"page = 1\n",
"for area_id in tqdm(areas.id):\n",
" print(f\"Getting papers from area {area_id} with {len(areas.loc[areas.id == area_id, 'tasks'].values[0])} tasks...\")\n",
" for task_id in trange(len(areas.loc[areas.id == area_id, \"tasks\"].values[0])):\n",
" count = requests.get(f\"https://paperswithcode.com/api/v1/tasks/{areas.loc[areas.id == area_id, 'tasks'].values[0][task_id]['id']}/papers?items_per_page=1\").json()[\"count\"]\n",
" # for page in trange(1, int(count / ITEMS_PER_PAGE) + 2): # If you want to see the progress of each task\n",
" for page in range(1, int(count / ITEMS_PER_PAGE) + 2):\n",
" try:\n",
" response = requests.get(f\"https://paperswithcode.com/api/v1/tasks/{areas.loc[areas.id == area_id, 'tasks'].values[0][task_id]['id']}/papers?items_per_page={ITEMS_PER_PAGE}&page={page}\").json()\n",
" if response[\"results\"] is not None:\n",
" # Write the papers in the corresponding tasks\n",
" areas.loc[areas.id == area_id, 'tasks'].values[0][task_id][\"papers\"] = response[\"results\"] \n",
" else:\n",
" print(f\"Error getting papers from task {areas.loc[areas.id == area_id, 'tasks'].values[0][task_id]['id']}.\")\n",
" break\n",
" page += 1\n",
" except Exception as e:\n",
" print(f\"Error getting papers from task {areas.loc[areas.id == area_id, 'tasks'].values[0][task_id]['id']}.\")\n",
" print(e)\n",
" print(response)\n",
" \n",
"print(\"Done.\")\n",
"\n",
"# Write areas to json file\n",
"areas.to_json(\"areas_tasks_papers.json\", orient=\"records\")\n",
"print(f\"Saved {len(areas)} areas with corresponding tasks and papers to areas_tasks_papers.json.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exploring the Data\n",
"\n",
"Now we have a look at the gathered areas, tasks and papers from paperswithcode.com. We will use plotly express to visualize the data."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"EXPLORING THE DATA\n"
]
},
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "name=%{x}
total_tasks=%{y}