def save(self): """ Show a dialog for saving the figure. Returns whether the figure was successfully saved. """ # Adapted from ``matplotlib.backends.backend_qt5`` canvas = self.figure.canvas filetypes = canvas.get_supported_filetypes_grouped() default_filetype = canvas.get_default_filetype() name_filters, selected_name_filter = file_type_filters( filetypes, default=default_filetype) filename = FileDialogEx.get_save_file_name( parent=self, name_filters=name_filters, selected_name_filter=selected_name_filter) if filename: try: canvas.print_figure(filename) except Exception as exc: warning(parent=self.parent, title='Save Error', text='Error saving figure', content=str(exc)) else: return True return False
def save(self): """ Show a dialog for saving the figure. Return whether the figure was successfully saved. """ name_filters, selected_name_filter = file_type_filters( self.save_formats, default=self.default_save_format) filename = FileDialogEx.get_save_file_name( parent=self, name_filters=name_filters, selected_name_filter=selected_name_filter) if filename: gc = PlotGraphicsContext((int(self.component.outer_width), int(self.component.outer_height))) self.component.draw(gc, mode="normal") try: gc.save(filename) except Exception as exc: warning(parent=self.parent, title='Save Error', text='Error saving figure', content=str(exc)) else: return True return False
def handle_error(self, msg, detail): """ Report a run error to the user. """ warning(parent=self.parent, title='Run error', text='Run error', content=msg, details='<pre>%s</pre>' % details_escape(detail))
def update_entity_plot_variables(self, widget, variables): """ Set new variables for an entity-level plot. """ # Determine the table to load from. sources = { var.source for var in variables } if len(sources) != 1: raise ValueError('Variables must come from exactly one source') source = sources.pop() if source == 'input': table = 'input' elif source in ('metric_value', 'metric_score', 'composite_score'): table = 'group_results' else: content = "There is no entity-level data with name '{name}'"\ .format(name = variables[0].name) warning(parent = self.window, title = 'No data', text = 'Data not available', content = content) return False # Load the data. columns = [ var.name for var in variables ] if widget.data_frame is not None: # Use existing groups, if possible. group_name = self.results.group_name groups = widget.data_frame[group_name].unique() else: groups = None try: df = self.load_entity_plot_data( table, columns=columns, pop=True, groups=groups ) except KeyError: content = 'This metric does not apply at the individual record '\ 'level and can only be used for group level plots.' warning(parent=self.window, title='Invalid data', text='Invalid data for plot', content=content) return False # Update the plot widget. if widget.validate_drop(df, columns): with widget.suppress_notifications(): widget.data_frame = df widget.data_columns = columns widget.data_info = dict(table = table) widget.reset_figure() return True return False
def save_file_to(self, path): """ Save the file to the specified path. """ try: with file(path, 'w') as f: self._save_file(f) except Exception as exc: warning(parent=self.window, title='Save error', text='Save error', content='An error occurred while saving the file.', details=details_escape(str(exc))) else: self.file_path = path self.file_dirty = False self.add_recent_file(path)
def load_file(self, path): """ Load a file from the specified path. """ try: with file(path, 'r') as f: self._load_file(f) except Exception as exc: warning(parent=self.window, title='Load error', text='Load error', content='An error occurred while loading the file.', details=details_escape(str(exc))) else: self.file_path = path self.file_dirty = False self.add_recent_file(path)
def _new_file(self): """ Open results data for inspection. """ clone = lambda x: x.clone_traits() if x is not None else None wizard = DataSourceWizard( input_source=clone(self.results.input_source), output_source=clone(self.results.output_source), mode='open') wizard.open() if wizard.return_code == OK: try: self.results = RunResults(input_source=wizard.input_source, output_source=wizard.output_source) self.session_file = '' except Exception as exc: warning(parent=self.window, title='Load error', text='Error loading results', content=str(exc))
def confirm_file_close(self): """ Confirm whether the current file can be closed. """ if self.file_dirty: button = warning( parent=self.window, title='Close file?', text='The current file has unsaved changes.', content='Do you want to save these changes?', buttons=[ DialogButton('Yes', 'accept'), DialogButton('No', 'accept'), DialogButton('Cancel', 'reject') ], ) return button and ((button.text == 'Yes' and self.save_file()) or button.text == 'No') else: return True