{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "0.0001\n",
      "0.0005\n",
      "0.001\n",
      "0.005\n",
      "0.01\n",
      "0.05\n",
      "0.1\n",
      "0.15\n",
      "0.2\n",
      "0.25\n",
      "0.3\n",
      "0.35\n",
      "0.4\n",
      "1.902881145477295\n"
     ]
    }
   ],
   "source": [
    "from causaldag import unknown_target_igsp\n",
    "import causaldag as cd\n",
    "import random\n",
    "from causaldag.utils.ci_tests import gauss_ci_suffstat, gauss_ci_test, MemoizedCI_Tester\n",
    "from causaldag.utils.invariance_tests import gauss_invariance_suffstat, gauss_invariance_test, MemoizedInvarianceTester\n",
    "import pandas\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "data = pandas.read_csv(\"./sachsdata/sachs_data_sel.csv\")\n",
    "Ts = pandas.read_csv(\"./sachsdata/sachs_Ts.csv\")[\"x\"]\n",
    "\n",
    "obs_samples = np.array(data.iloc[np.where(Ts == 0)])\n",
    "\n",
    "seed_number = 42\n",
    "\n",
    "n = 11\n",
    "\n",
    "diffTs = np.array(list(set(Ts)))\n",
    "diffTs = diffTs[np.where(diffTs > 0)]\n",
    "\n",
    "iv_samples_list = [np.array(data.iloc[np.where(Ts == ii)]) for ii in diffTs]\n",
    "\n",
    "\n",
    "# Form sufficient statistics\n",
    "obs_suffstat = gauss_ci_suffstat(obs_samples)\n",
    "invariance_suffstat = gauss_invariance_suffstat(obs_samples, iv_samples_list)\n",
    "setting_list = [dict(known_interventions=[]) for ii in diffTs]\n",
    "\n",
    "\n",
    "SP_alphas = np.array([0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4])\n",
    "DAGstring = [\"\"] * len(SP_alphas)\n",
    "\n",
    "print(seed_number)\n",
    "\n",
    "start_time = time.time()\n",
    "# Run UT-IGSP\n",
    "# Create conditional independence tester and invariance tester\n",
    "for ii in range(len(SP_alphas)):\n",
    "    alpha = SP_alphas[ii]\n",
    "    print(alpha)\n",
    "    alpha_inv = 1e-20 #alpha\n",
    "    ci_tester = MemoizedCI_Tester(gauss_ci_test, obs_suffstat, alpha=alpha)\n",
    "    invariance_tester = MemoizedInvarianceTester(gauss_invariance_test, invariance_suffstat, alpha=alpha_inv)\n",
    "    try:\n",
    "        est_dag, est_targets_list = unknown_target_igsp(setting_list, set(range(n)), ci_tester, invariance_tester)\n",
    "    except:\n",
    "        est_dag = \"error\"\n",
    "        print(\"SVD error\")\n",
    "    DAGstring[ii] = est_dag\n",
    "\n",
    "time_taken = time.time() - start_time\n",
    "print(time_taken)\n",
    "\n",
    "DAGs = pandas.DataFrame(DAGstring)\n",
    "\n",
    "DAGs.to_csv(\"./sachsnets/sachs_UT-IGSP_DAGs.csv\", header=False, index=False)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
