Skip to content

Commit

Permalink
Changed the implemetation of fused type as per Jaime's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
bewithaman committed Aug 20, 2015
1 parent 49214c1 commit 22f64f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
4 changes: 1 addition & 3 deletions scipy/ndimage/measurements.py
Expand Up @@ -270,9 +270,7 @@ def find_objects(input, max_label=0):

if max_label < 1:
max_label = input.max()

return _ni_measure._findObjects(input, max_label)

return _ni_measure._findObjects(input.flatten(), input.shape, max_label)

def labeled_comprehension(input, labels, index, func, out_dtype, default, pass_positions=False):
"""
Expand Down
48 changes: 19 additions & 29 deletions scipy/ndimage/src/_ni_measure.pyx
Expand Up @@ -36,16 +36,6 @@ ctypedef fused data_t:
np.uint32_t
np.uint64_t

#####################################################################
# Function Specializers and asociate function for using fused type
#####################################################################

ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label,
int *regions, int rank) nogil

def get_funcs(np.ndarray[data_t] input):
return (<Py_intptr_t> findObjectsPoint[data_t])

######################################################################
# Update Regions According to Input Data Type
######################################################################
Expand All @@ -62,32 +52,33 @@ cdef inline findObjectsPoint(data_t *data, np.flatiter _iti, np.intp_t max_label
int *regions, int rank):
cdef int kk =0
cdef np.intp_t cc

# only integer or boolean values are allowed, since s_index is being used in indexing
cdef np.intp_t s_index = <np.intp_t> (<data_t *> data)[0] - 1
cdef np.intp_t val
if s_index >= 0 and s_index < max_label:
if rank > 0:
s_index *= 2 * rank

with nogil:
# only integer or boolean values are allowed, since s_index is being used in indexing
if s_index >= 0 and s_index < max_label:
if rank > 0:
s_index *= 2 * rank

val = regions[s_index]
for kk in range(rank):
cc = (<PyArrayIterObject *>_iti).coordinates[kk]
if val < 0 or cc < regions[s_index + kk]:
regions[s_index + kk] = cc
if val < 0 or cc + 1 > regions[s_index + kk + rank]:
regions[s_index + kk + rank] = cc + 1
val = regions[s_index]
for kk in range(rank):
cc = (<PyArrayIterObject *>_iti).coordinates[kk]
if val < 0 or cc < regions[s_index + kk]:
regions[s_index + kk] = cc
if val < 0 or cc + 1 > regions[s_index + kk + rank]:
regions[s_index + kk + rank] = cc + 1

else:
regions[s_index] = 1
else:
regions[s_index] = 1

######################################################################
# Implementaion of find_Objects function:-
######################################################################

cpdef _findObjects(input_raw, m_label):
cpdef _findObjects(np.ndarray[data_t] input_raw, shape, m_label):
input_raw.resize(shape)
cdef:
funcs = get_funcs(input_raw.take([0]))

np.intp_t ii, rank, size_regions, max_label = m_label
np.intp_t start, jj, idx, end
Expand All @@ -96,8 +87,6 @@ cpdef _findObjects(input_raw, m_label):
np.flatiter _iti
PyArrayIterObject *iti

func_p findObjectsPoint = <func_p> <void *> <Py_intptr_t> funcs

int flags = np.NPY_NOTSWAPPED | np.NPY_ALIGNED
np.ndarray input

Expand Down Expand Up @@ -135,7 +124,8 @@ cpdef _findObjects(input_raw, m_label):

# Iteration over array:
while np.PyArray_ITER_NOTDONE(_iti):
findObjectsPoint(np.PyArray_ITER_DATA(_iti), _iti, max_label, regions, rank)
with gil:
findObjectsPoint(<data_t *>np.PyArray_ITER_DATA(_iti), _iti, max_label, regions, rank)
np.PyArray_ITER_NEXT(_iti)

result = []
Expand Down

0 comments on commit 22f64f8

Please sign in to comment.