def configure(self, simulator_gid): """ Make preparations for the adapter launch. """ self.log.debug("%s: Instantiating requested simulator..." % str(self)) simulator_service = SimulatorService() self.algorithm, connectivity_gid, simulation_state_gid = simulator_service.deserialize_simulator( simulator_gid, self.storage_path) self.branch_simulation_state_gid = simulation_state_gid # for monitor in self.algorithm.monitors: # if issubclass(monitor, Projection): # # TODO: add a service that loads a RM with Surface and Connectivity # pass connectivity_index = dao.get_datatype_by_gid(connectivity_gid.hex) connectivity = h5.load_from_index(connectivity_index) connectivity.gid = connectivity_gid self.algorithm.connectivity = connectivity self.simulation_length = self.algorithm.simulation_length self.log.debug("%s: Initializing storage..." % str(self)) try: self.algorithm.preconfigure() except ValueError as err: raise LaunchException( "Failed to configure simulator due to invalid Input Values. It could be because " "of an incompatibility between different version of TVB code.", err)
def __init__(self): BurstBaseController.__init__(self) self.range_parameters = SimulatorRangeParameters() self.burst_service2 = BurstService2() self.simulator_service = SimulatorService() self.files_helper = FilesHelper() self.cached_simulator_algorithm = self.flow_service.get_algorithm_by_module_and_class( IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS)
def __init__(self): BurstBaseController.__init__(self) self.range_parameters = SimulatorRangeParameters() self.burst_service = BurstService() self.simulator_service = SimulatorService() self.cached_simulator_algorithm = self.algorithm_service.get_algorithm_by_module_and_class( IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) self.context = SimulatorContext() self.monitors_handler = MonitorsWizardHandler()
def fire_simulation(project_id, simulator): project = dao.get_project_by_id(project_id) assert isinstance(simulator, Simulator) # Load the SimulatorAdapter algorithm from DB cached_simulator_algorithm = FlowService().get_algorithm_by_module_and_class(IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) # Instantiate a SimulatorService and launch the configured simulation simulator_service = SimulatorService() launched_operation = simulator_service.async_launch_and_prepare_simulation(None, project.administrator, project, cached_simulator_algorithm, simulator, None) LOG.info("Operation launched ....") return launched_operation
class SimulationFacade: def __init__(self): self.logger = get_logger(self.__class__.__module__) self.simulator_service = SimulatorService() self.project_service = ProjectService() def launch_simulation(self, current_user_id, zip_directory, project_gid): try: project = self.project_service.find_project_lazy_by_gid(project_gid) except ProjectServiceException: raise InvalidIdentifierException() try: simulator_h5_name = DirLoader(zip_directory, None).find_file_for_has_traits_type(Simulator) simulator_file = os.path.join(zip_directory, simulator_h5_name) except IOError: raise InvalidInputException('No Simulator h5 file found in the archive') try: simulator_algorithm = AlgorithmService().get_algorithm_by_module_and_class(SimulatorAdapter.__module__, SimulatorAdapter.__name__) simulation = self.simulator_service.prepare_simulation_on_server(user_id=current_user_id, project=project, algorithm=simulator_algorithm, zip_folder_path=zip_directory, simulator_file=simulator_file) return simulation.gid except Exception as excep: self.logger.error(excep, exc_info=True) raise ServiceException(str(excep))
def fire_simulation(project_id=1, **kwargs): project = dao.get_project_by_id(project_id) # Prepare a Simulator instance with defaults and configure it to use the previously loaded Connectivity simulator = Simulator(**kwargs) # Load the SimulatorAdapter algorithm from DB cached_simulator_algorithm = FlowService( ).get_algorithm_by_module_and_class(IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) # Instantiate a SimulatorService and launch the configured simulation simulator_service = SimulatorService() launched_operation = simulator_service.async_launch_and_prepare_simulation( None, project.administrator, project, cached_simulator_algorithm, simulator, None) return launched_operation
def build(test_user, test_project, simulation_length=10, is_group=False): model = SimulatorAdapterModel() model.connectivity = connectivity_index_factory().gid model.simulation_length = simulation_length burst = BurstConfiguration(test_project.id, name="Sim " + str(datetime.now())) burst.start_time = datetime.now() algorithm = dao.get_algorithm_by_module(SIMULATOR_MODULE, SIMULATOR_CLASS) service = SimulatorService() if is_group: range_param = RangeParameter("conduction_speed", float, Range(lo=50.0, hi=100.0, step=20.0)) burst.range1 = range_param.to_json() burst = BurstService().prepare_burst_for_pse(burst) op = service.async_launch_and_prepare_pse(burst, test_user, test_project, algorithm, range_param, None, model) else: dao.store_entity(burst) op = service.async_launch_and_prepare_simulation(burst, test_user, test_project, algorithm, model) return op
def run_simulation(): log = get_logger(__name__) # This ID of a project needs to exists in DB, and it can be taken from the WebInterface: project = dao.get_project_by_id(1) # Find a structural Connectivity and load it in memory connectivity_index = dao.get_generic_entity( ConnectivityIndex, DataTypeMetaData.DEFAULT_SUBJECT, "subject")[0] connectivity = h5.load_from_index(connectivity_index) # 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_model = SimulatorAdapterModel() simulator_model.connectivity = connectivity.gid simulator_model.simulation_length = 100 simulator_model.coupling = Scaling() simulator_service = SimulatorService() burst = BurstConfiguration(project.id, name="Simulation") dao.store_entity(burst) launched_operation = simulator_service.async_launch_and_prepare_simulation( burst, project.administrator, project, cached_simulator_algorithm, simulator_model) # wait for the operation to finish while not launched_operation.has_finished: sleep(5) launched_operation = dao.get_operation_by_id(launched_operation.id) if launched_operation.status == STATUS_FINISHED: ts = dao.get_generic_entity(TimeSeriesRegionIndex, launched_operation.id, "fk_from_operation")[0] log.info("TimeSeries result is: %s " % ts) else: log.warning( "Operation ended with problems [%s]: [%s]" % (launched_operation.status, launched_operation.additional_info))
def test_load_burst_only(self): zip_path = path.join(path.dirname(tvb_data.__file__), 'connectivity', 'connectivity_66.zip') TestFactory.import_zip_connectivity(self.test_user, self.test_project, zip_path, "John") connectivity = TestFactory.get_entity(self.test_project, ConnectivityIndex) simulator_index = SimulatorIndex() simulator_index.fill_from_has_traits(self.session_stored_simulator) burst_config = BurstConfiguration(self.test_project.id, simulator_index.id) burst_config = dao.store_entity(burst_config) simulator_index.fk_from_operation = burst_config.id simulator_index = dao.store_entity(simulator_index) simulator_index.fk_parent_burst = burst_config.id simulator_index = dao.store_entity(simulator_index) burst = dao.get_bursts_for_project(self.test_project.id) self.sess_mock['burst_id'] = str(burst[0].id) self.sess_mock['_connectivity'] = connectivity.gid self.sess_mock['_conduction_speed'] = "3.0" self.sess_mock['_coupling'] = "Sigmoidal" with patch('cherrypy.session', self.sess_mock, create=True): common.add2session(common.KEY_SIMULATOR_CONFIG, self.session_stored_simulator) self.simulator_controller.set_connectivity(**self.sess_mock._data) self.simulator_controller.set_stimulus(**self.sess_mock._data) storage_path = FilesHelper().get_project_folder( self.test_project, str(simulator_index.fk_from_operation)) simulator_service = SimulatorService() SimulatorSerializer().serialize_simulator( self.session_stored_simulator, simulator_index.gid, None, storage_path) with patch('cherrypy.session', self.sess_mock, create=True): self.simulator_controller.load_burst_read_only(str(burst[0].id)) is_simulator_load = common.get_from_session(KEY_IS_SIMULATOR_LOAD) is_simulator_copy = common.get_from_session(KEY_IS_SIMULATOR_COPY) last_loaded_form_url = common.get_from_session( KEY_LAST_LOADED_FORM_URL) database_simulator = dao.get_generic_entity(SimulatorIndex, burst_config.id, 'fk_parent_burst')[0] assert simulator_index.gid == database_simulator.gid, "Simulator was not added correctly!" assert is_simulator_load, "Simulator Load Flag should be True!" assert not is_simulator_copy, "Simulator Copy Flag should be False!" assert last_loaded_form_url == '/burst/setup_pse', "Incorrect last form URL!"
def fire_simulation(project_id, simulator): project = dao.get_project_by_id(project_id) assert isinstance(simulator, Simulator) # 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) LOG.info("Operation launched ....") return launched_operation
class FireSimulationResource(RestResource): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.logger = get_logger(self.__class__.__module__) self.simulator_service = SimulatorService() self.project_service = ProjectService() @check_permission(ProjectAccessPermission, 'project_gid') def post(self, project_gid): """ :start a simulation using a project id and a zip archive with the simulator data serialized """ file = self.extract_file_from_request( request_file_key=RequestFileKey.SIMULATION_FILE_KEY.value, file_extension=FilesHelper.TVB_ZIP_FILE_EXTENSION) destination_folder = RestResource.get_destination_folder() zip_path = RestResource.save_temporary_file(file, destination_folder) try: project = self.project_service.find_project_lazy_by_gid( project_gid) except ProjectServiceException: raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE % project_gid) result = FilesHelper().unpack_zip(zip_path, os.path.dirname(zip_path)) if len(result) == 0: raise InvalidInputException("Empty zip archive") folder_path = os.path.dirname(result[0]) simulator_algorithm = FlowService().get_algorithm_by_module_and_class( SimulatorAdapter.__module__, SimulatorAdapter.__name__) try: simulator_h5_name = DirLoader( folder_path, None).find_file_for_has_traits_type(Simulator) simulator_file = os.path.join(folder_path, simulator_h5_name) except IOError: raise InvalidInputException( 'No Simulator h5 file found in the archive') try: current_user = get_current_user() operation = self.simulator_service.prepare_simulation_on_server( user_id=current_user.id, project=project, algorithm=simulator_algorithm, zip_folder_path=folder_path, simulator_file=simulator_file) except Exception as excep: self.logger.error(excep, exc_info=True) raise ServiceException(str(excep)) return operation.gid, HTTP_STATUS_CREATED
def configure(self, simulator_gid): """ Make preparations for the adapter launch. """ self.log.debug("%s: Configuring simulator adapter..." % str(self)) self.algorithm, history_gid = SimulatorService().deserialize_simulator( simulator_gid, self.storage_path) self.branch_simulation_state_gid = history_gid # for monitor in self.algorithm.monitors: # if issubclass(monitor, Projection): # # TODO: add a service that loads a RM with Surface and Connectivity # pass try: self.algorithm.preconfigure() except ValueError as err: raise LaunchException( "Failed to configure simulator due to invalid Input Values. It could be because " "of an incompatibility between different version of TVB code.", err)
class SimulatorController(BurstBaseController): KEY_IS_LOAD_AFTER_REDIRECT = "is_load_after_redirect" COPY_NAME_FORMAT = "copy_of_{}" BRANCH_NAME_FORMAT = "{}_branch{}" KEY_KEEP_SAME_SIM_WIZARD = "keep_same_wizard" def __init__(self): BurstBaseController.__init__(self) self.range_parameters = SimulatorRangeParameters() self.burst_service = BurstService() self.simulator_service = SimulatorService() self.cached_simulator_algorithm = self.algorithm_service.get_algorithm_by_module_and_class( IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) self.context = SimulatorContext() self.monitors_handler = MonitorsWizardHandler() @expose_json def cancel_or_remove_burst(self, burst_id): """ Cancel or Remove the burst entity given by burst_id (and all linked entities: op, DTs) :returns True: if the op was successfully. """ burst_config = BurstService.load_burst_configuration(int(burst_id)) op_id, is_group = burst_config.operation_info_for_burst_removal return self.cancel_or_remove_operation(op_id, is_group, burst_config.is_finished) def cancel_or_remove_operation(self, operation_id, is_group, remove_after_stop=False): """ Stop the operation given by operation_id. If is_group is true stop all the operations from that group. """ # Load before we remove, to have its data in memory here burst_config = BurstService.get_burst_for_operation_id( operation_id, is_group) if burst_config is not None: self.burst_service.mark_burst_finished( burst_config, BurstConfiguration.BURST_CANCELED, store_h5_file=False) while GROUP_BURST_PENDING.get(burst_config.id, False): pass GROUP_BURST_PENDING.pop(burst_config.id, False) result = OperationService.stop_operation(operation_id, is_group, remove_after_stop) if remove_after_stop: current_burst = self.context.burst_config if (current_burst is not None and burst_config is not None and current_burst.id == burst_config.id and ((current_burst.fk_simulation == operation_id and not is_group) or (current_burst.fk_operation_group == operation_id and is_group))): self.reset_simulator_configuration() if burst_config is not None: burst_config = BurstService.load_burst_configuration( burst_config.id) if burst_config: BurstService.remove_burst_configuration(burst_config.id) return result @expose_page @settings @context_selected def index(self): """Get on burst main page""" template_specification = dict( mainContent="burst/main_burst", title="Simulation Cockpit", includedResources='project/included_resources') if not self.context.last_loaded_fragment_url: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_CONNECTIVITY_URL) self.context.set_burst_config() _, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if self.context.burst_config.start_time is not None: is_simulation_load = True self.context.add_simulator_load_to_session(True) template_specification['burstConfig'] = self.context.burst_config template_specification[ 'burst_list'] = self.burst_service.get_available_bursts( self.context.project.id) form = self.prepare_first_fragment() rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_CONNECTIVITY_URL, None, is_simulation_copy, is_simulation_load, last_form_url=self.context.last_loaded_fragment_url, last_request_type=cherrypy.request.method, is_first_fragment=True, is_branch=is_branch) template_specification.update(**rendering_rules.to_dict()) cherrypy.response.headers[ 'Cache-Control'] = 'no-cache, no-store, must-revalidate' cherrypy.response.headers['Pragma'] = 'no-cache' cherrypy.response.headers['Expires'] = '0' return self.fill_default_attributes(template_specification) def prepare_first_fragment(self): self.context.set_simulator() simulator, _, _, is_branch = self.context.get_common_params() branch_conditions = self.simulator_service.compute_conn_branch_conditions( is_branch, simulator) form = self.algorithm_service.prepare_adapter_form( form_instance=SimulatorAdapterForm(), project_id=self.context.project.id, extra_conditions=branch_conditions) self.simulator_service.validate_first_fragment(form, self.context.project.id, ConnectivityIndex) form.fill_from_trait(self.context.simulator) return form @expose_json def set_fragment_url(self, **data): try: self.context.add_last_loaded_form_url_to_session(data['url']) except KeyError: self.logger.error( "Cannot set last loaded url to session because the required data was not found." ) @expose_fragment('simulator_fragment') def set_connectivity(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, _ = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_COUPLING_PARAMS_URL) form = SimulatorAdapterForm() form.fill_from_post(data) self.simulator_service.reset_at_connectivity_change( is_simulation_copy, form, session_stored_simulator) form.fill_trait(session_stored_simulator) next_form = self.algorithm_service.prepare_adapter_form( form_instance=get_form_for_coupling( type(session_stored_simulator.coupling))()) self.range_parameters.coupling_parameters = next_form.get_range_parameters( ) next_form.fill_from_trait(session_stored_simulator.coupling) rendering_rules = SimulatorFragmentRenderingRules( next_form, SimulatorWizzardURLs.SET_COUPLING_PARAMS_URL, SimulatorWizzardURLs.SET_CONNECTIVITY_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_coupling_params(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_SURFACE_URL) form = get_form_for_coupling( type(session_stored_simulator.coupling))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.coupling) surface_fragment = self.algorithm_service.prepare_adapter_form( form_instance=SimulatorSurfaceFragment(), project_id=self.context.project.id) surface_fragment.fill_from_trait(session_stored_simulator.surface) rendering_rules = SimulatorFragmentRenderingRules( surface_fragment, SimulatorWizzardURLs.SET_SURFACE_URL, SimulatorWizzardURLs.SET_COUPLING_PARAMS_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method, is_branch=is_branch) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_surface(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) rendering_rules = SimulatorFragmentRenderingRules( previous_form_action_url=SimulatorWizzardURLs.SET_SURFACE_URL, is_simulation_copy=is_simulation_copy, is_simulation_readonly_load=is_simulation_load, last_form_url=self.context.last_loaded_fragment_url, last_request_type=cherrypy.request.method, is_branch=is_branch) if cherrypy.request.method == POST_REQUEST: form = SimulatorSurfaceFragment() form.fill_from_post(data) self.simulator_service.reset_at_surface_change( is_simulation_copy, form, session_stored_simulator) form.fill_trait(session_stored_simulator) if session_stored_simulator.surface: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_CORTEX_URL) else: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_STIMULUS_URL) return SimulatorSurfaceFragment.prepare_next_fragment_after_surface( session_stored_simulator, rendering_rules, self.context.project.id, SimulatorWizzardURLs.SET_CORTEX_URL, SimulatorWizzardURLs.SET_STIMULUS_URL) @expose_fragment('simulator_fragment') def set_cortex(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, _ = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_STIMULUS_URL) rm_fragment = SimulatorRMFragment() rm_fragment.fill_from_post(data) rm_fragment.fill_trait(session_stored_simulator.surface) rendering_rules = SimulatorFragmentRenderingRules( None, None, SimulatorWizzardURLs.SET_CORTEX_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return SimulatorStimulusFragment.prepare_stimulus_fragment( session_stored_simulator, rendering_rules, True, SimulatorWizzardURLs.SET_STIMULUS_URL, self.context.project.id) @expose_fragment('simulator_fragment') def set_stimulus(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_MODEL_URL) stimuli_fragment = SimulatorStimulusFragment( session_stored_simulator.is_surface_simulation) stimuli_fragment.fill_from_post(data) stimuli_fragment.fill_trait(session_stored_simulator) model_fragment = self.algorithm_service.prepare_adapter_form( form_instance=SimulatorModelFragment()) model_fragment.fill_from_trait(session_stored_simulator) rendering_rules = SimulatorFragmentRenderingRules( model_fragment, SimulatorWizzardURLs.SET_MODEL_URL, SimulatorWizzardURLs.SET_STIMULUS_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method, is_model_fragment=True, is_surface_simulation=session_stored_simulator. is_surface_simulation, is_branch=is_branch) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_model(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: set_next_wizard = True if SimulatorController.KEY_KEEP_SAME_SIM_WIZARD in data: set_next_wizard = False if set_next_wizard: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_MODEL_PARAMS_URL) form = SimulatorModelFragment() form.fill_from_post(data) form.fill_trait(session_stored_simulator) form = self.algorithm_service.prepare_adapter_form( form_instance=get_form_for_model( type(session_stored_simulator.model))(is_branch)) self.range_parameters.model_parameters = form.get_range_parameters() form.fill_from_trait(session_stored_simulator.model) rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_MODEL_PARAMS_URL, SimulatorWizzardURLs.SET_MODEL_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_model_params(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_INTEGRATOR_URL) form = get_form_for_model(type( session_stored_simulator.model))(is_branch) if is_branch: data['variables_of_interest'] = list( session_stored_simulator.model.variables_of_interest) form.fill_from_post(data) form.fill_trait(session_stored_simulator.model) integrator_fragment = self.algorithm_service.prepare_adapter_form( form_instance=SimulatorIntegratorFragment()) integrator_fragment.integrator.display_subform = False integrator_fragment.fill_from_trait(session_stored_simulator) rendering_rules = SimulatorFragmentRenderingRules( integrator_fragment, SimulatorWizzardURLs.SET_INTEGRATOR_URL, SimulatorWizzardURLs.SET_MODEL_PARAMS_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method, is_branch=is_branch) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_integrator(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_INTEGRATOR_PARAMS_URL) fragment = SimulatorIntegratorFragment() fragment.fill_from_post(data) fragment.fill_trait(session_stored_simulator) form = self.algorithm_service.prepare_adapter_form( form_instance=get_form_for_integrator( type(session_stored_simulator.integrator))(is_branch)) if hasattr(form, 'noise'): form.noise.display_subform = False form.fill_from_trait(session_stored_simulator.integrator) rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_INTEGRATOR_PARAMS_URL, SimulatorWizzardURLs.SET_INTEGRATOR_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_integrator_params(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: form = get_form_for_integrator( type(session_stored_simulator.integrator))(is_branch) if is_branch: data['dt'] = str(session_stored_simulator.integrator.dt) form.fill_from_post(data) form.fill_trait(session_stored_simulator.integrator) if isinstance(session_stored_simulator.integrator, IntegratorStochasticViewModel): self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_NOISE_PARAMS_URL) else: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_MONITORS_URL) rendering_rules = SimulatorFragmentRenderingRules( None, None, SimulatorWizzardURLs.SET_INTEGRATOR_PARAMS_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method, is_noise_fragment=False) if not isinstance(session_stored_simulator.integrator, IntegratorStochasticViewModel): return self.monitors_handler.prepare_monitor_fragment( session_stored_simulator, rendering_rules, SimulatorWizzardURLs.SET_MONITORS_URL) integrator_noise_fragment = get_form_for_noise( type(session_stored_simulator.integrator.noise))() if hasattr(integrator_noise_fragment, 'equation'): integrator_noise_fragment.equation.display_subform = False self.range_parameters.integrator_noise_parameters = integrator_noise_fragment.get_range_parameters( ) integrator_noise_fragment.fill_from_trait( session_stored_simulator.integrator.noise) rendering_rules.form = integrator_noise_fragment rendering_rules.form_action_url = SimulatorWizzardURLs.SET_NOISE_PARAMS_URL rendering_rules.is_noise_fragment = True return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_noise_params(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: form = get_form_for_noise( type(session_stored_simulator.integrator.noise))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.integrator.noise) if isinstance(session_stored_simulator.integrator.noise, AdditiveNoiseViewModel): self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_MONITORS_URL) else: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_NOISE_EQUATION_PARAMS_URL) rendering_rules = SimulatorFragmentRenderingRules( None, None, SimulatorWizzardURLs.SET_NOISE_PARAMS_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return self.monitors_handler.prepare_next_fragment_after_noise( session_stored_simulator, is_branch, rendering_rules, SimulatorWizzardURLs.SET_MONITORS_URL, SimulatorWizzardURLs.SET_NOISE_EQUATION_PARAMS_URL) @expose_fragment('simulator_fragment') def set_noise_equation_params(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, _ = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_MONITORS_URL) form = get_form_for_equation( type(session_stored_simulator.integrator.noise.b))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.integrator.noise.b) rendering_rules = SimulatorFragmentRenderingRules( None, None, SimulatorWizzardURLs.SET_NOISE_EQUATION_PARAMS_URL, is_simulation_copy, is_simulation_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return self.monitors_handler.prepare_monitor_fragment( session_stored_simulator, rendering_rules, SimulatorWizzardURLs.SET_MONITORS_URL) @staticmethod def build_monitor_url(fragment_url, monitor): url_regex = '{}/{}' url = url_regex.format(fragment_url, monitor) return url def get_first_monitor_fragment_url(self, simulator, monitors_url): first_monitor = simulator.first_monitor if first_monitor is not None: monitor_vm_name = type(first_monitor).__name__ return self.build_monitor_url(monitors_url, monitor_vm_name) return SimulatorWizzardURLs.SETUP_PSE_URL @expose_fragment('simulator_fragment') def set_monitors(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, is_branch = self.context.get_common_params( ) if cherrypy.request.method == POST_REQUEST: fragment = SimulatorMonitorFragment( is_surface_simulation=session_stored_simulator. is_surface_simulation) fragment.fill_from_post(data) self.monitors_handler.set_monitors_list_on_simulator( session_stored_simulator, fragment.monitors.value) last_loaded_fragment_url = self.get_first_monitor_fragment_url( session_stored_simulator, SimulatorWizzardURLs.SET_MONITOR_PARAMS_URL) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( last_loaded_fragment_url) rendering_rules = SimulatorFragmentRenderingRules( is_simulation_copy=is_simulation_copy, is_simulation_readonly_load=is_simulation_load, last_request_type=cherrypy.request.method, form_action_url=last_loaded_fragment_url, previous_form_action_url=SimulatorWizzardURLs.SET_MONITORS_URL) return self.monitors_handler.get_fragment_after_monitors( session_stored_simulator, self.context.burst_config, self.context.project.id, is_branch, rendering_rules, SimulatorWizzardURLs.SETUP_PSE_URL) def get_url_after_monitors(self, current_monitor, monitor_name, next_monitor): if isinstance(current_monitor, BoldViewModel): return self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_EQUATION_URL, monitor_name) if next_monitor is not None: return self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_PARAMS_URL, type(next_monitor).__name__) return SimulatorWizzardURLs.SETUP_PSE_URL @staticmethod def get_url_for_final_fragment(burst_config): if burst_config.is_pse_burst(): return SimulatorWizzardURLs.LAUNCH_PSE_URL return SimulatorWizzardURLs.SETUP_PSE_URL def get_urls_for_next_monitor_fragment(self, next_monitor, current_monitor): form_action_url = self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_PARAMS_URL, type(next_monitor).__name__) if_bold_url = self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_EQUATION_URL, type(current_monitor).__name__) return form_action_url, if_bold_url @expose_fragment('simulator_fragment') def set_monitor_params(self, current_monitor_name, **data): session_stored_simulator, is_simulation_copy, is_simulator_load, is_branch = self.context.get_common_params( ) current_monitor, next_monitor = self.monitors_handler.get_current_and_next_monitor_form( current_monitor_name, session_stored_simulator) if cherrypy.request.method == POST_REQUEST: form = get_form_for_monitor(type(current_monitor))( session_stored_simulator, is_branch) if is_branch: data['period'] = str(current_monitor.period) data['variables_of_interest'] = [ session_stored_simulator.model.variables_of_interest[i] for i in current_monitor.variables_of_interest ] form.fill_from_post(data) form.fill_trait(current_monitor) last_loaded_form_url = self.get_url_after_monitors( current_monitor, current_monitor_name, next_monitor) self.context.add_last_loaded_form_url_to_session( last_loaded_form_url) previous_form_action_url = self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_PARAMS_URL, current_monitor_name) rendering_rules = SimulatorFragmentRenderingRules( is_simulation_copy=is_simulation_copy, is_simulation_readonly_load=is_simulator_load, last_request_type=cherrypy.request.method, last_form_url=self.context.last_loaded_fragment_url, previous_form_action_url=previous_form_action_url) form_action_url, if_bold_url = self.get_urls_for_next_monitor_fragment( next_monitor, current_monitor) self.monitors_handler.update_monitor(current_monitor) return self.monitors_handler.handle_next_fragment_for_monitors( self.context, rendering_rules, current_monitor, next_monitor, False, form_action_url, if_bold_url) def get_url_after_monitor_equation(self, next_monitor): if next_monitor is None: return SimulatorWizzardURLs.SETUP_PSE_URL last_loaded_fragment_url = self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_PARAMS_URL, type(next_monitor).__name__) return last_loaded_fragment_url @expose_fragment('simulator_fragment') def set_monitor_equation(self, current_monitor_name, **data): session_stored_simulator, is_simulation_copy, is_simulator_load, is_branch = self.context.get_common_params( ) current_monitor, next_monitor = self.monitors_handler.get_current_and_next_monitor_form( current_monitor_name, session_stored_simulator) if cherrypy.request.method == POST_REQUEST: form = get_form_for_equation(type(current_monitor.hrf_kernel))() form.fill_from_post(data) form.fill_trait(current_monitor.hrf_kernel) last_loaded_fragment_url = self.get_url_after_monitor_equation( next_monitor) self.context.add_last_loaded_form_url_to_session( last_loaded_fragment_url) previous_form_action_url = self.build_monitor_url( SimulatorWizzardURLs.SET_MONITOR_EQUATION_URL, current_monitor_name) rendering_rules = SimulatorFragmentRenderingRules( None, None, previous_form_action_url, is_simulation_copy, is_simulator_load, self.context.last_loaded_fragment_url, cherrypy.request) form_action_url, if_bold_url = self.get_urls_for_next_monitor_fragment( next_monitor, current_monitor) return self.monitors_handler.handle_next_fragment_for_monitors( self.context, rendering_rules, current_monitor, next_monitor, True, form_action_url, if_bold_url) @expose_fragment('simulator_fragment') def setup_pse(self, **data): session_stored_simulator, is_simulation_copy, is_simulator_load, _ = self.context.get_common_params( ) burst_config = self.context.burst_config all_range_parameters = self.range_parameters.get_all_range_parameters() next_form = self.algorithm_service.prepare_adapter_form( form_instance=SimulatorPSEConfigurationFragment( self.range_parameters.get_all_range_parameters())) if cherrypy.request.method == POST_REQUEST: session_stored_simulator.simulation_length = float( data['simulation_length']) burst_config.name = data['input_simulation_name_id'] self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.SET_PSE_PARAMS_URL) param1, param2 = self.burst_service.handle_range_params_at_loading( burst_config, all_range_parameters) if param1: param_dict = {'pse_param1': param1.name} if param2 is not None: param_dict['pse_param2'] = param2.name next_form.fill_from_post(param_dict) rendering_rules = SimulatorFragmentRenderingRules( next_form, SimulatorWizzardURLs.SET_PSE_PARAMS_URL, SimulatorWizzardURLs.SETUP_PSE_URL, is_simulation_copy, is_simulator_load, self.context.last_loaded_fragment_url, cherrypy.request.method) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def set_pse_params(self, **data): session_stored_simulator, is_simulation_copy, is_simulation_load, _ = self.context.get_common_params( ) burst_config = self.context.burst_config form = SimulatorPSEConfigurationFragment( self.range_parameters.get_all_range_parameters()) if cherrypy.request.method == POST_REQUEST: self.context.add_last_loaded_form_url_to_session( SimulatorWizzardURLs.LAUNCH_PSE_URL) form.fill_from_post(data) param1 = form.pse_param1.value burst_config.range1 = param1.to_json() param2 = None if form.pse_param2.value: param2 = form.pse_param2.value burst_config.range2 = param2.to_json() else: all_range_parameters = self.range_parameters.get_all_range_parameters( ) param1, param2 = self.burst_service.handle_range_params_at_loading( burst_config, all_range_parameters) next_form = self.algorithm_service.prepare_adapter_form( form_instance=SimulatorPSERangeFragment(param1, param2)) rendering_rules = SimulatorFragmentRenderingRules( next_form, SimulatorWizzardURLs.LAUNCH_PSE_URL, SimulatorWizzardURLs.SET_PSE_PARAMS_URL, is_simulation_copy, is_simulation_load, last_form_url=self.context.last_loaded_fragment_url, is_launch_pse_fragment=True) return rendering_rules.to_dict() @expose_json def launch_pse(self, **data): session_stored_simulator = self.context.simulator all_range_parameters = self.range_parameters.get_all_range_parameters() range_param1, range_param2 = SimulatorPSERangeFragment.fill_from_post( all_range_parameters, **data) burst_config = self.context.burst_config burst_config.start_time = datetime.now() burst_config.range1 = range_param1.to_json() if range_param2: burst_config.range2 = range_param2.to_json() burst_config = self.burst_service.prepare_burst_for_pse(burst_config) session_stored_simulator.operation_group_gid = uuid.UUID( burst_config.operation_group.gid) session_stored_simulator.ranges = json.dumps(burst_config.ranges) try: thread = threading.Thread( target=self.simulator_service.async_launch_and_prepare_pse, kwargs={ 'burst_config': burst_config, 'user': self.context.logged_user, 'project': self.context.project, 'simulator_algo': self.cached_simulator_algorithm, 'range_param1': range_param1, 'range_param2': range_param2, 'session_stored_simulator': session_stored_simulator }) thread.start() return {'id': burst_config.id} except BurstServiceException as e: self.logger.exception("Could not launch burst!") return {'error': e.message} @expose_json def launch_simulation(self, launch_mode, **data): current_form = SimulatorFinalFragment() burst_config = self.context.burst_config burst_config.range1 = None burst_config.range2 = None try: current_form.fill_from_post(data) except Exception as exc: self.logger.exception(exc) return {'error': str(exc)} burst_name = current_form.simulation_name.value session_stored_simulator = self.context.simulator session_stored_simulator.simulation_length = current_form.simulation_length.value if burst_name != 'none_undefined': burst_config.name = burst_name if launch_mode == self.burst_service.LAUNCH_BRANCH: simulation_state_index = self.simulator_service.get_simulation_state_index( burst_config, SimulationHistoryIndex) session_stored_simulator.history_gid = simulation_state_index[ 0].gid burst_config.start_time = datetime.now() session_burst_config = self.burst_service.store_burst(burst_config) try: thread = threading.Thread(target=self.simulator_service. async_launch_and_prepare_simulation, kwargs={ 'burst_config': session_burst_config, 'user': self.context.logged_user, 'project': self.context.project, 'simulator_algo': self.cached_simulator_algorithm, 'simulator': session_stored_simulator }) thread.start() return {'id': session_burst_config.id} except BurstServiceException as e: self.logger.exception('Could not launch burst!') return {'error': e.message} @expose_fragment('burst/burst_history') def load_burst_history(self, initBurst=None): """ Load the available burst that are stored in the database at this time. This is one alternative to 'chrome-back problem'. """ bursts = self.burst_service.get_available_bursts( self.context.project.id) self.burst_service.populate_burst_disk_usage(bursts) fromInit = False if initBurst is not None: fromInit = True return { 'burst_list': bursts, 'fromInit': fromInit, 'selectedBurst': self.context.burst_config.id, 'first_fragment_url': SimulatorFragmentRenderingRules.FIRST_FORM_URL } @cherrypy.expose def get_last_fragment_url(self, burst_config_id): burst_config = self.burst_service.load_burst_configuration( burst_config_id) return self.get_url_for_final_fragment(burst_config) @expose_fragment('simulator_fragment') def load_burst_read_only(self, burst_config_id): try: burst_config = self.burst_service.load_burst_configuration( burst_config_id) storage_path = StorageInterface().get_project_folder( self.context.project.name, str(burst_config.fk_simulation)) simulator = h5.load_view_model(burst_config.simulator_gid, storage_path) last_loaded_form_url = self.get_url_for_final_fragment( burst_config) self.context.init_session_at_burst_loading(burst_config, simulator, last_loaded_form_url) form = self.prepare_first_fragment() self.monitors_handler.build_list_of_monitors_from_view_models( self.context.simulator) rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_CONNECTIVITY_URL, is_simulation_readonly_load=True, is_first_fragment=True) return rendering_rules.to_dict() except Exception: # Most probably Burst was removed. Delete it from session, so that client # has a good chance to get a good response on refresh self.logger.exception("Error loading burst") self.context.remove_burst_config_from_session() raise def _prepare_first_fragment_for_burst_copy(self, burst_config_id, burst_name_format): simulator, burst_config_copy = self.burst_service.prepare_data_for_burst_copy( burst_config_id, burst_name_format, self.context.project) self.monitors_handler.build_list_of_monitors_from_view_models( simulator) last_loaded_form_url = self.get_url_for_final_fragment( burst_config_copy) self.context.init_session_at_copy_preparation(burst_config_copy, simulator, last_loaded_form_url) return self.prepare_first_fragment() @expose_fragment('simulator_fragment') def copy_simulator_configuration(self, burst_config_id): self.context.add_branch_and_copy_to_session(False, True) form = self._prepare_first_fragment_for_burst_copy( burst_config_id, self.COPY_NAME_FORMAT) rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_CONNECTIVITY_URL, is_simulation_copy=True, is_simulation_readonly_load=True, is_first_fragment=True) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def branch_simulator_configuration(self, burst_config_id): self.context.add_branch_and_copy_to_session(True, False) form = self._prepare_first_fragment_for_burst_copy( burst_config_id, self.BRANCH_NAME_FORMAT) rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_CONNECTIVITY_URL, is_simulation_copy=True, is_simulation_readonly_load=True, is_first_fragment=True) return rendering_rules.to_dict() @expose_fragment('simulator_fragment') def reset_simulator_configuration(self): burst_config = BurstConfiguration(self.context.project.id) self.context.init_session_at_sim_reset( burst_config, SimulatorWizzardURLs.SET_CONNECTIVITY_URL) self.monitors_handler.clear_next_monitors_dict() form = self.prepare_first_fragment() rendering_rules = SimulatorFragmentRenderingRules( form, SimulatorWizzardURLs.SET_CONNECTIVITY_URL, is_first_fragment=True) return rendering_rules.to_dict() @expose_json def rename_burst(self, burst_id, burst_name): """ Rename the burst given by burst_id, setting it's new name to burst_name. """ validation_result = SimulatorFinalFragment.is_burst_name_ok(burst_name) if validation_result is True: self.burst_service.rename_burst(burst_id, burst_name) return {'success': "Simulation successfully renamed!"} else: self.logger.exception(validation_result) return {'error': validation_result} @expose_json def get_history_status(self, **data): """ For each burst id received, get the status and return it. """ return self.burst_service.update_history_status( json.loads(data['burst_ids'])) @cherrypy.expose @handle_error(redirect=False) @check_user def export(self, burst_id): export_manager = ExportManager() export_zip = export_manager.export_simulator_configuration(burst_id) result_name = "tvb_simulation_" + str(burst_id) + ".zip" return serve_file(export_zip, "application/x-download", "attachment", result_name) @expose_fragment("overlay") def get_upload_overlay(self): template_specification = self.fill_overlay_attributes( None, "Upload", "Simulation ZIP", "burst/upload_burst_overlay", "dialog-upload") template_specification[ 'first_fragment_url'] = SimulatorWizzardURLs.SET_CONNECTIVITY_URL return self.fill_default_attributes(template_specification) @cherrypy.expose @handle_error(redirect=True) @check_user @settings def load_simulator_configuration_from_zip(self, **data): """Upload Simulator from previously exported ZIP file""" self.logger.debug("Uploading ..." + str(data)) last_loaded_form_url = SimulatorWizzardURLs.SETUP_PSE_URL try: upload_param = "uploadedfile" if upload_param in data and data[upload_param]: simulator, burst_config, sim_folder = self.burst_service.load_simulation_from_zip( data[upload_param], self.context.project) dts_folder = os.path.join( sim_folder, StorageInterface.EXPORTED_SIMULATION_DTS_DIR) ImportService().import_project_operations( self.context.project, dts_folder, False, None) self.monitors_handler.build_list_of_monitors_from_view_models( simulator) if burst_config.is_pse_burst(): last_loaded_form_url = SimulatorWizzardURLs.LAUNCH_PSE_URL self.context.init_session_at_sim_config_from_zip( burst_config, simulator, last_loaded_form_url) except IOError as ioexcep: self.logger.exception(ioexcep) self.context.set_warning_message( "This ZIP does not contain a complete simulator configuration") except ServicesBaseException as excep: self.logger.warning(excep.message) self.context.set_warning_message(excep.message) raise cherrypy.HTTPRedirect('/burst/')
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.logger = get_logger(self.__class__.__module__) self.simulator_service = SimulatorService() self.project_service = ProjectService()
simulator = Simulator() simulator.connectivity = connectivity # Configure the Simulator to use a Scaling type coupling simulator.coupling = Scaling() # Choose a higher value for the 'tau' parameter of the Generic2dOscillator model simulator.model.tau = numpy.array([2.0]) # Configure the simulation length simulator.simulation_length = 100 # 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() launched_operation = simulator_service.async_launch_and_prepare_simulation( BurstConfiguration(project.id), project.administrator, project, cached_simulator_algorithm, simulator) # wait for the operation to finish while not launched_operation.has_finished: sleep(5) launched_operation = dao.get_operation_by_id(launched_operation.id) if launched_operation.status == STATUS_FINISHED: ts = dao.get_generic_entity(TimeSeriesRegionIndex, launched_operation.id, "fk_from_operation")[0] log.info("TimeSeries result is: %s " % ts) else:
class SimulatorAdapter(ABCAsynchronous): """ Interface between the Simulator and the Framework. """ _ui_name = "Simulation Core" algorithm = None branch_simulation_state_gid = None # This is a list with the monitors that actually return multi dimensions for the state variable dimension. # We exclude from this for example EEG, MEG or Bold which return HAVE_STATE_VARIABLES = [ "GlobalAverage", "SpatialAverage", "Raw", "SubSample", "TemporalAverage" ] def __init__(self): super(SimulatorAdapter, self).__init__() self.log.debug("%s: Initialized..." % str(self)) def get_form_class(self): return SimulatorAdapterForm def get_output(self): """ :returns: list of classes for possible results of the Simulator. """ return [TimeSeriesIndex, SimulationHistoryIndex] def configure(self, simulator_gid): """ Make preparations for the adapter launch. """ self.log.debug("%s: Configuring simulator adapter..." % str(self)) self.algorithm, history_gid = SimulatorService().deserialize_simulator( simulator_gid, self.storage_path) self.branch_simulation_state_gid = history_gid # for monitor in self.algorithm.monitors: # if issubclass(monitor, Projection): # # TODO: add a service that loads a RM with Surface and Connectivity # pass try: self.algorithm.preconfigure() except ValueError as err: raise LaunchException( "Failed to configure simulator due to invalid Input Values. It could be because " "of an incompatibility between different version of TVB code.", err) def get_required_memory_size(self, **kwargs): """ Return the required memory to run this algorithm. """ return self.algorithm.memory_requirement() def get_required_disk_size(self, **kwargs): """ Return the required disk size this algorithm estimates it will take. (in kB) """ return self.algorithm.storage_requirement() / 2**10 def get_execution_time_approximation(self, **kwargs): """ Method should approximate based on input arguments, the time it will take for the operation to finish (in seconds). """ # This is just a brute approx so cluster nodes won't kill operation before # it's finished. This should be done with a higher grade of sensitivity # Magic number connecting simulation length to simulation computation time # This number should as big as possible, as long as it is still realistic, to magic_number = 6.57e-06 # seconds approx_number_of_nodes = 500 approx_nvar = 15 approx_modes = 15 approx_integrator_dt = self.algorithm.integrator.dt if approx_integrator_dt == 0.0: approx_integrator_dt = 1.0 if self.algorithm.is_surface_simulation: approx_number_of_nodes *= approx_number_of_nodes estimation = (magic_number * approx_number_of_nodes * approx_nvar * approx_modes * self.algorithm.simulation_length / approx_integrator_dt) return max(int(estimation), 1) def _try_find_mapping(self, mapping_class, connectivity_gid): """ Try to find a DataType instance of class "mapping_class", linked to the given Connectivity. Entities in the current project will have priority. :param mapping_class: DT class, with field "_connectivity" on it :param connectivity_gid: GUID :return: None or instance of "mapping_class" """ dts_list = dao.get_generic_entity(mapping_class, connectivity_gid, 'connectivity_gid') if len(dts_list) < 1: return None for dt in dts_list: dt_operation = dao.get_operation_by_id(dt.fk_from_operation) if dt_operation.fk_launched_in == self.current_project_id: return dt return dts_list[0] def _try_load_region_mapping(self): region_map = None region_volume_map = None region_map_index = self._try_find_mapping( RegionMappingIndex, self.algorithm.connectivity.gid.hex) region_volume_map_index = self._try_find_mapping( RegionVolumeMappingIndex, self.algorithm.connectivity.gid.hex) if region_map_index: region_map = h5.load_from_index(region_map_index) if region_volume_map_index: region_volume_map = h5.load_from_index(region_volume_map_index) return region_map, region_volume_map def launch(self, simulator_gid): """ Called from the GUI to launch a simulation. *: string class name of chosen model, etc... *_parameters: dictionary of parameters for chosen model, etc... connectivity: tvb.datatypes.connectivity.Connectivity object. surface: tvb.datatypes.surfaces.CorticalSurface: or None. stimulus: tvb.datatypes.patters.* object """ result_h5 = dict() result_indexes = dict() start_time = self.algorithm.current_step * self.algorithm.integrator.dt self.algorithm.configure(full_configure=False) if self.branch_simulation_state_gid is not None: history_index = dao.get_datatype_by_gid( self.branch_simulation_state_gid.hex) history = h5.load_from_index(history_index) assert isinstance(history, SimulationHistory) history.fill_into(self.algorithm) region_map, region_volume_map = self._try_load_region_mapping() for monitor in self.algorithm.monitors: m_name = monitor.__class__.__name__ ts = monitor.create_time_series(self.algorithm.connectivity, self.algorithm.surface, region_map, region_volume_map) self.log.debug("Monitor created the TS") ts.start_time = start_time ts_index_class = h5.REGISTRY.get_index_for_datatype(type(ts)) ts_index = ts_index_class() ts_index.fill_from_has_traits(ts) ts_index.data_ndim = 4 ts_index.state = 'INTERMEDIATE' # state_variable_dimension_name = ts.labels_ordering[1] # if ts_index.user_tag_1: # ts_index.labels_dimensions[state_variable_dimension_name] = ts.user_tag_1.split(';') # elif m_name in self.HAVE_STATE_VARIABLES: # selected_vois = [self.algorithm.model.variables_of_interest[idx] for idx in monitor.voi] # ts.labels_dimensions[state_variable_dimension_name] = selected_vois ts_h5_class = h5.REGISTRY.get_h5file_for_datatype(type(ts)) ts_h5_path = h5.path_for(self.storage_path, ts_h5_class, ts.gid) ts_h5 = ts_h5_class(ts_h5_path) ts_h5.store(ts, scalars_only=True, store_references=False) ts_h5.sample_rate.store(ts.sample_rate) ts_h5.nr_dimensions.store(ts_index.data_ndim) if self.algorithm.surface: ts_index.surface_gid = self.algorithm.surface.region_mapping_data.surface.gid.hex ts_h5.surface.store(self.algorithm.surface.gid) else: ts_index.connectivity_gid = self.algorithm.connectivity.gid.hex ts_h5.connectivity.store(self.algorithm.connectivity.gid) if region_map: ts_index.region_mapping_gid = region_map.gid.hex ts_h5.region_mapping.store(region_map.gid) if region_volume_map: ts_index.region_mapping_volume_gid = region_volume_map.gid.hex ts_h5.region_mapping_volume.store(region_volume_map.gid) result_indexes[m_name] = ts_index result_h5[m_name] = ts_h5 # Run simulation self.log.debug("Starting simulation...") for result in self.algorithm( simulation_length=self.algorithm.simulation_length): for j, monitor in enumerate(self.algorithm.monitors): if result[j] is not None: m_name = monitor.__class__.__name__ ts_h5 = result_h5[m_name] ts_h5.write_time_slice([result[j][0]]) ts_h5.write_data_slice([result[j][1]]) self.log.debug( "Completed simulation, starting to store simulation state ") # Now store simulator history, at the simulation end results = [] if not self._is_group_launch(): simulation_history = SimulationHistory() simulation_history.populate_from(self.algorithm) history_index = h5.store_complete(simulation_history, self.storage_path) results.append(history_index) self.log.debug("Simulation state persisted, returning results ") for monitor in self.algorithm.monitors: m_name = monitor.__class__.__name__ ts_shape = result_h5[m_name].read_data_shape() result_indexes[m_name].fill_shape(ts_shape) result_h5[m_name].close() # self.log.info("%s: Adapter simulation finished!!" % str(self)) results.extend(result_indexes.values()) return results def _validate_model_parameters(self, model_instance, connectivity, surface): """ Checks if the size of the model parameters is set correctly. """ ui_configurable_params = model_instance.ui_configurable_parameters for param in ui_configurable_params: param_value = eval('model_instance.' + param) if isinstance(param_value, numpy.ndarray): if len(param_value) == 1 or connectivity is None: continue if surface is not None: if (len(param_value) != surface.number_of_vertices and len(param_value) != connectivity.number_of_regions): msg = str(surface.number_of_vertices) + ' or ' + str( connectivity.number_of_regions) msg = self._get_exception_message( param, msg, len(param_value)) self.log.error(msg) raise LaunchException(msg) elif len(param_value) != connectivity.number_of_regions: msg = self._get_exception_message( param, connectivity.number_of_regions, len(param_value)) self.log.error(msg) raise LaunchException(msg) @staticmethod def _get_exception_message(param_name, expected_size, actual_size): """ Creates the message that will be displayed to the user when the size of a model parameter is incorrect. """ msg = "The length of the parameter '" + param_name + "' is not correct." msg += " It is expected to be an array of length " + str( expected_size) + "." msg += " It is an array of length " + str(actual_size) + "." return msg @staticmethod def _is_surface_simulation(surface, surface_parameters): """ Is this a surface simulation? """ return surface is not None and surface_parameters is not None
def __init__(self): self.logger = get_logger(self.__class__.__module__) self.simulator_service = SimulatorService() self.project_service = ProjectService()
simulator = Simulator() simulator.connectivity = connectivity # Configure the Simulator to use a Scaling type coupling simulator.coupling = Scaling() # Choose a higher value for the 'tau' parameter of the Generic2dOscillator model simulator.model.tau = numpy.array([2.0]) # Configure the simulation length simulator.simulation_length = 100 # Load the SimulatorAdapter algorithm from DB cached_simulator_algorithm = FlowService( ).get_algorithm_by_module_and_class(IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) # Instantiate a SimulatorService and launch the configured simulation simulator_service = SimulatorService() launched_operation = simulator_service.async_launch_and_prepare_simulation( None, project.administrator, project, cached_simulator_algorithm, simulator, None) # wait for the operation to finish while not launched_operation.has_finished: sleep(5) launched_operation = dao.get_operation_by_id(launched_operation.id) if launched_operation.status == STATUS_FINISHED: ts = dao.get_generic_entity(TimeSeriesRegionIndex, launched_operation.id, "fk_from_operation")[0] log.info("TimeSeries result is: %s " % ts) else:
class SimulatorController(BurstBaseController): ACTION_KEY = 'action' PREVIOUS_ACTION_KEY = 'previous_action' FORM_KEY = 'form' IS_MODEL_FRAGMENT_KEY = 'is_model_fragment' IS_SURFACE_SIMULATION_KEY = 'is_surface_simulation' IS_FIRST_FRAGMENT_KEY = 'is_first_fragment' IS_LAST_FRAGMENT_KEY = 'is_last_fragment' IS_COPY = 'sim_copy' IS_LOAD = 'sim_load' dict_to_render = { ACTION_KEY: None, PREVIOUS_ACTION_KEY: None, FORM_KEY: None, IS_MODEL_FRAGMENT_KEY: False, IS_SURFACE_SIMULATION_KEY: False, IS_FIRST_FRAGMENT_KEY: False, IS_LAST_FRAGMENT_KEY: False, IS_COPY: False, IS_LOAD: False } def __init__(self): BurstBaseController.__init__(self) self.range_parameters = SimulatorRangeParameters() self.burst_service2 = BurstService2() self.simulator_service = SimulatorService() self.files_helper = FilesHelper() self.cached_simulator_algorithm = self.flow_service.get_algorithm_by_module_and_class( IntrospectionRegistry.SIMULATOR_MODULE, IntrospectionRegistry.SIMULATOR_CLASS) @expose_page @settings @context_selected def index(self): """Get on burst main page""" template_specification = dict( mainContent="burst/main_burst", title="Simulation Cockpit", baseUrl=TvbProfile.current.web.BASE_URL, includedResources='project/included_resources') project = common.get_current_project() burst_config = BurstConfiguration2(project.id) common.add2session(common.KEY_BURST_CONFIG, burst_config) template_specification['burstConfig'] = burst_config template_specification[ 'burst_list'] = self.burst_service2.get_available_bursts( common.get_current_project().id) portlets_list = [] # self.burst_service.get_available_portlets() template_specification['portletList'] = portlets_list template_specification['selectedPortlets'] = json.dumps(portlets_list) form = self.prepare_first_fragment() dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.IS_FIRST_FRAGMENT_KEY] = True dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = "/burst/set_connectivity" template_specification.update(**dict_to_render) return self.fill_default_attributes(template_specification) def prepare_first_fragment(self): is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) if is_simulator_copy is None: is_simulator_copy = False adapter_instance = ABCAdapter.build_adapter( self.cached_simulator_algorithm) form = adapter_instance.get_form()('', common.get_current_project().id, is_simulator_copy) session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) if session_stored_simulator is None: session_stored_simulator = Simulator( ) # self.burst_service.new_burst_configuration(common.get_current_project().id) common.add2session(common.KEY_SIMULATOR_CONFIG, session_stored_simulator) form.fill_from_trait(session_stored_simulator) return form @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_connectivity(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) form = SimulatorAdapterForm() if cherrypy.request.method == 'POST': is_simulator_copy = False form.fill_from_post(data) connectivity_index_gid = form.connectivity.value conduction_speed = form.conduction_speed.value coupling = form.coupling.value connectivity_index = ABCAdapter.load_entity_by_gid( connectivity_index_gid) connectivity = h5.load_from_index(connectivity_index) # TODO: handle this cases in a better manner session_stored_simulator.connectivity = connectivity session_stored_simulator.conduction_speed = conduction_speed session_stored_simulator.coupling = coupling() next_form = get_form_for_coupling( type(session_stored_simulator.coupling))() self.range_parameters.coupling_parameters = next_form.get_range_parameters( ) next_form.fill_from_trait(session_stored_simulator.coupling) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/set_coupling_params' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_connectivity' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_coupling_params(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_coupling( type(session_stored_simulator.coupling))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.coupling) surface_fragment = SimulatorSurfaceFragment( '', common.get_current_project().id) surface_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = surface_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_surface' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_coupling_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_surface(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) dict_to_render = copy.deepcopy(self.dict_to_render) surface_index = None if cherrypy.request.method == 'POST': is_simulator_copy = False form = SimulatorSurfaceFragment() form.fill_from_post(data) surface_index_gid = form.surface.value # surface_index_gid = data['_surface'] if surface_index_gid is None: session_stored_simulator.surface = None else: surface_index = ABCAdapter.load_entity_by_gid( surface_index_gid) session_stored_simulator.surface = Cortex() if session_stored_simulator.surface is None: stimuli_fragment = SimulatorStimulusFragment( '', common.get_current_project().id, False) stimuli_fragment.fill_from_trait(session_stored_simulator) dict_to_render[self.FORM_KEY] = stimuli_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_stimulus' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_surface' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render # TODO: work-around this situation: surf_index filter rm_fragment = SimulatorRMFragment('', common.get_current_project().id, surface_index) rm_fragment.fill_from_trait(session_stored_simulator) dict_to_render[self.FORM_KEY] = rm_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_cortex' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_surface' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_cortex(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False rm_fragment = SimulatorRMFragment() rm_fragment.fill_from_post(data) session_stored_simulator.surface.coupling_strength = rm_fragment.coupling_strength.data lc_gid = rm_fragment.lc.value if lc_gid == 'None': lc_index = ABCAdapter.load_entity_by_gid(lc_gid) lc = h5.load_from_index(lc_index) session_stored_simulator.surface.local_connectivity = lc rm_gid = rm_fragment.rm.value rm_index = ABCAdapter.load_entity_by_gid(rm_gid) rm = h5.load_from_index(rm_index) session_stored_simulator.surface.region_mapping_data = rm stimuli_fragment = SimulatorStimulusFragment( '', common.get_current_project().id, True) stimuli_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = stimuli_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_stimulus' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_cortex' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_stimulus(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False stimuli_fragment = SimulatorStimulusFragment( '', common.get_current_project().id, session_stored_simulator.is_surface_simulation) stimuli_fragment.fill_from_post(data) stimulus_gid = stimuli_fragment.stimulus.value if stimulus_gid != None: stimulus_index = ABCAdapter.load_entity_by_gid(stimulus_gid) stimulus = h5.load_from_index(stimulus_index) session_stored_simulator.stimulus = stimulus model_fragment = SimulatorModelFragment( '', common.get_current_project().id) model_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = model_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_model' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_stimulus' dict_to_render[self.IS_MODEL_FRAGMENT_KEY] = True dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load dict_to_render[ self. IS_SURFACE_SIMULATION_KEY] = session_stored_simulator.is_surface_simulation return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_model(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = SimulatorModelFragment() form.fill_from_post(data) session_stored_simulator.model = form.model.value() form = get_form_for_model(type(session_stored_simulator.model))() self.range_parameters.model_parameters = form.get_range_parameters() form.fill_from_trait(session_stored_simulator.model) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = '/burst/set_model_params' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_model' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_model_params(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_model(type(session_stored_simulator.model))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.model) integrator_fragment = SimulatorIntegratorFragment( '', common.get_current_project().id) integrator_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = integrator_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_integrator' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_model_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render # TODO: add state_variables selection step # @cherrypy.expose # @using_jinja_template("wizzard_form") # @handle_error(redirect=False) # @check_user # def set_model_variables_to_monitor(self, data): # session_stored_simulator = common.get_from_session(common.KEY_SIMULATOR_CONFIG) # form = get_form_for_model(type(session_stored_simulator.model.variables))() # form.fill_from_post(data) # # form.fill_trait(session_stored_simulator.model) # # integrator_fragment = SimulatorIntegratorFragment('', common.get_current_project().id) # # return {'form': integrator_fragment, 'action': '/burst/set_integrator', # 'previous_action': '/burst/set_model_variables_to_monitor'} @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_integrator(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False fragment = SimulatorIntegratorFragment() fragment.fill_from_post(data) session_stored_simulator.integrator = fragment.integrator.value() form = get_form_for_integrator( type(session_stored_simulator.integrator))() form.fill_from_trait(session_stored_simulator.integrator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = '/burst/set_integrator_params' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_integrator' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_integrator_params(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_integrator( type(session_stored_simulator.integrator))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.integrator) if isinstance(session_stored_simulator.integrator, IntegratorStochastic): integrator_noise_fragment = get_form_for_noise( type(session_stored_simulator.integrator.noise))() self.range_parameters.integrator_noise_parameters = integrator_noise_fragment.get_range_parameters( ) integrator_noise_fragment.fill_from_trait( session_stored_simulator.integrator.noise) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = integrator_noise_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_noise_params' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_integrator_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render monitor_fragment = SimulatorMonitorFragment( '', common.get_current_project().id) monitor_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = monitor_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_monitors' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_integrator_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_noise_params(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_noise( type(session_stored_simulator.integrator.noise))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.integrator.noise) if isinstance(session_stored_simulator.integrator.noise, Additive): monitor_fragment = SimulatorMonitorFragment( '', common.get_current_project().id) monitor_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = monitor_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_monitors' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_noise_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render equation_form = get_form_for_equation( type(session_stored_simulator.integrator.noise.b))() equation_form.equation.data = session_stored_simulator.integrator.noise.b.__class__.__name__ dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = equation_form dict_to_render[self.ACTION_KEY] = '/burst/set_noise_equation_params' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_noise_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_noise_equation_params(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_equation( type(session_stored_simulator.integrator.noise.b))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.integrator.noise.b) monitor_fragment = SimulatorMonitorFragment( '', common.get_current_project().id) monitor_fragment.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = monitor_fragment dict_to_render[self.ACTION_KEY] = '/burst/set_monitors' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_noise_equation_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_monitors(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False # TODO: handle multiple monitors fragment = SimulatorMonitorFragment() fragment.fill_from_post(data) session_stored_simulator.monitors = [fragment.monitor.value()] monitor = session_stored_simulator.monitors[0] form = get_form_for_monitor(type(monitor))( '', common.get_current_project().id) form.fill_from_trait(monitor) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = '/burst/set_monitor_params' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_monitors' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_monitor_params(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) monitor = session_stored_simulator.monitors[0] is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_monitor(type(monitor))() form.fill_from_post(data) form.fill_trait(monitor) if isinstance(monitor, Bold): next_form = get_form_for_equation(type(monitor.hrf_kernel))() next_form.fill_from_trait( session_stored_simulator.monitors[0].hrf_kernel) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/set_monitor_equation' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_monitor_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load return dict_to_render session_stored_simulator.monitors = [monitor] next_form = SimulatorLengthFragment() next_form.fill_from_trait(session_stored_simulator) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/set_simulation_length' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_monitor_params' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load if is_simulator_load: dict_to_render[self.ACTION_KEY] = '' dict_to_render[self.IS_LAST_FRAGMENT_KEY] = True return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_monitor_equation(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) if cherrypy.request.method == 'POST': is_simulator_copy = False form = get_form_for_monitor( type(session_stored_simulator.monitors[0]))() form.fill_from_post(data) form.fill_trait(session_stored_simulator.monitor.hrf_kernel) next_form = SimulatorLengthFragment() dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/set_simulation_length' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_monitor_equation' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LOAD] = is_simulator_load if is_simulator_load: dict_to_render[self.ACTION_KEY] = '' dict_to_render[self.IS_LAST_FRAGMENT_KEY] = True return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_simulation_length(self, **data): session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) is_simulator_load = common.get_from_session( common.KEY_IS_SIMULATOR_LOAD) session_burst_config = common.get_from_session(common.KEY_BURST_CONFIG) dict_to_render = copy.deepcopy(self.dict_to_render) if is_simulator_load: common.add2session(common.KEY_IS_SIMULATOR_LOAD, False) next_form = SimulatorFinalFragment() if session_burst_config.name: burst_name = session_burst_config.name copy_prefix = 'Copy of ' if is_simulator_copy and burst_name.find(copy_prefix) < 0: burst_name = copy_prefix + burst_name next_form.simulation_name.data = burst_name if cherrypy.request.method == 'POST': is_simulator_copy = False fragment = SimulatorLengthFragment() fragment.fill_from_post(data) session_stored_simulator.simulation_length = fragment.length.value dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/setup_pse' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_simulation_length' dict_to_render[self.IS_COPY] = is_simulator_copy dict_to_render[self.IS_LAST_FRAGMENT_KEY] = True return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def setup_pse(self, **data): next_form = SimulatorPSEConfigurationFragment( self.range_parameters.get_all_range_parameters()) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/set_pse_params' dict_to_render[ self.PREVIOUS_ACTION_KEY] = '/burst/set_simulation_length' return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def set_pse_params(self, **data): form = SimulatorPSEConfigurationFragment( self.range_parameters.get_all_range_parameters()) form.fill_from_post(data) param1 = form.pse_param1.value param2 = None if not form.pse_param2.value == form.pse_param2.missing_value: param2 = form.pse_param2.value project_id = common.get_current_project().id next_form = SimulatorPSEParamRangeFragment(param1, param2, project_id=project_id) dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.FORM_KEY] = next_form dict_to_render[self.ACTION_KEY] = '/burst/launch_pse' dict_to_render[self.PREVIOUS_ACTION_KEY] = '/burst/set_pse_params' return dict_to_render @cherrypy.expose @handle_error(redirect=False) @check_user def launch_pse(self, **data): # TODO: Split into: set range values and Launch, show message with finished config and nr of simulations all_range_parameters = self.range_parameters.get_all_range_parameters() range_param1, range_param2 = SimulatorPSEParamRangeFragment.fill_from_post( all_range_parameters, **data) session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) project = common.get_current_project() user = common.get_logged_user() burst_config = common.get_from_session(common.KEY_BURST_CONFIG) burst_config.start_time = datetime.now() # if burst_name != 'none_undefined': # burst_config.name = burst_name # TODO: branch simulation name is different if burst_config.name is None: new_id = dao.get_max_burst_id() + 1 burst_config.name = 'simulation_' + str(new_id) operation_group = OperationGroup( project.id, ranges=[range_param1.to_json(), range_param2.to_json()]) operation_group = dao.store_entity(operation_group) metric_operation_group = OperationGroup( project.id, ranges=[range_param1.to_json(), range_param2.to_json()]) metric_operation_group = dao.store_entity(metric_operation_group) burst_config.operation_group = operation_group burst_config.operation_group_id = operation_group.id burst_config.metric_operation_group = metric_operation_group burst_config.metric_operation_group_id = metric_operation_group.id dao.store_entity(burst_config) try: thread = threading.Thread( target=self.simulator_service.async_launch_and_prepare_pse, kwargs={ 'burst_config': burst_config, 'user': user, 'project': project, 'simulator_algo': self.cached_simulator_algorithm, 'range_param1': range_param1, 'range_param2': range_param2, 'session_stored_simulator': session_stored_simulator }) thread.start() except BurstServiceException as e: self.logger.exception("Could not launch burst!") return {'error': e.message} @expose_json def launch_simulation(self, launch_mode, **data): current_form = SimulatorFinalFragment() try: current_form.fill_from_post(data) except Exception as exc: self.logger.exception(exc) return {'error': str(exc)} burst_name = current_form.simulation_name.value session_stored_simulator = common.get_from_session( common.KEY_SIMULATOR_CONFIG) is_simulator_copy = common.get_from_session( common.KEY_IS_SIMULATOR_COPY) project = common.get_current_project() user = common.get_logged_user() session_burst_config = common.get_from_session(common.KEY_BURST_CONFIG) if burst_name != 'none_undefined': session_burst_config.name = burst_name burst_config_to_store = session_burst_config simulation_state_index_gid = None if launch_mode == self.simulator_service.LAUNCH_NEW: if session_burst_config.name is None: new_id = dao.get_max_burst_id() + 1 session_burst_config.name = 'simulation_' + str(new_id) if is_simulator_copy: burst_config_to_store = session_burst_config.clone() else: burst_config_to_store = session_burst_config.clone() count = dao.count_bursts_with_name(session_burst_config.name, session_burst_config.project_id) session_burst_config.name = session_burst_config.name + "_" + launch_mode + str( count) simulation_state_index = dao.get_generic_entity( SimulationStateIndex.__module__ + "." + SimulationStateIndex.__name__, session_burst_config.id, "fk_parent_burst") if simulation_state_index is None or len( simulation_state_index) < 1: exc = BurstServiceException( "Simulation State not found for %s, thus we are unable to branch from " "it!" % session_burst_config.name) self.logger.error(exc) raise exc simulation_state_index_gid = simulation_state_index[0].gid burst_config_to_store.start_time = datetime.now() dao.store_entity(burst_config_to_store) try: thread = threading.Thread(target=self.simulator_service. async_launch_and_prepare_simulation, kwargs={ 'burst_config': burst_config_to_store, 'user': user, 'project': project, 'simulator_algo': self.cached_simulator_algorithm, 'session_stored_simulator': session_stored_simulator, 'simulation_state_gid': simulation_state_index_gid }) thread.start() return {'id': burst_config_to_store.id} except BurstServiceException as e: self.logger.exception('Could not launch burst!') return {'error': e.message} @expose_fragment('burst/burst_history') def load_burst_history(self): """ Load the available burst that are stored in the database at this time. This is one alternative to 'chrome-back problem'. """ session_burst = common.get_from_session(common.KEY_BURST_CONFIG) bursts = self.burst_service2.get_available_bursts( common.get_current_project().id) self.burst_service2.populate_burst_disk_usage(bursts) return {'burst_list': bursts, 'selectedBurst': session_burst.id} @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def load_burst_read_only(self, burst_config_id): try: burst_config = dao.get_burst_by_id(burst_config_id) common.add2session(common.KEY_BURST_CONFIG, burst_config) simulator_index = dao.get_generic_entity(SimulatorIndex, burst_config.id, 'fk_parent_burst')[0] simulator_gid = simulator_index.gid project = common.get_current_project() storage_path = self.files_helper.get_project_folder( project, str(simulator_index.fk_from_operation)) simulator, _, _ = self.simulator_service.deserialize_simulator( simulator_gid, storage_path) session_stored_simulator = simulator common.add2session(common.KEY_SIMULATOR_CONFIG, session_stored_simulator) common.add2session(common.KEY_IS_SIMULATOR_LOAD, True) common.add2session(common.KEY_IS_SIMULATOR_COPY, False) form = self.prepare_first_fragment() dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.IS_FIRST_FRAGMENT_KEY] = True dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = "/burst/set_connectivity" dict_to_render[self.IS_LOAD] = True return dict_to_render except Exception: ### Most probably Burst was removed. Delete it from session, so that client ### has a good chance to get a good response on refresh self.logger.exception("Error loading burst") common.remove_from_session(common.KEY_BURST_CONFIG) raise @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def copy_simulator_configuration(self, burst_config_id): burst_config = dao.get_burst_by_id(burst_config_id) common.add2session(common.KEY_BURST_CONFIG, burst_config) simulator_index = dao.get_generic_entity(SimulatorIndex, burst_config.id, 'fk_parent_burst')[0] simulator_gid = simulator_index.gid project = common.get_current_project() storage_path = self.files_helper.get_project_folder( project, str(simulator_index.fk_from_operation)) simulator, _, _ = self.simulator_service.deserialize_simulator( simulator_gid, storage_path) session_stored_simulator = simulator common.add2session(common.KEY_SIMULATOR_CONFIG, session_stored_simulator) common.add2session(common.KEY_IS_SIMULATOR_COPY, True) common.add2session(common.KEY_IS_SIMULATOR_LOAD, False) form = self.prepare_first_fragment() dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.IS_FIRST_FRAGMENT_KEY] = True dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = "/burst/set_connectivity" dict_to_render[self.IS_COPY] = True return dict_to_render @cherrypy.expose @using_jinja_template("wizzard_form") @handle_error(redirect=False) @check_user def reset_simulator_configuration(self): common.add2session(common.KEY_SIMULATOR_CONFIG, None) common.add2session(common.KEY_IS_SIMULATOR_COPY, False) common.add2session(common.KEY_IS_SIMULATOR_LOAD, False) project = common.get_current_project() common.add2session(common.KEY_BURST_CONFIG, BurstConfiguration2(project.id)) form = self.prepare_first_fragment() dict_to_render = copy.deepcopy(self.dict_to_render) dict_to_render[self.IS_FIRST_FRAGMENT_KEY] = True dict_to_render[self.FORM_KEY] = form dict_to_render[self.ACTION_KEY] = "/burst/set_connectivity" return dict_to_render @expose_json def rename_burst(self, burst_id, burst_name): """ Rename the burst given by burst_id, setting it's new name to burst_name. """ validation_result = SimulatorFinalFragment.is_burst_name_ok(burst_name) if validation_result is True: self.burst_service2.rename_burst(burst_id, burst_name) return {'success': "Simulation successfully renamed!"} else: self.logger.exception(validation_result) return {'error': validation_result} @expose_json def get_history_status(self, **data): """ For each burst id received, get the status and return it. """ return self.burst_service2.update_history_status( json.loads(data['burst_ids']))