Hi Folks,

 

While we’re on the subject of array slice assignment: it would be really cute if the numpy-style assignment would also work viz:

 

            image = self._reader.read(index)

            array[k,:,:] = image.as_numpy_array()[y0:y1, x0:x1]

 

so we could avoid using numpy arrays in dxtbx ;o)

 

Any ideas?

 

Thanks,

 

Graeme

 

From: [email protected] [mailto:[email protected]] On Behalf Of James Stroud
Sent: 11 April 2013 08:49
To: cctbx mailing list
Subject: [cctbxbb] Flex array slice assignment work-alike

 

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

 

 

James

 

 

 

 


 

-- 

This e-mail and any attachments may contain confidential, copyright and or privileged material, and are for the use of the intended addressee only. If you are not the intended addressee or an authorised recipient of the addressee please notify us of receipt by returning the e-mail and do not use, copy, retain, distribute or disclose the information in or attached to the e-mail.
Any opinions expressed within this e-mail are those of the individual and not necessarily of Diamond Light Source Ltd.
Diamond Light Source Ltd. cannot guarantee that this e-mail or any attachments are free from viruses and we cannot accept liability for any damage which you may sustain as a result of software viruses which may be transmitted in or with the message.
Diamond Light Source Limited (company no. 4375679). Registered in England and Wales with its registered office at Diamond House, Harwell Science and Innovation Campus, Didcot, Oxfordshire, OX11 0DE, United Kingdom