Shailesh Appukuttan
Shailesh Appukuttan

Reputation: 830

How to specify a scalar multiplier for units when using Quantities?

The objective is to handle cell densities expressed as "1000/mm^3", i.e. thousands per cubic millimeter.

Currently I do this to handle "1/mm^3":

import quantities as pq
d1 = pq.Quantity(500000, "1/mm**3")

which gives:

array(500000) * 1/mm**3

But what I really need to do is to accept the values with units of "1000/mm^3". This should also be the form in which values are printed. When I try something like:

d1 = pq.Quantity(5, 1000/pq.mm**3)

I get the following error:

ValueError: units must be a scalar Quantity with unit magnitude, got 1000.0 1/mm**3

And if I try:

a = pq.Quantity(500, "1000/mm**3")

The output is:

array(500) * 1/mm**3

i.e. The 1000 just gets ignored. Any idea how can I fix this? Any workaround?

(The requirement arises from the standard practice followed in the domain.)

Upvotes: 1

Views: 127

Answers (1)

Shailesh Appukuttan
Shailesh Appukuttan

Reputation: 830

One possible solution I have found is to create new units such as this:

k_per_mm3 = pq.UnitQuantity('1000/mm3', 1e3/pq.mm**3, symbol='1000/mm3')
d1 = pq.Quantity(500, k_per_mm3)

Then on printing 'd1', I get:

array(500) * 1000/mm3

which is what I required.

Is this the only way to do this? Or can the same be achieved with existing units (which is preferable)?

Upvotes: 1

Related Questions