Permalink
Browse files

Globally change np to cnp if cimported

  • Loading branch information...
1 parent 729b311 commit 62d83ad42cf30855ecd969104e0c0285018c09eb @ahojnnes ahojnnes committed Feb 24, 2013
View
@@ -2,11 +2,11 @@
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False
-cimport cython
-cimport numpy as np
-from libc.math cimport sqrt
import math
import numpy as np
+
+cimport numpy as cnp
+from libc.math cimport sqrt
from skimage._shared.geometry cimport point_in_polygon
@@ -29,7 +29,7 @@ def line(Py_ssize_t y, Py_ssize_t x, Py_ssize_t y2, Py_ssize_t x2):
"""
- cdef np.ndarray[np.intp_t, ndim=1, mode="c"] rr, cc
+ cdef cnp.ndarray[cnp.intp_t, ndim=1, mode="c"] rr, cc
cdef char steep = 0
cdef Py_ssize_t dx = abs(x2 - x)
@@ -110,11 +110,11 @@ def polygon(y, x, shape=None):
cdef Py_ssize_t r, c
#: make contigous arrays for r, c coordinates
- cdef np.ndarray contiguous_rdata, contiguous_cdata
+ cdef cnp.ndarray contiguous_rdata, contiguous_cdata
contiguous_rdata = np.ascontiguousarray(y, 'double')
contiguous_cdata = np.ascontiguousarray(x, 'double')
- cdef np.double_t* rptr = <np.double_t*>contiguous_rdata.data
- cdef np.double_t* cptr = <np.double_t*>contiguous_cdata.data
+ cdef cnp.double_t* rptr = <cnp.double_t*>contiguous_rdata.data
+ cdef cnp.double_t* cptr = <cnp.double_t*>contiguous_cdata.data
#: output coordinate arrays
cdef list rr = list()
@@ -1,3 +1,8 @@
+#cython: cdivision=True
+#cython: boundscheck=False
+#cython: nonecheck=False
+#cython: wraparound=False
+
"""
Template matching using normalized cross-correlation.
@@ -30,22 +35,24 @@ the image window *before* squaring.)
.. [2] J. P. Lewis, "Fast Normalized Cross-Correlation", Industrial Light and
Magic.
"""
-import cython
-cimport numpy as np
+
import numpy as np
from scipy.signal import fftconvolve
-from skimage.transform import integral
+
+cimport numpy as cnp
from libc.math cimport sqrt, fabs
from skimage._shared.transform cimport integrate
-@cython.boundscheck(False)
-def match_template(np.ndarray[float, ndim=2, mode="c"] image,
- np.ndarray[float, ndim=2, mode="c"] template):
+from skimage.transform import integral
+
- cdef np.ndarray[float, ndim=2, mode="c"] corr
- cdef np.ndarray[float, ndim=2, mode="c"] image_sat
- cdef np.ndarray[float, ndim=2, mode="c"] image_sqr_sat
+def match_template(cnp.ndarray[float, ndim=2, mode="c"] image,
+ cnp.ndarray[float, ndim=2, mode="c"] template):
+
+ cdef cnp.ndarray[float, ndim=2, mode="c"] corr
+ cdef cnp.ndarray[float, ndim=2, mode="c"] image_sat
+ cdef cnp.ndarray[float, ndim=2, mode="c"] image_sqr_sat
cdef float template_mean = np.mean(template)
cdef float template_ssd
cdef float inv_area
@@ -88,4 +95,3 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image,
corr[r, c] /= den
return corr
-
@@ -3,20 +3,20 @@
#cython: nonecheck=False
#cython: wraparound=False
import numpy as np
-cimport numpy as np
+cimport numpy as cnp
from libc.math cimport sin, cos, abs
from skimage._shared.interpolation cimport bilinear_interpolation
-def _glcm_loop(np.ndarray[dtype=np.uint8_t, ndim=2,
- negative_indices=False, mode='c'] image,
- np.ndarray[dtype=np.float64_t, ndim=1,
- negative_indices=False, mode='c'] distances,
- np.ndarray[dtype=np.float64_t, ndim=1,
- negative_indices=False, mode='c'] angles,
+def _glcm_loop(cnp.ndarray[dtype=cnp.uint8_t, ndim=2,
+ negative_indices=False, mode='c'] image,
+ cnp.ndarray[dtype=cnp.float64_t, ndim=1,
+ negative_indices=False, mode='c'] distances,
+ cnp.ndarray[dtype=cnp.float64_t, ndim=1,
+ negative_indices=False, mode='c'] angles,
int levels,
- np.ndarray[dtype=np.uint32_t, ndim=4,
- negative_indices=False, mode='c'] out):
+ cnp.ndarray[dtype=cnp.uint32_t, ndim=4,
+ negative_indices=False, mode='c'] out):
"""Perform co-occurrence matrix accumulation.
Parameters
@@ -39,8 +39,8 @@ def _glcm_loop(np.ndarray[dtype=np.uint8_t, ndim=2,
cdef:
Py_ssize_t a_idx, d_idx, r, c, rows, cols, row, col
- np.uint8_t i, j
- np.float64_t angle, distance
+ cnp.uint8_t i, j
+ cnp.float64_t angle, distance
rows = image.shape[0]
cols = image.shape[1]
@@ -81,7 +81,7 @@ cdef inline int _bit_rotate_right(int value, int length):
return (value >> 1) | ((value & 1) << (length - 1))
-def _local_binary_pattern(np.ndarray[double, ndim=2] image,
+def _local_binary_pattern(cnp.ndarray[double, ndim=2] image,
int P, float R, char method='D'):
"""Gray scale and rotation invariant LBP (Local Binary Patterns).
@@ -111,19 +111,19 @@ def _local_binary_pattern(np.ndarray[double, ndim=2] image,
"""
# texture weights
- cdef np.ndarray[int, ndim=1] weights = 2 ** np.arange(P, dtype=np.int32)
+ cdef cnp.ndarray[int, ndim=1] weights = 2 ** np.arange(P, dtype=np.int32)
# local position of texture elements
rp = - R * np.sin(2 * np.pi * np.arange(P, dtype=np.double) / P)
cp = R * np.cos(2 * np.pi * np.arange(P, dtype=np.double) / P)
- cdef np.ndarray[double, ndim=2] coords = np.round(np.vstack([rp, cp]).T, 5)
+ cdef cnp.ndarray[double, ndim=2] coords = np.round(np.vstack([rp, cp]).T, 5)
# pre allocate arrays for computation
- cdef np.ndarray[double, ndim=1] texture = np.zeros(P, np.double)
- cdef np.ndarray[char, ndim=1] signed_texture = np.zeros(P, np.int8)
- cdef np.ndarray[int, ndim=1] rotation_chain = np.zeros(P, np.int32)
+ cdef cnp.ndarray[double, ndim=1] texture = np.zeros(P, np.double)
+ cdef cnp.ndarray[char, ndim=1] signed_texture = np.zeros(P, np.int8)
+ cdef cnp.ndarray[int, ndim=1] rotation_chain = np.zeros(P, np.int32)
output_shape = (image.shape[0], image.shape[1])
- cdef np.ndarray[double, ndim=2] output = np.zeros(output_shape, np.double)
+ cdef cnp.ndarray[double, ndim=2] output = np.zeros(output_shape, np.double)
cdef Py_ssize_t rows = image.shape[0]
cdef Py_ssize_t cols = image.shape[1]
View
@@ -10,18 +10,20 @@ Copyright (c) 2009-2011 Broad Institute
All rights reserved.
Original author: Lee Kamentsky
'''
+
import numpy as np
-cimport numpy as np
+
+cimport numpy as cnp
cimport cython
from libc.stdlib cimport malloc, free
from libc.string cimport memset
+
cdef extern from "../_shared/vectorized_ops.h":
- void add16(np.uint16_t *dest, np.uint16_t *src)
- void sub16(np.uint16_t *dest, np.uint16_t *src)
+ void add16(cnp.uint16_t *dest, cnp.uint16_t *src)
+ void sub16(cnp.uint16_t *dest, cnp.uint16_t *src)
-np.import_array()
##############################################################################
#
@@ -43,7 +45,7 @@ np.import_array()
DTYPE_UINT32 = np.uint32
DTYPE_BOOL = np.bool
-ctypedef np.uint16_t pixel_count_t
+ctypedef cnp.uint16_t pixel_count_t
###########
#
@@ -58,8 +60,8 @@ ctypedef np.uint16_t pixel_count_t
###########
cdef struct HistogramPiece:
- np.uint16_t coarse[16]
- np.uint16_t fine[256]
+ cnp.uint16_t coarse[16]
+ cnp.uint16_t fine[256]
cdef struct Histogram:
HistogramPiece top_left # top-left corner
@@ -92,9 +94,9 @@ cdef struct Histograms:
void *memory # pointer to the allocated memory
Histogram *histogram # pointer to the histogram memory
PixelCount *pixel_count # pointer to the pixel count memory
- np.uint8_t *data # pointer to the image data
- np.uint8_t *mask # pointer to the image mask
- np.uint8_t *output # pointer to the output array
+ cnp.uint8_t *data # pointer to the image data
+ cnp.uint8_t *mask # pointer to the image mask
+ cnp.uint8_t *output # pointer to the output array
Py_ssize_t column_count # number of columns represented by this
# structure
Py_ssize_t stripe_length # number of columns including "radius" before
@@ -172,15 +174,15 @@ cdef Histograms *allocate_histograms(Py_ssize_t rows,
Py_ssize_t col_stride,
Py_ssize_t radius,
Py_ssize_t percent,
- np.uint8_t *data,
- np.uint8_t *mask,
- np.uint8_t *output):
+ cnp.uint8_t *data,
+ cnp.uint8_t *mask,
+ cnp.uint8_t *output):
cdef:
Py_ssize_t adjusted_stripe_length = columns + 2*radius + 1
Py_ssize_t memory_size
void *ptr
Histograms *ph
- size_t roundoff
+ Py_ssize_t roundoff
Py_ssize_t a
SCoord *psc
@@ -232,7 +234,7 @@ cdef Histograms *allocate_histograms(Py_ssize_t rows,
# a_2 is the offset from the center to each of the octagon
# corners
#
- a = <Py_ssize_t>(<np.float64_t>radius * 2.0 / 2.414213)
+ a = <Py_ssize_t>(<cnp.float64_t>radius * 2.0 / 2.414213)
a_2 = a / 2
if a_2 == 0:
a_2 = 1
@@ -456,7 +458,7 @@ cdef inline void deaccumulate_fine_histogram(Histograms *ph,
############################################################################
cdef inline void accumulate(Histograms *ph):
- cdef np.int32_t accumulator
+ cdef cnp.int32_t accumulator
accumulate_coarse_histogram(ph, ph.current_column)
deaccumulate_coarse_histogram(ph, ph.current_column)
@@ -514,7 +516,7 @@ cdef inline void update_histogram(Histograms *ph,
Py_ssize_t current_stride = ph.current_stride
Py_ssize_t column_count = ph.column_count
Py_ssize_t row_count = ph.row_count
- np.uint8_t value
+ cnp.uint8_t value
Py_ssize_t stride
Py_ssize_t x
Py_ssize_t y
@@ -557,8 +559,8 @@ cdef inline void update_current_location(Histograms *ph):
Py_ssize_t bottom_left_off = tr_bl_colidx(ph, current_column)
Py_ssize_t bottom_right_off = tl_br_colidx(ph, current_column)
Py_ssize_t leading_edge_off = leading_edge_colidx(ph, current_column)
- np.int32_t *coarse_histogram
- np.int32_t *fine_histogram
+ cnp.int32_t *coarse_histogram
+ cnp.int32_t *fine_histogram
Py_ssize_t last_xoff
Py_ssize_t last_yoff
Py_ssize_t last_stride
@@ -597,13 +599,13 @@ cdef inline void update_current_location(Histograms *ph):
#
############################################################################
-cdef inline np.uint8_t find_median(Histograms *ph):
+cdef inline cnp.uint8_t find_median(Histograms *ph):
cdef:
Py_ssize_t pixels_below # of pixels below the median
Py_ssize_t i
Py_ssize_t j
Py_ssize_t k
- np.uint32_t accumulator
+ cnp.uint32_t accumulator
if ph.accumulator_count == 0:
return 0
@@ -625,7 +627,7 @@ cdef inline np.uint8_t find_median(Histograms *ph):
for j in range(i*16, (i + 1)*16):
accumulator += ph.accumulator.fine[j]
if accumulator > pixels_below:
- return <np.uint8_t>j
+ return <cnp.uint8_t>j
return 0
@@ -650,9 +652,9 @@ cdef int c_median_filter(Py_ssize_t rows,
Py_ssize_t col_stride,
Py_ssize_t radius,
Py_ssize_t percent,
- np.uint8_t *data,
- np.uint8_t *mask,
- np.uint8_t *output):
+ cnp.uint8_t *data,
+ cnp.uint8_t *mask,
+ cnp.uint8_t *output):
cdef:
Histograms *ph
Histogram *phistogram
@@ -731,11 +733,14 @@ cdef int c_median_filter(Py_ssize_t rows,
return 0
-def median_filter(np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, mode='c'] data,
- np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, mode='c'] mask,
- np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, mode='c'] output,
+def median_filter(cnp.ndarray[dtype=cnp.uint8_t, ndim=2,
+ negative_indices=False, mode='c'] data,
+ cnp.ndarray[dtype=cnp.uint8_t, ndim=2,
+ negative_indices=False, mode='c'] mask,
+ cnp.ndarray[dtype=cnp.uint8_t, ndim=2,
+ negative_indices=False, mode='c'] output,
int radius,
- np.int32_t percent):
+ cnp.int32_t percent):
"""Median filter with octagon shape and masking.
Parameters
@@ -770,10 +775,12 @@ def median_filter(np.ndarray[dtype=np.uint8_t, ndim=2, negative_indices=False, m
raise ValueError('Data shape (%d, %d) is not output shape (%d, %d)' %
(data.shape[0], data.shape[1],
output.shape[0], output.shape[1]))
- if c_median_filter(<np.int32_t> data.shape[0], <np.int32_t> data.shape[1],
- <np.int32_t> data.strides[0], <np.int32_t> data.strides[1],
+ if c_median_filter(<cnp.int32_t>data.shape[0],
+ <cnp.int32_t>data.shape[1],
+ <cnp.int32_t>data.strides[0],
+ <cnp.int32_t>data.strides[1],
radius, percent,
- <np.uint8_t *> data.data,
- <np.uint8_t *> mask.data,
- <np.uint8_t *> output.data):
+ <cnp.uint8_t*>data.data,
+ <cnp.uint8_t*>mask.data,
+ <cnp.uint8_t*>output.data):
raise MemoryError('Failed to allocate scratchpad memory')
@@ -1,17 +1,20 @@
-cimport numpy as np
+cimport numpy as cnp
+
+
+ctypedef cnp.uint16_t dtype_t
cdef int int_max(int a, int b)
cdef int int_min(int a, int b)
# 16-bit core kernel receives extra information about data bitdepth
-cdef void _core16(np.uint16_t kernel(Py_ssize_t *, float, np.uint16_t,
- Py_ssize_t, Py_ssize_t, Py_ssize_t, float,
- float, Py_ssize_t, Py_ssize_t),
- np.ndarray[np.uint16_t, ndim=2] image,
- np.ndarray[np.uint8_t, ndim=2] selem,
- np.ndarray[np.uint8_t, ndim=2] mask,
- np.ndarray[np.uint16_t, ndim=2] out,
+cdef void _core16(dtype_t kernel(Py_ssize_t *, float, dtype_t,
+ Py_ssize_t, Py_ssize_t, Py_ssize_t, float,
+ float, Py_ssize_t, Py_ssize_t),
+ cnp.ndarray[dtype_t, ndim=2] image,
+ cnp.ndarray[cnp.uint8_t, ndim=2] selem,
+ cnp.ndarray[cnp.uint8_t, ndim=2] mask,
+ cnp.ndarray[dtype_t, ndim=2] out,
char shift_x, char shift_y, Py_ssize_t bitdepth,
float p0, float p1, Py_ssize_t s0, Py_ssize_t s1) except *
Oops, something went wrong.

0 comments on commit 62d83ad

Please sign in to comment.