enable icon indicating copy to clipboard operation
enable copied to clipboard

Some functions in Agg wrapper don't play nice with numpy dtypes

Open tonysyu opened this issue 12 years ago • 1 comments

I noticed this issue with GraphicsContextArray.clip_to_rect, but I imagine this is an issue in a few other agg functions.

This issue pops up when running one of the Chaco examples:

https://github.com/enthought/chaco/blob/master/examples/user_guide/grid_plot_container.py

The problem in the example is that the height is being set to an int, which gets converted to a numpy int at some point. A python int seems to cast correctly, but numpy ints don't:

import numpy as np
from kiva.agg import GraphicsContextArray


def python_ints(values):
    return [int(a) for a in values]

def python_floats(values):
    return [float(a) for a in values]


def test_clip_to_rect_dtypes():

    gc = GraphicsContextArray((3, 3))
    rect = np.zeros(4)

    convert_funcs = [
        python_ints,
        python_floats,
        np.float64,
        np.float32,
        np.int64,
        np.int32,
        np.uint8,
    ]

    with gc:
        for convert in convert_funcs:
            try:
                gc.clip_to_rect(*convert(rect))
            except Exception as error:
                print convert
                print error

if __name__ == '__main__':
    test_clip_to_rect_dtypes()

tonysyu avatar Mar 08 '13 18:03 tonysyu

Output from Enable 5.0.0rc2:

<class 'numpy.float32'>
Wrong number or type of arguments for overloaded function 'GraphicsContextArray_clip_to_rect'.
  Possible C/C++ prototypes are:
    kiva::graphics_context_base::clip_to_rect(kiva::rect_type &)
    kiva::graphics_context_base::clip_to_rect(double,double,double,double)

<class 'numpy.int64'>
Wrong number or type of arguments for overloaded function 'GraphicsContextArray_clip_to_rect'.
  Possible C/C++ prototypes are:
    kiva::graphics_context_base::clip_to_rect(kiva::rect_type &)
    kiva::graphics_context_base::clip_to_rect(double,double,double,double)

<class 'numpy.int32'>
Wrong number or type of arguments for overloaded function 'GraphicsContextArray_clip_to_rect'.
  Possible C/C++ prototypes are:
    kiva::graphics_context_base::clip_to_rect(kiva::rect_type &)
    kiva::graphics_context_base::clip_to_rect(double,double,double,double)

<class 'numpy.uint8'>
Wrong number or type of arguments for overloaded function 'GraphicsContextArray_clip_to_rect'.
  Possible C/C++ prototypes are:
    kiva::graphics_context_base::clip_to_rect(kiva::rect_type &)
    kiva::graphics_context_base::clip_to_rect(double,double,double,double)

I guess that fixing this would require some additional type maps for the SWIG wrapper.

jwiggins avatar Feb 22 '21 17:02 jwiggins