From 4dca984ab7cd3bf9a494ad240e8dd6af702d318b Mon Sep 17 00:00:00 2001 From: "Michael D. M. Dryden" Date: Tue, 5 Apr 2016 18:52:20 -0400 Subject: [PATCH] Moved plots to dict attribute of Experiment instances. Separated plot save dialogue from actual saving. --- dstat_interface/dstat_comm.py | 1 + dstat_interface/interface/save.py | 74 ++++++++++++++----------------- dstat_interface/main.py | 12 ++--- dstat_interface/plot.py | 12 +++-- 4 files changed, 46 insertions(+), 53 deletions(-) diff --git a/dstat_interface/dstat_comm.py b/dstat_interface/dstat_comm.py index e4aef62..4c83d66 100755 --- a/dstat_interface/dstat_comm.py +++ b/dstat_interface/dstat_comm.py @@ -339,6 +339,7 @@ class Experiment(object): self.databytes = 8 self.scan = 0 self.time = 0 + self.plots = {} # list of scans, tuple of dimensions, list of data self.data = [([], [])] diff --git a/dstat_interface/interface/save.py b/dstat_interface/interface/save.py index 6908500..3f11d9f 100755 --- a/dstat_interface/interface/save.py +++ b/dstat_interface/interface/save.py @@ -50,14 +50,9 @@ def manSave(current_exp): logger.info("Selected filepath: %s", path) filter_selection = fcd.get_filter().get_name() - if filter_selection.endswith("(.npy)"): - if (current_exp.parameters['shutter_true'] and current_exp.parameters['sync_true']): - npy(current_exp, current_exp.data, "-".join((path,'data'))) - npy(current_exp, current_exp.ftdata, "-".join((path,'ft'))) - else: - npy(current_exp, current_exp.data, path, auto=True) - elif filter_selection.endswith("(.txt)"): - if (current_exp.parameters['shutter_true'] and current_exp.parameters['sync_true']): + 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: @@ -67,7 +62,7 @@ def manSave(current_exp): elif response == gtk.RESPONSE_CANCEL: fcd.destroy() -def plotSave(plots): +def plot_save_dialog(plots): fcd = gtk.FileChooserDialog("Save Plot…", None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, @@ -90,26 +85,42 @@ def plotSave(plots): path = fcd.get_filename() logger.info("Selected filepath: %s", path) filter_selection = fcd.get_filter().get_name() + + if filter_selection.endswith("(.pdf)"): + if not path.endswith(".pdf"): + path += ".pdf" - for i in plots: - save_path = path - save_path += '-' - save_path += i - - if filter_selection.endswith("(.pdf)"): - if not save_path.endswith(".pdf"): - save_path += ".pdf" - - elif filter_selection.endswith("(.png)"): - if not save_path.endswith(".png"): - save_path += ".png" + elif filter_selection.endswith("(.png)"): + if not path.endswith(".png"): + path += ".png" + + save_plot(plots, path) - plots[i].figure.savefig(save_path) # determines format from file extension fcd.destroy() 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…", None, @@ -176,8 +187,6 @@ def autoSave(current_exp, dir_button, name, expnumber): path = dir_button.get_filename() path += '/' path += name - path += '-' - path += str(expnumber) if (current_exp.parameters['shutter_true'] and current_exp.parameters['sync_true']): text(current_exp, current_exp.data, "-".join((path,'data')), auto=True) @@ -211,28 +220,13 @@ def autoPlot(plots, dir_button, name, expnumber): path += ".pdf" plots[i].figure.savefig(path) -def npy(exp, data, path, auto=False): - if path.endswith(".npy"): - path = path.rstrip(".npy") - - if auto == True: - j = 1 - while os.path.exists("".join([path, ".npy"])): - if j > 1: - path = path[:-len(str(j))] - path += str(j) - j += 1 - - np.save(path, data) - def text(exp, data, path, auto=False): if path.endswith(".txt"): path = path.rstrip(".txt") if auto == True: j = 1 - - while os.path.exists("".join([path, ".txt"])): + while os.path.exists("%s.txt" % path): if j > 1: path = path[:-len(str(j))] path += str(j) diff --git a/dstat_interface/main.py b/dstat_interface/main.py index bc2af93..75e2486 100755 --- a/dstat_interface/main.py +++ b/dstat_interface/main.py @@ -112,8 +112,8 @@ class Main(object): # Setup Plots self.plot_notebook = self.builder.get_object('plot_notebook') - self.plot = plot.plotbox(self.plotwindow) - self.ft_plot = plot.ft_box(self.ft_window) + self.plot = plot.PlotBox(self.plotwindow) + self.ft_plot = plot.FT_Box(self.ft_window) #fill adc_pot_box self.adc_pot_box = self.builder.get_object('gain_adc_box') @@ -805,13 +805,7 @@ class Main(object): def on_file_save_plot_activate(self, menuitem, data=None): """Activate dialogue to save current plot.""" - plots = {'data':self.plot} - - if (self.current_exp.parameters['shutter_true'] and - self.current_exp.parameters['sync_true']): - plots['ft'] = self.ft_plot - - save.plotSave(plots) + save.plot_save_dialog(self.current_exp.plots) 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 4509428..50b431e 100755 --- a/dstat_interface/plot.py +++ b/dstat_interface/plot.py @@ -90,7 +90,7 @@ def findBounds(y): return (start_index, stop_index) -class plotbox(object): +class PlotBox(object): """Contains main data plot and associated methods.""" def __init__(self, plotwindow_instance): """Creates plot and moves it to a gtk container. @@ -147,11 +147,13 @@ class plotbox(object): def changetype(self, Experiment): """Change plot type. Set axis labels and x bounds to those stored - in the Experiment instance. + in the Experiment instance. Stores class instance in Experiment. """ self.axe1.set_xlabel(Experiment.xlabel) self.axe1.set_ylabel(Experiment.ylabel) self.axe1.set_xlim(Experiment.xmin, Experiment.xmax) + + Experiment.plots['data'] = self self.figure.canvas.draw() @@ -163,7 +165,7 @@ class plotbox(object): return True -class ft_box(plotbox): +class FT_Box(PlotBox): def updateline(self, Experiment, line_number): def search_value(data, target): for i in range(len(data)): @@ -187,11 +189,13 @@ class ft_box(plotbox): def changetype(self, Experiment): """Change plot type. Set axis labels and x bounds to those stored - in the Experiment instance. + in the Experiment instance. Stores class instance in Experiment. """ self.axe1.set_xlabel("Freq (Hz)") self.axe1.set_ylabel("|Y| (A/Hz)") self.axe1.set_xlim(0, Experiment.parameters['adc_rate_hz']/2) + + Experiment.plots['ft'] = self self.figure.canvas.draw() -- GitLab