<div dir="ltr">Hi James,<div class="gmail_extra"><br><div class="gmail_quote">On 11 April 2013 22:20, James Stroud <span dir="ltr">&lt;<a href="mailto:xtald00d@gmail.com" target="_blank">xtald00d@gmail.com</a>&gt;</span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div>
<div>On Apr 11, 2013, at 11:46 AM, Richard Gildea wrote:</div><blockquote type="cite"><div dir="ltr"><div>When I implemented the multidimensional slicing for flex arrays, I only did so because I required that functionality in code I was developing, not for the fun or challenge of emulating numpy arrays.</div>


</div></blockquote><div><br></div></div><div>That&#39;s a good reminder to go back to the original question: what is the work-alike for slice assignment? Is set_selection() the best alternative?</div><div><div>
<br></div></div></div></div></blockquote><div>The answer to this is yes, set_selected() is currently the best way to do this in the cctbx. As well as flex.bool, set_selected() also accepts an iselection - a flex.size_t array containing the indices which you wish to modify. If you are using 2-d arrays, you can also use e.g. flex.double.matrix_paste_block_in_place().</div>


<div>�</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div>
<div></div><blockquote type="cite"><div dir="ltr"><div>I am unlikely to have time to add numpy-style assignment to slices in the near future, but if James, or indeed anyone else, requires and is willing to implement such behaviour, I&#39;m sure Nat can grant you svn commit access in order to make the changes you require (providing that the changes play well with the rest of the cctbx code and Phenix, etc.).</div>


</div></blockquote><div><br></div></div><div>I&#39;m not sure I&#39;d emulate numpy arrays in cctbx myself, as cool as they may be. It&#39;s usually better to try to find behavior that fits the rest of the library in &quot;look and feel&quot;. However, if I had SVN commit privileges, I&#39;d probably use them occasionally to fix problems I find or make enhancements that would aid my work.</div>


<span><font color="#888888"><div><br></div></font></span></div></div></blockquote><div style>Nat is the person to sort out SVN commit access.</div><div style><br></div><div style>Cheers,</div><div style><br></div><div style>

Richard</div><div>�</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">

<div><span><font color="#888888"><div></div><div>James</div></font></span><div><div><div><br></div><div><br></div><blockquote type="cite"><div class="gmail_extra"><div class="gmail_quote">On 11 April 2013 10:08, James Stroud <span dir="ltr">&lt;<a href="mailto:xtald00d@gmail.com" target="_blank">xtald00d@gmail.com</a>&gt;</span> wrote:<br>




<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><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 �but ...<br>




<br>On 11 Apr 2013, at 10:24, <a href="mailto:Graeme.Winter@Diamond.ac.uk" target="_blank">Graeme.Winter@Diamond.ac.uk</a> wrote:<br><br><blockquote type="cite">[...] <br></blockquote><blockquote type="cite"> �����������array[k,:,:] = image.as_numpy_array()[y0:y1, x0:x1]<br>




</blockquote><br>that&#39;s way more difficult!<br></div></blockquote></div><div><br></div><br></div><div>In python this type of magic is fairly straightforward because the slice is just a tuple:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">




<div><div><font face="Monaco">py&gt; class Example(object):</font></div></div><div><div><font face="Monaco">... � def __setitem__(self, i, v):</font></div></div><div><div><font face="Monaco">... � � print &quot;setting %s to %s&quot; % (i, v)</font></div>




</div><div><div><font face="Monaco">...</font></div></div><div><div><font face="Monaco">py&gt; e = Example()</font></div></div><div><div><font face="Monaco">py&gt; e[5:6, 4, 8:9, &quot;bob&quot;] = 2</font></div></div><div>




<div><font face="Monaco">setting (slice(5, 6, None), 4, slice(8, 9, None), &#39;bob&#39;) 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&#39;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 style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font face="Monaco">class Prototype(object):</font></div>




</div><div><div><font face="Monaco">� def __init__(self, data):</font></div></div><div><div><font face="Monaco">� � self.data = numpy.array(data)</font></div></div><div><div><font face="Monaco">� def __getitem__(self, i):</font></div>




</div><div><div><font face="Monaco">� � if isinstance (i, flex.grid):</font></div></div><div><div><font face="Monaco">� � � slices = [slice(*t) for t in zip(i.origin(), i.last())]</font></div></div><div><div><font face="Monaco">� � � return self.__class__(self.data[slices])</font></div>




</div><div><div><font face="Monaco">� � else:</font></div></div><div><div><font face="Monaco">� � � return self.__class__(self.data[i])</font></div></div><div><div><font face="Monaco">� def __setitem__(self, i, v):</font></div>




</div><div><div><font face="Monaco">� � if isinstance (i, flex.grid):</font></div></div><div><div><font face="Monaco">� � � slices = [slice(*t) for t in zip(i.origin(), i.last())]</font></div></div><div><div><font face="Monaco">� � � self.data[slices] = v</font></div>




</div><div><div><font face="Monaco">� � else:</font></div></div><div><div><font face="Monaco">� � � self.data[i] = v</font></div></div><div><div><font face="Monaco">� def __repr__(self):</font></div></div><div><div><font face="Monaco">� � 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 style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font face="Monaco">py&gt; grd = flex.grid((1, 1), (3, 3)) �# &lt;== selects [1:3, 1:3]</font></div>




</div><div><div><font face="Monaco">py&gt; ary = Prototype(numpy.arange(25).reshape((5,5)))</font></div></div><div><div><font face="Monaco">py&gt; ary</font></div></div><div><div><font face="Monaco">array([[ 0, �1, �2, �3, �4],</font></div>




</div><div><div><font face="Monaco">� � � �[ 5, �6, �7, �8, �9],</font></div></div><div><div><font face="Monaco">� � � �[10, 11, 12, 13, 14],</font></div></div><div><div><font face="Monaco">� � � �[15, 16, 17, 18, 19],</font></div>




</div><div><div><font face="Monaco">� � � �[20, 21, 22, 23, 24]])</font></div></div><div><div><font face="Monaco">py&gt; ary[grd]</font></div></div><div><div><font face="Monaco">array([[ 6, �7],</font></div></div><div><div>




<font face="Monaco">� � � �[11, 12]])</font></div></div><div><div><font face="Monaco">py&gt; ary[grd] = [[41, 42], [43, 44]]</font></div></div><div><div><font face="Monaco">py&gt; ary[grd]</font></div></div><div><div><font face="Monaco">array([[41, 42],</font></div>




</div><div><div><font face="Monaco">� � � �[43, 44]])</font></div></div><div><div><font face="Monaco">py&gt; ary</font></div></div><div><div><font face="Monaco">array([[ 0, �1, �2, �3, �4],</font></div></div><div><div><font face="Monaco">� � � �[ 5, 41, 42, �8, �9],</font></div>




</div><div><div><font face="Monaco">� � � �[10, 43, 44, 13, 14],</font></div></div><div><div><font face="Monaco">� � � �[15, 16, 17, 18, 19],</font></div></div><div><div><font face="Monaco">� � � �[20, 21, 22, 23, 24]])</font></div>




</div></blockquote><span><font color="#888888"><div><font face="Monaco"><br></font></div><div>James</div></font></span></div><br>_______________________________________________<br>
cctbxbb mailing list<br>
<a href="mailto:cctbxbb@phenix-online.org" target="_blank">cctbxbb@phenix-online.org</a><br>
<a href="http://phenix-online.org/mailman/listinfo/cctbxbb" target="_blank">http://phenix-online.org/mailman/listinfo/cctbxbb</a><br>
<br></blockquote></div><br></div>
_______________________________________________<br>cctbxbb mailing list<br><a href="mailto:cctbxbb@phenix-online.org" target="_blank">cctbxbb@phenix-online.org</a><br><a href="http://phenix-online.org/mailman/listinfo/cctbxbb" target="_blank">http://phenix-online.org/mailman/listinfo/cctbxbb</a><br>


</blockquote></div></div></div><br></div><br>_______________________________________________<br>
cctbxbb mailing list<br>
<a href="mailto:cctbxbb@phenix-online.org" target="_blank">cctbxbb@phenix-online.org</a><br>
<a href="http://phenix-online.org/mailman/listinfo/cctbxbb" target="_blank">http://phenix-online.org/mailman/listinfo/cctbxbb</a><br>
<br></blockquote></div><br></div></div>