{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:42.636088458Z",
     "start_time": "2023-10-31T18:00:42.591871078Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "class InputSet:\n",
    "    def __init__(self, height, width, ):\n",
    "        self.event_bitrate = None\n",
    "        self.adder_features = np.zeros((height, width), dtype=bool)\n",
    "        self.adder_time = None\n",
    "        self.cv_features = np.zeros((height, width), dtype=bool)\n",
    "        self.cv_time = None\n",
    "        self.psnr = None\n",
    "        self.mse = None\n",
    "        self.ssim = None\n",
    "        self.precision = None\n",
    "        self.recall = None\n",
    "        self.accuracy = None\n",
    "\n",
    "    def total_adder(self):\n",
    "        return np.sum(self.adder_features.astype(int))\n",
    "\n",
    "    def total_cv(self):\n",
    "        return np.sum(self.cv_features.astype(int))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "629c4e84518a93a5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:42.636377043Z",
     "start_time": "2023-10-31T18:00:42.632892020Z"
    }
   },
   "outputs": [],
   "source": [
    "def process_entry(entry, input_sets):\n",
    "    if 's' in entry:\n",
    "        if \"OpenCV\" in entry['s']:\n",
    "            input_sets[-1].cv_features[entry['y'], entry['x']] = True\n",
    "        elif \"ADDER\" in entry['s']:\n",
    "            input_sets[-1].adder_features[entry['y'], entry['x']] = True\n",
    "        else:\n",
    "            raise Exception(\"Malformed entry\")\n",
    "    elif 'psnr' in entry:\n",
    "        input_sets[-1].psnr = entry['psnr']\n",
    "        input_sets[-1].mse = entry['mse']\n",
    "        input_sets[-1].ssim = entry['ssim']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad60f4aad52fd50f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:51.266275705Z",
     "start_time": "2023-10-31T18:00:42.633063760Z"
    }
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "objects = []\n",
    "with (\n",
    "        open(\n",
    "            \"/home/andrew/Code/adder-codec-rs/adder-codec-rs/9_raw_VIRAT_S_050201_05_000890_000944__31_10_2023_13_37_00.log\",\n",
    "            \"rb\")) as openfile:\n",
    "    # Read the dimensions\n",
    "    dims = openfile.readline().decode('UTF-8')\n",
    "    print(dims)\n",
    "    width = int(dims.split('x')[0])\n",
    "    height = int(dims.split('x')[1])\n",
    "    channels = int(dims.split('x')[2])\n",
    "    #\n",
    "    dbg_lines = 0\n",
    "    #\n",
    "    input_interval_idx = 0\n",
    "    #\n",
    "    input_sets = [InputSet(height, width)]\n",
    "    recon_sets = [InputSet(height, width)]\n",
    "\n",
    "    while True:\n",
    "        try:\n",
    "            entry = pickle.load(openfile)\n",
    "            if type(entry) == str and \"ADDER FAST:\" in entry:\n",
    "                # The start of a new interval\n",
    "                time = entry.split(': ')[1]\n",
    "                input_sets[-1].adder_time = int(time)\n",
    "                continue\n",
    "            elif type(entry) == str and \"bps:\" in entry:\n",
    "                # The bitrate\n",
    "                # The end of the interval\n",
    "                bitrate = float(entry.split(': ')[1]) / 1024.0 / 1024.0\n",
    "                input_sets[-1].event_bitrate = bitrate\n",
    "\n",
    "                input_sets += [InputSet(height, width)]\n",
    "                input_interval_idx += 1\n",
    "                continue\n",
    "            elif type(entry) == str and \"OpenCV FAST:\" in entry:\n",
    "                time = entry.split(': ')[1]\n",
    "                input_sets[-1].cv_time = int(time)\n",
    "                \n",
    "            elif type(entry) == str and \"Feature results:\" in entry:\n",
    "                entry = pickle.load(openfile)\n",
    "                input_sets[-1].precision = entry\n",
    "                entry = pickle.load(openfile)\n",
    "                input_sets[-1].recall = entry\n",
    "                entry = pickle.load(openfile)\n",
    "                input_sets[-1].accuracy = entry\n",
    "\n",
    "            elif type(entry) == str and \"META:\" in entry:\n",
    "                print(entry)\n",
    "            elif type(entry) == str and \"RECONSTRUCTION\" in entry:\n",
    "                print(\"got recon\")\n",
    "                break\n",
    "            else:\n",
    "                process_entry(entry, input_sets)\n",
    "\n",
    "                # print(entry)\n",
    "                dbg_lines += 1\n",
    "                # if dbg_lines == 100000:\n",
    "                #     break\n",
    "        except pickle.UnpicklingError:\n",
    "            line = openfile.readline().decode('UTF-8')\n",
    "            if \"Ticks per second\" in line:\n",
    "                tps = int(line.split(': ')[2])\n",
    "            elif \"ticks per source interval\" in line:\n",
    "                tpf = int(line.split(': ')[2])\n",
    "            print('hm')\n",
    "            \n",
    "            print(line)\n",
    "        except EOFError:\n",
    "            break\n",
    "            \n",
    "        # Read the reconstructed quality metrics\n",
    "    while True:\n",
    "        # break\n",
    "        try:\n",
    "            print('a')\n",
    "            entry = pickle.load(openfile)\n",
    "            print(entry)\n",
    "            process_entry(entry, recon_sets)\n",
    "            recon_sets += [InputSet(height, width)]\n",
    "        except EOFError:\n",
    "            print('eof')\n",
    "            break\n",
    "\n",
    "    # Remove the last item because it might have none values\n",
    "    input_sets = input_sets[:-1]\n",
    "    recon_sets = recon_sets[:-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c9a5c629fab07f1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:51.542875458Z",
     "start_time": "2023-10-31T18:00:51.251760327Z"
    }
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import mpld3\n",
    "\n",
    "adder_feature_count = [obj.total_adder() for obj in input_sets]\n",
    "cv_feature_count = [obj.total_cv() for obj in input_sets]\n",
    "\n",
    "sns.set(style=\"whitegrid\")\n",
    "plt.figure(figsize=(8, 4))\n",
    "\n",
    "sns.lineplot(x=range(len(input_sets) - 1), y=adder_feature_count[:-1], marker=None, label=\"ADDER\")\n",
    "sns.lineplot(x=range(len(input_sets) - 1), y=cv_feature_count[:-1], marker=None, label=\"OpenCV\")\n",
    "plt.ylabel(\"# features\")\n",
    "plt.xlabel(\"Input frame\")\n",
    "plt.title(\"Feature count over time\")\n",
    "\n",
    "plt.show()\n",
    "\n",
    "\n",
    "average_precision = np.mean(np.nan_to_num(np.array([obj.precision for obj in input_sets]), nan=1.0))\n",
    "print(average_precision)\n",
    "average_recall = np.mean(np.array([obj.recall for obj in input_sets]))\n",
    "print(average_recall)\n",
    "average_accuracy = np.mean(np.array([obj.accuracy for obj in input_sets]))\n",
    "print(average_accuracy)\n",
    "# interactive_plot = mpld3.display()\n",
    "# interactive_plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38c70d6a3c5ffd2c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:52.016568955Z",
     "start_time": "2023-10-31T18:00:51.555530466Z"
    }
   },
   "outputs": [],
   "source": [
    "adder_times = [obj.adder_time for obj in input_sets]\n",
    "cv_times = [obj.cv_time for obj in input_sets]\n",
    "\n",
    "sns.set(style=\"whitegrid\")\n",
    "plt.figure(figsize=(8, 4))\n",
    "\n",
    "sns.lineplot(x=range(len(input_sets)), y=adder_times, marker=None, label=\"ADDER\")\n",
    "sns.lineplot(x=range(len(input_sets)), y=cv_times, marker=None, label=\"OpenCV\")\n",
    "plt.ylabel(\"Time (ns)\")\n",
    "plt.xlabel(\"Input frame\")\n",
    "plt.yscale('log')\n",
    "plt.title(\"Feature detection speed\")\n",
    "\n",
    "plt.show()\n",
    "# interactive_plot = mpld3.display()\n",
    "# interactive_plot\n",
    "\n",
    "\n",
    "cv_mean = np.mean(np.array(cv_times))\n",
    "adder_mean = np.mean(np.array(adder_times))\n",
    "percent_change = (adder_mean / cv_mean) - 1\n",
    "print('OpenCV mean:', cv_mean, '  Median:', np.median(np.array(cv_times)))\n",
    "print('ADDER mean:', adder_mean, \"({:.1f}%)\".format(percent_change * 100), '  Median:',\n",
    "      np.median(np.array(adder_times)))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a760237d6daf6438",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Bitrate over time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fe4db991a37017c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:52.225264638Z",
     "start_time": "2023-10-31T18:00:52.032484237Z"
    }
   },
   "outputs": [],
   "source": [
    "adder_bitrates = [obj.event_bitrate for obj in input_sets]\n",
    "\n",
    "total_adder_size = np.sum(adder_bitrates) / (tps / tpf)\n",
    "print(total_adder_size, \"Mb\")\n",
    "\n",
    "source_raw_Mbps = (width * height * channels * (tps / tpf) * 8.0) / 1024.0 / 1024.0\n",
    "total_raw_size = (width * height * channels * len(adder_bitrates) * 8.0) / 1024.0 / 1024.0\n",
    "print(total_raw_size, \"Mb\")\n",
    "framed_bitrates = [source_raw_Mbps for obj in input_sets]\n",
    "\n",
    "sns.set(style=\"whitegrid\")\n",
    "plt.figure(figsize=(8, 4))\n",
    "\n",
    "sns.lineplot(x=range(len(input_sets)), y=adder_bitrates, marker=None, label=\"ADDER\")\n",
    "sns.lineplot(x=range(len(input_sets)), y=framed_bitrates, marker=None, label=\"Framed\")\n",
    "plt.ylabel(\"Raw bitrate (Mb/s)\")\n",
    "plt.xlabel(\"Input frame\")\n",
    "# plt.yscale('log')\n",
    "plt.title(\"Bitrate\")\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1f51d4ef8c04e81",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-31T18:00:52.461723452Z",
     "start_time": "2023-10-31T18:00:52.239098642Z"
    }
   },
   "outputs": [],
   "source": [
    "adder_psnr = [obj.psnr for obj in input_sets]\n",
    "adder_ssim = [obj.ssim for obj in input_sets]\n",
    "\n",
    "sns.set(style=\"whitegrid\")\n",
    "plt.figure(figsize=(8, 4))\n",
    "\n",
    "sns.lineplot(x=range(len(input_sets)), y=adder_psnr, marker=None, label=\"PSNR\")\n",
    "sns.lineplot(x=range(len(input_sets)), y=adder_ssim, marker=None, label=\"SSIM\")\n",
    "plt.ylabel(\"Score\")\n",
    "plt.xlabel(\"Input frame\")\n",
    "# plt.yscale('log')\n",
    "plt.title(\"Quality metrics\")\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "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
}