Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Rewrote scipy.ndimage.find_object function in cython #5006

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

aman-harness
Copy link

MAINT: Rewrote scipy.ndimage.find_object function in cython.

@aman-harness aman-harness changed the title Rewrote _findObjects function in cython Rewrote scipy.ndimage.find_object function in cython Jun 30, 2015
@rgommers rgommers added scipy.ndimage maintenance Items related to regular maintenance tasks labels Jul 1, 2015
@rgommers
Copy link
Member

rgommers commented Jul 1, 2015

@aman-iitj the test failures on TravisCI are real

@aman-harness aman-harness force-pushed the cython_ndimage branch 2 times, most recently from d44d959 to e8a2979 Compare July 1, 2015 07:37
@aman-harness
Copy link
Author

@rgommers All the tests passed on my laptop. Even I checked the file in IPython and I am getting correct output for the failed test cases in travis. Have I missed something?

@rgommers
Copy link
Member

rgommers commented Jul 1, 2015

I guess so. TravisCI runs 64-bit Linux. Maybe you're on another OS or bit-ness, and this is a 32/64-bit issue or a compiler dependent bug.

@rgommers
Copy link
Member

rgommers commented Jul 1, 2015

@aman-iitj it would be good to write more informative commit messages, that use the prefixes and format described in http://docs.scipy.org/doc/numpy-dev/dev/gitwash/development_workflow.html#writing-the-commit-message

@aman-harness
Copy link
Author

@rgommers Yes, I am working on 32 -bit system.
Thanks for the link. I will try to follow the commit message guidelines.

@aman-harness aman-harness changed the title Rewrote scipy.ndimage.find_object function in cython MAINT: Rewrote scipy.ndimage.find_object function in cython Jul 3, 2015
@jaimefrio
Copy link
Member

Hi Aman,

Thanks for making this first PR. On a first scan of your code, I wasn't able to find anything obviously wrong that would lead to the test failures. But I did get the impression (which may be wrong) that it mimics too closely what the existing C implementation did, which makes it more complicated than could be achievable with Cython.

I'll try to turn that loose generic comment into actionable recommendations in the next few days: hopefully as we address those we can figure out where is the bug in your code.

@aman-harness
Copy link
Author

Thanks for going through the code and suggestions @jaimefrio .
Regarding the error in tests, I have narrowed the code which is causing error and soon I will fix it.

@aman-harness
Copy link
Author

@jaimefrio @rgommers TravisCI tests have passed :)

@aman-harness aman-harness force-pushed the cython_ndimage branch 4 times, most recently from 684a44d to 472af3d Compare July 14, 2015 12:17
@aman-harness
Copy link
Author

@jaimefrio Have you gone through the find_objects function. I have added 2 more functions using PointIterator in the PR. Please have a look at _ni_morphology.pyx file also.

@aman-harness aman-harness force-pushed the cython_ndimage branch 2 times, most recently from a4f0973 to 4f0df27 Compare July 14, 2015 18:02
@aman-harness aman-harness force-pushed the cython_ndimage branch 5 times, most recently from 7b63879 to be33ea5 Compare July 29, 2015 17:10
@jaimefrio
Copy link
Member

We shold also add the necessary configuration to the Bento build files.

np.npy_bool contiguous

ctypedef struct PyArrayObject:
int nd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not being used anywhere right now. If there is an advantage in using this over ndarray.ndim lets do it, if not we can remove it from here.

@jaimefrio
Copy link
Member

Try all the following together, I'm pretty sure it should work:

  • get_funcs should not be a Python def function, but should be cdef with return type func_p *. This should make the casting of findObjectsPoint[data_t] unnecessary, remove it too.
  • In _findObjects get rid of the funcs variable, and directly do:
cdef:
    func_p findObjectsPoint = get_funcs(input_raw.take([0]))

What errors are you getting when you try that?

@aman-harness
Copy link
Author

@jaimefrio i declared get_funcs like:
cdef * funcp get_funcs(np.ndarray[data_t] input): return ( findObjectsPoint[data_t])
And I am getting error as:

inux-gnu-gcc' failed with exit status 1\n"]
In [40]: reload(_ni_measure)
Error compiling Cython file:
------------------------------------------------------------
...
#####################################################################
ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label, 
               int *regions, int rank) nogil
cdef * funcp get_funcs(np.ndarray[data_t] input):
    ^
------------------------------------------------------------
_ni_measure.pyx:49:5: Expected an identifier, found '*'
Error compiling Cython file:
------------------------------------------------------------
...
#####################################################################
ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label, 
               int *regions, int rank) nogil
cdef * funcp get_funcs(np.ndarray[data_t] input):
            ^
------------------------------------------------------------
_ni_measure.pyx:49:13: Syntax error in C variable declaration

@aman-harness
Copy link
Author

If i declare it like this
cdef funcp get_funcs(np.ndarray[data_t] input): return ( findObjectsPoint[data_t])
Error is:

 In [41]: reload(_ni_measure)
Error compiling Cython file:
------------------------------------------------------------
...
#####################################################################
ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label, 
               int *regions, int rank) nogil
cdef funcp get_funcs(np.ndarray[data_t] input):
    ^
------------------------------------------------------------
_ni_measure.pyx:49:5: 'funcp' is not a type identifier
Error compiling Cython file:
------------------------------------------------------------
...
        np.flatiter _iti
        PyArrayIterObject *iti
        # func_p findObjectsPoint =    funcs
        func_p findObjectsPoint = get_funcs(input_raw.take([0]))
                                          ^
------------------------------------------------------------
_ni_measure.pyx:109:43: no suitable method found

@jaimefrio
Copy link
Member

I missed an underscore, the type should be func_p, which is the typedef defined in the line before that one.

@aman-harness
Copy link
Author

@jaimefrio Sorry for the silly mistake. I was sleepy yesterday.
But still this doesn't works.

In [6]: import _ni_measure
Error compiling Cython file:
------------------------------------------------------------
...
ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label, 
               int *regions, int rank) nogil
cdef func_p get_funcs(np.ndarray[data_t] input):
    return ( findObjectsPoint[data_t])
                            ^
------------------------------------------------------------
_ni_measure.pyx:50:29: Cannot assign type 'object (int8_t *, flatiter, int, int *, int)' to 'void (*)(void *, flatiter, int, int *, int) nogil'
Error compiling Cython file:
------------------------------------------------------------
...

@aman-harness
Copy link
Author

In [8]: import _ni_measure
Error compiling Cython file:
------------------------------------------------------------
...
#####################################################################
ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label, 
               int *regions, int rank) nogil
cdef *func_p get_funcs(np.ndarray[data_t] input):
    ^
------------------------------------------------------------
_ni_measure.pyx:49:5: Expected an identifier, found '*'
Error compiling Cython file:
------------------------------------------------------------
...
#####################################################################
ctypedef void (*func_p)(void *data, np.flatiter iti, np.intp_t max_label, 
               int *regions, int rank) nogil
cdef *func_p get_funcs(np.ndarray[data_t] input):
            ^
------------------------------------------------------------
_ni_measure.pyx:49:13: Syntax error in C variable declaration

@jaimefrio
Copy link
Member

The * goes after the type, i.e. func_p*, not *func_p. The other error is what we already discussed, about making those functions take a void *, then cast it to the right type inside the function. If data_t has to be part of the function signature we may have to add an np.ndarray[data_t] argument to those functions, just for selecting the right one.

@aman-harness
Copy link
Author

@jaimefrio I have already tried all the combinations of casting and functions declarations but nothing is working except casting with <Py_intptr_t> or <int *>.

As you have suggested in function findObjectPoint I need to add np.ndarray input[data_t] for signatures if I change data_t * to void *. If I declare input as a python object in _findObjects, it asks me to remove GIL giving compilation error: Python type test not allowed without GIL and after removing GlL it gives weird results(None for every case.). If I declare it as cdef np.ndarray input = np.PyArray_FROM_OF(input_raw, flags) it compiles successfully but is giving segmentation fault for every test case.


cdef inline findObjectsPoint(data_t *data, np.flatiter _iti, np.intp_t max_label,
int *regions, int rank):
cdef int kk =0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after =.

@aman-harness
Copy link
Author

@jaimefrio It doesn't works. I am still getting same errors.

@aman-harness
Copy link
Author

@jaimefrio Even If I change the declaration of function as:

cpdef findObjectsPoint():
    cdef np.intp_t s_index = - 1
    
    return s_index

and I call it using


while np.PyArray_ITER_NOTDONE(_iti):
findObjectsPoint()
np.PyArray_ITER_NEXT(_iti)

the file doesn't compiles. It gives the same error.

Should I remove the function findObjectspoint() and update the array regions in _findObjects() itself?

@aman-harness
Copy link
Author

@jaimefrio. How we are proceeding on it? Please have a look at my previous comments.

@ev-br
Copy link
Member

ev-br commented Jul 22, 2017

Jaime @jaimefrio where does this PR stand in your ongoing ndimage refactor --- something left here, or can it be closed?

@tupui tupui added C/C++ Items related to the internal C/C++ code base Cython Issues with the internal Cython code base needs-decision Items that need further discussion before they are merged or closed labels Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C/C++ Items related to the internal C/C++ code base Cython Issues with the internal Cython code base maintenance Items related to regular maintenance tasks needs-decision Items that need further discussion before they are merged or closed scipy.ndimage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants