adrn / thejoker

A custom Monte Carlo sampler for the (gravitational) two-body problem
MIT License
30 stars 8 forks source link

Error when reading from h5py file #100

Closed stephtdouglas closed 4 years ago

stephtdouglas commented 4 years ago

Now that I have successfully written out my samples to an h5py file, I can't read them back in again. I think it's coming from a disconnect between the JokerSamples.read() method and JokerSamples._read_tables() method. I can only give .read() it a filename, but _read_tables() wants a group and a path, and the default path is 'samples'. So the resulting error traceback is

Traceback (most recent call last):
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/astropy/io/misc/hdf5.py", line 100, in read_table_hdf5
    input = input[path]
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/h5py/_hl/group.py", line 264, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'samples' doesn't exist)"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "newjoker_mwe_readtables.py", line 33, in <module>
    samples0 = JokerSamples.read(results_filename)#f[name])
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/thejoker/samples.py", line 451, in read
    tbl = QTable.read(filename, path=cls._hdf5_path)
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/astropy/table/connect.py", line 52, in __call__
    out = registry.read(cls, *args, **kwargs)
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/astropy/io/registry.py", line 523, in read
    data = reader(*args, **kwargs)
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/astropy/io/misc/hdf5.py", line 143, in read_table_hdf5
    return read_table_hdf5(f, path=path, character_as_bytes=character_as_bytes)
  File "/pool/cfsi04_0/.conda/envs/mynewjoker/lib/python3.6/site-packages/astropy/io/misc/hdf5.py", line 102, in read_table_hdf5
    raise OSError(f"Path {path} does not exist")
OSError: Path samples does not exist
adrn commented 4 years ago

OK new quick fix for you in master :)

This now works:

with h5py.File('/tmp/test.hdf5', 'w') as f:
    g = f.create_group('test')
    samples.write(g)

with h5py.File('/tmp/test.hdf5', 'r') as f:
    samples1 = JokerSamples.read(f['test'])

samples2 = JokerSamples.read('/tmp/test.hdf5', path='test/samples')

Try it for your code?

stephtdouglas commented 4 years ago

Yep, looks like that worked!