
Hello, I'm working on adding support for the bool dtype for sparse matrices. But I'm having some problems I'm adding a wrapper for the npy_bool type. I've included a file called bool_ops.h which basically contains: typedef npy_int8 npy_bool_wrapper; ( https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... ) Then I included bool_ops.h and the wrapper npy_bool_wrapper in sparsetools.i and numpy.i where I thought it was necessary. So that the typemaps for the npy_bool type are generated. You can see this in my branch: in sparsetools.i https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... in numpy.i https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... However I still get the same error seen before in ticket #1533 ( https://github.com/scipy/scipy/issues/2058) TypeError: Array of type 'byte' required. Array of type 'bool' given Which is not what I expected because the typemaps for bool type should now be generated. Could the order in INSTANTIATE_ALL be the problem? ( https://github.com/cowlicks/scipy/blob/swig-sandbox/scipy/sparse/sparsetools... ) Why is order important here? I included npy_bool_wrapper where I did because the Numpy C api says npy_bool is an unsigned char. I'm currently trying to reorder INSTANTIATE_ALL to make things work. Thanks in advance,

Blake Griffith <blake.a.griffith <at> gmail.com> writes:
Hello, I'm working on adding support for the bool dtype for sparse matrices. But I'm having some problems
I'm adding a wrapper for the npy_bool type. I've included a file called bool_ops.h which basically contains:
typedef npy_int8 npy_bool_wrapper;
This will potentially not be enough for SWIG to treat bool wrapper output values as a separate type, it might have to be a separate class. So it's best to stick to the complex types recipe if you don't exactly know what you are doing. The order of instantiation and type maps should not matter. One way to check what is going on is to look into the SWIG generated cxx files. Does bool_wrapper appear there in the same way as the complex types? The generated output is quite crazy and difficult to understand, but if you don't see bool wrapper anywhere, then something is not done correctly on the swig level.

@Pauli, changing the order of the INSTANTIATE_ALL definition by putting npy_bool_wrapper first actually did fix things :) I'm not sure why order matters here, yet. But there is a comment just above this definition that states: /* * Order is important here, list int before float, float before * double, scalar before complex, etc. */ And since order does matter, having npy_bool_wrapper first probably is not optimal. So I'm trying to figure the relevance of ordering these things and what a proper order should be. Anyway, now I can use the toarray method with dtype=bool sparse matrices. I also ran scipy.sparse.test.test_base.py and got FAILED (SKIP=122,errors=16) the same number as before. So it doesn't seem like I broke anything new, but the test suite doesn't really do cross dtype checking. On Thu, May 30, 2013 at 11:52 AM, Pauli Virtanen <pav@iki.fi> wrote:
Blake Griffith <blake.a.griffith <at> gmail.com> writes:
Hello, I'm working on adding support for the bool dtype for sparse matrices. But I'm having some problems
I'm adding a wrapper for the npy_bool type. I've included a file called bool_ops.h which basically contains:
typedef npy_int8 npy_bool_wrapper;
This will potentially not be enough for SWIG to treat bool wrapper output values as a separate type, it might have to be a separate class. So it's best to stick to the complex types recipe if you don't exactly know what you are doing.
The order of instantiation and type maps should not matter.
One way to check what is going on is to look into the SWIG generated cxx files. Does bool_wrapper appear there in the same way as the complex types? The generated output is quite crazy and difficult to understand, but if you don't see bool wrapper anywhere, then something is not done correctly on the swig level.
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev

<at> Pauli, changing the order of the INSTANTIATE_ALL definition by
Blake Griffith <blake.a.griffith <at> gmail.com> writes: putting npy_bool_wrapper first actually did fix things :) I'm not sure why order matters here, yet. That's a bit surprising, looking at the SWIG output shows that it tries pyarray_cancastsafely for each type in template order, so it sort of makes sense. I'm not sure if relying on this behavior is intended in swig, or if typemaps are supposed to match exactly.

Blake Griffith <blake.a.griffith <at> gmail.com> writes:
And since order does matter, having npy_bool_wrapper first probably is not optimal. So I'm trying to figure the relevance of ordering these things and what a proper order should be.
Ok, sounds good. Bool probably should go first --- there's a natural safe-cast order for numpy types, see here: http://docs.scipy.org/doc/numpy/reference/ufuncs.html and nothing casts safely to a boolean --- so it should be first.
participants (2)
-
Blake Griffith
-
Pauli Virtanen