From 4ccf3c2d2e8c4c04fe022f19d9d8c0e9e57d05c8 Mon Sep 17 00:00:00 2001 From: "Michael D. M. Dryden" Date: Tue, 5 Apr 2016 20:47:04 -0400 Subject: [PATCH] Separate more save stuff from UI. --- dstat_interface/analysis.py | 12 +- dstat_interface/dstat_comm.py | 7 +- dstat_interface/interface/save.py | 182 +++++++++++++----------------- dstat_interface/main.py | 62 ++++++---- dstat_interface/plot.py | 12 +- 5 files changed, 134 insertions(+), 141 deletions(-) diff --git a/dstat_interface/analysis.py b/dstat_interface/analysis.py index 366c38a..33144c0 100755 --- a/dstat_interface/analysis.py +++ b/dstat_interface/analysis.py @@ -109,24 +109,24 @@ def do_analysis(experiment): if experiment.parameters['stats_start_true']: start = experiment.parameters['stats_start'] else: - start = min(experiment.data[0][0]) + start = min(experiment.data['data'][0][0]) if experiment.parameters['stats_stop_true']: stop = experiment.parameters['stats_stop'] else: - stop = min(experiment.data[0][0]) + stop = min(experiment.data['data'][0][0]) - data = _data_slice(experiment.data, + data = _data_slice(experiment.data['data'], start, stop ) else: - data = experiment.data + data = experiment.data['data'] experiment.analysis.update(_summary_stats(data)) try: - x, y = experiment.ftdata[0] + x, y = experiment.data['ft'][0] experiment.analysis['FT Integral'] = _integrateSpectrum( x, y, @@ -134,7 +134,7 @@ def do_analysis(experiment): float(experiment.parameters['fft_int']) ) - except AttributeError: + except KeyError: pass def _data_slice(data, start, stop): diff --git a/dstat_interface/dstat_comm.py b/dstat_interface/dstat_comm.py index 4c83d66..ffa43f2 100755 --- a/dstat_interface/dstat_comm.py +++ b/dstat_interface/dstat_comm.py @@ -340,9 +340,10 @@ class Experiment(object): self.scan = 0 self.time = 0 self.plots = {} + self.data = {} # list of scans, tuple of dimensions, list of data - self.data = [([], [])] + self.data['data'] = [([], [])] self.line_data = ([], []) major, minor = self.parameters['version'] @@ -744,7 +745,7 @@ class SWVExp(Experiment): self.datatype = "SWVData" self.xlabel = "Voltage (mV)" self.ylabel = "Current (A)" - self.data = [([], [], [], [])] # voltage, current, forwards, reverse + self.data['data'] = [([], [], [], [])] # voltage, current, forwards, reverse self.line_data = ([], [], [], []) self.datalength = 2 * self.parameters['scans'] self.databytes = 10 @@ -803,7 +804,7 @@ class DPVExp(SWVExp): self.datatype = "SWVData" self.xlabel = "Voltage (mV)" self.ylabel = "Current (A)" - self.data = [([], [], [], [])] # voltage, current, forwards, reverse + self.data['data'] = [([], [], [], [])] # voltage, current, forwards, reverse self.line_data = ([], [], [], []) self.datalength = 2 self.databytes = 10 diff --git a/dstat_interface/interface/save.py b/dstat_interface/interface/save.py index 3f11d9f..675bcd1 100755 --- a/dstat_interface/interface/save.py +++ b/dstat_interface/interface/save.py @@ -51,12 +51,8 @@ def manSave(current_exp): filter_selection = fcd.get_filter().get_name() if filter_selection.endswith("(.txt)"): - if (current_exp.parameters['shutter_true'] and - current_exp.parameters['sync_true']): - text(current_exp, current_exp.data, "-".join((path,'data'))) - text(current_exp, current_exp.ftdata, "-".join((path,'ft'))) - else: - text(current_exp, current_exp.data, path, auto=True) + save_text(current_exp, path) + fcd.destroy() elif response == gtk.RESPONSE_CANCEL: @@ -101,25 +97,6 @@ def plot_save_dialog(plots): elif response == gtk.RESPONSE_CANCEL: fcd.destroy() -def save_plot(plots, path): - """Saves everything in plots to path. Appends a number for duplicates. - If no file extension or unknown, uses pdf. - """ - root_path = path - name, _sep, ext = root_path.rpartition('.') - if ext == '': - ext = 'pdf' - - num = '' - j = 0 - - for i in plots: # Test for any existing files - while os.path.exists("%s%s-%s.%s" % (name, num, i, ext)): - j += 1 - num = j - - for i in plots: # save data - plots[i].figure.savefig("%s%s-%s.%s" % (name, num, i, ext)) def man_param_save(window): fcd = gtk.FileChooserDialog("Save Parameters…", @@ -181,98 +158,101 @@ def man_param_load(window): elif response == gtk.RESPONSE_CANCEL: fcd.destroy() -def autoSave(current_exp, dir_button, name, expnumber): +def autoSave(exp, path, name): if name == "": name = "file" - path = dir_button.get_filename() + path += '/' path += name + + save_text(exp, path) - if (current_exp.parameters['shutter_true'] and current_exp.parameters['sync_true']): - text(current_exp, current_exp.data, "-".join((path,'data')), auto=True) - text(current_exp, current_exp.ftdata, "-".join((path,'ft')), auto=True) - else: - text(current_exp, current_exp.data, path, auto=True) - -def autoPlot(plots, dir_button, name, expnumber): - for i in plots: - if name == "": - name = "file" - - path = dir_button.get_filename() - path += '/' - path += name - path += '-' - path += str(expnumber) - path += '-' - path += i - - if path.endswith(".pdf"): - path = path.rstrip(".pdf") - - j = 1 - while os.path.exists("".join([path, ".pdf"])): - if j > 1: - path = path[:-len(str(j))] - path += str(j) - j += 1 +def autoPlot(exp, path, name): + if name == "": + name = "file" + + path += '/' + path += name + if not (path.endswith(".pdf") or path.endswith(".png")): path += ".pdf" - plots[i].figure.savefig(path) - -def text(exp, data, path, auto=False): - if path.endswith(".txt"): - path = path.rstrip(".txt") - - if auto == True: - j = 1 - while os.path.exists("%s.txt" % path): - if j > 1: - path = path[:-len(str(j))] - path += str(j) - j += 1 - path += ".txt" - file = open(path, 'w') + save_plot(exp, path) - time = exp.time +def save_text(exp, path): + name, _sep, ext = path.rpartition('.') # ('','',string) if no match + if _sep == '': + name = ext + ext = 'txt' + + num = '' + j = 0 + + for dname in exp.data: # Test for any existing files + while os.path.exists("%s%s-%s.%s" % (name, num, dname, ext)): + j += 1 + num = j + + for dname in exp.data: # save data + file = open("%s%s-%s.%s" % (name, num, dname, ext), 'w') - header = "".join(['# TIME ', time.isoformat(), "\n"]) + time = exp.time + header = "".join(['# TIME ', time.isoformat(), "\n"]) - header += "# DSTAT COMMANDS\n# " - for i in exp.commands: - header += i + header += "# DSTAT COMMANDS\n# " + for i in exp.commands: + header += i - file.write("".join([header, '\n'])) + file.write("".join([header, '\n'])) - analysis_buffer = [] + analysis_buffer = [] - if exp.analysis != {}: - analysis_buffer.append("# ANALYSIS") - for key, value in exp.analysis.iteritems(): - analysis_buffer.append("# %s:" % key) - for scan in value: - number, result = scan - analysis_buffer.append( - "# Scan %s -- %s" % (number, result) - ) + if exp.analysis != {}: + analysis_buffer.append("# ANALYSIS") + for key, value in exp.analysis.iteritems(): + analysis_buffer.append("# %s:" % key) + for scan in value: + number, result = scan + analysis_buffer.append( + "# Scan %s -- %s" % (number, result) + ) - for i in analysis_buffer: - file.write("%s\n" % i) + for i in analysis_buffer: + file.write("%s\n" % i) - # Write out actual data - line_buffer = [] + # Write out actual data + line_buffer = [] - for scan in zip(*data): - for dimension in scan: - for i in range(len(dimension)): - try: - line_buffer[i] += "%s " % dimension[i] - except IndexError: - line_buffer.append("") - line_buffer[i] += "%s " % dimension[i] + for scan in zip(*exp.data[dname]): + for dimension in scan: + for i in range(len(dimension)): + try: + line_buffer[i] += "%s " % dimension[i] + except IndexError: + line_buffer.append("") + line_buffer[i] += "%s " % dimension[i] - for i in line_buffer: - file.write("%s\n" % i) + for i in line_buffer: + file.write("%s\n" % i) - file.close() + file.close() + +def save_plot(exp, path): + """Saves everything in exp.plots to path. Appends a number for duplicates. + If no file extension or unknown, uses pdf. + """ + name, _sep, ext = path.rpartition('.') + if _sep == '': + name = ext + ext = 'pdf' + + num = '' + j = 0 + + for i in exp.plots: # Test for any existing files + while os.path.exists("%s%s-%s.%s" % (name, num, i, ext)): + j += 1 + num = j + + for i in exp.plots: # save data + exp.plots[i].figure.savefig("%s%s-%s.%s" % (name, num, i, ext)) \ No newline at end of file diff --git a/dstat_interface/main.py b/dstat_interface/main.py index 75e2486..ff14497 100755 --- a/dstat_interface/main.py +++ b/dstat_interface/main.py @@ -605,7 +605,8 @@ class Main(object): raise def experiment_running_data(self): - """Receive data from experiment process and add to current_exp.data. + """Receive data from experiment process and add to + current_exp.data['data]. Run in GTK main loop. Returns: @@ -619,12 +620,12 @@ class Main(object): self.line, data = incoming if self.line > self.lastdataline: - self.current_exp.data.append( + self.current_exp.data['data'].append( deepcopy(self.current_exp.line_data)) self.lastdataline = self.line - for i in range(len(self.current_exp.data[self.line])): - self.current_exp.data[self.line][i].append(data[i]) + for i in range(len(self.current_exp.data['data'][self.line])): + self.current_exp.data['data'][self.line][i].append(data[i]) if comm.serial_instance.data_pipe_p.poll(): self.experiment_running_data() @@ -698,8 +699,6 @@ class Main(object): gobject.source_remove(self.experiment_proc[0]) gobject.source_remove(self.plot_proc) # stop automatic plot update self.experiment_running_plot() # make sure all data updated on plot - - self.databuffer.set_text("") self.databuffer.place_cursor(self.databuffer.get_start_iter()) @@ -712,10 +711,20 @@ class Main(object): self.current_exp.parameters['sync_true']): self.ft_plot.updateline(self.current_exp, 0) self.ft_plot.redraw() - for col in zip(*self.current_exp.ftdata): - for row in col: - self.databuffer.insert_at_cursor(str(row)+ " ") - self.databuffer.insert_at_cursor("\n") + + line_buffer = [] + + for scan in self.current_exp.data['ft']: + for dimension in scan: + for i in range(len(dimension)): + try: + line_buffer[i] += "%s " % dimension[i] + except IndexError: + line_buffer.append("") + line_buffer[i] += "%s " % dimension[i] + + for i in line_buffer: + self.databuffer.insert_at_cursor("%s\n" % i) # Run Analysis analysis.do_analysis(self.current_exp) @@ -752,7 +761,7 @@ class Main(object): line_buffer = [] - for scan in self.current_exp.data: + for scan in self.current_exp.data['data']: for dimension in scan: for i in range(len(dimension)): try: @@ -766,17 +775,15 @@ class Main(object): # Autosaving if self.autosave_checkbox.get_active(): - save.autoSave(self.current_exp, self.autosavedir_button, - self.autosavename.get_text(), self.expnumber) - plots = {'data':self.plot} - - if (self.current_exp.parameters['shutter_true'] and - self.current_exp.parameters['sync_true']): - plots['ft'] = self.ft_plot - - save.autoPlot(plots, self.autosavedir_button, - self.autosavename.get_text(), self.expnumber) - self.expnumber += 1 + save.autoSave(self.current_exp, + self.autosavedir_button.get_filename(), + self.autosavename.get_text() + ) + + save.autoPlot(self.current_exp, + self.autosavedir_button.get_filename(), + self.autosavename.get_text() + ) # uDrop # UI stuff @@ -800,12 +807,17 @@ class Main(object): def on_file_save_exp_activate(self, menuitem, data=None): """Activate dialogue to save current experiment data. """ - if self.current_exp: + try: save.manSave(self.current_exp) - + except AttributeError: + logger.warning("Tried to save with no experiment run") + def on_file_save_plot_activate(self, menuitem, data=None): """Activate dialogue to save current plot.""" - save.plot_save_dialog(self.current_exp.plots) + try: + save.plot_save_dialog(self.current_exp) + except AttributeError: + logger.warning("Tried to save with no experiment run") def on_file_save_params_activate(self, menuitem, data=None): """Activate dialogue to save current experiment parameters. """ diff --git a/dstat_interface/plot.py b/dstat_interface/plot.py index 50b431e..7c5c017 100755 --- a/dstat_interface/plot.py +++ b/dstat_interface/plot.py @@ -138,12 +138,12 @@ class PlotBox(object): the Experiment instance. """ # limits display to 2000 data points per line - divisor = len(Experiment.data[line_number][0]) // 2000 + 1 + divisor = len(Experiment.data['data'][line_number][0]) // 2000 + 1 self.axe1.lines[line_number].set_ydata( - Experiment.data[line_number][1][1::divisor]) + Experiment.data['data'][line_number][1][1::divisor]) self.axe1.lines[line_number].set_xdata( - Experiment.data[line_number][0][1::divisor]) + Experiment.data['data'][line_number][0][1::divisor]) def changetype(self, Experiment): """Change plot type. Set axis labels and x bounds to those stored @@ -172,8 +172,8 @@ class FT_Box(PlotBox): if data[i] > target: return i - y = Experiment.data[line_number][1] - x = Experiment.data[line_number][0] + y = Experiment.data['data'][line_number][1] + x = Experiment.data['data'][line_number][0] freq = Experiment.parameters['adc_rate_hz'] i = search_value(x, float(Experiment.parameters['fft_start'])) y1 = y[i:] @@ -185,7 +185,7 @@ class FT_Box(PlotBox): f, Y = plotSpectrum(y1[min_index:max_index],freq) self.axe1.lines[line_number].set_ydata(Y) self.axe1.lines[line_number].set_xdata(f) - Experiment.ftdata = [(f, Y)] + Experiment.data['ft'] = [(f, Y)] def changetype(self, Experiment): """Change plot type. Set axis labels and x bounds to those stored -- GitLab