[cctbxbb] Flex array slice assignment work-alike

James Stroud xtald00d at gmail.com
Thu Apr 11 00:49:19 PDT 2013

Hello All,

It seems that flex arrays do not support slice assignment:

py> from scitbx.array_family import flex
py> ary = flex.float(range(5))
py> list(ary)
[0.0, 1.0, 2.0, 3.0, 4.0]
py> ary[:3] = flex.float([41, 42, 43])
ArgumentError                             Traceback (most recent call last)
/usr/local/cctbx/cctbx_sources/mmtbx/dynamics/<ipython-input-13-5ce3d648bc6f> in <module>()
----> 1 ary[:3] = flex.float([41, 42, 43])

ArgumentError: Python argument types in
    float.__setitem__(float, slice, float)
did not match C++ signature:
    __setitem__(scitbx::af::versa<float, scitbx::af::flex_grid<scitbx::af::small<long, 10ul> > > {lvalue}, scitbx::af::small<long, 10ul>, float)
    __setitem__(scitbx::af::versa<float, scitbx::af::flex_grid<scitbx::af::small<long, 10ul> > > {lvalue}, long, float)

Slice assignment is common in python:

py> alist = range(5)
py> alist[:3] = [41, 42, 43]
py> alist
[41, 42, 43, 3, 4]

Is there a flex array work-alike for this operation?

I've found set_selected():

py> sel_ary = flex.bool([True, True, True, False, False])
py> ary.set_selected(sel_ary, flex.float([41, 42, 43]))
<scitbx_array_family_flex_ext.float at 0x101df1158>
py> list(ary)
[41.0, 42.0, 43.0, 3.0, 4.0]

But set_selected() is cumbersome for slice assignment because (1) you have to build a flex selection array somehow and (2) you have to put the new values into a flex array of the appropriate type. Is set_selected() the best way? If so, is there something in the cctbx libraries that makes building "slicing" selection arrays more efficient than say a list comprehension?

sel_ary = flex.bool([True if i < 3 else False for i in xrange(5)])


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

More information about the cctbxbb mailing list