Skip to content
Snippets Groups Projects
Commit 9f982b6f authored by antcsny's avatar antcsny
Browse files

Added dynamic variables checkbox for data plot

parent 5f831c3c
Branches
No related merge requests found
...@@ -53,7 +53,7 @@ class MainWindow (sg.Window): ...@@ -53,7 +53,7 @@ class MainWindow (sg.Window):
def def_file_browse(self, key:str): def def_file_browse(self, key:str):
data_path = os.path.dirname(os.path.realpath(__file__)) + "/data" data_path = os.path.dirname(os.path.realpath(__file__)) + "/data"
self._input_data = sg.Input(default_text=data_path, key=f'-path_{key}-', size=(130, 1), font=("Consolas", 10)) self._input_data = sg.Input(default_text=data_path, key=f'-path_{key}-', size=(67, 1), font=("Consolas", 10), enable_events=True)
self.import_xlsx = sg.FileBrowse("Browse EXCEL",initial_folder=data_path, file_types=(('Excel Files', '*.xlsx'),), key=f'-browse_{key}-') self.import_xlsx = sg.FileBrowse("Browse EXCEL",initial_folder=data_path, file_types=(('Excel Files', '*.xlsx'),), key=f'-browse_{key}-')
self.open_xlsx = sg.Button("OPEN", key=f'-open_{key}-') self.open_xlsx = sg.Button("OPEN", key=f'-open_{key}-')
return [self._input_data, self.import_xlsx, self.open_xlsx] return [self._input_data, self.import_xlsx, self.open_xlsx]
...@@ -134,5 +134,8 @@ while True: ...@@ -134,5 +134,8 @@ while True:
window.update_do() window.update_do()
if event == '-trace_selvar-': if event == '-trace_selvar-':
window.trace_selected_variables(1) window.trace_selected_variables(1)
if 'path' in event:
window[event].Widget.xview_moveto(1)
window.close() window.close()
\ No newline at end of file
...@@ -208,7 +208,8 @@ class MainProgram (MainWindow): ...@@ -208,7 +208,8 @@ class MainProgram (MainWindow):
""" """
try: try:
self.dataframe = pd.read_excel(path) self.dataframe = pd.read_excel(path)
self.dataframe['Sample_time_s'] = self.dataframe['Sample_time']/1000 self.data.update_layout_on_columns(self, self.dataframe.columns)
self.dataframe['Sample_time_s'] = self.dataframe['Sample']/1000 if 'TRACE' in path else self.dataframe['Sample_time']/1000
sg.popup("Done") sg.popup("Done")
self.data.enable_plot_buttons(True) self.data.enable_plot_buttons(True)
except Exception as e: except Exception as e:
...@@ -216,17 +217,12 @@ class MainProgram (MainWindow): ...@@ -216,17 +217,12 @@ class MainProgram (MainWindow):
self.data.enable_plot_buttons(False) self.data.enable_plot_buttons(False)
def trace_selected_variables (self): def trace_selected_variables (self):
if(self.data._do_tq): plt.close('all')
self.dataframe.plot(x="Sample_time_s",y=[f"Torque_A{i}" for i in range(1,7,1)], grid=True),plt.ylabel("Motor Torque (N.m)") for i, var in enumerate(self.data._var_totrace):
if(self.data._do_curr): if(self.data._do_trace_var[i]):
self.dataframe.plot(x="Sample_time_s",y=[f"Current_A{i}" for i in range(1,7,1)], grid=True),plt.ylabel("Motor Current (%)") self.dataframe.plot(x="Sample_time_s",y=[f"{var}_A{i}" for i in range(1,7,1)], grid=True),plt.ylabel(f"Motor {var}")
if(self.data._do_temp): plt.tight_layout()
self.dataframe.plot(x="Sample_time_s",y=[f"Temperature_A{i}" for i in range(1,7,1)], grid=True),plt.ylabel("Motor Temperature (°K)") plt.pause(0.1) # Alternative to plt.show() that is not blocking
if(self.data._do_posact):
self.dataframe.plot(x="Sample_time_s",y=[f"Position_Command_A{i}" for i in range(1,7,1)], grid=True),plt.ylabel("Robot command position in grid (mm))")
if(self.data._do_posreal):
self.dataframe.plot(x="Sample_time_s",y=[f"Position_A{i}" for i in range(1,7,1)], grid=True),plt.ylabel("Real Robot position in grid (mm))")
plt.pause(0.1) # Alternative to plt.show() that is not blocking
def trace_sampling (self): def trace_sampling (self):
self.dataframe.plot(x="Sample_time_s",y=["Queue_Read", "Queue_Write"], grid=True),plt.ylabel("Samples") self.dataframe.plot(x="Sample_time_s",y=["Queue_Read", "Queue_Write"], grid=True),plt.ylabel("Samples")
...@@ -272,7 +268,7 @@ class MainProgram (MainWindow): ...@@ -272,7 +268,7 @@ class MainProgram (MainWindow):
if event == '-doconst_speed-': if event == '-doconst_speed-':
self.collection_settings.update_speed_entry() self.collection_settings.update_speed_entry()
if '-do_' in event: if '-varplot_cbx' in event:
self.data.update_do() self.data.update_do()
## ---- Robot selector Events ---- ## ## ---- Robot selector Events ---- ##
...@@ -298,9 +294,12 @@ class MainProgram (MainWindow): ...@@ -298,9 +294,12 @@ class MainProgram (MainWindow):
self.gripper_close() self.gripper_close()
## ---- Trace Events ---- ## ## ---- Trace Events ---- ##
if 'path' in event:
self[event].Widget.xview_moveto(1)
if event == '-open_xlsx-': if '-open_data' in event :
self.open_xlsx(values['-data_path-']) nb = int(event.removeprefix("-open_data").removesuffix("-")) # get the identifier of the clicked button
self.open_xlsx(values[f'-path_data{nb}-'])
if event == '-trace_selvar-': if event == '-trace_selvar-':
self.trace_selected_variables() self.trace_selected_variables()
...@@ -311,7 +310,6 @@ class MainProgram (MainWindow): ...@@ -311,7 +310,6 @@ class MainProgram (MainWindow):
if event == '-trace_latency-': if event == '-trace_latency-':
self.trace_latencies() self.trace_latencies()
## ---- Other windows updates ---- ## ## ---- Other windows updates ---- ##
for i in range(len(self.window_latency)): for i in range(len(self.window_latency)):
......
import PySimpleGUI as sg import PySimpleGUI as sg
import pandas as pd
import os import os
class UI_Data (sg.Frame): class UI_Data (sg.Frame):
_disabled = False _disabled = False
_do_tq = True _var_totrace = []
_do_curr = True _do_trace_var = []
_do_temp = True
_do_posact = False
_do_posreal = False
def __init__ (self): def __init__ (self):
"""_summary_ : Class constructor """ : Class constructor
With __make_layout, generate a pysimplegui frame to be integrated in the main window for launching measuring sequence and data printing With __make_layout, generate a pysimplegui frame to be integrated in the main window for launching measuring sequence and data printing
sg elements like checkbox and buttons are stored in the class to be accessed easily sg elements like checkbox and buttons are stored in the class to be accessed easily
To print collection results from an excel file, select it with "Browse" and store it in python with "OPEN" To print collection results from an excel file, select it with "Browse" and store it in python with "OPEN"
...@@ -22,43 +20,38 @@ class UI_Data (sg.Frame): ...@@ -22,43 +20,38 @@ class UI_Data (sg.Frame):
super().__init__("Collected Data", self.__make_layout(), expand_x=True) super().__init__("Collected Data", self.__make_layout(), expand_x=True)
def __make_layout (self): def __make_layout (self):
data_path = os.path.dirname(os.path.realpath(__file__)) + "/data"
self._btn_start = sg.Button('START SEQUENCE', key='-BTN_start-', button_color='white', size=(15, 2),expand_x=True) self._btn_start = sg.Button('START SEQUENCE', key='-BTN_start-', button_color='white', size=(15, 2),expand_x=True)
self._input_data = sg.Input(default_text=data_path, key='-data_path-', size=(40, 1), font=("Consolas", 10)) self.import_data = self.__make_file_browse('1')
self.import_xlsx = sg.FileBrowse("Browse EXCEL",initial_folder=data_path, file_types=(('Excel Files', '*.xlsx'),), key='-browse_xlsx-')
self.open_xlsx = sg.Button("OPEN", key='-open_xlsx-')
self.trace_selvariables = sg.Button('Trace Selected variables', key='-trace_selvar-', disabled=True, expand_x=True) self.trace_selvariables = sg.Button('Trace Selected variables', key='-trace_selvar-', disabled=True, expand_x=True)
self.trace_samples = sg.Button('Trace Sample collection history', key='-trace_sample-', disabled=True, expand_x=True) self.trace_samples = sg.Button('Trace Sample collection history', key='-trace_sample-', disabled=True, expand_x=True)
self.trace_latency = sg.Button('Trace Sample latency distribution', key='-trace_latency-', disabled=True, expand_x=True) self.trace_latency = sg.Button('Trace Sample latency distribution', key='-trace_latency-', disabled=True, expand_x=True)
self.do_tq = sg.Checkbox('Motor torque', key='-do_tq-', size=(25, 1), enable_events=True, default=True) self.var_checkbox = []
self.do_curr = sg.Checkbox('Motor current', key='-do_curr-', size=(25, 1), enable_events=True, default=True)
self.do_tptr = sg.Checkbox('Motor temperature', key='-do_tprt-', size=(25, 1),enable_events=True, default=True)
self.do_posact = sg.Checkbox('Robot command position', key='-do_posact-', size=(25, 1), enable_events=True, default=False)
self.do_posreal = sg.Checkbox('Robot measured position', key='-do_posreal-', size=(25, 1), enable_events=True, default=False)
self._layout = [ self._layout = [
[ self._btn_start ], [ self._btn_start ],
[ sg.Frame("Quickview of the colelcted data in excel file :", border_width=0, expand_y=True, layout = [ [ sg.Frame("Quickview of the colelcted data in excel file :", border_width=0, expand_y=True, layout = [
[ sg.VPush() ], [ sg.VPush() ],
[ self._input_data, self.import_xlsx, self.open_xlsx ], [ self.import_data ],
[ self.trace_selvariables, self.trace_samples ], [ self.trace_selvariables, self.trace_samples ],
[ self.trace_latency ], [ self.trace_latency ],
[ sg.VPush() ] [ sg.VPush() ]
]), ]),
sg.Frame("Variables to plot :", border_width=0, layout=[ sg.Frame("Variables to plot :", layout=[ [sg.Col(layout=[], key='-col_var_to_plot-')] ])
[self.do_tq],
[self.do_curr],
[self.do_tptr],
[self.do_posact],
[self.do_posreal]
]),
], ],
] ]
return self._layout return self._layout
def __make_file_browse(self, key:str):
data_path = os.getcwd() + "/data"
self._input_data = sg.Input(default_text=data_path, key=f'-path_data{key}-', size=(50, 1), font=("Consolas", 10), enable_events=True)
self.import_xlsx = sg.FileBrowse("Browse EXCEL",initial_folder=data_path, file_types=(('Excel Files', '*.xlsx'),), key=f'-browse_data{key}-')
self.open_xlsx = sg.Button("OPEN", key=f'-open_data{key}-')
return [self._input_data, self.import_xlsx, self.open_xlsx]
@property @property
def disabled (self): # called when : print(disabled) def disabled (self): # called when : print(disabled)
return self._disabled return self._disabled
...@@ -72,7 +65,7 @@ class UI_Data (sg.Frame): ...@@ -72,7 +65,7 @@ class UI_Data (sg.Frame):
self._btn_start.update(disabled=v) self._btn_start.update(disabled=v)
def enable_plot_buttons(self, enable): def enable_plot_buttons(self, enable):
"""_summary_ """
Updates the state of the plot butons to prevent data plot if no data is already stored in python Updates the state of the plot butons to prevent data plot if no data is already stored in python
Args: Args:
enable (bool): True/False to enable/disable buttons enable (bool): True/False to enable/disable buttons
...@@ -82,11 +75,39 @@ class UI_Data (sg.Frame): ...@@ -82,11 +75,39 @@ class UI_Data (sg.Frame):
self.trace_latency.update(disabled=not(enable)) self.trace_latency.update(disabled=not(enable))
def update_do (self): def update_do (self):
"""_summary_
Update the state of the variables do_XXX if a checkbox is clicked
""" """
self._do_tq = bool(self.do_tq.get()) Update the state of the variable _do_trace_var if a checkbox is clicked
self._do_curr = bool(self.do_curr.get()) """
self._do_temp = bool(self.do_tptr.get()) for i, _ in enumerate(self._var_totrace):
self._do_posact = bool(self.do_posact.get()) self._do_trace_var[i] = bool(self.var_checkbox[i].get())
self._do_posreal = bool(self.do_posreal.get())
\ No newline at end of file def update_layout_on_columns(self, win: sg.Window, columns):
"""
Updates the window's layout to display new checkboxes
in order to plot or not the variables from a dataframe
Args:
win (sg.Window): window to modify the layout
columns (list): columns of the dataframe
Stored in class variable:
list: list of the collected variables on one robot axis
"""
for i, _ in enumerate(self._var_totrace): # reset before displaying
key = f'-varplot_line{i}-'
if key in win.key_dict:
win[key].Widget.destroy()
del win.AllKeysDict[key]
del win.AllKeysDict[f'-varplot_cbx{i}-']
win.read(100)
self._var_totrace = []
self._do_trace_var = []
self.var_checkbox = []
i=0
for col in columns:
if '_A1' in col: # sorting the variables per axis
colname = col.rsplit('_A1', 1)[0]
self._var_totrace.append(colname)
self.var_checkbox.append(sg.Checkbox('Motor ' + colname, enable_events=True, default=True, key=f'-varplot_cbx{i}-'))
win.extend_layout(win['-col_var_to_plot-'], [[sg.pin(sg.Col([[self.var_checkbox[i]]], key=f'-varplot_line{i}-', pad=(1,0.2)))]])
self._do_trace_var.append(True)
i+=1
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