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

Q_ triggers a warning when using as_compact #614

Open
vhirtham opened this issue Oct 25, 2021 · 2 comments
Open

Q_ triggers a warning when using as_compact #614

vhirtham opened this issue Oct 25, 2021 · 2 comments
Labels
units unit handling and pint

Comments

@vhirtham
Copy link
Collaborator

vhirtham commented Oct 25, 2021

When running the following script:

from weldx import Q_
from pint import Quantity

Q_(1000000020.,"s").to_compact()

I get the following warning:

RuntimeWarning: to_compact applied to non numerical types has an undefined behavior.
  Q_(1000000020.,"s").to_compact()

and the "wrong" unmodified output:

1000000020.0
s

Exchanging Q_ with Quantity removes the warning and the output is as wanted:

1.00000002 gigasecond

Here is the corresponding exception block at the beginning of the implementation from to_compact:

if not isinstance(self.magnitude, numbers.Number):
            msg = (
                "to_compact applied to non numerical types "
                "has an undefined behavior."
            )
            w = RuntimeWarning(msg)
            warnings.warn(w, stacklevel=2)
            return self

Guess it has something to do with our custom additions.

@vhirtham vhirtham added bug Something isn't working units unit handling and pint labels Oct 25, 2021
@CagtayFabry
Copy link
Member

As mentioned I guess this is because handling arrays that cover a wide magnitude range of values are not so easy to assign a 'compact' unit.
Our unit registry is configured to convert all values to arrays (even scalars) which is why it never works with Q_

we could implement this ourselves in some way but I am fine not having this functionality for now

@CagtayFabry CagtayFabry removed the bug Something isn't working label Oct 25, 2021
@vhirtham
Copy link
Collaborator Author

As mentioned I guess this is because handling arrays that cover a wide magnitude range of values are not so easy to assign a 'compact' unit. Our unit registry is configured to convert all values to arrays (even scalars) which is why it never works with Q_

we could implement this ourselves in some way but I am fine not having this functionality for now

It is not super important. A simple fix that would at least work for the tutorials is to override the function and turn length=1 arrays back into a number type and then pass the new quantity to the parent class function.

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

No branches or pull requests

2 participants