Skip to content

Commit

Permalink
Merge pull request #4316 from chrishavlin/fix_unit_system_name_reload
Browse files Browse the repository at this point in the history
Closes #4315
  • Loading branch information
neutrinoceros authored Feb 20, 2023
2 parents a6dc72c + baf6074 commit 3c564f2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nose_unit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ nologcapture=1
verbosity=2
where=yt
with-timer=1
ignore-files=(test_load_errors.py|test_load_sample.py|test_commons.py|test_ambiguous_fields.py|test_field_access_pytest.py|test_save.py|test_line_annotation_unit.py|test_eps_writer.py|test_registration.py|test_invalid_origin.py|test_outputs_pytest\.py|test_normal_plot_api\.py|test_load_archive\.py|test_stream_particles\.py|test_file_sanitizer\.py|test_version\.py|\test_on_demand_imports\.py|test_set_zlim\.py|test_add_field\.py|test_glue\.py|test_geometries\.py|test_firefly\.py|test_callable_grids\.py|test_external_frontends\.py|test_stream_stretched\.py|test_sanitize_center\.py)
ignore-files=(test_load_errors.py|test_load_sample.py|test_commons.py|test_ambiguous_fields.py|test_field_access_pytest.py|test_save.py|test_line_annotation_unit.py|test_eps_writer.py|test_registration.py|test_invalid_origin.py|test_outputs_pytest\.py|test_normal_plot_api\.py|test_load_archive\.py|test_stream_particles\.py|test_file_sanitizer\.py|test_version\.py|\test_on_demand_imports\.py|test_set_zlim\.py|test_add_field\.py|test_glue\.py|test_geometries\.py|test_firefly\.py|test_callable_grids\.py|test_external_frontends\.py|test_stream_stretched\.py|test_sanitize_center\.py|test_data_reload\.py)
exclude-test=yt.frontends.gdf.tests.test_outputs.TestGDF
1 change: 1 addition & 0 deletions tests/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ other_tests:
- "--ignore-file=test_firefly\\.py"
- "--ignore-file=test_callable_grids\\.py"
- "--ignore-file=test_external_frontends\\.py"
- "--ignore-file=test_data_reload\\.py"
- "--ignore-file=test_stream_stretched\\.py"
- "--ignore-files=test_sanitize_center\\.py"
- "--exclude-test=yt.frontends.gdf.tests.test_outputs.TestGDF"
Expand Down
26 changes: 26 additions & 0 deletions yt/frontends/ytdata/tests/test_data_reload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import numpy as np

from yt.loaders import load_uniform_grid
from yt.testing import requires_module_pytest
from yt.utilities.on_demand_imports import _h5py as h5py


@requires_module_pytest("h5py")
def test_save_as_data_unit_system(tmp_path):
# This test checks that the file saved with calling save_as_dataset
# for a ds with a "code" unit system contains the proper "unit_system_name".
# It checks the hdf5 file directly rather than using yt.load(), because
# https://github.com/yt-project/yt/issues/4315 only manifested restarting
# the python kernel (because the unit registry is state dependent).

fi = tmp_path / "output_data.h5"
shp = (4, 4, 4)
data = {"density": np.random.random(shp)}
ds = load_uniform_grid(data, shp, unit_system="code")
assert "code" in ds._unit_system_name

sp = ds.sphere(ds.domain_center, ds.domain_width[0] / 2.0)
sp.save_as_dataset(fi)

with h5py.File(fi, mode="r") as f:
assert f.attrs["unit_system_name"] == "code"
7 changes: 6 additions & 1 deletion yt/frontends/ytdata/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ def save_as_dataset(ds, filename, data, field_types=None, extra_attrs=None):
_yt_array_hdf5_attr(fh, "unit_registry_json", ds.unit_registry.to_json())

if hasattr(ds, "unit_system"):
_yt_array_hdf5_attr(fh, "unit_system_name", ds.unit_system.name.split("_")[0])
# Note: ds._unit_system_name is written here rather than
# ds.unit_system.name because for a 'code' unit system, ds.unit_system.name
# is a hash, not a unit system. And on re-load, we want to designate
# a unit system not a hash value.
# See https://github.com/yt-project/yt/issues/4315 for more background.
_yt_array_hdf5_attr(fh, "unit_system_name", ds._unit_system_name)

for attr in base_attrs:
if isinstance(ds, dict):
Expand Down

0 comments on commit 3c564f2

Please sign in to comment.