def test_read_stored_settings(self): """ Test to see that keys from the configuration dict is updated with the value from the configuration file after store. """ initial_configurations = self.settings_service.configurable_keys to_store_data = { key: value['value'] for key, value in initial_configurations.items() } for key, value in self.TEST_SETTINGS.items(): to_store_data[key] = value is_changed, shoud_reset = self.settings_service.save_settings( **to_store_data) assert shoud_reset and is_changed # enforce keys to get repopulated: TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService() updated_configurations = self.settings_service.configurable_keys for key, value in updated_configurations.items(): if key in self.TEST_SETTINGS: assert self.TEST_SETTINGS[key] == value['value'] elif key == SettingsService.KEY_ADMIN_PWD: assert TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD == value[ 'value'] assert TvbProfile.current.web.admin.ADMINISTRATOR_BLANK_PWD == initial_configurations[ key]['value'] else: assert initial_configurations[key]['value'] == value['value']
def _fake_restart_services(self, should_reset): """ This function will replace the SettingsController._restart_service method, to avoid problems in tests due to restart. """ self.was_reset = should_reset TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME)
def init_test_env(): """ This method prepares all necessary data for tests execution """ # Set a default test profile, for when running tests from dev-env. from tvb.basic.profile import TvbProfile if TvbProfile.CURRENT_PROFILE_NAME is None: profile = TvbProfile.TEST_SQLITE_PROFILE if len(sys.argv) > 1: for i in range(1, len(sys.argv) - 1): if "--profile=" in sys.argv[i]: profile = sys.argv[i].split("=")[1] TvbProfile.set_profile(profile) print("Not expected to happen except from PyCharm: setting profile", TvbProfile.CURRENT_PROFILE_NAME) db_file = TvbProfile.current.db.DB_URL.replace('sqlite:///', '') if os.path.exists(db_file): os.remove(db_file) from tvb.config.init.model_manager import reset_database from tvb.config.init.initializer import initialize reset_database() initialize(skip_import=True) # Add Dummy DataType REGISTRY.register_datatype(DummyDataType, DummyDataTypeH5, DummyDataTypeIndex) REGISTRY.register_datatype(None, None, DummyDataType2Index)
def test_read_stored_settings(self): """ Test to see that keys from the configuration dict is updated with the value from the configuration file after store. """ initial_configurations = self.settings_service.configurable_keys to_store_data = {key: value['value'] for key, value in initial_configurations.iteritems()} for key, value in self.TEST_SETTINGS.iteritems(): to_store_data[key] = value is_changed, shoud_reset = self.settings_service.save_settings(**to_store_data) assert shoud_reset and is_changed # enforce keys to get repopulated: TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService() updated_configurations = self.settings_service.configurable_keys for key, value in updated_configurations.iteritems(): if key in self.TEST_SETTINGS: assert self.TEST_SETTINGS[key] == value['value'] elif key == SettingsService.KEY_ADMIN_PWD: assert TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD == value['value'] assert TvbProfile.current.web.admin.ADMINISTRATOR_BLANK_PWD == initial_configurations[key]['value'] else: assert initial_configurations[key]['value'] == value['value']
def test_update_settings(self): """ Test update of settings: correct flags should be returned, and check storage folder renamed """ # 1. save on empty config-file: to_store_data = { key: value['value'] for key, value in self.settings_service.configurable_keys.items() } for key, value in self.TEST_SETTINGS.items(): to_store_data[key] = value is_changed, shoud_reset = self.settings_service.save_settings( **to_store_data) assert shoud_reset and is_changed # 2. Reload and save with the same values (is_changed expected to be False) TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService() to_store_data = { key: value['value'] for key, value in self.settings_service.configurable_keys.items() } is_changed, shoud_reset = self.settings_service.save_settings( **to_store_data) assert not is_changed assert not shoud_reset # 3. Reload and check that changing TVB_STORAGE is done correctly TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService() to_store_data = { key: value['value'] for key, value in self.settings_service.configurable_keys.items() } to_store_data[SettingsService.KEY_STORAGE] = os.path.join( TvbProfile.current.TVB_STORAGE, 'RENAMED') # Write a test-file and check that it is moved file_writer = open( os.path.join(TvbProfile.current.TVB_STORAGE, "test_rename-xxx43"), 'w') file_writer.write('test-content') file_writer.close() is_changed, shoud_reset = self.settings_service.save_settings( **to_store_data) assert is_changed assert not shoud_reset # Check that the file was correctly moved: data = open( os.path.join(TvbProfile.current.TVB_STORAGE, 'RENAMED', "test_rename-xxx43"), 'r').read() assert data == 'test-content' shutil.rmtree(os.path.join(TvbProfile.current.TVB_STORAGE, 'RENAMED')) os.remove( os.path.join(TvbProfile.current.TVB_STORAGE, "test_rename-xxx43"))
def setup_test_env(): from tvb.basic.profile import TvbProfile if len(sys.argv) > 1: profile = sys.argv[1] else: profile = TvbProfile.TEST_SQLITE_PROFILE TvbProfile.set_profile(profile)
def teardown_method(self): """ Restore configuration file """ if os.path.exists(TEST_CONFIG_FILE): os.remove(TEST_CONFIG_FILE) TvbProfile.current.__class__.TVB_CONFIG_FILE = self.old_config_file TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME)
def fire_operation(project_id, adapter_instance, view_model): TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) project = dao.get_project_by_id(project_id) # launch an operation and have the results stored both in DB and on disk launched_operation = OperationService().fire_operation(adapter_instance, project.administrator, project.id, view_model=view_model) LOG.info("Operation launched....") return launched_operation
def setup_method(self): """ Prepare the usage of a different config file for this class only. """ StorageInterface.remove_files([TEST_CONFIG_FILE, TestSQLiteProfile.DEFAULT_STORAGE]) self.old_config_file = TvbProfile.current.TVB_CONFIG_FILE TvbProfile.current.__class__.TVB_CONFIG_FILE = TEST_CONFIG_FILE TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService()
def import_conn_zip(project_id, zip_path): TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) project = dao.get_project_by_id(project_id) importer = ABCAdapter.build_adapter_from_class(ZIPConnectivityImporter) view_model = ZIPConnectivityImporterModel() view_model.uploaded = zip_path return OperationService().fire_operation(importer, project.administrator, project_id, view_model=view_model)
def cleanup(self): """ Have a different name than transactional_teardown_method so we can use it safely in transactions and it will not be called after running actual test. Using transactional_teardown_method here won't WORK!! See TransactionalTest """ if os.path.exists(TvbProfile.current.TVB_CONFIG_FILE): os.remove(TvbProfile.current.TVB_CONFIG_FILE) TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME)
def setup_method(self): """ Prepare the usage of a different config file for this class only. """ if os.path.exists(TEST_CONFIG_FILE): os.remove(TEST_CONFIG_FILE) self.old_config_file = TvbProfile.current.TVB_CONFIG_FILE TvbProfile.current.__class__.TVB_CONFIG_FILE = TEST_CONFIG_FILE TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService()
def setup_test_console_env(): import sys # Remove anything pointing to the framework, to make sure that only one direct dependency exists # TVB-Framework --> TVB Scientific Library # We should have nothing inverse. sys.path = [ path for path in sys.path if not path.endswith('framework_tvb') ] from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.TEST_LIBRARY_PROFILE)
def command_initializer(persist_settings=True, skip_import=False): if persist_settings and TvbProfile.is_first_run(): settings_service = SettingsService() settings = {} # Save default settings for key, setting in settings_service.configurable_keys.items(): settings[key] = setting['value'] settings_service.save_settings(**settings) TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) # Build new db engine in case DB URL value changed new_db_engine = build_db_engine() SA_SESSIONMAKER.configure(bind=new_db_engine) # Initialize application initialize(skip_import)
def import_conn_h5(project_id, h5_path): project = dao.get_project_by_id(project_id) TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) now = datetime.now() date_str = "%d-%d-%d_%d-%d-%d_%d" % (now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond) uq_name = "%s-Connectivity" % date_str new_path = os.path.join(TvbProfile.current.TVB_TEMP_FOLDER, uq_name) StorageInterface.copy_file(h5_path, new_path) importer = ABCAdapter.build_adapter_from_class(TVBImporter) view_model = importer.get_view_model_class()() view_model.data_file = new_path return OperationService().fire_operation(importer, project.administrator, project_id, view_model=view_model)
def settings(self, save_settings=False, **data): """Main settings page submit and get""" template_specification = dict(mainContent="../settings/system_settings", title="System Settings") if save_settings: try: form = SettingsForm() data = form.to_python(data) isrestart, isreset = self.settingsservice.save_settings(**data) if isrestart: thread = threading.Thread(target=self._restart_services, kwargs={'should_reset': isreset}) thread.start() common.add2session(common.KEY_IS_RESTART, True) common.set_important_message('Please wait until TVB is restarted properly!') raise cherrypy.HTTPRedirect('/tvb') # Here we will leave the same settings page to be displayed. # It will continue reloading when CherryPy restarts. except formencode.Invalid as excep: template_specification[common.KEY_ERRORS] = excep.unpack_errors() except InvalidSettingsException as excep: self.logger.error('Invalid settings! Exception %s was raised' % (str(excep))) common.set_error_message(excep.message) template_specification.update({'keys_order': self.settingsservice.KEYS_DISPLAY_ORDER, 'config_data': self.settingsservice.configurable_keys, common.KEY_FIRST_RUN: TvbProfile.is_first_run()}) return self.fill_default_attributes(template_specification)
def initialize(introspected_modules, load_xml_events=True): """ Initialize when Application is starting. Check for new algorithms or new DataTypes. """ SettingsService().check_db_url(TvbProfile.current.db.DB_URL) ## Initialize DB is_db_empty = initialize_startup() ## Create Projects storage root in case it does not exist. initialize_storage() ## Populate DB algorithms, by introspection event_folders = [] start_introspection_time = datetime.datetime.now() for module in introspected_modules: introspector = Introspector(module) # Introspection is always done, even if DB was not empty. introspector.introspect(True) event_path = introspector.get_events_path() if event_path: event_folders.append(event_path) # Now remove or mark as removed any unverified Algo-Group, Algo-Category or Portlet to_invalidate, to_remove = dao.get_non_validated_entities( start_introspection_time) for entity in to_invalidate: entity.removed = True dao.store_entities(to_invalidate) for entity in to_remove: dao.remove_entity(entity.__class__, entity.id) ## Populate events if load_xml_events: read_events(event_folders) if not TvbProfile.is_first_run(): ## Create default users. if is_db_empty: dao.store_entity( model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None)) UserService().create_user( username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME, password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD, email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL, role=model.ROLE_ADMINISTRATOR) ## In case actions related to latest code-changes are needed, make sure they are executed. CodeUpdateManager().run_all_updates() ## In case the H5 version changed, run updates on all DataTypes if TvbProfile.current.version.DATA_CHECKED_TO_VERSION < TvbProfile.current.version.DATA_VERSION: thread = threading.Thread( target=FilesUpdateManager().run_all_updates) thread.start() ## Clean tvb-first-time-run temporary folder, as we are no longer at the first run: shutil.rmtree(TvbProfile.current.FIRST_RUN_STORAGE, True)
def settings(self, save_settings=False, **data): """Main settings page submit and get""" template_specification = dict(mainContent="settings/system_settings", title="System Settings") if save_settings: try: form = SettingsForm() data = form.to_python(data) isrestart, isreset = self.settingsservice.save_settings(**data) if isrestart: thread = threading.Thread(target=self._restart_services, kwargs={'should_reset': isreset}) thread.start() common.add2session(common.KEY_IS_RESTART, True) common.set_important_message('Please wait until TVB is restarted properly!') raise cherrypy.HTTPRedirect('/tvb') # Here we will leave the same settings page to be displayed. # It will continue reloading when CherryPy restarts. except formencode.Invalid as excep: template_specification[common.KEY_ERRORS] = excep.unpack_errors() except InvalidSettingsException as excep: self.logger.error('Invalid settings! Exception %s was raised' % (str(excep))) common.set_error_message(excep.message) template_specification.update({'keys_order': self.settingsservice.KEYS_DISPLAY_ORDER, 'config_data': self.settingsservice.configurable_keys, common.KEY_FIRST_RUN: TvbProfile.is_first_run()}) return self.fill_default_attributes(template_specification)
def init_test_env(): """ This method prepares all necessary data for tests execution """ # Set a default test profile, for when running tests from dev-env. if TvbProfile.CURRENT_PROFILE_NAME is None: TvbProfile.set_profile(TvbProfile.TEST_SQLITE_PROFILE) print "Not expected to happen except from PyCharm: setting profile", TvbProfile.CURRENT_PROFILE_NAME db_file = TvbProfile.current.db.DB_URL.replace('sqlite:///', '') if os.path.exists(db_file): os.remove(db_file) from tvb.core.entities.model_manager import reset_database from tvb.core.services.initializer import initialize reset_database() initialize(["tvb.config", "tvb.tests.framework"], skip_import=True)
def init_test_env(): """ This method prepares all necessary data for tests execution """ # Set a default test profile, for when running tests from dev-env. if TvbProfile.CURRENT_PROFILE_NAME is None: TvbProfile.set_profile(TvbProfile.TEST_SQLITE_PROFILE) print "Not expected to happen except from PyCharm: setting profile", TvbProfile.CURRENT_PROFILE_NAME db_file = TvbProfile.current.db.DB_URL.replace("sqlite:///", "") if os.path.exists(db_file): os.remove(db_file) from tvb.core.entities.model_manager import reset_database from tvb.core.services.initializer import initialize reset_database() initialize(["tvb.config", "tvb.tests.framework"], skip_import=True)
def deco(*a, **b): if hasattr(cherrypy, common.KEY_SESSION): user = common.get_logged_user() if user is not None and user.is_administrator( ) or TvbProfile.is_first_run(): return func(*a, **b) raise common.NotAuthenticated( 'Only Administrators can access this application area!', redirect_url='/tvb')
def initialize(introspected_modules, load_xml_events=True): """ Initialize when Application is starting. Check for new algorithms or new DataTypes. """ SettingsService().check_db_url(TvbProfile.current.db.DB_URL) ## Initialize DB is_db_empty = initialize_startup() ## Create Projects storage root in case it does not exist. initialize_storage() ## Populate DB algorithms, by introspection event_folders = [] start_introspection_time = datetime.datetime.now() for module in introspected_modules: introspector = Introspector(module) # Introspection is always done, even if DB was not empty. introspector.introspect(True) event_path = introspector.get_events_path() if event_path: event_folders.append(event_path) # Now remove or mark as removed any unverified Algo-Group, Algo-Category or Portlet to_invalidate, to_remove = dao.get_non_validated_entities(start_introspection_time) for entity in to_invalidate: entity.removed = True dao.store_entities(to_invalidate) for entity in to_remove: dao.remove_entity(entity.__class__, entity.id) ## Populate events if load_xml_events: read_events(event_folders) if not TvbProfile.is_first_run(): ## Create default users. if is_db_empty: dao.store_entity(model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None)) UserService().create_user(username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME, password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD, email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL, role=model.ROLE_ADMINISTRATOR) ## In case actions related to latest code-changes are needed, make sure they are executed. CodeUpdateManager().run_all_updates() ## In case the H5 version changed, run updates on all DataTypes if TvbProfile.current.version.DATA_CHECKED_TO_VERSION < TvbProfile.current.version.DATA_VERSION: thread = threading.Thread(target=FilesUpdateManager().run_all_updates) thread.start() ## Clean tvb-first-time-run temporary folder, as we are no longer at the first run: shutil.rmtree(TvbProfile.current.FIRST_RUN_STORAGE, True)
def setup(): if sys.platform != 'darwin': unsupport_module('h5py') import logging logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.MATLAB_PROFILE) # MATLAB states the module doesn't exist if not importable and provides no traceback # to diagnose the import error, so we'll need to workaround this in the future. For now, # just try to import the simlab and report if it worked or not. try: import tvb.simulator.lab print('TVB modules available.') except Exception as exc: #print 'failed to import all TVB modules, not all functionality may be .' pass
def test_update_settings(self): """ Test update of settings: correct flags should be returned, and check storage folder renamed """ # 1. save on empty config-file: to_store_data = {key: value['value'] for key, value in self.settings_service.configurable_keys.iteritems()} for key, value in self.TEST_SETTINGS.iteritems(): to_store_data[key] = value is_changed, shoud_reset = self.settings_service.save_settings(**to_store_data) assert shoud_reset and is_changed # 2. Reload and save with the same values (is_changed expected to be False) TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService() to_store_data = {key: value['value'] for key, value in self.settings_service.configurable_keys.iteritems()} is_changed, shoud_reset = self.settings_service.save_settings(**to_store_data) assert not is_changed assert not shoud_reset # 3. Reload and check that changing TVB_STORAGE is done correctly TvbProfile._build_profile_class(TvbProfile.CURRENT_PROFILE_NAME) self.settings_service = SettingsService() to_store_data = {key: value['value'] for key, value in self.settings_service.configurable_keys.iteritems()} to_store_data[SettingsService.KEY_STORAGE] = os.path.join(TvbProfile.current.TVB_STORAGE, 'RENAMED') # Write a test-file and check that it is moved file_writer = open(os.path.join(TvbProfile.current.TVB_STORAGE, "test_rename-xxx43"), 'w') file_writer.write('test-content') file_writer.close() is_changed, shoud_reset = self.settings_service.save_settings(**to_store_data) assert is_changed assert not shoud_reset # Check that the file was correctly moved: data = open(os.path.join(TvbProfile.current.TVB_STORAGE, 'RENAMED', "test_rename-xxx43"), 'r').read() assert data == 'test-content' shutil.rmtree(os.path.join(TvbProfile.current.TVB_STORAGE, 'RENAMED')) os.remove(os.path.join(TvbProfile.current.TVB_STORAGE, "test_rename-xxx43"))
def test_first_run_save(self): """ Check that before setting something, all flags are pointing towards empty. After storing some configurations, check that flags are changed. """ initial_configurations = self.settings_service.configurable_keys first_run = TvbProfile.is_first_run() assert first_run, "Invalid First_Run flag!!" assert not os.path.exists(TEST_CONFIG_FILE) assert len(TvbProfile.current.manager.stored_settings) == 0 to_store_data = {key: value['value'] for key, value in initial_configurations.iteritems()} for key, value in self.TEST_SETTINGS.iteritems(): to_store_data[key] = value _, shoud_reset = self.settings_service.save_settings(**to_store_data) assert shoud_reset first_run = TvbProfile.is_first_run() assert not first_run, "Invalid First_Run flag!!" assert os.path.exists(TEST_CONFIG_FILE) assert not len(TvbProfile.current.manager.stored_settings) == 0
def test_first_run_save(self): """ Check that before setting something, all flags are pointing towards empty. After storing some configurations, check that flags are changed. """ initial_configurations = self.settings_service.configurable_keys first_run = TvbProfile.is_first_run() assert first_run, "Invalid First_Run flag!!" assert not os.path.exists(TEST_CONFIG_FILE) assert len(TvbProfile.current.manager.stored_settings) == 0 to_store_data = {key: value['value'] for key, value in initial_configurations.items()} for key, value in self.TEST_SETTINGS.items(): to_store_data[key] = value _, shoud_reset = self.settings_service.save_settings(**to_store_data) assert shoud_reset first_run = TvbProfile.is_first_run() assert not first_run, "Invalid First_Run flag!!" assert os.path.exists(TEST_CONFIG_FILE) assert not len(TvbProfile.current.manager.stored_settings) == 0
def fire_simulation(project_id, simulator_model): TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) project = dao.get_project_by_id(project_id) assert isinstance(simulator_model, SimulatorAdapterModel) # Load the SimulatorAdapter algorithm from DB cached_simulator_algorithm = AlgorithmService( ).get_algorithm_by_module_and_class(IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) # Instantiate a SimulatorService and launch the configured simulation simulator_service = SimulatorService() burst = BurstConfiguration(project.id) burst.name = "Sim " + str(datetime.now()) burst.start_time = datetime.now() dao.store_entity(burst) launched_operation = simulator_service.async_launch_and_prepare_simulation( burst, project.administrator, project, cached_simulator_algorithm, simulator_model) LOG.info("Operation launched ....") return launched_operation
def __init__(self): self.logger = get_logger(__name__) first_run = TvbProfile.is_first_run() storage = TvbProfile.current.TVB_STORAGE if not first_run else TvbProfile.current.DEFAULT_STORAGE self.configurable_keys = { self.KEY_STORAGE: {'label': 'Root folder for all projects', 'value': storage, 'readonly': not first_run, 'type': 'text'}, self.KEY_MAX_DISK_SPACE_USR: {'label': 'Max hard disk space per user (MBytes)', 'value': TvbProfile.current.MAX_DISK_SPACE / 2 ** 10, 'type': 'text'}, self.KEY_MATLAB_EXECUTABLE: {'label': 'Optional Matlab or Octave path', 'type': 'text', 'value': TvbProfile.current.MATLAB_EXECUTABLE or get_matlab_executable() or '', 'description': 'Some analyzers will not be available when ' 'matlab/octave are not found'}, self.KEY_SELECTED_DB: {'label': 'Select one DB engine', 'value': TvbProfile.current.db.SELECTED_DB, 'type': 'select', 'readonly': not first_run, 'options': TvbProfile.current.db.ACEEPTED_DBS}, self.KEY_DB_URL: {'label': "DB connection URL", 'value': TvbProfile.current.db.ACEEPTED_DBS[TvbProfile.current.db.SELECTED_DB], 'type': 'text', 'readonly': TvbProfile.current.db.SELECTED_DB == 'sqlite'}, self.KEY_PORT: {'label': 'Port to run Cherrypy on', 'value': TvbProfile.current.web.SERVER_PORT, 'dtype': 'primitive', 'type': 'text'}, self.KEY_PORT_MPLH5: {'label': 'Port to run Matplotlib on', 'type': 'text', 'dtype': 'primitive', 'value': TvbProfile.current.web.MPLH5_SERVER_PORT}, self.KEY_URL_WEB: {'label': 'URL for accessing web', 'value': TvbProfile.current.web.BASE_URL, 'type': 'text', 'dtype': 'primitive'}, self.KEY_URL_MPLH5: {'label': 'URL for accessing MPLH5 visualizers', 'type': 'text', 'value': TvbProfile.current.web.MPLH5_SERVER_URL, 'dtype': 'primitive'}, self.KEY_MAX_NR_THREADS: {'label': 'Maximum no. of threads for local installations', 'type': 'text', 'value': TvbProfile.current.MAX_THREADS_NUMBER, 'dtype': 'primitive'}, self.KEY_MAX_RANGE: {'label': 'Maximum no. of operations in one PSE', 'description': "Parameters Space Exploration (PSE) maximum number of operations", 'value': TvbProfile.current.MAX_RANGE_NUMBER, 'type': 'text', 'dtype': 'primitive'}, self.KEY_MAX_NR_SURFACE_VERTEX: {'label': 'Maximum no. of vertices in a surface', 'type': 'text', 'dtype': 'primitive', 'value': TvbProfile.current.MAX_SURFACE_VERTICES_NUMBER}, self.KEY_CLUSTER: {'label': 'Deploy on cluster', 'value': TvbProfile.current.cluster.IS_DEPLOY, 'description': 'Check this only if on the web-server machine OARSUB command is enabled.', 'dtype': 'primitive', 'type': 'boolean'}, self.KEY_ADMIN_NAME: {'label': 'Administrator User Name', 'value': TvbProfile.current.web.admin.ADMINISTRATOR_NAME, 'type': 'text', 'readonly': not first_run, 'description': ('Password and Email can be edited after first run, ' 'from the profile page directly.')}, self.KEY_ADMIN_PWD: {'label': 'Password', 'value': TvbProfile.current.web.admin.ADMINISTRATOR_BLANK_PWD if first_run else TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD, 'type': 'password', 'readonly': not first_run}, self.KEY_ADMIN_EMAIL: {'label': 'Administrator Email', 'value': TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL, 'readonly': not first_run, 'type': 'text'}}
def __init__(self, config_root): """ Prepare Python logger based on a configuration file. :param: config_root - current package to configure logger for it. """ config_file_name = TVBSettings.LOGGER_CONFIG_FILE_NAME package = __import__(config_root, globals(), locals(), ['__init__'], 0) package_path = package.__path__[0] #Specify logging configuration file for current package. if not TvbProfile.is_library_mode(): logging.config.fileConfig(os.path.join(package_path, config_file_name), disable_existing_loggers=True)
def init_test_env(): """ This method prepares all necessary data for tests execution """ # Set a default test profile, for when running tests from dev-env. if TvbProfile.CURRENT_PROFILE_NAME is None: profile = TvbProfile.TEST_SQLITE_PROFILE if len(sys.argv) > 1: for i in range(1,len(sys.argv)-1): if "--profile=" in sys.argv[i]: profile = sys.argv[i].split("=")[1] TvbProfile.set_profile(profile) print("Not expected to happen except from PyCharm: setting profile", TvbProfile.CURRENT_PROFILE_NAME) db_file = TvbProfile.current.db.DB_URL.replace('sqlite:///', '') if os.path.exists(db_file): os.remove(db_file) from tvb.core.entities.model_manager import reset_database from tvb.core.services.initializer import initialize reset_database() initialize(["tvb.config", "tvb.tests.framework"], skip_import=True)
def run_all_updates(self): """ Upgrade the code to current version. Go through all update scripts with lower SVN version than the current running version. """ if TvbProfile.is_first_run(): ## We've just started with a clean TVB. No need to upgrade anything. return super(CodeUpdateManager, self).run_all_updates() if self.checked_version < self.current_version: TvbProfile.current.manager.add_entries_to_config_file( {stored.KEY_LAST_CHECKED_CODE_VERSION: TvbProfile.current.version.SVN_VERSION})
def run_all_updates(self): """ Upgrade the code to current version. Go through all update scripts with lower SVN version than the current running version. """ if TvbProfile.is_first_run(): ## We've just started with a clean TVB. No need to upgrade anything. return super(CodeUpdateManager, self).run_all_updates() if self.checked_version < self.current_version: TvbProfile.current.manager.add_entries_to_config_file( {stored.KEY_LAST_CHECKED_CODE_VERSION: TvbProfile.current.version.REVISION_NUMBER})
def initialize(skip_import=False, skip_updates=False): """ Initialize when Application is starting. Check for new algorithms or new DataTypes. """ SettingsService().check_db_url(TvbProfile.current.db.DB_URL) # Initialize DB is_db_empty = initialize_startup() # Create Projects storage root in case it does not exist. initialize_storage() # Populate DB algorithms, by introspection start_introspection_time = datetime.now() # Introspection is always done, even if DB was not empty. introspector = Introspector() introspector.introspect() to_invalidate = dao.get_non_validated_entities(start_introspection_time) for entity in to_invalidate: entity.removed = True dao.store_entities(to_invalidate) if not TvbProfile.is_first_run() and not skip_updates: # Create default users. if is_db_empty: dao.store_entity( User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None)) UserService().create_user(username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME, display_name=TvbProfile.current.web.admin.ADMINISTRATOR_DISPLAY_NAME, password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD, email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL, role=ROLE_ADMINISTRATOR, skip_import=skip_import) # In case actions related to latest code-changes are needed, make sure they are executed. CodeUpdateManager().run_all_updates() # In case the H5 version changed, run updates on all DataTypes thread = None if TvbProfile.current.version.DATA_CHECKED_TO_VERSION < TvbProfile.current.version.DATA_VERSION: thread = threading.Thread(target=FilesUpdateManager().run_all_updates) thread.start() # Clean tvb-first-time-run temporary folder, as we are no longer at the first run: shutil.rmtree(TvbProfile.current.FIRST_RUN_STORAGE, True) return thread
import sys import ctypes mex = ctypes.CDLL('libmex') class mexOut(object): def write(self, s): mex.mexPrintf(s) def flush(self): mex.mexEvalString('drawnow;') mex_out = mexOut() if False: sys.stdout = sys.stderr = mexOut() print 'Python stdout/err -> mexPrintf enabled.' import logging logging.basicConfig(level=logging.DEBUG, stream=mex_out) LOG = logging.getLogger('helpers') LOG.info('ready') from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.MATLAB_PROFILE)
if isrestart: thread = threading.Thread(target=self._restart_services, kwargs={'should_reset': isreset}) thread.start() common.add2session(common.KEY_IS_RESTART, True) common.set_important_message('Please wait until TVB is restarted properly!') raise cherrypy.HTTPRedirect('/tvb') # Here we will leave the same settings page to be displayed. # It will continue reloading when CherryPy restarts. except formencode.Invalid, excep: template_specification[common.KEY_ERRORS] = excep.unpack_errors() except InvalidSettingsException, excep: self.logger.error('Invalid settings! Exception %s was raised' % (str(excep))) common.set_error_message(excep.message) template_specification.update({'keys_order': self.settingsservice.KEYS_DISPLAY_ORDER, 'config_data': self.settingsservice.configurable_keys, common.KEY_FIRST_RUN: TvbProfile.is_first_run()}) return self.fill_default_attributes(template_specification) def _restart_services(self, should_reset): """ Restart CherryPy and Backend. """ mplh5 = TvbProfile.current.web.MPLH5_Server_Thread if mplh5 is not None: mplh5.shutdown() mplh5.server_close() else: self.logger.warning('For some reason the mplh5 never started.') cherrypy.engine.exit()
if __name__ == "__main__": #Start all TVB tests (if in Coverage mode) if KEY_COVERAGE in sys.argv: import tvb.interfaces as intf SOURCE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(intf.__file__))) COVERAGE = coverage(source=[SOURCE_DIR], omit=generage_excludes([SOURCE_DIR]), cover_pylib=False) COVERAGE.start() ## This needs to be executed before any TVB import. import unittest import datetime from tvb.basic.profile import TvbProfile TvbProfile.set_profile(sys.argv[1]) from tvb.tests.framework.xml_runner import XMLTestRunner from tvb.tests.framework.core import core_tests_main from tvb.tests.framework.adapters import adapters_tests_main from tvb.tests.framework.analyzers import bct_test from tvb.tests.framework.interfaces.web import web_tests_main def suite(): """ Gather all the tests in a test suite. """ test_suite = unittest.TestSuite() test_suite.addTest(core_tests_main.suite())
def setUp(self): self.init(with_data=False) self.settings_c = SettingsController() self.assertTrue(TvbProfile.is_first_run())
# # Paula Sanz Leon, Stuart A. Knock, M. Marmaduke Woodman, Lia Domide, # Jochen Mersmann, Anthony R. McIntosh, Viktor Jirsa (2013) # The Virtual Brain: a simulator of primate brain network dynamics. # Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010) # # """ This is intended to be a Benchmarking and Validator script. .. moduleauthor:: bogdan.neacsa """ from tvb.basic.profile import TvbProfile as tvb_profile tvb_profile.set_profile(["-profile", "CONSOLE_PROFILE"], try_reload=False) import sys from time import sleep from tvb.config import SIMULATOR_MODULE, SIMULATOR_CLASS from tvb.core.services.flow_service import FlowService from tvb.core.services.operation_service import OperationService from tvb.core.entities.storage import dao from tvb.core.entities.model import STATUS_STARTED, STATUS_FINISHED, STATUS_ERROR, PARAM_RANGE_PREFIX KEY_PROJECT = 'project' class ModelValidator(object): overwrites = {}
# -*- coding: utf-8 -*- import time import numpy as np from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.LIBRARY_PROFILE) from tvb_multiscale.examples.plot_results import plot_results from tvb_multiscale.config import CONFIGURED from tvb_multiscale.tvb.simulator_builder import SimulatorBuilder from tvb_multiscale.plot.plotter import Plotter from tvb.simulator.models.reduced_wong_wang_exc_io_inh_i import ReducedWongWangExcIOInhI from tvb.simulator.models.spiking_wong_wang_exc_io_inh_i import SpikingWongWangExcIOInhI from tvb.simulator.models.multiscale_wong_wang_exc_io_inh_i import MultiscaleWongWangExcIOInhI from tvb.simulator.models.wilson_cowan_constraint import WilsonCowan from tvb.simulator.models.generic_2d_oscillator_multiscale import Generic2dOscillator def main_example(tvb_sim_model=ReducedWongWangExcIOInhI, connectivity=CONFIGURED.DEFAULT_CONNECTIVITY_ZIP, simulation_length=100.0, config=CONFIGURED, **model_params): plotter = Plotter(config) # ----------------------1. Define a TVB simulator (model, integrator, monitors...)---------------------------------- simulator_builder = SimulatorBuilder() # Optionally modify the default configuration: simulator_builder.model = tvb_sim_model
""" Return a folder, where all log files are to be stored. """ tmp_path = os.path.join(LibraryProfile.TVB_STORAGE, "logs") if not os.path.exists(tmp_path): os.makedirs(tmp_path) return tmp_path @classmethod def initialize_profile(cls): """No initialization needed for this particular profile. But useful in general""" pass ### ### Dependent of the selected profile and framework classes being present or not, load the correct configuration. ### if TvbProfile.is_library_mode(): ## TVB-Simulator-Library is used stand-alone. TVBSettings = LibraryProfile else: ## Initialization based on profile is further done in Framework. from tvb.config.settings import FrameworkSettings TVBSettings = FrameworkSettings TVBSettings.initialize_profile()
# Jochen Mersmann, Anthony R. McIntosh, Viktor Jirsa (2013) # The Virtual Brain: a simulator of primate brain network dynamics. # Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010) # # """ Find a TS in current project (by Subject) and later run an analyzer on it. __main__ will contain the code. .. moduleauthor:: Lia Domide <*****@*****.**> """ if __name__ == "__main__": from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) from time import sleep from tvb.basic.logger.builder import get_logger from tvb.core.adapters.abcadapter import ABCAdapter from tvb.core.entities import model from tvb.core.entities.storage import dao from tvb.core.entities.transient.structure_entities import DataTypeMetaData from tvb.core.services.flow_service import FlowService from tvb.core.services.operation_service import OperationService from tvb.adapters.analyzers.fourier_adapter import FourierAdapter from tvb.datatypes.time_series import TimeSeriesRegion from tvb.datatypes.spectral import FourierSpectrum LOG = get_logger(__name__)
def save_settings(self, **data): """ Check if new settings are correct. Make necessary changes, then save new data in configuration file. :returns: two boolean values -there were any changes to the configuration; -a reset should be performed on the TVB relaunch. """ new_storage = data[self.KEY_STORAGE] previous_storage = TvbProfile.current.TVB_STORAGE new_db = data[self.KEY_SELECTED_DB] previous_db = TvbProfile.current.db.SELECTED_DB db_changed = new_db != previous_db storage_changed = new_storage != previous_storage matlab_exec = data[self.KEY_MATLAB_EXECUTABLE] if matlab_exec == 'None': data[self.KEY_MATLAB_EXECUTABLE] = '' # Storage changed but DB didn't, just copy TVB storage to new one. if storage_changed and not db_changed: if os.path.exists(new_storage): if os.access(new_storage, os.W_OK): shutil.rmtree(new_storage) else: raise InvalidSettingsException("No Write access on storage folder!!") shutil.copytree(previous_storage, new_storage) if not os.path.isdir(new_storage): os.makedirs(new_storage) max_space = data[self.KEY_MAX_DISK_SPACE_USR] available_mem_kb = SettingsService.get_disk_free_space(new_storage) kb_value = int(max_space) * 2 ** 10 if not (0 < kb_value < available_mem_kb): raise InvalidSettingsException("Not enough disk space. There is a maximum of %d MB available on this disk " "or partition. Wanted %d" % (available_mem_kb / (2 ** 10), max_space)) data[self.KEY_MAX_DISK_SPACE_USR] = kb_value # Save data to file, all while checking if any data has changed first_run = TvbProfile.is_first_run() if first_run: data[stored.KEY_LAST_CHECKED_FILE_VERSION] = TvbProfile.current.version.DATA_VERSION data[stored.KEY_LAST_CHECKED_CODE_VERSION] = TvbProfile.current.version.SVN_VERSION file_data = data if self.KEY_ADMIN_PWD in data: data[self.KEY_ADMIN_PWD] = md5(data[self.KEY_ADMIN_PWD]).hexdigest() anything_changed = True else: file_data = TvbProfile.current.manager.stored_settings anything_changed = False for key in file_data: if key in data and str(data[key]) != str(file_data[key]): anything_changed = True file_data[key] = data[key] if db_changed: file_data[self.KEY_DB_URL] = TvbProfile.current.db.DB_URL for key in data: if key not in file_data: anything_changed = True file_data[key] = data[key] # Write in file new data if anything_changed: TvbProfile.current.manager.write_config_data(file_data) os.chmod(TvbProfile.current.TVB_CONFIG_FILE, 0o644) return anything_changed, first_run or db_changed
def deco(*a, **b): if not TvbProfile.is_first_run(): return func(*a, **b) raise common.NotAllowed('You should first set up tvb', redirect_url='/settings/settings')
def transactional_setup_method(self): self.init(with_data=False) self.settings_c = SettingsController() assert TvbProfile.is_first_run()
def deco(*a, **b): if hasattr(cherrypy, common.KEY_SESSION): user = common.get_logged_user() if user is not None and user.is_administrator() or TvbProfile.is_first_run(): return func(*a, **b) raise common.NotAuthenticated('Only Administrators can access this application area!', redirect_url='/tvb')
# Paula Sanz Leon, Stuart A. Knock, M. Marmaduke Woodman, Lia Domide, # Jochen Mersmann, Anthony R. McIntosh, Viktor Jirsa (2013) # The Virtual Brain: a simulator of primate brain network dynamics. # Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010) # # """ This is intended to be a Benchmarking and Validator script. .. moduleauthor:: bogdan.neacsa """ if __name__ == '__main__': from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) import sys from time import sleep from tvb.config import SIMULATOR_MODULE, SIMULATOR_CLASS from tvb.core.adapters.abcadapter import ABCAdapter from tvb.core.services.flow_service import FlowService from tvb.core.services.operation_service import OperationService from tvb.core.entities.storage import dao from tvb.core.entities.model import STATUS_STARTED, STATUS_FINISHED, STATUS_ERROR, PARAM_RANGE_PREFIX KEY_PROJECT = 'project' class ModelValidator(object):
4 is the operation id stored in the DataBase in the table "OPERATIONS" It gets the algorithm, and the adapter with its parameters from database. And finally launches the computation. The results of the computation will be stored by the adapter itself. .. moduleauthor:: Bogdan Neacsa <*****@*****.**> .. moduleauthor:: Lia Domide <*****@*****.**> .. moduleauthor:: Yann Gordon <*****@*****.**> """ import sys from tvb.basic.profile import TvbProfile if __name__ == '__main__': TvbProfile.set_profile(sys.argv[2], True) from tvb.basic.logger.builder import get_logger from tvb.core.adapters.abcadapter import ABCAdapter from tvb.core.entities.storage import dao from tvb.core.utils import parse_json_parameters from tvb.core.services.operation_service import OperationService from tvb.core.services.workflow_service import WorkflowService def do_operation_launch(operation_id): """ Event attached to the local queue for executing an operation, when we will have resources available. """ LOGGER = get_logger('tvb.core.operation_async_launcher')
4 is the operation id stored in the DataBase in the table "OPERATIONS" It gets the algorithm, and the adapter with its parameters from database. And finally launches the computation. The results of the computation will be stored by the adapter itself. .. moduleauthor:: Bogdan Neacsa <*****@*****.**> .. moduleauthor:: Lia Domide <*****@*****.**> .. moduleauthor:: Yann Gordon <*****@*****.**> """ ## Make sure selected profile is propagated when launching an operation. import sys from tvb.basic.profile import TvbProfile as tvb_profile tvb_profile.set_profile(sys.argv) ### Overwrite PostgreSQL number of connections when executed in the context of a node from tvb.basic.config.settings import TVBSettings TVBSettings.MAX_DB_CONNECTIONS = TVBSettings.MAX_DB_ASYNC_CONNECTIONS TVBSettings.OPERATION_EXECUTION_PROCESS = True import matplotlib from tvb.basic.logger.builder import get_logger from tvb.core.adapters.abcadapter import ABCAdapter from tvb.core.entities.storage import dao from tvb.core.utils import parse_json_parameters from tvb.core.traits import db_events from tvb.core.services.operation_service import OperationService from tvb.core.services.workflow_service import WorkflowService
Launches the web server and configure the controllers for UI. .. moduleauthor:: Lia Domide <*****@*****.**> """ import os import sys import cherrypy import webbrowser from copy import copy from cherrypy import Tool from sys import platform, argv ### This will set running profile from arguments. from tvb.basic.profile import TvbProfile TvbProfile.set_profile(argv, True) from tvb.basic.config.settings import TVBSettings ### For Linux Distribution, correctly set MatplotLib Path, before start. if TVBSettings().is_linux(): os.environ['MATPLOTLIBDATA'] = os.path.join(TVBSettings().get_library_folder(), 'mpl-data') ### Import MPLH5 to have the back-end Thread started. from tvb.interfaces.web.mplh5 import mplh5_server from tvb.basic.logger.builder import get_logger LOGGER = get_logger('tvb.interfaces.web.mplh5.mplh5_server') mplh5_server.start_server(LOGGER) from tvb.core.adapters.abcdisplayer import ABCDisplayer from tvb.core.decorators import user_environment_execution from tvb.core.services.settings_service import SettingsService
# Jochen Mersmann, Anthony R. McIntosh, Viktor Jirsa (2013) # The Virtual Brain: a simulator of primate brain network dynamics. # Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010) # # """ Based on the Brunel and Wang model. .. moduleauthor:: Paula Sanz Leon <*****@*****.**> .. moduleauthor:: Marmaduke Woodman <*****@*****.**> .. moduleauthor:: Stuart A. Knock <*****@*****.**> """ from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.TEST_LIBRARY_PROFILE) import inspect import numpy from tvb.basic.neotraits.api import NArray, Range, List, Final import tvb.datatypes.lookup_tables as lookup_tables import tvb.simulator.models as models from tvb.simulator.common import get_logger LOG = get_logger(__name__) class BrunelWang(models.Model): """ .. [DJ_2012] Deco G and Jirsa V. *Ongoing Cortical Activity at Rest: Criticality, Multistability, and Ghost Attractors*.
# # """ Make use of the correlation_coefficient analyzer to compute functional connectivity, using the demo data at the region level. ``Run time``: ``Memory requirement``: .. moduleauthor:: Paula Sanz Leon <*****@*****.**> """ from tvb.basic.profile import TvbProfile TvbProfile.set_profile(["-profile", "LIBRARY_PROFILE"], try_reload=False) import numpy import tvb.datatypes.connectivity as connectivity import tvb.analyzers.correlation_coefficient as corr_coeff from tvb.datatypes.time_series import TimeSeriesRegion from tvb.basic.logger.builder import get_logger from tvb.simulator.plot.tools import * LOG = get_logger(__name__) #Load the demo region timeseries dataset try: data = numpy.load("demo_data_region_16s_2048Hz.npy") except IOError: