-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
1572 lines (1381 loc) · 89.6 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import pandas as pd
import itertools
import math
import tkinter.ttk as ttk
import tkinter as tk
from tkinter import messagebox, filedialog
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import os
os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'
import uuid
import warnings
import json
# import copy
import sys
from simulator import *
import matplotlib
matplotlib.use('TkAgg') # Ensure using the Tkinter backend
import warnings
warnings.filterwarnings('ignore')
import subprocess
import platform
# Not yet implemented - window allowing users to add custom temperature profiles.
class TempProfileEditor(tk.Toplevel):
def __init__(self):
super().__init__()
ttk.Label(self, text="Profile Name: ").grid(row=0,column=0)
self.pname_entry = ttk.Entry(self)
self.pname_entry.grid(row=0,column=1)
self.var_frame = ttk.Frame(self)
self.var_frame.grid(row=1,column=0,columnspan=2)
ttk.Label(self.var_frame, text="Display Name").grid(row=0,column=0)
ttk.Label(self.var_frame, text="Variable Name").grid(row=0,column=1)
self.var_entries = []
self.add_variable()
ttk.Button(self, text="Add Variable", command = self.add_variable).grid(row=2,column=0)
ttk.Button(self, text="Autoname", command = self.autoname).grid(row=2,column=1)
ttk.Label(self, text="Enter Function Below").grid(row=3,column=0)
self.function_entry = ttk.Entry(self)
self.function_entry.grid(row=4,column=0, columnspan=2, sticky="NSEW")
ttk.Button(self, text="Save", command = self.save_profile).grid(row=5,column=0)
ttk.Button(self, text="Cancel", command = self.destroy).grid(row=5,column=1)
def remove_row(self, row_num):
for e in var_entries[row-1]:
e.destroy()
del var_entries[row-1]
def add_variable(self):
display_entry = ttk.Entry(self.var_frame)
cur_row = 1+len(self.var_entries)
display_entry.grid(row=cur_row, column=0)
var_entry = ttk.Entry(self.var_frame)
var_entry.grid(row=cur_row, column=1)
rmv_btn = ttk.Button(self.var_frame, text="X", command = lambda: self.remove_row(cur_row))
rmv_btn.grid(row=cur_row, column=2)
self.var_entries.append((display_entry, var_entry, rmv_btn))
def autoname(self):
for display_entry, var_entry, _ in self.var_entries:
display_name = display_entry.get()
var_name = display_name.lower()
if 0x30 <= ord(var_name[0]) <= 0x39:
var_name = "_"+var_name[1:]
for sc in [" ", "(",")","[","]",".","@","*","+","-","/","&","^","~","|","{","}","%"]:
var_name = var_name.replace(sc, "_")
var_entry.delete(0, tk.END)
var_entry.insert(0, var_name)
def save_profile(self):
pass
class TempProfileFrame(ttk.Frame):
def __init__(self,parent):
super().__init__(parent)
self.profile_options = {
"Constant": {
"vars": {
"temp": "Temperature (C)"
},
"func": "temp"
},
"Linear Ramp":{
"vars":{
"flash_time": "Flash Time (min)",
"start_temp": "Initial Temperature (C)",
"end_temp": "Final Temperature (C)",
"ramp_time": "Ramp Time (min)"
},
"func": "np.clip((t-flash_time)/ramp_time * (end_temp - start_temp) + start_temp, start_temp, end_temp)"
},
"Exponential Ramp": {
"vars":{
"flash_time": "Flash Time (min)",
"start_temp": "Initial Temperature (C)",
"end_temp": "Final Temperature (C)",
"ramp_time": "Ramp Time Constant (min)"
},
"func": "np.clip(end_temp - np.exp(-(t-flash_time)/ramp_time) * (end_temp - start_temp), start_temp, end_temp)"
}
}
ttk.Label(self, text="Temperature Profile").grid(row=0, column=0)
self.t_profile_selection = tk.StringVar(self)
self.t_profile_dropdown = ttk.OptionMenu(self, self.t_profile_selection,
list(self.profile_options.keys())[0],
*self.profile_options.keys(),
command=self.update_profile_frame)
self.t_profile_dropdown.grid(row=0, column=1)
self.profile_frame = ttk.Frame(self)
self.profile_frame.grid(row=1, column=0, columnspan=2)
self.update_profile_frame()
def update_profile_frame(self, *args):
for widget in self.profile_frame.winfo_children():
widget.destroy()
selected_profile = self.t_profile_selection.get()
self.profile_options[selected_profile]["entry_map"] = {}
for i, (var, label) in enumerate(self.profile_options[selected_profile]["vars"].items()):
ttk.Label(self.profile_frame, text=label).grid(row=i, column=0)
entry = ttk.Entry(self.profile_frame)
entry.grid(row=i, column=1)
self.profile_options[selected_profile]["entry_map"][var] = entry
def get_selected_profile(self):
selected_profile = self.t_profile_selection.get()
profile_params = {}
for var_label, entry in self.profile_options[selected_profile]["entry_map"].items():
try:
value = entry.get()
if not value:
raise ValueError(f"Empty value for {var_label}")
profile_params[var_label] = float(value)
except Exception as e:
print(f"Error processing {var_label}: {str(e)}")
return None, None
return selected_profile, profile_params
def get_temp_profile(self):
selected_profile = self.t_profile_selection.get()
profile_params = {}
try:
for var_label, entry in self.profile_options[selected_profile]["entry_map"].items():
value = entry.get()
if not value:
raise ValueError(f"Empty value for {var_label}")
profile_params[var_label] = float(value)
func_str = self.profile_options[selected_profile]["func"]
temp_func = lambda t: eval(func_str, globals(), {**profile_params, 't': t, 'np': np})
# Test the function
test_t = 0
test_result = temp_func(test_t)
if not isinstance(test_result, (int, float)):
raise ValueError(f"Function returned non-numeric result: {test_result}")
return temp_func
except Exception as e:
tk.messagebox.showwarning(title="Invalid Input", message=f"Error in temperature curve: {str(e)}")
return None
class TargetParamFrame(ttk.Frame):
"""
Frame for entering target resin parameters. Used on main & reformulate screens.
"""
def __init__(self, root):
super().__init__(root)
ttk.Label(self, text="Target Parameters", font=("Arial", 14)).grid(row=4, column=0, columnspan=2)
ttk.Label(self, text="δD").grid(row=5,column=0)
self.dD_entry = ttk.Entry(self)
self.dD_entry.grid(row=5, column=1)
ttk.Label(self, text="δP").grid(row=6,column=0)
self.dP_entry = ttk.Entry(self)
self.dP_entry.grid(row=6, column=1)
ttk.Label(self, text="δH").grid(row=7,column=0)
self.dH_entry = ttk.Entry(self)
self.dH_entry.grid(row=7, column=1)
ttk.Label(self, text="R").grid(row=8,column=0)
self.R_entry = ttk.Entry(self)
self.R_entry.grid(row=8, column=1)
ttk.Label(self, text="Wt% Solids").grid(row=9,column=0)
self.solids_entry = ttk.Entry(self)
self.solids_entry.grid(row=9, column=1)
def get_params(self):
try:
assert float(self.R_entry.get()) > 0 and 0 <= float(self.solids_entry.get()) <= 100
return {"dD":float(self.dD_entry.get()), "dP":float(self.dP_entry.get()), "dH":float(self.dH_entry.get()), "R0": float(self.R_entry.get()), "solids": float(self.solids_entry.get())}
except ValueError:
tk.messagebox.showwarning(title="Invalid Input", message="Please enter decimal numbers for target parameters (R > 0, 0 <= solids <= 100)")
return
def get_config(self):
return {"dD":self.dD_entry.get(), "dP":self.dP_entry.get(), "dH":self.dH_entry.get(), "R0": self.R_entry.get(), "solids": self.solids_entry.get()}
def set_config(self, config):
for entry, value in zip([self.dD_entry, self.dP_entry, self.dH_entry, self.R_entry, self.solids_entry], config.values()):
entry.delete(0, tk.END)
entry.insert(0, value)
class MainInputFrame(ttk.Frame):
"""
Handles all input for the main program screen.
"""
def __init__(self, root, graph, compare_input, compare_screen, reform_input, reform_screen):
super().__init__(root)
# Store the passed parameters as instance variables
# self.master = master
self.graph = graph
self.compare_input = compare_input
self.compare_screen = compare_screen
self.reform_input = reform_input
self.reform_input.set_main_input(self)
self.reform_screen = reform_screen
ttk.Label(self, text="Formulation", font=("Arial", 14)).grid(row=0, column=0, columnspan=2)
self.solvent_button = ttk.Button(self, text="Select Solvent Info File", command=self.select_solvent_file)
self.solvent_label = ttk.Label(self, text="")
self.solvent_fname = ""
self.solvent_button.grid(row=1, column=0)
self.solvent_label.grid(row=1, column=1)
self.formulation_button = ttk.Button(self, text="Select Formulation File", command=self.select_formulation_file)
self.formulation_label = ttk.Label(self, text="")
self.formulation_button.grid(row=2, column=0)
self.formulation_label.grid(row=2, column=1)
self.formulation_fname = ""
self.formulation_table = ttk.Treeview(self, columns=('chemical_name','conc'), show="headings")
self.formulation_table.heading('chemical_name', text='Chemical Name')
self.formulation_table.heading('conc', text='Weight Fraction')
self.formulation_table.grid(row=3, column=0, columnspan=2)
self.target_frame = TargetParamFrame(self)
self.target_frame.grid(row=4, column=0, columnspan=2, rowspan=5)
self.temp_frame = TempProfileFrame(self)
self.temp_frame.grid(row=9, column=0, columnspan=2, rowspan=3)
ttk.Label(self, text="Total Time (min): ").grid(row=12,column=0)
self.total_time = ttk.Entry(self)
self.total_time.grid(row=12,column=1)
self.big_style = ttk.Style()
self.big_style.configure("big.TButton", font=('Arial',14))
ttk.Button(self, text="Run Simulation", style='big.TButton', command=self.run_evap_predictor).grid(row=13,column=0,columnspan=2)
ttk.Button(self, text="Export to Excel", style='big.TButton', command=self.write_output).grid(row=14,column=0,columnspan=2)
ttk.Button(self, text="Compare Outputs", style='big.TButton', command=self.compare_screen.deiconify).grid(row=15,column=0,columnspan=2)
ttk.Button(self, text="Reformulate", style='big.TButton', command=self.reform_screen.deiconify).grid(row=16,column=0,columnspan=2)
ttk.Button(self, text="Load Config", style='big.TButton', command=self.read_config).grid(row=17,column=0)
ttk.Button(self, text="Save Config", style='big.TButton', command=self.write_config).grid(row=17,column=1)
self.all_solvents_df = None
# Initialize formulation_df as an empty DataFrame
self.formulation_df = pd.DataFrame()
def show_error_message(self, message):
root = tk.Tk()
root.withdraw() # Hide the main window
messagebox.showerror("Error", message)
root.destroy()
def select_formulation_file(self, fname=None):
if fname is None:
fname = tk.filedialog.askopenfilename(filetypes=[("Excel files (.xlsx)", "*.xlsx")])
self.formulation_fname = fname
try:
self.formulation_df = pd.read_excel(fname)
except Exception:
if fname == "":
return
tk.messagebox.showwarning(title="Could not Open", message=f"Could not open file {fname}, likely don't have read permission (often happens if the file is in onedrive)")
return
if "Name" not in self.formulation_df.columns or ("Volume Fraction" not in self.formulation_df.columns and "Weight Fraction" not in self.formulation_df.columns):
tk.messagebox.showwarning(title="Invalid Input", message="Formulation file missing either name column or both weight and volume fraction column")
self.focus_force()
self.formulation_fname = ""
self.formulation_df = None
else:
self.convert_to_weight = "Volume Fraction" not in self.formulation_df.columns
self.formulation_table.delete(*self.formulation_table.get_children()) # Clear previous data
try:
if self.convert_to_weight:
self.formulation_table.heading('conc', text='Weight Fraction')
for index, row in self.formulation_df.iterrows():
assert type(row["Weight Fraction"]) in [int, float]
self.formulation_table.insert('', tk.END, values=(row["Name"], row["Weight Fraction"]))
else:
self.formulation_table.heading('conc', text='Volume Fraction')
for index, row in self.formulation_df.iterrows():
assert type(row["Volume Fraction"]) in [int, float]
self.formulation_table.insert('', tk.END, values=(row["Name"], row["Volume Fraction"]))
except AssertionError:
tk.messagebox.showwarning(title="Invalid Input", message="Formulation file has invalid data")
self.focus_force()
self.formulation_fname = ""
self.formulation_df = None
return
self.formulation_label.configure(text=os.path.basename(fname))
def select_solvent_file(self, fname=None):
if fname is None:
fname = tk.filedialog.askopenfilename(filetypes=[("Excel files (.xlsx)", "*.xlsx")])
try:
self.all_solvents_df = pd.read_excel(fname, index_col="Solvents")
except Exception:
if fname == "":
return
tk.messagebox.showwarning(title="Could not Open", message=f"Could not open file {fname}, likely don't have read permission (often happens if the file is in onedrive)")
return
#Check file format
num_cols = ['δD', 'δP', 'δH', 'RER', 'AA', 'AB', 'AC', 'VP@25', 'Density']
bool_cols = ['Exempt', 'Multi-Component']
other_cols = ['Components', 'Amounts']
reqd_cols = num_cols + bool_cols + other_cols
if not all([x in self.all_solvents_df.columns for x in reqd_cols]):
tk.messagebox.showwarning(title="Invalid Input", message=f"Solvent info file missing required columns {' '.join([x for x in reqd_cols if x not in self.all_solvents_df.columns])}")
self.focus_force()
self.all_solvents_df = None
return
for col in num_cols:
try:
assert all([x == "-" or str(x).replace('.','',1).isdigit() for x in self.all_solvents_df[col]])
except Exception:
print([x for x in self.all_solvents_df[col]])
print([x == "-" or str(x).replace('.','',1).isdigit() for x in self.all_solvents_df[col]])
tk.messagebox.showwarning(title="Invalid Input", message=f"Bad data in column {col} of solvent info")
self.focus_force()
self.all_solvents_df = None
return
for col in bool_cols:
try:
assert all([type(x) == bool for x in self.all_solvents_df[col]])
except Exception:
tk.messagebox.showwarning(title="Invalid Input", message=f"Bad data in column {col} of solvent info")
self.focus_force()
self.all_solvents_df = None
return
self.solvent_fname = fname
self.solvent_label.configure(text=os.path.basename(fname))
self.reform_input.solvent_label.configure(text=os.path.basename(fname))
def run_evap_predictor(self):
"""
Processes inputs and sends them to :func:`get_evap_curve`"
"""
# Check if formulation_df is empty
if self.formulation_df.empty:
tk.messagebox.showwarning(title="No Formulation", message="Please load or enter a formulation before running the predictor.")
return
# Commented out portion below which was incorrectly flagging Volume Fraction as a KeyError missing solvent
'''try:
volume = self.formulation_df["Weight Fraction"] / np.array(self.all_solvents_df["Density"][self.formulation_df["Name"]])
except KeyError as e:
missing_solvents = str(e).strip("[]'")
error_message = f"The following solvents are not in the database: {missing_solvents}"
self.show_error_message(error_message)
return # Exit the method early '''
if "Volume Fraction" not in self.formulation_df.columns:
try:
volume = self.formulation_df["Weight Fraction"] / np.array(self.all_solvents_df["Density"][self.formulation_df["Name"]])
except KeyError as e:
missing_solvents = str(e).strip("[]'")
error_message = f"The following solvents are not in the database: {missing_solvents}"
self.show_error_message(error_message)
return # Exit the method early
self.formulation_df["Volume Fraction"] = volume / sum(volume)
try:
blend = [self.all_solvents_df.loc[name,:] for name in self.formulation_df["Name"]]
except KeyError:
tk.messagebox.showwarning(title="Invalid Input", message="Name mismatch between formulation and solvent database")
return
try:
conc = self.formulation_df["Volume Fraction"]/sum(self.formulation_df["Volume Fraction"])
assert not any(np.isnan(np.array(conc)))
except Exception:
tk.messagebox.showwarning(title="Invalid Input", message="Volume fractions could not be computed - were densities entered correctly?")
return
try:
t_span = [0, float(self.total_time.get())]
except ValueError:
tk.messagebox.showwarning(title="Invalid Input", message="Please enter a decimal number for total time")
return
target = self.target_frame.get_params()
if target is None:
return
print("Getting temperature profile...")
temp_curve = self.temp_frame.get_temp_profile()
if temp_curve is None:
print("Temperature profile is None")
return
print("Getting selected profile...")
# Get the selected temperature profile and its parameters
self.temp_profile, self.temp_params = self.temp_frame.get_selected_profile()
print(f"Selected profile: {self.temp_profile}")
print(f"Profile parameters: {self.temp_params}")
self.t, self.total_profile, self.partial_profiles, self.RED = get_evap_curve(conc, blend, target, temp_curve, t_span,
self.all_solvents_df, self.convert_to_weight)
self.temp = [temp_curve(ts) for ts in self.t]
self.graph.clear_artists()
self.graph.temp_ax.plot(self.t, self.temp)
self.graph.temp_ax.relim()
self.graph.RED_ax.plot(self.t, self.RED)
self.graph.RED_ax.relim()
for pp, name in zip(self.partial_profiles, self.formulation_df["Name"]):
self.graph.conc_ax.plot(self.t, pp, label = name)
self.graph.conc_ax.set_ylabel("Weight Fraction" if self.convert_to_weight else "Volume Fraction")
self.graph.conc_ax.plot(self.t, self.total_profile, label="Total")
self.graph.conc_ax.relim()
self.graph.conc_ax.legend(fontsize=6)
self.graph.canvas.draw()
def write_output(self):
fname = tk.filedialog.asksaveasfilename(defaultextension=".xlsx")
if not fname:
return # User cancelled the file dialog
target_params = self.target_frame.get_params()
if target_params is None:
return
temp_profile, temp_params = self.temp_frame.get_selected_profile()
# The commented out portion below seems useless since write_output is not used in the Reformulation Screen
'''if hasattr(self, 'selected_blend') and self.selected_blend:
# This is for the Reformulation Tool screen
blend = self.selected_blend
conc = self.selected_conc
replace_by = self.replace_by
caption = "Reformulation"
# Recalculate evaporation curve for the selected blend
t_span = [0, float(self.total_time.get())]
a_comp = [self.main_input.all_solvents_df.loc[name,:] for name in blend]
if hasattr(self, 'min_comp'):
a_comp += [self.main_input.all_solvents_df.loc[name,:] for name in self.min_comp["Name"]]
if replace_by == "Weight Fraction":
a_wf = np.array(list(conc) + list(self.min_comp["Weight Fraction"]))
a_c0 = a_wf * np.array(pd.DataFrame(a_comp)["Density"])
a_c0 /= sum(a_c0)
else:
a_c0 = np.array(list(conc) + list(self.min_comp["Volume Fraction"]))
else:
a_c0 = np.array(conc)
temp_curve = self.temp_frame.get_temp_profile()
t, total_profile, partial_profiles, RED = get_evap_curve(a_c0, a_comp, target_params, temp_curve, t_span, self.main_input.all_solvents_df, replace_by == "Weight Fraction")
temp = temp_curve(t)
if hasattr(self, 'min_comp'):
blend = blend + list(self.min_comp["Name"])
else:
# This is for the Evaporation Simulator Comparison Tool screen
blend = self.formulation_df["Name"]
t = self.t
total_profile = self.total_profile
partial_profiles = self.partial_profiles
RED = self.RED
temp = self.temp
#caption = self.formulation_fname
caption = ""'''
# This is for the Evaporation Simulator Comparison Tool screen
blend = self.formulation_df["Name"]
t = self.t
total_profile = self.total_profile
partial_profiles = self.partial_profiles
RED = self.RED
temp = self.temp
#caption = self.formulation_fname
print(f"self.formulation_df.colums: {self.formulation_df.columns}")
if "Weight Fraction" in self.formulation_df.columns:
caption = "Weight Fraction"
else:
caption = "Volume Fraction"
target_params['Total Time (min)'] = float(self.total_time.get())
target_params['Initial Temperature (C)'] = temp[0]
try:
write_to_excel(fname, blend, t, total_profile, partial_profiles, RED, temp,
target_params, temp_profile, temp_params, caption=caption)
except PermissionError:
tk.messagebox.showwarning(title="Error File Overwrite", message="Could not overwrite file. Try creating a new file name")
return
# # Open the file using the default application
# if sys.platform.startswith('darwin'): # macOS
# subprocess.call(('open', fname))
# elif sys.platform.startswith('linux'): # Linux
# subprocess.call(('xdg-open', fname))
# elif sys.platform.startswith('win'): # Windows
# os.startfile(fname)
# else:
# print(f"File saved at: {fname}")
os.startfile(fname)
tk.messagebox.showinfo("Export Successful", f"Data exported to {fname}")
def get_config(self):
return {"temp_frame": self.temp_frame.get_config(),
"target_frame": self.target_frame.get_config(),
"formulation_fname":self.formulation_fname,
"solvent_fname":self.solvent_fname,
"total_time": self.total_time.get(),
"compare_input": self.compare_input.get_config(),
"reform_input":self.reform_input.get_config()}
def set_config(self, config):
self.compare_input.set_config(config["compare_input"])
self.reform_input.set_config(config["reform_input"])
self.temp_frame.set_config(config["temp_frame"])
self.target_frame.set_config(config["target_frame"])
if config["formulation_fname"] != "":
self.select_formulation_file(config["formulation_fname"])
if config["solvent_fname"] != "":
self.select_solvent_file(config["solvent_fname"])
self.total_time.delete(0, tk.END)
self.total_time.insert(0, config["total_time"])
def write_config(self):
fname = tk.filedialog.asksaveasfilename(filetypes=[("JSON files (.json)", "*.json")])
if fname[-5:] != ".json":
fname += ".json"
with open(fname, "w") as f:
json.dump(self.get_config(), f)
def read_config(self):
fname = tk.filedialog.askopenfilename(filetypes=[("JSON files (.json)", "*.json")])
with open(fname) as f:
self.set_config(json.load(f))
class CompareInputFrame(ttk.Frame):
"""
Handles all input for the comparison screen
"""
def __init__(self, root, graph):
super().__init__(root)
self.graph = graph
self.files = set()
self.file_buttons = dict()
ttk.Button(self, text="Select Output Files", command=self.get_files).grid(row=0,column=0)
self.file_frame = ttk.Frame(self)
self.file_frame.grid(row=1,column=0)
self.file_frame_row = 0
self.big_style = ttk.Style()
self.big_style.configure("big.TButton", font=('Arial',14))
ttk.Label(self, text="Compare:").grid(row=2, column=0)
self.plot_total = tk.IntVar()
self.plot_partial = tk.IntVar()
self.plot_RED = tk.IntVar()
self.plot_temp = tk.IntVar()
ttk.Checkbutton(self, text="Total Profiles", variable=self.plot_total).grid(row=3, column=0, columnspan=2)
ttk.Checkbutton(self, text="Partial Profiles", variable=self.plot_partial).grid(row=4, column=0, columnspan=2)
ttk.Checkbutton(self, text="RED", variable=self.plot_RED).grid(row=5, column=0, columnspan=2)
ttk.Checkbutton(self, text="Temperature Profiles", variable=self.plot_temp).grid(row=6, column=0, columnspan=2)
ttk.Button(self, text="Compare", style="big.TButton", command=self.compare).grid(row=7, column=0, columnspan=2)
self.whitelist = []
self.blacklist = []
self.using_whitelist = False
def remove_file(self, fname):
for w in self.file_buttons[fname]:
w.grid_forget()
del self.file_buttons[fname]
self.files.remove(fname)
def add_files(self, new_full_files):
self.focus_force()
new_files = [os.path.basename(f) for f in new_full_files]
for base, full in zip(new_files, new_full_files):
if full not in self.files:
file_name = ttk.Label(self.file_frame, text=base)
file_name.grid(row=self.file_frame_row, column=0)
x_button = ttk.Button(self.file_frame, text="X", command=lambda f=full:self.remove_file(f))
x_button.grid(row=self.file_frame_row, column=1)
self.file_frame_row += 1
self.file_buttons[full] = [file_name, x_button]
self.files = self.files.union(new_full_files)
def get_files(self):
self.add_files(tk.filedialog.askopenfilenames())
def clear(self):
for fname in self.files:
for w in self.file_buttons[fname]:
w.grid_forget()
del self.file_buttons[fname]
self.files = set()
self.graph.clear_artists()
def delete_tmp(directory="./tmp"):
"""
Deletes tmp directory and closes the program
- Removing the tmp directory prevents unnecessary data storage
- Does not delete tmp when run from ui.py file. Only deletes tmp when run from .exe file in a folder without ui.py.
- Called at the end when program is closed
"""
compare_screen.destroy() # Destroy screens first to close all open files in program before deletion
reform_screen.destroy()
root.destroy()
if os.path.isfile("ui.py") is True: # Only delete tmp folder if run from the .exe program
print(f"Will not delete directory {directory} when run from Python interpreter")
return
if os.path.isdir(directory) is False:
print(f"No directory {directory} to delete")
return
try:
for sub_directory in os.listdir(directory): # Sort through tmp and delete files before folders
sub_directory = directory +"/"+ sub_directory
for file in os.listdir(sub_directory):
file = sub_directory +"/"+ file
os.remove(file)
os.rmdir(sub_directory)
os.rmdir(directory)
print(f"Deleted directory: {directory}")
except Exception as e:
print(e)
tk.messagebox.showinfo("Deletion unsuccessful", f"Error occured: \n {e}")
root.destroy()
def compare(self):
"""
Generate comparison plot using loaded data and inputs.
"""
self.graph.clear_artists()
mode = None
for fname in self.files:
base = os.path.basename(fname)
xl = pd.ExcelFile(fname)
#if len(xl.sheet_names) != 3: # Change this if adding additional Excel sheets to file
#tk.messagebox.showwarning(title="Invalid Input", message=f"Bad format (Wrong # of sheets in Excel file) - {fname}")
#return
if mode == None:
mode = xl.sheet_names[-1] # Last sheet in file must be the "All Data" sheet
elif xl.sheet_names[-1] != mode: # Checks if sheet name has Weight Fraction or Volume Fraction in it
tk.messagebox.showwarning(title="Invalid Input", message=f"Can't mix weight% and volume% - {fname}")
return
df = xl.parse(mode)
try:
if self.plot_total.get():
self.graph.conc_ax.plot(df.iloc[:,1], df.iloc[:,-3], label=base)
if self.plot_partial.get():
for i in range(3, len(df.columns)-3):
self.graph.conc_ax.plot(df.iloc[:,1], df.iloc[:,i:i+1], label=f"{base} - {df.columns[i]}")
if self.plot_RED.get():
self.graph.RED_ax.plot(df.iloc[:,1], df.iloc[:,-2], label=base)
if self.plot_temp.get():
self.graph.temp_ax.plot(df.iloc[:,1], df.iloc[:,-1], label=base)
except Exception:
tk.messagebox.showwarning(title="Invalid Input", message=f"Bad format - {fname}")
return
self.graph.conc_ax.set_ylabel(mode[10:-1])
self.graph.temp_ax.relim()
self.graph.RED_ax.relim()
self.graph.conc_ax.relim()
self.graph.conc_ax.legend(fontsize=6)
self.graph.canvas.draw()
def get_config(self):
return {"files": list(self.files)}
def set_config(self, config):
self.clear()
self.add_files(config["files"])
class ReformInputFrame(ttk.Frame):
"""
Handles all input for the reformulation/alternative solvent selection screen.
"""
def __init__(self, root, compare_screen, compare_input, results_frame):
super().__init__(root)
# self.columnconfigure(0, weight=1)
# self.rowconfigure(0, weight=1)
self.compare_screen = compare_screen
self.compare_input = compare_input
self.results_frame = results_frame
self.results_frame.set_input_frame(self)
ttk.Button(self, text="Load Control Blend", command=self.load_control_blend).grid(row=0,column=0,columnspan=2)
self.control_blend_label = ttk.Label(self, text="")
self.control_blend_label.grid(row=0,column=2,columnspan=2)
ttk.Button(self, text="Load Minimum Composition", command=self.load_min_comp).grid(row=1,column=0,columnspan=2)
self.min_comp_label = ttk.Label(self, text="")
self.min_comp_label.grid(row=1,column=2,columnspan=2)
ttk.Button(self, text="Load Whitelist/Blacklist", command=self.load_wl_bl).grid(row=2,column=0,columnspan=2)
self.wl_bl_label = ttk.Label(self, text="")
self.wl_bl_label.grid(row=2,column=2,columnspan=2)
ttk.Button(self, text="Select Solvent Info File", command=self.select_solvent_file).grid(row=3,column=0,columnspan=2)
self.solvent_label = ttk.Label(self, text="")
self.solvent_label.grid(row=3, column=2, columnspan=2)
ttk.Label(self, text="Max VOC ").grid(row=4,column=0)
self.max_VOC_type = tk.StringVar(self, "wt %")
ttk.Combobox(self, textvariable = self.max_VOC_type, values=("wt %", "vol %")).grid(row=4,column=3)
self.max_VOC = ttk.Entry(self)
self.max_VOC.grid(row=4,column=1,columnspan=2)
ttk.Label(self, text="# Exempt Solvents: ").grid(row=5,column=0)
self.solvent_nums = dict()
self.solvent_nums["min_exempt"] = tk.StringVar(self, "1")
self.solvent_nums["min_exempt"].trace("w", lambda i, v, o: self.update_solvent_nums("min_exempt"))
ttk.Combobox(self, textvariable=self.solvent_nums["min_exempt"], values=("1","2")).grid(row=5,column=1)
ttk.Label(self, text=" to ").grid(row=5,column=2)
self.solvent_nums["max_exempt"] = tk.StringVar(self, "2")
self.solvent_nums["max_exempt"].trace("w", lambda i, v, o: self.update_solvent_nums("max_exempt"))
ttk.Combobox(self, textvariable=self.solvent_nums["max_exempt"], values=("1","2")).grid(row=5,column=3)
ttk.Label(self, text="# Non-exempt Solvents: ").grid(row=6,column=0)
self.solvent_nums["min_ne"] = tk.StringVar(self, "1")
self.solvent_nums["min_ne"].trace("w", lambda i, v, o: self.update_solvent_nums("min_ne"))
ttk.Combobox(self, textvariable=self.solvent_nums["min_ne"], values=("0","1","2","3")).grid(row=6,column=1)
ttk.Label(self, text=" to ").grid(row=6,column=2)
self.solvent_nums["max_ne"] = tk.StringVar(self, "2")
self.solvent_nums["max_ne"].trace("w", lambda i, v, o: self.update_solvent_nums("max_ne"))
ttk.Combobox(self, textvariable=self.solvent_nums["max_ne"], values=("0","1","2","3")).grid(row=6,column=3)
self.target_frame = TargetParamFrame(self)
self.target_frame.grid(row=7, column=0, columnspan=4)
ttk.Label(self, text="Formulation Density: ").grid(row=8,column=0)
self.density_units = tk.StringVar(self, "lb/gal")
ttk.Combobox(self, textvariable = self.density_units, values=("lb/gal", "g/L")).grid(row=8, column=3)
self.density_entry = ttk.Entry(self)
self.density_entry.grid(row=8,column=1,columnspan=2)
self.temp_frame = TempProfileFrame(self)
self.temp_frame.grid(row=9, column=0, columnspan=4)
self.big_style = ttk.Style()
self.big_style.configure("big.TButton", font=('Arial',14))
ttk.Label(self, text="Total Time (min): ").grid(row=10,column=0)
self.total_time = ttk.Entry(self)
self.total_time.grid(row=10,column=1)
ttk.Label(self, text="# Blends to display: ").grid(row=11,column=0)
self.num_results = ttk.Entry(self)
self.num_results.grid(row=11, column=1)
ttk.Button(self, text="Find Solvent Blends", style="big.TButton", command=self.find_blends).grid(row=12, column=0, columnspan=2)
self.control_fname = ""
self.mc_fname = ""
self.wl_bl_fname = ""
self.control_blend = None
self.min_comp = pd.DataFrame({"Name":[],"Volume Fraction":[]})
self.whitelist = None
self.using_whitelist = False
self.blacklist = pd.Series()
def set_main_input(self, main_input):
self.main_input = main_input
def load_control_blend(self, fname=None):
if fname is None:
fname = tk.filedialog.askopenfilename(filetypes=[("Excel files (.xlsx)", "*.xlsx")])
self.control_fname = fname
try:
self.control_blend = pd.read_excel(fname)
except Exception:
if fname == "":
return
tk.messagebox.showwarning(title="Could not Open", message=f"Could not open file {fname}, likely don't have read permission (often happens if the file is in onedrive)")
self.focus_force()
return
if "Name" not in self.control_blend.columns or ("Weight Fraction" not in self.control_blend.columns and "Volume Fraction" not in self.control_blend.columns):
tk.messagebox.showwarning(title="Invalid Input", message="Incorrectly formatted control blend file")
self.control_fname = None
self.control_blend = None
self.focus_force()
return
self.control_blend_label.configure(text=os.path.basename(fname))
self.focus_force()
def load_min_comp(self, fname=None):
if fname is None:
fname = tk.filedialog.askopenfilename(filetypes=[("Excel files (.xlsx)", "*.xlsx")])
try:
self.min_comp = pd.read_excel(fname)
except Exception:
if fname == "":
return
tk.messagebox.showwarning(title="Could not Open", message=f"Could not open file {fname}, likely don't have read permission (often happens if the file is in onedrive)")
self.focus_force()
return
self.mc_fname = fname
# if "Name" not in self.min_comp.columns or ("Weight Fraction" not in self.min_comp.columns and "Volume Fraction" not in self.min_comp.columns):
# tk.messagebox.showwarning(title="Invalid Input", message="Incorrectly formatted minimum composition file")
# self.mc_fname = None
# self.min_comp = None
# self.focus_force()
# return
if "Name" not in self.min_comp.columns:
tk.messagebox.showwarning(title="Invalid Input", message="Minimum composition file is missing the 'Name' column.")
self.mc_fname = None
self.min_comp = None
self.focus_force()
return
if "Weight Fraction" not in self.min_comp.columns and "Volume Fraction" not in self.min_comp.columns:
tk.messagebox.showwarning(title="Invalid Input", message="Minimum composition file is missing both 'Weight Fraction' and 'Volume Fraction' columns. At least one is required.")
self.mc_fname = None
self.min_comp = None
self.focus_force()
return
self.min_comp_label.configure(text=os.path.basename(fname))
self.focus_force()
def load_wl_bl(self, fname=None):
if fname is None:
fname = tk.filedialog.askopenfilename(filetypes=[("Excel files (.xlsx)", "*.xlsx")])
try:
wl_bl = pd.read_excel(fname)
except Exception:
if fname == "":
return
tk.messagebox.showwarning(title="Could not Open", message=f"Could not open file {fname}, likely don't have read permission (often happens if the file is in onedrive)")
self.focus_force()
return
self.wl_bl_fname = fname
if len(wl_bl.columns) == 0:
tk.messagebox.showwarning(title="Invalid Input", message="Incorrectly formatted whitelist/blacklist file")
wl_bl = None
self.focus_force()
return
if wl_bl.columns[0].lower() == "whitelist":
self.whitelist = wl_bl.iloc[:,0]
self.blacklist = None
self.using_whitelist = True
self.wl_bl_label.configure(text=os.path.basename(fname)+" (Whitelist)")
elif wl_bl.columns[0].lower() == "blacklist":
self.blacklist = wl_bl.iloc[:,0]
self.whitelist = None
self.using_whitelist = False
self.wl_bl_label.configure(text=os.path.basename(fname)+" (Blacklist)")
else:
tk.messagebox.showwarning(title="Invalid Input", message="Incorrectly formatted whitelist/blacklist file")
wl_bl = None
self.focus_force()
return
self.focus_force()
def update_solvent_nums(self, upd):
"""
Update the bounds on the min/max number of solvents to use
"""
updated = self.solvent_nums[upd]
other = self.solvent_nums["min"+upd[3:] if upd[:3] =="max" else "max"+upd[3:]]
try:
if upd[:3] == "min" and int(updated.get()) > int(other.get()) or upd[:3] == "max" and int(updated.get()) < int(other.get()):
other.set(updated.get())
except ValueError:
pass
def find_blends(self):
"""
Gathers inputs, checks them, and then calls :func:`get_alternative_blends`
"""
self.results_frame.clear() # Clears the Alternative Blends Treeview Frame
try:
min_exempt = int(self.solvent_nums["min_exempt"].get()) # Gathers the number of exempt and non-exempt solvents specified by user
max_exempt = int(self.solvent_nums["max_exempt"].get())
min_ne = int(self.solvent_nums["min_ne"].get())
max_ne = int(self.solvent_nums["max_ne"].get())
if max_exempt <= min_exempt:
raise ValueError("Maximum exempt solvents must be greater than minimum.")
if max_ne <= min_ne:
raise ValueError("Maximum non-exempt solvents must be greater than minimum.")
except ValueError as e:
tk.messagebox.showwarning(title="Invalid Input", message=f"Error in solvent numbers: {str(e)}\nPlease enter valid integers with max > min for both exempt and non-exempt solvents.")
self.focus_force()
return
try:
max_voc = float(self.max_VOC.get())
if self.max_VOC_type.get() in ["vol %", "wt %"]:
if not 0 <= max_voc <= 100:
raise ValueError("VOC must be between 0 and 100.")
max_voc /= 100
except ValueError as e:
tk.messagebox.showwarning(title="Invalid Input", message=f"Error in max VOC: {str(e)}\nPlease enter a decimal number between 0 and 100 for max VOC.")
self.focus_force()
return
target_params = self.target_frame.get_params() # Get Target parameters from user input
temp_profile = self.temp_frame.get_temp_profile() # Get Temperature parameters from user input
if target_params is None or temp_profile is None:
return
if self.main_input.all_solvents_df is None:
tk.messagebox.showwarning(title="Missing File", message="Solvent info file is missing. Please load a solvent info file before proceeding.")
self.focus_force()
return
if self.using_whitelist:
missing_solvents = [] # missing_solvents tracks what whitelist solvents are not in solvent database for error message
for solvent in self.whitelist:
if solvent not in self.main_input.all_solvents_df.index:
missing_solvents.append(str(solvent))
if missing_solvents:
tk.messagebox.showwarning(title="Invalid Input", message=f"The following solvents in the whitelist are not in the solvent database: {', '.join(missing_solvents)}")
self.focus_force()
return
elif self.blacklist is not None:
missing_solvents = [] # tracks what blacklist solvents are not in solvent database ## Would this even cause an issue?
for solvent in self.blacklist:
if solvent not in self.main_input.all_solvents_df.index:
missing_solvents.append(str(solvent))
if missing_solvents:
tk.messagebox.showwarning(title="Invalid Input", message=f"The following solvents in the blacklist are not in the solvent database: {', '.join(missing_solvents)}")
self.focus_force()
return
replace_by = "Volume Fraction" if "Volume Fraction" in self.min_comp.columns else "Weight Fraction" # Converts betweeen Volume and Weight Fraction
missing_solvents = [] # tracks what min_comp solvents are not in the database
for solvent in self.min_comp["Name"]:
if solvent not in self.main_input.all_solvents_df.index:
missing_solvents.append(str(solvent))
if missing_solvents:
tk.messagebox.showwarning(title="Invalid Input", message=f"The following solvents in the minimum composition are not in the solvent database: {', '.join(missing_solvents)}")
self.focus_force()
return
if str(self.min_comp[replace_by].dtype)[:-2] not in ['int','float']:
tk.messagebox.showwarning(title="Invalid Input", message=f"The {replace_by} column in the minimum composition file contains non-numeric values.")
self.focus_force()
return
try:
formulation_density = float(self.density_entry.get())
if self.density_units.get() == "g/L": # Convert density from g/L to lb/gal
formulation_density *= 0.0083
except ValueError:
tk.messagebox.showwarning(title="Invalid Input", message="Please enter a valid decimal number for formulation density (or 0 if unknown).")
self.focus_force()
return
control_solvents = pd.DataFrame([self.main_input.all_solvents_df.loc[n,:] for n in self.control_blend["Name"]]) # Gather info for solvents in control
try:
if "Volume Fraction" not in self.control_blend.columns:
self.control_blend["Volume Fraction"] = self.control_blend["Weight Fraction"] / np.array(control_solvents["Density"])
self.control_blend["Volume Fraction"] /= sum(self.control_blend["Volume Fraction"])
assert not any(np.isnan(np.array(self.control_blend["Volume Fraction"])))
if "Weight Fraction" not in self.control_blend.columns:
self.control_blend["Weight Fraction"] = self.control_blend["Volume Fraction"] * np.array(control_solvents["Density"])
self.control_blend["Weight Fraction"] /= sum(self.control_blend["Weight Fraction"])
assert not any(np.isnan(np.array(self.control_blend["Weight Fraction"])))
except AssertionError:
tk.messagebox.showwarning(title="Invalid Input", message="Volume and/or weight fractions could not be computed - were densities entered correctly?")
return
if self.control_blend is None:
tk.messagebox.showwarning(title="Missing file", message="Missing control blend file")
self.focus_force()
return
if not all([solvent in self.main_input.all_solvents_df.index for solvent in self.control_blend["Name"]]) or str(self.control_blend[replace_by].dtype)[:-2] not in ['int','float']:
tk.messagebox.showwarning(title="Invalid Input", message="Control blend incorrectly configured")
self.focus_force()
return
VOC_limit_type = self.max_VOC_type.get()
if VOC_limit_type == "vol %" and replace_by == "Weight Fraction" or VOC_limit_type == "wt %" and replace_by == "Volume Fraction":
tk.messagebox.showwarning(title="Invalid Input", message="VOC limit should match minimum composition units")
self.focus_force()
return
try:
num_results = int(self.num_results.get())
except TypeError:
tk.messagebox.showwarning(title="Invalid Input", message="# Blends in Results must be an integer")
except ValueError:
num_results = 10 # If no value is entered, display 10 results as the default
# Does the processing of the reformulator window
results = get_alternative_blends(self.main_input.all_solvents_df, self.control_blend, self.min_comp, replace_by, target_params,
temp_profile, (min_exempt, max_exempt), (min_ne, max_ne), (max_voc, VOC_limit_type), formulation_density, num_results,
self.whitelist if self.using_whitelist else None, self.blacklist if not self.using_whitelist else None)