{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import ck.kernel as ck\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']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "r=ck.access({\n",
    "  'action'      : 'list_points',\n",
    "  'module_uoa'  : 'experiment',\n",
    "  'data_uoa'    : 'explore-clsmith-datasets-any'\n",
    "})\n",
    "\n",
    "if r['return']>0:\n",
    "  print (\"Error: %s\" % r['error'])\n",
    "  exit(1)\n",
    "\n",
    "path   = r['path']\n",
    "points = r['points']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Analyse results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "failures = {}\n",
    "failures['num_total'] = 0\n",
    "failures['num_timeouts'] = 0\n",
    "failures['num_nonuniques'] = 0\n",
    "failures['num_enqueue_errors'] = 0\n",
    "failures['num_assertions'] = 0\n",
    "failures['assertions'] = { }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def analyse(failures, point):\n",
    "    test   = point.get('##choices#dataset_file#min')\n",
    "    result = point.get('##characteristics#run#result_string#min')\n",
    "    reason = point.get('##characteristics#run#fail_reason#min')\n",
    "    stderr = point.get('##characteristics#run#stderr#min')\n",
    "    stdout = point.get('##characteristics#run#stdout#min')\n",
    "\n",
    "    num_unique    = len(set(result.split(','))) if result != None else 0\n",
    "    timeout_error = reason and (reason == 'process timed out and had been terminated')\n",
    "    enqueue_error = ('Error enqueueing kernel: -5' in stdout)\n",
    "    assertion     = ('Assertion' in stderr)\n",
    "\n",
    "    if num_unique > 1:\n",
    "        failures['num_nonuniques'] += 1\n",
    "\n",
    "    if timeout_error:\n",
    "        failures['num_timeouts'] += 1\n",
    "        \n",
    "    if enqueue_error:\n",
    "        failures['num_enqueue_errors'] += 1\n",
    "        \n",
    "    if assertion:\n",
    "        failures['num_assertions'] += 1\n",
    "        failures['assertions'][test] = stderr\n",
    "\n",
    "    print '== %s =='       % test\n",
    "    print 'unique results: %s' % num_unique\n",
    "    print 'failure reason: %s' % reason\n",
    "    print 'stderr: %s' % stderr\n",
    "    print 'stdout: %s' % stdout\n",
    "\n",
    "    if ((num_unique != 1) or (reason != None) or (stderr != '')):\n",
    "        failures['num_total'] += 1\n",
    "        print '== FAILED =='\n",
    "    else:\n",
    "        print '== PASSED =='\n",
    "        \n",
    "    print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Analyse each experiment point.\n",
    "for point in points:\n",
    "    with open(os.path.join(path, 'ckp-%s.flat.json' % point)) as point_file:\n",
    "        point_data = json.load(point_file)\n",
    "    analyse(failures, point_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Print summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 'TOTAL NUMBER OF FAILURES: %d' % failures['num_total']\n",
    "print '- NUMBER OF TIMEOUTS: %d' % failures['num_timeouts']\n",
    "print '- NUMBER OF NON-UNIQUE RESULTS: %d' % failures['num_nonuniques']\n",
    "print '- NUMBER OF ENQUEUE ERRORS: %d'% failures['num_enqueue_errors']\n",
    "print '- NUMBER OF COMPILER ASSERTIONS: %d' % failures['num_assertions']\n",
    "if failures['num_assertions'] > 0:\n",
    "    print\n",
    "    print 'ALL COMPILER ASSERTIONS:'\n",
    "    for test, assertion in failures['assertions'].iteritems():\n",
    "        print '== %s ==' % test\n",
    "        print assertion"
   ]
  }
 ],
 "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
