def test_remove_used_surface(self): """ Tries to remove an used surface """ filter = FilterChain(fields=[FilterChain.datatype + '.surface_type'], operations=["=="], values=[CORTICAL]) mapping = try_get_last_datatype(self.test_project.id, RegionMappingIndex) surface = try_get_last_datatype(self.test_project.id, SurfaceIndex, filter) assert mapping is not None, "There should be one Mapping." assert surface is not None, "There should be one Costical Surface." assert surface.gid == mapping.fk_surface_gid, "The surfaces should have the same GID" try: self.project_service.remove_datatype(self.test_project.id, surface.gid) raise AssertionError( "The surface should still be used by a RegionMapping " + str(surface.gid)) except RemoveDataTypeException: # OK, do nothing pass res = dao.get_datatype_by_gid(surface.gid) assert surface.id == res.id, "A used surface was deleted"
def test_export_import_burst(self, user_factory, project_factory, simulation_launch): """ Test that fk_parent_burst is correctly preserved after export/import """ test_user = user_factory() test_project = project_factory(test_user, "TestIESim") sim_op = simulation_launch(test_user, test_project, simulation_length=10) tries = 5 while not sim_op.has_finished and tries > 0: sleep(5) tries = tries - 1 sim_op = dao.get_operation_by_id(sim_op.id) assert sim_op.has_finished, "Simulation did not finish in the given time" self.zip_path = ExportManager().export_project(test_project) assert self.zip_path is not None, "Exported file is none" self.project_service.remove_project(test_project.id) self.import_service.import_project_structure(self.zip_path, test_user.id) retrieved_project = self.project_service.retrieve_projects_for_user( test_user.id)[0][0] ts = try_get_last_datatype(retrieved_project.id, TimeSeriesRegionIndex) bursts = dao.get_bursts_for_project(retrieved_project.id) assert 1 == len(bursts) assert ts.fk_parent_burst == bursts[0].gid
def get_entity(project, expected_data, filters=None): """ Return the first entity with class given by `expected_data` :param expected_data: specifies the class whose entity is returned """ return try_get_last_datatype(project.id, expected_data, filters)
def step_1(self): """ Used for generating the interface which allows the user to define a stimulus. """ current_surface_stim = common.get_from_session(KEY_SURFACE_STIMULI) project_id = common.get_current_project().id surface_stim_selector_form = StimulusSurfaceSelectorForm(project_id) surface_stim_selector_form.surface_stimulus.data = current_surface_stim.gid.hex surface_stim_creator_form = SurfaceStimulusCreatorForm(self.possible_spatial_equations, self.possible_temporal_equations, project_id) if not hasattr(current_surface_stim, 'surface') or not current_surface_stim.surface: default_surface_index = try_get_last_datatype(project_id, SurfaceIndex, SurfaceStimulusCreatorForm.get_filters()) if default_surface_index is None: common.set_error_message(self.MSG_MISSING_SURFACE) current_surface_stim.surface = uuid.uuid4() else: current_surface_stim.surface = uuid.UUID(default_surface_index.gid) surface_stim_creator_form.fill_from_trait(current_surface_stim) surface_stim_selector_form.display_name.data = common.get_from_session(KEY_SURFACE_STIMULI_NAME) template_specification = dict(title="Spatio temporal - Surface stimulus") template_specification['surfaceStimulusSelectForm'] = self.render_spatial_form(surface_stim_selector_form) template_specification['surfaceStimulusCreateForm'] = self.render_spatial_form(surface_stim_creator_form) self.plotted_equation_prefixes = { self.SURFACE_FIELD: surface_stim_creator_form.surface.name, self.DISPLAY_NAME_FIELD: surface_stim_selector_form.display_name.name } template_specification['mainContent'] = 'spatial/stimulus_surface_step1_main' template_specification['baseUrl'] = self.base_url template_specification['spatialFieldsPrefixes'] = json.dumps(self.plotted_equation_prefixes) template_specification['next_step_url'] = '/spatial/stimulus/surface/step_1_submit' template_specification['definedFocalPoints'] = current_surface_stim.focal_points_triangles.tolist() template_specification = self._add_extra_fields_to_interface(template_specification) return self.fill_default_attributes(template_specification)
def test_import_export(self, user_factory, project_factory, value_wrapper_factory): """ Test the import/export mechanism for a project structure. The project contains the following data types: Connectivity, Surface, MappedArray and ValueWrapper. """ test_user = user_factory() test_project = project_factory(test_user, "TestImportExport", "test_desc") zip_path = os.path.join(os.path.dirname(tvb_data.__file__), 'connectivity', 'connectivity_66.zip') TestFactory.import_zip_connectivity(test_user, test_project, zip_path) value_wrapper = value_wrapper_factory(test_user, test_project) result = self.get_all_datatypes() expected_results = {} for one_data in result: expected_results[one_data.gid] = (one_data.module, one_data.type) # Export project as ZIP self.zip_path = ExportManager().export_project(test_project) assert self.zip_path is not None, "Exported file is none" # Remove the original project self.project_service.remove_project(test_project.id) result, lng_ = self.project_service.retrieve_projects_for_user( test_user.id) assert 0 == len(result), "Project Not removed!" assert 0 == lng_, "Project Not removed!" # Now try to import again project self.import_service.import_project_structure(self.zip_path, test_user.id) result = self.project_service.retrieve_projects_for_user( test_user.id)[0] assert len(result) == 1, "There should be only one project." assert result[ 0].name == "TestImportExport", "The project name is not correct." assert result[ 0].description == "test_desc", "The project description is not correct." test_project = result[0] count_operations = dao.get_filtered_operations(test_project.id, None, is_count=True) # 1 op. - import conn; 2 op. - BCT Analyzer assert 2 == count_operations, "Invalid ops number after export and import !" for gid in expected_results: datatype = dao.get_datatype_by_gid(gid) assert datatype.module == expected_results[gid][ 0], 'DataTypes not imported correctly' assert datatype.type == expected_results[gid][ 1], 'DataTypes not imported correctly' # check the value wrapper new_val = try_get_last_datatype(test_project.id, ValueWrapperIndex) assert value_wrapper.data_value == new_val.data_value, "Data value incorrect" assert value_wrapper.data_type == new_val.data_type, "Data type incorrect" assert value_wrapper.data_name == new_val.data_name, "Data name incorrect"
def test_remove_time_series(self, time_series_region_index_factory): """ Tests the happy flow for the deletion of a time series. """ count_ts = self.count_all_entities(TimeSeriesRegionIndex) assert 0 == count_ts, "There should be no time series" conn = try_get_last_datatype(self.test_project.id, ConnectivityIndex) conn = h5.load_from_index(conn) rm = try_get_last_datatype(self.test_project.id, RegionMappingIndex) rm = h5.load_from_index(rm) time_series_region_index_factory(conn, rm) series = self.get_all_entities(TimeSeriesRegionIndex) assert 1 == len(series), "There should be only one time series" self.project_service.remove_datatype(self.test_project.id, series[0].gid) res = dao.get_datatype_by_gid(series[0].gid) assert res is None, "The time series was not deleted."
def ensure_shell_surface(project_id, shell_surface=None, preferred_type=FACE): filter = FilterChain(fields=[FilterChain.datatype + '.surface_type'], operations=["=="], values=[preferred_type]) if shell_surface is None: shell_surface = try_get_last_datatype(project_id, SurfaceIndex, filter) if not shell_surface: LOG.warning('No object of type %s found in current project.' % preferred_type) return shell_surface
def step_1(self): """ Generate the required template dictionary for the first step. """ current_stimuli_region = common.get_from_session(KEY_REGION_STIMULUS) selected_stimulus_gid = current_stimuli_region.gid.hex project_id = common.get_current_project().id region_stim_selector_form = StimulusRegionSelectorForm(project_id) region_stim_selector_form.region_stimulus.data = selected_stimulus_gid region_stim_selector_form.display_name.data = common.get_from_session( KEY_REGION_STIMULUS_NAME) region_stim_creator_form = RegionStimulusCreatorForm( self.equation_choices, project_id) if not hasattr( current_stimuli_region, 'connectivity') or not current_stimuli_region.connectivity: conn = try_get_last_datatype(project_id, ConnectivityIndex) if conn is None: current_stimuli_region.connectivity = uuid.uuid4() common.set_error_message(self.MSG_MISSING_CONNECTIVITY) else: current_stimuli_region.connectivity = uuid.UUID(conn.gid) region_stim_creator_form.fill_from_trait(current_stimuli_region) template_specification = dict( title="Spatio temporal - Region stimulus") template_specification[ 'mainContent'] = 'spatial/stimulus_region_step1_main' template_specification['isSingleMode'] = True template_specification[ 'regionStimSelectorForm'] = self.render_spatial_form( region_stim_selector_form) template_specification[ 'regionStimCreatorForm'] = self.render_spatial_form( region_stim_creator_form) template_specification['baseUrl'] = '/spatial/stimulus/region' self.plotted_equation_prefixes = { self.CONNECTIVITY_FIELD: region_stim_creator_form.connectivity.name, self.TEMPORAL_FIELD: region_stim_creator_form.temporal.name, self.TEMPORAL_PARAMS_FIELD: region_stim_creator_form.temporal_params.name[1:], self.DISPLAY_NAME_FIELD: region_stim_selector_form.display_name.name } template_specification['fieldsWithEvents'] = json.dumps( self.plotted_equation_prefixes) template_specification[ 'next_step_url'] = '/spatial/stimulus/region/step_1_submit' template_specification['anyScaling'] = 0 template_specification = self._add_extra_fields_to_interface( template_specification) return self.fill_default_attributes(template_specification)
def step_1(self, do_reset=0, **kwargs): """ Generate the html for the first step of the local connectivity page. :param do_reset: Boolean telling to start from empty page or not :param kwargs: not actually used, but parameters are still submitted from UI since we just\ use the same js function for this. """ project_id = common.get_current_project().id if int(do_reset) == 1: new_lconn = LocalConnectivityCreatorModel() default_surface_index = try_get_last_datatype(project_id, SurfaceIndex, LocalConnectivityCreatorForm.get_filters()) if default_surface_index: new_lconn.surface = uuid.UUID(default_surface_index.gid) else: # Surface is required in model and we should keep it like this, but we also want to new_lconn.surface = uuid.uuid4() common.set_error_message(self.MSG_MISSING_SURFACE) common.add2session(KEY_LCONN, new_lconn) current_lconn = common.get_from_session(KEY_LCONN) existent_lcon_form = self.algorithm_service.prepare_adapter_form(form_instance=LocalConnectivitySelectorForm(), project_id=common.get_current_project().id) existent_lcon_form.existentEntitiesSelect.data = current_lconn.gid.hex configure_lcon_form = self.algorithm_service.prepare_adapter_form( form_instance=LocalConnectivityCreatorForm(), project_id=common.get_current_project().id) configure_lcon_form.fill_from_trait(current_lconn) current_lconn.equation = configure_lcon_form.spatial.value() template_specification = dict(title="Surface - Local Connectivity") template_specification['mainContent'] = 'spatial/local_connectivity_step1_main' template_specification['inputList'] = self.render_spatial_form(configure_lcon_form) template_specification['displayCreateLocalConnectivityBtn'] = True template_specification['loadExistentEntityUrl'] = LOAD_EXISTING_URL template_specification['resetToDefaultUrl'] = RELOAD_DEFAULT_PAGE_URL template_specification['existentEntitiesInputList'] = self.render_spatial_form(existent_lcon_form) template_specification['submit_parameters_url'] = '/spatial/localconnectivity/create_local_connectivity' template_specification['equationViewerUrl'] = '/spatial/localconnectivity/get_equation_chart' template_specification['baseUrl'] = self.base_url self.plotted_equation_prefixes = {self.SURFACE_FIELD: configure_lcon_form.surface.name, self.EQUATION_FIELD: configure_lcon_form.spatial.name, self.CUTOFF_FIELD: configure_lcon_form.cutoff.name, self.DISPLAY_NAME_FIELD: configure_lcon_form.display_name.name, self.EQUATION_PARAMS_FIELD: configure_lcon_form.spatial.subform_field.name[1:]} template_specification['equationsPrefixes'] = json.dumps(self.plotted_equation_prefixes) template_specification['next_step_url'] = '/spatial/localconnectivity/step_2' return self.fill_default_attributes(template_specification)
def build(test_user, test_project): view_model = BaseBCTModel() view_model.connectivity = get_filtered_datatypes(test_project.id, ConnectivityIndex, page_size=1)[0][0][2] adapter = ABCAdapter.build_adapter_from_class(TransitivityBinaryDirected) op = OperationService().fire_operation(adapter, test_user, test_project.id, view_model=view_model) # wait for the operation to finish tries = 5 while not op.has_finished and tries > 0: sleep(5) tries = tries - 1 op = dao.get_operation_by_id(op.id) value_wrapper = try_get_last_datatype(test_project.id, ValueWrapperIndex) count = dao.count_datatypes(test_project.id, ValueWrapperIndex) assert 1 == count return value_wrapper
def test_remove_used_connectivity(self): """ Tests the remove of a connectivity which is used by other data types TODO: TVB-2688 """ conn = try_get_last_datatype(self.test_project.id, ConnectivityIndex) assert conn is not None conn_gid = conn.gid count_rm = self.count_all_entities(RegionMappingIndex) assert 1 == count_rm try: self.project_service.remove_datatype(self.test_project.id, conn.gid) raise AssertionError( "The connectivity is still used. It should not be possible to remove it." + str(conn_gid)) except RemoveDataTypeException: # OK, do nothing pass res = dao.get_datatype_by_gid(conn_gid) assert conn.id == res.id, "Used connectivity removed"
def _assert_one_more_datatype(project, dt_class, prev_count=0): dt = try_get_last_datatype(project.id, dt_class) count = dao.count_datatypes(project.id, dt_class) assert prev_count + 1 == count, "Project should contain only one new DT." assert dt is not None, "Retrieved DT should not be empty" return dt