.. _hspy-format: HSpy - HyperSpy's HDF5 Specification ------------------------------------ This is `HyperSpy's `_ default format and for data processed in HyperSpy, it is the only format that guarantees that no information will be lost in the writing process and that supports saving data of arbitrary dimensions. It is based on the `HDF5 open standard `_. The HDF5 file format is supported by `many applications `_. Parts of the specifications are documented in :external+hyperspy:ref:`metadata_structure`. .. versionadded:: HyperSpy_v1.2 Enable saving HSpy files with the ``.hspy`` extension. Previously only the ``.hdf5`` extension was recognised. .. versionchanged:: HyperSpy_v1.3 The default extension for the HyperSpy HDF5 specification is now ``.hspy``. The option to change the default is no longer present in ``preferences``. Only loading of HDF5 files following the HyperSpy specifications is supported by this plugin. Usually their extension is ``.hspy`` extension, but older versions of HyperSpy would save them with the ``.hdf5`` extension. Both extensions are recognised by HyperSpy since version 1.2. However, HyperSpy versions older than 1.2 won't recognise the ``.hspy`` extension. To work around the issue when using old HyperSpy installations simply change the extension manually to ``.hdf5`` or directly save the file using this extension by explicitly adding it to the filename e.g.: .. code-block:: python >>> import hyperspy.api as hs >>> s = hs.signals.BaseSignal([0]) >>> s.save('test.hdf5') When saving to ``.hspy``, all supported objects in the signal's :external+hyperspy:attr:`hyperspy.signal.BaseSignal.metadata` are stored. This includes lists, tuples and signals. Please note that in order to increase saving efficiency and speed, if possible, the inner-most structures are converted to numpy arrays when saved. This procedure homogenizes any types of the objects inside, most notably casting numbers as strings if any other strings are present: .. code-block:: python >>> # before saving: >>> somelist [1, 2.0, 'a name'] >>> # after saving: ['1', '2.0', 'a name'] The change of type is done using numpy "safe" rules, so no information is lost, as numbers are represented to full machine precision. This feature is particularly useful when using :external+hyperspy:meth:`hyperspy._signals.eds.EDSSpectrum.get_lines_intensity`: .. code-block:: python >>> s = hs.datasets.example_signals.EDS_SEM_Spectrum() >>> s.metadata.Sample.intensities = s.get_lines_intensity() >>> s.save('EDS_spectrum.hspy') >>> s_new = hs.load('EDS_spectrum.hspy') >>> s_new.metadata.Sample.intensities [, , , , ] .. _hspy-chunks: Chunking ^^^^^^^^ .. versionadded:: HyperSpy_v1.3.1 ``chunks`` keyword argument The HyperSpy HDF5 format supports chunking the data into smaller pieces to make it possible to load only part of a dataset at a time. By default, the data is saved in chunks that are optimised to contain at least one full signal. It is possible to customise the chunk shape using the ``chunks`` keyword. For example, to save the data with ``(20, 20, 256)`` chunks instead of the default ``(7, 7, 2048)`` chunks for this signal: .. code-block:: python >>> s = hs.signals.Signal1D(np.random.random((100, 100, 2048))) >>> s.save("test_chunks", chunks=(20, 20, 256)) Note that currently it is not possible to pass different customised chunk shapes to all signals and arrays contained in a signal and its metadata. Therefore, the value of ``chunks`` provided on saving will be applied to all arrays contained in the signal. By passing ``True`` to ``chunks`` the chunk shape is guessed using the ``guess_chunk`` function of ``h5py`` For large signal spaces, the autochunking usually leads to smaller chunks as ``guess_chunk`` does not impose the constrain of storing at least one signal per chunk. For example, for the signal in the example above passing ``chunks=True`` results in chunks of ``(7, 7, 256)``. Choosing the correct chunk-size can significantly affect the speed of reading, writing and performance of many HyperSpy algorithms. See the :external+hyperspy:ref:`HyperSpy chunking section ` for more information. .. Note:: Also see the :ref:`hdf5-utils` for inspecting HDF5 files. API functions ^^^^^^^^^^^^^ .. automodule:: rsciio.hspy :members: