{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import ck.kernel as ck\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib as matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print \"Collective Knowledge: v%s\" % ck.version({})['version_str']\n",
    "print \"pandas: v%s\" % pd.__version__\n",
    "print \"NumPy: v%s\" % np.version.version\n",
    "print \"Matplotlib: v%s\" % matplotlib.__version__\n",
    "print \"JSON: v%s\" % json.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dataset = 'SGEMM_NT'\n",
    "data_uoa =  dataset + '-explore-f-n'\n",
    "module_uoa = 'experiment'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "r=ck.access({'action':'list_points', 'module_uoa':module_uoa, 'data_uoa':data_uoa})\n",
    "if r['return']>0:\n",
    "  print (\"Error: %s\" % r['error'])\n",
    "  exit(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Show results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data_list  = []\n",
    "index_list = []\n",
    "\n",
    "for point in r['points']:\n",
    "    with open(os.path.join(r['path'], 'ckp-%s.flat.json' % point)) as point_file:\n",
    "        point_data = json.load(point_file)\n",
    "    # Data.    \n",
    "    diff = point_data.get(\"##characteristics#run#run_time_state#RESULTS#max_abs_diff#all\")\n",
    "    match = point_data.get(\"##characteristics#run#run_time_state#RESULTS#match#all\")\n",
    "    diff_match = [x for t in zip(diff, match) for x in t]\n",
    "    data_list.append(diff_match)\n",
    "    \n",
    "    # Row index.\n",
    "    cl_file = point_data.get(\"##characteristics#run#run_time_state#METADATA#file#all_unique\")[0]\n",
    "    matrix_order = point_data.get(\"##characteristics#run#run_time_state#CMD_LINE_ARGS#matrix_order#all_unique\")[0]\n",
    "    index_list.append((cl_file, matrix_order))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "num_repetitions = 4 # TODO: get from points\n",
    "cols_i = ['Max abs diff', 'Match?'] * num_repetitions\n",
    "rows_mi = pd.MultiIndex.from_tuples(names=['OpenCL program', 'Matrix order'], tuples=index_list)\n",
    "df = pd.DataFrame(data=data_list, index=rows_mi, columns=cols_i).sortlevel()\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Dump to file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rows_mi_tex = pd.MultiIndex.from_tuples(tuples=index_list)\n",
    "df_tex = pd.DataFrame(data=data_list, index=rows_mi_tex, columns=cols_i).sortlevel()\n",
    "with open('%s-match_tmp.tex' % data_uoa, 'w') as tex_file:\n",
    "    tex_file.write(df_tex.to_latex())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
