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_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 test_process_image(self): image = numpy.zeros(shape=(1024, 512), dtype="uint16") image += 1 pv_name_prefix = "JUST_TESTING" axis = numpy.linspace(9100, 9200, 512) roi = [0, 1024] parameters = {"background": ""} processed_data = process_image(image, axis, pv_name_prefix, roi, parameters) self.assertSetEqual(set(processed_data.keys()), {pv_name_prefix + ":processing_parameters", pv_name_prefix + ":SPECTRUM_Y", pv_name_prefix + ":SPECTRUM_X", pv_name_prefix + ":SPECTRUM_CENTER", pv_name_prefix + ":SPECTRUM_FWHM", pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE}) # Original image should not be manipulated self.assertEqual(image.shape, (1024, 512)) self.assertEqual(len(processed_data[pv_name_prefix + ":SPECTRUM_Y"]), 512) processing_parameters = json.loads(processed_data[pv_name_prefix + ":processing_parameters"])
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 test_stream_processor(self): pv_name_prefix = "JUST_TESTING" n_images = 5 original_roi_signal = [0, 1024, 0, 1024] original_roi_background = [0, 100, 0, 100] image = numpy.zeros(shape=(1024, 1024), dtype="uint16") image += 1 data_to_send = {pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE: image} def send_data(): sleep(1) with sender(port=10000) 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) stream_processor(event, original_roi_signal, original_roi_background, {}) running_event = Event() send_process = Process(target=send_data) processing_process = Process(target=process_data, args=(running_event, )) send_process.start() processing_process.start() data_received = [] with source(host="localhost", port=11000, mode=PULL, receive_timeout=2000) as input_stream: for _ in range(n_images): data_received.append(input_stream.receive()) running_event.clear() sleep(0.2) send_process.terminate() processing_process.terminate() self.assertEqual(len(data_received), n_images) parameters = data_received[0].data.data[pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".processing_parameters"].value processing_parameters = json.loads(parameters) self.assertEqual(processing_parameters["roi_signal"], original_roi_signal) self.assertEqual(processing_parameters["roi_background"], original_roi_background) roi_signal = data_received[0].data.data[pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".roi_signal_x_profile"].value self.assertEqual(len(roi_signal), 1024) self.assertListEqual(list(roi_signal), [1024] * 1024) roi_background = data_received[0].data.data[ pv_name_prefix + config.EPICS_PV_SUFFIX_IMAGE + ".roi_background_x_profile"].value self.assertEqual(len(roi_background), 100) self.assertListEqual(list(roi_background), [100] * 100)