[cctbxbb] flex and numpy caveats

Luc Bourhis luc_j_bourhis at mac.com
Thu Apr 4 02:15:22 PDT 2013

Hi James,

> 1. The first example should really throw an exception, because silent failures like this can be catastrophic:
> py> from numpy import random
> py> r = random.randint(2, size=10)
> py> r
> array([1, 0, 1, 1, 0, 1, 0, 1, 1, 1])
> py> list(flex.bool(r))
> [True, False, False, False, False, False, False, False, False, False]
> This example is clearly due to incorrect assumptions about the internal representations of numpy ndarrays.

As far as I can tell, this is much worse than that as the numpy-to-flex conversion assumes the same element type in the source and target array. 
Thus converting a numpy array of int's into a flex array of bools is illegal but this precondition is not asserted unfortunately.
Nasty indeed. I do not understand numpy well enough to propose a solution, I am afraid.

> 2. Although not as potentially catastrophic as the first, t
> py> flex.int(range(10))[r[0]]
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> /Users/jstroud/Unison/Code/radialx/testdata/<ipython-input-753-f14587f0ceeb> in <module>()
> ----> 1 flex.int(range(10))[r[0]]
> TypeError: 'numpy.int64' object is not iterable

For flex arrays, the following __getitem__ variants are tried in this order:
1. one that takes a tuple
2. one that takes a slice
3. one that takes a single index, of type long in C++

The problem I think is that we are missing a registered conversion from the type numpy.int64 to the type long, and therefore all 3 variants fail. 
Somehow the first failure is reported.

Best wishes,


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://phenix-online.org/pipermail/cctbxbb/attachments/20130404/74137282/attachment.htm>

More information about the cctbxbb mailing list