Last active
November 3, 2016 14:04
-
-
Save pfernique/b4d9222c60ecb67d011425c800980723 to your computer and use it in GitHub Desktop.
JSS autowig examples
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Wrapping a basic library\n", | |
"\n", | |
"We here aim at presenting the interactive wrapping workflow.\n", | |
"For the sake of simplicity, we consider a basic example of *C++* library." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Preamble\n", | |
"\n", | |
"The source code is available in the `basic` directory." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"!pygmentize basic/src/cpp/binomial.h" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This directory already contains wrappers, we therefore need to remove them." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from path import path\n", | |
"srcdir = path('basic')/'src'/'py'\n", | |
"for wrapper in srcdir.walkfiles('*.cpp'):\n", | |
" wrapper.unlink()\n", | |
"for wrapper in srcdir.walkfiles('*.h'):\n", | |
" wrapper.unlink()\n", | |
"wrapper = srcdir/'basic'/'_basic.py'\n", | |
"if wrapper.exists():\n", | |
" wrapper.unlink()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Then, we need to install and compile the *C++* library.\n", | |
"This is done using available **Conda** recipes." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"!conda build basic/conda/libbasic -c statiskit -c conda-forge\n", | |
"!conda install libbasic --use-local -c statiskit -c conda-forge" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Wrapping the *C++* library\n", | |
"\n", | |
"Once these preliminaries done, we can proceed to the actual generation of wrappers for the **Basic** *C++* library.\n", | |
"For this, we import **AutoWIG** and create an empty Abstract Semantic Graph (ASG)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import autowig\n", | |
"asg = autowig.AbstractSemanticGraph()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Then, we parse headers with relevant compilation flags." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%%time\n", | |
"import sys\n", | |
"prefix = path(sys.prefix)\n", | |
"asg = autowig.parser(asg, [prefix/'include'/'basic'/'overload.h',\n", | |
" prefix/'include'/'basic'/'binomial.h'],\n", | |
" ['-x', 'c++', '-std=c++11', '-I' + str((prefix/'include').abspath())],\n", | |
" silent = True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Since most of **AutoWIG** guidelines are respected, the `default` `controller` implementation is suitable." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%%time\n", | |
"autowig.controller.plugin = 'default'\n", | |
"asg = autowig.controller(asg)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In order to wrap the library we need to select the `boost_python_internal` `generator` implementation." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%%time\n", | |
"autowig.generator.plugin = 'boost_python_internal'\n", | |
"wrappers = autowig.generator(asg,\n", | |
" module = 'basic/src/py/_basic.cpp',\n", | |
" decorator = 'basic/src/py/basic/_basic.py',\n", | |
" prefix = 'wrapper_')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The wrappers are only generated in-memory.\n", | |
"It is therefore needed to write them on the disk to complete the process." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%%time\n", | |
"wrappers.write()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here is an example of the generated wrappers." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"!pygmentize basic/src/py/_basic.cpp" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Once the wrappers are written on disk, we need to compile and install the *Python* bindings." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"!conda build basic/conda/python-basic -c statiskit -c conda-forge\n", | |
"!conda install python-basic --use-local -c statiskit -c conda-forge" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Using the *C++* library *Python* bindings\n", | |
"\n", | |
"Finally, we can hereafter use the *C++* library in the *Python* interpreter." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import basic\n", | |
"binomial = basic.BinomialDistribution(1, .5)\n", | |
"binomial" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"binomial.pmf(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"binomial.pmf(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"binomial.n = 0\n", | |
"binomial" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"binomial.pmf(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"binomial.set_pi(1.1)" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"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.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
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
echo OFF | |
echo "AutoWIG Examples" | |
echo "================" | |
echo "" | |
echo "This script will run the four examples presented in the" | |
echo "accompanying paper. Corresponding Jupyter notebooks can" | |
echo "also be run individually, echo using, the following" | |
echo "commands," | |
echo "" | |
echo ON | |
jupyter nbconvert --to notebook --execute basic.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
jupyter nbconvert --to notebook --execute subset.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
jupyter nbconvert --to notebook --execute template.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
jupyter nbconvert --to notebook --execute dependent.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
echo OFF | |
echo "In order to visualize the Jupyter notebooks execute the " | |
echo "folowing command" | |
echo "" | |
echo "jupyter notebook index.ipynb" |
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
set +x | |
set +e | |
echo "AutoWIG Examples" | |
echo "================" | |
echo "" | |
echo "This script will run the four examples presented in the" | |
echo "accompanying paper. Corresponding Jupyter notebooks can" | |
echo "also be run individually, echo using, the following" | |
echo "commands," | |
echo "" | |
set -x | |
# git clone http://github.com/StatisKit/AutoWIG.git --depth=1 | |
# mv AutoWIG/doc/examples/basic basic | |
# rm -rf AutoWIG | |
# jupyter nbconvert --to notebook --execute basic.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
# git clone http://github.com/StatisKit/PyClangLite.git --depth=1 | |
# jupyter nbconvert --to notebook --execute subset.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
# rm -rf PySTL | |
# git clone http://github.com/StatisKit/PySTL.git --depth=1 | |
# jupyter nbconvert --to notebook --execute template.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
git clone http://github.com/pfernique/StructureAnalysis.git | |
cd StructureAnalysis | |
git checkout -b remove_tool origin/remove_tool | |
cd .. | |
jupyter nbconvert --to notebook --execute dependent.ipynb --allow-errors --inplace --ExecutePreprocessor.timeout=3600 --NotebookApp.kernel_spec_manager_class='environment_kernels.EnvironmentKernelSpecManager' | |
set -e | |
set +x | |
echo "In order to visualize the Jupyter notebooks execute the " | |
echo "folowing command" | |
echo "" | |
echo "jupyter notebook index.ipynb" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment