Skip to content
Snippets Groups Projects
Commit 4dca984a authored by Michael DM Dryden's avatar Michael DM Dryden
Browse files

Moved plots to dict attribute of Experiment instances.

Separated plot save dialogue from actual saving.
parent 2c7f5c3f
No related merge requests found
......@@ -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 = [([], [])]
......
......@@ -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)
......
......@@ -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. """
......
......@@ -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()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment