{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-25T14:36:59.434554Z",
     "start_time": "2024-09-25T14:36:59.428793Z"
    }
   },
   "source": [
    "import pickle\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T14:36:59.670614Z",
     "start_time": "2024-09-25T14:36:59.640369Z"
    }
   },
   "cell_type": "code",
   "source": [
    "with open('../../storage/ilbpx91y/inference_data/synthetics_epoch=50_seed=0.pkl', 'rb') as f:\n",
    "    syns = pickle.load(f)\n",
    "with open('../../storage/ilbpx91y/inference_data/coefficients_epoch=50_seed=0.pkl', 'rb') as f:\n",
    "    coeffs = pickle.load(f)\n",
    "syns.shape, coeffs.shape"
   ],
   "id": "22869f153623332d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((8192, 30, 3), (8192, 3, 6, 30))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T14:36:44.942657Z",
     "start_time": "2024-09-25T14:36:44.937182Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def get_valmatrix_graph_strongest_all(coefficients: np.ndarray, keep_top: float, n_features, max_lag) -> (\n",
    "        np.ndarray, np.ndarray):\n",
    "    # coefficients.shape = [n_samples, n_features, n_features*max_lag, seq_len-max_lag]\n",
    "    n_samples = coefficients.shape[0]\n",
    "\n",
    "    c_pos = np.quantile(coefficients, q=.95, axis=-1)\n",
    "    c_neg = np.quantile(coefficients, q=.05, axis=-1)\n",
    "    c = np.asarray(\n",
    "        [\n",
    "            [p if p > abs(n) else n for p, n in zip(pos.flatten(), neg.flatten())]\n",
    "            for pos, neg in zip(c_pos, c_neg)\n",
    "        ]\n",
    "    )\n",
    "    c = c.reshape(n_samples, n_features, n_features, max_lag).transpose((0, 2, 1, 3))\n",
    "    c = np.flip(c, axis=-1)\n",
    "    z = np.zeros((n_samples, n_features, n_features, 1))\n",
    "    val_matrix = np.concatenate([z, c], axis=-1)\n",
    "    val_matrix = val_matrix.flatten()\n",
    "\n",
    "    threshold_pos = np.quantile(val_matrix, q=1 - keep_top)\n",
    "    threshold_neg = np.quantile(val_matrix, q=keep_top)\n",
    "\n",
    "    val_matrix = np.where(val_matrix > threshold_pos, 1., (np.where(val_matrix < threshold_neg, -1., 0.)))\n",
    "    # val_matrix.shape = [n_samples, n_features, n_features, max_lag + 1]\n",
    "    return val_matrix.reshape(n_samples, n_features, n_features, max_lag + 1)"
   ],
   "id": "16cae994120f61fa",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T14:37:34.749776Z",
     "start_time": "2024-09-25T14:37:34.444345Z"
    }
   },
   "cell_type": "code",
   "source": "val_matrix = get_valmatrix_graph_strongest_all(coeffs, .01, 3, 2)",
   "id": "f87b8eac415acaa2",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T14:37:35.687777Z",
     "start_time": "2024-09-25T14:37:35.547114Z"
    }
   },
   "cell_type": "code",
   "source": [
    "i = 14\n",
    "syn = syns[i]\n",
    "plt.plot(syn, label=['Dillingen', 'Kempten', 'Lenggries'], linewidth=3)\n",
    "plt.axvline(19, c='r', linestyle='--', linewidth=3)\n",
    "plt.xlabel('Time-steps', fontsize=18)\n",
    "plt.legend(fontsize=22)\n",
    "plt.tick_params('both', labelsize=16)\n",
    "plt.tight_layout()\n",
    "# plt.savefig(f'../../experiments/samples_with_graph/sample_i={i}.pdf')\n",
    "plt.show()\n",
    "plt.close()\n",
    "graph = val_matrix[i]\n",
    "graph"
   ],
   "id": "7877594eab663c34",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ],
      "image/png": "[encoded data, 57836 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[0., 1., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 1., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T11:29:58.448056Z",
     "start_time": "2024-09-25T11:29:58.284442Z"
    }
   },
   "cell_type": "code",
   "source": [
    "i = 17\n",
    "syn = syns[i]\n",
    "plt.plot(syn, label=['Dillingen', 'Kempten', 'Lenggries'], linewidth=3)\n",
    "plt.xlabel('Time-steps', fontsize=18)\n",
    "plt.legend(fontsize=22)\n",
    "plt.tick_params('both', labelsize=16)\n",
    "plt.tight_layout()\n",
    "# plt.savefig(f'../../experiments/samples_with_graph/sample_i={i}.pdf')\n",
    "plt.show()\n",
    "plt.close()\n",
    "graph = val_matrix[i]\n",
    "graph"
   ],
   "id": "1d0cf1d917e0b9c0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ],
      "image/png": "[encoded data, 44732 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "dc05da7c098fdf8b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
