SymbolicML / DynamicQuantities.jl

Efficient and type-stable physical quantities in Julia
https://symbolicml.org/DynamicQuantities.jl/dev/
Apache License 2.0
132 stars 17 forks source link

Adding "missing" units #69

Closed jkrumbiegel closed 11 months ago

jkrumbiegel commented 11 months ago

Symbolic units have milligrams and micrograms, but for example not nanograms.

julia> us"mg"
1.0 mg

julia> us"ng"
ERROR: LoadError: UndefVarError: `ng` not defined
Stacktrace:
 [1] top-level scope
   @ :0
 [2] eval
   @ ./boot.jl:370 [inlined]
 [3] eval
   @ ~/.julia/packages/DynamicQuantities/ZX9NZ/src/symbolic_dimensions.jl:266 [inlined]
 [4] sym_uparse(raw_string::String)
   @ DynamicQuantities.SymbolicUnitsParse ~/.julia/packages/DynamicQuantities/ZX9NZ/src/symbolic_dimensions.jl:340
 [5] var"@us_str"(__source__::LineNumberNode, __module__::Module, s::Any)
   @ DynamicQuantities ~/.julia/packages/DynamicQuantities/ZX9NZ/src/symbolic_dimensions.jl:367
in expression starting at REPL[5]:

I could not determine from the docs if this system is extensible, or whether one would have to define their own units to add nanograms (and how to do that).

MilesCranmer commented 11 months ago

I didn't add n for grams yet but we can definitely add it. To do this we simply change this line:

https://github.com/SymbolicML/DynamicQuantities.jl/blob/e5f4305829eac76864720dfdb0b051c29c9ca2b7/src/units.jl#L55

and add n.

If you see the available prefixes: https://github.com/SymbolicML/DynamicQuantities.jl/blob/e5f4305829eac76864720dfdb0b051c29c9ca2b7/src/units.jl#L54-L60

you can see most combinations don't exist – I basically just didn't want to blow up the list of symbols until someone actually has a need for them (e.g., I don't think people use kilosecond (?), so I didn't add ks). But since you do have a need for ng, let's add it!

jkrumbiegel commented 11 months ago

We also use picograms, so we could add that, too. kilosecond, I agree, is not common. But in pharmacology, small quantities are pretty common.

This @add_prefixes macro cannot be called dynamically, right? If we had the need for special combinations you don't want to add, would we be fine copying the unit code with our modifications for our own dimensions type? Is that how the system is intended?

MilesCranmer commented 11 months ago

No, it would have to be internal to the library – @add_prefixes cannot be called later. But I really don't mind adding it! I just wanted to play it by ear and not add units until people asked for them, since I don't know what units are common in which fields.

MilesCranmer commented 10 months ago

I'm also thinking of ways we could enable libraries to add more units upon loading... I'm not sure if such a strategy is dangerous or not.