hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.32k stars 457 forks source link

Can I create substance specific units? #1287

Open Midnighter opened 3 years ago

Midnighter commented 3 years ago

Hello,

I work in the biochemistry/biotechnology area and often have to handle substance specific units. What I mean by that is, for example, concentration measurements given in units of gram cell dry weight (gCDW) such that when I have 0.02 gram of glucose per gram cell dry weight

  1. the units should not cancel out and
  2. concentrations for different substances (compounds) should not be additive. So I should not be able to add 0.03 gram of pyruvate per gram cell dry weight to the 0.02 gram of glucose per gram cell dry weight. However, I should be able to add concentrations of the same compound.

Can I achieve this with pint? I have seen pyEQL and its Solution class looks quite useful but is not directly what I need. Thank you for any pointers.

mikapfl commented 3 years ago

Hi,

From a strict metrological standpoint, the substance is not a part of the unit. This is how in everyday life we use units, a kilogram is a kilogram regardless if you measure the mass of apples or flour or whatever (and, often the units are properly additive in that context, 250 g of flour and 100 g of sugar surely is 350 g of a sugar-flour-mixture). However, in many scientific disciplines, the description of the entity being measured is also put into the "unit", and if you want to, you can do that with pint; however, it is some work. If you want to see how it can be done, have a look at https://github.com/openscm/openscm-units , where this is done to have pseudo-units for different gases in a climate context. Most of the magic happens in https://github.com/openscm/openscm-units/blob/master/src/openscm_units/_unit_registry.py . It has the property that, as you describe, different gases cannot be added together and the gas pseudo-units don't cancel out.

Cheers,

Mika