From b260831ce49f40b1c4d7464f880be1e47e8693e0 Mon Sep 17 00:00:00 2001 From: NicoRenaud Date: Wed, 22 Apr 2020 17:12:04 +0200 Subject: [PATCH] fix inline --- h5xplorer/menu_plot.py | 55 ++++++++++++++++++++++------------------- h5xplorer/menu_tools.py | 54 ++++++++++++++++++++++++---------------- 2 files changed, 62 insertions(+), 47 deletions(-) diff --git a/h5xplorer/menu_plot.py b/h5xplorer/menu_plot.py index 5596d7b..11fca59 100644 --- a/h5xplorer/menu_plot.py +++ b/h5xplorer/menu_plot.py @@ -3,7 +3,7 @@ from h5xplorer.menu_tools import * -def plot_histogram(self,item,treeview,nbins=10): +def plot_histogram(self, item, treeview, nbins=10): """Plot an histogram of the data Example: @@ -22,21 +22,22 @@ def plot_histogram(self,item,treeview,nbins=10): nbins (int, optional): number of bins in the histogram """ - grp = get_current_hdf5_group(self,item) + grp = get_current_hdf5_group(self, item) value = get_group_data(grp) value = value.flatten() if value is not None: - data_dict = {'_value':value} + data_dict = {'_value': value} treeview.emitDict.emit(data_dict) - cmd = "%matplotlib inline\n" - cmd += "import matplotlib.pyplot as plt\nplt.hist(_value,%s)\nplt.show()\n" %nbins - data_dict = {'exec_cmd':cmd} + # cmd = "%matplotlib inline\n" + cmd = "import matplotlib.pyplot as plt\nplt.hist(_value,%s)\nplt.show()\n" % nbins + data_dict = {'exec_cmd': cmd} treeview.emitDict.emit(data_dict) -def plot_line(self,item,treeview): + +def plot_line(self, item, treeview): """Plot a line plot of a single item VS its index Example: @@ -54,19 +55,20 @@ def plot_line(self,item,treeview): treeview (HDF5TreeWidget): treeview """ - grp = get_current_hdf5_group(self,item) + grp = get_current_hdf5_group(self, item) value = get_group_data(grp) if value is not None: - data_dict = {'_value':value} + data_dict = {'_value': value} treeview.emitDict.emit(data_dict) - cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.plot(_value)\nplt.show()\n" - data_dict = {'exec_cmd':cmd} + # cmd = "%matplotlib inline" + cmd = "import matplotlib.pyplot as plt\nplt.plot(_value)\nplt.show()\n" + data_dict = {'exec_cmd': cmd} treeview.emitDict.emit(data_dict) -def plot1D(self,item0,item1,treeview,plot='line'): +def plot1D(self, item0, item1, treeview, plot='line'): """Plot a XY line or scatter plot of two items Note: You must be able to select multiple items to use this method. @@ -92,8 +94,8 @@ def plot1D(self,item0,item1,treeview,plot='line'): plot (str, optional): 'line' or 'scatter' """ - grp0 = get_current_hdf5_group(self,item0) - grp1 = get_current_hdf5_group(self,item1) + grp0 = get_current_hdf5_group(self, item0) + grp1 = get_current_hdf5_group(self, item1) x = get_group_data(grp0) y = get_group_data(grp1) @@ -101,15 +103,16 @@ def plot1D(self,item0,item1,treeview,plot='line'): xlabel = item0.basename ylabel = item1.basename - if x.ndim != 1 or y.ndim != 1 or x.shape!=y.shape: + if x.ndim != 1 or y.ndim != 1 or x.shape != y.shape: #show_error_message('Size Incompatible') return - data_dict = {'_x':x,'_y':y} + data_dict = {'_x': x, '_y': y} treeview.emitDict.emit(data_dict) data_dict = {} - cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n" + # cmd = "%matplotlib inline" + cmd = "import matplotlib.pyplot as plt\n" cmd += "fig,ax = plt.subplots()\n" if plot == 'scatter': @@ -117,13 +120,14 @@ def plot1D(self,item0,item1,treeview,plot='line'): elif plot == 'line': cmd += "ax.plot(_x,_y)\n" - cmd += "ax.set_xlabel('%s')\n" %xlabel - cmd += "ax.set_ylabel('%s')\n" %ylabel + cmd += "ax.set_xlabel('%s')\n" % xlabel + cmd += "ax.set_ylabel('%s')\n" % ylabel cmd += "plt.show()\n" data_dict['exec_cmd'] = cmd treeview.emitDict.emit(data_dict) -def plot2d(self,item,treeview): + +def plot2d(self, item, treeview): """Plot a map of a 2D data array Example: @@ -140,20 +144,19 @@ def plot2d(self,item,treeview): item (HDF5TreeItem): treeview item treeview (HDF5TreeWidget): treeview """ - - - grp = get_current_hdf5_group(self,item) + grp = get_current_hdf5_group(self, item) data = get_group_data(grp) if data.ndim != 2: return - data_dict = {'_map':data} + data_dict = {'_map': data} treeview.emitDict.emit(data_dict) data_dict = {} - cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n" + # cmd = "%matplotlib inline" + cmd = "import matplotlib.pyplot as plt\n" cmd += "plt.imshow(_map)\n" cmd += "plt.show()\n" data_dict['exec_cmd'] = cmd - treeview.emitDict.emit(data_dict) \ No newline at end of file + treeview.emitDict.emit(data_dict) diff --git a/h5xplorer/menu_tools.py b/h5xplorer/menu_tools.py index 4b51063..7a184a6 100644 --- a/h5xplorer/menu_tools.py +++ b/h5xplorer/menu_tools.py @@ -1,7 +1,8 @@ from PyQt5 import QtWidgets from h5xplorer.standarddialogs import * -def get_current_item(self,treeview,single=False): + +def get_current_item(self, treeview, single=False): """Get the item(s) that was selected Args: @@ -11,15 +12,17 @@ def get_current_item(self,treeview,single=False): Returns: TYPE: Description """ - items = [self._index_to_item(index) for index in treeview.selectedIndexes()] + items = [self._index_to_item(index) + for index in treeview.selectedIndexes()] if single: - if len(items)!=1: + if len(items) != 1: return items = items[0] return items -def get_current_hdf5_group(self,item): + +def get_current_hdf5_group(self, item): """Get the HDF5 group of the selected item Args: @@ -30,6 +33,7 @@ def get_current_hdf5_group(self,item): """ return self.root_item.data_file[item.name] + def get_group_data(group): """Get the dataset of the item @@ -40,11 +44,12 @@ def get_group_data(group): dataset or None: return np.array if the group has a dataset or None otherwise """ try: - return group.value + return group[()] except Exception as ex: return None -def get_actions(treeview,position,list_action): + +def get_actions(treeview, position, list_action): """Generate a singlelevel context menu of action and return the selected one Example: @@ -71,10 +76,12 @@ def get_actions(treeview,position,list_action): menu.addSeparator() else: actions[operation] = menu.addAction(operation) - selected_action = menu.exec_(treeview.viewport().mapToGlobal(position)) - return selected_action,actions + selected_action = menu.exec_( + treeview.viewport().mapToGlobal(position)) + return selected_action, actions -def get_multilevel_actions(treeview,position,list_action,sub_list): + +def get_multilevel_actions(treeview, position, list_action, sub_list): """Generate a multilevel context menu of action and return the selected one Example: @@ -96,36 +103,40 @@ def get_multilevel_actions(treeview,position,list_action,sub_list): menu = QtWidgets.QMenu() actions = {} - for iop,operation in enumerate(list_action): + for iop, operation in enumerate(list_action): if len(sub_list[iop]) == 0: actions[operation] = menu.addAction(operation) else: sub_menu = menu.addMenu(operation) for subop in sub_list[iop]: - actions[(operation,subop)] = sub_menu.addAction(subop) + actions[(operation, subop) + ] = sub_menu.addAction(subop) + + selected_action = menu.exec_( + treeview.viewport().mapToGlobal(position)) + return selected_action, actions - selected_action = menu.exec_(treeview.viewport().mapToGlobal(position)) - return selected_action,actions -def send_dict_to_console(self,item,treeview): +def send_dict_to_console(self, item, treeview): """Send a dictionany to the QT console Args: item (HDF5TreeItem): treeview item treeview (HDF5TreeWidget): the treeview """ - grp = get_current_hdf5_group(self,item) - data = {'attrs':list(grp.attrs)} + grp = get_current_hdf5_group(self, item) + data = {'attrs': list(grp.attrs)} treeview.emitDict.emit(data) -def print_attributes(self,item,treeview): + +def print_attributes(self, item, treeview): """Print the attribute in the console Args: item (HDF5TreeItem): treeview item treeview (HDF5TreeWidget): the treeview """ - grp = get_current_hdf5_group(self,item) + grp = get_current_hdf5_group(self, item) attrs = list(grp.attrs) val = list(grp.attrs.values()) print(attrs) @@ -135,7 +146,8 @@ def print_attributes(self,item,treeview): data[a] = 0 treeview.emitDict.emit(data) -def get_user_values(varnames,vartypes='float',windowtitle='Enter Values'): + +def get_user_values(varnames, vartypes='float', windowtitle='Enter Values'): """Get the values of variables from the users Args: @@ -145,6 +157,6 @@ def get_user_values(varnames,vartypes='float',windowtitle='Enter Values'): Returns: list: list of float of the desired variables """ - dialog = Dialog(varnames,vartypes=vartypes) + dialog = Dialog(varnames, vartypes=vartypes) dialog.exec_() - return dialog.returnValues() \ No newline at end of file + return dialog.returnValues()