66from MLC .Population .Creation .IndividualSelection import IndividualSelection
77from MLC .Population .Creation .CreationFactory import CreationFactory
88from PyQt5 .QtWidgets import QInputDialog
9+ from PyQt5 .QtWidgets import QFileDialog
910from PyQt5 .QtWidgets import QMessageBox
1011
1112logger = get_gui_logger ()
@@ -59,6 +60,57 @@ def add_individuals_from_textfile(self):
5960 logger .debug ('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - '
6061 'Executing add_individuals_from_textfile function'
6162 .format (self ._experiment_name ))
63+ # Get the path of the experiment to import
64+ indivs_file = QFileDialog .getOpenFileName (self ._parent , "Import Individuals" , "." ,
65+ "Text File (*.txt)" )[0 ]
66+ if not indivs_file :
67+ # User clicked 'Cancel' or simply closed the Dialog
68+ return
69+
70+ # Get the individuals. Be sure to remove the end of line from every line
71+ indivs = None
72+ with open (indivs_file ) as f :
73+ indivs = f .readlines ()
74+ indivs = [x .strip () for x in indivs ]
75+
76+ amount_indivs = len (indivs )
77+ if amount_indivs == 0 :
78+ logger .debug ('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - Indivs from Textfile: '
79+ 'The file {1} was empty. Nothing to do'
80+ .format (self ._experiment_name , indivs_file ))
81+ QMessageBox .information (self , "Add individuals from textfile" ,
82+ 'The file {0} was empty. Nothing to do'
83+ .format (indivs_file ))
84+ return
85+
86+ counter = 0
87+ for indiv in indivs :
88+ if check_individual_value (parent = self ._parent ,
89+ experiment_name = self ._experiment_name ,
90+ log_prefix = "[FIRST_INDIVS_MANAGER]" ,
91+ indiv_value = indiv ,
92+ nodialog = True ):
93+ self ._individuals .append (indiv )
94+ counter += 1
95+
96+ logger .info ('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - Indivs from Textfile: '
97+ '{0} out of {1} individuals has been inserted.'
98+ .format (self ._experiment_name , counter , amount_indivs ))
99+
100+ if counter == 0 :
101+ QMessageBox .critical (self ._parent , "Individuals from textfile" ,
102+ "No individual could be inserted. Check them to be well-formed." )
103+ return
104+
105+ if amount_indivs == counter :
106+ QMessageBox .information (self ._parent , "Individuals from textfile" ,
107+ "{0} individuals has been succesfully inserted"
108+ .format (counter ))
109+ else :
110+ QMessageBox .information (self ._parent , "Individuals from textfile" ,
111+ "{0} out of {1} individuals has been succesfully inserted"
112+ .format (counter , amount_indivs ))
113+ self ._load_table ()
62114
63115 def modify_individual (self , left , right ):
64116 logger .debug ('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - '
@@ -74,10 +126,10 @@ def modify_individual(self, left, right):
74126 new_value = table_model .get_data (left .row (), left .column ())
75127
76128 # Check if the value of the new individual is valid
77- valid_indiv = check_individual_value (parent = self ._parent ,
78- experiment_name = self ._experiment_name ,
79- log_prefix = "[FIRST_INDIVS_MANAGER]" ,
80- indiv_value = new_value )
129+ valid_indiv = check_individual_value (parent = self ._parent ,
130+ experiment_name = self ._experiment_name ,
131+ log_prefix = "[FIRST_INDIVS_MANAGER]" ,
132+ indiv_value = new_value )
81133 if not valid_indiv :
82134 table_model .set_data (left .row (), left .column (), old_value )
83135 return
@@ -148,6 +200,8 @@ def get_gen_creator(self):
148200 indivs_dict = {}
149201 for index in xrange (len (self ._individuals )):
150202 indiv = Individual (self ._individuals [index ])
203+ print type (indiv )
204+ print indiv .get_value ()
151205 indivs_dict [indiv ] = [index ]
152206
153207 return IndividualSelection (indivs_dict , fill_creator )
0 commit comments