{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6875e5a5",
   "metadata": {},
   "source": [
    "Loading Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4deb63ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Load and drop unwanted index column if present\n",
    "def load_top100(path):\n",
    "    s = pd.read_csv(path, header=None)[0]\n",
    "    s = s[s != \"0\"]      # ⬅ remove bogus index-row\n",
    "    return s.tolist()\n",
    "\n",
    "lasso_auc_top100 = load_top100(\"../results/lasso_auc_top100.csv\")\n",
    "lasso_recall_top100 = load_top100(\"../results/lasso_recall_top100.csv\")\n",
    "\n",
    "en_auc_top100 = load_top100(\"../results/elasticnet_auc_top100.csv\")\n",
    "en_recall_top100 = load_top100(\"../results/elasticnet_recall_top100.csv\")\n",
    "\n",
    "rfe_auc_top100 = load_top100(\"../results/rfe_auc_top100.csv\")\n",
    "rfe_recall_top100 = load_top100(\"../results/rfe_recall_top100.csv\")\n",
    "\n",
    "rfecv_auc_top100 = load_top100(\"../results/rfecv_auc_top100.csv\")\n",
    "rfecv_recall_top100 = load_top100(\"../results/rfecv_recall_top100.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6db7e845",
   "metadata": {},
   "source": [
    "Building the Master Method Dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ee4d528",
   "metadata": {},
   "outputs": [],
   "source": [
    "methods = {\n",
    "    \"Lasso AUC\": lasso_auc_top100,\n",
    "    \"Lasso Recall\": lasso_recall_top100,\n",
    "\n",
    "    \"EN AUC\": en_auc_top100,\n",
    "    \"EN Recall\": en_recall_top100,\n",
    "\n",
    "    \"RFE AUC\": rfe_auc_top100,\n",
    "    \"RFE Recall\": rfe_recall_top100,\n",
    "\n",
    "    \"RCV AUC\": rfecv_auc_top100,\n",
    "    \"RCV Recall\": rfecv_recall_top100,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "493fe76b",
   "metadata": {},
   "source": [
    "Building the Unified Feature Set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ead7c24c",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_feats = sorted(set().union(*methods.values()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6b6fbb0",
   "metadata": {},
   "source": [
    "Constructing the Agreement Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2580dce0",
   "metadata": {},
   "outputs": [],
   "source": [
    "table = pd.DataFrame({\"Feature\": all_feats})\n",
    "\n",
    "for m in methods:\n",
    "    table[m] = table[\"Feature\"].apply(lambda f: 1 if f in methods[m] else 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "727dfb50",
   "metadata": {},
   "source": [
    "Sum of how many methods selected each feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e7e08ba6",
   "metadata": {},
   "outputs": [],
   "source": [
    "table[\"SUM\"] = table[list(methods.keys())].sum(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91438f8f",
   "metadata": {},
   "source": [
    "Sort by agreement strength"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b14de42f",
   "metadata": {},
   "outputs": [],
   "source": [
    "table = table.sort_values(\"SUM\", ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9da13dd2",
   "metadata": {},
   "source": [
    "Exporting the Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7987a93b",
   "metadata": {},
   "outputs": [],
   "source": [
    "table.to_csv(\"../results/feature_agreement_table.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "92a85060",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Agreement table saved → ../results/feature_agreement_table.csv\n"
     ]
    }
   ],
   "source": [
    "print(\"Agreement table saved → ../results/feature_agreement_table.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
