{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7bc4ecef",
   "metadata": {},
   "outputs": [],
   "source": [
    "## generate pole separation across years\n",
    "#creed2_/GSS/Qnets\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import collections\n",
    "import seaborn as sns \n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from cognet.cognet import cognet as cg\n",
    "from cognet.dataFormatter import dataFormatter\n",
    "from cognet.model import model \n",
    "\n",
    "# get years \n",
    "features_by_year = pd.read_csv('examples_data/features_by_year_GSS.csv',\n",
    "                               keep_default_na=True, \n",
    "                               index_col=False)\n",
    "POLEFILE='examples_data/polar_vectors.csv'\n",
    "qnets_by_year = {x[-11:-7]: x for x in os.listdir('../../creed2_/GSS/Qnets/')}\n",
    "data_by_year = {x[4:8]: x for x in os.listdir('../../creed2_/GSS/data/processed_data')}\n",
    "polarDist_by_year = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3573550b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "17 pole features not found in sample features\n",
      "updating\n",
      "11 pole features not found in sample features\n",
      "updating\n",
      "19 pole features not found in sample features\n",
      "updating\n",
      "4 pole features not found in sample features\n",
      "updating\n",
      "26 pole features not found in sample features\n",
      "updating\n",
      "29 pole features not found in sample features\n",
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "12 pole features not found in sample features\n",
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "10 pole features not found in sample features\n",
      "updating\n",
      "18 pole features not found in sample features\n",
      "updating\n",
      "19 pole features not found in sample features\n",
      "updating\n",
      "9 pole features not found in sample features\n",
      "updating\n",
      "23 pole features not found in sample features\n",
      "updating\n",
      "28 pole features not found in sample features\n",
      "updating\n",
      "12 pole features not found in sample features\n",
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "0 pole features not found in sample features\n",
      "updating\n",
      "10 pole features not found in sample features\n",
      "updating\n",
      "14 pole features not found in sample features\n",
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "10 pole features not found in sample features\n",
      "updating\n",
      "15 pole features not found in sample features\n",
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "27 pole features not found in sample features\n",
      "updating\n",
      "7 pole features not found in sample features\n",
      "updating\n",
      "10 pole features not found in sample features\n",
      "updating\n",
      "33 pole features not found in sample features\n",
      "updating\n",
      "23 pole features not found in sample features\n",
      "updating\n",
      "13 pole features not found in sample features\n",
      "updating\n",
      "17 pole features not found in sample features\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'1989': 0.028666814537318012,\n",
       " '1985': 0.03411295889677604,\n",
       " '2006': 0.016904539879532627,\n",
       " '1977': 0.053331064850174255,\n",
       " '2018': 0.03291744722724784,\n",
       " '1983': 0.01751297346187136,\n",
       " '1972': 0.03655227900166918,\n",
       " '1988': 0.03215295078751239,\n",
       " '2004': 0.013893357861687987,\n",
       " '2002': 0.014969646755538123,\n",
       " '2010': 0.031198108265804502,\n",
       " '1982': 0.04521168265918178,\n",
       " '1976': 0.04576085494124103,\n",
       " '2016': 0.03289166281269663,\n",
       " '1973': 0.05706279698614031,\n",
       " '1978': 0.024967766403796138,\n",
       " '1996': 0.01667469548753219,\n",
       " '1990': 0.026615269788044304,\n",
       " '2008': 0.030638640216348486,\n",
       " '2012': 0.03001906887055811,\n",
       " '1987': 0.03584595482288138,\n",
       " '1993': 0.0314343394696634,\n",
       " '1991': 0.02731887389560888,\n",
       " '1984': 0.04218896338804029,\n",
       " '2000': 0.015484248871896268,\n",
       " '1975': 0.02871663384857438,\n",
       " '1998': 0.026917453791723,\n",
       " '2014': 0.027777536050700075,\n",
       " '1986': 0.008699365568925753,\n",
       " '1974': 0.05564955589480665,\n",
       " '1994': 0.01895786140551179,\n",
       " '1980': 0.04424879201595486}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for year, qnet in qnets_by_year.items():\n",
    "    qnet = '../../creed2_/GSS/Qnets/' + qnet\n",
    "    data_obj=dataFormatter('../../creed2_/GSS/data/processed_data/'+data_by_year[year])\n",
    "    model_obj = model()\n",
    "    model_obj.load(qnet)\n",
    "    cognet_obj = cg()\n",
    "    cognet_obj.load_from_model(model_obj, data_obj, 'all')\n",
    "\n",
    "    # produce stats on how many column names actually match\n",
    "    stats = cognet_obj.set_poles(POLEFILE,\"R\",\"L\",steps=120)\n",
    "\n",
    "    # compute polar distance matrix\n",
    "    dmatrix = cognet_obj.polar_separation(nsteps=0)\n",
    "    polarDist_by_year[year]=dmatrix[0][1]\n",
    "polarDist_by_year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c144763",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7f8c999729b0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 59168 characters]\n",
      "text/plain": [
       "<Figure size 1440x576 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sorted_dist = collections.OrderedDict(sorted(polarDist_by_year.items()))\n",
    "df = pd.DataFrame.from_dict(sorted_dist, orient='index')\n",
    "df.reset_index(level=0, inplace=True)\n",
    "df.columns = ['year', 'dist']\n",
    "\n",
    "sns.set(style='darkgrid', rc={'figure.figsize':(20, 8)})\n",
    "sns.lineplot(x='year', y='dist', data=df)\n",
    "    \n",
    "# year = list(sorted_dist.keys())\n",
    "# dist = list(sorted_dist.values())     \n",
    "# plt.plot(year, dist)\n",
    "# plt.show"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qnet-dev",
   "language": "python",
   "name": "qnet-dev"
  },
  "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
