Skip to content

Commit 4c7ed38

Browse files
WilliamHPNielsenjenshnielsen
authored andcommitted
feat: make gnuplot dataset writing more flexible (#886)
* feat: make gnuplot dataset writing more flexible Allow users to save multiple files with a custom filename to a single folder containing no snapshots * fix: remove debug print * fix: don't pass filename kwarg to hdf5 formatters
1 parent f86f51e commit 4c7ed38

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

qcodes/data/data_set.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def read_metadata(self):
469469
return
470470
self.formatter.read_metadata(self)
471471

472-
def write(self, write_metadata=False, only_complete=True):
472+
def write(self, write_metadata=False, only_complete=True, filename=None):
473473
"""
474474
Writes updates to the DataSet to storage.
475475
N.B. it is recommended to call data_set.finalize() when a DataSet is
@@ -480,15 +480,26 @@ def write(self, write_metadata=False, only_complete=True):
480480
only_complete (bool): passed on to the match_save_range inside
481481
self.formatter.write. Used to ensure that all new data gets
482482
saved even when some columns are strange.
483+
filename (Optional[str]): The filename (minus extension) to use.
484+
The file gets saved in the usual location.
483485
"""
484486
if self.location is False:
485487
return
486488

487-
self.formatter.write(self,
488-
self.io,
489-
self.location,
490-
write_metadata=write_metadata,
491-
only_complete=only_complete)
489+
# Only the gnuplot formatter has a "filename" kwarg
490+
if isinstance(self.formatter, GNUPlotFormat):
491+
self.formatter.write(self,
492+
self.io,
493+
self.location,
494+
write_metadata=write_metadata,
495+
only_complete=only_complete,
496+
filename=filename)
497+
else:
498+
self.formatter.write(self,
499+
self.io,
500+
self.location,
501+
write_metadata=write_metadata,
502+
only_complete=only_complete)
492503

493504
def write_copy(self, path=None, io_manager=None, location=None):
494505
"""
@@ -562,21 +573,28 @@ def save_metadata(self):
562573
self.snapshot()
563574
self.formatter.write_metadata(self, self.io, self.location)
564575

565-
def finalize(self):
576+
def finalize(self, filename=None, write_metadata=True):
566577
"""
567578
Mark the DataSet complete and write any remaining modifications.
568579
569580
Also closes the data file(s), if the ``Formatter`` we're using
570581
supports that.
582+
583+
Args:
584+
filename (Optional[str]): The file name (minus extension) to
585+
write to. The location of the file is the usual one.
586+
write_metadata (bool): Whether to save a snapshot. For e.g. dumping
587+
raw data inside a loop, a snapshot is not wanted.
571588
"""
572589
log.debug('Finalising the DataSet. Writing.')
573590
# write all new data, not only (to?) complete columns
574-
self.write(only_complete=False)
591+
self.write(only_complete=False, filename=filename)
575592

576593
if hasattr(self.formatter, 'close_file'):
577594
self.formatter.close_file(self)
578595

579-
self.save_metadata()
596+
if write_metadata:
597+
self.save_metadata()
580598

581599
def snapshot(self, update=False):
582600
"""JSON state of the DataSet."""

qcodes/data/gnuplot_format.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def _get_labels(self, labelstr):
244244
return [l.replace('\\"', '"').replace('\\\\', '\\') for l in parts]
245245

246246
def write(self, data_set, io_manager, location, force_write=False,
247-
write_metadata=True, only_complete=True):
247+
write_metadata=True, only_complete=True, filename=None):
248248
"""
249249
Write updates in this DataSet to storage.
250250
@@ -259,6 +259,9 @@ def write(self, data_set, io_manager, location, force_write=False,
259259
or only complete rows? Is used to make sure that everything
260260
gets written when the DataSet is finalised, even if some
261261
dataarrays are strange (like, full of nans)
262+
filename (Optional[str]): Filename to save to. Will override
263+
the usual naming scheme and possibly overwrite files, so
264+
use with care. The file will be saved in the normal location.
262265
"""
263266
arrays = data_set.arrays
264267

@@ -271,7 +274,11 @@ def write(self, data_set, io_manager, location, force_write=False,
271274
for group in groups:
272275
log.debug('Attempting to write the following '
273276
'group: {}'.format(group))
274-
fn = io_manager.join(location, group.name + self.extension)
277+
278+
if filename:
279+
fn = io_manager.join(location, filename + self.extension)
280+
else:
281+
fn = io_manager.join(location, group.name + self.extension)
275282

276283
written_files.add(fn)
277284

0 commit comments

Comments
 (0)