Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce_units? #2076

Open
rwijtvliet opened this issue Nov 9, 2024 · 3 comments
Open

reduce_units? #2076

rwijtvliet opened this issue Nov 9, 2024 · 3 comments

Comments

@rwijtvliet
Copy link

Using these definitions:

#unitdefinitions.txt

mega- = 1e6 = M-

### Base dimensions and their units. Base unit without prefix. ###

# Dimensionless
fraction = []

# Duration (Time)
hour = [time] = h = hr

# Energy
watthour = [energy] = Wh

# Revenue (Currency)
euro = [currency] = Eur = €

### Derived dimensions and their units. ###

# Power
[power] = [energy] / [time]
watt = watthour / hour = W

# Price
[price] = [currency] / [energy]
euro_per_MWh = euro / (1_000_000 * watthour)  = Eur/MWh

I often get into a situation where a quantity has redundant units, even though auto_reduce_dimensions is set to True. For example:

 import pint
 ureg = pint.UnitRegistry(str(path), auto_reduce_dimensions=True)
 
power = ureg.Quantity(5, 'MW')
duration = ureg.Quantity(2, 'h')
price = ureg.Quantity(1, 'Eur/MWh')
revenue = power * duration * price # in euros

revenue
<Quantity(10, 'megawatt * hour * euro / megawatthour')>   # wat

revenue.dimensionality
<UnitsContainer({'[currency]': 1})> # as expected

Is there an issue with my config?

I know I can do revenue.to('Eur'), but I'd like to avoid having to do this.

@andrewgsavage
Copy link
Collaborator

https://pint.readthedocs.io/en/stable/getting/tutorial.html#simplifying-units

There are also methods to_reduced_units() and ito_reduced_units() which perform a simplified dimensional reduction, combining units with the same dimensionality but otherwise keeping your unit definitions intact.

it'll reduce m and mm. megawatt * hour are treated as MW and hour, which have different dimensionality to megawatthour so won't reduce

try using to_base_units or to_preffered

@rwijtvliet
Copy link
Author

rwijtvliet commented Nov 9, 2024

Thanks for that fast reply. I think to_preferred is actually what I want.

One follow-up question if I may, as I cannot find it in the docs.

If I set autoconvert_to_preferred=True when initialising a unit registry - where do I specify, what those preferred units are?

@andrewgsavage
Copy link
Collaborator

andrewgsavage commented Nov 9, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants