1818
1919import ConfigParser
2020
21+
2122class MLCWorkspaceTest (unittest .TestCase ):
2223 WORKSPACE_DIR = os .path .abspath ("/tmp/mlc_workspace/" )
23- DEFAULT_CONFIGURATION = {"BEHAVIOUR" : {"save_dir" }}
24-
24+ FILE_WITH_RANDOMS = None
2525 ORIGINAL_EXPERIMENT = "test_first_experiment"
2626 ORIGINAL_CONFIGURATION = None
27+ NEW_EXPERIMENT = "test_new_experiment"
28+ NEW_CONFIGURATION = None
2729
28- NEW_EXPERIMENT = "new_experiment"
29- NEW_CONFIGURATION = {"PARAMS" : {"test_param" : "test_value" }}
30-
31- FILE_WITH_RANDOMS = None
3230
3331 @classmethod
3432 def setUpClass (cls ):
33+ os .makedirs (MLCWorkspaceTest .WORKSPACE_DIR )
34+ mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
3535 # general settings for MLC
3636 set_logger ('console' )
3737 set_working_directory (MLCWorkspaceTest .WORKSPACE_DIR )
3838
3939 if not os .path .exists (MLCWorkspaceTest .WORKSPACE_DIR ):
4040 os .makedirs (MLCWorkspaceTest .WORKSPACE_DIR )
4141
42- # copy initial configuration file
43- experiment_cf , experiment_db = Experiment .get_experiment_files ( MLCWorkspaceTest .WORKSPACE_DIR ,
44- MLCWorkspaceTest .ORIGINAL_EXPERIMENT )
45-
4642 this_dir = os .path .dirname (os .path .abspath (__file__ ))
47- shutil .copy (os .path .join (this_dir , MLCWorkspaceTest .ORIGINAL_EXPERIMENT + ".conf" ), experiment_cf )
48-
49- # read configuration
50- original_config = ConfigParser .ConfigParser ()
51- original_config .read (os .path .join (this_dir , MLCWorkspaceTest .ORIGINAL_EXPERIMENT + ".conf" ))
52- MLCWorkspaceTest .ORIGINAL_CONFIGURATION = Config .to_dictionary (original_config )
53-
54- # create experiment database
55- Config .get_instance ().read (experiment_cf )
56- s = Simulation ()
43+ MLCWorkspaceTest .ORIGINAL_CONFIGURATION = \
44+ os .path .join (this_dir , MLCWorkspaceTest .ORIGINAL_EXPERIMENT + ".conf" )
45+ MLCWorkspaceTest .NEW_CONFIGURATION = \
46+ os .path .join (this_dir , MLCWorkspaceTest .NEW_EXPERIMENT + ".conf" )
47+ mlc .new_experiment (MLCWorkspaceTest .ORIGINAL_EXPERIMENT ,
48+ MLCWorkspaceTest .ORIGINAL_CONFIGURATION )
5749
5850 # random file for simulations
59- this_dir = os .path .dirname (os .path .abspath (__file__ ))
6051 MLCWorkspaceTest .FILE_WITH_RANDOMS = os .path .join (this_dir , "matlab_randoms.txt" )
6152
53+ @classmethod
54+ def teardownClass (cls ):
55+ shutil .rmtree (MLCWorkspaceTest .WORKSPACE_DIR )
56+
6257 def test_obtain_experiments (self ):
6358 mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
6459 experiments = mlc .get_workspace_experiments ()
6560 self .assertEqual (len (experiments ), 1 )
6661 self .assertEqual (experiments [0 ], MLCWorkspaceTest .ORIGINAL_EXPERIMENT )
6762
6863 def test_obtain_configuration_from_a_closed_experiment (self ):
69- mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
7064 try :
65+ mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
7166 mlc .get_experiment_configuration (MLCWorkspaceTest .ORIGINAL_EXPERIMENT )
7267 self .assertTrue (False , "Configuration from a closed experiment should not be obtained" )
7368 except MLC .api .mlc .ClosedExperimentException :
7469 self .assertTrue (True )
7570
7671 def test_open_invalid_experiment (self ):
77- mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
7872 try :
73+ mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
7974 mlc .open_experiment ("invalid_name" )
8075 self .assertTrue (False )
8176 except MLC .api .mlc .ExperimentNotExistException :
@@ -106,17 +101,21 @@ def test_open_and_close_experiment(self):
106101 def test_create_duplicated_experiment (self ):
107102 try :
108103 mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
109- mlc .new_experiment (MLCWorkspaceTest .ORIGINAL_EXPERIMENT , MLCWorkspaceTest .DEFAULT_CONFIGURATION )
104+ mlc .new_experiment (MLCWorkspaceTest .ORIGINAL_EXPERIMENT ,
105+ MLCWorkspaceTest .ORIGINAL_CONFIGURATION )
110106 self .assertTrue (False )
111107 except MLC .api .mlc .DuplicatedExperimentError :
112108 self .assertTrue (True )
113109
114110 def test_create_experiment_and_obtain_configuration (self ):
115111 try :
116112 mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
117- mlc .new_experiment (MLCWorkspaceTest .NEW_EXPERIMENT , MLCWorkspaceTest .NEW_CONFIGURATION )
113+ mlc .new_experiment (MLCWorkspaceTest .NEW_EXPERIMENT ,
114+ MLCWorkspaceTest .NEW_CONFIGURATION )
118115 mlc .open_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
119116 configuration = mlc .get_experiment_configuration (MLCWorkspaceTest .NEW_EXPERIMENT )
117+ print configuration
118+ mlc .close_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
120119
121120 # check configuration structure
122121 self .assertIsInstance (configuration , dict )
@@ -127,34 +126,24 @@ def test_create_experiment_and_obtain_configuration(self):
127126
128127 finally :
129128 # FIXME: use Setup/TearDown testcase
130- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , MLCWorkspaceTest .NEW_EXPERIMENT )+ ".conf" )
131- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , MLCWorkspaceTest .NEW_EXPERIMENT )+ ".mlc" )
132-
129+ mlc .delete_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
133130
134131 def test_delete_experiment (self ):
135- mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
136-
132+ # delete an experiment that not exists
137133 try :
138- # delete an experiment that not exists
139- try :
140- mlc .delete_experiment_from_workspace (MLCWorkspaceTest .NEW_EXPERIMENT )
141- self .assertTrue (False )
142- except MLC .api .mlc .ExperimentNotExistException :
143- self .assertTrue (True )
144-
145- mlc .new_experiment (MLCWorkspaceTest .NEW_EXPERIMENT , MLCWorkspaceTest .NEW_CONFIGURATION )
146- mlc .delete_experiment_from_workspace (MLCWorkspaceTest .NEW_EXPERIMENT )
147- self .assertFalse (os .path .exists (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , MLCWorkspaceTest .NEW_EXPERIMENT )+ ".conf" ))
148- self .assertFalse (os .path .exists (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , MLCWorkspaceTest .NEW_EXPERIMENT )+ ".mlc" ))
134+ mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
135+ mlc .delete_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
136+ self .assertTrue (False )
137+ except MLC .api .mlc .ExperimentNotExistException :
138+ self .assertTrue (True )
149139
150- finally :
151- pass
152- # FIXME: use Setup/TearDown testcase
153- # os.unlink (os.path.join(MLCWorkspaceTest.WORKSPACE_DIR, MLCWorkspaceTest.NEW_EXPERIMENT) + ".conf")
154- # os.unlink(os.path.join(MLCWorkspaceTest.WORKSPACE_DIR, MLCWorkspaceTest.NEW_EXPERIMENT) + ".mlc" )
140+ mlc . new_experiment ( MLCWorkspaceTest . NEW_EXPERIMENT ,
141+ MLCWorkspaceTest . NEW_CONFIGURATION )
142+ mlc . delete_experiment ( MLCWorkspaceTest . NEW_EXPERIMENT )
143+ self . assertFalse ( os .path . exists (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR ,
144+ MLCWorkspaceTest .NEW_EXPERIMENT )) )
155145
156146 def test_set_configuration (self ):
157-
158147 try :
159148 mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
160149
@@ -163,25 +152,30 @@ def test_set_configuration(self):
163152 mlc .open_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
164153 original_config = mlc .get_experiment_configuration (MLCWorkspaceTest .NEW_EXPERIMENT )
165154 self .assertEqual (original_config ["PARAMS" ]["test_param" ], "test_value" )
166-
167155 # chage paramenter value
168156 mlc .set_experiment_configuration (MLCWorkspaceTest .NEW_EXPERIMENT , {"PARAMS" : {"test_param" : "new_value" }})
157+ mlc .close_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
169158
170159 # reload mlc_workspace
171160 mlc_reloaded = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
172- mlc .open_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
173- original_config = mlc .get_experiment_configuration (MLCWorkspaceTest .NEW_EXPERIMENT )
161+ mlc_reloaded .open_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
162+ original_config = mlc_reloaded .get_experiment_configuration (MLCWorkspaceTest .NEW_EXPERIMENT )
174163 self .assertEqual (original_config ["PARAMS" ]["test_param" ], "new_value" )
164+ mlc_reloaded .close_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
175165
176166 # set specific parameter
177167 mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
178168 mlc .open_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
179- mlc .set_experiment_configuration_parameter (MLCWorkspaceTest .NEW_EXPERIMENT , "another_section" , "another_param" , "another_value" )
169+ mlc .set_experiment_configuration_parameter (MLCWorkspaceTest .NEW_EXPERIMENT ,
170+ "another_section" , "another_param" ,
171+ "another_value" )
172+ mlc .close_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
180173
181174 # reload mlc_workspace
182175 mlc_reloaded = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
183176 mlc_reloaded .open_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
184177 config = mlc_reloaded .get_experiment_configuration (MLCWorkspaceTest .NEW_EXPERIMENT )
178+ mlc_reloaded .close_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
185179 self .assertEqual (config ["PARAMS" ]["test_param" ], "new_value" )
186180
187181 self .assertIn ("another_section" , config )
@@ -190,8 +184,7 @@ def test_set_configuration(self):
190184
191185 finally :
192186 # FIXME: use Setup/TearDown testcase
193- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , MLCWorkspaceTest .NEW_EXPERIMENT ) + ".conf" )
194- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , MLCWorkspaceTest .NEW_EXPERIMENT ) + ".mlc" )
187+ mlc .delete_experiment (MLCWorkspaceTest .NEW_EXPERIMENT )
195188 pass
196189
197190 def test_get_info_empty_simulation (self ):
@@ -200,7 +193,7 @@ def test_get_info_empty_simulation(self):
200193 info = mlc .get_experiment_info (MLCWorkspaceTest .ORIGINAL_EXPERIMENT )
201194
202195 self ._assert_key_value (info , "name" , MLCWorkspaceTest .ORIGINAL_EXPERIMENT )
203- self ._assert_key_value (info , "filename" , MLCWorkspaceTest .ORIGINAL_EXPERIMENT + ".mlc" )
196+ self ._assert_key_value (info , "filename" , MLCWorkspaceTest .ORIGINAL_EXPERIMENT + ".mlc" )
204197 self ._assert_key_value (info , "generations" , 0 )
205198 self ._assert_key_value (info , "individuals" , 0 )
206199 self ._assert_key_value (info , "individuals_per_generation" , 10 )
@@ -260,47 +253,46 @@ def test_go_and_get_individuals(self):
260253 indiv_data = mlc .get_individuals ("test_go_and_check" )[2 ]
261254 self .assertEqual (indiv_data .get_appearances (), 2 )
262255 self .assertEqual (indiv_data .get_cost_history (), {1 : [(1000.0 , 1001 )], 2 : [(1000.0 , 1001 )]})
256+ mlc .close_experiment ("test_go_and_check" )
263257 finally :
264258 # FIXME: use Setup/TearDown testcase
265- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , "test_go_and_check" ) + ".conf" )
266- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , "test_go_and_check" ) + ".mlc" )
267- pass
259+ mlc .delete_experiment ("test_go_and_check" )
268260
269261 @nottest
270262 def test_go_and_get_generations (self ):
271- try :
263+ try :
272264 # load random values for the simulation
273- self ._load_random_values ()
274-
275- # Execute a simulation
276- mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
277- mlc .new_experiment ("test_go_and_check" , MLCWorkspaceTest .ORIGINAL_CONFIGURATION )
278- mlc .open_experiment ("test_go_and_check" )
279- mlc .go ("test_go_and_check" , 2 )
280-
281- # get first population
282- first_generation = mlc .get_generation ("test_go_and_check" , 1 )
283- self .assertIsInstance (first_generation , MLCPopulation )
284-
285- # get second generation
286- second_generation = mlc .get_generation ("test_go_and_check" , 2 )
287- self .assertIsInstance (second_generation , MLCPopulation )
288-
289- # third generation does not exist and must raise an Exception
290- # TODO: Use a specific exception instead of IndexError
291-
292- try :
293- third_generation = mlc .get_generation ("test_go_and_check" , 3 )
294- self .assertIsInstance (third_generation , MLCPopulation )
295- self .assertTrue (False )
296- except IndexError :
297- self .assertTrue (True )
298-
299- finally :
300- # FIXME: use Setup/TearDown testcase
301- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , "test_go_and_check" ) + ".conf" )
302- os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , "test_go_and_check" ) + ".mlc" )
303- pass
265+ self ._load_random_values ()
266+
267+ # Execute a simulation
268+ mlc = MLCLocal (working_dir = MLCWorkspaceTest .WORKSPACE_DIR )
269+ mlc .new_experiment ("test_go_and_check" , MLCWorkspaceTest .ORIGINAL_CONFIGURATION )
270+ mlc .open_experiment ("test_go_and_check" )
271+ mlc .go ("test_go_and_check" , 2 )
272+
273+ # get first population
274+ first_generation = mlc .get_generation ("test_go_and_check" , 1 )
275+ self .assertIsInstance (first_generation , MLCPopulation )
276+
277+ # get second generation
278+ second_generation = mlc .get_generation ("test_go_and_check" , 2 )
279+ self .assertIsInstance (second_generation , MLCPopulation )
280+
281+ # third generation does not exist and must raise an Exception
282+ # TODO: Use a specific exception instead of IndexError
283+
284+ try :
285+ third_generation = mlc .get_generation ("test_go_and_check" , 3 )
286+ self .assertIsInstance (third_generation , MLCPopulation )
287+ self .assertTrue (False )
288+ except IndexError :
289+ self .assertTrue (True )
290+
291+ finally :
292+ # FIXME: use Setup/TearDown testcase
293+ os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , "test_go_and_check" ) + ".conf" )
294+ os .unlink (os .path .join (MLCWorkspaceTest .WORKSPACE_DIR , "test_go_and_check" ) + ".mlc" )
295+ pass
304296
305297 def _assert_key_value (self , dictionary , key , value ):
306298 self .assertIsInstance (dictionary , dict )
0 commit comments