<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Apr 11, 2013, at 9:18 AM, Luc Bourhis wrote:</div><blockquote type="cite"><div>On 11 Apr 2013, at 09:49, James Stroud wrote:<br><br><blockquote type="cite">It seems that flex arrays do not support slice assignment:<br></blockquote><br>it would not be too difficult to code 1-dimensional slice assignment &nbsp;but ...<br><br>On 11 Apr 2013, at 10:24, <a href="mailto:Graeme.Winter@Diamond.ac.uk">Graeme.Winter@Diamond.ac.uk</a> wrote:<br><br><blockquote type="cite">[...] <br></blockquote><blockquote type="cite"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[k,:,:] = image.as_numpy_array()[y0:y1, x0:x1]<br></blockquote><br>that's way more difficult!<br></div></blockquote></div><div><br></div><br><div>In python this type of magic is fairly straightforward because the slice is just a tuple:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="Monaco">py&gt; class Example(object):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">... &nbsp; def __setitem__(self, i, v):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">... &nbsp; &nbsp; print "setting %s to %s" % (i, v)</font></div></div><div><div><font class="Apple-style-span" face="Monaco">...</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; e = Example()</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; e[5:6, 4, 8:9, "bob"] = 2</font></div></div><div><div><font class="Apple-style-span" face="Monaco">setting (slice(5, 6, None), 4, slice(8, 9, None), 'bob') to 2</font></div></div></blockquote><div><br></div><div><br></div><div>This should actually be the case on the C side, too, not that it wouldn't take a few lines of code to build an iterator based on the slice.</div><div><br></div><div>But as a way to circumvent all of this magic with slices (which would be very cool, by the way), it might be easiest for the programmer (and somewhat intuitive for those familiar with flex) just to add a signature to set_selected() that uses a flex.grid to represent a slice because flex.grid already encapsulates the most useful aspects of slicing semantics.</div><div><br></div><div><br></div><div>Here is a prototype for the suggested behavior in python using ndarray as a back-end:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="Monaco">class Prototype(object):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; def __init__(self, data):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; self.data = numpy.array(data)</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; def __getitem__(self, i):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; if isinstance (i, flex.grid):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; slices = [slice(*t) for t in zip(i.origin(), i.last())]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; return self.__class__(self.data[slices])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; else:</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; return self.__class__(self.data[i])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; def __setitem__(self, i, v):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; if isinstance (i, flex.grid):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; slices = [slice(*t) for t in zip(i.origin(), i.last())]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; self.data[slices] = v</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; else:</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; self.data[i] = v</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; def __repr__(self):</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; return repr(self.data)</font></div></div></blockquote><div><br></div><div><br></div><div>And here it is in action:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="Monaco">py&gt; grd = flex.grid((1, 1), (3, 3)) &nbsp;# &lt;== selects [1:3, 1:3]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; ary = Prototype(numpy.arange(25).reshape((5,5)))</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; ary</font></div></div><div><div><font class="Apple-style-span" face="Monaco">array([[ 0, &nbsp;1, &nbsp;2, &nbsp;3, &nbsp;4],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[ 5, &nbsp;6, &nbsp;7, &nbsp;8, &nbsp;9],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[10, 11, 12, 13, 14],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[15, 16, 17, 18, 19],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[20, 21, 22, 23, 24]])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; ary[grd]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">array([[ 6, &nbsp;7],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[11, 12]])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; ary[grd] = [[41, 42], [43, 44]]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; ary[grd]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">array([[41, 42],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[43, 44]])</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py&gt; ary</font></div></div><div><div><font class="Apple-style-span" face="Monaco">array([[ 0, &nbsp;1, &nbsp;2, &nbsp;3, &nbsp;4],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[ 5, 41, 42, &nbsp;8, &nbsp;9],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[10, 43, 44, 13, 14],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[15, 16, 17, 18, 19],</font></div></div><div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;[20, 21, 22, 23, 24]])</font></div></div></blockquote><div><font class="Apple-style-span" face="Monaco"><br></font></div><div>James</div></body></html>