class PlotView(QFrame): """PlotView presents a matplotlib canvas with interaction possibilities. (picking and tooltip)""" def __init__(self): """Create a PlotView instance""" QFrame.__init__(self) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # setup some default data values self.data = PlotData() self.data.x_data_type = "number" self.data.setValid(False) self.plot_figure = PlotFigure() self.plot_settings = PlotSettings() self.canvas = FigureCanvas(self.plot_figure.getFigure()) self.canvas.setParent(self) self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.mouse_handler = MouseHandler(self) def toggleMember(self, line): gid = int(line.get_gid()) if gid in self.plot_settings.getSelectedMembers(): self.plot_settings.unselectMember(gid) else: self.plot_settings.selectMember(gid) @ert_gui.widgets.util.may_take_a_long_time def drawPlot(self): self.plot_figure.drawPlot(self.data, self.plot_settings) self.canvas.draw() def resizeEvent(self, event): QFrame.resizeEvent(self, event) self.canvas.resize(event.size().width(), event.size().height()) def loadSettings(self, name): if self.data.isValid(): plot_config_loader = PlotSettingsLoader() if not plot_config_loader.load(name, self.plot_settings): self.drawPlot() def saveSettings(self): if self.data.isValid(): plot_config_saver = PlotSettingsSaver() plot_config_saver.save(self.data.getSaveName(), self.plot_settings) def setData(self, data): self.saveSettings() self.data = data self.loadSettings(self.data.getSaveName()) def setXZoomFactors(self, xminf, xmaxf): self.plot_settings.setMinXZoom(xminf) self.plot_settings.setMaxXZoom(xmaxf) def setYZoomFactors(self, yminf, ymaxf): self.plot_settings.setMinYZoom(yminf) self.plot_settings.setMaxYZoom(ymaxf) def save(self): self.saveSettings() plot_generator = PlotGenerator(self.plot_settings.getPlotPath(), self.plot_settings.getPlotConfigPath()) plot_generator.save(self.data) def saveAll(self): self.saveSettings() plot_generator = PlotGenerator(self.plot_settings.getPlotPath(), self.plot_settings.getPlotConfigPath()) plot_generator.saveAll() def copyPlotSettings(self): plot_config_loader = PlotSettingsLoader() plot_config_loader.copy(self.plot_settings) def setPlotPath(self, plot_path): self.plot_settings.setPlotPath(plot_path) def setPlotConfigPath(self, path): self.plot_settings.setPlotConfigPath(path) def _selectedMemberIdentifier(self, artist): return artist.get_gid() in self.plot_settings.getSelectedMembers() def clearSelection(self): selected_lines = self.plot_figure.fig.findobj(self._selectedMemberIdentifier) for line in selected_lines: self.plot_settings.unselectMember(line.get_gid()) def displayToolTip(self, event): if not self.data is None and not event.xdata is None and not event.ydata is None: if self.data.getXDataType() == "time": date = matplotlib.dates.num2date(event.xdata) self.setToolTip("x: %s y: %04f" % (date.strftime("%d/%m-%Y"), event.ydata)) else: self.setToolTip("x: %04f y: %04f" % (event.xdata, event.ydata)) else: self.setToolTip("") def annotate(self, label, x, y, xt=None, yt=None): self.plot_settings.addAnnotation(label, x, y, xt, yt) def removeAnnotation(self, annotation_artist): annotations = self.plot_settings.getAnnotations() for annotation in annotations: if annotation.getUserData() == annotation_artist: self.plot_settings.removeAnnotation(annotation) def moveAnnotation(self, annotation_artist, xt, yt): annotations = self.plot_settings.getAnnotations() for annotation in annotations: if annotation.getUserData() == annotation_artist: annotation.xt = xt annotation.yt = yt annotation_artist.xytext = (xt, yt) def draw(self): self.canvas.draw() def setMinYLimit(self, value): self.plot_settings.setMinYLimit(value) def setMaxYLimit(self, value): self.plot_settings.setMaxYLimit(value) def setMinXLimit(self, value): self.plot_settings.setMinXLimit(value) def setMaxXLimit(self, value): self.plot_settings.setMaxXLimit(value) def getPlotConfigList(self): return self.plot_settings.getPlotConfigList()
class PlotGenerator(QFrame): def __init__(self, plot_path, plot_config_path): QFrame.__init__(self) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.plot_figure = PlotFigure() self.canvas = FigureCanvas(self.plot_figure.getFigure()) self.canvas.setParent(self) self.canvas.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) size = QSize(297*2, 210*2) # A4 aspectratio self.canvas.setMaximumSize(size) self.canvas.setMinimumSize(size) self.setMaximumSize(size) self.setMinimumSize(size) self.popup = Popup(self) self.plot_config_loader = PlotSettingsLoader() self.plot_settings = PlotSettings() self.plot_settings.setPlotPath(plot_path) self.plot_settings.setPlotConfigPath(plot_config_path) self.connect(self.popup, SIGNAL('updateProgress(int)'), self.updateProgress) self.plot_context_data_fetcher = PlotContextDataFetcher() self.plot_context_data_fetcher.initialize(self.plot_context_data_fetcher.getModel()) self.plot_data_fetcher = PlotDataFetcher() self.plot_data_fetcher.initialize(self.plot_data_fetcher.getModel()) def updateProgress(self, progress = 1): value = self.popup.progress_bar.value() self.popup.progress_bar.setValue(value + progress) def saveAll(self): self.popup.show() context_data = self.plot_context_data_fetcher.getFromModel() save_list = [] count = 0 for parameter in context_data.parameters: pt = parameter.type if pt == SummaryModel.TYPE or pt == KeywordModel.TYPE or pt == enums.obs_impl_type.FIELD_OBS: save_list.append(parameter) parameter.setUserData({'state' : enums.ert_state_enum.FORECAST}) if pt == KeywordModel.TYPE: choices = context_data.key_index_list[parameter.name] parameter.getUserData()['key_index_choices'] = choices count += len(choices) else: count += 1 self.popup.progress_bar.setMaximum(count) for parameter in save_list: if parameter.type == KeywordModel.TYPE: for choice in parameter.getUserData()['key_index_choices']: self.plot_data_fetcher.setParameter(parameter, context_data) parameter.getUserData()['key_index'] = choice # because setParameter overwrites this value self.plot_data_fetcher.fetchContent() self.savePlot(self.plot_data_fetcher.data) else: self.plot_data_fetcher.setParameter(parameter, context_data) self.plot_data_fetcher.fetchContent() self.savePlot(self.plot_data_fetcher.data) self.popup.ok_button.setEnabled(True) def save(self, plot_data): self.popup.show() self.popup.progress_bar.setMaximum(1) self.savePlot(plot_data) self.popup.ok_button.setEnabled(True) def savePlot(self, plot_data): generated_plot = self.generatePlot(plot_data) if generated_plot: self.savePlotToFile(plot_data.getSaveName()) self.popup.emit(SIGNAL('updateProgress(int)'), 1) def generatePlot(self, plot_data): name = plot_data.getSaveName() load_success = self.plot_config_loader.load(name, self.plot_settings) if load_success: self.plot_figure.drawPlot(plot_data, self.plot_settings) self.canvas.draw() return load_success def savePlotToFile(self, filename): """Save the plot visible in the figure.""" plot_path = self.plot_settings.getPlotPath() if not os.path.exists(plot_path): os.makedirs(plot_path) path = plot_path + "/" + filename self.plot_figure.getFigure().savefig(path + ".png", dpi=400, format="png") self.plot_figure.getFigure().savefig(path + ".pdf", dpi=400, format="pdf")
class PlotView(QFrame): """PlotView presents a matplotlib canvas with interaction possibilities. (picking and tooltip)""" def __init__(self): """Create a PlotView instance""" QFrame.__init__(self) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # setup some default data values self.data = PlotData() self.data.x_data_type = "number" self.data.setValid(False) self.plot_figure = PlotFigure() self.plot_settings = PlotSettings() self.canvas = FigureCanvas(self.plot_figure.getFigure()) self.canvas.setParent(self) self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.mouse_handler = MouseHandler(self) def toggleMember(self, line): gid = int(line.get_gid()) if gid in self.plot_settings.getSelectedMembers(): self.plot_settings.unselectMember(gid) else: self.plot_settings.selectMember(gid) @ert_gui.widgets.util.may_take_a_long_time def drawPlot(self): self.plot_figure.drawPlot(self.data, self.plot_settings) self.canvas.draw() def resizeEvent(self, event): QFrame.resizeEvent(self, event) self.canvas.resize(event.size().width(), event.size().height()) def loadSettings(self, name): if self.data.isValid(): plot_config_loader = PlotSettingsLoader() if not plot_config_loader.load(name, self.plot_settings): self.drawPlot() def saveSettings(self): if self.data.isValid(): plot_config_saver = PlotSettingsSaver() plot_config_saver.save(self.data.getSaveName(), self.plot_settings) def setData(self, data): self.saveSettings() self.data = data self.loadSettings(self.data.getSaveName()) def setXZoomFactors(self, xminf, xmaxf): self.plot_settings.setMinXZoom(xminf) self.plot_settings.setMaxXZoom(xmaxf) def setYZoomFactors(self, yminf, ymaxf): self.plot_settings.setMinYZoom(yminf) self.plot_settings.setMaxYZoom(ymaxf) def save(self): self.saveSettings() plot_generator = PlotGenerator(self.plot_settings.getPlotPath(), self.plot_settings.getPlotConfigPath()) plot_generator.save(self.data) def saveAll(self): self.saveSettings() plot_generator = PlotGenerator(self.plot_settings.getPlotPath(), self.plot_settings.getPlotConfigPath()) plot_generator.saveAll() def copyPlotSettings(self): plot_config_loader = PlotSettingsLoader() plot_config_loader.copy(self.plot_settings) def setPlotPath(self, plot_path): self.plot_settings.setPlotPath(plot_path) def setPlotConfigPath(self, path): self.plot_settings.setPlotConfigPath(path) def _selectedMemberIdentifier(self, artist): return artist.get_gid() in self.plot_settings.getSelectedMembers() def clearSelection(self): selected_lines = self.plot_figure.fig.findobj( self._selectedMemberIdentifier) for line in selected_lines: self.plot_settings.unselectMember(line.get_gid()) def displayToolTip(self, event): if not self.data is None and not event.xdata is None and not event.ydata is None: if self.data.getXDataType() == "time": date = matplotlib.dates.num2date(event.xdata) self.setToolTip("x: %s y: %04f" % (date.strftime("%d/%m-%Y"), event.ydata)) else: self.setToolTip("x: %04f y: %04f" % (event.xdata, event.ydata)) else: self.setToolTip("") def annotate(self, label, x, y, xt=None, yt=None): self.plot_settings.addAnnotation(label, x, y, xt, yt) def removeAnnotation(self, annotation_artist): annotations = self.plot_settings.getAnnotations() for annotation in annotations: if annotation.getUserData() == annotation_artist: self.plot_settings.removeAnnotation(annotation) def moveAnnotation(self, annotation_artist, xt, yt): annotations = self.plot_settings.getAnnotations() for annotation in annotations: if annotation.getUserData() == annotation_artist: annotation.xt = xt annotation.yt = yt annotation_artist.xytext = (xt, yt) def draw(self): self.canvas.draw() def setMinYLimit(self, value): self.plot_settings.setMinYLimit(value) def setMaxYLimit(self, value): self.plot_settings.setMaxYLimit(value) def setMinXLimit(self, value): self.plot_settings.setMinXLimit(value) def setMaxXLimit(self, value): self.plot_settings.setMaxXLimit(value) def getPlotConfigList(self): return self.plot_settings.getPlotConfigList()