Plasmapy: Flag to ignore units

Created on 2 Nov 2017  路  9Comments  路  Source: PlasmaPy/PlasmaPy

Just having gone through this with scipy.integrate.tplquad for integrating over distribution functions, it might be handy to implement a similar flag for all functions where we wish to ignore the checking and converting of units in certain cases. This would, in effect, be an optimization flag for speeding up code that doesn't play well with all the unit calls.

Opinions?

Feature request Refactoring

Most helpful comment

@Cadair wrote a good part of astropy.units

This is wildly overstating the facts 馃槃 I contributed u.quantity_input.

My general feeling is some performance cost is worth units, but performance sensitive code should be unitless where appropriate. Just make sure to balance that with the maintenance burden of the extra complexity.

All 9 comments

Relates to #59

Those performance issues are exactly what I was afraid of when I was initially looking into the units package...

I'm not sure this needs to be optional. There's going to be code where units are an issue
(like your tplquad integration case), and then there's going to be code where adding units doesn't matter much - most of physics.parameters comes to mind. There's not much point in having a flag to disable unit checks for a function consisting of three multiplications and a sqrt.

How about this as a general framework for functions:

  1. Get inputs, check units
  2. If this is a computationally intensive function, drop units
  3. Do the calculations quickly
  4. If units were dropped, add them back on the way out

Of course we'd need to have that code tested properly and slowly the first time it's added to make sure we're not making any mistakes ourselves.

Does that make sense?

In some cases, like tplquad, it won't work because the inputs and outputs have to be unitless. So you just end up with an entirely unitless function. Also, I don't know how much dropping units within the function saves you on time. My guess is that this will heavily depend on what this function is being used for. If you're running something which is essentially a thin-wrapper over some highly efficient C code, then I assume passing around these astropy objects will kill many of the optimizations... really depends on how close astropy is to numpy I think.

Dropping units is basically free. Converting units is obviously not. Stripping units and then putting them back will take some time but should not be as bad as some weird C layer side effects of having a ndarray subclass.

@Cadair on principle I agree with you, but has this been tested?

@Cadair wrote a good part of astropy.units, so I'd be willing to trust him on this one. :smile:

fair play 馃榿 . I'm still going to plead ignorance and ask if this has been tested. Sometimes one finds surprises 馃槣

With a simple notebook, we can see that droping units for a simple multiplication can increase the performance by 2...

We could start with the framework sugjected by @StanczakDominik , and if the performence really is an issue add a flag...

There is the notebook:

import astropy.units as u
a = 1 * u.m
b = 2* u.s
%%timeit
c = a * b

56.4 碌s 卤 1.01 碌s per loop (mean 卤 std. dev. of 7 runs, 10000 loops each)
%%timeit

aprim = a.value
bprim = b.value

c = aprim*bprim

c *= u.m*u.s
28.7 碌s 卤 165 ns per loop (mean 卤 std. dev. of 7 runs, 10000 loops each)

@Cadair wrote a good part of astropy.units

This is wildly overstating the facts 馃槃 I contributed u.quantity_input.

My general feeling is some performance cost is worth units, but performance sensitive code should be unitless where appropriate. Just make sure to balance that with the maintenance burden of the extra complexity.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

namurphy picture namurphy  路  3Comments

StanczakDominik picture StanczakDominik  路  5Comments

lemmatum picture lemmatum  路  6Comments

lemmatum picture lemmatum  路  4Comments

namurphy picture namurphy  路  3Comments