Last active
April 2, 2017 01:51
-
-
Save rvernica/a5f8cd60e7231706c8cf324fed1116dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import scidbpy\n", | |
"import numpy\n", | |
"import pandas\n", | |
"\n", | |
"db = scidbpy.connect()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"db.query(\"\"\"\n", | |
" store(\n", | |
" build(\n", | |
" <x:int64, y:float>[i=1:2; j=2:3],\n", | |
"\n", | |
" '[[(1 , .1) , (?2, .2 )],\n", | |
" [(10, ?10), (20, .02)]]', true), foo)\"\"\")\n", | |
"\n", | |
"# use 'apply' instead of 'unpack'\n", | |
"db.query('store(apply(foo, dim_i, i, dim_j, j), bar)')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"deletable": true, | |
"editable": true | |
}, | |
"source": [ | |
"### Binary Download" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'\\xff\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xff...'" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"barray = db._scan_array('bar', \n", | |
" fmt = '(int64 NULL, float NULL, int64, int64)') \n", | |
"barray[:10] + '...'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"deletable": true, | |
"editable": true | |
}, | |
"source": [ | |
"### Map to NumPy Array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([((255, 1), (255, 0.10000000149011612), 1, 2),\n", | |
" ((2, 0), (255, 0.20000000298023224), 1, 3),\n", | |
" ((255, 10), (10, 0.0), 2, 2),\n", | |
" ((255, 20), (255, 0.019999999552965164), 2, 3)], \n", | |
" dtype=[('x', [('null', 'u1'), ('val', '<i8')]), ('y', [('null', 'u1'), ('val', '<f4')]), ('i', '<i8'), ('j', '<i8')])" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"foo = numpy.frombuffer(\n", | |
" barray,\n", | |
" [('x', [('null', 'uint8'), ('val', 'int64' )]), \n", | |
" ('y', [('null', 'uint8'), ('val', 'float32')]),\n", | |
" ('i', 'int64'),\n", | |
" ('j', 'int64')])\n", | |
"foo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"deletable": true, | |
"editable": true | |
}, | |
"source": [ | |
"### Map to Pandas DataFrame" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th></th>\n", | |
" <th>x</th>\n", | |
" <th>y</th>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>i</th>\n", | |
" <th>j</th>\n", | |
" <th></th>\n", | |
" <th></th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th rowspan=\"2\" valign=\"top\">1</th>\n", | |
" <th>2</th>\n", | |
" <td>1.0</td>\n", | |
" <td>0.10</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>NaN</td>\n", | |
" <td>0.20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th rowspan=\"2\" valign=\"top\">2</th>\n", | |
" <th>2</th>\n", | |
" <td>10.0</td>\n", | |
" <td>NaN</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>20.0</td>\n", | |
" <td>0.02</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" x y\n", | |
"i j \n", | |
"1 2 1.0 0.10\n", | |
" 3 NaN 0.20\n", | |
"2 2 10.0 NaN\n", | |
" 3 20.0 0.02" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Replace nulls with nan\n", | |
"# http://pandas.pydata.org/pandas-docs/stable/gotchas.html#na-type-promotions\n", | |
"foo_x = foo['x']['val'].astype(numpy.float64)\n", | |
"foo_x[numpy.where(foo['x']['null'] != 255)] = numpy.nan\n", | |
"\n", | |
"foo_y = foo['y']['val'].copy()\n", | |
"foo_y[numpy.where(foo['y']['null'] != 255)] = numpy.nan\n", | |
"\n", | |
"idx = pandas.MultiIndex.from_arrays([foo['i'].copy(), foo['j'].copy()],\n", | |
" names = ('i', 'j'))\n", | |
"\n", | |
"# https://github.com/pandas-dev/pandas/issues/15860\n", | |
"pandas.DataFrame({'x': foo_x, 'y': foo_y}, index = idx)" | |
] | |
} | |
], | |
"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.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment