[cctbxbb] flex and numpy caveats

James Stroud xtald00d at gmail.com
Fri Apr 5 10:38:24 PDT 2013

Hu Luc,

On Apr 5, 2013, at 6:46 AM, Luc Bourhis wrote:
> However, we haven't address the important question: what behaviour do we really want?
> (i) the numpy array and the flex array must have the same element type, and an exception is thrown if this precondition is violated;
> (ii) we use a fast copy of the numpy array to the flex array if the element types are the same; otherwise we fall back to a slower conversion.

My understanding of the "python way" would be that the desired behavior is (ii). I usually base these types of conclusions on how the python interpreter behaves, and, if that isn't specific enough, I look to the standard library.

Consider the behavior of python's built-in array, which works like lists:

py> import array
py> array.array('f', [1, 2, 3])
array('f', [1.0, 2.0, 3.0])
py> array.array('f', flex.bool([True, False, False]))
array('f', [1.0, 0.0, 0.0])
py> array.array('i', numpy.arange(4))
array('i', [0, 1, 2, 3])

Python arrays and lists don't care what type of elements comprise the data structures they are built from. For example, with array, the constructor doesn't care what the elements of the second argument are as long as they behave as specified in the first: integers can be converted to floats, bools to ints, numpy.int64s can be converted to ints, etc. The "python way" is concerned with behavior rather than type (usually at the sacrifice of performance, which is purposefully an afterthought).

Besides being the "python way", I actually think that such behavior is often easier to implement. Numpy arrays already contain all of the information that describes how they are built. And furthermore, they are able to be converted to any other type without the need to inspect their innards. So the programmer only really needs to know what type of array he wants, and to avoid decisions based on what he has (which would be required in order to throw an error for every "illegal" type).

Taking the "pythonic" approach only requires


For each of the element types that could comprise flex arrays, one only needs to write one PyArray_Descr


which becomes the second argument for PyArray_CastToType.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://phenix-online.org/pipermail/cctbxbb/attachments/20130405/815c60c8/attachment-0001.htm>

More information about the cctbxbb mailing list