CHAP.common.reader

File : reader.py Author : Valentin Kuznetsov Description: Module for Writers used in multiple experiment-specific workflows.

Module Contents

Classes

BinaryFileReader

Reader for binary files.

FabioImageReader

Reader for images using the python package fabio.

H5Reader

Reader for h5 files.

MapReader

Reader for CHESS sample maps.

NexusReader

Reader for NeXus files.

NXdataReader

Reader for constructing an NXdata object from components

NXfieldReader

Reader for an NXfield with options to modify certain attributes.

SpecReader

Reader for CHESS SPEC scans

URLReader

Reader for data available over HTTPS.

YAMLReader

Reader for YAML files.

API

class CHAP.common.reader.BinaryFileReader

Bases: CHAP.Reader

Reader for binary files.

Initialization

Constructor of PipelineItem class

read(filename)

Return a content of a given binary file.

Parameters:

filename (str) – The name of the binary file to read from.

Returns:

The file content.

Return type:

binary

class CHAP.common.reader.FabioImageReader

Bases: CHAP.Reader

Reader for images using the python package fabio.

Initialization

Constructor of PipelineItem class

read(filename, frame=None, inputdir='.')

Return the data from the image file(s) provided.

Parameters:
  • filename (int, optional) – The image filename, or glob pattern for image filenames, to read.

  • frame – The index of a specific frame to read from the file(s), defaults to None.

Returns:

Image data as a numpy array (or list of numpy arrays, if a glob pattern matching more than one file was provided).

class CHAP.common.reader.H5Reader

Bases: CHAP.Reader

Reader for h5 files.

Initialization

Constructor of PipelineItem class

read(filename, h5path='/', idx=None)

Return the data object stored at h5path in an h5-file.

Parameters:
  • filename (str) – The name of the h5-file to read from.

  • h5path (str, optional) – The path to a specific location in the h5 file to read data from, defaults to '/'

Returns:

The object indicated by filename and h5path.

Return type:

object

class CHAP.common.reader.MapReader

Bases: CHAP.Reader

Reader for CHESS sample maps.

Initialization

Constructor of PipelineItem class

read(filename=None, map_config=None, detector_names=[], inputdir=None)

Take a map configuration dictionary and return a representation of the map as a NeXus NXentry object. The NXentry’s default data group will contain the raw data collected over the course of the map.

Parameters:
  • filename (str, optional) – The name of a file with the map configuration to read and pass onto the constructor of CHAP.common.models.map.MapConfig, defaults to None.

  • map_config (dict, optional) – A map configuration to be passed directly to the constructor of CHAP.common.models.map.MapConfig, defaults to None.

  • detector_names (list[str], optional) – Detector prefixes to include raw data for in the returned NeXus NXentry object, defaults to [].

Returns:

Data from the provided map configuration.

Return type:

nexusformat.nexus.NXentry

class CHAP.common.reader.NexusReader

Bases: CHAP.Reader

Reader for NeXus files.

Initialization

Constructor of PipelineItem class

read(filename, nxpath='/')

Return the NeXus object stored at nxpath in a NeXus file.

Parameters:
  • filename (str) – The name of the NeXus file to read from.

  • nxpath (str, optional) – The path to a specific location in the NeXus file tree to read from, defaults to '/'

Raises:

nexusformat.nexus.NeXusError – If filename is not a NeXus file or nxpath is not in its tree.

Returns:

The NeXus object indicated by filename and nxpath.

Return type:

nexusformat.nexus.NXobject

class CHAP.common.reader.NXdataReader

Bases: CHAP.Reader

Reader for constructing an NXdata object from components

Initialization

Constructor of PipelineItem class

read(name, nxfield_params, signal_name, axes_names, attrs={}, inputdir='.')

Return a basic NXdata object constructed from components.

Parameters:
  • name (str) – The name of the NXdata group.

  • nxfield_params (list[dict]) – List of sets of parameters for NXfieldReader specifying the NXfields belonging to the NXdata.

  • signal_name – Name of the signal for the NXdata (must be one of the names of the NXfields indicated in nxfields)

  • axes_names (Union[str, list[str]]) – Name or names of the coordinate axes NXfields associated with the signal (must be names of NXfields indicated in nxfields)

  • attrs (dict) – Optional dictionary of additional attributes for the NXdata

  • inputdir (str) – Input directory to use for NXfieldReaders, defaults to "."

Returns:

A new NXdata object

Return type:

nexusformat.nexus.NXdata

class CHAP.common.reader.NXfieldReader

Bases: CHAP.Reader

Reader for an NXfield with options to modify certain attributes.

Initialization

Constructor of PipelineItem class

read(filename, nxpath, nxname=None, update_attrs=None, slice_params=None, inputdir='.')

Return a copy of the indicated NXfield from the file. Name and attributes of the returned copy may be modified with the nxname and update_attrs keyword arguments.

Parameters:
  • filename (str) – Name of the NeXus file containing the NXfield to read.

  • nxpath (str) – Path in nxfile pointing to the NXfield to read.

  • nxname (str, optional) – Optional new name for the returned NXfield, defaults to None

  • update_attrs (dict, optional) – Optional dictonary used to add to / update the original NXfield’s attributes, defaults to None

  • slice_params (list[dict[str, int]], optional) – Parameters for returning just a slice of the full field data. Slice parameters are provided in a list dictionaries with integer values for any / all of the following keys: "start", "end", "step". Default values used are: "start" - 0, "end"None, "step"1. The order of the list must correspond to the order of the field’s axes. Defaults to None.

  • inputdir (str) – Directory containing nxfile, defaults to "."

Returns:

A copy of the indicated NXfield (with name and attributes optionally modified).

Return type:

nexusformat.nexus.NXfield

class CHAP.common.reader.SpecReader

Bases: CHAP.Reader

Reader for CHESS SPEC scans

Initialization

Constructor of PipelineItem class

read(filename=None, config=None, detector_names=None, inputdir=None)

Take a SPEC configuration filename or dictionary and return the raw data as a Nexus NXentry object.

Parameters:
  • filename (str, optional) – The name of file with the SPEC configuration to read from to pass onto the constructor of CHAP.common.models.map.SpecConfig, defaults to None.

  • config (dict, optional) – A SPEC configuration to be passed directly to the constructor of CHAP.common.models.map.SpecConfig, defaults to None.

  • detector_names (Union(int, str, list[int], list[str]), optional) – Detector names/prefixes to include raw data for in the returned NeXus NXentry object, defaults to None.

Returns:

The data from the provided SPEC configuration.

Return type:

nexusformat.nexus.NXroot

class CHAP.common.reader.URLReader

Bases: CHAP.Reader

Reader for data available over HTTPS.

Initialization

Constructor of PipelineItem class

read(url, headers={}, timeout=10)

Make an HTTPS request to the provided URL and return the results. Headers for the request are optional.

Parameters:
  • url (str) – The URL to read.

  • headers (dict, optional) – Headers to attach to the request, defaults to {}

Returns:

The content of the response.

Return type:

object

class CHAP.common.reader.YAMLReader

Bases: CHAP.Reader

Reader for YAML files.

Initialization

Constructor of PipelineItem class

read(filename)

Return a dictionary from the contents of a yaml file.

Parameters:

filename (str) – The name of the YAML file to read from.

Returns:

The contents of the file.

Return type:

dict