{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "educational-investment",
   "metadata": {},
   "source": [
    "# Linear Convolutional Network Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "representative-heating",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import seaborn as sns\n",
    "from matplotlib.colors import LogNorm\n",
    "Cout = [1, 2, 4, 8]\n",
    "T = 10\n",
    "\n",
    "# Read arrays\n",
    "def get_betas(pair):\n",
    "    beta_trials = []\n",
    "    for t in range(T):\n",
    "        betas = []\n",
    "        name = \"experiments-data/\" + \"betas-linear\" + str(pair) + str(t)+ \".csv\"\n",
    "        beta = pd.read_csv(name, header=None).values.astype(np.float)\n",
    "        beta_trials.append(beta)\n",
    "    return beta_trials\n",
    "\n",
    "def get_rbetas(pair):\n",
    "    name =  \"experiments-data/\" + str(pair) + \"rbetas-linear\" + \".csv\"   \n",
    "    rbetas = pd.read_csv(name, header=None).values.astype(np.float)\n",
    "    return rbetas\n",
    "\n",
    "def get_betas_all(k):\n",
    "    betas_all = []\n",
    "    for c in Cout:\n",
    "        pair = (k, c)\n",
    "        beta = get_betas(pair)\n",
    "        betas_all.append(beta)\n",
    "    return betas_all\n",
    "\n",
    "def get_rbetas_all(k):\n",
    "    rbetas_all = []\n",
    "    for c in Cout:\n",
    "        pair = (k, c)\n",
    "        rbetas = get_rbetas(pair)\n",
    "        rbetas_all.append(rbetas)\n",
    "    return rbetas_all\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "aboriginal-stopping",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.35\\pm 4.01e-02\n",
      "3.3\\pm 3.58e-02\n",
      "3.29\\pm 1.46e-02\n",
      "3.29\\pm 1.83e-02\n"
     ]
    }
   ],
   "source": [
    "# Get rbeta values\n",
    "k = 8\n",
    "rbetas_all = get_rbetas_all(k)\n",
    "\n",
    "means = []\n",
    "variances = []\n",
    "for i in range(len(Cout)):\n",
    "    sorted_vec = np.sort(rbetas_all[i])\n",
    "#     print(\"Cout=\" + str(i) + \": rbetas are \" + str(sorted_vec))\n",
    "    means.append(np.mean(rbetas_all[i]))\n",
    "    variances.append(np.var(rbetas_all[i]))\n",
    "    \n",
    "for i in range(len(Cout)):\n",
    "    print(str(np.round(means[i], 2)) + \"\\pm \" + str(np.format_float_scientific(np.sqrt(variances)[i], exp_digits=2, precision=2)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "satisfactory-extreme",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 23096 characters]\n",
      "text/plain": [
       "<Figure size 1440x720 with 10 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot betas\n",
    "betas_all = []\n",
    "ks = [1,3, 8, 16, 28]\n",
    "for k in ks:\n",
    "    betas = get_sample_betas(k)\n",
    "    betas_all.append(betas)\n",
    "\n",
    "print(len(betas_all))\n",
    "\n",
    "cout = 0\n",
    "def plot():\n",
    "    beta_to_plot_time = []\n",
    "    beta_to_plot_freq = []\n",
    "    for k in range(len(ks)):\n",
    "        beta = np.copy(betas_all[k][cout])\n",
    "        beta_to_plot_time.append(beta)\n",
    "#         print(beta.shape)\n",
    "#         print(np.fft.fft2(beta, norm='ortho').shape)\n",
    "        beta_to_plot_freq.append(np.abs(np.fft.fft2(beta, norm='ortho')))\n",
    "\n",
    "    # Compute beta for linear CNNs\n",
    "    sns.set_context(\"paper\",font_scale=3, rc={\"lines.linewidth\":1})\n",
    "    fig, axs = plt.subplots(2, 5, figsize=(20,10))\n",
    "    for k in range(len(ks)):\n",
    "        axs[0][k].imshow(beta_to_plot_time[k],cmap='gray')\n",
    "        axs[0][k].set_xticklabels([])\n",
    "        axs[0][k].set_yticklabels([])\n",
    "        \n",
    "        axs[1][k].imshow(beta_to_plot_freq[k],cmap='gray', norm=LogNorm(vmin=0.0001, vmax=0.08))\n",
    "        axs[1][k].set_xticklabels([])\n",
    "        axs[1][k].set_yticklabels([])\n",
    "\n",
    "    plt.tight_layout() # for some reason not using tight_layout before savefig messes some borders up\n",
    "    name = \"plots/many-channels\" + str(Cout[cout]) + \".pdf\"\n",
    "    plt.savefig(name, format='pdf')\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "trained-silence",
   "metadata": {},
   "source": [
    "# Nonlinear Convolutional Network Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "lyric-forge",
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 3\n",
    "def get_rbetas_nonlinear_bias(pair):\n",
    "    name =  \"experiments-data/\" + str(pair) + \"rbetas-nonlinear-bias\" + \".csv\"   \n",
    "    rbetas = pd.read_csv(name, header=None).values.astype(np.float)\n",
    "    return rbetas\n",
    "\n",
    "\n",
    "def get_rbetas_all_nonlinear_bias(k):\n",
    "    rbetas_all = []\n",
    "    for c in Cout:\n",
    "        pair = (k, c)\n",
    "        rbetas = get_rbetas_nonlinear_bias(pair)\n",
    "        rbetas_all.append(rbetas)\n",
    "    return rbetas_all\n",
    "\n",
    "\n",
    "def get_rbetas_nonlinear(pair):\n",
    "    name =  \"experiments-data/\" + str(pair) + \"rbetas-nonlinear\" + \".csv\"\n",
    "    rbetas = pd.read_csv(name, header=None).values.astype(np.float)\n",
    "    return rbetas\n",
    "\n",
    "\n",
    "def get_rbetas_all_nonlinear(k):\n",
    "    rbetas_all = []\n",
    "    for c in Cout:\n",
    "        pair = (k, c)\n",
    "        rbetas = get_rbetas_nonlinear(pair)\n",
    "        rbetas_all.append(rbetas)\n",
    "    return rbetas_all\n",
    "\n",
    "\n",
    "def get_medians_nonlinear(k):\n",
    "    rbetas_all = get_rbetas_all_nonlinear(k)\n",
    "    medians = []\n",
    "\n",
    "    for i in range(len(Cout)):\n",
    "        sorted_vec = np.sort(rbetas_all[i])\n",
    "        print(\"Cout=\" + str(i) + \": rbetas are \" + str(sorted_vec))\n",
    "        medians.append(np.median(rbetas_all[i]))\n",
    "    \n",
    "    for i in range(len(Cout)):\n",
    "        print(str(np.round(medians[i], 3)))\n",
    "        \n",
    "def get_medians_nonlinear_bias(k):\n",
    "    rbetas_all = get_rbetas_all_nonlinear_bias(k)\n",
    "    medians = []\n",
    "\n",
    "    for i in range(len(Cout)):\n",
    "        sorted_vec = np.sort(rbetas_all[i])\n",
    "        print(\"Cout=\" + str(i) + \": rbetas are \" + str(sorted_vec))\n",
    "        medians.append(np.median(rbetas_all[i]))\n",
    "\n",
    "    for i in range(len(Cout)):\n",
    "        print(str(np.round(medians[i], 3)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "interpreted-engagement",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "With bias\n",
      "Cout=0: rbetas are [[22.98060067]\n",
      " [10.56706548]\n",
      " [10.57813307]\n",
      " [19.11011389]\n",
      " [10.58097863]]\n",
      "Cout=1: rbetas are [[10.56908209]\n",
      " [10.58230089]\n",
      " [10.57123763]\n",
      " [10.56794842]\n",
      " [10.58065839]]\n",
      "Cout=2: rbetas are [[20.00965623]\n",
      " [10.5775848 ]\n",
      " [10.57738424]\n",
      " [10.57281013]\n",
      " [10.58207472]]\n",
      "Cout=3: rbetas are [[10.57260352]\n",
      " [10.57549358]\n",
      " [10.58131186]\n",
      " [10.57604943]\n",
      " [10.58040739]]\n",
      "10.581\n",
      "10.571\n",
      "10.578\n",
      "10.576\n",
      "Without bias\n",
      "Cout=0: rbetas are [[ 11.4118916 ]\n",
      " [ 11.41177177]\n",
      " [109.47130905]\n",
      " [ 11.41195909]\n",
      " [ 11.41184855]]\n",
      "Cout=1: rbetas are [[ 11.41387834]\n",
      " [ 11.41290243]\n",
      " [109.47386582]\n",
      " [ 11.41193824]\n",
      " [ 11.41315423]]\n",
      "Cout=2: rbetas are [[11.41386882]\n",
      " [11.4139749 ]\n",
      " [11.41703692]\n",
      " [11.41647683]\n",
      " [11.41439695]]\n",
      "Cout=3: rbetas are [[11.41304968]\n",
      " [11.4195499 ]\n",
      " [11.41361214]\n",
      " [11.41484352]\n",
      " [11.41579989]]\n",
      "11.412\n",
      "11.413\n",
      "11.414\n",
      "11.415\n"
     ]
    }
   ],
   "source": [
    "k = 1\n",
    "\n",
    "print(\"With bias\")\n",
    "get_medians_nonlinear_bias(k)\n",
    "\n",
    "print(\"Without bias\")\n",
    "get_medians_nonlinear(k)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "rough-kingston",
   "metadata": {},
   "source": [
    "# CVXPY plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "extraordinary-stopping",
   "metadata": {},
   "outputs": [],
   "source": [
    "name =  \"experiments-data/cvxpy-ell2\"\n",
    "beta_ell2 = pd.read_csv(name, header=None).values.astype(np.float)\n",
    "\n",
    "name =  \"experiments-data/cvxpy-ell1\"\n",
    "beta_ell1 = pd.read_csv(name, header=None).values.astype(np.float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "correct-delaware",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16056 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "beta_to_plot_time = []\n",
    "beta_to_plot_freq = []\n",
    "beta_to_plot_time.append(beta_ell2)\n",
    "beta_to_plot_freq.append(np.abs(np.fft.fft2(beta_ell2, norm='ortho')))\n",
    "\n",
    "beta_to_plot_time.append(beta_ell1)\n",
    "beta_to_plot_freq.append(np.abs(np.fft.fft2(beta_ell1, norm='ortho')))\n",
    "\n",
    "# Compute beta for linear CNNs\n",
    "sns.set_context(\"paper\",font_scale=3, rc={\"lines.linewidth\":1})\n",
    "fig, axs = plt.subplots(2, 2, figsize=(10,10))\n",
    "for k in range(2):\n",
    "    axs[0][k].imshow(beta_to_plot_time[k],cmap='gray')\n",
    "    axs[0][k].set_xticklabels([])\n",
    "    axs[0][k].set_yticklabels([])\n",
    "\n",
    "    axs[1][k].imshow(beta_to_plot_freq[k],cmap='gray', norm=LogNorm(vmin=0.0001, vmax=0.08))\n",
    "    axs[1][k].set_xticklabels([])\n",
    "    axs[1][k].set_yticklabels([])\n",
    "\n",
    "plt.tight_layout() # for some reason not using tight_layout before savefig messes some borders up\n",
    "name = \"plots/cvxpy\" + \".pdf\"\n",
    "plt.savefig(name, format='pdf')\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "monthly-cameroon",
   "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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
