Skip to content

Commit 06cb335

Browse files
committed
Added add individuals from text file functionality
* #48
1 parent 11cd78b commit 06cb335

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

MLC/GUI/Experiment/ExperimentWindow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def on_first_add_indiv_from_textfile_button_clicked(self):
399399
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - '
400400
'Executing on_first_add_indiv_from_textfile_button_clicked function'
401401
.format(self._experiment_name))
402+
self._first_indivs_manager.add_individuals_from_textfile()
402403

403404
def on_first_remove_indiv_button_clicked(self):
404405
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - '

MLC/GUI/Experiment/FirstIndividualsManager.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from MLC.Population.Creation.IndividualSelection import IndividualSelection
77
from MLC.Population.Creation.CreationFactory import CreationFactory
88
from PyQt5.QtWidgets import QInputDialog
9+
from PyQt5.QtWidgets import QFileDialog
910
from PyQt5.QtWidgets import QMessageBox
1011

1112
logger = 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)

MLC/GUI/util.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_individual_value(parent, experiment_name, log_prefix, indiv_value, conf
5050

5151

5252

53-
def check_individual_value(parent, experiment_name, log_prefix, indiv_value):
53+
def check_individual_value(parent, experiment_name, log_prefix, indiv_value, nodialog=False):
5454
try:
5555
"""
5656
Evaluate an individual in order to check its correctness. Handle Exceptions
@@ -60,11 +60,12 @@ def check_individual_value(parent, experiment_name, log_prefix, indiv_value):
6060
except ExprException, err:
6161
# Print the error message returned in the exception,
6262
# removing the prefix ([EXPR_EXCEPTION]])
63-
QMessageBox.critical(parent,
64-
"Invalid Individsual",
65-
"Individual inserted is not well-formed. "
66-
"Error Msg: {0}"
67-
.format(err.message[err.message.find(']') + 2:]))
63+
if not nodialog:
64+
QMessageBox.critical(parent,
65+
"Invalid Individsual",
66+
"Individual inserted is not well-formed. "
67+
"Error Msg: {0}"
68+
.format(err.message[err.message.find(']') + 2:]))
6869
logger.error("{0} Experiment {1} - "
6970
"Individual inserted is not well-formed. "
7071
"Error Msg: {2}"

0 commit comments

Comments
 (0)