ChessAnalysisPipeline 0.0.11__py3-none-any.whl → 0.0.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ChessAnalysisPipeline might be problematic. Click here for more details.

CHAP/common/writer.py CHANGED
@@ -5,11 +5,10 @@ Author : Valentin Kuznetsov <vkuznet AT gmail dot com>
5
5
  Description: Module for Writers used in multiple experiment-specific workflows.
6
6
  """
7
7
 
8
- # system modules
9
- from os import mkdir
8
+ # System modules
10
9
  from os import path as os_path
11
10
 
12
- # local modules
11
+ # Local modules
13
12
  from CHAP import Writer
14
13
 
15
14
  def write_matplotlibfigure(data, filename, force_overwrite=False):
@@ -89,6 +88,9 @@ def write_yaml(data, filename, force_overwrite=False):
89
88
  yaml.dump(data, f, sort_keys=False)
90
89
 
91
90
  def write_filetree(data, outputdir, force_overwrite=False):
91
+ # System modules
92
+ from os import mkdir
93
+
92
94
  # Third party modules
93
95
  from nexusformat.nexus import (
94
96
  NXentry,
@@ -130,15 +132,15 @@ def write_filetree(data, outputdir, force_overwrite=False):
130
132
  class ExtractArchiveWriter(Writer):
131
133
  """Writer for tar files from binary data"""
132
134
  def write(self, data, filename):
133
- """Take a .tar archive represented as bytes in `data` and
134
- write the extracted archive to files.
135
+ """Take a .tar archive represented as bytes contained in `data`
136
+ and write the extracted archive to files.
135
137
 
136
- :param data: the archive data
137
- :type data: bytes
138
- :param filename: the name of a directory to which the archive
139
- files will be written
138
+ :param data: The data to write to archive.
139
+ :type data: CHAP.pipeline.PipelineData
140
+ :param filename: The name of the directory to write the archive
141
+ files to.
140
142
  :type filename: str
141
- :return: the original `data`
143
+ :return: The achived data
142
144
  :rtype: bytes
143
145
  """
144
146
  # System modules
@@ -153,86 +155,179 @@ class ExtractArchiveWriter(Writer):
153
155
  return data
154
156
 
155
157
 
158
+ class FileTreeWriter(Writer):
159
+ """Writer for a file tree in NeXus format"""
160
+ def write(self, data, outputdir, force_overwrite=False):
161
+ """Write a NeXus format object contained in `data` to a
162
+ directory tree stuctured like the NeXus tree.
163
+
164
+ :param data: The data to write to disk.
165
+ :type data: CHAP.pipeline.PipelineData
166
+ :param outputdir: The name of the directory to write to.
167
+ :type outputdir: str
168
+ :param force_overwrite: Flag to allow data to be overwritten
169
+ if it already exists, defaults to `False`.
170
+ :type force_overwrite: bool, optional
171
+ :raises RuntimeError: If `filename` already exists and
172
+ `force_overwrite` is `False`.
173
+ :return: The data written to disk.
174
+ :rtype: Union[nexusformat.nexus.NXroot,
175
+ nexusformat.nexus.NXentry]
176
+ """
177
+ # Third party modules
178
+ from nexusformat.nexus import (
179
+ NXentry,
180
+ NXroot,
181
+ )
182
+
183
+ data = self.unwrap_pipelinedata(data)[-1]
184
+ if isinstance(data, NXroot):
185
+ if 'default' in data.attrs:
186
+ nxentry = data[data.attrs['default']]
187
+ else:
188
+ nxentry = [v for v in data.values()
189
+ if isinstance(data, NXentry)]
190
+ if len(nxentry) == 1:
191
+ nxentry = nxentry[0]
192
+ else:
193
+ raise TypeError('Cannot write object of type '
194
+ f'{type(data).__name__} as a file tree '
195
+ 'to disk.')
196
+ elif isinstance(data, NXentry):
197
+ nxentry = data
198
+ else:
199
+ raise TypeError('Cannot write object of type '
200
+ f'{type(data).__name__} as a file tree to disk.')
201
+
202
+ write_filetree(nxentry, outputdir, force_overwrite)
203
+
204
+ return data
205
+
206
+
207
+ class MatplotlibAnimationWriter(Writer):
208
+ """Writer for saving matplotlib animations."""
209
+ def write(self, data, filename, fps=1):
210
+ """Write the matplotlib.animation.ArtistAnimation object
211
+ contained in `data` to file.
212
+
213
+ :param data: The matplotlib animation.
214
+ :type data: CHAP.pipeline.PipelineData
215
+ :param filename: The name of the file to write to.
216
+ :type filename: str
217
+ :param fps: Movie frame rate (frames per second),
218
+ defaults to `1`
219
+ :type fps: int, optional
220
+ :return: The original animation.
221
+ :rtype: matplotlib.animation.ArtistAnimation
222
+ """
223
+ data = self.unwrap_pipelinedata(data)[-1]
224
+ extension = os_path.splitext(filename)[1]
225
+ if not extension:
226
+ data.save(f'{filename}.gif', fps=fps)
227
+ elif extension in '.gif':
228
+ data.save(filename, fps=fps)
229
+ elif extension == '.mp4':
230
+ data.save(filename, writer='ffmpeg', fps=fps)
231
+
232
+ return data
233
+
234
+
156
235
  class MatplotlibFigureWriter(Writer):
157
236
  """Writer for saving matplotlib figures to image files."""
158
237
  def write(self, data, filename, savefig_kw={}, force_overwrite=False):
159
- """Write the matplotlib.fgure.Figure contained in `data` to
160
- the filename provided.
238
+ """Write the matplotlib.figure.Figure contained in `data` to
239
+ file.
161
240
 
162
- :param data: input containing a matplotlib figure
241
+ :param data: The matplotlib figure
163
242
  :type data: CHAP.pipeline.PipelineData
164
- :param filename: name of the file to write to.
165
- :param savefig_kw: keyword args to pass to
166
- matplotlib.figure.Figure.savefig, defaults to {}
243
+ :param filename: The name of the file to write to.
244
+ :type filename: str
245
+ :param savefig_kw: Keyword args to pass to
246
+ matplotlib.figure.Figure.savefig, defaults to {}.
167
247
  :type savefig_kw: dict, optional
168
- :param force_overwrite: flag to allow data in `filename` to be
169
- overwritten, if it already exists.
170
- :return: the original input data
248
+ :param force_overwrite: Flag to allow data in `filename` to be
249
+ overwritten if it already exists, defaults to `False`.
250
+ :type force_overwrite: bool, optional
251
+ :raises RuntimeError: If `filename` already exists and
252
+ `force_overwrite` is `False`.
253
+ :return: The original figure object
254
+ :rtype: matplotlib.figure.Figure
171
255
  """
172
256
  data = self.unwrap_pipelinedata(data)[-1]
173
257
  write_matplotlibfigure(data, filename, force_overwrite)
258
+
174
259
  return data
175
260
 
176
261
 
177
262
  class NexusWriter(Writer):
178
263
  """Writer for NeXus files from `NXobject`-s"""
179
264
  def write(self, data, filename, force_overwrite=False):
180
- """Write `data` to a NeXus file
181
-
182
- :param data: the data to write to `filename`.
183
- :type data: nexusformat.nexus.NXobject
184
- :param filename: name of the file to write to.
185
- :param force_overwrite: flag to allow data in `filename` to be
186
- overwritten, if it already exists.
187
- :return: the original input data
265
+ """Write the NeXus object contained in `data` to file.
266
+
267
+ :param data: The data to write to file.
268
+ :type data: CHAP.pipeline.PipelineData
269
+ :param filename: The name of the file to write to.
270
+ :param force_overwrite: Flag to allow data in `filename` to be
271
+ overwritten if it already exists, defaults to `False`.
272
+ :type force_overwrite: bool, optional
273
+ :raises RuntimeError: If `filename` already exists and
274
+ `force_overwrite` is `False`.
275
+ :return: The data written to file.
276
+ :rtype: nexusformat.nexus.NXobject
188
277
  """
189
278
  data = self.unwrap_pipelinedata(data)[-1]
190
279
  write_nexus(data, filename, force_overwrite)
280
+
191
281
  return data
192
282
 
193
283
 
194
284
  class TXTWriter(Writer):
195
- """Writer for plain text files from string or list of strings."""
196
- def write(self, data, filename, force_overwrite=False, append=False):
197
- """If `data` is a `str`, `tuple[str]` or `list[str]`, write it
198
- to `filename`.
199
-
200
- :param data: the string or tuple or list of strings to write to
201
- `filename`.
202
- :type data: str, tuple, list
203
- :param filename: name of the file to write to.
285
+ """Writer for plain text files from string or tuples or lists of
286
+ strings."""
287
+ def write(self, data, filename, append=False, force_overwrite=False):
288
+ """Write a string or tuple or list of strings contained in
289
+ `data` to file.
290
+
291
+ :param data: The data to write to disk.
292
+ :type data: str, tuple[str], list[str]
293
+ :param filename: The name of the file to write to.
204
294
  :type filename: str
205
- :param force_overwrite: flag to allow data in `filename` to be
206
- overwritten if it already exists.
207
- :type force_overwrite: bool
208
- :raises TypeError: if `data` is not a `str`, `tuple[str]` or
209
- `list[str]`
210
- :raises RuntimeError: if `filename` already exists and
295
+ :param append: Flag to allow data in `filename` to be
296
+ be appended, defaults to `False`.
297
+ :type append: bool, optional
298
+ :param force_overwrite: Flag to allow data in `filename` to be
299
+ overwritten if it already exists, defaults to `False`.
300
+ :type force_overwrite: bool, optional
301
+ :raises TypeError: If the object contained in `data` is not a
302
+ `str`, `tuple[str]` or `list[str]`.
303
+ :raises RuntimeError: If `filename` already exists and
211
304
  `force_overwrite` is `False`.
212
- :return: the original input data
213
- :rtype: str, tuple, list
305
+ :return: The data written to file.
306
+ :rtype: str, tuple[str], list[str]
214
307
  """
215
308
  data = self.unwrap_pipelinedata(data)[-1]
216
309
  write_txt(data, filename, force_overwrite, append)
310
+
217
311
  return data
218
312
 
219
313
 
220
314
  class YAMLWriter(Writer):
221
315
  """Writer for YAML files from `dict`-s"""
222
316
  def write(self, data, filename, force_overwrite=False):
223
- """If `data` is a `dict`, write it to `filename`.
317
+ """Write the dictionary contained in `data` to file.
224
318
 
225
- :param data: the dictionary to write to `filename`.
319
+ :param data: The data to write to file.
226
320
  :type data: dict
227
- :param filename: name of the file to write to.
321
+ :param filename: The name of the file to write to.
228
322
  :type filename: str
229
- :param force_overwrite: flag to allow data in `filename` to be
230
- overwritten if it already exists.
231
- :type force_overwrite: bool
232
- :raises TypeError: if `data` is not a `dict`
233
- :raises RuntimeError: if `filename` already exists and
323
+ :param force_overwrite: Flag to allow data in `filename` to be
324
+ overwritten if it already exists, defaults to `False`.
325
+ :type force_overwrite: bool, optional
326
+ :raises TypeError: If the object contained in `data` is not a
327
+ `dict`.
328
+ :raises RuntimeError: If `filename` already exists and
234
329
  `force_overwrite` is `False`.
235
- :return: the original input data
330
+ :return: The data written to file.
236
331
  :rtype: dict
237
332
  """
238
333
  data = self.unwrap_pipelinedata(data)[-1]
@@ -240,47 +335,8 @@ class YAMLWriter(Writer):
240
335
  return data
241
336
 
242
337
 
243
- class FileTreeWriter(Writer):
244
- """Writer for a file tree in NeXus format"""
245
- def write(self, data, outputdir, force_overwrite=False):
246
- """Write `data` to a NeXus file
247
-
248
- :param data: the data to write to disk.
249
- :type data: Union[nexusformat.nexus.NXroot,
250
- nexusformat.nexus.NXentry]
251
- :param force_overwrite: flag to allow data to be overwritten,
252
- if it already exists.
253
- :return: the original input data
254
- """
255
- # Third party modules
256
- from nexusformat.nexus import (
257
- NXentry,
258
- NXroot,
259
- )
260
-
261
- data = self.unwrap_pipelinedata(data)[-1]
262
- if isinstance(data, NXroot):
263
- if 'default' in data.attrs:
264
- nxentry = data[data.attrs['default']]
265
- else:
266
- nxentry = [v for v in data.values()
267
- if isinstance(data, NXentry)]
268
- if len(nxentry) == 1:
269
- nxentry = nxentry[0]
270
- else:
271
- raise TypeError('Cannot write object of type '
272
- f'{type(data).__name__} as a file tree '
273
- 'to disk.')
274
- elif isinstance(data, NXentry):
275
- nxentry = data
276
- else:
277
- raise TypeError('Cannot write object of type '
278
- f'{type(data).__name__} as a file tree to disk.')
279
-
280
- write_filetree(nxentry, outputdir, force_overwrite)
281
- return data
282
-
283
-
284
338
  if __name__ == '__main__':
339
+ # Local modules
285
340
  from CHAP.writer import main
341
+
286
342
  main()