diff --git a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/simulation.py b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/simulation.py index 219b6b4cdded6b4429e427d03846abb46e267df3..ff113dd81b8a4f32108cb792c3a925779979d2c5 100644 --- a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/simulation.py +++ b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/simulation.py @@ -361,7 +361,6 @@ class Simulation(object): :param script_type: The type of the script to be retrieved (transfer-function, brain, state-machine) """ - assert isinstance(script_name, str) script_type_print_format = ' '.join(script_type.split('-')).title() @@ -555,57 +554,51 @@ class Simulation(object): self.__logger.info(err) raise Exception("Failed to save %s." % experiment_data_type) - def save_transfer_functions(self, transfer_functions_list): + def save_transfer_functions(self): """ - Saves the transfer functions in the storage - - :param transfer_functions_list: List[str] of strings where each string is the code of a - transfer function + Saves the current transfer functions to the storage """ + transfer_functions = self.__get_simulation_scripts('transfer-function')['data'].keys() + transfer_functions_list = [] + for tf in transfer_functions: + transfer_functions_list.append(self.get_transfer_function(str(tf))) - assert isinstance(transfer_functions_list, list) + self.__save_experiment_data('transfer-function', + {'experimentId': self.__experiment_id, + 'transfer_functions': transfer_functions_list}) - return self.__save_experiment_data('transfer-function', { - 'experimentId': self.__experiment_id, - 'transfer_functions': transfer_functions_list - }) - - def save_state_machines(self, state_machines_list): + def save_state_machines(self): """ - Saves the state machines in the storage - - :param state_machines_list: Dict[str, str] of the state machines with key being the state - machine name and the value is the state machine code + Saves the current state machines to the storage """ + state_machines_dict = {} + for sm in self.__get_simulation_scripts('state-machine')['data'].keys(): + state_machines_dict[sm] = self.get_state_machine(str(sm)) - assert isinstance(state_machines_list, dict) + self.__save_experiment_data('state-machine', {'experimentId': self.__experiment_id, + 'state_machines': state_machines_dict}) - return self.__save_experiment_data('state-machine', { - 'experimentId': self.__experiment_id, - 'state_machines': state_machines_list - }) - - def save_brain(self, populations, pynn_script): + def save_brain(self): """ - Saves the populations and pynn_script - - :param populations: List of populations. - A population can be a list, with the format: {name:str, list:[int]} (list has neurons ids) - A population can be a slice, with the format: {name:str, from: int, to: int, step: int} - :param pynn_script: string with the PyNN script + Saves the current brain script and populations to the storage """ + pynn_script = self.get_brain() + populations = self.get_populations() - assert isinstance(populations, list) - assert isinstance(pynn_script, str) + # Remove the regex entry from the populations dictionary if it is available. The regex + # entry is provided by the frontend to check for duplicate population names and should be + # refactored. + if 'regex' in populations.values()[0]: + for pop in populations: + del populations[pop]['regex'] - return self.__save_experiment_data('brain', { - 'data': pynn_script, - 'additional_populations': populations - }) + self.save_transfer_functions() + self.__save_experiment_data('brain', {'data': pynn_script, + 'additional_populations': populations}) def save_csv(self): """ - Saves the recorded csv data + Saves the recorded csv data to storage """ if not self.__sim_url: @@ -625,9 +618,8 @@ class Simulation(object): def save_world(self): """ - Saves the world + Saves the current sdf world to the storage """ - return self.__save_experiment_data('sdf-world', {}, method='post') def get_state_machine(self, state_machine_name): diff --git a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_simulation.py b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_simulation.py index c5ebb2d21109364802155745962ffd38711186e8..54a8fecf4fd9dd9ac2a96de025352b646584edf9 100644 --- a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_simulation.py +++ b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_simulation.py @@ -144,7 +144,6 @@ class TestSimulation(unittest.TestCase): sim._Simulation__set_state = Mock() self.assertEqual(sim.launch('id', 'conf', 'server-name', 'reservation'), True) - #self.assertEqual(sim._Simulation__oidc_client.request.call_count, 2) # calling launch twice on an instance should fail after successful creation self.assertRaises(Exception, sim.launch, 'id', 'conf', 'server-name', 'reservation') @@ -590,6 +589,16 @@ class TestSimulation(unittest.TestCase): exp_id = 'exp_id' serverurl = 'serverurl' + + sim._Simulation__get_simulation_scripts = Mock() + sim._Simulation__get_simulation_scripts.return_value = {'data': {'foo': ''}} + + sim.get_transfer_function = Mock() + sim.get_transfer_function.return_value = 'bar' + + sim.get_state_machine = Mock() + sim.get_state_machine.return_value = 'bar' + sim._Simulation__experiment_id = exp_id sim._Simulation__server_info = { 'gzweb':{ @@ -607,65 +616,44 @@ class TestSimulation(unittest.TestCase): res_NOK = Response(500) mock_put.return_value = res_NOK - tfs = ["""@nrp.Robot2Neuron() -def benchmark_evaluation(t): - pass -""", -"""@nrp.Robot2Neuron() -def benchmark_evaluation2(t): - pass -"""] - - self.assertRaises(Exception, sim.save_transfer_functions, tfs) - mock_put.return_value = res_OK mock_put.reset_mock() - sim.save_transfer_functions(tfs) - mock_put.assert_called_once_with('%s/experiment/%s/transfer-functions' % (serverurl, exp_id), - headers=sim._Simulation__headers, - json={'transfer_functions': tfs, 'experimentId': exp_id}) - + sim.save_transfer_functions() + mock_put.assert_called_once('%s/experiment/%s/state-machines' % (serverurl, exp_id), + headers=sim._Simulation__headers, + json={'transfer_functions': 'bar', + 'experimentId': exp_id}) mock_put.reset_mock() - state_machines = { - 'state_machine_0': '# bla bla 0', - 'state_machine_1': '# bla bla 1' - } - - sim.save_state_machines(state_machines) - + sim.save_state_machines() mock_put.assert_called_once_with('%s/experiment/%s/state-machines' % (serverurl, exp_id), headers=sim._Simulation__headers, - json={'state_machines': state_machines, + json={'state_machines': {'foo': 'bar'}, 'experimentId': exp_id}) - mock_put.reset_mock() - populations = [ - { - 'name': 'pop1', - 'from': 0, - 'to': 1, - 'step': 1, - 'regex':"^\b(?!\bpop0\b)([A-z_]+[\w_]*)$" - } - ] + populations = { + 'pop1': {'from': 0, 'to': 1, 'step': 1, 'regex': "^\b(?!\bpop0\b)([A-z_]+[\w_]*)$"}} - brain = 'some brain code' - sim.save_brain(populations, brain) + sim.get_brain = Mock() + sim.get_brain.return_value = 'some brain code' + + sim.get_populations = Mock() + sim.get_populations.return_value = populations + sim.save_transfer_functions = Mock() + sim.save_brain() mock_put.assert_called_once_with('%s/experiment/%s/brain' % (serverurl, exp_id), headers=sim._Simulation__headers, json={'additional_populations': populations, - 'data': brain}) + 'data': 'some brain code'}) mock_post.return_value = res_OK sim.save_world() mock_post.assert_called_once_with('%s/experiment/%s/sdf_world' % (serverurl, exp_id), - headers=sim._Simulation__headers, - json={}) + headers=sim._Simulation__headers, json={}) mock_put.reset_mock() mock_put.return_value = res_NOK