def __init__(self): BurstBaseController.__init__(self) self.available_models = get_traited_subclasses(models.Model) self.available_integrators = get_traited_subclasses(integrators.Integrator) self.cache = SessionCache() # Work around a numexpr thread safety issue. See TVB-1639. self.traj_lock = threading.Lock()
def __init__(self): BurstBaseController.__init__(self) self.available_models = get_traited_subclasses(models.Model) self.available_integrators = get_traited_subclasses( integrators.Integrator) self.cache = SessionCache() # Work around a numexpr thread safety issue. See TVB-1639. self.traj_lock = threading.Lock()
def test_simulator_region(self): AVAILABLE_MODELS = get_traited_subclasses(models.Model) AVAILABLE_METHODS = get_traited_subclasses(integrators.Integrator) MODEL_NAMES = AVAILABLE_MODELS.keys() METHOD_NAMES = AVAILABLE_METHODS.keys() METHOD_NAMES.append('RungeKutta4thOrderDeterministic') #init test_simulator = Simulator() #test cases for model_name, method_name in itertools.product( MODEL_NAMES, METHOD_NAMES): test_simulator.configure(model=model_name, method=method_name, surface_sim=False) test_simulator.run_simulation()
def build_noise(parent_parameters): """ Build Noise entity from dictionary of parameters. :param parent_parameters: dictionary of parameters for the entity having Noise as attribute. \ The dictionary is after UI form-submit and framework pre-process. :return: Noise entity. """ if KEY_NOISE not in parent_parameters: return None available_noise = parameters_factory.get_traited_subclasses(Noise) if 'Noise' not in available_noise: available_noise['Noise'] = Noise selected_noise = parent_parameters[KEY_NOISE] noise_params = parent_parameters[PARAMS_NOISE] del parent_parameters[KEY_NOISE] del parent_parameters[PARAMS_NOISE] if PARAMS_RANDOM_STREAM in noise_params: stream_params = noise_params[PARAMS_RANDOM_STREAM] random_stream = numpy.random.RandomState( seed=stream_params['init_seed']) del noise_params[PARAMS_RANDOM_STREAM] del noise_params[KEY_RANDOM_STREAM] noise_params[KEY_RANDOM_STREAM] = random_stream if PARAMS_EQUATION in noise_params: available_equations = parameters_factory.get_traited_subclasses( equations.Equation) eq_parameters = noise_params[PARAMS_EQUATION]["parameters"] equation = noise_params[KEY_EQUATION] equation = available_equations[equation](parameters=eq_parameters) del noise_params[PARAMS_EQUATION] del noise_params[KEY_EQUATION] noise_params[KEY_EQUATION] = equation noise_entity = available_noise[str(selected_noise)](**noise_params) parent_parameters[KEY_NOISE] = noise_entity return noise_entity
def build_noise(parent_parameters): """ Build Noise entity from dictionary of parameters. :param parent_parameters: dictionary of parameters for the entity having Noise as attribute. \ The dictionary is after UI form-submit and framework pre-process. :return: Noise entity. """ if KEY_NOISE not in parent_parameters: return None available_noise = parameters_factory.get_traited_subclasses(Noise) if 'Noise' not in available_noise: available_noise['Noise'] = Noise selected_noise = parent_parameters[KEY_NOISE] noise_params = parent_parameters[PARAMS_NOISE] del parent_parameters[KEY_NOISE] del parent_parameters[PARAMS_NOISE] if PARAMS_RANDOM_STREAM in noise_params: stream_params = noise_params[PARAMS_RANDOM_STREAM] random_stream = numpy.random.RandomState(seed=stream_params['init_seed']) del noise_params[PARAMS_RANDOM_STREAM] del noise_params[KEY_RANDOM_STREAM] noise_params[KEY_RANDOM_STREAM] = random_stream if PARAMS_EQUATION in noise_params: available_equations = parameters_factory.get_traited_subclasses(equations.Equation) eq_parameters = noise_params[PARAMS_EQUATION]["parameters"] equation = noise_params[KEY_EQUATION] equation = available_equations[equation](parameters=eq_parameters) del noise_params[PARAMS_EQUATION] del noise_params[KEY_EQUATION] noise_params[KEY_EQUATION] = equation noise_entity = available_noise[str(selected_noise)](**noise_params) parent_parameters[KEY_NOISE] = noise_entity return noise_entity
def test_traitedsubclassed(self): """ Tests successful creation of traited classes. """ # We imported array so we should have all these traited classes registered expected = [ 'IntegerArray', 'StringArray', 'PositionArray', 'IndexArray', 'BoolArray', 'OrientationArray', 'FloatArray', 'ComplexArray', 'Array', 'SparseMatrix' ] subclasses = get_traited_subclasses(Array) for key in expected: assert key in subclasses
""" #SpecificTestModel = TestModel #SpecificTestModel.__bases__ = (BaseModel,) #tester = SpecificTestModel(**kwargs) tester = TestModel(model = BaseModel(**kwargs)) tester.configure() tester.model.configure_initial(1.0, (1, tester.model.nvar, 1, tester.model.number_of_modes)) return tester if __name__ == '__main__': # Do some stuff that tests or makes use of this module... LOG.info("Generating phase-plane images for tvb.simulator.models...") from tvb.basic.traits.parameters_factory import get_traited_subclasses AVAILABLE_MODELS = get_traited_subclasses(models.Model) MODEL_NAMES = AVAILABLE_MODELS.keys() for model_name in MODEL_NAMES: LOG.info("Generating phase-planes for %s" % model_name) tester = test_factory(AVAILABLE_MODELS[model_name]) tester.pplane() ### EoF ###
class TimeseriesMetricsAdapter(ABCAsynchronous): """ TVB adapter for exposing as a group the measure algorithm. """ _ui_name = "TimeSeries Metrics" _ui_description = "Compute a single number for a TimeSeries input DataType." _ui_subsection = "timeseries" available_algorithms = get_traited_subclasses( BaseTimeseriesMetricAlgorithm) def get_input_tree(self): """ Compute interface based on introspected algorithms found. """ algorithm = BaseTimeseriesMetricAlgorithm() algorithm.trait.bound = self.INTERFACE_ATTRIBUTES_ONLY tree = algorithm.interface[self.INTERFACE_ATTRIBUTES] tree[0]['conditions'] = FilterChain( fields=[FilterChain.datatype + '._nr_dimensions'], operations=["=="], values=[4]) algo_names = self.available_algorithms.keys() options = [] for name in algo_names: options.append({ ABCAdapter.KEY_NAME: name, ABCAdapter.KEY_VALUE: name }) tree.append({ 'name': 'algorithms', 'label': 'Selected metrics to be applied', 'type': ABCAdapter.TYPE_MULTIPLE, 'required': False, 'options': options, 'description': 'The selected metric algorithms will be applied on the input TimeSeries' }) return tree def get_output(self): return [DatatypeMeasure] def configure(self, time_series, **kwargs): """ Store the input shape to be later used to estimate memory usage. """ self.input_shape = time_series.read_data_shape() def get_required_memory_size(self, **kwargs): """ Return the required memory to run this algorithm. """ input_size = numpy.prod(self.input_shape) * 8.0 return input_size def get_required_disk_size(self, **kwargs): """ Returns the required disk size to be able to run the adapter (in kB). """ return 0 def launch(self, time_series, algorithms=None, start_point=None, segment=None): """ Launch algorithm and build results. :param time_series: the time series on which the algorithms are run :param algorithms: the algorithms to be run for computing measures on the time series :type algorithms: any subclass of BaseTimeseriesMetricAlgorithm (KuramotoIndex, GlobalVariance, VarianceNodeVariance) :rtype: `DatatypeMeasure` """ if algorithms is None: algorithms = self.available_algorithms.keys() shape = time_series.read_data_shape() log_debug_array(LOG, time_series, "time_series") metrics_results = {} for algorithm_name in algorithms: ##------------- NOTE: Assumes 4D, Simulator timeSeries. --------------## node_slice = [ slice(shape[0]), slice(shape[1]), slice(shape[2]), slice(shape[3]) ] ##---------- Iterate over slices and compose final result ------------## unstored_ts = TimeSeries(use_storage=False) unstored_ts.data = time_series.read_data_slice(tuple(node_slice)) ##-------------------- Fill Algorithm for Analysis -------------------## algorithm = self.available_algorithms[algorithm_name]( time_series=unstored_ts) if segment is not None: algorithm.segment = segment if start_point is not None: algorithm.start_point = start_point ## Validate that current algorithm's filter is valid. if (algorithm.accept_filter is not None and not algorithm. accept_filter.get_python_filter_equivalent(time_series)): LOG.warning( 'Measure algorithm will not be computed because of incompatibility on input. ' 'Filters failed on algo: ' + str(algorithm_name)) continue else: LOG.debug("Applying measure: " + str(algorithm_name)) unstored_result = algorithm.evaluate() ##----------------- Prepare a Float object(s) for result ----------------## if isinstance(unstored_result, dict): metrics_results.update(unstored_result) else: metrics_results[algorithm_name] = unstored_result result = DatatypeMeasure(analyzed_datatype=time_series, storage_path=self.storage_path, data_name=self._ui_name, metrics=metrics_results) return result
class SimulatorAdapter(ABCAsynchronous): """ Interface between the Simulator and the Framework. """ _ui_name = "Simulation Core" algorithm = None available_models = get_traited_subclasses(Model) available_monitors = get_traited_subclasses(Monitor) available_integrators = get_traited_subclasses(Integrator) available_couplings = get_traited_subclasses(Coupling) # 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_input_tree2(self): sim = Simulator() sim.trait.bound = self.INTERFACE_ATTRIBUTES_ONLY result = sim.interface_experimental return result def get_input_tree(self): """ Return a list of lists describing the interface to the simulator. This is used by the GUI to generate the menus and fields necessary for defining a simulation. """ sim = Simulator() sim.trait.bound = self.INTERFACE_ATTRIBUTES_ONLY result = sim.interface[self.INTERFACE_ATTRIBUTES] # We should add as hidden the Simulator State attribute. result.append({ self.KEY_NAME: 'simulation_state', self.KEY_TYPE: 'tvb.datatypes.simulation_state.SimulationState', self.KEY_LABEL: "Continuation of", self.KEY_REQUIRED: False, self.KEY_UI_HIDE: True }) return result def get_output(self): """ :returns: list of classes for possible results of the Simulator. """ return [time_series.TimeSeries] def configure(self, model, model_parameters, integrator, integrator_parameters, connectivity, monitors, monitors_parameters=None, surface=None, surface_parameters=None, stimulus=None, coupling=None, coupling_parameters=None, initial_conditions=None, conduction_speed=None, simulation_length=0, simulation_state=None): """ Make preparations for the adapter launch. """ self.log.debug("available_couplings: %s..." % str(self.available_couplings)) self.log.debug("coupling: %s..." % str(coupling)) self.log.debug("coupling_parameters: %s..." % str(coupling_parameters)) self.log.debug("%s: Initializing Model..." % str(self)) noise_framework.build_noise(model_parameters) model_instance = self.available_models[str(model)](**model_parameters) self._validate_model_parameters(model_instance, connectivity, surface) self.log.debug("%s: Initializing Integration scheme..." % str(self)) noise_framework.build_noise(integrator_parameters) integr = self.available_integrators[integrator]( **integrator_parameters) self.log.debug("%s: Instantiating Monitors..." % str(self)) monitors_list = [] for monitor_name in monitors: if (monitors_parameters is not None) and (str(monitor_name) in monitors_parameters): current_monitor_parameters = monitors_parameters[str( monitor_name)] HRFKernelEquation.build_equation_from_dict( 'hrf_kernel', current_monitor_parameters, True) monitors_list.append( self.available_monitors[str(monitor_name)]( **current_monitor_parameters)) else: ### We have monitors without any UI settable parameter. monitors_list.append( self.available_monitors[str(monitor_name)]()) if len(monitors) < 1: raise LaunchException( "Can not launch operation without monitors selected !!!") self.log.debug("%s: Initializing Coupling..." % str(self)) coupling_inst = self.available_couplings[str(coupling)]( **coupling_parameters) self.log.debug("Initializing Cortex...") if self._is_surface_simulation(surface, surface_parameters): cortex_entity = Cortex(use_storage=False).populate_cortex( surface, surface_parameters) if cortex_entity.region_mapping_data.connectivity.number_of_regions != connectivity.number_of_regions: raise LaunchException( "Incompatible RegionMapping -- Connectivity !!") if cortex_entity.region_mapping_data.surface.number_of_vertices != surface.number_of_vertices: raise LaunchException( "Incompatible RegionMapping -- Surface !!") select_loc_conn = cortex_entity.local_connectivity if select_loc_conn is not None and select_loc_conn.surface.number_of_vertices != surface.number_of_vertices: raise LaunchException( "Incompatible LocalConnectivity -- Surface !!") else: cortex_entity = None self.log.debug("%s: Instantiating requested simulator..." % str(self)) if conduction_speed not in (0.0, None): connectivity.speed = numpy.array([conduction_speed]) else: raise LaunchException("conduction speed cannot be 0 or missing") self.algorithm = Simulator(connectivity=connectivity, coupling=coupling_inst, surface=cortex_entity, stimulus=stimulus, model=model_instance, integrator=integr, monitors=monitors_list, initial_conditions=initial_conditions, conduction_speed=conduction_speed) self.simulation_length = 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 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( self.simulation_length) / 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 simulation_length = int(float(kwargs['simulation_length'])) approx_integrator_dt = float(kwargs['integrator_parameters']['dt']) if approx_integrator_dt == 0.0: approx_integrator_dt = 1.0 if 'surface' in kwargs and kwargs[ 'surface'] is not None and kwargs['surface'] != '': approx_number_of_nodes *= approx_number_of_nodes estimation = magic_number * approx_number_of_nodes * approx_nvar * approx_modes * 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') 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 launch(self, model, model_parameters, integrator, integrator_parameters, connectivity, monitors, monitors_parameters=None, surface=None, surface_parameters=None, stimulus=None, coupling=None, coupling_parameters=None, initial_conditions=None, conduction_speed=None, simulation_length=0, simulation_state=None): """ 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_datatypes = dict() start_time = self.algorithm.current_step * self.algorithm.integrator.dt self.algorithm.configure(full_configure=False) if simulation_state is not None: simulation_state.fill_into(self.algorithm) region_map = self._try_find_mapping(region_mapping.RegionMapping, connectivity.gid) region_volume_map = self._try_find_mapping( region_mapping.RegionVolumeMapping, connectivity.gid) for monitor in self.algorithm.monitors: m_name = monitor.__class__.__name__ ts = monitor.create_time_series(self.storage_path, connectivity, surface, region_map, region_volume_map) self.log.debug("Monitor %s created the TS %s" % (m_name, ts)) # Now check if the monitor will return results for each state variable, in which case store # the labels for these state variables. # todo move these into monitors as well # and replace check if ts.user_tag_1 with something better (e.g. pre_ex & post) state_variable_dimension_name = ts.labels_ordering[1] if ts.user_tag_1: ts.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.start_time = start_time result_datatypes[m_name] = ts #### Create Simulator State entity and persist it in DB. H5 file will be empty now. if not self._is_group_launch(): simulation_state = SimulationState(storage_path=self.storage_path) self._capture_operation_results([simulation_state]) ### Run simulation self.log.debug("%s: Starting simulation..." % str(self)) for result in self.algorithm(simulation_length=simulation_length): for j, monitor in enumerate(monitors): if result[j] is not None: result_datatypes[monitor].write_time_slice([result[j][0]]) result_datatypes[monitor].write_data_slice([result[j][1]]) self.log.debug( "%s: Completed simulation, starting to store simulation state " % str(self)) ### Populate H5 file for simulator state. This step could also be done while running sim, in background. if not self._is_group_launch(): simulation_state.populate_from(self.algorithm) self._capture_operation_results([simulation_state]) self.log.debug("%s: Simulation state persisted, returning results " % str(self)) final_results = [] for result in result_datatypes.values(): result.close_file() final_results.append(result) self.log.info("%s: Adapter simulation finished!!" % str(self)) return final_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
class SimulatorAdapter(ABCAsynchronous): """ Interface between the Simulator and the Framework. """ _ui_name = "Simulation Core" algorithm = None available_models = get_traited_subclasses(Model) available_monitors = get_traited_subclasses(Monitor) available_integrators = get_traited_subclasses(Integrator) available_couplings = get_traited_subclasses(Coupling) available_noise = get_traited_subclasses(Noise) ### Info: This are the possible results returned with this adapter from different Monitors. ### When a list appears(surface & region), we actually return only one based on param surface being None or not. # MONITOR_RESULTS = {"Raw": [time_series.TimeSeriesRegion, time_series.TimeSeriesSurface], # "SubSample": [time_series.TimeSeriesRegion, time_series.TimeSeriesSurface], # "SpatialAverage": time_series.TimeSeries, # "GlobalAverage": time_series.TimeSeries, # "TemporalAverage": [time_series.TimeSeriesRegion, time_series.TimeSeriesSurface], # "EEG": time_series.TimeSeriesEEG, # "SphericalEEG": time_series.TimeSeriesEEG, # "SphericalMEG": time_series.TimeSeriesMEG, # "Bold": [time_series.TimeSeriesRegion, time_series.TimeSeriesSurface]} RESULTS_MAP = {time_series.TimeSeriesEEG: ["SphericalEEG", "EEG"], time_series.TimeSeriesMEG: ["SphericalMEG"], # Add here also "MEG" monitor reference time_series.TimeSeries: ["GlobalAverage", "SpatialAverage"]} # time_series.TimeSeriesVolume: ["Bold"], #SK: For a number of reasons, it's probably best to avoid returning TimeSeriesVolume , # from a simulation directly, instead just stick with the source, i.e. Region and Surface, # then later we can add a voxelisation "analyser" to produce TimeSeriesVolume on which Volume # based analysers and visualisers (which don't exist yet) can operate. # 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_input_tree(self): """ Return a list of lists describing the interface to the simulator. This is used by the GUI to generate the menus and fields necessary for defining a simulation. """ sim = Simulator() sim.trait.bound = self.INTERFACE_ATTRIBUTES_ONLY result = sim.interface[self.INTERFACE_ATTRIBUTES] # We should add as hidden the Simulator State attribute. result.append({'name': 'simulation_state', 'type': SimulationState, 'required': False, 'ui_hidden': True}) return result def get_output(self): """ :returns: list of classes for possible results of the Simulator. """ return [time_series.TimeSeries] def configure(self, model, model_parameters, integrator, integrator_parameters, connectivity, monitors, monitors_parameters=None, surface=None, surface_parameters=None, stimulus=None, coupling=None, coupling_parameters=None, initial_conditions=None, conduction_speed=None, simulation_length=0, simulation_state=None): """ Make preparations for the adapter launch. """ self.log.debug("available_couplings: %s..." % str(self.available_couplings)) self.log.debug("coupling: %s..." % str(coupling)) self.log.debug("coupling_parameters: %s..." % str(coupling_parameters)) self.log.debug("%s: Initializing Model..." % str(self)) noise_framework.build_noise(model_parameters) model_instance = self.available_models[str(model)](**model_parameters) self._validate_model_parameters(model_instance, connectivity, surface) self.log.debug("%s: Initializing Integration scheme..." % str(self)) noise_framework.build_noise(integrator_parameters) integr = self.available_integrators[integrator](**integrator_parameters) self.log.debug("%s: Instantiating Monitors..." % str(self)) monitors_list = [] for monitor_name in monitors: if (monitors_parameters is not None) and (str(monitor_name) in monitors_parameters): monitors_list.append(self.available_monitors[str(monitor_name) ](**monitors_parameters[str(monitor_name)])) else: ### We have monitors without any UI settable parameter. monitors_list.append(self.available_monitors[str(monitor_name)]()) if len(monitors) < 1: raise LaunchException("Can not launch operation without monitors selected !!!") self.log.debug("%s: Initializing Coupling..." % str(self)) coupling_inst = self.available_couplings[str(coupling)](**coupling_parameters) self.log.debug("Initializing Cortex...") if surface is not None and surface_parameters is not None: cortex_entity = Cortex(use_storage=False).populate_cortex(surface, surface_parameters) if cortex_entity.region_mapping_data.connectivity.number_of_regions != connectivity.number_of_regions: raise LaunchException("Incompatible RegionMapping -- Connectivity !!") if cortex_entity.region_mapping_data.surface.number_of_vertices != surface.number_of_vertices: raise LaunchException("Incompatible RegionMapping -- Surface !!") select_loc_conn = cortex_entity.local_connectivity if select_loc_conn is not None and select_loc_conn.surface.number_of_vertices != surface.number_of_vertices: raise LaunchException("Incompatible LocalConnectivity -- Surface !!") else: cortex_entity = None self.log.debug("%s: Instantiating requested simulator..." % str(self)) connectivity.configure() self.algorithm = Simulator(connectivity=connectivity, coupling=coupling_inst, surface=cortex_entity, stimulus=stimulus, model=model_instance, integrator=integr, monitors=monitors_list, initial_conditions=initial_conditions, conduction_speed=conduction_speed) self.simulation_length = simulation_length self.log.debug("%s: Initializing storage..." % str(self)) try: self.algorithm.configure() if simulation_state is not None: simulation_state.fill_into(self.algorithm) except ValueError, 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)
import numpy import itertools from tvb.tests.library.base_testcase import BaseTestCase from tvb.simulator.common import get_logger from tvb.simulator import simulator, models, coupling, integrators, monitors, noise from tvb.datatypes.connectivity import Connectivity from tvb.datatypes.cortex import Cortex from tvb.datatypes.local_connectivity import LocalConnectivity from tvb.datatypes.region_mapping import RegionMapping from tvb.basic.traits.parameters_factory import get_traited_subclasses from tvb.datatypes.projections import ProjectionMatrix from tvb.datatypes.sensors import SensorsEEG, SensorsMEG LOG = get_logger(__name__) AVAILABLE_MODELS = get_traited_subclasses(models.Model) AVAILABLE_METHODS = get_traited_subclasses(integrators.Integrator) MODEL_CLASSES = AVAILABLE_MODELS.values() METHOD_NAMES = AVAILABLE_METHODS.keys() METHOD_NAMES.append('RungeKutta4thOrderDeterministic') class Simulator(object): """ Simulator test class """ def __init__(self): """ Initialise the structural information, coupling function, and monitors.
class SimulatorAdapter(ABCAsynchronous): """ Interface between the Simulator and the Framework. """ _ui_name = "Simulation Core" algorithm = None available_models = get_traited_subclasses(Model) available_monitors = get_traited_subclasses(Monitor) available_integrators = get_traited_subclasses(Integrator) available_couplings = get_traited_subclasses(Coupling) # 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_input_tree(self): """ Return a list of lists describing the interface to the simulator. This is used by the GUI to generate the menus and fields necessary for defining a simulation. """ sim = Simulator() sim.trait.bound = self.INTERFACE_ATTRIBUTES_ONLY result = sim.interface[self.INTERFACE_ATTRIBUTES] # We should add as hidden the Simulator State attribute. result.append({ self.KEY_NAME: 'simulation_state', self.KEY_TYPE: SimulationState, self.KEY_LABEL: "Continuation of", self.KEY_REQUIRED: False, self.KEY_UI_HIDE: True }) return result def get_output(self): """ :returns: list of classes for possible results of the Simulator. """ return [time_series.TimeSeries] def configure(self, model, model_parameters, integrator, integrator_parameters, connectivity, monitors, monitors_parameters=None, surface=None, surface_parameters=None, stimulus=None, coupling=None, coupling_parameters=None, initial_conditions=None, conduction_speed=None, simulation_length=0, simulation_state=None): """ Make preparations for the adapter launch. """ self.log.debug("available_couplings: %s..." % str(self.available_couplings)) self.log.debug("coupling: %s..." % str(coupling)) self.log.debug("coupling_parameters: %s..." % str(coupling_parameters)) self.log.debug("%s: Initializing Model..." % str(self)) noise_framework.build_noise(model_parameters) model_instance = self.available_models[str(model)](**model_parameters) self._validate_model_parameters(model_instance, connectivity, surface) self.log.debug("%s: Initializing Integration scheme..." % str(self)) noise_framework.build_noise(integrator_parameters) integr = self.available_integrators[integrator]( **integrator_parameters) self.log.debug("%s: Instantiating Monitors..." % str(self)) monitors_list = [] for monitor_name in monitors: if (monitors_parameters is not None) and (str(monitor_name) in monitors_parameters): current_monitor_parameters = monitors_parameters[str( monitor_name)] HRFKernelEquation.build_equation_from_dict( 'hrf_kernel', current_monitor_parameters, True) monitors_list.append( self.available_monitors[str(monitor_name)]( **current_monitor_parameters)) else: ### We have monitors without any UI settable parameter. monitors_list.append( self.available_monitors[str(monitor_name)]()) if len(monitors) < 1: raise LaunchException( "Can not launch operation without monitors selected !!!") self.log.debug("%s: Initializing Coupling..." % str(self)) coupling_inst = self.available_couplings[str(coupling)]( **coupling_parameters) self.log.debug("Initializing Cortex...") if self._is_surface_simulation(surface, surface_parameters): cortex_entity = Cortex(use_storage=False).populate_cortex( surface, surface_parameters) if cortex_entity.region_mapping_data.connectivity.number_of_regions != connectivity.number_of_regions: raise LaunchException( "Incompatible RegionMapping -- Connectivity !!") if cortex_entity.region_mapping_data.surface.number_of_vertices != surface.number_of_vertices: raise LaunchException( "Incompatible RegionMapping -- Surface !!") select_loc_conn = cortex_entity.local_connectivity if select_loc_conn is not None and select_loc_conn.surface.number_of_vertices != surface.number_of_vertices: raise LaunchException( "Incompatible LocalConnectivity -- Surface !!") else: cortex_entity = None self.log.debug("%s: Instantiating requested simulator..." % str(self)) if conduction_speed not in (0.0, None): connectivity.speed = numpy.array([conduction_speed]) else: raise LaunchException("conduction speed cannot be 0 or missing") self.algorithm = Simulator(connectivity=connectivity, coupling=coupling_inst, surface=cortex_entity, stimulus=stimulus, model=model_instance, integrator=integr, monitors=monitors_list, initial_conditions=initial_conditions, conduction_speed=conduction_speed) self.simulation_length = simulation_length self.log.debug("%s: Initializing storage..." % str(self)) try: self.algorithm.preconfigure() except ValueError, 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)
#SpecificTestModel = TestModel #SpecificTestModel.__bases__ = (BaseModel,) #tester = SpecificTestModel(**kwargs) tester = TestModel(model = BaseModel(**kwargs)) tester.configure() tester.model.configure_initial(1.0, (1, tester.model.nvar, 1, tester.model.number_of_modes)) return tester if __name__ == '__main__': # Do some stuff that tests or makes use of this module... LOG.info("Generating phase-plane images for tvb.simulator.models...") from tvb.basic.traits.parameters_factory import get_traited_subclasses AVAILABLE_MODELS = get_traited_subclasses(models.Model) ##AVAILABLE_MODELS = {models.LarterBreakspear.__name__: models.LarterBreakspear} MODEL_NAMES = AVAILABLE_MODELS.keys() for model_name in MODEL_NAMES: LOG.info("Generating phase-planes for %s" % model_name) tester = test_factory(AVAILABLE_MODELS[model_name]) tester.pplane() ### EoF ###