CHAP.common.writer

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

Module Contents

Classes

ExtractArchiveWriter

Writer for tar files from binary data

FileTreeWriter

Writer for a file tree in NeXus format

H5Writer

Writer for H5 files from an nexusformat.nexus.NXdata object

MatplotlibAnimationWriter

Writer for saving matplotlib animations.

MatplotlibFigureWriter

Writer for saving matplotlib figures to image files.

NexusWriter

Writer for NeXus files from NXobject-s

PyfaiResultsWriter

Writer for results of one or more pyFAI integrations. Able to handle multiple output formats. Currently supported formats are: .npz, .nxs.

TXTWriter

Writer for plain text files from string or tuples or lists of strings.

YAMLWriter

Writer for YAML files from dict-s

Functions

write_matplotlibfigure

write_nexus

write_tif

write_txt

write_yaml

write_filetree

API

CHAP.common.writer.write_matplotlibfigure(data, filename, force_overwrite=False)
CHAP.common.writer.write_nexus(data, filename, force_overwrite=False)
CHAP.common.writer.write_tif(data, filename, force_overwrite=False)
CHAP.common.writer.write_txt(data, filename, force_overwrite=False, append=False)
CHAP.common.writer.write_yaml(data, filename, force_overwrite=False)
CHAP.common.writer.write_filetree(data, outputdir, force_overwrite=False)
class CHAP.common.writer.ExtractArchiveWriter

Bases: CHAP.Writer

Writer for tar files from binary data

Initialization

Constructor of PipelineItem class

write(data, filename)

Take a .tar archive represented as bytes contained in data and write the extracted archive to files.

Parameters:
  • data (list[PipelineData]) – The data to write to archive.

  • filename (str) – The name of the directory to write the archive files to.

Returns:

The achived data

Return type:

bytes

class CHAP.common.writer.FileTreeWriter

Bases: CHAP.Writer

Writer for a file tree in NeXus format

Initialization

Constructor of PipelineItem class

write(data, outputdir, force_overwrite=False)

Write a NeXus format object contained in data to a directory tree stuctured like the NeXus tree.

Parameters:
  • data (list[PipelineData]) – The data to write to disk.

  • outputdir (str) – The name of the directory to write to.

  • force_overwrite (bool, optional) – Flag to allow data to be overwritten if it already exists, defaults to False.

Raises:

RuntimeError – If filename already exists and force_overwrite is False.

Returns:

The data written to disk.

Return type:

Union[nexusformat.nexus.NXroot, nexusformat.nexus.NXentry]

class CHAP.common.writer.H5Writer

Bases: CHAP.Writer

Writer for H5 files from an nexusformat.nexus.NXdata object

Initialization

Constructor of PipelineItem class

write(data, filename, force_overwrite=False)

Write the NeXus object contained in data to hdf5 file.

Parameters:
  • data (CHAP.pipeline.PipelineData) – The data to write to file.

  • filename – The name of the file to write to.

  • force_overwrite (bool, optional) – Flag to allow data in filename to be overwritten if it already exists, defaults to False.

Raises:

RuntimeError – If filename already exists and force_overwrite is False.

Returns:

The data written to file.

Return type:

nexusformat.nexus.NXobject

class CHAP.common.writer.MatplotlibAnimationWriter

Bases: CHAP.Writer

Writer for saving matplotlib animations.

Initialization

Constructor of PipelineItem class

write(data, filename, fps=1)

Write the matplotlib.animation.ArtistAnimation object contained in data to file.

Parameters:
  • data (list[PipelineData]) – The matplotlib animation.

  • filename (str) – The name of the file to write to.

  • fps (int, optional) – Movie frame rate (frames per second), defaults to 1

Returns:

The original animation.

Return type:

matplotlib.animation.ArtistAnimation

class CHAP.common.writer.MatplotlibFigureWriter

Bases: CHAP.Writer

Writer for saving matplotlib figures to image files.

Initialization

Constructor of PipelineItem class

write(data, filename, savefig_kw={}, force_overwrite=False)

Write the matplotlib.figure.Figure contained in data to file.

Parameters:
  • data (list[PipelineData]) – The matplotlib figure

  • filename (str) – The name of the file to write to.

  • savefig_kw (dict, optional) – Keyword args to pass to matplotlib.figure.Figure.savefig, defaults to {}.

  • force_overwrite (bool, optional) – Flag to allow data in filename to be overwritten if it already exists, defaults to False.

Raises:

RuntimeError – If filename already exists and force_overwrite is False.

Returns:

The original figure object

Return type:

matplotlib.figure.Figure

class CHAP.common.writer.NexusWriter

Bases: CHAP.Writer

Writer for NeXus files from NXobject-s

Initialization

Constructor of PipelineItem class

write(data, filename, nxpath=None, force_overwrite=False)

Write the NeXus object contained in data to file.

Parameters:
  • data (list[PipelineData]) – The data to write to file.

  • filename – The name of the file to write to.

  • force_overwrite (bool, optional) – Flag to allow data in filename to be overwritten if it already exists, defaults to False.

Raises:

RuntimeError – If filename already exists and force_overwrite is False.

Returns:

The data written to file.

Return type:

nexusformat.nexus.NXobject

class CHAP.common.writer.PyfaiResultsWriter

Bases: CHAP.Writer

Writer for results of one or more pyFAI integrations. Able to handle multiple output formats. Currently supported formats are: .npz, .nxs.

Initialization

Constructor of PipelineItem class

write(data, filename, force_overwrite=False)

Save pyFAI integration results to a file. Format is determined automatically form the extension of filename.

Parameters:
  • data (Union[PipelineData, list[pyFAI.containers.IntegrateResult]]) – Integration results to save.

  • filename (str) – Name of the file to which results will be saved. Format of output is determined ffrom the extension. Currently supported formats are: .npz, .nxs

write_npz(results, filename)

Save results to the .npz file, filename

abstract write_nxs(filename)

Save results to the .nxs file, filename

class CHAP.common.writer.TXTWriter

Bases: CHAP.Writer

Writer for plain text files from string or tuples or lists of strings.

Initialization

Constructor of PipelineItem class

write(data, filename, append=False, force_overwrite=False)

Write a string or tuple or list of strings contained in data to file.

Parameters:
  • data (str, tuple[str], list[str]) – The data to write to disk.

  • filename (str) – The name of the file to write to.

  • append (bool, optional) – Flag to allow data in filename to be be appended, defaults to False.

  • force_overwrite (bool, optional) – Flag to allow data in filename to be overwritten if it already exists, defaults to False.

Raises:
  • TypeError – If the object contained in data is not a str, tuple[str] or list[str].

  • RuntimeError – If filename already exists and force_overwrite is False.

Returns:

The data written to file.

Return type:

str, tuple[str], list[str]

class CHAP.common.writer.YAMLWriter

Bases: CHAP.Writer

Writer for YAML files from dict-s

Initialization

Constructor of PipelineItem class

write(data, filename, force_overwrite=False)

Write the dictionary contained in data to file.

Parameters:
  • data (dict) – The data to write to file.

  • filename (str) – The name of the file to write to.

  • force_overwrite (bool, optional) – Flag to allow data in filename to be overwritten if it already exists, defaults to False.

Raises:
  • TypeError – If the object contained in data is not a dict.

  • RuntimeError – If filename already exists and force_overwrite is False.

Returns:

The data written to file.

Return type:

dict