[cctbxbb] flex and numpy caveats

Luc Bourhis luc_j_bourhis at mac.com
Fri Apr 5 23:38:37 PDT 2013


On 5 Apr 2013, at 19:38, James Stroud wrote:

> 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). [...]

Good demonstration: I fully agree with you. We will try to implement (ii) then.

> Taking the "pythonic" approach only requires
> 
>     http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_CastToType
> 
> For each of the element types that could comprise flex arrays, one only needs to write one PyArray_Descr
> 
>    http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#PyArray_Descr
> 
> which becomes the second argument for PyArray_CastToType.

Thanks again to look into this for me. 

Unfortunately the real difficulty is that a flex array does not have a descriptor as a numpy array does. In some sense the latter does not know the type of its elements: only the C++ compiler does. Thus some template trickery is still needed to map the each numpy array element type to the right flex element type.

But thanks for your researches, that has helped a long way. 

Best wishes,

Luc

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


More information about the cctbxbb mailing list