From 56fabfeebd10a23f96228cd06de077fea0741350 Mon Sep 17 00:00:00 2001 From: WilliamHPNielsen Date: Tue, 28 Nov 2017 14:07:24 +0100 Subject: [PATCH 1/3] feat: make gnuplot dataset writing more flexible Allow users to save multiple files with a custom filename to a single folder containing no snapshots --- qcodes/data/data_set.py | 21 ++++++++++++++++----- qcodes/data/gnuplot_format.py | 12 ++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/qcodes/data/data_set.py b/qcodes/data/data_set.py index 0213d0b48673..8fa4e205d892 100644 --- a/qcodes/data/data_set.py +++ b/qcodes/data/data_set.py @@ -469,7 +469,7 @@ def read_metadata(self): return self.formatter.read_metadata(self) - def write(self, write_metadata=False, only_complete=True): + def write(self, write_metadata=False, only_complete=True, filename=None): """ Writes updates to the DataSet to storage. N.B. it is recommended to call data_set.finalize() when a DataSet is @@ -480,15 +480,19 @@ def write(self, write_metadata=False, only_complete=True): only_complete (bool): passed on to the match_save_range inside self.formatter.write. Used to ensure that all new data gets saved even when some columns are strange. + filename (Optional[str]): The filename (minus extension) to use. + The file gets saved in the usual location. """ if self.location is False: return + # NB: Only the gnuplot formatter has a "filename" kwarg self.formatter.write(self, self.io, self.location, write_metadata=write_metadata, - only_complete=only_complete) + only_complete=only_complete, + filename=filename) def write_copy(self, path=None, io_manager=None, location=None): """ @@ -562,21 +566,28 @@ def save_metadata(self): self.snapshot() self.formatter.write_metadata(self, self.io, self.location) - def finalize(self): + def finalize(self, filename=None, write_metadata=True): """ Mark the DataSet complete and write any remaining modifications. Also closes the data file(s), if the ``Formatter`` we're using supports that. + + Args: + filename (Optional[str]): The file name (minus extension) to + write to. The location of the file is the usual one. + write_metadata (bool): Whether to save a snapshot. For e.g. dumping + raw data inside a loop, a snapshot is not wanted. """ log.debug('Finalising the DataSet. Writing.') # write all new data, not only (to?) complete columns - self.write(only_complete=False) + self.write(only_complete=False, filename=filename) if hasattr(self.formatter, 'close_file'): self.formatter.close_file(self) - self.save_metadata() + if write_metadata: + self.save_metadata() def snapshot(self, update=False): """JSON state of the DataSet.""" diff --git a/qcodes/data/gnuplot_format.py b/qcodes/data/gnuplot_format.py index b3f0c288193b..32436cd1603a 100644 --- a/qcodes/data/gnuplot_format.py +++ b/qcodes/data/gnuplot_format.py @@ -244,7 +244,7 @@ def _get_labels(self, labelstr): return [l.replace('\\"', '"').replace('\\\\', '\\') for l in parts] def write(self, data_set, io_manager, location, force_write=False, - write_metadata=True, only_complete=True): + write_metadata=True, only_complete=True, filename=None): """ Write updates in this DataSet to storage. @@ -259,7 +259,11 @@ def write(self, data_set, io_manager, location, force_write=False, or only complete rows? Is used to make sure that everything gets written when the DataSet is finalised, even if some dataarrays are strange (like, full of nans) + filename (Optional[str]): Filename to save to. Will override + the usual naming scheme and possibly overwrite files, so + use with care. The file will be saved in the normal location. """ + print('DEBUG: received location: {}'.format(location)) arrays = data_set.arrays # puts everything with same dimensions together @@ -271,7 +275,11 @@ def write(self, data_set, io_manager, location, force_write=False, for group in groups: log.debug('Attempting to write the following ' 'group: {}'.format(group)) - fn = io_manager.join(location, group.name + self.extension) + + if filename: + fn = io_manager.join(location, filename + self.extension) + else: + fn = io_manager.join(location, group.name + self.extension) written_files.add(fn) From 3078731f3a07f84e9dd3245888afa767b53218b3 Mon Sep 17 00:00:00 2001 From: WilliamHPNielsen Date: Tue, 28 Nov 2017 14:10:45 +0100 Subject: [PATCH 2/3] fix: remove debug print --- qcodes/data/gnuplot_format.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qcodes/data/gnuplot_format.py b/qcodes/data/gnuplot_format.py index 32436cd1603a..12bfb35b11d6 100644 --- a/qcodes/data/gnuplot_format.py +++ b/qcodes/data/gnuplot_format.py @@ -263,7 +263,6 @@ def write(self, data_set, io_manager, location, force_write=False, the usual naming scheme and possibly overwrite files, so use with care. The file will be saved in the normal location. """ - print('DEBUG: received location: {}'.format(location)) arrays = data_set.arrays # puts everything with same dimensions together From 2b4b5fe89b3be7eabb178754d547989c70f545a6 Mon Sep 17 00:00:00 2001 From: WilliamHPNielsen Date: Tue, 28 Nov 2017 14:43:26 +0100 Subject: [PATCH 3/3] fix: don't pass filename kwarg to hdf5 formatters --- qcodes/data/data_set.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/qcodes/data/data_set.py b/qcodes/data/data_set.py index 8fa4e205d892..3c62d9de8c97 100644 --- a/qcodes/data/data_set.py +++ b/qcodes/data/data_set.py @@ -486,13 +486,20 @@ def write(self, write_metadata=False, only_complete=True, filename=None): if self.location is False: return - # NB: Only the gnuplot formatter has a "filename" kwarg - self.formatter.write(self, - self.io, - self.location, - write_metadata=write_metadata, - only_complete=only_complete, - filename=filename) + # Only the gnuplot formatter has a "filename" kwarg + if isinstance(self.formatter, GNUPlotFormat): + self.formatter.write(self, + self.io, + self.location, + write_metadata=write_metadata, + only_complete=only_complete, + filename=filename) + else: + self.formatter.write(self, + self.io, + self.location, + write_metadata=write_metadata, + only_complete=only_complete) def write_copy(self, path=None, io_manager=None, location=None): """