Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix auto testing of examples
  • Loading branch information
sklam committed Sep 8, 2016
1 parent a5d8a5b commit d196c9e
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 307 deletions.
5 changes: 4 additions & 1 deletion examples/binarytree.py 100644 → 100755
@@ -1,11 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
This is a more complicated jitclasses example.
Here, we implement a binarytree and iterative preorder and inorder traversal
function using a handwritten stack.
"""
from __future__ import print_function, absolute_import
import random
from numba.utils import OrderedDict
from collections import OrderedDict
from numba import njit
from numba import jitclass
from numba import int32, deferred_type, optional
Expand Down
2 changes: 1 addition & 1 deletion examples/compile_with_pycc.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
from numba import exportmany, export
from numba.pycc import exportmany, export


def mult(a, b):
Expand Down
60 changes: 28 additions & 32 deletions examples/example.py
Expand Up @@ -2,31 +2,18 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import

from scipy.misc import lena
import os
import matplotlib

if os.environ.get('DISPLAY') is None:
matplotlib.use('agg')
print("suppress matplotlib display")

from scipy.misc import ascent
from numpy import ones
import numpy

from numba.decorators import jit
from numba import int32, int64

# Original approach will be slower for now due to the object mode failback
# for numpy.zero_like
#
# @jit(argtypes=[int32[:,:], int32[:,:]], restype=int32[:,:])
# def filter2d(image, filt):
# M, N = image.shape
# Mf, Nf = filt.shape
# Mf2 = Mf // 2
# Nf2 = Nf // 2
# result = numpy.zeros_like(image)
# for i in range(Mf2, M - Mf2):
# for j in range(Nf2, N - Nf2):
# num = 0.0
# for ii in range(Mf):
# for jj in range(Nf):
# num += (filt[Mf-1-ii, Nf-1-jj] * image[i-Mf2+ii, j-Nf2+jj])
# result[i, j] = num
# return result


@jit(nopython=True)
Expand All @@ -43,37 +30,46 @@ def filter2d_core(image, filt, result):
num += (filt[Mf-1-ii, Nf-1-jj] * image[i-Mf2+ii,j-Nf2+jj])
result[i, j] = num

@jit

@jit(nopython=True)
def filter2d(image, filt):
result = numpy.zeros_like(image)
filter2d_core(image, filt, result)
return result


image = lena()
filter = ones((7,7), dtype='int32')
image = ascent().astype(numpy.float64)
filter = ones((7,7), dtype=image.dtype)

result = filter2d(image, filter) # warm up

import time
start = time.time()
from timeit import default_timer as time

start = time()
result = filter2d(image, filter)
duration = time.time() - start
duration = time() - start

from scipy.ndimage import convolve
start = time.time()

start = time()
result2 = convolve(image, filter)
duration2 = time.time() - start
duration2 = time() - start

print("Time for Numba filter = %f\nTime for scipy convolve = %f" % (duration, duration2))

from pylab import subplot, imshow, show, title, gray
subplot(1,2,1)

subplot(1,3,1)
imshow(image)
title('Original Image')
gray()
subplot(1,2,2)
subplot(1,3,2)
imshow(result)
title('Filtered Image')
title('Numba Filtered Image')
gray()
subplot(1,3,3)
imshow(result2)
title('Scipy Filtered Image')
gray()

show()
3 changes: 3 additions & 0 deletions examples/jitclass.py 100644 → 100755
@@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
A simple jitclass example.
"""
Expand Down
3 changes: 3 additions & 0 deletions examples/linkedlist.py 100644 → 100755
@@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
This example demonstrates jitclasses and deferred types for writing a
singly-linked-list.
Expand Down

0 comments on commit d196c9e

Please sign in to comment.