def deserialize_simulator(simulator_gid, storage_path): simulator_in_path = h5.path_for(storage_path, SimulatorH5, simulator_gid) simulator_in = SimulatorAdapterModel() with SimulatorH5(simulator_in_path) as simulator_in_h5: simulator_in_h5.load_into(simulator_in) simulator_in.connectivity = simulator_in_h5.connectivity.load() simulator_in.stimulus = simulator_in_h5.stimulus.load() simulator_in.history_gid = simulator_in_h5.simulation_state.load() for monitor in simulator_in.monitors: if isinstance(monitor, Projection): with SimulatorH5(simulator_in_path) as simulator_in_h5: monitor_h5_path = simulator_in_h5.get_reference_path( monitor.gid) monitor_h5_class = h5_factory.monitor_h5_factory(type(monitor)) with monitor_h5_class(monitor_h5_path) as monitor_h5: sensors_gid = monitor_h5.sensors.load() projection_gid = monitor_h5.projection.load() region_mapping_gid = monitor_h5.region_mapping.load() sensors_index = ABCAdapter.load_entity_by_gid(sensors_gid.hex) projection_index = ABCAdapter.load_entity_by_gid( projection_gid.hex) sensors_class = monitor.projection_class().sensors.field_type sensors = h5.load_from_index(sensors_index, dt_class=sensors_class) projection_class = monitor.projection_class() projection = h5.load_from_index(projection_index, dt_class=projection_class) region_mapping = ABCAdapter.load_traited_by_gid( region_mapping_gid) monitor.sensors = sensors monitor.projection = projection monitor.region_mapping = region_mapping if simulator_in.surface: cortex_path = h5.path_for(storage_path, CortexH5, simulator_in.surface.gid) with CortexH5(cortex_path) as cortex_h5: simulator_in.surface.local_connectivity = cortex_h5.local_connectivity.load( ) simulator_in.surface.region_mapping_data = cortex_h5.region_mapping_data.load( ) rm_index = dao.get_datatype_by_gid( simulator_in.surface.region_mapping_data.hex) simulator_in.surface.surface_gid = uuid.UUID( rm_index.fk_surface_gid) return simulator_in
def deserialize_simulator(simulator_gid, storage_path): simulator_in_path = h5.path_for(storage_path, SimulatorH5, simulator_gid) simulator_in = SimulatorAdapterModel() with SimulatorH5(simulator_in_path) as simulator_in_h5: simulator_in_h5.load_into(simulator_in) simulator_in.connectivity = simulator_in_h5.connectivity.load() simulator_in.stimulus = simulator_in_h5.stimulus.load() simulator_in.history_gid = simulator_in_h5.simulation_state.load() if isinstance(simulator_in.monitors[0], Projection): # TODO: simplify this part with SimulatorH5(simulator_in_path) as simulator_in_h5: monitor_h5_path = simulator_in_h5.get_reference_path( simulator_in.monitors[0].gid) monitor_h5_class = h5_factory.monitor_h5_factory( type(simulator_in.monitors[0])) with monitor_h5_class(monitor_h5_path) as monitor_h5: sensors_gid = monitor_h5.sensors.load() region_mapping_gid = monitor_h5.region_mapping.load() sensors = ABCAdapter.load_traited_by_gid(sensors_gid) if isinstance(simulator_in.monitors[0], EEG): sensors = SensorsEEG.build_sensors_subclass(sensors) elif isinstance(simulator_in.monitors[0], MEG): sensors = SensorsMEG.build_sensors_subclass(sensors) elif isinstance(simulator_in.monitors[0], iEEG): sensors = SensorsInternal.build_sensors_subclass(sensors) simulator_in.monitors[0].sensors = sensors region_mapping = ABCAdapter.load_traited_by_gid(region_mapping_gid) simulator_in.monitors[0].region_mapping = region_mapping if simulator_in.surface: cortex_path = h5.path_for(storage_path, CortexH5, simulator_in.surface.gid) with CortexH5(cortex_path) as cortex_h5: simulator_in.surface.local_connectivity = cortex_h5.local_connectivity.load( ) simulator_in.surface.region_mapping_data = cortex_h5.region_mapping_data.load( ) rm_index = dao.get_datatype_by_gid( simulator_in.surface.region_mapping_data.hex) simulator_in.surface.surface_gid = uuid.UUID( rm_index.surface_gid) return simulator_in
def test_export_simulator_configuration(self, operation_factory, connectivity_factory): """ Test export of a simulator configuration """ operation = operation_factory() simulator = SimulatorAdapterModel() simulator.connectivity = connectivity_factory(4).gid burst_configuration = BurstConfiguration(self.test_project.id) burst_configuration.fk_simulation_id = operation.id burst_configuration.simulator_gid = simulator.gid.hex burst_configuration = dao.store_entity(burst_configuration) storage_path = FilesHelper().get_project_folder( self.test_project, str(operation.id)) h5_path = h5.path_for(storage_path, SimulatorH5, simulator.gid) with SimulatorH5(h5_path) as h5_file: h5_file.store(simulator) export_file = self.export_manager.export_simulator_configuration( burst_configuration.id) assert export_file is not None, "Export process should return path to export file" assert os.path.exists( export_file ), "Could not find export file: %s on disk." % export_file assert zipfile.is_zipfile( export_file), "Generated file is not a valid ZIP file"
def test_export(self): op = TestFactory.create_operation() simulator_index = SimulatorIndex() simulator_index.fill_from_has_traits(self.session_stored_simulator) burst_config = BurstConfiguration(self.test_project.id, simulator_index.id) burst_config = dao.store_entity(burst_config) simulator_index.fk_from_operation = op.id simulator_index = dao.store_entity(simulator_index) simulator_index.fk_parent_burst = burst_config.id simulator_index = dao.store_entity(simulator_index) simulator_h5 = h5.path_for_stored_index(simulator_index) with SimulatorH5(simulator_h5) as h5_file: h5_file.store(self.session_stored_simulator) burst = dao.get_bursts_for_project(self.test_project.id) self.sess_mock['burst_id'] = str(burst[0].id) with patch('cherrypy.session', self.sess_mock, create=True): common.add2session(common.KEY_BURST_CONFIG, self.session_stored_simulator) common.add2session(common.KEY_BURST_CONFIG, burst_config) result = self.simulator_controller.export(str(burst[0].id)) assert path.exists(result.input.name), "Simulation was not exported!"
def serialize_simulator(simulator, simulation_state_gid, storage_path): simulator_path = h5.path_for(storage_path, SimulatorH5, simulator.gid) with SimulatorH5(simulator_path) as simulator_h5: simulator_h5.store(simulator) if simulation_state_gid: simulator_h5.simulation_state.store( uuid.UUID(simulation_state_gid))
def test_export(self): op = TestFactory.create_operation(test_user=self.test_user, test_project=self.test_project) burst_config = BurstConfiguration(self.test_project.id) burst_config.fk_simulation = op.id burst_config.simulator_gid = self.session_stored_simulator.gid.hex burst_config = dao.store_entity(burst_config) storage_path = FilesHelper().get_project_folder( self.test_project, str(op.id)) h5_path = h5.path_for(storage_path, SimulatorH5, self.session_stored_simulator.gid) with SimulatorH5(h5_path) as h5_file: h5_file.store(self.session_stored_simulator) burst = dao.get_bursts_for_project(self.test_project.id) self.sess_mock['burst_id'] = str(burst[0].id) with patch('cherrypy.session', self.sess_mock, create=True): common.add2session(common.KEY_BURST_CONFIG, self.session_stored_simulator) common.add2session(common.KEY_BURST_CONFIG, burst_config) result = self.simulator_controller.export(str(burst[0].id)) assert path.exists(result.input.name), "Simulation was not exported!"
def test_export_simulator_configuration(self, operation_factory): """ Test export of a simulator configuration """ operation = operation_factory() simulator = Simulator() simulator_index = SimulatorIndex() simulator_index.fill_from_has_traits(simulator) simulator_index.fk_from_operation = operation.id simulator_index = dao.store_entity(simulator_index) burst_configuration = BurstConfiguration(self.test_project.id, simulator_index.id) burst_configuration = dao.store_entity(burst_configuration) simulator_index.fk_parent_burst = burst_configuration.id simulator_index = dao.store_entity(simulator_index) simulator_h5 = h5.path_for_stored_index(simulator_index) with SimulatorH5(simulator_h5) as h5_file: h5_file.store(simulator) export_file = self.export_manager.export_simulator_configuration( burst_configuration.id) assert export_file is not None, "Export process should return path to export file" assert os.path.exists( export_file ), "Could not find export file: %s on disk." % export_file assert zipfile.is_zipfile( export_file), "Generated file is not a valid ZIP file"
def deserialize_simulator(simulator_gid, storage_path): simulator_in_path = h5.path_for(storage_path, SimulatorH5, simulator_gid) simulator_in = Simulator() with SimulatorH5(simulator_in_path) as simulator_in_h5: simulator_in_h5.load_into(simulator_in) connectivity_gid = simulator_in_h5.connectivity.load() stimulus_gid = simulator_in_h5.stimulus.load() simulation_state_gid = simulator_in_h5.simulation_state.load() conn_index = dao.get_datatype_by_gid(connectivity_gid.hex) conn = h5.load_from_index(conn_index) simulator_in.connectivity = conn if simulator_in.surface: cortex_path = h5.path_for(storage_path, CortexH5, simulator_in.surface.gid) with CortexH5(cortex_path) as cortex_h5: local_conn_gid = cortex_h5.local_connectivity.load() region_mapping_gid = cortex_h5.region_mapping_data.load() region_mapping_index = dao.get_datatype_by_gid( region_mapping_gid.hex) region_mapping_path = h5.path_for_stored_index( region_mapping_index) region_mapping = RegionMapping() with RegionMappingH5(region_mapping_path) as region_mapping_h5: region_mapping_h5.load_into(region_mapping) region_mapping.gid = region_mapping_h5.gid.load() surf_gid = region_mapping_h5.surface.load() surf_index = dao.get_datatype_by_gid(surf_gid.hex) surf_h5 = h5.h5_file_for_index(surf_index) surf = CorticalSurface() surf_h5.load_into(surf) surf_h5.close() region_mapping.surface = surf simulator_in.surface.region_mapping_data = region_mapping if local_conn_gid: local_conn_index = dao.get_datatype_by_gid(local_conn_gid.hex) local_conn = h5.load_from_index(local_conn_index) simulator_in.surface.local_connectivity = local_conn if stimulus_gid: stimulus_index = dao.get_datatype_by_gid(stimulus_gid.hex) stimulus = h5.load_from_index(stimulus_index) simulator_in.stimulus = stimulus return simulator_in, simulation_state_gid
def serialize_simulator(simulator, simulator_gid, simulation_state_gid, storage_path): simulator_path = h5.path_for(storage_path, SimulatorH5, simulator_gid) with SimulatorH5(simulator_path) as simulator_h5: simulator_h5.gid.store(uuid.UUID(simulator_gid)) simulator_h5.store(simulator) simulator_h5.connectivity.store(simulator.connectivity.gid) if simulator.stimulus: simulator_h5.stimulus.store(uuid.UUID(simulator.stimulus.gid)) if simulation_state_gid: simulator_h5.simulation_state.store(uuid.UUID(simulation_state_gid)) return simulator_gid
def prepare_simulation_on_server(self, user_id, project, algorithm, zip_folder_path, simulator_file): with SimulatorH5(simulator_file) as simulator_h5: simulator_gid = simulator_h5.gid.load() metadata = {} simulator_id = algorithm.id algo_category = algorithm.algorithm_category operation = self._prepare_operation(project.id, user_id, simulator_id, simulator_gid, algo_category, None, metadata) storage_operation_path = self.files_helper.get_project_folder(project, str(operation.id)) self.async_launch_simulation_on_server(operation, zip_folder_path, storage_operation_path) return operation
def load_from_zip(self, zip_file, project): import_service = ImportService() simulator_folder = import_service.import_simulator_configuration_zip( zip_file) simulator_h5_filename = DirLoader( simulator_folder, None).find_file_for_has_traits_type(Simulator) with SimulatorH5(os.path.join(simulator_folder, simulator_h5_filename)) as sim_h5: simulator_gid = sim_h5.gid.load() simulator = SimulatorSerializer.deserialize_simulator( simulator_gid, simulator_folder) burst_config = self.burst_service.load_burst_configuration_from_folder( simulator_folder, project) return simulator, burst_config