<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><font class="Apple-style-span" size="4">Hello All,</font><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">I've noticed some incompatibilities between numpy and cctbx, which is understandable. However, the incompatibilities manifest either as nonsense errors or, worse, not at all.</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">Following are a couple of examples I have found.</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">1. The first example should really throw an exception, because silent failures like this can be catastrophic:</font></div><div><font class="Apple-style-span" size="4"><br></font></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font class="Apple-style-span" face="Monaco">py&gt; from numpy import random</font></div><div><font class="Apple-style-span" face="Monaco">py&gt; r = random.randint(2, size=10)</font></div><div><font class="Apple-style-span" face="Monaco">py&gt; r</font></div><div><font class="Apple-style-span" face="Monaco">array([1, 0, 1, 1, 0, 1, 0, 1, 1, 1])</font></div><div><font class="Apple-style-span" face="Monaco">py&gt; list(flex.bool(r))</font></div><div><font class="Apple-style-span" face="Monaco">[True, False, False, False, False, False, False, False, False, False]</font></div></blockquote><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">This example is clearly due to incorrect assumptions about the internal representations of numpy ndarrays.</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4"><b><u>Much</u></b>&nbsp;better behavior can be seen when using a sequence of python `int` objects:</font></div><div><font class="Apple-style-span" size="4"><br></font></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font class="Apple-style-span" face="Monaco"><div>py&gt; list(flex.bool(range(10)))</div></font></div><div><font class="Apple-style-span" face="Monaco"><div>---------------------------------------------------------------------------</div></font></div><div><font class="Apple-style-span" face="Monaco"><div>TypeError &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Traceback (most recent call last)</div></font></div><div><font class="Apple-style-span" face="Monaco"><div>/Users/jstroud/Unison/Code/radialx/testdata/&lt;ipython-input-754-dce96b02979e&gt; in &lt;module&gt;()</div></font></div><div><font class="Apple-style-span" face="Monaco"><div>----&gt; 1 list(flex.bool(range(10)))</div></font></div><div><font class="Apple-style-span" face="Monaco"><div><br></div></font></div><div><font class="Apple-style-span" face="Monaco"><div>TypeError: No registered converter was able to produce a C++ rvalue of type bool from this Python object of type int</div></font></div></blockquote><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">2. Although not as potentially catastrophic as the first, t</font><span class="Apple-style-span" style="font-size: large; ">he second example should (in a perfect world) either work or throw a more meaningful exception:</span></div><div><font class="Apple-style-span" size="4"><br></font></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font class="Apple-style-span" face="Monaco">py&gt; flex.int(range(10))[r[0]]</font></div><div><font class="Apple-style-span" face="Monaco">---------------------------------------------------------------------------</font></div><div><font class="Apple-style-span" face="Monaco">TypeError &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Traceback (most recent call last)</font></div><div><font class="Apple-style-span" face="Monaco">/Users/jstroud/Unison/Code/radialx/testdata/&lt;ipython-input-753-f14587f0ceeb&gt; in &lt;module&gt;()</font></div><div><font class="Apple-style-span" face="Monaco">----&gt; 1 flex.int(range(10))[r[0]]</font></div><div><font class="Apple-style-span" face="Monaco"><br></font></div><div><font class="Apple-style-span" face="Monaco">TypeError: 'numpy.int64' object is not iterable</font></div></blockquote><div><br></div><div><font class="Apple-style-span" size="4">This example is obviously a result of type checking for a python `int`. The usual python approach of duck-type checking would avoid this problem:</font></div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font class="Apple-style-span" face="Monaco">def __getitem__(self, i):</font></div><div><font class="Apple-style-span" face="Monaco">&nbsp; try:</font></div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; i = int(i)</font></div><div><font class="Apple-style-span" face="Monaco">&nbsp; except:</font></div><div><font class="Apple-style-span" face="Monaco">&nbsp; &nbsp; pass</font></div><div><font class="Apple-style-span" face="Monaco">&nbsp; return do_whatever_with_i(i)</font></div></blockquote><div><br></div><br><font class="Apple-style-span" size="4">James</font></body></html>