{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipfml import processing\n",
    "from ipfml import utils\n",
    "from PIL import Image\n",
    "from scipy import signal\n",
    "from skimage import color\n",
    "import scipy.stats as stats\n",
    "import seaborn as sns\n",
    "import cv2\n",
    "import numpy as np=\n",
    "import matplotlib.pyplot as plt\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_folder = \"../fichiersSVD_light\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MSCN analysis on Synthesis Images "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utils functions definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_images_path(scene, prefix, indices):\n",
    "    images_path = []\n",
    "    for index in indices:\n",
    "        path = os.path.join(data_folder, os.path.join(scene, prefix + index + \".png\"))\n",
    "        print(path)\n",
    "        images_path.append(path)\n",
    "    return images_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_mscn_distribution(images_path):\n",
    "    \n",
    "    images = []\n",
    "    scene_name = images_path[0].split('/')[2]\n",
    "    \n",
    "    # compute mscn coefficients for each image\n",
    "    for id, path in enumerate(images_path):\n",
    "        #utils.divide_in_blocks()\n",
    "        images.append(processing.get_mscn_coefficients(path))\n",
    "\n",
    "    sns.set(rc={'figure.figsize':(20,15)})\n",
    "    plt.rc('legend', fontsize=14) \n",
    "    plt.rc('axes', titlesize=14) \n",
    "\n",
    "    for id, img in enumerate(images):\n",
    "        h = img.flatten()\n",
    "        lm = sns.kdeplot(h, bw=.2, label=scene_name + \"_\" + image_indices[id])\n",
    "        lm.set_xlim(-0.4, 0.4)\n",
    "        print(image_indices[id], \" => \", np.var(h))\n",
    "    \n",
    "    plt.legend();\n",
    "    plt.savefig(scene_name + '_mscn_distribution.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_sub_blocks_mscn_variance(images_path, block_size=(200, 200)):\n",
    "    # study sub blocks variance for each noise level\n",
    "    images_data = []\n",
    "    scene_name = images_path[0].split('/')[2]\n",
    "    \n",
    "    plt.rc('legend', fontsize=14) \n",
    "    plt.rc('axes', titlesize=14) \n",
    "    # compute mscn coefficients for each image\n",
    "    for id, path in enumerate(images_path):\n",
    "        blocks = processing.divide_in_blocks(Image.open(path), block_size)\n",
    "    \n",
    "        data = []\n",
    "    \n",
    "        for block in blocks:\n",
    "            mscn_coeff = processing.get_mscn_coefficients(block)\n",
    "            data.append(np.var(mscn_coeff))\n",
    "        \n",
    "        images_data.append(data)\n",
    "    \n",
    "    for id, data in enumerate(images_data):\n",
    "        data = np.asarray(data)\n",
    "        indices = data.argsort()[-4:][::-1]\n",
    "        plt.plot(np.sort(data[indices]), label=scene_name + \"_\" + image_indices[id])\n",
    "        print(np.sum(data))\n",
    "        #lm.set_ylim(0, 0.02)\n",
    "    \n",
    "    plt.legend();\n",
    "    plt.savefig(scene_name + '_sub_blocks_mscn_variance.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scenes MSCN variance analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Appart01 scene "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "scene_name = \"Appart1opt02\"\n",
    "prefix_name = \"appartAopt_\"\n",
    "image_indices = [\"00020\", \"00050\", \"00100\", \"00200\", \"00300\", \"00500\", \"00900\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../fichiersSVD_light/Appart1opt02/appartAopt_00020.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00050.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00100.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00200.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00300.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00500.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00900.png\n"
     ]
    }
   ],
   "source": [
    "images_path = compute_images_path(scene_name, prefix_name, image_indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00020  =>  0.0013007368923888193\n",
      "00050  =>  0.0008614542685043583\n",
      "00100  =>  0.000719609540073056\n",
      "00200  =>  0.0006473836020377824\n",
      "00300  =>  0.0006256486440919737\n",
      "00500  =>  0.0006071633806632832\n",
      "00900  =>  0.0005957700480215396\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 101348 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_mscn_distribution(images_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.029858769841469\n",
      "0.025388548632194835\n",
      "0.0236218981403041\n",
      "0.022487000913283282\n",
      "0.02209157796963592\n",
      "0.021781000866678935\n",
      "0.021454595180229484\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 143208 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_sub_blocks_mscn_variance(images_path, (200, 200))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Cuisine01 analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "scene_name = \"Cuisine01\"\n",
    "prefix_name = \"cuisine01_\"\n",
    "image_indices = [\"00050\", \"00100\", \"00200\", \"00300\", \"00500\", \"00700\", \"01200\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../fichiersSVD_light/Cuisine01/cuisine01_00050.png\n",
      "../fichiersSVD_light/Cuisine01/cuisine01_00100.png\n",
      "../fichiersSVD_light/Cuisine01/cuisine01_00200.png\n",
      "../fichiersSVD_light/Cuisine01/cuisine01_00300.png\n",
      "../fichiersSVD_light/Cuisine01/cuisine01_00500.png\n",
      "../fichiersSVD_light/Cuisine01/cuisine01_00700.png\n",
      "../fichiersSVD_light/Cuisine01/cuisine01_01200.png\n"
     ]
    }
   ],
   "source": [
    "images_path = compute_images_path(scene_name, prefix_name, image_indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00050  =>  0.0009676431057037502\n",
      "00100  =>  0.0008311065862023871\n",
      "00200  =>  0.0007549075717485793\n",
      "00300  =>  0.0007261569074363732\n",
      "00500  =>  0.0007007603214148727\n",
      "00700  =>  0.0006893560383379428\n",
      "01200  =>  0.0006775955930850441\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 97476 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_mscn_distribution(images_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.019864530267821087\n",
      "0.01784888777150448\n",
      "0.016725939339642525\n",
      "0.01630845135274984\n",
      "0.015940405284697114\n",
      "0.01577934050802251\n",
      "0.015609827896335627\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 156188 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_sub_blocks_mscn_variance(images_path, (200, 200))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SdbCentre scene "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "scene_name = \"SdbCentre\"\n",
    "prefix_name = \"SdB2_\"\n",
    "image_indices = [\"00020\", \"00050\", \"00100\", \"00200\", \"00300\", \"00500\", \"00950\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../fichiersSVD_light/SdbCentre/SdB2_00020.png\n",
      "../fichiersSVD_light/SdbCentre/SdB2_00050.png\n",
      "../fichiersSVD_light/SdbCentre/SdB2_00100.png\n",
      "../fichiersSVD_light/SdbCentre/SdB2_00200.png\n",
      "../fichiersSVD_light/SdbCentre/SdB2_00300.png\n",
      "../fichiersSVD_light/SdbCentre/SdB2_00500.png\n",
      "../fichiersSVD_light/SdbCentre/SdB2_00950.png\n"
     ]
    }
   ],
   "source": [
    "images_path = compute_images_path(scene_name, prefix_name, image_indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00020  =>  0.0013007368923888193\n",
      "00050  =>  0.0008614542685043583\n",
      "00100  =>  0.000719609540073056\n",
      "00200  =>  0.0006473836020377824\n",
      "00300  =>  0.0006256486440919737\n",
      "00500  =>  0.0006071633806632832\n",
      "00900  =>  0.0005957700480215396\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 101348 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_mscn_distribution(images_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.029858769841469\n",
      "0.025388548632194835\n",
      "0.0236218981403041\n",
      "0.022487000913283282\n",
      "0.02209157796963592\n",
      "0.021781000866678935\n",
      "0.021454595180229484\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 280400 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_sub_blocks_mscn_variance(images_path, (200, 200))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "thesis-venv",
   "language": "python",
   "name": "thesis-venv"
  },
  "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.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
