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

Dimension Conversion Fails in Mixed Dimension Quantities #2072

Open
julian-belina opened this issue Oct 31, 2024 · 1 comment
Open

Dimension Conversion Fails in Mixed Dimension Quantities #2072

julian-belina opened this issue Oct 31, 2024 · 1 comment

Comments

@julian-belina
Copy link

Hi there,

Unfortunately I ran into a problem with converting mixed dimensions using contexts. I want to convert one dimension into another dimension in a mixed quantity like 1*[MyDimension]/[km] -->1.43*[MyOtherDimension]/[km]. If I only use my self defined dimensions 1*[MyDimension]] -->1.43*[MyOtherDimension], everything works as expected. However, if I mix the dimensions, the conversion fails. I have come up with the following minimal example:

import pathlib

import pint

# generate pint registry
unit_registry = pint.UnitRegistry(system="SI")
unit_registry.formatter.default_format = "~"

# add currency dimensions to pint registry
current_folder = pathlib.Path(__file__).parents[0]
path_to_registry_definition = current_folder.joinpath(r"unit_def_file.txt")
unit_registry.load_definitions(path_to_registry_definition)


# Create quantity from self defined unit in dimension 1
my_quantity = 1 * unit_registry.Unit("my_unit_1_dim_1")
# Convert to unit within the same dimension --> Works
print("Conversion 1", my_quantity.to("my_unit_2_dim_1"))

# Convert to unit in other dimension --> Works
print(
    "Conversion to other dim:", my_quantity.to("my_unit_1_dim_2", "context_dim_1_dim_2")
)
# Convert standard units using the the self defined context -->works
length_quantity = 1 * unit_registry.Unit("km")
print(length_quantity.to("m", "context_dim_1_dim_2"))

# Convert self from one self defined dimension to other self defined dimension with composite units --> Fails
my_composite_quantity = 1 * unit_registry.Unit("my_unit_1_dim_1/km")
print(my_composite_quantity.to("my_unit_1_dim_2/km", "context_dim_1_dim_2"))

Exception has occurred: DimensionalityError
Cannot convert from 'my_unit_1_dim_1 / kilometer' ([my_dimension_1] / [length]) to 'my_unit_1_dim_2 / kilometer' ([my_dimension_2] / [length]

This is the context of my definition file :unit_def_file.txt

my_unit_1_dim_1=[my_dimension_1]

my_unit_2_dim_1 = my_unit_1_dim_1 2.5 

my_unit_1_dim_2=[my_dimension_2]

my_unit_2_dim_2 = my_unit_1_dim_2 4

@context context_dim_1_dim_2
	[my_dimension_1] -> [my_dimension_2] : value * 3.5 * my_unit_1_dim_2/my_unit_1_dim_1
	[my_dimension_2] -> [my_dimension_1] : (value / 3.5) * my_unit_1_dim_1/my_unit_1_dim_2
@end

I would like to use these conversions to implement inflation rates and different currencies, while also dealing with other units. A typical use case would be to convert €/l to $/gallon.

Is this a bug, have I misconfigured something or is the functionality just not implemented?Any help would be appreciated.

@julian-belina julian-belina changed the title Units with comp Dimension Conversion Fails in Mixed Dimension Quantities Oct 31, 2024
@chrisjwin
Copy link

second

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