Skip to content

Commit

Permalink
msa format
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaehne committed Dec 8, 2022
1 parent 7bbb000 commit 7c5b51a
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 38 deletions.
34 changes: 10 additions & 24 deletions docs/supported_formats/msa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,19 @@ with Gatan's Digital Micrograph or other software packages. A wide range of
programs supports exporting to and reading from the ``.msa`` format.

.. WARNING::
If several spectra are loaded and stacked (``hs.load('pattern', stack_signals=True``)
the calibration read from the first spectrum and applied to all other spectra.

Extra saving arguments
^^^^^^^^^^^^^^^^^^^^^^

For the ``.msa`` format the ``format`` argument is used to specify whether the
energy axis should also be saved with the data. The default, 'Y' omits the
energy axis in the file. The alternative, 'XY', saves a second column with the
calibrated energy data. It is possible to personalise the separator with the
`separator` keyword.

.. Warning::

However, if a different separator is chosen the resulting file will not
comply with the MSA/EMSA standard and RosettaSciIO and other software may not
be able to read it.

The default encoding is `latin-1`. It is possible to set a different encoding
using the `encoding` argument, e.g.:

.. code-block:: python
>>> s.save('file.msa', encoding = 'utf8')
If several spectra are loaded and stacked in `HyperSpy <https://hyperspy.org>`_
(``hs.load('pattern', stack_signals=True)``)
the calibration is read from the first spectrum and applied to all other spectra.

Reference
^^^^^^^^^

For specifications of the format, see the `documentation by the Microscopy Society
of America <https://www.microscopy.org/resources/scientific_data/>`_.


API functions
^^^^^^^^^^^^^

.. automodule:: rsciio.msa
:members:
1 change: 1 addition & 0 deletions rsciio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"JobinYvon",
"MRC",
"MRCZ",
"MSA",
"netCDF",
"NeXus",
"Phenom",
Expand Down
5 changes: 5 additions & 0 deletions rsciio/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
Whether to open the file lazily or not.
"""

ENCODING_DOC = """encoding : str, Default="latin-1"
The encoding used to read the content of the file. Different file
encodings, such as ``"utf8"`` can be set via this argument.
"""

ENDIANESS_DOC = """endianess : str, Default="<"
``"<"`` or ``">"``, depending on how the bits are written to
the file.
Expand Down
13 changes: 13 additions & 0 deletions rsciio/msa/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
from ._api import (
file_reader,
file_writer,
)


__all__ = [
"file_reader",
"file_writer",
]


def __dir__():
return sorted(__all__)
48 changes: 48 additions & 0 deletions rsciio/msa/api.py → rsciio/msa/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@

import numpy as np

from rsciio.docstrings import (
FILENAME_DOC,
ENCODING_DOC,
RETURNS_DOC,
SIGNAL_DOC,
)
from rsciio.version import __version__
from rsciio.utils.tools import DTBox

Expand Down Expand Up @@ -337,11 +343,46 @@ def parse_msa_string(string, filename=None):


def file_reader(filename, encoding="latin-1", **kwds):
"""
Read an MSA file.
Parameters
----------
%s
%s
%s
"""
with codecs.open(filename, encoding=encoding, errors="replace") as spectrum_file:
return parse_msa_string(string=spectrum_file, filename=filename)


file_reader.__doc__ %= (FILENAME_DOC, ENCODING_DOC, RETURNS_DOC)


def file_writer(filename, signal, format=None, separator=", ", encoding="latin-1"):
"""
Write signal to an MSA file.
Parameters
----------
%s
%s
format : str, Default="Y"
Specify whether the X-axis (energy/wavelength) should also be saved with
the data. The default, ``"Y"`` omits the X-axis in the file. The alternative,
``"XY"``, saves the calibrated signal axis as first column.
separator: str, Default=", "
Change the column separator. However, if a different separator is chosen
the resulting file will not comply with the MSA/EMSA standard and
RosettaSciIO and other software may not be able to read it.
%s
Examples
--------
>>> from rsciio.msa import file_writer
>>> file_writer("file.msa", signal, encoding="utf8")
"""
loc_kwds = {}
FORMAT = "EMSA/MAS Spectral Data File"
md = DTBox(signal["metadata"], box_dots=True)
Expand Down Expand Up @@ -469,3 +510,10 @@ def file_writer(filename, signal, format=None, separator=", ", encoding="latin-1
raise ValueError("format must be one of: None, 'XY' or 'Y'")

f.write("#%-12s: End Of Data and File" % "ENDOFDATA")


file_writer.__doc__ %= (
FILENAME_DOC.replace("read", "write to"),
SIGNAL_DOC,
ENCODING_DOC.replace("read", "write"),
)
19 changes: 9 additions & 10 deletions rsciio/ripple/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from rsciio.docstrings import (
FILENAME_DOC,
LAZY_DOC,
ENCODING_DOC,
RETURNS_DOC,
SIGNAL_DOC,
)
Expand Down Expand Up @@ -265,10 +266,7 @@ def file_reader(
A dictionary containing the keywords in order to read a ``.raw`` file
without corresponding ``.rpl`` file. If ``None``, the keywords are parsed
automatically from the ``.rpl`` file.
enconding : str, optional
The encoding used to read the content of the ``rpl`` file. The default
encoding is ``latin-1``. Different file encodings, such as ``utf8`` can be
set via this argument.
%s
mmap_mode : str, optional
Default is copy-on-write ``"c"``, but different modes can be set. However,
note that lazy loading does not support in-place writing (i.e lazy loading
Expand Down Expand Up @@ -466,7 +464,7 @@ def file_reader(
]


file_reader.__doc__ %= (FILENAME_DOC, LAZY_DOC, RETURNS_DOC)
file_reader.__doc__ %= (FILENAME_DOC, LAZY_DOC, ENCODING_DOC, RETURNS_DOC)


def file_writer(filename, signal, encoding="latin-1", *args, **kwds):
Expand All @@ -477,10 +475,7 @@ def file_writer(filename, signal, encoding="latin-1", *args, **kwds):
----------
%s
%s
enconding : str, optional
The encoding used to write the content of the ``rpl`` file. The default
encoding is ``latin-1``. Different file encodings, such as ``utf8`` can be
set via this argument.
%s
"""

# Set the optional keys to None
Expand Down Expand Up @@ -606,7 +601,11 @@ def file_writer(filename, signal, encoding="latin-1", *args, **kwds):
write_raw(filename, signal, record_by, sig_axes, nav_axes)


file_writer.__doc__ %= (FILENAME_DOC.replace("read", "write to"), SIGNAL_DOC)
file_writer.__doc__ %= (
FILENAME_DOC.replace("read", "write to"),
SIGNAL_DOC,
ENCODING_DOC.replace("read", "write"),
)


def write_rpl(filename, keys_dictionary, encoding="ascii"):
Expand Down
4 changes: 4 additions & 0 deletions rsciio/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def test_dir_plugins():

assert dir(mrcz) == ["file_reader", "file_writer"]

from rsciio import msa

assert dir(msa) == ["file_reader", "file_writer"]

from rsciio import netcdf

assert dir(netcdf) == ["file_reader"]
Expand Down
4 changes: 2 additions & 2 deletions rsciio/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_load_save_filereader_metadata():

my_path = os.path.dirname(__file__)
s = hs.load(os.path.join(my_path, "msa_files", "example1.msa"))
assert s.metadata.General.FileIO.Number_0.io_plugin == "rsciio.msa.api"
assert s.metadata.General.FileIO.Number_0.io_plugin == "rsciio.msa"
assert s.metadata.General.FileIO.Number_0.operation == "load"
assert s.metadata.General.FileIO.Number_0.hyperspy_version == hs.__version__

Expand All @@ -313,7 +313,7 @@ def test_load_save_filereader_metadata():
s.save(f)
expected = {
"0": {
"io_plugin": "rsciio.msa.api",
"io_plugin": "rsciio.msa",
"operation": "load",
"hyperspy_version": hs.__version__,
},
Expand Down
4 changes: 2 additions & 2 deletions rsciio/tests/test_msa.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"0": {
"operation": "load",
"hyperspy_version": hs.__version__,
"io_plugin": "rsciio.msa.api",
"io_plugin": "rsciio.msa",
}
},
},
Expand Down Expand Up @@ -135,7 +135,7 @@
"0": {
"operation": "load",
"hyperspy_version": hs.__version__,
"io_plugin": "rsciio.msa.api",
"io_plugin": "rsciio.msa",
}
},
},
Expand Down

0 comments on commit 7c5b51a

Please sign in to comment.