44import numpy as np
55import sys
66import time
7- sys .path .append (os .path .abspath ("." ) + "/../.." )
7+ # sys.path.append(os.path.abspath(".") + "/../..")
88
99from MLC .Log .log import get_gui_logger
1010from MLC .GUI .Autogenerated .autogenerated import Ui_ExperimentWindow
2323from PyQt5 .QtCore import pyqtSignal
2424from PyQt5 .QtCore import Qt
2525from PyQt5 .QtCore import QUrl
26+ from PyQt5 .QtCore import QFileSystemWatcher
2627from PyQt5 .QtGui import QDesktopServices
2728from PyQt5 .QtWidgets import QAbstractItemView
2829from PyQt5 .QtWidgets import QMainWindow
@@ -73,6 +74,16 @@ def __init__(self, mlc_local,
7374 # Signal to be emitted when the experiment is closed
7475 self ._experiment_closed_signal = experiment_closed_signal
7576
77+ # Watch changes in the experiment config file
78+ self ._file_watcher = QFileSystemWatcher ()
79+ conf_path_list = [self ._mlc_local .get_working_dir (),
80+ self ._experiment_name ,
81+ self ._experiment_name + ".conf" ]
82+ # Use the splat operator to dearrange the list
83+ self ._experiment_conf_path = os .path .join (* conf_path_list )
84+ self ._file_watcher .addPath (self ._experiment_conf_path )
85+ self ._file_watcher .fileChanged .connect (self ._reload_experiment_config )
86+
7687 # Experiment in progress chart configuration
7788 self ._chart_conf = ChartConfiguration (self ._autogenerated_object )
7889 indivs_per_gen = Config .get_instance ().getint ("POPULATION" , "size" )
@@ -204,7 +215,12 @@ def on_dimension_check_clicked(self):
204215 def on_save_config_button_clicked (self ):
205216 logger .debug ('[EXPERIMENT {0}] [SAVE_CONFIG_BUTTON_CLICKED] - Executing on_save_config_button_clicked function'
206217 .format (self ._experiment_name ))
218+
219+ # Remove the config file from the Qt File watcher momentarily
220+ # to avoid rendering unnecesary warnings
221+ self ._file_watcher .removePath (self ._experiment_conf_path )
207222 self ._persist_experiment_config ()
223+ self ._file_watcher .addPath (self ._experiment_conf_path )
208224
209225 def on_tab_changed (self , tab_index ):
210226 logger .debug ('[EXPERIMENT {0}] [TAB_CHANGED] - Executing on_tab_changed function'
@@ -313,6 +329,12 @@ def on_board_config_button_clicked(self):
313329 board_config_window = BoardConfigurationWindow (parent = self )
314330 board_config_window .show ()
315331
332+ def on_edit_config_button_clicked (self ):
333+ logger .debug ('[EXPERIMENT {0}] [EDIT_CONFIG_BUTTON] - '
334+ 'Executing on_edit_config_button_clicked function'
335+ .format (self ._experiment_name ))
336+ QDesktopServices .openUrl (QUrl (self ._experiment_conf_path ))
337+
316338 def _config_table_edited (self , left , right ):
317339 config_table = self ._autogenerated_object .config_table
318340 table_model = config_table .model ()
@@ -472,15 +494,23 @@ def _update_experiment_info(self):
472494 number_of_gens = experiment_info ["generations" ]
473495 if number_of_gens == 0 :
474496 from_gen_combo .addItems ([str (1 )])
475- to_gen_combo .addItems ([str (x ) for x in xrange (2 , ExperimentWindow .MAX_GENERATIONS )])
497+ to_gen_combo .addItems ([str (x ) for x in xrange (2 , ExperimentWindow .MAX_GENERATIONS + 1 )])
476498 else :
477499 from_gen_combo .addItems ([str (x ) for x in xrange (1 , number_of_gens + 1 )])
478- to_gen_combo .addItems ([str (x ) for x in xrange (2 , ExperimentWindow .MAX_GENERATIONS )])
500+ to_gen_combo .addItems ([str (x ) for x in xrange (2 , ExperimentWindow .MAX_GENERATIONS + 1 )])
479501
480502 # Set the from gen combo to the last generation evaluated
481503 index = from_gen_combo .findText (str (number_of_gens ), Qt .MatchFixedString )
482504 from_gen_combo .setCurrentIndex (index )
483505
506+ # set the to_gen_combo to the lest generation evaluated + 1
507+ to_gen_index = number_of_gens + 1
508+ if number_of_gens == ExperimentWindow .MAX_GENERATIONS :
509+ to_gen_index = ExperimentWindow .MAX_GENERATIONS
510+
511+ index = to_gen_combo .findText (str (to_gen_index ), Qt .MatchFixedString )
512+ to_gen_combo .setCurrentIndex (index )
513+
484514 # Fill the db_view
485515 gen_count_group = self ._autogenerated_object .gen_count_group
486516 if number_of_gens != 0 :
@@ -557,6 +587,23 @@ def _update_individuals_figure(self):
557587 # Add the Indiv Canvas
558588 chart_layout .addWidget (indiv_canvas )
559589
590+ def _reload_experiment_config (self , some_string ):
591+ logger .debug ('[EXPERIMENT {0}] [EXPERIMENT_CONF_FILE_CHANGED] - '
592+ 'Executing _experiment_conf_file_changed function'
593+ .format (self ._experiment_name ))
594+
595+ self ._mlc_local .reload_experiment_configuration (self ._experiment_name )
596+ self ._load_experiment_config ()
597+
598+ QMessageBox .warning (self , "Experiment Config Changed" , "The Experiment config file "
599+ "has changed. The configuration will be reloaded. "
600+ "The changes made will be discarded" )
601+ logger .info ('[EXPERIMENT {0}] [RELOAD_CONFIG] - Config file has changed. Configuration will be reloaded' )
602+ self ._autogenerated_object .save_config_button .setDisabled (True )
603+ # FIXME: Dunno why?, but I have to add the config file to the
604+ # watcher in order to work again the next time
605+ self ._file_watcher .addPath (self ._experiment_conf_path )
606+
560607 def _persist_experiment_config (self ):
561608 try :
562609 self ._mlc_local .set_experiment_configuration (self ._experiment_name , self ._experiment_config )
@@ -579,7 +626,9 @@ def _ask_if_experiment_config_must_be_saved(self):
579626 QMessageBox .Yes | QMessageBox .No ,
580627 QMessageBox .Yes )
581628 if response == QMessageBox .Yes :
629+ self ._file_watcher .removePath (self ._experiment_conf_path )
582630 self ._persist_experiment_config ()
631+ self ._file_watcher .addPath (self ._experiment_conf_path )
583632 else :
584633 self ._load_experiment_config ()
585634
0 commit comments