<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Apr 4, 2013, at 3:15 AM, Luc Bourhis wrote:</div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><span class="Apple-style-span" style="font-size: large; ">1. The first example should really throw an exception, because silent failures like this can be catastrophic:</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; 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></blockquote><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">As far as I can tell, this is much worse than that as the numpy-to-flex conversion assumes the same element type in the source and target array.&nbsp;Thus converting a numpy array of int's into a flex array of bools is illegal but this precondition is not asserted unfortunately.</font></div></div></div></blockquote><div><blockquote type="cite"><font class="Apple-style-span" size="4">Nasty indeed. I do not understand numpy well enough to propose a solution, I am afraid.</font></blockquote></div></div><font class="Apple-style-span" size="4"><br></font><div><font class="Apple-style-span" size="4">On the python side, the correct types for which to check have names that mirror the python names:</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"><div>py&gt; numpy.complex is type(complex())</div><div>True</div><div>py&gt; numpy.bool is type(bool())</div><div>True</div><div>py&gt; numpy.int is type(int())</div><div>True</div><div>py&gt; numpy.float is type(float())</div><div>True</div><div><div>py&gt; numpy.long is type(long())</div><div>True</div></div></font></div></blockquote><div style="font-family: Monaco; "><font class="Apple-style-span" face="Monaco"><br></font></div><div><font class="Apple-style-span" size="4">It is unfortunate that array creation by numpy does not respect this naming symmetry:</font></div><div style="font-family: Monaco; "><font class="Apple-style-span" face="Monaco"><br></font></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><font class="Apple-style-span" face="Monaco"><div><font class="Apple-style-span" face="Monaco"><div><div>py&gt; numpy.array([1, 2, 3]).dtype is int</div><div>False</div></div><div>py&gt; numpy.array([1, 2, 3], dtype=int).dtype is int</div></font></div></font><font class="Apple-style-span" face="Monaco"><div><font class="Apple-style-span" face="Monaco"><div>False</div><div>py&gt; # same as</div><div><div>py&gt; numpy.array([1, 2, 3], dtype=numpy.int).dtype is int</div><div>False</div></div></font></div></font></blockquote><div style="font-family: Monaco; "><font class="Apple-style-span" face="Monaco"><br></font></div><div><font class="Apple-style-span" size="4">So, even if numpy-to-flex conversion catches wrongly [1] typed arrays, users still need to know which numpy dtypes to use for which flex constructors. Here is what I have determined:</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><ul><li><span class="Apple-style-span" style="font-size: large; "><b>flex.bool</b> : numpy.bool</span></li><li><span class="Apple-style-span" style="font-size: large; "><b>flex.int</b>&nbsp;: numpy.int32</span></li><li><span class="Apple-style-span" style="font-size: large; "><b>flex.long</b>&nbsp;: numpy.int64, numpy.int</span></li><li><span class="Apple-style-span" style="font-size: large; "><b>flex.float</b>&nbsp;: numpy.float32</span></li><li><span class="Apple-style-span" style="font-size: large; ">f<b>lex.double</b> :&nbsp;numpy.float, numpy.double</span></li></ul></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">In all cases, if a wrongly typed numpy array is used then flex will fail silently as of build 2013_02_27_0005. Here's one more example:</font></div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><span class="Apple-style-span" style="font-family: Monaco; ">py&gt; numpy.arange(4)</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">py&gt;&nbsp;array([0, 1, 2, 3])</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">py&gt; list(flex.int(numpy.arange(4)))</span></div></div><div><div><font class="Apple-style-span" face="Monaco">[0, 0, 1, 0]</font></div></div></blockquote><font class="Apple-style-span" size="4"><br></font><div><font class="Apple-style-span" size="4">James</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">[1] The concept of "wrongly" typed objects doesn't truly exist in python.</font></div><div><font class="Apple-style-span" face="Monaco"><br></font></div></body></html>