orix icon indicating copy to clipboard operation
orix copied to clipboard

Misleading error message when trying to write HDF5 file to non-existing directory

Open hakonanes opened this issue 2 years ago • 1 comments

The following error message is printed when trying to write a CrystalMap to an HDF5 file in a non-existing directory:

>>> from orix import io
# Importing of other libraries and creation of CrystalMap `xmap`
>>> io.save("xmap.h5", xmap)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File ~/miniconda3/envs/kp-dev/lib/python3.9/site-packages/orix/io/plugins/orix_hdf5.py:240, in file_writer(filename, crystal_map, **kwargs)
    239 try:
--> 240     f = File(filename, mode="w")
    241 except OSError:

File ~/miniconda3/envs/kp-dev/lib/python3.9/site-packages/h5py/_hl/files.py:507, in File.__init__(self, name, mode, driver, libver, userblock_size, swmr, rdcc_nslots, rdcc_nbytes, rdcc_w0, track_order, fs_strategy, fs_persist, fs_threshold, fs_page_size, page_buf_size, min_meta_keep, min_raw_keep, locking, **kwds)
    504     fcpl = make_fcpl(track_order=track_order, fs_strategy=fs_strategy,
    505                      fs_persist=fs_persist, fs_threshold=fs_threshold,
    506                      fs_page_size=fs_page_size)
--> 507     fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
    509 if isinstance(libver, tuple):

File ~/miniconda3/envs/kp-dev/lib/python3.9/site-packages/h5py/_hl/files.py:226, in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    225 elif mode == 'w':
--> 226     fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
    227 elif mode == 'a':
    228     # Open in append mode (read/write).
    229     # If that fails, create a new file only if it won't clobber an
    230     # existing one (ACC_EXCL)

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

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

File h5py/h5f.pyx:126, in h5py.h5f.create()

FileNotFoundError: [Errno 2] Unable to create file (unable to open file: name = '/home/hakon/phd/data/tina/c/kp/merged/xmap_merged.h5', errno = 2, error message = 'Fila eller mappa finnes ikke', flags = 13, o_flags = 242)

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
Input In [109], in <cell line: 6>()
      1 xmap_merged = kp.indexing.merge_crystal_maps(
      2     crystal_maps=xmaps,
      3     mean_n_best=1,
      4 )
----> 6 io.save(os.path.join(dir_merged, "xmap_merged.h5"), xmap_merged)
      7 io.save(os.path.join(dir_merged, "xmap_merged.ang"), xmap_merged, confidence_index_prop="scores")
      9 xmap_merged

File ~/miniconda3/envs/kp-dev/lib/python3.9/site-packages/orix/io/__init__.py:207, in save(filename, object2write, overwrite, **kwargs)
    204         raise ValueError("`overwrite` parameter can only be None, True or False.")
    206 if write:
--> 207     writer.file_writer(filename, object2write, **kwargs)

File ~/miniconda3/envs/kp-dev/lib/python3.9/site-packages/orix/io/plugins/orix_hdf5.py:242, in file_writer(filename, crystal_map, **kwargs)
    240     f = File(filename, mode="w")
    241 except OSError:
--> 242     raise OSError(f"Cannot write to the already open file '{filename}'.")
    244 from orix import __version__
    246 file_dict = {
    247     "manufacturer": "orix",
    248     "version": __version__,
    249     "crystal_map": crystalmap2dict(crystal_map),
    250 }

OSError: Cannot write to the already open file '/home/hakon/merged/xmap_merged.h5'.

The final message OSError: Cannot write to the already open file /home/hakon/merged/xmap_merged.h5'. is printed by us, but is misleading as the initial issue here is that the directory named "merged" doesn't exist.

A solution would be to try to catch the FileNotFoundError and give the initial error message if possible.

hakonanes avatar May 30 '22 08:05 hakonanes

What's the except OSError catching here (L242) ? Are you just using it to give a custom error?

pc494 avatar May 31 '22 12:05 pc494