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

HSpy writer requires undocumented "package_info" key in the signal dictionary #179

Open
hakonanes opened this issue Nov 2, 2023 · 2 comments
Labels
type: bug Something isn't working

Comments

@hakonanes
Copy link
Contributor

Describe the bug

This is more of a question than a bug report.

rsciio.hspy.file_writer() requires a "package_info" key in the signal dictionary. This is not listed in the API docs. In HyperSpy's use of file_writer(), this dictionary field is populated by calling the utility function rsciio.utils.tools.get_object_package_info(). This function is not listed in Rosetta's public API, so I assume it is private.

Is the intention that a user or another package imports this private function and populates the "package_info" key when using the HSpy writer? Or shouldn't the writer be used by anyone else than HyperSpy?

Please correct me if I missed something here.

To Reproduce

kikuchipy 0.8.7 supports writing to HyperSpy's HSpy file format via a custom save() method. This works fine when calling the HSpy writer from HyperSpy 1.7.5 (internally in kikuchipy). In the effort to make kikuchipy compatible with HyperSpy 2.0 and start using RosettaSciIO 0.1, the need to populate the "package_info" dictionary field presented itself:

>>> from rsciio import hspy
>>> import kikuchipy as kp
>>> s = kp.data.nickel_ebsd_small()  # Extends HyperSpy's Signal2D class for EBSD
>>> hspy.file_writer("test.hspy", s._to_dictionary())  # Calls BaseSignal._to_dictionary()
Traceback (most recent call last):
  File "/home/hakon/miniconda3/envs/kp-dev/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-26-92e4bdbf6859>", line 1, in <module>
    hspy.file_writer("/home/hakon/test.hspy", s._to_dictionary())
  File "/home/hakon/miniconda3/envs/kp-dev/lib/python3.11/site-packages/rsciio/hspy/_api.py", line 271, in file_writer
    writer.write()
  File "/home/hakon/miniconda3/envs/kp-dev/lib/python3.11/site-packages/rsciio/_hierarchical.py", line 700, in write
    self.write_signal(self.signal, self.group, **self.kwds)
  File "/home/hakon/miniconda3/envs/kp-dev/lib/python3.11/site-packages/rsciio/_hierarchical.py", line 704, in write_signal
    group.attrs.update(signal["package_info"])
                       ~~~~~~^^^^^^^^^^^^^^^^
KeyError: 'package_info'

😮

The "package_info" field is populated in HyperSpy's io.save() function and not in BaseSignal._to_dictionary().

Expected behavior

To write the signal to file 😅

The "package_info" dictionary field is new to me (although I see it was introduced by @francisco-dlp July last year...).

Python environement:

Additional context

None at the moment.

@hakonanes hakonanes added the type: bug Something isn't working label Nov 2, 2023
@ericpre
Copy link
Member

ericpre commented Nov 2, 2023

This is specific to the hyperspy specification and I would say that this should be documented ("package_info" and rsciio.utils.tools.get_object_package_info), as well as saving other signals attributes, which are currently undocumented too.

@hakonanes
Copy link
Contributor Author

Thank you for the quick reply. It's good to know that I can then import get_object_package_info() to mimick HyperSpy's approach.

I hope to find time to open a PR that starts on updating the specification documentation soon. Feel free to do it yourself, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants