Skip to content

Instantly share code, notes, and snippets.

@dmrd
Created April 21, 2016 03:28
Show Gist options
  • Select an option

  • Save dmrd/b47c89c045279973258f997eeff1e863 to your computer and use it in GitHub Desktop.

Select an option

Save dmrd/b47c89c045279973258f997eeff1e863 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": true
},
"outputs": [],
"source": [
"using PyCall"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"@pyimport numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BitArray vs. Float conversion\n",
"BitArray fails the default conversion, so it falls back and rev dim is not passed in"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"bool = bitrand(2,2,5);\n",
"float = reshape(collect(1:20), (2,2,5));"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"PyObject [[[False, False, False, False, False], [False, False, False, False, True]], [[False, False, False, True, False], [False, False, True, False, True]]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py_bool_rev = PyObject(bool, true)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"PyObject array([[[ 1, 2],\n",
" [ 3, 4]],\n",
"\n",
" [[ 5, 6],\n",
" [ 7, 8]],\n",
"\n",
" [[ 9, 10],\n",
" [11, 12]],\n",
"\n",
" [[13, 14],\n",
" [15, 16]],\n",
"\n",
" [[17, 18],\n",
" [19, 20]]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py_float_rev = PyObject(float, true)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "LoadError",
"evalue": "LoadError: KeyError: shape not found\nwhile loading In[6], in expression starting on line 1",
"output_type": "error",
"traceback": [
"LoadError: KeyError: shape not found\nwhile loading In[6], in expression starting on line 1",
"",
" in getindex at /Users/ddohan/.julia/v0.4/PyCall/src/PyCall.jl:228",
" in getindex at /Users/ddohan/.julia/v0.4/PyCall/src/PyCall.jl:233"
]
}
],
"source": [
"py_bool_rev[:shape] # This is not an array"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(5,2,2)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py_float_rev[:shape] # This is actually an array"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2x5 Array{Any,2}:\n",
" false false false false false\n",
" false false false false true"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py_bool_rev[1] # Note it is 2x5"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2x2 Array{Int64,2}:\n",
" 1 2\n",
" 3 4"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py_float_rev[1]# Note it is 2x2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Work around\n",
"Round to UInt8. Takes 8x as much space, but numpy doesn't seem to do bit arrays"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2x2x5 Array{UInt8,3}:\n",
"[:, :, 1] =\n",
" 0x00 0x00\n",
" 0x00 0x00\n",
"\n",
"[:, :, 2] =\n",
" 0x00 0x00\n",
" 0x00 0x00\n",
"\n",
"[:, :, 3] =\n",
" 0x00 0x00\n",
" 0x00 0x01\n",
"\n",
"[:, :, 4] =\n",
" 0x00 0x00\n",
" 0x01 0x00\n",
"\n",
"[:, :, 5] =\n",
" 0x00 0x01\n",
" 0x00 0x01"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool_rounded = round(UInt8, bool)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"py_bool_rounded = PyObject(bool_rounded,false) = PyObject array([[[0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 1]],\n",
"\n",
" [[0, 0, 0, 1, 0],\n",
" [0, 0, 1, 0, 1]]], dtype=uint8)"
]
},
{
"data": {
"text/plain": [
"(2,2,5)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@show py_bool_rounded = PyObject(bool_rounded, false)\n",
"@show py_bool_rounded[:shape]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(5,2,2)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"py_bool_rounded[:shape] = (2,2,5)\n",
"py_bool_rounded_rev = PyObject(bool_rounded,true) = PyObject array([[[0, 0],\n",
" [0, 0]],\n",
"\n",
" [[0, 0],\n",
" [0, 0]],\n",
"\n",
" [[0, 0],\n",
" [0, 1]],\n",
"\n",
" [[0, 1],\n",
" [0, 0]],\n",
"\n",
" [[0, 0],\n",
" [1, 1]]], dtype=uint8)\n",
"py_bool_rounded_rev[:shape] = (5,2,2)\n"
]
}
],
"source": [
"@show py_bool_rounded_rev = PyObject(bool_rounded, true)\n",
"@show py_bool_rounded_rev[:shape]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.4.3",
"language": "julia",
"name": "julia-0.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment