-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__main__.py
1218 lines (943 loc) · 55.4 KB
/
__main__.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 sys
import os
import io
import copy
import numpy as np
import pandas as pd
from PySide6.QtWidgets import QWidget, QApplication, QMainWindow, QFileDialog, QMessageBox, QTextEdit, QVBoxLayout, QDialog, QSplashScreen, QTableWidget, QTableWidgetItem
from PySide6.QtCore import Signal, QThread, QUrl
from progress.App.MainWindow import Ui_MainWindow
from progress.mod_sysdata import RASystemData
from progress.mod_utilities import RAUtilities
from progress.mod_solar import Solar
from progress.mod_kmeans import KMeans_Pipeline
from progress.mod_wind import Wind
from progress.mod_matrices import RAMatrices
from progress.mod_plot import RAPlotTools
from progress.paths import get_path
base_dir = get_path()
from PySide6.QtPdf import QPdfDocument
from PySide6.QtPdfWidgets import QPdfView
class PDFViewer(QWidget):
def __init__(self, pdf_path=None):
super().__init__()
# Create a QPdfView instance
self.pdf_view = QPdfView(self)
# Create a layout and add the QPdfView to it
layout = QVBoxLayout(self)
layout.addWidget(self.pdf_view)
self.setLayout(layout)
# Load the PDF file if a path is provided
if pdf_path:
self.load_pdf(pdf_path)
def load_pdf(self, pdf_path):
# Print the provided path for debugging
#print(f"Provided PDF path: {pdf_path}")
# Get the absolute path and print it for debugging
abs_path = os.path.abspath(pdf_path)
#print(f"Absolute PDF path: {abs_path}")
# Check if the PDF file exists
if not os.path.exists(abs_path):
print("The specified PDF file does not exist.")
return
#print(f"Attempting to load PDF from: {abs_path}") # Debugging output
# Create a QPdfDocument instance
self.pdf_document = QPdfDocument()
load_error = self.pdf_document.load(abs_path) # Load the PDF document
self.pdf_view.setDocument(self.pdf_document)
# # Check if the PDF loaded successfully
# if load_error == QPdfDocument.NoError: # NoError indicates success
# self.pdf_view.setDocument(self.pdf_document)
# else:
# print("Failed to load PDF document. Error code:", load_error)
def get_pdf_view(self):
"""Returns the QPdfView instance for adding to other layouts."""
return self.pdf_view
class OutputWindow(QDialog):
"""
A dialog window for displaying output text.
Methods:
- __init__(self, parent=None): Initializes the output window.
- update_output(self, text): Appends text to the output display.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("QuESt Reliability Processes")
self.output_text = QTextEdit()
self.output_text.setMinimumSize(700, 600)
self.output_text.setReadOnly(True)
layout = QVBoxLayout()
layout.addWidget(self.output_text)
self.setLayout(layout)
def update_output(self, text):
self.output_text.append(text)
class WorkerThread(QThread):
"""
A worker thread for running long-running methods in the background.
Signals:
- finished: Emitted when the thread finishes execution.
- output_updated: Emitted when the output is updated.
Methods:
- __init__(self, method, *args): Initializes the worker thread with a method and its arguments.
- run(self): Redirects stdout, runs the method, restores stdout, and emits the finished signal.
"""
finished = Signal()
output_updated = Signal(str)
def __init__(self, method, *args):
super().__init__()
self.method = method
self.args = args
def run(self):
# Redirect stdout to a buffer
stdout_buffer = StdoutBuffer(self)
sys.stdout = stdout_buffer
# Execute the long-running method
self.method(*self.args)
# Restore stdout
sys.stdout = sys.__stdout__
# Emit the finished signal when the process completes
self.finished.emit()
class StdoutBuffer:
"""
A buffer for capturing and emitting stdout text.
Methods:
- __init__(self, worker_thread): Initializes the buffer with a worker thread.
- write(self, text): Captures and emits text.
- flush(self): No-op for compatibility.
"""
def __init__(self, worker_thread):
self.worker_thread = worker_thread
self.buffer = ""
def write(self, text):
self.buffer += text
lines = self.buffer.split("\n")
for line in lines[:-1]:
self.worker_thread.output_updated.emit(line)
self.buffer = lines[-1]
def flush(self):
pass
class MainAppWindow(QMainWindow):
"""
The main application window.
Methods:
- __init__(self, parent=None): Initializes the main window and connects UI elements to methods.
- switch_to_home_page(self): Switches to the home page.
- switch_to_DI_page(self): Switches to the data input page.
- switch_to_system_page(self): Switches to the system tab.
- switch_to_solar_page(self): Switches to the solar tab.
- switch_to_wind_page(self): Switches to the wind tab.
- switch_to_sim_page(self): Switches to the simulation page.
- handle_output(self, text): Updates the output window with text.
- open_folder_in_explorer(self, path): Opens a folder in the system's file explorer.
- open_sys_directory(self): Opens a dialog to select the system directory.
- load_sys_data(self): Loads system data and calculates required variables.
- show_help_solar(self): Displays a help message for the solar tab.
- open_solar_directory(self): Opens a dialog to select the solar directory.
- save_solarinput(self): Saves input data provided by the user in the solar tab.
- solar_data_process(self): Downloads and processes solar data.
- start_download_thread(self): Starts the download thread.
- start_gather_thread(self): Starts the gather thread.
- end_gather_thread(self): Handles the end of the gather thread.
- kmeans_eval(self): Evaluates clustering metrics.
- kmeans_gen(self): Generates clusters for solar data.
- save_windinput(self): Saves input data provided by the user in the wind tab.
- open_wind_directory(self): Opens a dialog to select the wind directory.
- download_wind_data(self): Downloads wind data.
- cal_wind_tr_rates(self, wind): Calculates wind transition rates.
- download_finished(self): Handles the completion of the download.
- save_mcsinput(self): Saves input data provided by the user in the simulation page.
- run(self): Runs the selected Monte Carlo Simulation (MCS) method.
- MCS_zonal(self): Performs MCS using the zonal model.
- MCS_cs(self): Performs MCS using the copper sheet model.
- plot(self): Plots the results of the simulation.
"""
def __init__(self, parent=None):
super(MainAppWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self) # Setup the UI using the imported class
self.output_window = OutputWindow()
# button connections in the landing page (stacked widget #1)
# self.ui.pushButton_getStarted.clicked.connect(self.switch_to_DI_page)
self.ui.actionHome_Page.triggered.connect(self.switch_to_home_page)
self.ui.stackedWidget.setCurrentIndex(1)
# button connections in the data input page (stacked widget #2)
#self.ui.pushButton_DI_previous.clicked.connect(self.switch_to_home_page)
self.ui.pushButton_help_API.clicked.connect(self.show_help_api)
self.ui.pushButton_help_API_2.clicked.connect(self.show_help_name)
self.ui.pushButton_help_API_3.clicked.connect(self.show_skip_api)
self.ui.pushButton_save_solarinput.clicked.connect(self.save_api_input)
self.ui.pushButton_DI_next_4.setVisible(False)
# button connections in tab widget "solar"
self.ui.widget_5.setVisible(False)
self.ui.textBrowser_4.setVisible(False)
self.ui.pushButton_solar_dl.setVisible(False)
self.ui.pushButton_DI_next_2.setVisible(False)
self.ui.pushButton_DI_next_5.setVisible(False)
self.ui.pushButton_solar_upload.setVisible(False)
self.ui.comboBox_2.currentIndexChanged.connect(self.solar_cb_changed)
self.ui.pushButton_solar_upload.clicked.connect(self.upload_solar_data)
self.ui.pushButton_help_solar.clicked.connect(self.show_help_solar)
self.ui.textBrowser_6.setVisible(False)
self.ui.textBrowser_5.setVisible(False)
self.ui.pushButton_solar_dl.clicked.connect(self.solar_data_process)
self.ui.pushButton_2.clicked.connect(self.kmeans_eval)
self.ui.pushButton_3.clicked.connect(self.kmeans_gen)
self.ui.pushButton.clicked.connect(self.save_solar_data)
# # button connections in tab widget "wind"
self.ui.widget_9.setVisible(False)
self.ui.pushButton_4.setVisible(False)
self.ui.pushButton_7.setVisible(False)
self.ui.textBrowser_3.setVisible(False)
self.ui.pushButton_wind_upload.setVisible(False)
self.ui.pushButton_DI_next_3.setVisible(False)
self.ui.pushButton_help_wind.setVisible(False)
self.ui.comboBox_3.currentIndexChanged.connect(self.wind_cb_changed)
self.ui.pushButton_wind_upload.clicked.connect(self.upload_wind_data)
self.ui.pushButton_help_wind.clicked.connect(self.wind_process_help)
self.ui.pushButton_4.clicked.connect(self.download_wind_data)
self.ui.pushButton_7.clicked.connect(self.process_existing_wdata)
self.ui.pushButton_5.clicked.connect(self.run)
#define data paths
self.sys_directory = os.path.join(base_dir, "Data", "System")
self.load_sys_data()
self.solar_directory = os.path.join(base_dir, "Data", "Solar")
self.wind_directory = os.path.join(base_dir, "Data", "Wind")
self.cluster_results = os.path.join(base_dir, "Data", "Solar", "clustering_results.txt")
self.pdf_path = os.path.join(base_dir, "Data", "Solar", "SSE_Curve.png")
self.ui.pushButton_getStarted.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.api_tab))
self.ui.pushButton_skip_API.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.solar_tab))
self.ui.pushButton_DI_next_4.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.solar_tab))
self.ui.pushButton_DI_previous_2.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.api_tab))
self.ui.pushButton_DI_next_2.clicked.connect(lambda: self.ui.stackedWidget_2.setCurrentIndex(1))
self.ui.pushButton_DI_previous_4.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.tab_7))
self.ui.pushButton.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.wind_tab))
self.ui.pushButton_DI_next_5.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.wind_tab))
self.ui.pushButton_DI_previous_5.clicked.connect(lambda: self.ui.stackedWidget_2.setCurrentIndex(0))
self.ui.pushButton_DI_next_3.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.sim_tab))
self.ui.pushButton_DI_previous_3.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.solar_tab))
self.ui.pushButton_sim_previous.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.wind_tab))
self.ui.pushButton_6.clicked.connect(lambda: self.ui.tabWidget.setCurrentWidget(self.ui.results_tab))
#self.ui.pushButton_6.clicked.connect(self.plot)
self.counter = 0
self.plot_count = 0
self.tester=0
def switch_to_home_page(self):
self.ui.stackedWidget.setCurrentIndex(0)
def switch_to_DI_page(self):
self.ui.stackedWidget.setCurrentIndex(1)
def switch_to_system_page(self):
self.ui.tabWidget.setCurrentIndex(0)
def switch_to_solar_page(self):
self.ui.tabWidget.setCurrentIndex(1)
self.ui.tab_2.setEnabled(True)
def switch_to_wind_page(self):
self.ui.tabWidget.setCurrentIndex(2)
self.ui.tab_3.setEnabled(True)
def switch_to_sim_page(self):
self.ui.stackedWidget.setCurrentIndex(2)
def handle_output(self, text_browser, text):
# Update the GUI with the output text
text_browser.append(text)
def open_folder_in_explorer(self, path):
if sys.platform == "win32":
os.startfile(path)
elif sys.platform == "darwin":
os.system(f"open {path}")
else:
os.system(f"xdg-open {path}")
def open_sys_directory(self):
sys_directory = QFileDialog.getExistingDirectory(self, "Select Directory", "")
if sys_directory:
self.ui.lineEdit_3.setText(sys_directory)
self.sys_directory = sys_directory
# load system data and calculate required variables
def load_sys_data(self):
rasd = RASystemData()
self.data_gen = self.sys_directory + '/gen.csv'
self.data_branch = self.sys_directory + '/branch.csv'
self.data_bus = self.sys_directory + '/bus.csv'
self.data_load = self.sys_directory + '/load.csv'
self.data_storage = self.sys_directory + '/storage.csv'
self.genbus, self.ng, self.pmax, self.pmin, self.FOR_gen, self.MTTF_gen, self.MTTR_gen, self.gencost = rasd.gen(self.data_gen)
self.nl, self.fb, self.tb, self.cap_trans, self.MTTF_trans, self.MTTR_trans = rasd.branch(self.data_branch)
self.bus_name, self.bus_no, self.nz = rasd.bus(self.data_bus)
self.load_all_regions = rasd.load(self.bus_name, self.data_load)
self.essname, self.essbus, self.ness, self.ess_pmax, self.ess_pmin, self.ess_duration, self.ess_socmax, self.ess_socmin, \
self.ess_eff, self.disch_cost, self.ch_cost, self.MTTF_ess, self.MTTR_ess, self.ess_units = rasd.storage(self.data_storage)
self.raut = RAUtilities()
self.mu_tot, self.lambda_tot = self.raut.reltrates(self.MTTF_gen, self.MTTF_trans, self.MTTR_gen, self.MTTR_trans, self.MTTF_ess, self.MTTR_ess)
self.cap_max, self.cap_min = self.raut.capacities(self.nl,self.pmax, self.pmin, self.ess_pmax, self.ess_pmin, self.cap_trans) # calling this function to get values of cap_max and cap_min
# Open help message box in the "solar" tab
def show_help_api(self):
QMessageBox.information(self, "API Help 1", "Signup for API key: https://developer.nrel.gov/signup/")
# Open help message box in the "solar" tab
def show_help_name(self):
QMessageBox.information(self, "API Help 2", "Use '+' instead of space for name and affiliation, e.g., john+doe.")
def show_skip_api(self):
QMessageBox.information(self, "API Help 3", "You can skip this step if you are using your own data.")
# save input data provided by the user in the solar tab
def save_api_input(self):
# save user input
self.input_api = self.ui.lineEdit_api.text()
self.input_name = self.ui.lineEdit_name.text()
self.input_email = self.ui.lineEdit_email.text()
self.input_aff = self.ui.lineEdit_aff.text()
self.input_api_w = self.input_api
self.input_email_w = self.input_email
self.input_aff_w = self.input_aff
QMessageBox.information(self, "API information", "Saved!")
self.ui.pushButton_DI_next_4.setVisible(True)
self.ui.pushButton_skip_API.setVisible(False)
self.ui.pushButton_help_API_3.setVisible(False)
# Open help message box in the "solar" tab
def show_help_solar(self):
QMessageBox.information(self, "Solar Help", "You can skip this step if your solar power generation data has previously been clustered.")
def solar_cb_changed(self, index):
if index == 1:
self.ui.textBrowser_4.setVisible(True)
self.ui.widget_5.setVisible(True)
self.ui.pushButton_solar_dl.setVisible(True)
self.ui.pushButton_solar_upload.setVisible(False)
elif index == 2:
self.ui.pushButton_solar_upload.setVisible(True)
self.ui.textBrowser_4.setVisible(False)
self.ui.widget_5.setVisible(False)
self.ui.pushButton_solar_dl.setVisible(False)
# self.ui.pushButton_DI_next_2.setVisible(True)
def upload_solar_data(self):
self.solar_directory = QFileDialog.getExistingDirectory(self, "Select Directory", "")
QMessageBox.information(self, "Solar Upload", "Solar data uploaded and saved!")
self.ui.pushButton_DI_next_2.setVisible(True)
# download weather data and convert to solar generation data for all sites
def solar_data_process(self):
self.ui.textBrowser_4.append("Downloading solar data...")
self.input_starty = int(self.ui.lineEdit_starty.text())
self.input_endy = int(self.ui.lineEdit_endy.text())
self.solar_site_data = self.solar_directory+"/solar_sites.csv"
self.solar_prob_data = self.solar_directory+"/solar_probs.csv"
self.solar = Solar(self.solar_site_data, self.solar_directory)
# Create worker threads for download and gather processes
self.download_thread = WorkerThread(self.solar.SolarGen, self.input_api, self.input_name, \
self.input_aff, self.input_email, self.input_starty, self.input_endy)
self.gather_thread = WorkerThread(self.solar.SolarGenGather, self.input_starty, self.input_endy)
self.start_download_thread()
# Connect the output_updated signal to update the GUI
self.download_thread.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_4, text))
# Connect the finished signal of download_thread to start the gather_thread
self.download_thread.finished.connect(self.start_gather_thread)
# Connect the output_updated signal to update the GUI
self.gather_thread.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_4, text))
# Connect the finished signal of gather_thread to handle thread completion
self.gather_thread.finished.connect(self.end_gather_thread)
self.ui.pushButton_DI_next_2.setVisible(True)
def start_download_thread(self):
# start download thread
self.download_thread.start()
#self.ui.textBrowser_4.append("Downloading solar data please wait until the process has finished.")
#self.output_window.show()
def start_gather_thread(self):
# Start the gather_thread
self.gather_thread.start()
def end_gather_thread(self):
pass
def kmeans_eval(self):
self.ui.textBrowser_6.setVisible(True)
self.ui.textBrowser_5.setVisible(True)
self.solar_site_data = self.solar_directory + "/solar_sites.csv"
self.solar_prob_data = self.solar_directory + "/solar_probs.csv"
self.solar = Solar(self.solar_site_data, self.solar_directory)
QMessageBox.information(self, "Clustering Metrics", "Press OK to continue. This may take a few minutes.")
self.clust_eval = self.ui.lineEdit.text()
# Create a worker thread for the instantiation of KMeans_Pipeline
self.worker_pipeline = WorkerThread(self.create_pipeline)
self.worker_pipeline.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_6, text))
#self.worker_pipeline.finished.connect(self.start_test_metrics)
self.worker_pipeline.finished.connect(self.checker)
# Start the worker thread for pipeline instantiation
self.worker_pipeline.start()
def checker(self):
if self.counter==0:
# print(self.counter)
self.counter = 1
# print(self.counter)
else:
self.start_test_metrics()
self.counter = 0
def create_pipeline(self):
self.pipeline = KMeans_Pipeline(self.solar_directory, self.solar_site_data)
def start_test_metrics(self):
# Check if worker_pipeline has completed
if hasattr(self, 'worker_pipeline') and self.worker_pipeline.isFinished():
# Create worker threads for the pipeline methods
self.worker1 = WorkerThread(self.pipeline.test_metrics, int(self.clust_eval))
self.worker2 = WorkerThread(self.display_text_file, self.cluster_results)
# Connect signals
self.worker1.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_6, text))
self.worker1.finished.connect(self.start_worker2)
self.worker2.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_5, text))
self.worker2.finished.connect(self.on_workers_finished)
# Start the first worker
self.worker1.start()
else:
print("worker_pipeline has not finished yet.")
def start_worker2(self):
if self.tester==0:
self.tester=1
else:
self.worker2.start()
self.tester=0
def display_text_file(self, file_path):
try:
with open(file_path, 'r') as file:
content = file.read()
self.ui.textBrowser_5.append(content) # Append the content to the QTextBrowser
except Exception as e:
self.ui.textBrowser_5.append(f"Error loading file: {e}")
def display_png(self, file_path):
if os.path.isfile(file_path): # Check if the file exists
url = QUrl.fromLocalFile(file_path) # Convert the file path to a URL
html_content = f'<img src="{url.toString()}" />'
self.ui.textBrowser_6.setHtml(html_content) # Load the image in the QTextBrowser
print('Image displayed successfully.')
else:
self.ui.textBrowser_6.setText("File does not exist.")
print("File does not exist.")
def on_workers_finished(self):
# QMessageBox.information(self, "Clustering Metrics", "Please look at SSE curve and silhouette score results to make an informed choice on the number of clusters.")
self.display_png(self.pdf_path)
# self.display_text_file(self.cluster_results)
def kmeans_gen(self):
# self.ui.textBrowser_5.setVisible(False)
self.clust_gen = self.ui.lineEdit_2.text()
self.pipeline.run(n_clusters = int(self.clust_gen))
self.pipeline.calculate_cluster_probability()
self.pipeline.split_and_cluster_data()
self.s_sites, self.s_zone_no, self.s_max, self.s_profiles, self.solar_prob = self.solar.GetSolarProfiles(self.solar_prob_data)
QMessageBox.information(self, "Clustering Complete", "Clustering of solar data complete!")
self.ui.pushButton_DI_next_5.setVisible(True)
def save_solar_data(self):
solar_site_data = self.solar_directory+"/solar_sites.csv"
solar_prob_data = self.solar_directory+"/solar_probs.csv"
solar = Solar(solar_site_data, self.solar_directory)
self.s_sites, self.s_zone_no, self.s_max, self.s_profiles, self.solar_prob = solar.GetSolarProfiles(solar_prob_data)
def wind_cb_changed(self, index):
if index == 1:
self.ui.widget_9.setVisible(True)
self.ui.pushButton_4.setVisible(True)
self.ui.pushButton_7.setVisible(False)
elif index == 2:
self.ui.pushButton_help_wind.setVisible(True)
self.ui.pushButton_DI_next_3.setVisible(True)
self.ui.widget_9.setVisible(False)
self.ui.pushButton_4.setVisible(False)
self.ui.pushButton_wind_upload.setVisible(True)
def upload_wind_data(self):
self.wind_directory = QFileDialog.getExistingDirectory(self, "Select Directory", "")
self.wind_site_data = self.wind_directory+"/wind_sites.csv"
self.pcurve_data = self.wind_directory+"/w_power_curves.csv"
wind = Wind()
self.w_sites, self.farm_name, self.zone_no, self.w_classes, self.w_turbines, self.r_cap, self.p_class, \
self.out_curve2, self.out_curve3, self.start_speed = wind.WindFarmsData(self.wind_site_data, self.pcurve_data)
wind_tr_rate = self.wind_directory + '/t_rate.xlsx'
if os.path.exists(wind_tr_rate):
self.tr_mats = pd.read_excel(wind_tr_rate, sheet_name=None)
self.tr_mats = np.array([self.tr_mats[sheet_name].to_numpy() for sheet_name in self.tr_mats])
else:
QMessageBox.information(self, "Transition Matrix", "Transition rate matrix does not exist. Please process wind speed data first.")
QMessageBox.information(self, "Wind Upload", "Wind data uploaded and saved!")
self.ui.pushButton_7.setVisible(True)
self.ui.pushButton_DI_next_3.setVisible(True)
def wind_process_help(self):
QMessageBox.information(self, "Wind Process Help", "Wind speed data (downloaded or user-provided) is utilized to generate transition rate matrix in this step. You can skip this step if you already have the required matrix.")
def download_wind_data(self):
self.ui.textBrowser_3.setVisible(True)
self.input_starty_w = int(self.ui.lineEdit_22.text())
self.input_endy_w = int(self.ui.lineEdit_23.text())
self.wind_site_data = self.wind_directory+"/wind_sites.csv"
self.pcurve_data = self.wind_directory+"/w_power_curves.csv"
wind = Wind()
self.w_sites, self.farm_name, self.zone_no, self.w_classes, self.w_turbines, self.r_cap, self.p_class, \
self.out_curve2, self.out_curve3, self.start_speed = wind.WindFarmsData(self.wind_site_data, self.pcurve_data)
# Create a worker thread for the DownloadWindData method
self.download_thread = WorkerThread(wind.DownloadWindData, self.wind_directory, self.wind_site_data, self.input_api_w, self.input_email_w, \
self.input_aff_w, self.input_starty_w, self.input_endy_w)
self.download_thread.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_3, text))
# Connect the finished signal to handle thread completion
self.download_thread.finished.connect(lambda: self.cal_wind_tr_rates())
# Start the worker thread
self.download_thread.start()
self.ui.pushButton_DI_next_3.setVisible(True)
self.ui.pushButton_7.setVisible(True)
def cal_wind_tr_rates(self):
self.windspeed_data = self.wind_directory+"/windspeed_data.csv"
self.wind_site_data = self.wind_directory+"/wind_sites.csv"
self.pcurve_data = self.wind_directory+"/w_power_curves.csv"
wind = Wind()
self.w_sites, self.farm_name, self.zone_no, self.w_classes, self.w_turbines, self.r_cap, self.p_class, \
self.out_curve2, self.out_curve3, self.start_speed = wind.WindFarmsData(self.wind_site_data, self.pcurve_data)
# calculate transition rates
wind.CalWindTrRates(self.wind_directory, self.windspeed_data, self.wind_site_data, self.pcurve_data)
# QMessageBox.information(self, "Processing Complete", "Wind data processing complete!")
# self.ui.pushButton_DI_next_3.setVisible(True)
def download_finished(self):
wind_tr_rate = self.wind_directory + '/t_rate.xlsx'
self.tr_mats = pd.read_excel(wind_tr_rate, sheet_name=None)
self.tr_mats = np.array([self.tr_mats[sheet_name].to_numpy() for sheet_name in self.tr_mats])
self.ui.pushButton_DI_next_3.setVisible(True)
def process_existing_wdata(self):
self.ui.textBrowser_3.setVisible(False)
self.cal_wind_tr_rates()
self.download_finished()
QMessageBox.information(self, "Existing Wind Data", "Processed!")
self.ui.pushButton_DI_next_3.setVisible(True)
def save_mcsinput(self):
self.samples= int(self.ui.lineEdit_4.text())
self.sim_hours = int(self.ui.lineEdit_5.text())
self.load_factor = float(self.ui.lineEdit_6.text())
self.ui.pushButton_5.setVisible(True)
self.current_text = self.ui.comboBox.currentText()
QMessageBox.information(self, "Sim Input", "Input Saved!")
def run(self):
self.save_mcsinput()
if self.current_text == "Zonal Model":
self.worker_zonal = WorkerThread(self.MCS_zonal)
#self.worker_zonal.output_updated.connect(self.handle_output)
self.worker_zonal.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_2, text))
self.worker_zonal.start()
self.worker_zonal.finished.connect(self.plot)
# self.output_window.show()
elif self.current_text == "Copper Sheet Model":
self.worker_copper= WorkerThread(self.MCS_cs)
#self.worker_copper.output_updated.connect(self.handle_output)
# Connect the output_updated signal to update the GUI
self.worker_copper.output_updated.connect(lambda text: self.handle_output(self.ui.textBrowser_2, text))
self.worker_copper.start()
self.worker_copper.finished.connect(self.plot)
# self.output_window.show()
def MCS_zonal(self):
"""
Performs mixed time sequential Monte Carlo Simulation (MCS) to evaluate the reliability of a power system. Uses transportation model.
Parameters:
samples (int): Number of samples to simulate.
sim_hours (int): Number of hours to simulate.
system_directory (str): Path to the directory containing system data files.
solar_directory (str or bool): Path to the directory containing solar data files or False if not used.
wind_directory (str or bool): Path to the directory containing wind data files or False if not used.
Returns:
tuple: A tuple containing indices, rank, SOC records, curtailment records, renewable records, bus names, and ESS names.
"""
BMva = 100
# matrices required for optimization
ramat = RAMatrices(self.nz)
gen_mat = ramat.genmat(self.ng, self.genbus, self.ness, self.essbus)
ch_mat = ramat.chmat(self.ness, self.essbus, self.nz)
A_inc = ramat.Ainc(self.nl, self.fb, self.tb)
curt_mat = ramat.curtmat(self.nz)
# dictionary for storing temp. index values
indices_rec = {"LOLP_rec": np.zeros(self.samples), "EUE_rec": np.zeros(self.samples), "MDT_rec": np.zeros(self.samples), \
"LOLF_rec": np.zeros(self.samples), "EPNS_rec": np.zeros(self.samples), "LOLP_hr": np.zeros(self.sim_hours), \
"LOLE_rec": np.zeros(self.samples), "mLOLP_rec":np.zeros(self.samples), "COV_rec": np.zeros(self.samples)}
LOL_track = np.zeros((self.samples, self.sim_hours))
for s in range(self.samples):
print(f'Sample: {s+1}')
# temp variables to be used for each sample
var_s = {"t_min": 0, "LLD": 0, "curtailment": np.zeros(self.sim_hours), "label_LOLF": np.zeros(self.sim_hours), "freq_LOLF": 0, "LOL_days": 0, \
"outage_day": np.zeros(365)}
# current states of components
current_state = np.ones(self.ng + self.nl + self.ness) # all gens and TLs in up state at the start of the year
if self.wind_directory:
current_w_class = np.floor(np.random.uniform(0, 1, self.w_sites)*self.w_classes).astype(int) # starting wind speed class for each site (random)
# record data for plotting and exporting (optional)
self.renewable_rec = {"wind_rec": np.zeros((self.nz, self.sim_hours)), "solar_rec": np.zeros((self.nz, self.sim_hours)), "congen_temp": 0, \
"rengen_temp": 0}
SOC_old = 0.5*(np.multiply(np.multiply(self.ess_pmax, self.ess_duration), self.ess_socmax))/BMva
self.SOC_rec = np.zeros((self.ness, self.sim_hours))
self.curt_rec = np.zeros(self.sim_hours)
# gen_rec = np.zeros((sim_hours, ng))
for n in range(self.sim_hours):
# get current states(up/down) and capacities of all system components
next_state, current_cap, var_s["t_min"] = self.raut.NextState(var_s["t_min"], self.ng, self.ness, self.nl, \
self.lambda_tot, self.mu_tot, current_state, self.cap_max, self.cap_min, self.ess_units)
current_state = copy.deepcopy(next_state)
# update SOC based on failures in ESS
ess_smax, ess_smin, SOC_old = self.raut.updateSOC(self.ng, self.nl, current_cap, self.ess_pmax, self.ess_duration, self.ess_socmax, \
self.ess_socmin, SOC_old)
# calculate upper and lower bounds of gens and tls
gt_limits = {"g_lb": np.concatenate((current_cap["min"][0:self.ng]/BMva, current_cap["min"][self.ng + self.nl::]/BMva)), \
"g_ub": np.concatenate((current_cap["max"][0:self.ng]/BMva, current_cap["max"][self.ng + self.nl::]/BMva)), "tl": current_cap["max"][self.ng:self.ng + self.nl]/BMva}
def fb_Pg(model, i):
return (gt_limits["g_lb"][i], gt_limits["g_ub"][i])
def fb_flow(model,i):
return (-gt_limits["tl"][i], gt_limits["tl"][i])
def fb_ess(model, i):
return(-current_cap["max"][self.ng + self.nl::][i]/BMva, current_cap["min"][self.ng + self.nl::][i]/BMva)
def fb_soc(model, i):
return(ess_smin[i]/BMva, ess_smax[i]/BMva)
# get wind power output for all zones/areas
if self.wind_directory:
w_zones, current_w_class = self.raut.WindPower(self.nz, self.w_sites, self.zone_no, \
self.w_classes, self.r_cap, current_w_class, self.tr_mats, self.p_class, self.w_turbines, self.out_curve2, self.out_curve3)
# get solar power output for all zones/areas
if self.solar_directory:
s_zones = self.raut.SolarPower(n, self.nz, self.s_zone_no, self.solar_prob, self.s_profiles, self.s_sites, self.s_max)
# record wind and solar profiles for plotting (optional)
if self.wind_directory:
self.renewable_rec["wind_rec"][:, n] = w_zones
if self.solar_directory:
s_zones_t = np.transpose(s_zones)
self.renewable_rec["solar_rec"][:, n] = s_zones_t[:, n%24]
# recalculate net load (for distribution side resources, optional)
part_netload = self.load_factor*self.load_all_regions
if self.solar_directory and self.wind_directory:
net_load = part_netload[n] - w_zones - s_zones[n%24]
elif self.solar_directory==False and self.wind_directory:
net_load = part_netload[n] - w_zones
elif self.solar_directory and self.wind_directory==False:
net_load = part_netload[n] - s_zones[n%24]
elif self.solar_directory==False and self.wind_directory==False:
net_load = part_netload[n]
# optimize dipatch and calculate load curtailment
load_curt, SOC_old = self.raut.OptDispatch(self.ng, self.nz, self.nl, self.ness, fb_ess, fb_soc, BMva, fb_Pg, fb_flow, A_inc, gen_mat, curt_mat, ch_mat, \
self.gencost, net_load, SOC_old, self.ess_pmax, self.ess_eff, self.disch_cost, self.ch_cost)
# record values for visualization purposes
self.SOC_rec[:, n] = SOC_old*BMva
self.curt_rec[n] = load_curt*BMva
# track loss of load states
var_s, LOL_track = self.raut.TrackLOLStates(load_curt, BMva, var_s, LOL_track, s, n)
if (n+1)%100 == 0:
print(f'Hour {n + 1}')
# collect indices for all samples
indices_rec = self.raut.UpdateIndexArrays(indices_rec, var_s, self.sim_hours, s)
# check for convergence using LOLP and COV
indices_rec["mLOLP_rec"][s] = np.mean(indices_rec["LOLP_rec"][0:s+1])
var_LOLP = np.var(indices_rec["LOLP_rec"][0:s+1])
indices_rec["COV_rec"][s] = np.sqrt(var_LOLP)/indices_rec["mLOLP_rec"][s]
# calculate reliability indices for the MCS
indices = self.raut.GetReliabilityIndices(indices_rec, self.sim_hours, self.samples)
self.mLOLP_rec = indices_rec["mLOLP_rec"]
self.COV_rec = indices_rec["COV_rec"]
print("Simulation complete! You can view the results now by clicking next! Plots are also saved to the results folder.")
self.ui.pushButton_6.setVisible(True)
self.main_folder = os.path.dirname(os.path.abspath(__file__))
self.results_dir = os.path.join(self.main_folder, 'Results')
if not os.path.exists(f"{self.main_folder}/Results"):
os.makedirs(f"{self.main_folder}/Results")
df = pd.DataFrame([indices])
df.to_csv(f"{self.main_folder}/Results/indices.csv", index=False)
if self.sim_hours == 8760:
self.raut.OutageHeatMap(LOL_track, 1, self.samples, self.main_folder)
def MCS_cs(self):
"""
Performs mixed time sequential Monte Carlo Simulation (MCS) to evaluate the reliability of a power system. Uses copper sheet model.
Parameters:
samples (int): Number of samples to simulate.
sim_hours (int): Number of hours to simulate.
system_directory (str): Path to the directory containing system data files.
solar_directory (str or bool): Path to the directory containing solar data files or False if not used.
wind_directory (str or bool): Path to the directory containing wind data files or False if not used.
Returns:
tuple: A tuple containing indices, rank, SOC records, curtailment records, renewable records, bus names, and ESS names.
"""
BMva = 100
# matrices required for optimization
ramat = RAMatrices(self.nz)
gen_mat = ramat.genmat(self.ng, self.genbus, self.ness, self.essbus)
ch_mat = ramat.chmat(self.ness, self.essbus, self.nz)
A_inc = ramat.Ainc(self.nl, self.fb, self.tb)
curt_mat = ramat.curtmat(self.nz)
# dictionary for storing temp. index values
indices_rec = {"LOLP_rec": np.zeros(self.samples), "EUE_rec": np.zeros(self.samples), "MDT_rec": np.zeros(self.samples), \
"LOLF_rec": np.zeros(self.samples), "EPNS_rec": np.zeros(self.samples), "LOLP_hr": np.zeros(self.sim_hours), \
"LOLE_rec": np.zeros(self.samples), "mLOLP_rec":np.zeros(self.samples), "COV_rec": np.zeros(self.samples)}
LOL_track = np.zeros((self.samples, self.sim_hours))
for s in range(self.samples):
print(f'Sample: {s+1}')
# temp variables to be used for each sample
var_s = {"t_min": 0, "LLD": 0, "curtailment": np.zeros(self.sim_hours), "label_LOLF": np.zeros(self.sim_hours), "freq_LOLF": 0, "LOL_days": 0, \
"outage_day": np.zeros(365)}
# current states of components
current_state = np.ones(self.ng + self.nl + self.ness) # all gens and TLs in up state at the start of the year
if self.wind_directory:
current_w_class = np.floor(np.random.uniform(0, 1, self.w_sites)*self.w_classes).astype(int) # starting wind speed class for each site (random)
# record data for plotting and exporting (optional)
self.renewable_rec = {"wind_rec": np.zeros((self.nz, self.sim_hours)), "solar_rec": np.zeros((self.nz, self.sim_hours)), "congen_temp": 0, \
"rengen_temp": 0}
SOC_old = 0.5*(np.multiply(np.multiply(self.ess_pmax, self.ess_duration), self.ess_socmax))/BMva
self.SOC_rec = np.zeros((self.ness, self.sim_hours))
self.curt_rec = np.zeros(self.sim_hours)
# gen_rec = np.zeros((sim_hours, ng))
for n in range(self.sim_hours):
# get current states(up/down) and capacities of all system components
next_state, current_cap, var_s["t_min"] = self.raut.NextState(var_s["t_min"], self.ng, self.ness, self.nl, \
self.lambda_tot, self.mu_tot, current_state, self.cap_max, self.cap_min, self.ess_units)
current_state = copy.deepcopy(next_state)
# update SOC based on failures in ESS
ess_smax, ess_smin, SOC_old = self.raut.updateSOC(self.ng, self.nl, current_cap, self.ess_pmax, self.ess_duration, self.ess_socmax, \
self.ess_socmin, SOC_old)
# calculate upper and lower bounds of gens and tls
gt_limits = {"g_lb": np.concatenate((current_cap["min"][0:self.ng]/BMva, current_cap["min"][self.ng + self.nl::]/BMva)), \
"g_ub": np.concatenate((current_cap["max"][0:self.ng]/BMva, current_cap["max"][self.ng + self.nl::]/BMva)), "tl": current_cap["max"][self.ng:self.ng + self.nl]/BMva}
def fb_Pg(model, i):
return (gt_limits["g_lb"][i], gt_limits["g_ub"][i])
def fb_flow(model,i):
return (-gt_limits["tl"][i], gt_limits["tl"][i])
def fb_ess(model, i):
return(-current_cap["max"][self.ng + self.nl::][i]/BMva, current_cap["min"][self.ng + self.nl::][i]/BMva)
def fb_soc(model, i):
return(ess_smin[i]/BMva, ess_smax[i]/BMva)
# get wind power output for all zones/areas
if self.wind_directory:
w_zones, current_w_class = self.raut.WindPower(self.nz, self.w_sites, self.zone_no, \
self.w_classes, self.r_cap, current_w_class, self.tr_mats, self.p_class, self.w_turbines, self.out_curve2, self.out_curve3)
# get solar power output for all zones/areas
if self.solar_directory:
s_zones = self.raut.SolarPower(n, self.nz, self.s_zone_no, self.solar_prob, self.s_profiles, self.s_sites, self.s_max)
# record wind and solar profiles for plotting (optional)
if self.wind_directory:
self.renewable_rec["wind_rec"][:, n] = w_zones
if self.solar_directory:
s_zones_t = np.transpose(s_zones)
self.renewable_rec["solar_rec"][:, n] = s_zones_t[:, n%24]
# recalculate net load (for distribution side resources, optional)
part_netload = 1.25*self.load_all_regions
if self.solar_directory and self.wind_directory:
net_load = part_netload[n] - w_zones - s_zones[n%24]
elif self.solar_directory==False and self.wind_directory:
net_load = part_netload[n] - w_zones
elif self.solar_directory and self.wind_directory==False:
net_load = part_netload[n] - s_zones[n%24]
elif self.solar_directory==False and self.wind_directory==False:
net_load = part_netload[n]
# optimize dipatch and calculate load curtailment
load_curt, SOC_old = self.raut.OptDispatchLite(self.ng, self.nz, self.ness, fb_ess, fb_soc, BMva, fb_Pg, A_inc,\
self.gencost, net_load, SOC_old, self.ess_pmax, self.ess_eff, self.disch_cost, self.ch_cost)
# record values for visualization purposes
self.SOC_rec[:, n] = SOC_old*BMva
self.curt_rec[n] = load_curt*BMva
# gen_rec[n] = gen[0:ng]
# track loss of load states
var_s, LOL_track = self.raut.TrackLOLStates(load_curt, BMva, var_s, LOL_track, s, n)
if (n+1)%100 == 0:
print(f'Hour {n + 1}')
# collect indices for all samples
indices_rec = self.raut.UpdateIndexArrays(indices_rec, var_s, self.sim_hours, s)
# check for convergence using LOLP and COV
indices_rec["mLOLP_rec"][s] = np.mean(indices_rec["LOLP_rec"][0:s+1])
var_LOLP = np.var(indices_rec["LOLP_rec"][0:s+1])
indices_rec["COV_rec"][s] = np.sqrt(var_LOLP)/indices_rec["mLOLP_rec"][s]
# calculate reliability indices for the MCS
indices = self.raut.GetReliabilityIndices(indices_rec, self.sim_hours, self.samples)
self.mLOLP_rec = indices_rec["mLOLP_rec"]
self.COV_rec = indices_rec["COV_rec"]
print("Simulation complete! You can view the results now by clicking next! Plots are also saved to the results folder.")
self.ui.pushButton_6.setVisible(True)
self.main_folder = os.path.dirname(os.path.abspath(__file__))
self.results_dir = os.path.join(self.main_folder, 'Results')
if not os.path.exists(f"{self.main_folder}/Results"):
os.makedirs(f"{self.main_folder}/Results")
df = pd.DataFrame([indices])
df.to_csv(f"{self.main_folder}/Results/indices.csv", index=False)
if self.sim_hours == 8760:
self.raut.OutageHeatMap(LOL_track, 1, self.samples, self.main_folder)
def plot(self):
if self.plot_count == 0:
self.plot_count = 1
else:
rapt = RAPlotTools(self.main_folder)
rapt.PlotSolarGen(self.renewable_rec["solar_rec"], self.bus_name)
rapt.PlotWindGen(self.renewable_rec["wind_rec"], self.bus_name)
rapt.PlotSOC(self.SOC_rec, self.essname)
rapt.PlotLoadCurt(self.curt_rec)
rapt.PlotLOLP(self.mLOLP_rec, self.samples, 1)
rapt.PlotCOV(self.COV_rec, self.samples, 1)
if self.sim_hours == 8760:
rapt.OutageMap(f"{self.main_folder}/Results/LOL_perc_prob.csv")
#self.ui.textBrowser_2.append("Plotting complete, view plots by clicking next. Plots are also saved in the Results folder.")
#QMessageBox.information(self, "Plots", "Plotting complete, view plots in the Results folder.")
#self.open_folder_in_explorer(self.results_dir)
self.load_plots()
self.load_csv_files()
self.plot_count = 0
def load_plots(self):
# List of test graphs and their corresponding layout
graphs = [
("solar_generation.pdf", self.ui.verticalLayout_55),
("COV_track.pdf", self.ui.verticalLayout_46),
("loadcurt.pdf", self.ui.verticalLayout_49),
("LOLP_track.pdf", self.ui.verticalLayout_51),
("SOC.pdf", self.ui.verticalLayout_53),
("wind_generation.pdf", self.ui.verticalLayout_59),
("heatmap.pdf", self.ui.verticalLayout_47),
]
# Remove existing PDF viewers if they exist
for graph_name, layout in graphs:
viewer_attr_name = f"pdf_viewer_{graph_name.split('.')[0]}"
if hasattr(self, viewer_attr_name):
viewer = getattr(self, viewer_attr_name)
layout.removeWidget(viewer.get_pdf_view())