diff --git a/CHAP/common/__init__.py b/CHAP/common/__init__.py index 45b2dbe..a537814 100755 --- a/CHAP/common/__init__.py +++ b/CHAP/common/__init__.py @@ -28,6 +28,7 @@ ) from CHAP.common.writer import ( ExtractArchiveWriter, + MatplotlibFigureWriter, NexusWriter, YAMLWriter, TXTWriter, diff --git a/CHAP/common/writer.py b/CHAP/common/writer.py index a342472..14022a6 100755 --- a/CHAP/common/writer.py +++ b/CHAP/common/writer.py @@ -37,6 +37,37 @@ def write(self, data, filename): return data +class MatplotlibFigureWriter(Writer): + """Writer for saving matplotlib figures to image files.""" + def write(self, data, filename, savefig_kw={}, force_overwrite=False): + """Write the matplotlib.fgure.Figure contained in `data` to + the filename provided. + + :param data: input containing a matplotlib figure + :type data: CHAP.pipeline.PipelineData + :param filename: name of the file to write to. + :param savefig_kw: keyword args to pass to + matplotlib.figure.Figure.savefig, defaults to {} + :type savefig_kw: dict, optional + :param force_overwrite: flag to allow data in `filename` to be + overwritten, if it already exists. + :return: the original input data + """ + + if os.path.isfile(filename) and not force_overwrite: + raise FileExistsError(f'{filename} already exists') + + figure = self.unwrap_pipelinedata(data) + + from matplotlib.figure import Figure + if not isinstance(figure, Figure): + raise TypeError('Cannot write object of type' + f'{type(figure)} as a matplotlib Figure.') + + figure.savefig(filename, **savefig_kw) + return figure + + class NexusWriter(Writer): """Writer for NeXus files from `NXobject`-s""" def write(self, data, filename, force_overwrite=False): diff --git a/galaxy-tools/chapbook_demo/chapbook_demo.xml b/galaxy-tools/chapbook_demo/chapbook_demo.xml new file mode 100644 index 0000000..7ee4fdd --- /dev/null +++ b/galaxy-tools/chapbook_demo/chapbook_demo.xml @@ -0,0 +1,45 @@ + + + ../macros.xml + + + + nexusformat + pyfai + pydantic + + + + /home/chess_chapaas/chess/chap/ + + + + + + + + + + + + + + + + + + + +