-
Notifications
You must be signed in to change notification settings - Fork 280
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
check for _unit_system_name
in save_as_dataset
#4316
check for _unit_system_name
in save_as_dataset
#4316
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this happening because of something special about the base dataset? Why does it not have a unit system name? It might be worth understanding this before this goes in.
yt/frontends/ytdata/utilities.py
Outdated
if hasattr(ds, "_unit_system_name"): | ||
_yt_array_hdf5_attr(fh, "unit_system_name", ds._unit_system_name) | ||
else: | ||
_yt_array_hdf5_attr( | ||
fh, "unit_system_name", ds.unit_system.name.split("_")[0] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like this? Note, I haven't tested if it solves the problem, just a code suggestion.
if hasattr(ds, "_unit_system_name"): | |
_yt_array_hdf5_attr(fh, "unit_system_name", ds._unit_system_name) | |
else: | |
_yt_array_hdf5_attr( | |
fh, "unit_system_name", ds.unit_system.name.split("_")[0] | |
) | |
name = getattr(ds, "_unit_system_name", ds.unit_system.name.split("_")[0]) | |
_yt_array_hdf5_attr(fh, "unit_system_name", name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the digging this morning, I think it might also be totally sufficient only write the _unit_system_name
attribute
_yt_array_hdf5_attr(fh, "unit_system_name", ds._unit_system_name)
I initially wasn't 100% sure that having unit_system
ensures always having _unit_system_name
and so I put it in the if block, but I suspect it might be the case. I'll check on that more thoroughly...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up changing how the unit unit_system.name
is initially set
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually ended up having to switch back to this approach. But this section is now only writing ds._unit_system_name
I agree! Should have marked it as a WIP on Friday when I pushed it up :) But I think I figured it out after a bit more digging... The following import yt
ds = yt.load("AM06/AM06.out1.00300.athdf")
print(ds.unit_system.name) prints out That string comes from how the code def create_code_unit_system(unit_registry, current_mks_unit=None):
code_unit_system = UnitSystem(
name=unit_registry.unit_system_id,
length_unit="code_length",
mass_unit="code_mass",
time_unit="code_time",
temperature_unit="code_temperature",
current_mks_unit=current_mks_unit,
registry=unit_registry,
)
<----------- TRIMMED ---------------> That The history around that name="code_{}".format(unit_registry.unit_system_id), which actually would fix the bug here since when you call _yt_array_hdf5_attr(
fh, "unit_system_name", ds.unit_system.name.split("_")[0]
) but then the The reason why you need to restart the session to get the error is because import yt
print(yt.unit_system_registry.keys())
ds = yt.load("AM06/AM06.out1.00300.athdf")
print(yt.unit_system_registry.keys()) prints
but when you restart your kernel, So in the end, I think my fix here might actually be a good approach. I'm also still thinking about how to write a test for this -- it's tricky because it's session-scope dependent. There might be a nice way to do it with pytest... |
_unit_system_name
in save_as_dataset
_unit_system_name
in save_as_dataset
changed to WIP for now while I test things out more carefully -- feel free to continue commenting though! I just didn't want it to get merged inadvertently... |
I think we completely missed this in #2728, great job finding your way back there ! If just re-adding the |
_unit_system_name
in save_as_dataset
_unit_system_name
in save_as_dataset
I agree! It is simpler, and it does fix the bug here. I did add a minimal unit test -- it's not a full test of the bug, but it at least checks that loading back in a saved dataset has the proper |
Ok, so turns out switching the name to include the Apologies for the back and forth on this. But I think I should be done now assuming all the tests pass... |
If tests pass this time, I suggest rebasing the branch to a single commit so the history will hopefully be easier to follow in the future |
03144f3
to
e66c31c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @chrishavlin !
I'll give @brittonsmith some time to respond before I merge
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
Closes #4315
There might be a better way to fix this, I'm not super familiar with the unit system attributes and how they're handled on re-load. But I wanted to get this in before the weekend.