Skip to content

Commit

Permalink
fix inline
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoRenaud committed Apr 22, 2020
1 parent 3f3c8af commit b260831
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
55 changes: 29 additions & 26 deletions h5xplorer/menu_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -92,38 +94,40 @@ 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)

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':
cmd += "ax.scatter(_x,_y)\n"
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:
Expand All @@ -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)
treeview.emitDict.emit(data_dict)
54 changes: 33 additions & 21 deletions h5xplorer/menu_tools.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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()
return dialog.returnValues()

0 comments on commit b260831

Please sign in to comment.