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

hdf5 reading for unyt.quantity. #559

Open
aaramburo opened this issue Feb 6, 2025 · 3 comments · May be fixed by #560
Open

hdf5 reading for unyt.quantity. #559

aaramburo opened this issue Feb 6, 2025 · 3 comments · May be fixed by #560
Labels
bug Something isn't working

Comments

@aaramburo
Copy link

Hi I believe that there is a bug whenever I try to read a unyt.unyt_quantity that was stored in a hdf5 file using write_hdf5. Basically I think it treats the quantity as a non-zero dimensional array, and uses ellipses to get the data. I have tried to use unyt.unyt_quantity.from_hdf5() and unyt.unyt_array.from_hdf5().

----> 1 unyt.unyt_quantity.from_hdf5("...")
File (.../python3.9/site-packages/unyt/array.py:1557), in unyt_array.from_hdf5(cls, filename, dataset_name, group_name)
   1555     g = f
  1555     g = f
   1556 dataset = g[dataset_name]
-> 1557 data = dataset[:]
   1558 units = dataset.attrs.get("units", "")
   1559 unit_lut = default_unit_symbol_lut.copy()

File (objects.pyx:54), in h5py._objects.with_phil.wrapper()

File (objects.pyx:55), in h5py._objects.with_phil.wrapper()

File(..../h5py/_hl/dataset.py:817), in Dataset.__getitem__(self, args, new_dtype)
    815 if self.shape == ():
    816     fspace = self.id.get_space()
--> 817     selection = sel2.select_read(fspace, args)
    818     if selection.mshape is None:
    819         arr = numpy.zeros((), dtype=new_dtype)

File (../h5py/_hl/selections2.py:101), in select_read(fspace, args)
     96 """ Top-level dispatch function for reading.
     97 
     98 At the moment, only supports reading from scalar datasets.
     99 """
    100 if fspace.shape == ():
--> 101     return ScalarReadSelection(fspace, args)
    103 raise NotImplementedError()

File [.../h5py/_hl/selections2.py:86), in ScalarReadSelection.__init__(self, fspace, args)
     84     self.mshape = ()
     85 else:
---> 86     raise ValueError("Illegal slicing argument for scalar dataspace")
     88 self.mspace = h5s.create(h5s.SCALAR)
     89 self.fspace = fspace

ValueError: Illegal slicing argument for scalar dataspace
@neutrinoceros
Copy link
Member

Can you provide a self-contained reproducer script ?

@aaramburo
Copy link
Author

aaramburo commented Feb 6, 2025

Hi yes, here is a simple script that for me reproduces the issue

import unyt

variable_quantity = 5*unyt.cm
variable_quantity.write_hdf5("example.hdf5",dataset_name="variable", group_name="example_quantity")
in_quantity = unyt.unyt_quantity.from_hdf5("example.hdf5",dataset_name="variable", group_name="example_quantity")

@neutrinoceros neutrinoceros added the bug Something isn't working label Feb 6, 2025
neutrinoceros added a commit to neutrinoceros/unyt that referenced this issue Feb 6, 2025
@neutrinoceros
Copy link
Member

Thank you for reporting this. I opened a fix at #560

neutrinoceros added a commit to neutrinoceros/unyt that referenced this issue Feb 6, 2025
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
2 participants