{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imports",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pyprojroot import here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "load",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(here() / 'data/base/data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "create-table",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create summary statistics\n",
    "summary_stats = pd.DataFrame({\n",
    "    'Metric': [\n",
    "        'Total Experiments',\n",
    "        'Participating Countries',\n",
    "        'Participating Institutions',\n",
    "        'Mean Bell Parameter (S)',\n",
    "        'Mean Entanglement Fidelity',\n",
    "        'Mean Visibility (\\\\%)',\n",
    "        'Bell Violations (\\\\%)',\n",
    "        'High Fidelity ($>0.98$) (\\\\%)'\n",
    "    ],\n",
    "    'Value': [\n",
    "        len(data),\n",
    "        data['Country'].nunique(),\n",
    "        data['Institution'].nunique(),\n",
    "        f\"{data['Bell Parameter (S)'].mean():.3f} $\\pm$ {data['Bell Parameter (S)'].std():.3f}\",\n",
    "        f\"{data['Entanglement Fidelity'].mean():.4f} $\\pm$ {data['Entanglement Fidelity'].std():.4f}\",\n",
    "        f\"{data['Visibility (%)'].mean():.2f} $\\pm$ {data['Visibility (%)'].std():.2f}\",\n",
    "        f\"{(data['CHSH Violation'] == 'Yes').sum() / len(data) * 100:.1f}\",\n",
    "        f\"{(data['Entanglement Fidelity'] > 0.98).sum() / len(data) * 100:.1f}\"\n",
    "    ]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "save-latex",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert to LaTeX\n",
    "latex_table = r'''\\begin{table}[h]\n",
    "\\centering\n",
    "\\caption{Summary Statistics of Quantum Entanglement Measurements}\n",
    "\\label{tab:summary}\n",
    "\\begin{tabular}{lr}\n",
    "\\toprule\n",
    "\\textbf{Metric} & \\textbf{Value} \\\\\n",
    "\\midrule\n",
    "'''\n",
    "\n",
    "for _, row in summary_stats.iterrows():\n",
    "    latex_table += f\"{row['Metric']} & {row['Value']} \\\\\\\\\\n\"\n",
    "\n",
    "latex_table += r'''\\bottomrule\n",
    "\\end{tabular}\n",
    "\\end{table}\n",
    "'''\n",
    "\n",
    "# Save to file\n",
    "with open(here() / 'output/tables/summary.tex', 'w') as f:\n",
    "    f.write(latex_table)\n",
    "\n",
    "print(\"Summary table saved\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
