<div dir="ltr">I'm converting my code to use astropy.units, and decided to use it mainly at the interface level, and use my own set of fixed units inside the software units. <div><br></div><div>That said, it seems to me that it would be useful if you could set some flag to tell the code that the units are not going to change until you unset the flag. Then the "y = xx.value" steps are not needed. Or perhaps this is a really stupid idea because I don't know the ins and outs. .. ;-)</div><div><br></div><div>Paul  </div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 24, 2015 at 11:41 PM, Erik Bray <span dir="ltr"><<a href="mailto:embray@stsci.edu" target="_blank">embray@stsci.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 08/24/2015 06:32 PM, Derek Homeier wrote:<br>
> On 24 Aug 2015, at 3:57 pm, Kevin Gullikson <<a href="mailto:kevin.gullikson@gmail.com">kevin.gullikson@gmail.com</a>> wrote:<br>
>><br>
>> I am writing an MCMC fitting code, and my current implementation uses the (very useful) astropy quantities framework to take care of unit conversions and such. However, the code is surprisingly slow so I profiled it with %prun and it looks like the quantity utilities that wrap numpy functions are taking a huge chunk of time. Is that just the price of convenience?<br>
>><br>
> I am not an expert on the quantities/units implementation, but I would imagine that they could indeed<br>
> introduce a significant overhead, as the class adds a number of validations and checks for a function<br>
> calling a quantity object. Especially with rather numerous operations on comparatively small arrays<br>
> this could become problematic.<br>
> Did you experiment with using decorators as described in<br>
> <a href="http://astropy.readthedocs.org/en/stable/units/quantity.html#functions-accepting-quantities" rel="noreferrer" target="_blank">http://astropy.readthedocs.org/en/stable/units/quantity.html#functions-accepting-quantities</a><br>
><br>
> - iiuc this would allow bypassing some of the validations?<br>
<br>
</span>I don't think this is really relevant to the OP's question (though always good<br>
to know about).  This decorator just makes it easy to bypass writing a lot of<br>
boilerplate code for checking the units of its inputs--it doesn't remove any<br>
overhead (in fact it adds it--but this was used for cases where that overhead<br>
already existed--the code was just highly repetitive).<br>
<span class=""><br>
> Another option might be to only pass plain numpy arrays to the innermost loops of your code with .value,<br>
> of course at the expense of some of the convenience gained in the first place...<br>
<br>
</span>Right, that's the big problem.  The thing Quantity supports (which most similar<br>
libraries in Python don't) is that it *is* a Numpy ndarray, and with few<br>
exceptions [1] works transparently with all of Numpy's universal functions.<br>
However, Numpy currently doesn't make it easy to wrap calls to ufuncs, so<br>
there's some overhead associated with redoing unit conversions on every function<br>
call (and even if Numpy did make this easier, as it will in 1.10, performing<br>
unit conversions repeatedly in a loop will add significant overhead to any<br>
fitting operation, so better to do the unit conversions first so that all<br>
quantities are expressed in the same base units and orders of magnitude, then as<br>
Derek suggested pass in the raw arrays (from .value) to high performance code.<br>
<br>
Having a mechanism to automate such conversions will be useful I think.<br>
<br>
Erik<br>
<br>
<br>
[1] <a href="http://astropy.readthedocs.org/en/stable/known_issues.html#quantity-issues" rel="noreferrer" target="_blank">http://astropy.readthedocs.org/en/stable/known_issues.html#quantity-issues</a><br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@scipy.org">AstroPy@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">http://mail.scipy.org/mailman/listinfo/astropy</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><br>* * * * * * * * <a href="http://www.mssl.ucl.ac.uk/~npmk/" target="_blank">http://www.mssl.ucl.ac.uk/~npmk/</a> * * * *<br>Dr. N.P.M. Kuin      (<a href="mailto:n.kuin@ucl.ac.uk" target="_blank">n.kuin@ucl.ac.uk</a>)      <br>phone +44-(0)1483 (prefix) -204927 (work) <br>mobile +44(0)7806985366  skype ID: npkuin<br>Mullard Space Science Laboratory  – University College London  –<br>Holmbury St Mary – Dorking – Surrey RH5 6NT–  U.K.</div></div>
</div></div>