Skip to content

Instantly share code, notes, and snippets.

@bgbg
Created January 26, 2016 14:55
Show Gist options
  • Save bgbg/2ede90c96fdd27e10743 to your computer and use it in GitHub Desktop.
Save bgbg/2ede90c96fdd27e10743 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[INFO] Using MetricMock instead of real metrics, mode is: QA\n"
]
}
],
"source": [
"import sframe as gl"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1.6'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gl.version"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[INFO] Start server at: ipc:///tmp/graphlab_server-8521 - Server binary: /Users/me/anaconda/lib/python2.7/site-packages/sframe/unity_server - Server log: /tmp/sframe_server_1453820022.log\n",
"[INFO] GraphLab Server Version: 1.6\n"
]
},
{
"data": {
"text/plain": [
"{'num_edges': 796, 'num_vertices': 749}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gr = gl.load_sgraph('/Users/me/temp/my_graph')\n",
"gr.summary()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def sum_weight(src, edge, dst):\n",
" src['__total_weight'] += edge['weight']\n",
" return src, edge, dst\n",
"\n",
"def normalize_weight(src, edge, dst):\n",
" edge['weight'] /= src['__total_weight']\n",
" return src, edge, dst"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gr.vertices['__total_weight'] = 0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gr = gr.triple_apply(sum_weight, ['__total_weight'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "RuntimeError",
"evalue": "Runtime Exception. Runtime Exception. Traceback (most recent call last):\n File \"<ipython-input-4-aafa1c00c8df>\", line 6, in normalize_weight\nZeroDivisionError: float division by zero\n",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-7-9fb4c6a4478c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtriple_apply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnormalize_weight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'weight'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/me/anaconda/lib/python2.7/site-packages/sframe/data_structures/sgraph.pyc\u001b[0m in \u001b[0;36mtriple_apply\u001b[0;34m(self, triple_apply_fn, mutated_fields, input_fields)\u001b[0m\n\u001b[1;32m 1019\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mcython_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1021\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mSGraph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_proxy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__proxy__\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlambda_triple_apply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtriple_apply_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmutated_fields\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1022\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1023\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msave\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'auto'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/me/anaconda/lib/python2.7/site-packages/sframe/cython/context.pyc\u001b[0m in \u001b[0;36m__exit__\u001b[0;34m(self, exc_type, exc_value, traceback)\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow_cython_trace\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;31m# To hide cython trace, we re-raise from here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 49\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexc_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexc_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 50\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;31m# To show the full trace, we do nothing and let exception propagate\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mRuntimeError\u001b[0m: Runtime Exception. Runtime Exception. Traceback (most recent call last):\n File \"<ipython-input-4-aafa1c00c8df>\", line 6, in normalize_weight\nZeroDivisionError: float division by zero\n"
]
}
],
"source": [
"gr = gr.triple_apply(normalize_weight, ['weight'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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
}
@bgbg
Copy link
Author

bgbg commented Jan 26, 2016

The data file is located here th: https://www.dropbox.com/s/yr7h1z8s2uo1mw9/my_graph.tar.gz?dl=0 (gzipped)

@bgbg
Copy link
Author

bgbg commented Jan 29, 2016

This gist relates to the following discussion: forum.dato.com/discussion/1674/strange-behaviour-of-triple-apply

The problem was caused by self-links that resulted in unpredicted order vertex updates: turi-code/how-to#37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment