```{python}
import os
import pandas as pd
import plotly.io as pio
pio.templates.default = "plotly_white" # Set plotly theme

from er_evaluation.summary import (
    homonymy_rate,
    name_variation_rate,
)
from er_evaluation.plots import (
    compare_plots, 
    plot_cluster_sizes_distribution,
    plot_entropy_curve,
)
from pv_evaluation.benchmark import (
    inventor_benchmark_plot,
    inventor_estimates_plot,
    plot_entropy_curves,
    top_inventors,
    plot_cluster_sizes,
    plot_homonymy_rates,
    plot_name_variation_rates,
)
```

# Summary Statistics

```{python}
def read_auto(datapath, dtype):
    _, ext = os.path.splitext(datapath)

    if ext == ".csv":
        return pd.read_csv(datapath, dtype=dtype)
    elif ext == ".tsv":
        return pd.read_csv(datapath, sep="\t", dtype=dtype)
    elif ext in [".parquet", ".pq", ".parq"]:
        return pd.read_parquet(datapath, dtype=dtype)
    elif ext in [".xlsx", ".xls"]:
        return pd.read_excel(datapath, dtype=dtype)
    else:
        raise Exception("Unsupported file type. Should be one of csv, tsv, parquet, or xlsx.")

def load_disambiguation(filename):
    data = read_auto(filename, dtype=str)
    data.set_index("mention_id", inplace=True)
    disambiguation = data.iloc[:, 0]
    disambiguation.rename(filename, inplace=True)

    return disambiguation

disambiguations = {filename: load_disambiguation(filename) for filename in {{ disambiguation_files }}}

inventor_not_disambiguated = read_auto("{{ inventor_not_disambiguated_file }}", dtype=str)
inventor_not_disambiguated["mention_id"] = "US" + inventor_not_disambiguated.patent_id + "-" + inventor_not_disambiguated.inventor_sequence
inventor_not_disambiguated.set_index("mention_id", inplace=True)

full_names = inventor_not_disambiguated.raw_inventor_name_first + " " + inventor_not_disambiguated.raw_inventor_name_last
full_names = full_names.rename("full_name")
```

::: {.panel-tabset}

## Cluster Sizes

{% include 'inventor/callouts/cluster-sizes-tips.qmd' %}

:::{.column-body-outset}
```{python}
plot_cluster_sizes(disambiguations)
```
:::

## Top Inventors

{% include 'inventor/callouts/top-inventors-tips.qmd' %}

::: {.panel-tabset}

{% for filename in  disambiguation_files%}

## {{ filename }}

```{python}
from IPython.core.display import display, HTML

display(HTML(top_inventors(disambiguations["{{ filename }}"], full_names).to_html()))
```

{% endfor %}

:::

## Entropy Curve

{% include 'inventor/callouts/entropy-tips.qmd' %}

:::{.column-body-outset}
```{python}
plot_entropy_curves(disambiguations)
```
:::

## Cluster Homogeneity

{% include 'inventor/callouts/cluster-homogeneity-tips.qmd' %}

:::{.column-body-outset}
```{python}
plot_name_variation_rates(disambiguations, full_names)
```
:::

## Between-cluster similarity

{% include 'inventor/callouts/between-cluster-tips.qmd'%}

:::{.column-body-outset}
```{python}
plot_homonymy_rates(disambiguations, full_names)
```
:::

:::
