def test_collect_background(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_manager.background_manager = BackgroundImageManager( self.background_folder) pipeline_id = "test_pipeline" number_of_images = 10 pipeline_config = PipelineConfig( pipeline_id, parameters={"camera_name": "simulation"}) instance_manager.config_manager.save_pipeline_config( pipeline_id, pipeline_config.get_configuration()) pipeline_stream_address = instance_manager.get_instance_stream( pipeline_id) pipeline_host, pipeline_port = get_host_port_from_stream_address( pipeline_stream_address) # Collect from the pipeline. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "This should really not happen anymore.") camera_name = instance_manager.get_instance( pipeline_id).get_info()["camera_name"] background_id = instance_manager.collect_background( camera_name, number_of_images) self.assertTrue(background_id.startswith("simulation"), "Background id not as expected.") host, port = get_host_port_from_stream_address( instance_manager.cam_server_client.get_instance_stream( "simulation")) # Collect from the camera. with source(host=host, port=port, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "This should really not happen anymore.") self.assertEqual( instance_manager.background_manager.get_background( background_id).shape, data.data.data["image"].value.shape, "Background and image have to be of the same shape.") self.assertEqual( instance_manager.background_manager.get_background( background_id).dtype, data.data.data["image"].value.dtype, "Background and image have to be of the same dtype.") instance_manager.stop_all_instances()
def test_user_scripts(self): script_name = "Test.py" script_content = "print('Hello world')" self.pipeline_client.set_user_script(script_name, script_content) scripts = self.pipeline_client.get_user_scripts() self.assertIn(script_name, scripts) ret = self.pipeline_client.get_user_script(script_name) self.assertEqual(ret, script_content) filename = "temp/Test2.py" with open(filename, "w") as data_file: data_file.write(script_content) self.pipeline_client.upload_user_script(filename) os.remove(filename) self.pipeline_client.download_user_script(filename) with open(filename, "r") as data_file: ret = data_file.read() self.assertEqual(ret, script_content) script_content = """ from cam_server.pipeline.data_processing import functions, processor def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata): ret = processor.process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata) ret["average_value"] = float(ret["intensity"]) / len(ret["x_axis"]) / len(ret["y_axis"]) return ret """ filename = "temp/Test.py" with open(filename, "w") as data_file: data_file.write(script_content) instance_id, stream_address = self.pipeline_client.create_instance_from_config( {"camera_name": "simulation"}) host, port = get_host_port_from_stream_address(stream_address) with source(host=host, port=port, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "This should really not happen anymore.") self.assertIsNotNone(data.data.data["width"].value) self.assertIsNotNone(data.data.data["height"].value) self.pipeline_client.set_function_script(instance_id, filename) time.sleep(1.0) with source(host=host, port=port, mode=SUB) as stream: data = stream.receive() print(data.data.data.keys()) self.assertIsNotNone(data.data.data["average_value"].value)
def run_continuously(self): with source( channels=[ "SARES20-CAMS142-M5.roi_signal_x_profile", "SAR-CVME-TIFALL5:EvtSet", ] ) as s: while True: m = s.receive() ix = m.data.pulse_id prof = m.data.data["SARES20-CAMS142-M5.roi_signal_x_profile"].value if prof is None: continue codes = m.data.data["SAR-CVME-TIFALL5:EvtSet"].value if codes is None: continue is_reference = codes[25] == 1 try: if (lastgoodix - ix) > 1: print(f"missed {lastgoodix-ix-1} events!") except: pass lastgoodix = ix if is_reference: self.bg.append(prof) else: self.sig.append((prof / np.asarray(self.bg).mean(axis=0))) self.spec_ppd.append(prof) pos, amp = self.analyze_step() self.pos.append(pos) self.amp.append(amp)
def collect_background(camera_name, stream_address, n_images, background_manager): try: host, port = get_host_port_from_stream_address(stream_address) accumulator_image = None with source(host=host, port=port, mode=SUB) as stream: for _ in range(n_images): data = stream.receive() image = data.data.data["image"].value accumulator_image = sum_images(image, accumulator_image) background_prefix = camera_name background_image = accumulator_image / n_images # Convert image to uint16. background_image = background_image.astype(dtype="uint16") background_id = background_manager.save_background( background_prefix, background_image) return background_id except: _logger.exception("Error while collecting background.") raise
def test_no_roi(self): client = PsenProcessingClient("http://localhost:10000/") roi_signal = [] client.set_roi_signal(roi_signal) roi_background = None client.set_roi_background(roi_background) client.start() processed_data = [] with source(host="localhost", port=12000, mode=PULL, receive_timeout=1000) as input_stream: self.sending_process.start() sleep(0.5) for index in range(self.n_images): processed_data.append(input_stream.receive()) client.stop() roi_signal_parameter_name = self.pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".roi_signal_x_profile" roi_background_parameter_name = self.pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".roi_background_x_profile" # All the messages should be equal. received_data = processed_data[0] # If the roi is not set, the value should not be added to the output. self.assertTrue(roi_signal_parameter_name not in received_data.data.data) self.assertTrue(roi_background_parameter_name not in received_data.data.data)
def test_store_pipeline_with_simulated_camera(self): manager = multiprocessing.Manager() stop_event = multiprocessing.Event() statistics = manager.Namespace() parameter_queue = multiprocessing.Queue() self.client.stop_all_instances() pipeline_config = PipelineConfig("test_pipeline") def send(): store_pipeline(stop_event, statistics, parameter_queue, self.client, pipeline_config, 12003, MockBackgroundManager()) thread = Thread(target=send) thread.start() sleep(0.5) with source(host="127.0.0.1", port=12003, mode=PULL, receive_timeout=3000) as stream: data = stream.receive() self.assertIsNotNone(data, "Receiving timeout.") required_keys = set(["simulation" + config.EPICS_PV_SUFFIX_IMAGE]) self.assertSetEqual( required_keys, set(data.data.data.keys()), "Missing required keys in pipeline output bsread message.") stop_event.set() thread.join()
def process_requests(stream_address, receive_timeout=None, mode=PULL, data_retrieval_delay=None): if receive_timeout is None: receive_timeout = config.DEFAULT_RECEIVE_TIMEOUT if data_retrieval_delay is None: data_retrieval_delay = config.DEFAULT_DATA_RETRIEVAL_DELAY source_host, source_port = stream_address.rsplit(":", maxsplit=1) source_host = source_host.split("//")[1] source_port = int(source_port) _logger.info("Connecting to broker host %s:%s." % (source_host, source_port)) _logger.info("Using data_retrieval_delay=%s seconds." % data_retrieval_delay) with source(host=source_host, port=source_port, mode=mode, receive_timeout=receive_timeout) as input_stream: while True: message = input_stream.receive() if message is None: continue process_message(message, data_retrieval_delay)
def test_get_simulated_camera(self): from cam_server import CamClient from cam_server.utils import get_host_port_from_stream_address from bsread import source, SUB # Change to match your camera server server_address = "http://0.0.0.0:8888" # Initialize the client. camera_client = CamClient(server_address) # Get stream address of simulation camera. Stream address in format tcp://hostname:port. camera_stream_address = camera_client.get_instance_stream("simulation") # Extract the stream hostname and port from the stream address. camera_host, camera_port = get_host_port_from_stream_address( camera_stream_address) # Subscribe to the stream. with source(host=camera_host, port=camera_port, mode=SUB) as stream: # Receive next message. data = stream.receive() image_width = data.data.data["width"].value image_height = data.data.data["height"].value image_bytes = data.data.data["image"].value print("Image size: %d x %d" % (image_width, image_height)) print("Image data: %s" % image_bytes) x_size, y_size = get_simulated_camera().get_geometry() self.assertIsNotNone(data) self.assertEqual(image_width, x_size) self.assertIsNotNone(image_height, y_size)
def collect_background(self, cam_server_client, camera_name, n_images): stream_address = cam_server_client.get_instance_stream(camera_name) try: host, port = get_host_port_from_stream_address(stream_address) accumulator_image = None with (ipc_source(address=stream_address, mode=SUB) if stream_address.startswith("ipc") else source( host=host, port=port, mode=SUB)) as stream: for _ in range(n_images): data = stream.receive() image = data.data.data["image"].value accumulator_image = sum_images(image, accumulator_image) background_prefix = camera_name background_image = accumulator_image / n_images # Convert image to uint16. background_image = background_image.astype(dtype="uint16") background_id = self.save_background(background_prefix, background_image) return background_id except: _logger.exception("Error while collecting background.") raise
def processStream(): """ Process stream message: 1. collect stream metadata 2. generate and encode the corresponding image data 3. emit the data to connected clients """ with source(host=stream_output_host, port=stream_output_port, receive_timeout=1000) as input_stream: while True: socketio.sleep(delay) message = input_stream.receive() if message is None: continue beam_energy = message.data.data["beam_energy"].value repetition_rate = message.data.data["repetition_rate"].value image = message.data.data["image"].value pyplot.imshow(image) figfile = BytesIO() pyplot.savefig(figfile, format="jpg") figfile.seek(0) metadata = { 'beam_energy': str(beam_energy), 'repetition_rate': str(repetition_rate), 'img': str(base64.b64encode(figfile.getvalue()), encoding='utf-8') } print(beam_energy) socketio.emit('server_response', metadata, namespace='/test_conn')
def test_stream_processor(self): pv_name_prefix = "JUST_TESTING" n_images = 50 original_parameters = {"background": ""} image = numpy.zeros(shape=(1024, 512), dtype="uint16") image += 1 data_to_send = {pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE: image} def send_data(): with sender(port=10000, queue_size=100) as output_stream: for x in range(n_images): output_stream.send(data=data_to_send) def process_data(event): stream_processor = get_stream_processor(input_stream_host="localhost", input_stream_port=10000, data_output_stream_port=11000, image_output_stream_port=11001, epics_pv_name_prefix=pv_name_prefix, output_pv_name="Does not matter", center_pv_name="", fwhm_pv_name="", ymin_pv_name="", ymax_pv_name="", axis_pv_name="") stream_processor(event, original_parameters, {}) running_event = Event() send_process = Process(target=send_data) processing_process = Process(target=process_data, args=(running_event,)) send_process.start() sleep(1) processing_process.start() final_data = [] with source(host="localhost", port=11000, mode=PULL) as input_stream: final_data.append(input_stream.receive()) running_event.clear() sleep(0.2) send_process.terminate() processing_process.terminate() self.assertEqual(len(final_data), 1) parameters = final_data[0].data.data[pv_name_prefix + ":processing_parameters"].value processing_parameters = json.loads(parameters) spectrum = final_data[0].data.data[pv_name_prefix + ":SPECTRUM_Y"].value self.assertEqual(len(spectrum), 512) self.assertListEqual(list(spectrum), [1024] * 512)
def run_continuously(self): with source(channels=[ f"SARES20-CAMS142-{self.cam}.roi_signal_x_profile", "SAR-CVME-TIFALL5:EvtSet", ]) as s: counter = 0 while self._running: m = s.receive() ix = m.data.pulse_id prof = m.data.data[ f"SARES20-CAMS142-{self.cam}.roi_signal_x_profile"].value if prof is None: continue if self.mask: prof = prof[slice(*self.mask)] evt = m.data.data["SAR-CVME-TIFALL5:EvtSet"].value if evt is None: continue if counter == 0: self.tt_sig = np.ndarray((self.Nshots, len(prof))) self.evts = np.ndarray((self.Nshots, len(evt))) self.ids = np.ndarray((self.Nshots, )) lastgoodix = ix self.tt_sig[counter] = prof self.evts[counter] = evt self.ids[counter] = ix counter = counter + 1 if counter == self.Nshots: counter = 0 self.evaluate() continue
def test_start_missing_header(self): with sender(port=self.STREAM_PORT) as output_stream: with source(host="localhost", port=self.STREAM_PORT) as input_stream: for index in range(10): data = {"fast_source": index, "slow_source": None} output_stream.send(data=data) self.writer.write_message(input_stream.receive(handler=self.handler.receive)) for index in range(10, 20): data = {"fast_source": index, "slow_source": index} output_stream.send(data=data) self.writer.write_message(input_stream.receive(handler=self.handler.receive)) self.writer.close() file = h5py.File(self.OUTPUT_FILE) fast_source = file["/data/fast_source/data"] slow_source = file["/data/slow_source/data"] self.assertIsNotNone(fast_source) self.assertIsNotNone(slow_source) self.assertListEqual(list(fast_source), list(range(20))) self.assertListEqual(list(slow_source[:10]), [0] * 10) self.assertListEqual(list(slow_source[10:]), list(range(10, 20))) file.close()
def get_images(self, N_images): data = [] with source(host=self._stream_host, port=self._stream_port, mode=SUB) as input_stream: input_stream.connect() for n in range(N_images): data.append(input_stream.receive().data.data["image"].value) return data
def test_change_roi_while_running(self): client = PsssProcessingClient("http://localhost:10000/") roi = [0, 1024, 0, 1024] client.set_roi(roi) processed_data = [] client.start() with source(host="localhost", port=12000, mode=PULL) as input_stream: # First pulse_id comes before the source connects. for index in range(self.n_images - 1): message = input_stream.receive() processed_data.append(message) updated_roi = [100, 200, 100, 200] client.set_roi(updated_roi) data_to_send = { self.pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE: self.image } with sender(port=10001) as output_stream: for x in range(self.n_images): output_stream.send(data=data_to_send) with source(host="localhost", port=12000, mode=PULL) as input_stream: for index in range(self.n_images): message = input_stream.receive() processed_data.append(message) client.stop() processing_parameters_name = self.pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".processing_parameters" start_processing_parameters = json.loads( processed_data[0].data.data[processing_parameters_name].value) end_processing_parameters = json.loads( processed_data[8].data.data[processing_parameters_name].value) self.assertListEqual(roi, start_processing_parameters["roi"]) self.assertListEqual(updated_roi, end_processing_parameters["roi"])
def stream_receive(): global state try: with source(channels=[reference, streaked]) as stream: while True: message = stream.receive() data_buffer.append(message.data.data) state = 'receiving' except Exception as e: print(e)
def test_change_roi_while_running(self): client = PsenProcessingClient("http://localhost:10000/") roi_signal = [0, 1024, 0, 1024] client.set_roi_signal(roi_signal) roi_background = [0, 100, 0, 100] client.set_roi_background(roi_background) client.start() processed_data = [] with source(host="localhost", port=12000, mode=PULL, receive_timeout=1000) as input_stream: self.sending_process.start() sleep(0.5) for index in range(self.n_images): processed_data.append(input_stream.receive()) updated_roi_signal = [100, 200, 100, 200] client.set_roi_signal(updated_roi_signal) data_to_send = {self.pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE: self.image} def send_data(): with sender(port=11000) as output_stream: for x in range(self.n_images): output_stream.send(data=data_to_send) def receive_data(): with source(host="localhost", port=12000, mode=PULL, receive_timeout=1000) as input_stream: for index in range(self.n_images): processed_data.append(input_stream.receive()) send_thread = Thread(target=send_data) receive_thread = Thread(target=receive_data) receive_thread.start() sleep(0.5) send_thread.start() send_thread.join() receive_thread.join() client.stop() processing_parameters_name = self.pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".processing_parameters" start_processing_parameters = json.loads(processed_data[0].data.data[processing_parameters_name].value) end_processing_parameters = json.loads(processed_data[-1].data.data[processing_parameters_name].value) self.assertListEqual(roi_signal, start_processing_parameters["roi_signal"]) self.assertListEqual(updated_roi_signal, end_processing_parameters["roi_signal"])
def test_bsread_transceiver(self): manager = multiprocessing.Manager() stop_event = multiprocessing.Event() statistics = manager.Namespace() parameter_queue = multiprocessing.Queue() expected_width = 659 expected_height = 494 expected_shape = [expected_width, expected_height] mock_camera = MockCameraBsread(CameraConfig("SLG-LCAM-C102"), expected_width, expected_height, "tcp://0.0.0.0:9999") def transceiver(): process_bsread_camera(stop_event, statistics, parameter_queue, mock_camera, 12000) thread1 = Thread(target=transceiver) thread1.start() test_base_dir = os.path.split(os.path.abspath(__file__))[0] thread2 = Thread(target=replay_dump, args=("tcp://0.0.0.0:9999", os.path.join(test_base_dir, "test_camera_dump"))) thread2.start() with source(host="0.0.0.0", port=12000, mode=SUB) as stream: data1 = stream.receive() data2 = stream.receive() self.assertIsNotNone(data1) self.assertIsNotNone(data2) stop_event.set() thread1.join() thread2.join() self.assertListEqual(list(data1.data.data["image"].value.shape), expected_shape[::-1]) self.assertListEqual(list(data2.data.data["image"].value.shape), expected_shape[::-1]) self.assertEqual(data1.data.data["width"].value, data2.data.data["width"].value) self.assertEqual(data1.data.data["height"].value, data2.data.data["height"].value) self.assertEqual(data1.data.data["width"].value, expected_width) self.assertEqual(data1.data.data["height"].value, expected_height) self.assertEqual(data1.data.pulse_id, data2.data.pulse_id - 1)
def test_classic_interaction(self): client = PsssProcessingClient("http://localhost:10000/") self.assertEqual(client.get_status(), "stopped") self.assertDictEqual(client.get_parameters(), config.DEFAULT_PARAMETERS) client.set_background() self.assertDictEqual(client.get_parameters(), {"background": ""}) self.assertDictEqual(client.get_statistics(), {}) client.start() self.assertEqual(client.get_status(), "processing") # Wait for PV connection timeout. sleep(1) statistics = client.get_statistics() self.assertEqual(len(statistics), 4) self.assertTrue("processing_start_time" in statistics) self.assertTrue("last_sent_pulse_id" in statistics) self.assertTrue("last_sent_time" in statistics) self.assertTrue("n_processed_images" in statistics) processed_data = [] with source(host="localhost", port=12000, mode=PULL) as input_stream: processed_data.append(input_stream.receive()) statistics = client.get_statistics() self.assertTrue(statistics["n_processed_images"] > 0) # Pulse ids are 0 based. self.assertTrue(statistics["last_sent_pulse_id"] > 0) self.assertTrue("processing_start_time" in statistics) self.assertTrue("last_sent_time" in statistics) self.assertEqual(client.get_status(), "processing") client.stop() self.assertEqual(client.get_status(), "stopped") client.start() self.assertEqual(client.get_status(), "processing") client.stop() self.assertEqual(client.get_status(), "stopped")
def run_tests(n_tests, n_messages, log_level="ERROR"): dispatching_layer_request_times = [] first_message_request_times = [] other_messages_request_times = [] logging.basicConfig(level=log_level) for index_test in range(n_tests): print("Starting test %d/%d" % (index_test+1, n_tests)) start_time = time() with source(channels=channels_to_save, mode=SUB) as input_stream: end_time = time() dispatching_layer_request_times.append(end_time - start_time) print("Connected to dispatching layer.") start_time = time() input_stream.receive() end_time = time() first_message_request_times.append(end_time - start_time) print("First message received.") current_test_receive_times = [] for index_message in range(n_messages): print("Received message %d/%d." % (index_message+1, n_messages)) start_time = time() input_stream.receive() end_time = time() current_test_receive_times.append(end_time - start_time) other_messages_request_times.append(current_test_receive_times) dispatching_layer_avg_time = sum(dispatching_layer_request_times) / len(dispatching_layer_request_times) first_message_avg_time = sum(first_message_request_times) / len(first_message_request_times) other_messages_avg_per_test = [sum(x) / len(x) for x in other_messages_request_times] other_messages_avg_time = sum(other_messages_avg_per_test) / len(other_messages_avg_per_test) print("Test parameters: hostname: %s; n_tests: %d; n_messages: %d;" % (socket.gethostname(), n_tests, n_messages)) print("Dispatching layer average time to connect (seconds): %.3f" % dispatching_layer_avg_time) print("First message average time (seconds): %.3f" % first_message_avg_time) print("Non-first messages average time (seconds): %.3f" % other_messages_avg_time) print("Non-first message frequency (Hz): %.3f" % (1 / other_messages_avg_time))
def test_pipeline_background_manager(self): manager = multiprocessing.Manager() stop_event = multiprocessing.Event() statistics = manager.Namespace() parameter_queue = multiprocessing.Queue() pipeline_config = PipelineConfig("test_pipeline", parameters={ "camera_name": "simulation", "image_background": "full_background", "image_background_enable": True, "image_threshold": 0 }) background_manager = MockBackgroundManager() with self.assertRaises(Exception): processing_pipeline(stop_event, statistics, parameter_queue, self.client, pipeline_config, 12000, background_manager) simulated_camera_shape = (960, 1280) background_array = numpy.zeros(shape=simulated_camera_shape) background_array.fill(99999) background_manager.save_background("full_background", background_array, append_timestamp=False) def send(): processing_pipeline(stop_event, statistics, parameter_queue, self.client, pipeline_config, 12000, background_manager) thread = Thread(target=send) thread.start() with source(host="127.0.0.1", port=12000, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "Received None message.") self.assertTrue( numpy.array_equal(data.data.data["image"].value, numpy.zeros(shape=simulated_camera_shape))) stop_event.set() thread.join()
def test_named_buffered_pipeline(self): instance_id, stream_address = self.pipeline_client.create_instance_from_name( "simulation", additional_config={ "buffer_size": 20, "no_client_timeout": 60, "mode": "PUSH", "queue_size": 1 }) host, port = get_host_port_from_stream_address(stream_address) time.sleep(2.0) with source(host=host, port=port, mode=PULL) as stream: for i in range(10): data = stream.receive() self.assertEqual(data.data.pulse_id, i + 1) self.pipeline_client.stop_all_instances()
def record_images(self, fina, N_images, dsetname="images"): ds = None with h5py.File(fina, "w") as f: with source(host=self._stream_host, port=self._stream_port, mode=SUB) as input_stream: input_stream.connect() for n in range(N_images): image = input_stream.receive().data.data["image"].value if not ds: ds = f.create_dataset(dsetname, dtype=image.dtype, shape=(N_images, ) + image.shape) ds[n, :, :] = image
def get_image(camName, ROI, numImg, angle): pipeline_client = PipelineClient() pipeline_config = {"camera_name": camName, "image_region_of_interest": ROI} pipeline_config = {"camera_name": camName,"rotation":angle} instance_id, pipeline_stream_address = pipeline_client.create_instance_from_config(pipeline_config) pipeline_host, pipeline_port = get_host_port_from_stream_address(pipeline_stream_address) img = [] x_profile = [] y_profile = [] x_fwhm = [] y_fwhm = [] x_center_of_mass = [] y_center_of_mass = [] intensity = [] max_value = [] width = [] height = [] with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: for i in range(0,numImg): data = stream.receive() img.append(data.data.data["image"].value) x_profile.append(data.data.data["x_profile"].value) y_profile.append(data.data.data["y_profile"].value) x_fwhm.append(data.data.data["x_fwhm"].value) y_fwhm.append(data.data.data["y_fwhm"].value) x_center_of_mass.append(data.data.data["x_center_of_mass"].value) intensity.append(data.data.data["intensity"].value) width.append(data.data.data["width"].value) height.append(data.data.data["height"].value) img = np.asarray(img) dataout = { "mean": img.mean(axis=0), "image":img, "x_profile":x_profile, "y_profile":y_profile, "x_fwhm":x_fwhm, "y_fwhm":y_fwhm, "x_center_of_mass":x_center_of_mass, "y_center_of_mass":y_center_of_mass, "intensity":intensity, "max_value":max_value, "width":width, "height":height } return dataout
def buffer_bsread_messages(stream_address, message_buffer, running_event, use_analyzer=False, receive_timeout=1000, mode=PULL): _logger.info("Input stream connecting to '%s'.", stream_address) try: source_host, source_port = stream_address.rsplit(":", maxsplit=1) source_host = source_host.split("//")[1] source_port = int(source_port) _logger.info("Input stream host '%s' and port '%s'.", source_host, source_port) with source(host=source_host, port=source_port, mode=mode, receive_timeout=receive_timeout) as stream: while running_event.is_set(): message = stream.receive() # In case you set a receive timeout, the returned message can be None. if message is None: continue message_timestamp = time() if use_analyzer: analyze_message(message) message_buffer.append((message, message_timestamp)) _logger.debug( 'Message with pulse_id %d and timestamp %s added to the buffer.', message.data.pulse_id, message_timestamp) except Exception as e: running_event.clear() _logger.error("Exception happened in buffer thread. Stopping buffer.", e)
def test_create_pipeline_with_background(self): from cam_server import PipelineClient from cam_server.utils import get_host_port_from_stream_address from bsread import source, SUB # Change to match your pipeline server server_address = "http://0.0.0.0:8889" camera_name = "simulation" # Initialize the client. pipeline_client = PipelineClient(server_address) # Collect the background for the given camera. background_id = pipeline_client.collect_background(camera_name) # Setup the pipeline config. Use the simulation camera as the pipeline source, and the collected background. pipeline_config = { "camera_name": camera_name, "background_id": background_id } # Create a new pipeline with the provided configuration. Stream address in format tcp://hostname:port. instance_id, pipeline_stream_address = pipeline_client.create_instance_from_config( pipeline_config) # Extract the stream hostname and port from the stream address. pipeline_host, pipeline_port = get_host_port_from_stream_address( pipeline_stream_address) # Subscribe to the stream. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: # Receive next message. data = stream.receive() image_width = data.data.data["width"].value image_height = data.data.data["height"].value image_bytes = data.data.data["image"].value print("Image size: %d x %d" % (image_width, image_height)) print("Image data: %s" % image_bytes) x_size, y_size = get_simulated_camera().get_geometry() self.assertIsNotNone(data) self.assertEqual(image_width, x_size) self.assertIsNotNone(image_height, y_size)
def test_change_header(self): with sender(port=self.STREAM_PORT) as output_stream: with source(host="localhost", port=self.STREAM_PORT) as input_stream: for index in range(10): data = {"scalar_source": index, "changing_source": index} output_stream.send(data=data) self.writer.write_message(input_stream.receive(handler=self.handler.receive)) for index in range(10, 20): data = {"scalar_source": index, "changing_source": [index, index, index]} output_stream.send(data=data) self.writer.write_message(input_stream.receive(handler=self.handler.receive)) self.writer.close() file = h5py.File(self.OUTPUT_FILE) scalar_source = file["/data/scalar_source/data"] changing_source_1 = file["/data/changing_source/data(1)"] changing_source_2 = file["/data/changing_source/data"] self.assertIsNotNone(scalar_source) self.assertIsNotNone(changing_source_1) self.assertIsNotNone(changing_source_2) self.assertEqual(len(scalar_source), 20) self.assertEqual(len(changing_source_1), 10) self.assertEqual(len(changing_source_2), 20) self.assertListEqual(list(scalar_source), list(range(20))) self.assertListEqual(list(changing_source_1[:10]), list(range(10))) for index in range(10): self.assertListEqual(list(changing_source_2[index]), [0] * 3) for index in range(10, 20): self.assertListEqual(list(changing_source_2[index]), [index] * 3) file.close()
def stream_receive(): global state try: from bsread import source except ImportError: state = "stopped" logger.info("bsread is not available") return try: with source(channels=[reference, streaked]) as stream: while True: message = stream.receive() data_buffer.append(message.data.data) state = "receiving" except Exception: logger.exception("can not read from stream")
def test_a_quick_start(self): from cam_server import PipelineClient from cam_server.utils import get_host_port_from_stream_address from bsread import source, SUB # Create a pipeline client. client = PipelineClient() # ADDITIONAL, FOR THE TEST camera_name = "simulation" client = self.pipeline_client client.create_instance_from_config( configuration={"camera_name": camera_name}, instance_id=camera_name + "_sp1") # Define the camera name you want to read. This should be the same camera you are streaming in screen panel. camera_name = "simulation" # Format of the instance id when screen_panel creates a pipeline. pipeline_instance_id = camera_name + "_sp1" # Get the stream for the pipelie instance. stream_address = client.get_instance_stream(pipeline_instance_id) # Extract the stream host and port from the stream_address. stream_host, stream_port = get_host_port_from_stream_address( stream_address) # Open connection to the stream. When exiting the 'with' section, the source disconnects by itself. with source(host=stream_host, port=stream_port, mode=SUB) as input_stream: input_stream.connect() # Read one message. message = input_stream.receive() # Print out the received stream data - dictionary. print("Dictionary with data:\n", message.data.data) # Print out the X center of mass. print("X center of mass: ", message.data.data["x_center_of_mass"].value)
def test_received_data_on_send_timeout(self): client = PsenProcessingClient("http://localhost:10000/") client.start() processed_data = [] with source(host="localhost", port=12000, mode=PULL, receive_timeout=1000) as input_stream: self.sending_process.start() sleep(0.5) for index in range(self.n_images): if index == 2: # Wait for the send timeout to happen. sleep(config.DATA_OUTPUT_STREAM_SEND_TIMEOUT + 1) processed_data.append(input_stream.receive()) for index, data in enumerate(processed_data): self.assertEqual(data.data.pulse_id, index)