Commit bfccdea3 authored by Roel Aaij's avatar Roel Aaij
Browse files

Add some python tests

parent ad5ad185
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import KM3NeT-bkg-gen as xt\n",
"\n",
"from IPython.display import display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function example1"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"example1(arg0: numpy.ndarray[float]) -> float\n",
"\n",
"Return the first element of an array, of dimension at least one\n",
"\n"
]
}
],
"source": [
"print(xt.example1.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"5.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"a = np.arange(15).reshape(3, 5) + 5\n",
"b = xt.example1(a)\n",
"display(a)\n",
"display(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function example2"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"example2(arg0: numpy.ndarray[float]) -> numpy.ndarray[float]\n",
"\n",
"Return the the specified array plus 2\n",
"\n"
]
}
],
"source": [
"print(xt.example2.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 2., 3., 4., 5., 6.],\n",
" [ 7., 8., 9., 10., 11.],\n",
" [ 12., 13., 14., 15., 16.]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"a = np.arange(15).reshape(3, 5)\n",
"b = xt.example2(a)\n",
"display(a)\n",
"display(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function readme_example1"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"readme_example1(arg0: numpy.ndarray[float]) -> float\n",
"\n",
"Accumulate the sines of all the values of the specified array\n",
"\n"
]
}
],
"source": [
"print(xt.readme_example1.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.03173326, 0.06346652, 0.09519978, 0.12693304,\n",
" 0.1586663 , 0.19039955, 0.22213281, 0.25386607, 0.28559933,\n",
" 0.31733259, 0.34906585, 0.38079911, 0.41253237, 0.44426563,\n",
" 0.47599889, 0.50773215, 0.53946541, 0.57119866, 0.60293192,\n",
" 0.63466518, 0.66639844, 0.6981317 , 0.72986496, 0.76159822,\n",
" 0.79333148, 0.82506474, 0.856798 , 0.88853126, 0.92026451,\n",
" 0.95199777, 0.98373103, 1.01546429, 1.04719755, 1.07893081,\n",
" 1.11066407, 1.14239733, 1.17413059, 1.20586385, 1.23759711,\n",
" 1.26933037, 1.30106362, 1.33279688, 1.36453014, 1.3962634 ,\n",
" 1.42799666, 1.45972992, 1.49146318, 1.52319644, 1.5549297 ,\n",
" 1.58666296, 1.61839622, 1.65012947, 1.68186273, 1.71359599,\n",
" 1.74532925, 1.77706251, 1.80879577, 1.84052903, 1.87226229,\n",
" 1.90399555, 1.93572881, 1.96746207, 1.99919533, 2.03092858,\n",
" 2.06266184, 2.0943951 , 2.12612836, 2.15786162, 2.18959488,\n",
" 2.22132814, 2.2530614 , 2.28479466, 2.31652792, 2.34826118,\n",
" 2.37999443, 2.41172769, 2.44346095, 2.47519421, 2.50692747,\n",
" 2.53866073, 2.57039399, 2.60212725, 2.63386051, 2.66559377,\n",
" 2.69732703, 2.72906028, 2.76079354, 2.7925268 , 2.82426006,\n",
" 2.85599332, 2.88772658, 2.91945984, 2.9511931 , 2.98292636,\n",
" 3.01465962, 3.04639288, 3.07812614, 3.10985939, 3.14159265])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"63.02006849910228"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"63.02006849910227"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"PI = np.pi\n",
"a = np.linspace(0, PI, 100)\n",
"b = xt.readme_example1(a)\n",
"display(a)\n",
"display(np.sin(a).sum())\n",
"display(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function vectorize_example1"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"vectorize_example1(arg0: numpy.ndarray[float], arg1: numpy.ndarray[float]) -> numpy.ndarray[float]\n",
"\n",
"Add the sine and and cosine of the two specified values\n",
"\n"
]
}
],
"source": [
"print(xt.vectorize_example1.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.34906585, 0.6981317 , 1.04719755, 1.3962634 ,\n",
" 1.74532925, 2.0943951 , 2.44346095, 2.7925268 , 3.14159265])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 0. , 0.6981317 , 1.3962634 , 2.0943951 , 2.7925268 ,\n",
" 3.4906585 , 4.1887902 , 4.88692191, 5.58505361, 6.28318531])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 1. , 1.10806459, 0.81643579, 0.3660254 , 0.04511513,\n",
" 0.04511513, 0.3660254 , 0.81643579, 1.10806459, 1. ])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ True, True, True, True, True, True, True, True, True, True], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"PI = np.pi\n",
"a = np.linspace(0, PI, 10)\n",
"b = np.linspace(0, 2*PI, 10)\n",
"c = xt.vectorize_example1(a, b)\n",
"d = np.sin(a)+np.cos(b)\n",
"display(a)\n",
"display(b)\n",
"display(c)\n",
"display(np.isclose(c, d))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
[pytest]
addopts = --verbose
python_files = tests/python/*.py
testpaths = tests
norecursedirs = .git build
[aliases]
test = pytest
......@@ -77,11 +77,13 @@ setup(
version=__version__,
author='Roel Aaij',
author_email='roelaaij@nikhef.nl',
url='https://sikkel.nikhef.nl/roelaaij/generate_and_sort',
url='https://sikkel.nikhef.nl/roelaaij/k40gen',
description='standalone background generator for KM3NeT',
long_description='',
ext_modules=ext_modules,
install_requires=['numpy', 'cmake'],
setup_requires=['pytest-runner', 'numpy', 'cmake'],
install_requires=['numpy'],
tests_require=["pytest"],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
)
import k40gen as m
from unittest import TestCase
import numpy as np
def generate():
import k40gen as m
gens = m.Generators(21341, 1245, [7000., 700., 70., 0.])
times = m.generate_k40(0, int(1e8), gens, False)
assert(times.shape[0] == 4)
class K40GenTest(TestCase):
def generate(self):
# v = np.arange(15).reshape(3, 5)
# y = m.readme_example1(v)
# np.testing.assert_allclose(y, 1.2853996391883833, 1e-12)
return True
def test_generate():
generate()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment