Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
update sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsmia1939 committed Nov 11, 2022
1 parent d8761c9 commit 44c65b7
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 146 deletions.
144 changes: 50 additions & 94 deletions PySimpleCV/PySimpleCV
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,9 @@ from matplotlib.figure import Figure
import PySimpleGUI as sg
import matplotlib
import pandas as pd
matplotlib.use('TkAgg')

from PySimpleCV_main_func import CV_file2df, get_CV, battery_xls2df, find_seg_start_end

def get_CV_init(CV_file):
df = CV_file2df(CV_file)
cv_size = df.shape[0]
volt = df[:,0]
current = df[:,1]
return cv_size, volt, current

def get_Battery_init(bat_file):
df, row_size, time_df, volt_df, current_df, capacity_df, state_df = battery_xls2df(bat_file)
return df, row_size, time_df, volt_df, current_df, capacity_df, state_df

def find_state_seg(state_df):
charge_seq = find_seg_start_end(state_df,'C_CC')
discharge_seq = find_seg_start_end(state_df,'D_CC')
rest_seq = find_seg_start_end(state_df,'R')
return charge_seq, discharge_seq, rest_seq

def cy_idx_state_end(state_df, cycle_start, cycle_end, charge_seq, discharge_seq):
cycle_index = np.stack((charge_seq[cycle_start-1:cycle_end], discharge_seq[cycle_start-1:cycle_end])) #no need to include rest
cycle_idx_start = np.amin(cycle_index)
cycle_idx_end = np.amax(cycle_index)+1
return cycle_idx_start, cycle_idx_end

def get_bat_eff(df, row_size, time_df, volt_df, current_df, capacity_df, state_df, cycle_start, cycle_end, charge_seq, discharge_seq):
# Calculate the area of charge and discharge cycle and find VE for each cycle
VE_lst = []
CE_lst = []
for i in range(cycle_start-1,cycle_end):
time_seq_C_CC = time_df[charge_seq[i][0]:charge_seq[i][1]+1]
volt_seq_C_CC = volt_df[charge_seq[i][0]:charge_seq[i][1]+1]
current_seq_C_CC = current_df[charge_seq[i][0]:charge_seq[i][1]+1]
time_seq_D_CC = time_df[discharge_seq[i][0]:discharge_seq[i][1]+1]
volt_seq_D_CC = volt_df[discharge_seq[i][0]:discharge_seq[i][1]+1]
current_seq_D_CC = current_df[discharge_seq[i][0]:discharge_seq[i][1]+1]
int_vt_C = np.trapz(volt_seq_C_CC,time_seq_C_CC)
int_vt_D = np.trapz(volt_seq_D_CC,time_seq_D_CC)
int_ct_C = np.trapz(current_seq_C_CC,time_seq_C_CC)
# During discharge, current is negative, must make to positive
int_ct_D = -(np.trapz(current_seq_D_CC,time_seq_D_CC))
VE = int_vt_D/int_vt_C
CE = int_ct_D/int_ct_C
VE_lst.append(VE)
CE_lst.append(CE)
VE_arr = np.array(VE_lst)
CE_arr = np.array(CE_lst)
return VE_arr, CE_arr
matplotlib.use('TkAgg')
from PySimpleCV_main_func import get_CV, battery_xls2df, get_CV_init, find_state_seq, get_battery_eff, cy_idx_state_range, CV_file2df

def draw_figure(canvas, figure):
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
Expand Down Expand Up @@ -92,7 +45,7 @@ bat_layout = [
[sg.Text('Voltage, current, energy efficiency for each row of cycle(%), press ctrl+c to copy')],
[sg.Text('To copy to spreadsheet, select space as delimiter and merge delimters.')],
[sg.Multiline('',size=(50,5), disabled=True, key = 'output_arr')],
[sg.Text('Cycle start'), sg.Slider(range=(1, 1), size=(60, 10), orientation='h', key='cycle_start', enable_events=True, disabled=False)],
[sg.Text('Cycle start'), sg.Slider(range=(0, 1), size=(60, 10), orientation='h', key='cycle_start', enable_events=True, disabled=False)],
[sg.Text('Cycle end'), sg.Slider(range=(1, 1), size=(60, 10), orientation='h', key='cycle_end', enable_events=True, disabled=False)],
[sg.Button('Clear plot',key='bat_clear'),sg.Button('Exit', key='exit_2')]
]
Expand Down Expand Up @@ -144,26 +97,27 @@ while True:
continue
elif CV_file_new == '':
continue
try:
CV_file = CV_file_new
cv_size, volt, current = get_CV_init(CV_file) # Only need the cv_size so set to [0]
window['sl_cut_val'].Update(range=(0,cv_size))
window['sl_jpa_lns'].Update(range=(0,cv_size))
window['sl_jpa_lne'].Update(range=(0,cv_size))
window['sl_jpc_lns'].Update(range=(0,cv_size))
window['sl_jpc_lne'].Update(range=(0,cv_size))
window['sl_cut_val'].Update(disabled=False)
window['sl_jpa_lns'].Update(disabled=False)
window['sl_jpa_lne'].Update(disabled=False)
window['sl_jpc_lns'].Update(disabled=False)
window['sl_jpc_lne'].Update(disabled=False)
window['CV_file_use'].Update(CV_file)
ax_cv.grid()
ax_cv.plot(volt, current, '-')
ax_cv.grid()
fig_agg_cv.draw()
except Exception as file_error:
sg.popup(file_error, keep_on_top=True)
# try:
CV_file = CV_file_new
df_CV = CV_file2df(CV_file)
cv_size, volt, current = get_CV_init(df_CV) # Only need the cv_size so set to [0]
window['sl_cut_val'].Update(range=(0,cv_size))
window['sl_jpa_lns'].Update(range=(0,cv_size))
window['sl_jpa_lne'].Update(range=(0,cv_size))
window['sl_jpc_lns'].Update(range=(0,cv_size))
window['sl_jpc_lne'].Update(range=(0,cv_size))
window['sl_cut_val'].Update(disabled=False)
window['sl_jpa_lns'].Update(disabled=False)
window['sl_jpa_lne'].Update(disabled=False)
window['sl_jpc_lns'].Update(disabled=False)
window['sl_jpc_lne'].Update(disabled=False)
window['CV_file_use'].Update(CV_file)
ax_cv.grid()
ax_cv.plot(volt, current, '-')
ax_cv.grid()
fig_agg_cv.draw()
# except Exception as file_error:
# sg.popup(file_error, keep_on_top=True)
case 'sl_cut_val' | 'sl_jpa_lns' | 'sl_jpa_lne' | 'sl_jpc_lns' | 'sl_jpc_lne' :
cut_val = int(values['sl_cut_val']) # Getting the k value from the slider element.
jpa_lns = int(values['sl_jpa_lns'])
Expand All @@ -173,7 +127,7 @@ while True:
ax_cv.cla()
ax_cv.grid()
# Start plotting
volt, current, jpa_ref_ln, jpa_ref, idx_jpa_max, jpa_abs, jpa_base, jpc_ref_ln, jpc_ref, idx_jpc_min, jpc_abs, jpc_base, jpa_lns, jpa_lne, jpc_lns, jpc_lne, jpa, jpc = get_CV(CV_file,cut_val,jpa_lns,jpa_lne,jpc_lns,jpc_lne)
volt, current, jpa_ref_ln, jpa_ref, idx_jpa_max, jpa_abs, jpa_base, jpc_ref_ln, jpc_ref, idx_jpc_min, jpc_abs, jpc_base, jpa_lns, jpa_lne, jpc_lns, jpc_lne, jpa, jpc = get_CV(df_CV,cut_val,jpa_lns,jpa_lne,jpc_lns,jpc_lne)
ax_cv.plot(volt, current)
ax_cv.set_xlabel("Voltage")
ax_cv.set_ylabel("Current")
Expand Down Expand Up @@ -206,29 +160,30 @@ while True:
continue
try:
bat_file = bat_file_new
df, row_size, time_df, volt_df, current_df, capacity_df, state_df = battery_xls2df(bat_file)
df_bat, row_size, time_df, volt_df, current_df, capacity_df, state_df = battery_xls2df(bat_file)
# Sequence information
charge_seq, discharge_seq, rest_seq = find_state_seg(state_df)
charge_seq, discharge_seq, rest_seq = find_state_seq(state_df)
tot_cycle_number = np.shape(charge_seq)[0]
VE_arr, CE_arr = get_bat_eff(df, row_size, time_df, volt_df, current_df, capacity_df, state_df, 1, tot_cycle_number, charge_seq, discharge_seq)
EE_arr = VE_arr * CE_arr
VE_arr, CE_arr, EE_arr = get_battery_eff(row_size, time_df, volt_df, current_df, capacity_df, state_df, 1, tot_cycle_number, charge_seq, discharge_seq)
window['cycle_end'].Update(disabled=False)
window['cycle_start'].Update(disabled=False)
window['cycle_start'].Update(range=(1,tot_cycle_number))
window['cycle_start'].Update(range=(0,tot_cycle_number-1))
window['cycle_end'].Update(range=(1,tot_cycle_number))
window['bat_file_use'].Update(bat_file)
VE_avg = np.average(VE_arr)
CE_avg = np.average(CE_arr)
EE_avg = VE_avg * CE_avg
df_display=pd.DataFrame([VE_arr*100,CE_arr*100,EE_arr*100]) # Create dataframe to display in multiline output
df_display=pd.DataFrame([VE_arr,CE_arr,EE_arr]) # Create dataframe to display in multiline output
df_display=df_display.T
df_display.columns = ['Volt', 'Current', 'Energy']
df_display.index = df_display.index + 1 # Start index at 1 for nice looking
window['output_arr'].Update(df_display.to_string()) # Make sure to print all lines
window['tot_cycle'].Update(tot_cycle_number)
window['output_ve'].Update(np.round(VE_avg*100,3))
window['output_ce'].Update(np.round(CE_avg*100,3))
window['output_ee'].Update(np.round(EE_avg*100,3))

VE_avg = np.average(VE_arr[0:cycle_end])
CE_avg = np.average(CE_arr[0:cycle_end])
EE_avg = np.average(EE_arr[0:cycle_end])

window['output_ve'].Update(np.round(VE_avg,3))
window['output_ce'].Update(np.round(CE_avg,3))
window['output_ee'].Update(np.round(EE_avg,3))
ax_bat_volt.plot(time_df, volt_df, '-',color='blue')
ax_bat_current.plot(time_df, current_df, '--',color='red')
ax_bat_volt.grid()
Expand All @@ -250,16 +205,17 @@ while True:
ax_bat_volt.cla()
ax_bat_volt.grid()
# Start plotting
df, row_size, time_df, volt_df, current_df, capacity_df, state_df = battery_xls2df(bat_file)
charge_seq, discharge_seq, rest_seq = find_state_seg(state_df)
cycle_idx_start, cycle_idx_end = cy_idx_state_end(state_df, cycle_start, cycle_end, charge_seq, discharge_seq)
VE_avg = np.average(VE_arr[cycle_start-1:cycle_end])
CE_avg = np.average(CE_arr[cycle_start-1:cycle_end])
EE_avg = VE_avg * CE_avg
df_bat, row_size, time_df, volt_df, current_df, capacity_df, state_df = battery_xls2df(bat_file)
charge_seq, discharge_seq, rest_seq = find_state_seq(state_df)
cycle_idx_range = cy_idx_state_range(state_df, cycle_start, cycle_end, charge_seq, discharge_seq)

VE_avg = np.average(VE_arr[cycle_start:cycle_end])
CE_avg = np.average(CE_arr[cycle_start:cycle_end])
EE_avg = np.average(EE_arr[cycle_start:cycle_end])

ax_bat_volt.plot(time_df, volt_df, '-', color='blue')
left_bound = time_df[cycle_idx_start:cycle_idx_end][0]
right_bound = time_df[cycle_idx_start:cycle_idx_end][-1]
left_bound = time_df[cycle_idx_range][0]
right_bound = time_df[cycle_idx_range][1]
ax_bat_volt.set_xlim(left=left_bound,right=right_bound)
ax_bat_volt.set_xlabel("Time")
ax_bat_volt.set_ylabel("Voltage")
Expand All @@ -268,8 +224,8 @@ while True:
ax_bat_current.plot(time_df, current_df,'--',color='red')
ax_bat_current.set_ylabel("Current")

window['output_ve'].Update(np.round(VE_avg*100,3))
window['output_ce'].Update(np.round(CE_avg*100,3))
window['output_ee'].Update(np.round(EE_avg*100,3))
window['output_ve'].Update(np.round(VE_avg,3))
window['output_ce'].Update(np.round(CE_avg,3))
window['output_ee'].Update(np.round(EE_avg,3))
fig_agg_bat.draw()
window.close()
Loading

0 comments on commit 44c65b7

Please sign in to comment.