nxdata: NXdata parsing and validation

To parse an existing NXdata group, use NXdata.

Following functions help you check the validity of a existing NXdata group:

To help you write a NXdata group, you can use save_NXdata().

Classes

class NXdata(group, validate=True)[source]

NXdata parser.

Note

Before attempting to access any attribute or property, you should check that is_valid is True.

Parameters:
  • group – h5py-like group following the NeXus NXdata specification.
  • validate (boolean) – Set this parameter to False to skip the initial validation. This option is provided for optimisation purposes, for cases where silx.io.nxdata.is_valid_nxdata() has already been called prior to instantiating this NXdata.
group = None

h5py-like group object with @NX_class=NXdata.

issues = None

List of error messages for malformed NXdata.

is_valid = None

Validity status for this NXdata. If False, all properties and attributes will be None.

signal = None

Main signal dataset in this NXdata group. In case more than one signal is present in this group, the other ones can be found in auxiliary_signals.

signal_name = None

Signal long name, as specified in the @long_name attribute of the signal dataset. If not specified, the dataset name is used.

axes_names = None

List of axes names in a NXdata group.

This attribute is similar to axes_dataset_names except that if an axis dataset has a “@long_name” attribute, it will be used instead of the dataset name.

signal_dataset_name

Name of the main signal dataset.

auxiliary_signals_dataset_names

Sorted list of names of the auxiliary signals datasets.

These are the names provided by the @auxiliary_signals attribute on the NXdata group.

In case the NXdata group does not specify a @signal attribute but has a dataset with an attribute @signal=1, we look for datasets with attributes @signal=2, @signal=3… (deprecated NXdata specification).

auxiliary_signals_names

List of names of the auxiliary signals.

Similar to auxiliary_signals_dataset_names, but the @long_name is used when this attribute is present, instead of the dataset name.

auxiliary_signals

List of all auxiliary signal datasets.

interpretation

@interpretation attribute associated with the signal dataset of the NXdata group. None if no interpretation attribute is present.

The interpretation attribute provides information about the last dimensions of the signal. The allowed values are:

  • “scalar”: 0-D data to be plotted
  • “spectrum”: 1-D data to be plotted
  • “image”: 2-D data to be plotted
  • “vertex”: 3-D data to be plotted

For example, a 3-D signal with interpretation “spectrum” should be considered to be a 2-D array of 1-D data. A 3-D signal with interpretation “image” should be interpreted as a 1-D array (a list) of 2-D images. An n-D array with interpretation “image” should be interpreted as an (n-2)-D array of images.

A warning message is logged if the returned interpretation is not one of the allowed values, but no error is raised and the unknown interpretation is returned anyway.

axes

List of the axes datasets.

The list typically has as many elements as there are dimensions in the signal dataset, the exception being scatter plots which use a 1D signal and multiple 1D axes of the same size.

If an axis dataset applies to several dimensions of the signal, it will be repeated in the list.

If a dimension of the signal has no dimension scale, None is inserted in its position in the list.

Note

The @axes attribute should define as many entries as there are dimensions in the signal, to avoid any ambiguity. If this is not the case, this implementation relies on the existence of an @interpretation (spectrum or image) attribute in the signal dataset.

Note

If an axis dataset defines attributes @first_good or @last_good, the output will be a numpy array resulting from slicing that axis (axis[first_good:last_good + 1]).

Return type:List[Dataset or 1D array or None]
axes_dataset_names

List of axes dataset names.

If an axis dataset applies to several dimensions of the signal, its name will be repeated in the list.

If a dimension of the signal has no dimension scale (i.e. there is a “.” in that position in the @axes array), None is inserted in the output list in its position.

title

Plot title. If not found, returns an empty string.

This attribute does not appear in the NXdata specification, but it is implemented in nexpy as a dataset named “title” inside the NXdata group. This dataset is expected to contain text.

Because the nexpy approach could cause a conflict if the signal dataset or an axis dataset happened to be called “title”, we also support providing the title as an attribute of the NXdata group.

get_axis_errors(axis_name)[source]

Return errors (uncertainties) associated with an axis.

If the axis has attributes @first_good or @last_good, the output is trimmed accordingly (a numpy array will be returned rather than a dataset).

Parameters:axis_name (str) – Name of axis dataset. This dataset must exist.
Returns:Dataset with axis errors, or None
Raises:KeyError – if this group does not contain a dataset named axis_name
errors

Return errors (uncertainties) associated with the signal values.

Returns:Dataset with errors, or None
plot_style

Information extracted from the optional SILX_style attribute

Raises:InvalidNXdataError
is_scatter

True if the signal is 1D and all the axes have the same size as the signal.

is_x_y_value_scatter

True if this is a scatter with a signal and two axes.

is_unsupported_scatter

True if this is a scatter with a signal and more than 2 axes.

is_curve

This property is True if the signal is 1D or interpretation is “spectrum”, and there is at most one axis with a consistent length.

is_image

True if the signal is 2D, or 3D with last dimension of length 3 or 4 and interpretation rgba-image, or >2D with interpretation image. The axes (if any) length must also be consistent with the signal shape.

is_stack

True in the signal is at least 3D and interpretation is not “scalar”, “spectrum”, “image” or “rgba-image”. The axes length must also be consistent with the last 3 dimensions of the signal.

is_volume
True in the signal is exactly 3D and interpretation
“scalar”, or nothing.

The axes length must also be consistent with the 3 dimensions of the signal.

Functions

get_default(group, validate=True)[source]

Return a NXdata object corresponding to the default NXdata group in the group specified as parameter.

This function can find the NXdata if the group is already a NXdata, or if it is a NXentry defining a default NXdata, or if it is a NXroot defining such a default valid NXentry.

Return None if no valid NXdata could be found.

Parameters:
  • group – h5py-like group following the Nexus specification (NXdata, NXentry or NXroot).
  • validate (bool) – Set this to False if you are sure that group is valid NXdata (i.e. silx.io.nxdata.is_valid_nxdata(group)() returns True). Parameter provided for optimisation purposes.
Returns:

NXdata object or None

Raises:

TypeError – if group is not a h5py-like group

is_valid_nxdata(group)[source]

Check if a h5py group is a valid NX_data group.

Parameters:group – h5py-like group
Returns:True if this NXdata group is valid.
Raises:TypeError – if group is not a h5py group, a spech5 group, or a fabioh5 group
is_group_with_default_NXdata(group, validate=True)[source]

Return True if group defines a valid default NXdata.

Parameters:
  • group – h5py-like object.
  • validate (bool) – Set this to skip the NXdata validation, and only check the existence of the group. Parameter provided for optimisation purposes, to avoid double validation if the validation is already performed separately.
is_NXentry_with_default_NXdata(group, validate=True)[source]

Return True if group is a valid NXentry defining a valid default NXdata.

Parameters:
  • group – h5py-like object.
  • validate (bool) – Set this to skip the NXdata validation, and only check the existence of the group. Parameter provided for optimisation purposes, to avoid double validation if the validation is already performed separately.
is_NXroot_with_default_NXdata(group, validate=True)[source]

Return True if group is a valid NXroot defining a default NXentry defining a valid default NXdata.

Note

A NXroot group cannot directly define a default NXdata. If a @default argument is present, it must point to a NXentry group. This NXentry must define a valid NXdata for this function to return True.

Parameters:
  • group – h5py-like object.
  • validate (bool) – Set this to False if you are sure that the target group is valid NXdata (i.e. silx.io.nxdata.is_valid_nxdata(target_group)() returns True). Parameter provided for optimisation purposes.
save_NXdata(filename, signal, axes=None, signal_name='data', axes_names=None, signal_long_name=None, axes_long_names=None, signal_errors=None, axes_errors=None, title=None, interpretation=None, nxentry_name='entry', nxdata_name=None)[source]

Write data to an NXdata group.

Note

No consistency checks are made regarding the dimensionality of the signal and number of axes. The user is responsible for providing meaningful data, that can be interpreted by visualization software.

Parameters:
  • filename (str) – Path to output file. If the file does not exists, it is created.
  • signal (numpy.ndarray) – Signal array.
  • axes (List[numpy.ndarray]) – List of axes arrays.
  • signal_name (str) – Name of signal dataset, in output file
  • axes_names (List[str]) – List of dataset names for axes, in output file
  • signal_long_name (str) – @long_name attribute for signal, or None.
  • axes_long_names (List[str, None]) – None, or list of long names for axes
  • signal_errors (numpy.ndarray) – Array of errors associated with the signal
  • axes_errors (List[numpy.ndarray, None]) – List of arrays of errors associated with each axis
  • title (str) – Graph title (saved as a “title” dataset) or None.
  • interpretation (str) – @interpretation attribute (“spectrum”, “image”, “rgba-image” or None). This is only needed in cases of ambiguous dimensionality, e.g. a 3D array which represents a RGBA image rather than a stack.
  • nxentry_name (str) –

    Name of group in which the NXdata group is created. By default, “/entry” is used.

    Note

    The Nexus format specification requires for NXdata groups be part of a NXentry group. The specified group should have attribute @NX_class=NXentry, in order for the created file to be nexus compliant.

  • nxdata_name (str) – Name of NXdata group. If omitted (None), the function creates a new group using the first available name (“data0”, or “data1”…). Overwriting an existing group (or dataset) is not supported, you must delete it yourself prior to calling this function if this is what you want.
Returns:

True if save was successful, else False.