def test_server_launch_operation(self, mocker, time_series_index_factory): self._mock_user(mocker) algorithm_module = "tvb.adapters.analyzers.fourier_adapter" algorithm_class = "FourierAdapter" input_ts_index = time_series_index_factory() fft_model = FFTAdapterModel() fft_model.time_series = UUID(input_ts_index.gid) fft_model.window_function = list(SUPPORTED_WINDOWING_FUNCTIONS)[0] input_folder = self.storage_interface.get_project_folder(self.test_project.name) view_model_h5_path = h5.store_view_model(fft_model, input_folder) # Mock flask.request.files to return a dictionary request_mock = mocker.patch.object(flask, 'request', spec={}) fp = open(view_model_h5_path, 'rb') request_mock.files = { RequestFileKey.LAUNCH_ANALYZERS_MODEL_FILE.value: FileStorage(fp, os.path.basename(view_model_h5_path))} # Mock launch_operation() call and current_user mocker.patch.object(OperationService, 'launch_operation') operation_gid, status = self.launch_resource.post(project_gid=self.test_project.gid, algorithm_module=algorithm_module, algorithm_classname=algorithm_class) fp.close() assert type(operation_gid) is str assert len(operation_gid) > 0
def test_server_launch_operation(self, mocker, time_series_index_factory): algorithm_module = "tvb.adapters.analyzers.fourier_adapter" algorithm_class = "FourierAdapter" input_ts_index = time_series_index_factory() fft_model = FFTAdapterModel() fft_model.time_series = UUID(input_ts_index.gid) fft_model.window_function = list(SUPPORTED_WINDOWING_FUNCTIONS)[0] input_folder = self.files_helper.get_project_folder(self.test_project) view_model_h5_path = h5.path_for(input_folder, ViewModelH5, fft_model.gid) view_model_h5 = ViewModelH5(view_model_h5_path, fft_model) view_model_h5.store(fft_model) view_model_h5.close() # Mock flask.request.files to return a dictionary request_mock = mocker.patch.object(flask, 'request') fp = open(view_model_h5_path, 'rb') request_mock.files = { 'file': FileStorage(fp, os.path.basename(view_model_h5_path)) } # Mock launch_operation() call mocker.patch.object(OperationService, 'launch_operation') operation_gid, status = self.launch_resource.post( self.test_project.gid, algorithm_module, algorithm_class) fp.close() assert type(operation_gid) is str assert len(operation_gid) > 0
def run_analyzer(): from tvb.interfaces.command.lab import * 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) # Prepare the Adapter adapter_instance = ABCAdapter.build_adapter_from_class(FourierAdapter) # Prepare the input algorithms as if they were coming from web UI submit: time_series = dao.get_generic_entity(TimeSeriesRegionIndex, DataTypeMetaData.DEFAULT_SUBJECT, "subject") if len(time_series) < 1: log.error("We could not find a compatible TimeSeries Datatype!") fourier_model = FFTAdapterModel() fourier_model.time_series = time_series[0].gid fourier_model.window_function = 'hamming' fourier_model.segment_length = 100 # launch an operation and have the results stored both in DB and on disk launched_operation = OperationService().fire_operation( adapter_instance, project.administrator, project.id, view_model=fourier_model)[0] # 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: fourier_spectrum = dao.get_generic_entity(FourierSpectrumIndex, launched_operation.id, "fk_from_operation")[0] log.info("Fourier Spectrum result is: %s " % fourier_spectrum) else: log.warning( "Operation ended with problems [%s]: [%s]" % (launched_operation.status, launched_operation.additional_info))
def fire_simulation_example(tvb_client_instance): logger.info("Requesting projects for logged user") projects_of_user = tvb_client_instance.get_project_list() assert len(projects_of_user) > 0 logger.info("TVB has {} projects for this user".format( len(projects_of_user))) project_gid = projects_of_user[0].gid logger.info("Requesting datatypes from project {}...".format(project_gid)) data_in_project = tvb_client_instance.get_data_in_project(project_gid) logger.info("We have {} datatypes".format(len(data_in_project))) logger.info("Requesting operations from project {}...".format(project_gid)) ops_in_project, _ = tvb_client_instance.get_operations_in_project( project_gid, 1) logger.info("Displayname of the first operation is: {}".format( ops_in_project[0].displayname)) connectivity_gid = None datatypes_type = [] for datatype in data_in_project: datatypes_type.append(datatype.type) if datatype.type == ConnectivityIndex().display_type: connectivity_gid = datatype.gid logger.info("The datatypes in project are: {}".format(datatypes_type)) if connectivity_gid: logger.info("Preparing the simulator...") simulator = SimulatorAdapterModel() simulator.connectivity = connectivity_gid simulator.simulation_length = 100 logger.info("Starting the simulation...") operation_gid = tvb_client_instance.fire_simulation( project_gid, simulator) logger.info("Monitoring the simulation operation...") monitor_operation(tvb_client_instance, operation_gid) logger.info("Requesting the results of the simulation...") simulation_results = tvb_client_instance.get_operation_results( operation_gid) datatype_names = [] for datatype in simulation_results: datatype_names.append(datatype.name) logger.info("The resulted datatype are: {}".format(datatype_names)) time_series_gid = simulation_results[1].gid logger.info("Download the time series file...") time_series_path = tvb_client_instance.retrieve_datatype( time_series_gid, tvb_client_instance.temp_folder) logger.info( "The time series file location is: {}".format(time_series_path)) logger.info("Requesting algorithms to run on time series...") algos = tvb_client_instance.get_operations_for_datatype( time_series_gid) algo_names = [algo.displayname for algo in algos] logger.info("Possible algorithms are {}".format(algo_names)) logger.info("Launch Fourier Analyzer...") fourier_model = FFTAdapterModel() fourier_model.time_series = time_series_gid fourier_model.window_function = 'hamming' operation_gid = tvb_client_instance.launch_operation( project_gid, FourierAdapter, fourier_model) logger.info( "Fourier Analyzer operation has launched with gid {}".format( operation_gid)) logger.info("Download the connectivity file...") connectivity_path = tvb_client_instance.retrieve_datatype( connectivity_gid, tvb_client_instance.temp_folder) logger.info( "The connectivity file location is: {}".format(connectivity_path)) logger.info("Loading an entire Connectivity datatype in memory...") connectivity = tvb_client_instance.load_datatype_from_file( connectivity_path) logger.info("Info on current Connectivity: {}".format( connectivity.summary_info())) logger.info( "Loading a chuck from the time series H5 file, as this can be very large..." ) with TimeSeriesH5(time_series_path) as time_series_h5: data_shape = time_series_h5.read_data_shape() chunk = time_series_h5.read_data_slice( tuple([ slice(20), slice(data_shape[1]), slice(data_shape[2]), slice(data_shape[3]) ])) assert chunk.shape[0] == 20 assert chunk.shape[1] == data_shape[1] assert chunk.shape[2] == data_shape[2] assert chunk.shape[3] == data_shape[3] return project_gid, time_series_gid
logger.info("The resulted datatype are: {}".format(datatype_names)) time_series_gid = simulation_results[1].gid logger.info("Download the time series file...") time_series_path = tvb_client.retrieve_datatype( time_series_gid, tvb_client.temp_folder) logger.info( "The time series file location is: {}".format(time_series_path)) logger.info("Requesting algorithms to run on time series...") algos = tvb_client.get_operations_for_datatype(time_series_gid) algo_names = [algo.displayname for algo in algos] logger.info("Possible algorithms are {}".format(algo_names)) logger.info("Launch Fourier Analyzer...") fourier_model = FFTAdapterModel() fourier_model.time_series = uuid.UUID(time_series_gid) fourier_model.window_function = 'hamming' algo_dto = None for algo in algos: if algo.classname == 'FourierAdapter': algo_dto = algo operation_gid = tvb_client.launch_operation(project_gid, algo_dto.module, algo_dto.classname, fourier_model) logger.info( "Fourier Analyzer operation has launched with gid {}".format( operation_gid))
logger.info("The resulted datatype are: {}".format(datatype_names)) time_series_gid = simulation_results[1].gid logger.info("Download the time series file...") time_series_path = tvb_client.retrieve_datatype( time_series_gid, tvb_client.temp_folder) logger.info( "The time series file location is: {}".format(time_series_path)) logger.info("Requesting algorithms to run on time series...") algos = tvb_client.get_operations_for_datatype(time_series_gid) algo_names = [algo.displayname for algo in algos] logger.info("Possible algorithms are {}".format(algo_names)) logger.info("Launch Fourier Analyzer...") fourier_model = FFTAdapterModel() fourier_model.time_series = time_series_gid fourier_model.window_function = 'hamming' operation_gid = tvb_client.launch_operation(project_gid, FourierAdapter, fourier_model) logger.info( "Fourier Analyzer operation has launched with gid {}".format( operation_gid)) logger.info("Download the connectivity file...") connectivity_path = tvb_client.retrieve_datatype( connectivity_gid, tvb_client.temp_folder) logger.info( "The connectivity file location is: {}".format(connectivity_path))