sempre/interactive/generate_plots.ipynb

409 lines
16 KiB
Plaintext

{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"A line looks like this\n",
" \"time\": \"2017-01-21T05:31:57.474\",\n",
" \"id\": \"AMT_A1HKYY6XI2OHO1\",\n",
" \"log\": \"(:q \\\"repeat 10 [ Ebony Wall; select front]\\\")\",\n",
" \"stats.type\": \"q\",\n",
" \"stats.size\": 1,\n",
" \"stats.status\": \"Induced\",\n",
" \"queryCount\": 2707"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"import csv\n",
"import numpy as np\n",
"import pandas as pd \n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"import json\n",
"import os\n",
"import subprocess\n",
"from collections import OrderedDict\n",
"%matplotlib inline \n",
"\n",
"with open('../state/lastExec', 'rb') as lastExec:\n",
" lastExecInd = lastExec.readline().strip()\n",
"print lastExecInd\n",
" \n",
"rows = []; \n",
"execInd = lastExecInd;\n",
"execPath = '../state/execs/%s.exec/' % execInd\n",
"#print 'analyzing: ' + execPath\n",
"def printOptions():\n",
" with open(os.path.join(execPath,'options.map')) as optionsfile:\n",
" opts = filter(lambda l: 'file' in l or 'logFiles' in l or 'reqParams' in l, optionsfile.readlines());\n",
" for opt in opts: print opt.strip()\n",
" # egrep 'file|Simulator'\n",
"printOptions()\n",
"\n",
"with open('../state/execs/%s.exec/plotInfo.json' % execInd, 'rb') as jsonfile:\n",
" json_lines = jsonfile.readlines()\n",
"print '%d lines in plotInfo' % len(json_lines)\n",
"rows = [json.loads(l) for l in json_lines]\n",
"# rows = []\n",
"# for l in json_lines:\n",
"# try:\n",
"# rows += [json.loads(l)];\n",
"# except:\n",
"# print l\n",
"# accepts = [[r['queryCount'], 1 if r['stats.rank']>=0 else 0] for r in rows if r['stats.type'] == 'accept'];\n",
"\n",
"\n",
"def print_stats():\n",
" filtered_rows = [r for r in rows if r['stats.type'] == 'accept']\n",
" induced_rows = [r for r in rows if r['stats.type'] == 'accept' and r['stats.status']=='Induced']\n",
" core_rows = [r for r in rows if r['stats.type'] == 'accept' and r['stats.status']=='Core']\n",
" none_rows = [r for r in rows if r['stats.type'] == 'accept' and r['stats.status']=='Nothing']\n",
"\n",
" stats = {\\\n",
" 'accepted': len(filtered_rows), \\\n",
" 'induced': len(induced_rows), \\\n",
" 'inducedp':len(induced_rows)/float(len(filtered_rows)),\\\n",
" 'core': len(core_rows), \\\n",
" 'corep':len(core_rows)/float(len(filtered_rows)),\\\n",
" 'none': len(none_rows), \\\n",
" 'nonep':len(none_rows)/float(len(filtered_rows))\\\n",
" }\n",
" print 'total:{accepted}\\t induced:{induced}({inducedp:.4f}), core:{core}({corep:.4f}), none:{none}({nonep:.4f}),'.format(**stats)\n",
" #print 'check %f' % (stats['inducedp']+stats['corep']+stats['nonep'])\n",
" \n",
" statscorrect = {\n",
" 'accepted0': np.mean([1 if r['stats.rank']==0 and r['stats.status']!='Nothing' else 0 for r in filtered_rows]),\\\n",
" 'accepted1': np.mean([1 if r['stats.rank']>=0 and r['stats.status']!='Nothing' else 0 for r in filtered_rows]),\\\n",
" 'induced0': np.mean([1 if r['stats.rank']==0 and r['stats.status']=='Induced' else 0 for r in filtered_rows]),\\\n",
" 'induced1': np.mean([1 if r['stats.rank']>=0 and r['stats.status']=='Induced' else 0 for r in filtered_rows]),\\\n",
" 'core0': np.mean([1 if r['stats.rank']==0 and r['stats.status']=='Core' else 0 for r in filtered_rows]),\\\n",
" 'core1': np.mean([1 if r['stats.rank']>=0 and r['stats.status']=='Core' else 0 for r in filtered_rows])\\\n",
" }\n",
" print 'total:{accepted0:.4f}/{accepted1:.4f}, induced:{induced0:.4f}/{induced1:.4f}, core:{core0:.4f}/{core1:.4f}'.format(**statscorrect)\n",
"\n",
" token_types = set();\n",
" rows_types = [r for r in rows if r['stats.type'] == 'q'];\n",
" for r in rows_types:\n",
" token_types |= set(r['q'].split(' '))\n",
" print 'token_types %d out of %d' % (len(token_types), len(rows_types))\n",
" \n",
" \n",
"print_stats()\n",
"\n",
"\n",
"def plot_reset():\n",
" global p\n",
" p = {'color': 'r', 'linewidth': 2, 'alpha':0.5}\n",
" #, 'marker':'*', 'markersize':0.3}\n",
"plot_reset()\n",
"def savefig(filename = 'fig.pdf'):\n",
" plt.savefig(os.path.join(execPath,filename) , bbox_inches=\"tight\")\n",
"\n",
"def plot_cumavg(x, y, xlabel='query#', ylabel='recall', title=None):\n",
" y_cum = np.cumsum(y).tolist()\n",
" #print accepts_np[:,1]\n",
" y_cumavg = [cum / float(count+1) for count,cum in enumerate(y_cum)]\n",
" #N = 500;\n",
" #y_cumavg = np.convolve(np.array(y), np.ones((N,))/N, mode='same').tolist()\n",
"\n",
" #print accept_rate \n",
" \n",
" #plt.scatter(means_baseline[0:], means[0:], s=colors, alpha=0.8, c='r')\n",
" plt.plot(x, y_cumavg, **p)\n",
" \n",
" plt.xlabel(xlabel, fontsize=12)\n",
" plt.ylabel(ylabel, fontsize=12)\n",
" # plt.xlim(0, 0.65)\n",
" plt.ylim(0, max(y_cumavg)*1.02)\n",
" \n",
" xp = np.linspace(0, 0.65, 300)\n",
" \n",
" #plt.gca().set_aspect('equal', adjustable='box')\n",
" plottitle = title if title is not None else '%s_vs_%s.pdf' % (xlabel, ylabel)\n",
" # plt.savefig(os.path.join(execPath,plottitle) , bbox_inches=\"tight\")\n",
" \n",
"def print_avg(x, name = 'unnamed'):\n",
" print 'avg(%s): %f' % (name, reduce(lambda a,b: a+b, x) / float(len(x)));\n",
"def average_stat(stat = 'stats.size', type = 'accept'):\n",
" filtered_rows = [r for r in rows if r['stats.type'] == type]\n",
" query_counts = [r['queryCount'] for r in filtered_rows]\n",
" stats = [r[stat] for r in filtered_rows]\n",
" plot_cumavg(query_counts, stats, xlabel='query#', ylabel=stat.replace('stats.','').replace('size','# parses'));\n",
" print_avg(stats, stat)\n",
"plt.figure()\n",
"p['color'] = 'b'\n",
"average_stat(stat = 'stats.size')\n",
"#savefig('ambiguity.pdf')\n",
"\n",
"\n",
"def precent_status(filtered_rows, status = 'Core'):\n",
" query_counts = [r['queryCount'] for r in filtered_rows]\n",
" is_status = [100 if r['stats.status'] == status else 0 for r in filtered_rows]\n",
" print_avg(is_status, 'percent of status ' + status)\n",
" plot_cumavg(query_counts, is_status, xlabel='query #', ylabel='percent');\n",
"\n",
"\n",
"def plotCoreInducedNone(filtered_rows):\n",
" p['color'] = 'g'; p['label'] = 'induced';\n",
" precent_status(filtered_rows,status = 'Induced');\n",
" p['color'] = 'b'; p['label'] = 'core';\n",
" precent_status(filtered_rows, status = 'Core');\n",
" plt.legend(frameon=False)\n",
" \n",
"plt.figure()\n",
"plotCoreInducedNone([r for r in rows if r['stats.type'] == 'accept'])\n",
"# savefig('parse_status_accepted.pdf')\n",
"\n",
"def plotCoreInducedNone(filtered_rows):\n",
" p['color'] = 'r'; p['label'] = 'none';\n",
" precent_status(filtered_rows, status = 'Nothing');\n",
" p['color'] = 'g'; p['label'] = 'induced';\n",
" precent_status(filtered_rows,status = 'Induced');\n",
" p['color'] = 'b'; p['label'] = 'core';\n",
" precent_status(filtered_rows, status = 'Core');\n",
" plt.legend(frameon=False)\n",
"plt.figure()\n",
"plotCoreInducedNone([r for r in rows if r['stats.type'] == 'q'])\n",
"plt.ylim(0, 70)\n",
"savefig('parse_status_q.pdf')\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def percentCore():\n",
" allq = [r for r in rows if r['stats.type'] == 'accept'];\n",
" is_status = [100 if r['stats.status'] == 'Induced' else 0 for r in allq]\n",
" print np.mean(is_status)\n",
" leng = len(is_status)\n",
" print leng\n",
" print np.mean(is_status[leng-10000:])\n",
"percentCore()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def top_users(counts, line):\n",
" id = 'sessionId'\n",
" if line[id] in counts:\n",
" counts[line[id]] = counts[line[id]] + 1\n",
" else:\n",
" counts[line[id]] = 1\n",
" return counts\n",
"\n",
"with open('../state/lastExec', 'rb') as lastExec:\n",
" lastExecInd = lastExec.readline().strip()\n",
"print lastExecInd\n",
"\n",
"rows = []; \n",
"execInd = lastExecInd;\n",
"with open('../state/execs/%s.exec/plotInfo.json' % execInd, 'rb') as jsonfile:\n",
" json_lines = jsonfile.readlines()\n",
" \n",
"rows = [json.loads(l) for l in json_lines]\n",
"\n",
"accept_all = [r for r in rows if r['stats.type']=='accept']\n",
"accept_nothing = [r for r in rows if r['stats.type']=='accept' and r['stats.status']=='Nothing']\n",
"print 'accepts: %d / %d totallines: %d' % (len(accept_nothing), len(accept_all), len(json_lines))\n",
"\n",
"for r in accept_nothing[:5]:\n",
" print '{q}'.format(**r)\n",
" \n",
"sorted(reduce(top_users, rows, {}).items(), key=lambda x: -x[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def precent_status_user(filtered_rows):\n",
" query_counts = [r['queryCount'] for r in filtered_rows]\n",
" is_status = [100 if r['stats.status'] == 'Induced' else 0 for r in filtered_rows]\n",
" plot_cumavg(query_counts, is_status, xlabel='query #', ylabel='precent induced');\n",
"\n",
"\n",
"\n",
"rows_to_count = [r for r in rows if r['stats.type'] == 'accept']\n",
"ranked_users = sorted(reduce(top_users, rows_to_count[5000:], {}).items(), key=lambda x: -x[1])\n",
"\n",
"plt.figure()\n",
"topnum = 5;\n",
"plot_reset()\n",
"p['alpha'] = 1;\n",
"p['linewidth'] = 5;\n",
"p['label'] = 'all'; \n",
"p['color'] = 'k';\n",
"precent_status_user([r for r in rows if r['stats.type'] == 'accept']); \n",
"\n",
"colors = ['c', 'r', 'm', 'y', 'b', 'g']\n",
"plot_reset()\n",
"p['alpha'] = 1;\n",
"p['linewidth'] = 5;\n",
"p['alpha'] = 0.5;\n",
"p['marker'] = 'o';\n",
"p['markersize'] = 1;\n",
"\n",
"for g in enumerate(ranked_users[0:5]):\n",
" # (0, (u'AMT_A1HKYY6XI2OHO1', 2830))\n",
" p['label'] = '#%d' % (g[0]+1);\n",
" #plotsetting['alpha'] = 1-float(g[0])/topnum;\n",
" p['color'] = colors[g[0]];\n",
" # print plotsetting['color']\n",
" precent_status_user([r for r in rows if r['stats.type'] == 'accept' and r['sessionId'] == g[1][0]])\n",
" print g\n",
"\n",
"plt.ylim(-0.1, 100)\n",
"plt.legend(frameon=False, loc='lower right')\n",
"savefig('parse_status_topuser.pdf');"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"def expressivity(status = 'Core'):\n",
" filtered_rows = [r for r in rows if r['stats.type'] == 'accept' and r['stats.status'] == status]\n",
" query_counts = [r['queryCount'] for r in filtered_rows]\n",
" len_formula = [r['stats.len_formula'] for r in filtered_rows]\n",
" len_utterance = [r['stats.len_utterance'] for r in filtered_rows]\n",
" form_per_q = [float(ls[0])/ls[1] for ls in zip(len_formula, len_utterance)]\n",
" plot_cumavg(query_counts, form_per_q, xlabel='query#', ylabel='\"expressiveness\"');\n",
"\n",
" #plot_cumavg(query_counts, len_utterance, xlabel='query#', ylabel='length');\n",
" print_avg(form_per_q, 'formula length')\n",
"plt.figure()\n",
"plot_reset()\n",
"p['color'] = 'b'; p['label'] = 'core'; expressivity('Core')\n",
"p['color'] = 'g'; p['label'] = 'induced'; expressivity('Induced')\n",
"plt.legend(frameon=False, loc='upper left')\n",
"# savefig('expressiveness.pdf')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def expressivity_by_users(filtered_rows):\n",
" query_counts = [r['queryCount'] for r in filtered_rows]\n",
" len_formula = [r['stats.len_formula'] for r in filtered_rows]\n",
" len_utterance = [r['stats.len_utterance'] for r in filtered_rows]\n",
" form_per_q = [float(ls[0])/ls[1] for ls in zip(len_formula, len_utterance)]\n",
" plot_cumavg(query_counts, form_per_q, xlabel='query#', ylabel='len(z) / len(x)');\n",
"\n",
"plot_reset()\n",
"plt.figure()\n",
"p['alpha'] = 1;\n",
"p['linewidth'] = 5;\n",
"p['label'] = 'all'; \n",
"p['color'] = 'k';\n",
"expressivity_by_users([r for r in rows if r['stats.type'] == 'accept' ])\n",
" \n",
"colors = ['c', 'r', 'm', 'y', 'b']\n",
"plot_reset()\n",
"p['alpha'] = 1;\n",
"p['linewidth'] = 5;\n",
"p['alpha'] = 0.5;\n",
"p['marker'] = 'o';\n",
"p['markersize'] = 1;\n",
"rows_to_count = [r for r in rows if r['stats.type'] == 'accept']\n",
"ranked_users = sorted(reduce(top_users, rows_to_count, {}).items(), key=lambda x: -x[1])\n",
"for g in enumerate(ranked_users[0:5]):\n",
" print g\n",
" p['label'] = '#%d' % (g[0]+1);\n",
" p['color'] = colors[g[0]];\n",
" expressivity_by_users([r for r in rows if r['stats.type'] == 'accept' \\\n",
" and r['sessionId'] == g[1][0]])\n",
"\n",
"plt.ylim([0,100])\n",
"plt.legend(frameon=False, loc='lower right')\n",
"savefig('expressiveness_by_user.pdf')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"q_all = [r for r in rows if r['stats.type']=='q']\n",
"q_nothing = [r for r in rows if r['stats.type']=='q' and r['stats.status']=='Nothing']\n",
"print 'q_nothing: %d / %d totallines: %d' % (len(q_nothing), len(q_all), len(json_lines))\n",
"ranked_users = sorted(reduce(top_users, q_all, {}).items(), key=lambda x: -x[1])\n",
"\n",
"for g in (ranked_users[1:5]):\n",
" print g\n",
" print '********************'\n",
" rows_userg = [r for r in rows if r['stats.type']=='q' and r['sessionId']==g[0]]\n",
" print_count = 0\n",
" prev_nothing = False\n",
" for r in rows_userg[-1500:]:\n",
" if prev_nothing or r['stats.status']=='Nothing':\n",
" print_count = print_count + 1\n",
" if print_count>100: break\n",
" print r['stats.status'] + ':\\t' + r['q'].replace('(:q \"','').replace('\")','') ;\n",
" prev_nothing = True if r['stats.status']=='Nothing' else False\n",
"\n"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}