Skip to content

Commit

Permalink
make shape optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhi committed Nov 1, 2021
1 parent 2e8f5cd commit bbf54bc
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions fast_overlap/fast_overlap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,34 @@ cdef extern from "parallel_overlap.cpp":

@cython.wraparound(False)
@cython.nonecheck(False)
cpdef overlap_parallel(int [:,::1] prev, int[:,::1] curr, shape):
cpdef overlap_parallel(int [:,::1] prev, int[:,::1] curr, shape=None):
"""
Calculate the pairwise overlap the labels for two arrays in
parallel using openmp. This may not work on non-linux OSes.
Currently limited to only accept `int` type arrays.
Parameters
----------
prev, curr : 2D array-like of int
curr will have at least as many unique labels as prev
shape : tuple of int, optional
The shape of the output array. This should reflect the maximum
value of labels.
Returns
-------
arr : (N, M) array of int
N is the number of unique labels in prev and M the number of unique in curr.
The ijth entry in the array gives the number of pixels for which label *i* in prev
overlaps with *j* in curr.
"""
prev = np.ascontiguousarray(prev)
curr = np.ascontiguousarray(curr)

if shape is None:
shape = (prev.max(), curr.max())

cdef np.ndarray[int, ndim=2, mode="c"] output = np.zeros(shape, dtype=np.dtype("i"))
cdef Py_ssize_t ncols = shape[1]

Expand All @@ -46,14 +70,17 @@ ctypedef fused ints:
# @cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
cpdef overlap(ints[:, :] prev, ints[:,:] curr, shape):
cpdef overlap(ints[:, :] prev, ints[:,:] curr, shape=None):
"""
Calculate the pairwise overlap the labels for two arrays.
Parameters
----------
prev, curr : 2D array-like of int
curr will have at least as many unique labels as prev
shape : tuple of int, optional
The shape of the output array. This should reflect the maximum
value of labels.
Returns
-------
Expand All @@ -64,6 +91,10 @@ cpdef overlap(ints[:, :] prev, ints[:,:] curr, shape):
"""
prev = np.ascontiguousarray(prev)
curr = np.ascontiguousarray(curr)

if shape is None:
shape = (prev.max(), curr.max())

cdef int [:, :] arr
arr = np.zeros(shape, dtype=np.dtype("i"))
cdef size_t I, J, i, j
Expand Down

0 comments on commit bbf54bc

Please sign in to comment.