Example #1
0
 def transactional_setup_method(self):
     """
     Sets up the environment for testing;
     creates a `RegionsModelParametersController` and a connectivity
     """
     self.init()
     self.region_m_p_c = RegionsModelParametersController()
     SimulatorController().index()
     self.simulator = cherrypy.session[common.KEY_SIMULATOR_CONFIG]
     self._setup_dynamic()
Example #2
0
 def transactional_setup_method(self):
     """
     Sets up the environment for testing;
     creates a `RegionsModelParametersController` and a connectivity
     """
     self.init()
     self.region_m_p_c = RegionsModelParametersController()
     simulator_controller = SimulatorController()
     simulator_controller.index()
     self.simulator = simulator_controller.context.simulator
     self._setup_dynamic()
 def transactional_setup_fixture(self, connectivity_factory):
     """
     Sets up the environment for testing;
     creates a `RegionsModelParametersController` and a connectivity
     """
     self.init()
     self.region_m_p_c = RegionsModelParametersController()
     BurstController().index()
     stored_burst = cherrypy.session[common.KEY_BURST_CONFIG]
     _, self.connectivity = connectivity_factory
     new_params = {}
     for key, val in SIMULATOR_PARAMETERS.items():
         new_params[key] = {'value': val}
     new_params['connectivity'] = {'value': self.connectivity.gid}
     stored_burst.simulator_configuration = new_params
     self._setup_dynamic()
Example #4
0
def init_cherrypy(arguments=None):
    #### Mount static folders from modules marked for introspection
    arguments = arguments or []
    CONFIGUER = TvbProfile.current.web.CHERRYPY_CONFIGURATION
    for module in arguments:
        module_inst = importlib.import_module(str(module))
        module_path = os.path.dirname(os.path.abspath(module_inst.__file__))
        CONFIGUER["/static_" + str(module)] = {'tools.staticdir.on': True,
                                               'tools.staticdir.dir': '.',
                                               'tools.staticdir.root': module_path}

    #### Mount controllers, and specify the root URL for them.
    cherrypy.tree.mount(BaseController(), "/", config=CONFIGUER)
    cherrypy.tree.mount(UserController(), "/user/", config=CONFIGUER)
    cherrypy.tree.mount(ProjectController(), "/project/", config=CONFIGUER)
    cherrypy.tree.mount(FigureController(), "/project/figure/", config=CONFIGUER)
    cherrypy.tree.mount(FlowController(), "/flow/", config=CONFIGUER)
    cherrypy.tree.mount(SettingsController(), "/settings/", config=CONFIGUER)
    cherrypy.tree.mount(HelpController(), "/help/", config=CONFIGUER)
    cherrypy.tree.mount(SimulatorController(), "/burst/", config=CONFIGUER)
    cherrypy.tree.mount(ParameterExplorationController(), "/burst/explore/", config=CONFIGUER)
    cherrypy.tree.mount(DynamicModelController(), "/burst/dynamic/", config=CONFIGUER)
    cherrypy.tree.mount(SpatioTemporalController(), "/spatial/", config=CONFIGUER)
    cherrypy.tree.mount(RegionsModelParametersController(), "/burst/modelparameters/regions/", config=CONFIGUER)
    cherrypy.tree.mount(SurfaceModelParametersController(), "/spatial/modelparameters/surface/", config=CONFIGUER)
    cherrypy.tree.mount(RegionStimulusController(), "/spatial/stimulus/region/", config=CONFIGUER)
    cherrypy.tree.mount(SurfaceStimulusController(), "/spatial/stimulus/surface/", config=CONFIGUER)
    cherrypy.tree.mount(LocalConnectivityController(), "/spatial/localconnectivity/", config=CONFIGUER)
    cherrypy.tree.mount(NoiseConfigurationController(), "/burst/noise/", config=CONFIGUER)
    cherrypy.tree.mount(HPCController(), "/hpc/", config=CONFIGUER)

    cherrypy.config.update(CONFIGUER)

    # ----------------- Register additional request handlers -----------------
    # This tool checks for MAX upload size
    cherrypy.tools.upload = Tool('on_start_resource', RequestHandler.check_upload_size)
    # This tools clean up files on disk (mainly after export)
    cherrypy.tools.cleanup = Tool('on_end_request', RequestHandler.clean_files_on_disk)
    # ----------------- End register additional request handlers ----------------

    # Register housekeeping job
    if TvbProfile.current.hpc.IS_HPC_RUN and TvbProfile.current.hpc.CAN_RUN_HPC:
        cherrypy.engine.housekeeper = cherrypy.process.plugins.BackgroundTask(
            TvbProfile.current.hpc.BACKGROUND_JOB_INTERVAL, HPCOperationService.check_operations_job)
        cherrypy.engine.housekeeper.start()

    # HTTP Server is fired now ######
    cherrypy.engine.start()
 def setUp(self):
     """
     Sets up the environment for testing;
     creates a `RegionsModelParametersController` and a connectivity
     """
     self.init()
     self.region_m_p_c = RegionsModelParametersController()
     BurstController().index()
     stored_burst = cherrypy.session[common.KEY_BURST_CONFIG]
     _, self.connectivity = DatatypesFactory().create_connectivity()
     new_params = {}
     for key, val in SIMULATOR_PARAMETERS.iteritems():
         new_params[key] = {'value': val}
     new_params['connectivity'] = {'value': self.connectivity.gid}
     stored_burst.simulator_configuration = new_params
     self._setup_dynamic()
Example #6
0
class TestRegionsModelParametersController(BaseTransactionalControllerTest):
    """ Unit tests for RegionsModelParametersController """
    def transactional_setup_method(self):
        """
        Sets up the environment for testing;
        creates a `RegionsModelParametersController` and a connectivity
        """
        self.init()
        self.region_m_p_c = RegionsModelParametersController()
        SimulatorController().index()
        self.simulator = cherrypy.session[common.KEY_SIMULATOR_CONFIG]
        self._setup_dynamic()

    def transactional_teardown_method(self):
        """ Cleans the testing environment """
        self.cleanup()

    def _setup_dynamic(self):
        dynamic_g = Dynamic(
            "test_dyn", self.test_user.id,
            ModelsEnum.GENERIC_2D_OSCILLATOR.get_class().__name__,
            '[["tau", 1.0], ["a", 5.0], ["b", -10.0], ["c", 10.0], ["I", 0.0], ["d", 0.02], '
            '["e", 3.0], ["f", 1.0], ["g", 0.0], ["alpha", 1.0], ["beta", 5.0], ["gamma", 1.0]]',
            HeunDeterministic.__name__, None)

        dynamic_k = Dynamic("test_dyn_kura", self.test_user.id,
                            ModelsEnum.KURAMOTO.get_class().__name__,
                            '[["omega", 1.0]]', HeunDeterministic.__name__,
                            None)

        self.dynamic_g = dao.store_entity(dynamic_g)
        self.dynamic_k = dao.store_entity(dynamic_k)

    def test_index(self, connectivity_index_factory):
        """
        Verifies that result dictionary has the expected keys / values after call to
        `edit_model_parameters()`
        """
        self.connectivity_index = connectivity_index_factory()
        self.simulator.connectivity = self.connectivity_index.gid
        result_dict = self.region_m_p_c.index()
        assert self.connectivity_index.gid == result_dict[
            'connectivity_entity'].gid.hex
        assert result_dict['mainContent'] == 'burst/model_param_region'
        assert result_dict[
            'submit_parameters_url'] == '/burst/modelparameters/regions/submit_model_parameters'
        assert 'dynamics' in result_dict
        assert 'dynamics_json' in result_dict
        assert 'pointsLabels' in result_dict
        assert 'positions' in result_dict

        json.loads(result_dict['dynamics_json'])

    def test_submit_model_parameters_happy(self, connectivity_index_factory):
        """
        Verifies call to `submit_model_parameters(...)` correctly redirects to '/burst/'
        """
        self.connectivity_index = connectivity_index_factory()
        self.simulator.connectivity = self.connectivity_index.gid
        self.region_m_p_c.index()

        dynamic_ids = json.dumps([
            self.dynamic_g.id
            for _ in range(self.connectivity_index.number_of_regions)
        ])

        self._expect_redirect('/burst/',
                              self.region_m_p_c.submit_model_parameters,
                              dynamic_ids)

    def test_submit_model_parameters_inconsistent_models(
            self, connectivity_index_factory):
        self.connectivity_index = connectivity_index_factory()
        self.simulator.connectivity = self.connectivity_index.gid
        self.region_m_p_c.index()

        dynamic_ids = [
            self.dynamic_g.id
            for _ in range(self.connectivity_index.number_of_regions)
        ]
        dynamic_ids[-1] = self.dynamic_k.id
        dynamic_ids = json.dumps(dynamic_ids)

        with pytest.raises(Exception):
            self.region_m_p_c.submit_model_parameters(dynamic_ids)
class TestRegionsModelParametersController(BaseTransactionalControllerTest):
    """ Unit tests for RegionsModelParametersController """

    @pytest.fixture(scope='module')
    def transactional_setup_fixture(self, connectivity_factory):
        """
        Sets up the environment for testing;
        creates a `RegionsModelParametersController` and a connectivity
        """
        self.init()
        self.region_m_p_c = RegionsModelParametersController()
        BurstController().index()
        stored_burst = cherrypy.session[common.KEY_BURST_CONFIG]
        _, self.connectivity = connectivity_factory
        new_params = {}
        for key, val in SIMULATOR_PARAMETERS.items():
            new_params[key] = {'value': val}
        new_params['connectivity'] = {'value': self.connectivity.gid}
        stored_burst.simulator_configuration = new_params
        self._setup_dynamic()

    def transactional_teardown_method(self):
        """ Cleans the testing environment """
        self.cleanup()


    def _setup_dynamic(self):
        dynamic_g = Dynamic("test_dyn", self.test_user.id, Generic2dOscillator.__name__,
                            '[["tau", 1.0], ["a", 5.0], ["b", -10.0], ["c", 10.0], ["I", 0.0], ["d", 0.02], '
                            '["e", 3.0], ["f", 1.0], ["g", 0.0], ["alpha", 1.0], ["beta", 5.0], ["gamma", 1.0]]',
                            HeunDeterministic.__name__, None)

        dynamic_k = Dynamic("test_dyn_kura", self.test_user.id, Kuramoto.__name__,
                            '[["omega", 1.0]]', HeunDeterministic.__name__, None)

        self.dynamic_g = dao.store_entity(dynamic_g)
        self.dynamic_k = dao.store_entity(dynamic_k)

    def test_index(self, transactional_setup_fixture):
        """
        Verifies that result dictionary has the expected keys / values after call to
        `edit_model_parameters()`
        """
        result_dict = self.region_m_p_c.index()
        assert self.connectivity.gid == result_dict['connectivity_entity'].gid
        assert result_dict['mainContent'] == 'burst/model_param_region'
        assert result_dict['submit_parameters_url'] == '/burst/modelparameters/regions/submit_model_parameters'
        assert 'dynamics' in result_dict
        assert 'dynamics_json' in result_dict
        assert 'pointsLabels' in result_dict
        assert 'positions' in result_dict

        json.loads(result_dict['dynamics_json'])

    def test_submit_model_parameters_happy(self, transactional_setup_fixture):
        """
        Verifies call to `submit_model_parameters(...)` correctly redirects to '/burst/'
        """
        self.region_m_p_c.index()

        dynamic_ids = json.dumps([self.dynamic_g.id for _ in range(self.connectivity.number_of_regions)])

        self._expect_redirect('/burst/', self.region_m_p_c.submit_model_parameters, dynamic_ids)
        

    def test_submit_model_parameters_inconsistent_models(self, transactional_setup_fixture):
        self.region_m_p_c.index()

        dynamic_ids = [self.dynamic_g.id for _ in range(self.connectivity.number_of_regions)]
        dynamic_ids[-1] = self.dynamic_k.id
        dynamic_ids = json.dumps(dynamic_ids)

        with pytest.raises(Exception):
            self.region_m_p_c.submit_model_parameters(dynamic_ids)
class RegionsModelParametersControllerTest(BaseTransactionalControllerTest):
    """ Unit tests for RegionsModelParametersController """


    def setUp(self):
        """
        Sets up the environment for testing;
        creates a `RegionsModelParametersController` and a connectivity
        """
        self.init()
        self.region_m_p_c = RegionsModelParametersController()
        BurstController().index()
        stored_burst = cherrypy.session[common.KEY_BURST_CONFIG]
        _, self.connectivity = DatatypesFactory().create_connectivity()
        new_params = {}
        for key, val in SIMULATOR_PARAMETERS.iteritems():
            new_params[key] = {'value': val}
        new_params['connectivity'] = {'value': self.connectivity.gid}
        stored_burst.simulator_configuration = new_params
        self._setup_dynamic()

    def tearDown(self):
        """ Cleans the testing environment """
        self.cleanup()


    def _setup_dynamic(self):
        dynamic_g = Dynamic("test_dyn", self.test_user.id, Generic2dOscillator.__name__,
                            '[["tau", 1.0], ["a", 5.0], ["b", -10.0], ["c", 10.0], ["I", 0.0], ["d", 0.02], '
                            '["e", 3.0], ["f", 1.0], ["g", 0.0], ["alpha", 1.0], ["beta", 5.0], ["gamma", 1.0]]',
                            HeunDeterministic.__name__, None)

        dynamic_k = Dynamic("test_dyn_kura", self.test_user.id, Kuramoto.__name__,
                            '[["omega", 1.0]]', HeunDeterministic.__name__, None)

        self.dynamic_g = dao.store_entity(dynamic_g)
        self.dynamic_k = dao.store_entity(dynamic_k)
    
    
    def test_index(self):
        """
        Verifies that result dictionary has the expected keys / values after call to
        `edit_model_parameters()`
        """
        result_dict = self.region_m_p_c.index()
        self.assertEqual(self.connectivity.gid, result_dict['connectivity_entity'].gid)
        self.assertEqual(result_dict['mainContent'], 'burst/model_param_region')
        self.assertEqual(result_dict['submit_parameters_url'], 
                         '/burst/modelparameters/regions/submit_model_parameters')
        self.assertTrue('dynamics' in result_dict)
        self.assertTrue('dynamics_json' in result_dict)
        self.assertTrue('pointsLabels' in result_dict)
        self.assertTrue('positions' in result_dict)

        json.loads(result_dict['dynamics_json'])

        
    def test_submit_model_parameters_happy(self):
        """
        Verifies call to `submit_model_parameters(...)` correctly redirects to '/burst/'
        """
        self.region_m_p_c.index()

        dynamic_ids = json.dumps([self.dynamic_g.id for _ in range(self.connectivity.number_of_regions)])

        self._expect_redirect('/burst/', self.region_m_p_c.submit_model_parameters, dynamic_ids)
        

    def test_submit_model_parameters_inconsistent_models(self):
        self.region_m_p_c.index()

        dynamic_ids = [self.dynamic_g.id for _ in range(self.connectivity.number_of_regions)]
        dynamic_ids[-1] = self.dynamic_k.id
        dynamic_ids = json.dumps(dynamic_ids)

        self.assertRaises(Exception, self.region_m_p_c.submit_model_parameters, dynamic_ids)