Esempio n. 1
0
 def test_timeseriessurface(self):
     data = numpy.random.random((10, 10))
     dt = time_series.TimeSeriesSurface(data=data)
     assert dt.data.shape ==  (10, 10)
     assert dt.labels_ordering ==  ['Time', 'State Variable', 'Vertex', 'Mode']
     assert dt.sample_period ==  1.0
     assert dt.sample_rate ==  0.0
     assert dt.start_time ==  0.0
     assert dt.time.shape ==  (0,)
Esempio n. 2
0
 def test_timeseriessurface(self):
     data = numpy.random.random((10, 10))
     dt = time_series.TimeSeriesSurface(data=data)
     self.assertEqual(dt.data.shape, (10, 10))
     self.assertEqual(dt.labels_ordering, ['Time', 'State Variable', 'Vertex', 'Mode'])
     self.assertEqual(dt.sample_period, 1.0)
     self.assertEqual(dt.sample_rate, 0.0)
     self.assertEqual(dt.start_time, 0.0)
     self.assertEqual(dt.time.shape, (0,))
Esempio n. 3
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
        m_ind = -1
        for m_name in monitors:
            m_ind += 1
            sample_period = self.algorithm.monitors[m_ind].period
            # Create the required output for each monitor that was submitted
            if (m_name in self.RESULTS_MAP[time_series.TimeSeriesEEG]
                    and hasattr(self.algorithm.monitors[m_ind], 'sensors')):
                result_datatypes[m_name] = time_series.TimeSeriesEEG(storage_path=self.storage_path,
                                                                     sensors=self.algorithm.monitors[m_ind].sensors,
                                                                     sample_period=sample_period,
                                                                     title=' ' + m_name, start_time=start_time, )

            elif (m_name in self.RESULTS_MAP[time_series.TimeSeriesMEG]
                  and hasattr(self.algorithm.monitors[m_ind], 'sensors')):
                result_datatypes[m_name] = time_series.TimeSeriesMEG(storage_path=self.storage_path,
                                                                     sensors=self.algorithm.monitors[m_ind].sensors,
                                                                     sample_period=sample_period,
                                                                     title=' ' + m_name, start_time=start_time)

            elif m_name in self.RESULTS_MAP[time_series.TimeSeries]:
                result_datatypes[m_name] = time_series.TimeSeries(storage_path=self.storage_path,
                                                                  sample_period=sample_period,
                                                                  title=' ' + m_name, start_time=start_time)

            elif surface is None:
                ## We do not have a surface selected from UI, or regions only result.
                result_datatypes[m_name] = time_series.TimeSeriesRegion(storage_path=self.storage_path,
                                                                        connectivity=connectivity,
                                                                        sample_period=sample_period,
                                                                        title='Regions ' + m_name,
                                                                        start_time=start_time)

            else:
                result_datatypes[m_name] = time_series.TimeSeriesSurface(storage_path=self.storage_path,
                                                                         surface=surface, sample_period=sample_period,
                                                                         title='Surface ' + m_name,
                                                                         start_time=start_time)
            # Now check if the monitor will return results for each state variable, in which case store
            # the labels for these state variables.
            if m_name in self.HAVE_STATE_VARIABLES:
                selected_state_vars = [self.algorithm.model.state_variables[idx]
                                       for idx in self.algorithm.monitors[m_ind].voi]
                state_variable_dimension_name = result_datatypes[m_name].labels_ordering[1]
                result_datatypes[m_name].labels_dimensions[state_variable_dimension_name] = selected_state_vars
        
        #### 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