Commit 1d86d5fe authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Quite some progress with this tutorial. Only two parts missing.

parent c00d9ed6
...@@ -8,7 +8,7 @@ ctypedef fused my_type: ...@@ -8,7 +8,7 @@ ctypedef fused my_type:
@cython.boundscheck(False) @cython.boundscheck(False)
@cython.wraparound(False) @cython.wraparound(False)
cpdef naive_convolve_fused_types(my_type [:,:] f, my_type [:,:] g): cpdef naive_convolve(my_type [:,:] f, my_type [:,:] g):
if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1: if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
raise ValueError("Only odd dimensions on filter supported") raise ValueError("Only odd dimensions on filter supported")
......
# cython: infer_types=True # cython: infer_types=True
import numpy as np import numpy as np
cimport cython
DTYPE = np.intc DTYPE = np.intc
@cython.boundscheck(False) @cython.boundscheck(False)
@cython.wraparound(False) @cython.wraparound(False)
def naive_convolve_infer_types(int [:,::1] f, int [:,::1] g): def naive_convolve(int [:,::1] f, int [:,::1] g):
if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1: if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
raise ValueError("Only odd dimensions on filter supported") raise ValueError("Only odd dimensions on filter supported")
......
...@@ -2,7 +2,7 @@ import numpy as np ...@@ -2,7 +2,7 @@ import numpy as np
DTYPE = np.intc DTYPE = np.intc
def naive_convolve_memview(int [:,:] f, int [:,:] g): def naive_convolve(int [:,:] f, int [:,:] g):
if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1: if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
raise ValueError("Only odd dimensions on filter supported") raise ValueError("Only odd dimensions on filter supported")
......
from __future__ import division from __future__ import division
import numpy as np import numpy as np
def naive_convolve_py(f, g): def naive_convolve(f, g):
# f is an image and is indexed by (v, w) # f is an image and is indexed by (v, w)
# g is a filter kernel and is indexed by (s, t), # g is a filter kernel and is indexed by (s, t),
# it needs odd dimensions # it needs odd dimensions
......
import numpy as np import numpy as np
# "def" can type its arguments but not have a return type. The type of the
# arguments for a "def" function is checked at run-time when entering the
# function.
# We now need to fix a datatype for our arrays. I've used the variable # We now need to fix a datatype for our arrays. I've used the variable
# DTYPE for this, which is assigned to the usual NumPy runtime # DTYPE for this, which is assigned to the usual NumPy runtime
# type info object. # type info object.
DTYPE = np.intc DTYPE = np.intc
# The arrays f, g and h is typed as "np.ndarray" instances. The only effect
# this has is to a) insert checks that the function arguments really are def naive_convolve(f, g):
# NumPy arrays, and b) make some attribute access like f.shape[0] much
# more efficient. (In this example this doesn't matter though.)
def naive_convolve_types(f, g):
if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1: if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
raise ValueError("Only odd dimensions on filter supported") raise ValueError("Only odd dimensions on filter supported")
assert f.dtype == DTYPE and g.dtype == DTYPE assert f.dtype == DTYPE and g.dtype == DTYPE
...@@ -46,6 +40,8 @@ def naive_convolve_types(f, g): ...@@ -46,6 +40,8 @@ def naive_convolve_types(f, g):
cdef int value cdef int value
for x in range(xmax): for x in range(xmax):
for y in range(ymax): for y in range(ymax):
# Cython has built-in C functions for min and max
# This makes the following lines very fast.
s_from = max(smid - x, -smid) s_from = max(smid - x, -smid)
s_to = min((xmax - x) - smid, smid + 1) s_to = min((xmax - x) - smid, smid + 1)
t_from = max(tmid - y, -tmid) t_from = max(tmid - y, -tmid)
......
This diff is collapsed.
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