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

BUG: fix an issue where array functions would raise UnitConsistencyError on unyt arrays using non-default unit registries #463

Merged

Conversation

neutrinoceros
Copy link
Member

Address the first part of #462

@neutrinoceros neutrinoceros force-pushed the hotfix_unit_consistency_validation branch from c57f31d to 9dbd8ec Compare November 1, 2023 21:51
@neutrinoceros neutrinoceros changed the title UG: fix an issue where array functions would raise UnitConsistencyError on unyt arrays using non-default unit registries BUG: fix an issue where array functions would raise UnitConsistencyError on unyt arrays using non-default unit registries Nov 1, 2023
@neutrinoceros neutrinoceros self-assigned this Nov 1, 2023
@neutrinoceros neutrinoceros added the bug Something isn't working label Nov 1, 2023
sunits = set(units)
if len(sunits) == 1:
unique_units = set(units)
if len(unique_units) == 1 or all(u.is_dimensionless for u in unique_units):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain a little bit more how this resolves the issue (sorry, it may be because it's late for me)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem was that

  • Unit() and Unit(registry=UnitRegistry()) have different hash, so they are considered as 2 separate unique units
  • Unit() (or NULL_UNIT is this context) gets automatically inserted for non-unyt array-like data structures only because we want them to be treated as dimensionless

This condition makes it so all dimensionless units are treated as equal as long as nothing else is received.

@jzuhone
Copy link
Contributor

jzuhone commented Nov 2, 2023

What happens if the unit is dimensionless but is not the dimensionless unit? Say, what happens if it is Zsun?

@neutrinoceros
Copy link
Member Author

neutrinoceros commented Nov 2, 2023

What happens if the unit is dimensionless but is not the dimensionless unit? Say, what happens if it is Zsun?

that's good question. I think we should replicate the behaviour of basic operations (like addition), which I believe my current patch does. At the moment, 1*Zsun+1 or 1*Zsun+1*dimensionless are all treated as valid operations (in unyt 2.9.5 as well as 3.0.0). I must say I don't understand the result though, or why 1*Zsun + 1 != 1 + 1*Zsun, is this another bug ?

@jzuhone
Copy link
Contributor

jzuhone commented Nov 2, 2023

@neutrinoceros they are equal--I just checked:

import unyt as u
a = 1+1.0*u.Zsun
b = 1.0*u.Zsun+1
print(a)
print(b)
print(a == b)

gives:

1.01295 dimensionless
78.22007722007721 Zsun
True

@jzuhone jzuhone merged commit e59cea9 into yt-project:main Nov 2, 2023
@neutrinoceros neutrinoceros deleted the hotfix_unit_consistency_validation branch November 2, 2023 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants