{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d01ae085-ac7a-4310-8b6b-dbcdb121816f",
   "metadata": {},
   "source": [
    "# Analysis notebook\n",
    "\n",
    "This is an analysis of patents data from webscraping WIPO PATENTSCOPE using: <br> In the main page (https://patentscope.wipo.int/search/en/search.jsf), set *\"Field\"* to **Publication Date**. Then, write the first year of interest in the *\"Search terms...\"* section. <br> In the section right under the search bar, set *\"Sort\"* to **Relevance** and *\"Per page\"* to **200**.    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d128f378-ddec-4003-9adf-c2aee74789dc",
   "metadata": {},
   "source": [
    "## Importing libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "629f9abf-0878-43a1-866b-5d260a6c17dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import plotly.express as px\n",
    "import country_converter as coco\n",
    "import plotly.io as pio\n",
    "from IPython.display import IFrame\n",
    "pio.renderers.default = \"plotly_mimetype\"\n",
    "cc = coco.CountryConverter()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6003f4b3-a2d2-40b9-91fc-a75243f7123b",
   "metadata": {},
   "source": [
    "## Importing data "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f79ebc49-416f-436c-8b12-a8099c70d089",
   "metadata": {},
   "outputs": [],
   "source": [
    "pat = pd.read_csv(\"../data/amateur-science/pat.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50d82dfb-fbee-4f94-8afd-48331d78300b",
   "metadata": {},
   "source": [
    "## Data Processing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9a5d625-8ef7-4bd8-93bd-4e47b8e358f7",
   "metadata": {},
   "source": [
    "Adding country names, codes and continents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe5017c5-f9a5-4de3-8892-1c6420b59240",
   "metadata": {},
   "outputs": [],
   "source": [
    "pat[\"Full Country\"] = cc.pandas_convert(series=pat[\"Office Country\"], to='name_short', not_found = \"NaN\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b928b605-5f07-4677-be3a-7826fa8da121",
   "metadata": {},
   "source": [
    "Function to get patent type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "526eb62e-33a9-4e4e-b5f2-d9eb649a7407",
   "metadata": {},
   "outputs": [],
   "source": [
    "#function\n",
    "def determine_status(row):\n",
    "    applicant_types = {'research', 'group', 'society', 'foundation', 'inc', 'compania', 'lab', 'industries', 'societe', 'manufacturing', 'machine', 'co '\n",
    "                       'corp', 'association', 'university', 'institute', 'company', 'llc', 'ltd', 'lfp', 'industria', 'industrie', 'firm', '+', 'co.',\n",
    "                       'pharmaceuticals', 'roche', \"l'oreal\", 'campos', 'technologies', 'corp', 'inst', 'pharma', 'electronics', 'volvo', 'corporation',\n",
    "                      'ltda', 'communications', 'ifp', 'technik', 'siemens','s.a', 'operations', 'limited', 'gmbh', 'novartis', 'agency',\n",
    "                      'elektronik', 's.p.a', 'UNIWERSYTET', 's.l', 's.r.l', 'a.s','urs', 'ag ', 'UNIVERSITEIT', 'hospital', 'silverphase',\n",
    "                      'sanofi', 'science', 'medicament', 'recherche', 'tech', 'international', 'networks', 'france', 'nucleix', 'cosmetique', \n",
    "                       'astrazeneca', 'universite', 'les ', 'igt', 'service', 'services', 'univ', 'products', 'product', 'bank', 'compan', \n",
    "                      'cotton', '& co', '&co', 'comp', 'constructions', 'meca', 'sciences', 'tech', 'consulting', ' spa', 'management', 'associates', \n",
    "                       'holdings', 'systems', ' as', ' co', 'electric', 'printing', 'steel', ' ind', 'chemicals', ' ag', 'a.g', \"johnson & johnson\"\n",
    "                      ,'gm. b. h', 'informazioni', 'g. m. b. h.', 'anonyme', 'limitada', 'sociedad', 'solex', 's. a', 'eleuterio', 'societr',\n",
    "                      'commissariat', 's.a', 'interlight', 's. l', 'electronique', 'moebius & ruppert', 'g m b h', 'elektro', 'società', 'energía',\n",
    "                      'philips', '&', 's.c.i.', 'société', 'sté', 'g.m.b.h', 'energy', 'a. k','investigación', 'fabrica', 'limited'}\n",
    "    if pd.notna(row['Inventor']) and pd.notna(row['Applicant']): #if inventor and applicant are both not missing\n",
    "        if isinstance(row['Inventor'], str) and isinstance(row['Applicant'], str):\n",
    "            if row['Applicant'] in row['Inventor']:\n",
    "                return \"Solo Inventor\"\n",
    "            elif any(word in row['Applicant'].lower() for word in applicant_types):\n",
    "                return \"Research/Company\"\n",
    "            else:\n",
    "                return \"default\"\n",
    "    elif pd.isna(row['Inventor']) and pd.notna(row['Applicant']): #if inventor is missing but not applicant\n",
    "        if any(word in row['Applicant'].lower() for word in applicant_types):\n",
    "            return \"Research/Company\"\n",
    "        elif row['Applicant'] == 'applicant name missing':\n",
    "            return 'default'\n",
    "        else:\n",
    "            return \"Solo Inventor\"\n",
    "    elif pd.notna(row['Inventor']) and pd.isna(row['Applicant']):#if applicant is missing but not inventor\n",
    "        # if (row['Year']) < 1920:\n",
    "            return \"Solo Inventor\"\n",
    "        # else: return \"default 2\"\n",
    "    else:\n",
    "        return \"default\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0264b2d7-e236-4fba-b0c2-06748df4d68c",
   "metadata": {},
   "source": [
    "Converting to date, applying function to extract patent type, grouping by year and patent type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb958fe8-a360-46f3-879e-02b7a4fbc8ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "#convert date string to actual date\n",
    "pat[\"Publication date\"] = pd.to_datetime(pat[\"Publication date\"], format=\"%d.%m.%Y\")\n",
    "#converting date to year\n",
    "pat[\"Year\"] = pat[\"Publication date\"].dt.year\n",
    "#apply method to get patent type\n",
    "pat['Type'] = pat.apply(determine_status, axis=1)\n",
    "pat = pat.groupby(['Year', 'Type', 'Full Country']).size().reset_index(name='Patents')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67d0ae43-e27c-4e32-bf56-45c6cebf4941",
   "metadata": {},
   "source": [
    "Getting proportions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78b6c5e1-4892-46c4-82da-7bd038454845",
   "metadata": {},
   "outputs": [],
   "source": [
    "#dropping default patents\n",
    "pat_proportions = pat[pat[\"Type\"] != \"default\"]\n",
    "#getting sum of patents per year\n",
    "total_per_year = pat_proportions.groupby('Year')['Patents'].transform('sum')\n",
    "#getting proportions of patent types\n",
    "pat_proportions['Proportion'] = pat_proportions['Patents'] / total_per_year\n",
    "pat_proportions= pat_proportions.groupby([\"Year\", \"Type\"]).sum().reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17c30628-abab-4647-8f5e-773781955ff4",
   "metadata": {},
   "source": [
    "### Data Visualization: Stacked Bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ebbb140-fead-44ee-bf66-9a553ef7856c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| label: fig1nb\n",
    "\n",
    "fig1 = px.bar(pat_proportions\n",
    "             , x=\"Year\"\n",
    "             , y=\"Proportion\"\n",
    "             , color = \"Type\"\n",
    "             , template= \"plotly_white\"\n",
    "             , color_discrete_sequence=['#F7C0BB', '#EB5E51']\n",
    "             ,title=\"<b>Worldwide Patent Applications Classification</b>\"\n",
    "             , height= 400\n",
    "             , width = 1100\n",
    "              , hover_name = \"Year\"\n",
    "             , hover_data = [\"Patents\", \"Proportion\"]\n",
    "             , orientation='v')\n",
    "fig1.update_xaxes(title = None, dtick=5, ticks=\"outside\",ticklen=4,range=[1910,2023]) \n",
    "fig1.update_yaxes(title = \"Relative Proportion\", tickformat= ',.0%')\n",
    "fig1.update_layout(legend_title = None,\n",
    "                   font_family=\"Calibri\",\n",
    "                   font_color=\"black\",\n",
    "                   title_font_family=\"Calibri\",\n",
    "                   font=dict(size=14),\n",
    "                   title_font_color=\"black\") \n",
    "annotations = [\n",
    "      {'text': \"Source: WIPO Patentscope\", 'showarrow': False, 'x': 0.99, 'y': -0.23, 'xref': 'paper', 'yref': 'paper','font':{'size':11, 'color':\"grey\"}}\n",
    "]\n",
    "for annotation in annotations:\n",
    "    fig1.add_annotation(annotation)\n",
    "fig1.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8548a2f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| label: fig2nb\n",
    "\n",
    "IFrame(src='https://janeabdo.github.io/carousel/', width='800', height='700')"
   ]
  }
 ],
 "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
