def test_get_launchable_algorithms(self): factory = DatatypesFactory() conn = factory.create_connectivity(4)[1] ts = factory.create_timeseries(conn) result = self.flow_service.get_launchable_algorithms(ts.gid) assert 'Analyze' in result assert 'View' in result
def test_get_launchable_algorithms(self): factory = DatatypesFactory() conn = factory.create_connectivity(4)[1] ts = factory.create_timeseries(conn) result = self.flow_service.get_launchable_algorithms(ts.gid) self.assertTrue('Analyze' in result) self.assertTrue('View' in result)
def transactional_setup_method(self): self.init() self.surface_m_p_c = SurfaceModelParametersController() BurstController().index() stored_burst = cherrypy.session[common.KEY_BURST_CONFIG] datatypes_factory = DatatypesFactory() _, self.connectivity = datatypes_factory.create_connectivity() _, self.surface = datatypes_factory.create_surface() new_params = {} for key, val in SIMULATOR_PARAMETERS.iteritems(): new_params[key] = {'value': val} new_params['connectivity'] = {'value': self.connectivity.gid} new_params['surface'] = {'value': self.surface.gid} stored_burst.simulator_configuration = new_params
def transactional_setup_method(self): self.init() self.surface_m_p_c = SurfaceModelParametersController() BurstController().index() stored_burst = cherrypy.session[common.KEY_BURST_CONFIG] datatypes_factory = DatatypesFactory() _, self.connectivity = datatypes_factory.create_connectivity() _, self.surface = datatypes_factory.create_surface() new_params = {} for key, val in SIMULATOR_PARAMETERS.iteritems(): new_params[key] = {'value': val} new_params['connectivity'] = {'value': self.connectivity.gid} new_params['surface'] = {'value': self.surface.gid} stored_burst.simulator_configuration = new_params
class SimulatorAdapterTest(TransactionalTestCase): """ Basic testing that Simulator is still working from UI. """ CONNECTIVITY_NODES = 74 def setUp(self): """ Reset the database before each test. """ initialize_storage() self.datatypes_factory = DatatypesFactory() self.test_user = self.datatypes_factory.get_user() self.test_project = self.datatypes_factory.get_project() self.connectivity = self.datatypes_factory.create_connectivity( self.CONNECTIVITY_NODES)[1] algorithm = dao.get_algorithm_by_module(SIMULATOR_MODULE, SIMULATOR_CLASS) self.simulator_adapter = ABCAdapter.build_adapter(algorithm) self.operation = TestFactory.create_operation( algorithm, self.test_user, self.test_project, model.STATUS_STARTED, json.dumps(SIMULATOR_PARAMETERS)) SIMULATOR_PARAMETERS['connectivity'] = self.connectivity.gid def test_happy_flow_launch(self): """ Test that launching a simulation from UI works. """ OperationService().initiate_prelaunch(self.operation, self.simulator_adapter, {}, **SIMULATOR_PARAMETERS) sim_result = dao.get_generic_entity(TimeSeriesRegion, 'TimeSeriesRegion', 'type')[0] self.assertEquals(sim_result.read_data_shape(), (32, 1, self.CONNECTIVITY_NODES, 1)) def _estimate_hdd(self, new_parameters_dict): """ Private method, to return HDD estimation for a given set of input parameters""" filtered_params = self.simulator_adapter.prepare_ui_inputs( new_parameters_dict) self.simulator_adapter.configure(**filtered_params) return self.simulator_adapter.get_required_disk_size(**filtered_params) def test_estimate_hdd(self): """ Test that occupied HDD estimation for simulation results considers simulation length. """ factor = 5 simulation_parameters = copy(SIMULATOR_PARAMETERS) ## Estimate HDD with default simulation parameters estimate1 = self._estimate_hdd(simulation_parameters) self.assertTrue(estimate1 > 1) ## Change simulation length and monitor period, we expect a direct proportial increase in estimated HDD simulation_parameters['simulation_length'] = float( simulation_parameters['simulation_length']) * factor period = float(simulation_parameters[ 'monitors_parameters_option_TemporalAverage_period']) simulation_parameters[ 'monitors_parameters_option_TemporalAverage_period'] = period / factor estimate2 = self._estimate_hdd(simulation_parameters) self.assertEqual(estimate1, estimate2 / factor / factor) ## Change number of nodes in connectivity. Expect HDD estimation increase. large_conn_gid = self.datatypes_factory.create_connectivity( self.CONNECTIVITY_NODES * factor)[1].gid simulation_parameters['connectivity'] = large_conn_gid estimate3 = self._estimate_hdd(simulation_parameters) self.assertEqual(estimate2, estimate3 / factor) def test_estimate_execution_time(self): """ Test that get_execution_time_approximation considers the correct params """ ## Compute reference estimation params = self.simulator_adapter.prepare_ui_inputs(SIMULATOR_PARAMETERS) estimation1 = self.simulator_adapter.get_execution_time_approximation( **params) ## Estimation when the surface input parameter is set params['surface'] = "GID_surface" estimation2 = self.simulator_adapter.get_execution_time_approximation( **params) self.assertEqual(estimation1, estimation2 / 500) params['surface'] = "" ## Modify integration step and simulation length: initial_simulation_length = float(params['simulation_length']) initial_integration_step = float(params['integrator_parameters']['dt']) for factor in (2, 4, 10): params['simulation_length'] = initial_simulation_length * factor params['integrator_parameters'][ 'dt'] = initial_integration_step / factor estimation3 = self.simulator_adapter.get_execution_time_approximation( **params) self.assertEqual(estimation1, estimation3 / factor / factor) ## Check that no division by zero happens params['integrator_parameters']['dt'] = 0 estimation4 = self.simulator_adapter.get_execution_time_approximation( **params) self.assertTrue(estimation4 > 0) ## even with length zero, still a positive estimation should be returned params['simulation_length'] = 0 estimation5 = self.simulator_adapter.get_execution_time_approximation( **params) self.assertTrue(estimation5 > 0) def test_noise_2d_bad_shape(self): """ Test a simulation with noise. Pass a wrong shape and expect exception to be raised. """ params = copy(SIMULATOR_PARAMETERS) params['integrator'] = u'HeunStochastic' noise_4d_config = [[1 for _ in range(self.CONNECTIVITY_NODES)] for _ in range(4)] params[ 'integrator_parameters_option_HeunStochastic_dt'] = u'0.01220703125' params[ 'integrator_parameters_option_HeunStochastic_noise'] = u'Additive' params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_nsig'] = str( noise_4d_config) params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_ntau'] = u'0.0' params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream'] = u'RandomStream' params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream_parameters_option_RandomStream_init_seed'] = u'42' filtered_params = self.simulator_adapter.prepare_ui_inputs(params) self.simulator_adapter.configure(**filtered_params) if hasattr(self.simulator_adapter, 'algorithm'): self.assertEqual( (4, 74), self.simulator_adapter.algorithm.integrator.noise.nsig.shape) else: self.fail("Simulator adapter was not initialized properly") self.assertRaises(Exception, OperationService().initiate_prelaunch, self.operation, self.simulator_adapter, {}, **params) def test_noise_2d_happy_flow(self): """ Test a simulation with noise. """ params = copy(SIMULATOR_PARAMETERS) params['integrator'] = u'HeunStochastic' noise_2d_config = [[1 for _ in range(self.CONNECTIVITY_NODES)] for _ in range(2)] params[ 'integrator_parameters_option_HeunStochastic_dt'] = u'0.01220703125' params[ 'integrator_parameters_option_HeunStochastic_noise'] = u'Additive' params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_nsig'] = str( noise_2d_config) params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_ntau'] = u'0.0' params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream'] = u'RandomStream' params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream_parameters_option_RandomStream_init_seed'] = u'42' self._launch_and_check_noise(params, (2, 74)) sim_result = dao.get_generic_entity(TimeSeriesRegion, 'TimeSeriesRegion', 'type')[0] self.assertEquals(sim_result.read_data_shape(), (32, 1, self.CONNECTIVITY_NODES, 1)) params[ 'integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_nsig'] = '[1]' self._launch_and_check_noise(params, (1, )) def _launch_and_check_noise(self, params, expected_noise_shape): filtered_params = self.simulator_adapter.prepare_ui_inputs(params) self.simulator_adapter.configure(**filtered_params) if hasattr(self.simulator_adapter, 'algorithm'): self.assertEqual( expected_noise_shape, self.simulator_adapter.algorithm.integrator.noise.nsig.shape) else: self.fail("Simulator adapter was not initialized properly") OperationService().initiate_prelaunch(self.operation, self.simulator_adapter, {}, **params) def test_simulation_with_stimulus(self): """ Test a simulation with noise. """ params = copy(SIMULATOR_PARAMETERS) params["stimulus"] = self.datatypes_factory.create_stimulus( self.connectivity).gid filtered_params = self.simulator_adapter.prepare_ui_inputs(params) self.simulator_adapter.configure(**filtered_params) OperationService().initiate_prelaunch(self.operation, self.simulator_adapter, {}, **params)
class NIFTIImporterTest(TransactionalTestCase): """ Unit-tests for NIFTI importer. """ NII_FILE = os.path.join(os.path.dirname(demo_data.__file__), 'minimal.nii') GZ_NII_FILE = os.path.join(os.path.dirname(demo_data.__file__), 'minimal.nii.gz') TIMESERIES_NII_FILE = os.path.join(os.path.dirname(demo_data.__file__), 'time_series_152.nii.gz') WRONG_NII_FILE = os.path.abspath(__file__) DEFAULT_ORIGIN = [[0.0, 0.0, 0.0]] UNKNOWN_STR = "unknown" def setUp(self): self.datatypeFactory = DatatypesFactory() self.test_project = self.datatypeFactory.get_project() self.test_user = self.datatypeFactory.get_user() def tearDown(self): """ Clean-up tests data """ FilesHelper().remove_project_structure(self.test_project.name) def _import(self, import_file_path=None, expected_result_class=StructuralMRI, connectivity=None): """ This method is used for importing data in NIFIT format :param import_file_path: absolute path of the file to be imported """ ### Retrieve Adapter instance importer = TestFactory.create_adapter('tvb.adapters.uploaders.nifti_importer', 'NIFTIImporter') args = {'data_file': import_file_path, DataTypeMetaData.KEY_SUBJECT: "bla bla", 'apply_corrections': True, 'connectivity': connectivity} ### Launch import Operation FlowService().fire_operation(importer, self.test_user, self.test_project.id, **args) dts, count = dao.get_values_of_datatype(self.test_project.id, expected_result_class, None) self.assertEqual(1, count, "Project should contain only one data type.") result = ABCAdapter.load_entity_by_gid(dts[0][2]) self.assertTrue(result is not None, "Result should not be none") return result def test_import_demo_ts(self): """ This method tests import of a NIFTI file. """ time_series = self._import(self.TIMESERIES_NII_FILE, TimeSeriesVolume) # Since self.assertAlmostEquals is not available on all machine # We compare floats as following self.assertTrue(abs(1.0 - time_series.sample_period) <= 0.001) self.assertEqual("sec", str(time_series.sample_period_unit)) self.assertEqual(0.0, time_series.start_time) self.assertTrue(time_series.title.startswith("NIFTI")) data_shape = time_series.read_data_shape() self.assertEquals(4, len(data_shape)) # We have 5 time points self.assertEqual(5, data_shape[0]) dimension_labels = time_series.labels_ordering self.assertTrue(dimension_labels is not None) self.assertEquals(4, len(dimension_labels)) volume = time_series.volume self.assertTrue(volume is not None) self.assertTrue(numpy.equal(self.DEFAULT_ORIGIN, volume.origin).all()) self.assertEquals("mm", volume.voxel_unit) def test_import_nii_without_time_dimension(self): """ This method tests import of a NIFTI file. """ structure = self._import(self.NII_FILE) self.assertEqual("T1", structure.weighting) data_shape = structure.array_data.shape self.assertEquals(3, len(data_shape)) self.assertEqual(64, data_shape[0]) self.assertEqual(64, data_shape[1]) self.assertEqual(10, data_shape[2]) volume = structure.volume self.assertTrue(volume is not None) self.assertTrue(numpy.equal(self.DEFAULT_ORIGIN, volume.origin).all()) self.assertTrue(numpy.equal([3.0, 3.0, 3.0], volume.voxel_size).all()) self.assertEquals(self.UNKNOWN_STR, volume.voxel_unit) def test_import_nifti_compressed(self): """ This method tests import of a NIFTI file compressed in GZ format. """ structure = self._import(self.GZ_NII_FILE) self.assertEqual("T1", structure.weighting) def test_import_region_mapping(self): """ This method tests import of a NIFTI file compressed in GZ format. """ to_link_conn = self.datatypeFactory.create_connectivity()[1] mapping = self._import(self.GZ_NII_FILE, RegionVolumeMapping, to_link_conn.gid) self.assertTrue(-1 <= mapping.array_data.min()) self.assertTrue(mapping.array_data.max() < to_link_conn.number_of_regions) conn = mapping.connectivity self.assertTrue(conn is not None) self.assertEquals(to_link_conn.number_of_regions, conn.number_of_regions) volume = mapping.volume self.assertTrue(volume is not None) self.assertTrue(numpy.equal(self.DEFAULT_ORIGIN, volume.origin).all()) self.assertTrue(numpy.equal([3.0, 3.0, 3.0], volume.voxel_size).all()) self.assertEquals(self.UNKNOWN_STR, volume.voxel_unit) def test_import_wrong_nii_file(self): """ This method tests import of a file in a wrong format """ try: self._import(self.WRONG_NII_FILE) self.fail("Import should fail in case of a wrong NIFTI format.") except OperationException: # Expected exception pass
class SimulatorAdapterTest(TransactionalTestCase): """ Basic testing that Simulator is still working from UI. """ CONNECTIVITY_NODES = 74 def setUp(self): """ Reset the database before each test. """ initialize_storage() self.datatypes_factory = DatatypesFactory() self.test_user = self.datatypes_factory.get_user() self.test_project = self.datatypes_factory.get_project() self.connectivity = self.datatypes_factory.create_connectivity(self.CONNECTIVITY_NODES)[1] algorithm = dao.get_algorithm_by_module(SIMULATOR_MODULE, SIMULATOR_CLASS) self.simulator_adapter = ABCAdapter.build_adapter(algorithm) self.operation = TestFactory.create_operation(algorithm, self.test_user, self.test_project, model.STATUS_STARTED, json.dumps(SIMULATOR_PARAMETERS)) SIMULATOR_PARAMETERS['connectivity'] = self.connectivity.gid def test_happy_flow_launch(self): """ Test that launching a simulation from UI works. """ OperationService().initiate_prelaunch(self.operation, self.simulator_adapter, {}, **SIMULATOR_PARAMETERS) sim_result = dao.get_generic_entity(TimeSeriesRegion, 'TimeSeriesRegion', 'type')[0] self.assertEquals(sim_result.read_data_shape(), (32, 1, self.CONNECTIVITY_NODES, 1)) def _estimate_hdd(self, new_parameters_dict): """ Private method, to return HDD estimation for a given set of input parameters""" filtered_params = self.simulator_adapter.prepare_ui_inputs(new_parameters_dict) self.simulator_adapter.configure(**filtered_params) return self.simulator_adapter.get_required_disk_size(**filtered_params) def test_estimate_hdd(self): """ Test that occupied HDD estimation for simulation results considers simulation length. """ factor = 5 simulation_parameters = copy(SIMULATOR_PARAMETERS) ## Estimate HDD with default simulation parameters estimate1 = self._estimate_hdd(simulation_parameters) self.assertTrue(estimate1 > 1) ## Change simulation length and monitor period, we expect a direct proportial increase in estimated HDD simulation_parameters['simulation_length'] = float(simulation_parameters['simulation_length']) * factor period = float(simulation_parameters['monitors_parameters_option_TemporalAverage_period']) simulation_parameters['monitors_parameters_option_TemporalAverage_period'] = period / factor estimate2 = self._estimate_hdd(simulation_parameters) self.assertEqual(estimate1, estimate2 / factor / factor) ## Change number of nodes in connectivity. Expect HDD estimation increase. large_conn_gid = self.datatypes_factory.create_connectivity(self.CONNECTIVITY_NODES * factor)[1].gid simulation_parameters['connectivity'] = large_conn_gid estimate3 = self._estimate_hdd(simulation_parameters) self.assertEqual(estimate2, estimate3 / factor) def test_estimate_execution_time(self): """ Test that get_execution_time_approximation considers the correct params """ ## Compute reference estimation params = self.simulator_adapter.prepare_ui_inputs(SIMULATOR_PARAMETERS) estimation1 = self.simulator_adapter.get_execution_time_approximation(**params) ## Estimation when the surface input parameter is set params['surface'] = "GID_surface" estimation2 = self.simulator_adapter.get_execution_time_approximation(**params) self.assertEqual(estimation1, estimation2 / 500) params['surface'] = "" ## Modify integration step and simulation length: initial_simulation_length = float(params['simulation_length']) initial_integration_step = float(params['integrator_parameters']['dt']) for factor in (2, 4, 10): params['simulation_length'] = initial_simulation_length * factor params['integrator_parameters']['dt'] = initial_integration_step / factor estimation3 = self.simulator_adapter.get_execution_time_approximation(**params) self.assertEqual(estimation1, estimation3 / factor / factor) ## Check that no division by zero happens params['integrator_parameters']['dt'] = 0 estimation4 = self.simulator_adapter.get_execution_time_approximation(**params) self.assertTrue(estimation4 > 0) ## even with length zero, still a positive estimation should be returned params['simulation_length'] = 0 estimation5 = self.simulator_adapter.get_execution_time_approximation(**params) self.assertTrue(estimation5 > 0) def test_noise_2d_bad_shape(self): """ Test a simulation with noise. Pass a wrong shape and expect exception to be raised. """ params = copy(SIMULATOR_PARAMETERS) params['integrator'] = u'HeunStochastic' noise_4d_config = [[1 for _ in xrange(self.CONNECTIVITY_NODES)] for _ in xrange(4)] params['integrator_parameters_option_HeunStochastic_dt'] = u'0.01220703125' params['integrator_parameters_option_HeunStochastic_noise'] = u'Additive' params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_nsig'] = str(noise_4d_config) params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_ntau'] = u'0.0' params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream'] = u'RandomStream' params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream_parameters_option_RandomStream_init_seed'] = u'42' filtered_params = self.simulator_adapter.prepare_ui_inputs(params) self.simulator_adapter.configure(**filtered_params) if hasattr(self.simulator_adapter, 'algorithm'): self.assertEqual((4, 74), self.simulator_adapter.algorithm.integrator.noise.nsig.shape) else: self.fail("Simulator adapter was not initialized properly") self.assertRaises(Exception, OperationService().initiate_prelaunch, self.operation, self.simulator_adapter, {}, **params) def test_noise_2d_happy_flow(self): """ Test a simulation with noise. """ params = copy(SIMULATOR_PARAMETERS) params['integrator'] = u'HeunStochastic' noise_2d_config = [[1 for _ in xrange(self.CONNECTIVITY_NODES)] for _ in xrange(2)] params['integrator_parameters_option_HeunStochastic_dt'] = u'0.01220703125' params['integrator_parameters_option_HeunStochastic_noise'] = u'Additive' params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_nsig'] = str(noise_2d_config) params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_ntau'] = u'0.0' params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream'] = u'RandomStream' params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_random_stream_parameters_option_RandomStream_init_seed'] = u'42' self._launch_and_check_noise(params, (2, 74)) sim_result = dao.get_generic_entity(TimeSeriesRegion, 'TimeSeriesRegion', 'type')[0] self.assertEquals(sim_result.read_data_shape(), (32, 1, self.CONNECTIVITY_NODES, 1)) params['integrator_parameters_option_HeunStochastic_noise_parameters_option_Additive_nsig'] = '[1]' self._launch_and_check_noise(params, (1,)) def _launch_and_check_noise(self, params, expected_noise_shape): filtered_params = self.simulator_adapter.prepare_ui_inputs(params) self.simulator_adapter.configure(**filtered_params) if hasattr(self.simulator_adapter, 'algorithm'): self.assertEqual(expected_noise_shape, self.simulator_adapter.algorithm.integrator.noise.nsig.shape) else: self.fail("Simulator adapter was not initialized properly") OperationService().initiate_prelaunch(self.operation, self.simulator_adapter, {}, **params) def test_simulation_with_stimulus(self): """ Test a simulation with noise. """ params = copy(SIMULATOR_PARAMETERS) params["stimulus"] = self.datatypes_factory.create_stimulus(self.connectivity).gid filtered_params = self.simulator_adapter.prepare_ui_inputs(params) self.simulator_adapter.configure(**filtered_params) OperationService().initiate_prelaunch(self.operation, self.simulator_adapter, {}, **params)