Beispiel #1
0
    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()
Beispiel #2
0
    def test_manager(self):
        #Creating instances from config
        instance_id_1, instance_stream_1 = self.pipeline_client.create_instance_from_name(
            "simulation_sp")
        pipeline_host_1, pipeline_port_1 = get_host_port_from_stream_address(
            instance_stream_1)
        self.assertEqual(pipeline_port_1, 10123)

        self.pipeline_client.stop_instance(instance_id_1)

        instance_id_2, instance_stream_2 = self.pipeline_client.create_instance_from_name(
            "simulation_sp")
        pipeline_host_2, pipeline_port_2 = get_host_port_from_stream_address(
            instance_stream_2)
        self.assertEqual(pipeline_host_1, pipeline_host_2)
        self.assertEqual(pipeline_port_1, pipeline_port_2)
Beispiel #3
0
    def get_stream(self, timeout=config.ZMQ_RECEIVE_TIMEOUT, data_change_callback=None):
        source_host, source_port = get_host_port_from_stream_address(self.bsread_stream_address)

        self.bsread_source = Source(host=source_host, port=source_port, mode=PULL,
                                    receive_timeout=timeout)
        self.bsread_source.handler = Handler(data_change_callback)
        return self.bsread_source
Beispiel #4
0
    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
Beispiel #5
0
    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)
Beispiel #6
0
def connect_to_stream():
    global source
    pars = get_parameters()
    input_stream = pars.get("input_stream")
    if input_stream:
        bsread_address = input_stream
        bsread_channels = None
    else:
        bsread_address = pars.get("bsread_address")
        bsread_channels = pars.get("bsread_channels")

    default_input_mode = "SUB"
    conn_type = CONNECT
    if pars.get("pipeline_type", "") in [
            "fanin",
    ]:
        conn_type = BIND
        default_input_mode = "PULL"

    bsread_mode = pars.get("input_mode", default_input_mode)

    _logger.debug("Connecting to stream %s. %s" %
                  (str(bsread_address), str(bsread_channels)))

    receive_timeout = int(
        pars.get("receive_timeout", config.PIPELINE_RECEIVE_TIMEOUT))

    dispatcher_url, dispatcher_verify_request, dispatcher_disable_compression = get_dispatcher_parameters(
        pars)

    if bsread_address:
        bsread_host, bsread_port = get_host_port_from_stream_address(
            bsread_address)
        bsread_mode = SUB if bsread_mode == "SUB" else PULL
    else:
        bsread_host, bsread_port = None, 9999
        bsread_mode = PULL if bsread_mode == "PULL" else SUB

    if bsread_channels is not None:
        if type(bsread_channels) != list:
            bsread_channels = json.loads(bsread_channels)
        if len(bsread_channels) == 0:
            bsread_channels = None

    ret = bssource(
        host=bsread_host,
        conn_type=conn_type,
        port=bsread_port,
        mode=bsread_mode,
        channels=bsread_channels,
        receive_timeout=receive_timeout,
        dispatcher_url=dispatcher_url,
        dispatcher_verify_request=dispatcher_verify_request,
        dispatcher_disable_compression=dispatcher_disable_compression)
    if not is_camera_source():
        source = ret.source
    return ret
Beispiel #7
0
def create_sender(output_stream_port, stop_event):
    global sender, pid_buffer, pid_buffer_size
    sender = None
    pars = get_parameters()

    def no_client_action():
        global sender
        nonlocal pars
        if pars["no_client_timeout"] > 0:
            _logger.warning(
                "No client connected to the pipeline stream for %d seconds. Closing instance. %s",
                pars["no_client_timeout"], log_tag)
            stop_event.set()
            if sender:
                if pars["mode"] == "PUSH" and pars["block"]:
                    _logger.warning(
                        "Killing the process: cannot stop gracefully if sender is blocking"
                    )
                    os._exit(0)

    if pars["mode"] == "FILE":
        file_name = pars["file"]
        records = pars.get("records")
        sender = WriterSender(output_file=file_name,
                              number_of_records=records
                              if records else UNDEFINED_NUMBER_OF_RECORDS,
                              layout=pars["layout"],
                              save_local_timestamps=pars["localtime"],
                              change=pars["change"],
                              attributes={})
    else:
        output_stream = pars.get("output_stream", None)
        address = "tcp://*"
        connect_type = BIND
        if output_stream:
            connect_type = CONNECT
            address, output_stream_port = get_host_port_from_stream_address(
                output_stream)
            address = "tcp://" + address

        sender = Sender(port=output_stream_port,
                        address=address,
                        conn_type=connect_type,
                        mode=PUSH if (pars["mode"] == "PUSH") else PUB,
                        queue_size=pars["queue_size"],
                        block=pars["block"],
                        data_header_compression=config.
                        CAMERA_BSREAD_DATA_HEADER_COMPRESSION)
    sender.open(no_client_action=no_client_action,
                no_client_timeout=pars["no_client_timeout"]
                if pars["no_client_timeout"] > 0 else sys.maxsize)
    init_sender(sender, pars)
    if pars.get("pid_buffer", 0) > 1:
        pid_buffer_size = pars.get("pid_buffer")
        pid_buffer = {}
    return sender
    def get_running_camserver(self, camera, status):
        servers = []
        load = self.get_load(status)
        loads = []
        try:
            instances = self.cam_server_client.get_server_info(
                timeout=self.update_timeout if self.update_timeout else config.
                DEFAULT_SERVER_INFO_TIMEOUT)["active_instances"]
            host, port = get_host_port_from_stream_address(
                instances[camera]["host"])
            if not host.count(".") == 3:  #If not IP, get only prefix
                host = host.split(".")[0].lower()
                local_names = ["127.0.0.1" "localhost"]
                local_host_name = socket.gethostname()
                if local_host_name:
                    local_names.append(local_host_name.split(".")[0].lower())

            for i in range(len(self.server_pool)):
                if load[i] < 1000:
                    try:
                        server_host, server_port = get_host_port_from_stream_address(
                            self.server_pool[i].get_address())
                        if not host.count(
                                ".") == 3:  # If not IP, get only prefix
                            server_host = server_host.split(
                                ".")[0].lower() if server_host else None
                        if host == server_host:
                            servers.append(self.server_pool[i])
                            loads.append(load[i])
                        if (host in local_names) and (server_host
                                                      in local_names):
                            servers.append(self.server_pool[i])
                            loads.append(load[i])
                    except:
                        pass
        except:
            pass
        if len(servers) > 0:
            m = min(loads)
            return servers[loads.index(m)]

        return None
Beispiel #9
0
    def get_stream(self):

        self.verify_camera_online()
        self._collect_camera_settings()

        source_host, source_port = get_host_port_from_stream_address(self.bsread_stream_address)

        self.bsread_source = Source(host=source_host, port=source_port, mode=PULL,
                                    receive_timeout=config.ZMQ_RECEIVE_TIMEOUT)

        return self.bsread_source
Beispiel #10
0
def get_images(screen, n_images, beamline='Aramis', dry_run=None):

    print('Start get_images for screen %s, %i images, beamline %s' %
          (screen, n_images, beamline))

    meta_dict_1 = get_meta_data(screen)

    positioner = pyscan.BsreadPositioner(n_messages=n_images)
    settings = pyscan.scan_settings(settling_time=0.01,
                                    measurement_interval=0.2,
                                    n_measurements=1)

    pipeline_client = PipelineClient("http://sf-daqsync-01:8889/")
    cam_instance_name = str(screen) + "_sp1"
    stream_address = pipeline_client.get_instance_stream(cam_instance_name)
    stream_host, stream_port = get_host_port_from_stream_address(
        stream_address)

    # Configure bsread
    pyscan.config.bs_default_host = stream_host
    pyscan.config.bs_default_port = stream_port

    logging.getLogger("mflow.mflow").setLevel(logging.ERROR)

    readables = get_readables(beamline)

    raw_output = pyscan.scan(positioner=positioner,
                             readables=readables,
                             settings=settings)
    output = [[x] for x in raw_output]

    result_dict = pyscan_result_to_dict(readables, output, scrap_bs=True)

    for ax in ['x_axis', 'y_axis']:
        arr = result_dict[ax] * 1e-6  # convert to m
        if len(arr.shape) == 3:
            result_dict[ax + '_m'] = arr[0, 0, :]
        elif len(arr.shape) == 2:
            result_dict[ax + '_m'] = arr[0, :]
        else:
            raise ValueError('Unexpected', len(arr.shape))

    meta_dict_2 = get_meta_data(screen)

    output_dict = {
        'pyscan_result': result_dict,
        'meta_data_begin': meta_dict_1,
        'meta_data_end': meta_dict_2,
    }

    print('End get_images')

    return output_dict
Beispiel #11
0
def create_source(camera_stream_address,
                  receive_timeout=config.PIPELINE_RECEIVE_TIMEOUT,
                  mode=SUB):
    source_host, source_port = get_host_port_from_stream_address(
        camera_stream_address)
    if camera_stream_address.startswith("ipc"):
        return IpcSource(address=camera_stream_address,
                         receive_timeout=receive_timeout,
                         mode=mode)
    else:
        return Source(host=source_host,
                      port=source_port,
                      receive_timeout=receive_timeout,
                      mode=mode)
Beispiel #12
0
def resolve_camera_source(cam_client, pars=None):
    if pars is None:
        pars = get_parameters()
    source_mode = SUB
    input_stream = pars.get("input_stream")
    if input_stream:
        camera_stream_address = input_stream
        source_mode = PULL if pars.get("input_mode", "SUB") == "PULL" else SUB
    else:
        camera_stream_address = cam_client.get_instance_stream(
            pars.get("camera_name"))
    source_host, source_port = get_host_port_from_stream_address(
        camera_stream_address)
    return camera_stream_address, source_host, source_port, source_mode
    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 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()
Beispiel #15
0
    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)
Beispiel #16
0
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
Beispiel #17
0
        def _collect_camera_settings(self):
            try:
                #self.width_raw, self.height_raw  = 659, 494
                source_host, source_port = get_host_port_from_stream_address(self.bsread_stream_address)

                stream = Source(host=source_host, port=source_port, mode=PULL,receive_timeout=3000)
                stream.connect()
                data = stream.receive()
                image = data.data.data[self.camera_config.get_source() + config.EPICS_PV_SUFFIX_IMAGE].value
                if image is None:
                    self.height_raw, self.width_raw = 0,0
                else:
                    image = transform_image(image, self.camera_config)
                    self.height_raw, self.width_raw = image.shape
            except:
                raise RuntimeError("Could not fetch camera settings cam_server:{}".format(self.camera_config.get_source()))
            finally:
                stream.disconnect()
Beispiel #18
0
    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)
Beispiel #19
0
    def get_instance_message(self, instance_id):
        """
        Get a single message from a stream instance.
        :param instance_id: Instance id of the stream.
        :return: Message from the stream.
        """
        instance_address = self.get_instance_stream(instance_id)

        instance_config = self.get_instance_config(instance_id)
        pipeline_type = instance_config["pipeline_type"]

        if pipeline_type != "processing":
            raise ValueError("Cannot get message from '%s' pipeline type." %
                             pipeline_type)

        host, port = get_host_port_from_stream_address(instance_address)

        with source(host=host, port=port, mode=SUB) as stream:
            message = stream.receive()

        return message
Beispiel #20
0
    def start(self, stream, stream_mode=SUB):
        self.stream = stream
        try:
            stream_host, stream_port = get_host_port_from_stream_address(
                stream)
            with source(host=stream_host, port=stream_port,
                        mode=stream_mode) as stream:
                while True:
                    if (self.number_of_records >= 0) and (
                            self.current_record >= self.number_of_records):
                        break
                    rec = stream.receive()

                    pulse_id, data, format_changed, global_timestamp, global_timestamp_offset = \
                        rec.data.pulse_id, rec.data.data, rec.data.format_changed, rec.data.global_timestamp, \
                        rec.data.global_timestamp_offset
                    self.add_record(pulse_id, data, format_changed,
                                    global_timestamp, global_timestamp_offset)

        finally:
            self.close()
Beispiel #21
0
def get_axis(screen):
    camera_client = CamClient()
    camera_stream_adress = camera_client.get_instance_stream(screen)
    host, port = get_host_port_from_stream_address(camera_stream_adress)

    max_tries = 3
    for _try in range(max_tries):
        try:
            with source(host=host, port=port, mode=SUB) as stream:
                data = stream.receive()
                print('Received image')

                x_axis = np.array(data.data.data['x_axis'].value) * 1e-6
                y_axis = np.array(data.data.data['y_axis'].value) * 1e-6
        except Exception as e:
            print('Try %i' % _try, e)
            pass
        else:
            break
    else:
        raise

    return x_axis, y_axis
Beispiel #22
0
    def get_request_server(self, status=None):
        try:
            servers = []
            load = self.get_load(status)
            loads = []
            server_name, server_prefix, server_address = self.get_source()

            for i in range(len(self.server_pool)):
                if load[i] < 1000:
                    name = self.server_pool[i].get_address()
                    host, port = get_host_port_from_stream_address(name)
                    host_prefix = host.split(".")[0].lower() if host else None
                    local_host_name = socket.gethostname()
                    local_host_prefix = local_host_name.split(
                        ".")[0].lower() if local_host_name else None

                    if server_address == "127.0.0.1":
                        if (host == "127.0.0.1") or (host_prefix
                                                     in (local_host_prefix,
                                                         "localhost")):
                            servers.append(self.server_pool[i])
                            loads.append(load[i])
                    elif server_prefix == host_prefix:
                        servers.append(self.server_pool[i])
                        loads.append(load[i])
                    elif server_address == host:
                        servers.append(self.server_pool[i])
                        loads.append(load[i])

            #Balancing between servers in same machine to pass manager test
            if len(servers) > 0:
                m = min(loads)
                return servers[loads.index(m)]
        except Exception as e:
            _logger.warning('Failed to identify request origin: ' + str(e))
        return None
Beispiel #23
0
def store_pipeline(stop_event, statistics, parameter_queue, cam_client,
                   pipeline_config, output_stream_port, background_manager):
    # TODO: Implement statistics: n_clients, input_throughput

    def no_client_timeout():
        _logger.warning(
            "No client connected to the pipeline stream for %d seconds. Closing instance."
            % config.MFLOW_NO_CLIENTS_TIMEOUT)
        stop_event.set()

    source = None
    sender = None

    try:

        camera_stream_address = cam_client.get_camera_stream(
            pipeline_config.get_camera_name())
        camera_name = pipeline_config.get_camera_name()
        _logger.debug("Connecting to camera %s on stream address %s.",
                      camera_name, camera_stream_address)

        source_host, source_port = get_host_port_from_stream_address(
            camera_stream_address)

        source = Source(host=source_host,
                        port=source_port,
                        receive_timeout=config.PIPELINE_RECEIVE_TIMEOUT,
                        mode=SUB)

        source.connect()

        _logger.debug("Opening output stream on port %d.", output_stream_port)

        sender = Sender(port=output_stream_port,
                        mode=PUSH,
                        data_header_compression=config.
                        CAMERA_BSREAD_DATA_HEADER_COMPRESSION,
                        block=False)

        sender.open(no_client_action=no_client_timeout,
                    no_client_timeout=config.MFLOW_NO_CLIENTS_TIMEOUT)
        # TODO: Register proper channels.

        # Indicate that the startup was successful.
        stop_event.clear()

        _logger.debug("Transceiver started.")

        while not stop_event.is_set():
            try:

                data = source.receive()

                # In case of receiving error or timeout, the returned data is None.
                if data is None:
                    continue

                forward_data = {camera_name: data.data.data["image"].value}

                pulse_id = data.data.pulse_id
                timestamp = (data.data.global_timestamp,
                             data.data.global_timestamp_offset)

                sender.send(data=forward_data,
                            pulse_id=pulse_id,
                            timestamp=timestamp)

            except:
                _logger.exception("Could not process message.")
                stop_event.set()

        _logger.info("Stopping transceiver.")

    except:
        _logger.exception(
            "Exception while trying to start the receive and process thread.")
        raise

    finally:
        if source:
            source.disconnect()

        if sender:
            sender.close()
    def test_client(self):
        expected_pipelines = [
            "pipeline_example_1", "pipeline_example_2", "pipeline_example_3",
            "pipeline_example_4"
        ]
        for pipeline in expected_pipelines:
            self.assertIn(pipeline, self.pipeline_client.get_pipelines(),
                          "Test config pipelines have changed?")
        expected_pipelines = set(self.pipeline_client.get_pipelines())

        camera_config = self.pipeline_client.get_pipeline_config(
            "pipeline_example_4")
        self.pipeline_client.save_pipeline_config("testing_config",
                                                  camera_config)

        with self.assertRaisesRegex(
                ValueError, "Camera name not specified in configuration."):
            self.pipeline_client.save_pipeline_config("testing_config", {})

        with self.assertRaisesRegex(
                ValueError, "Camera name not specified in configuration."):
            self.pipeline_client.save_pipeline_config("testing_config",
                                                      {"invalid": "config"})

        with self.assertRaisesRegex(
                ValueError,
                "pipeline_type 'invalid' not present in mapping. Available"):
            self.pipeline_client.save_pipeline_config(
                "testing_config", {
                    "camera_name": "simulation",
                    "pipeline_type": "invalid"
                })

        expected_pipelines.add("testing_config")

        self.assertSetEqual(set(self.pipeline_client.get_pipelines()),
                            expected_pipelines,
                            "Testing config was not added.")

        stream_address_1 = self.pipeline_client.get_instance_stream(
            "testing_config")
        stream_address_2 = self.pipeline_client.get_instance_stream(
            "testing_config")
        self.assertEqual(
            len(self.pipeline_client.get_server_info()["active_instances"]), 1,
            "Instance not started or too many instances started.")

        with self.assertRaisesRegex(ValueError,
                                    "Config updates cannot be empty."):
            self.pipeline_client.set_instance_config("testing_config", {})

        with self.assertRaisesRegex(
                ValueError, "Cannot set config on a read only instance."):
            self.pipeline_client.set_instance_config(
                "testing_config", {"camera_name": "simulation"})

        self.assertEqual(stream_address_1, stream_address_2,
                         "Stream addresses should be equal.")

        self.pipeline_client.stop_instance("testing_config")

        self.assertEqual(
            len(self.pipeline_client.get_server_info()["active_instances"]), 0,
            "The instance should be stopped.")

        instance_id_1, instance_stream_1 = self.pipeline_client.create_instance_from_name(
            "testing_config")
        instance_id_2, instance_stream_2 = self.pipeline_client.create_instance_from_name(
            "testing_config")

        instance_info = self.pipeline_client.get_instance_info(instance_id_1)
        self.assertTrue("last_start_time" in instance_info)
        self.assertTrue("statistics" in instance_info)

        with self.assertRaisesRegex(
                ValueError,
                "Cannot change the camera name on a running instance. "
                "Stop the instance first."):
            self.pipeline_client.set_instance_config(
                instance_id_1, {"camera_name": "different_camera"})

        self.pipeline_client.set_instance_config(
            instance_id_2, {
                "image_threshold": 99999,
                "image_region_of_interest": [0, 200, 0, 200]
            })

        # Wait for the config update.
        sleep(0.5)

        pipeline_host, pipeline_port = get_host_port_from_stream_address(
            instance_stream_2)

        with source(host=pipeline_host, port=pipeline_port,
                    mode=SUB) as stream:
            data = stream.receive()
            self.assertIsNotNone(data,
                                 "This should really not happen anymore.")
            # shape 200, 200 -> Account for the region of interest change.
            self.assertTrue(
                numpy.array_equal(data.data.data["image"].value,
                                  numpy.zeros(shape=(200, 200))),
                "Array should be all zeros, because of the threshold config.")

            # Adjust width and height with the region of interest.
            self.assertEqual(data.data.data["width"].value, 200,
                             "Region of interest not takes into account.")
            self.assertEqual(data.data.data["height"].value, 200,
                             "Region of interest not takes into account.")

        self.assertNotEqual(instance_id_1, instance_id_2,
                            "Instances should be different.")
        self.assertNotEqual(instance_stream_1, instance_stream_2,
                            "Stream addresses should be different.")

        self.assertEqual(
            len(self.pipeline_client.get_server_info()["active_instances"]), 2,
            "Two instances should be running.")

        instance_stream_3 = self.pipeline_client.get_instance_stream(
            instance_id_2)
        self.assertEqual(instance_stream_2, instance_stream_3,
                         "Stream addresses should be equal.")

        self.assertEqual(
            len(self.pipeline_client.get_server_info()["active_instances"]), 2,
            "Two instances should be running, get does not increase the number of instances."
        )

        self.pipeline_client.create_instance_from_config(
            {"camera_name": "simulation"})
        self.pipeline_client.create_instance_from_config(
            {"camera_name": "simulation"})

        self.assertEqual(
            len(self.pipeline_client.get_server_info()["active_instances"]), 4,
            "Two new instances should be created.")

        with self.assertRaisesRegex(
                ValueError, "You must specify either the pipeline name or the "
                "configuration for the pipeline."):
            self.pipeline_client.create_instance_from_config({})

        with self.assertRaisesRegex(
                ValueError, "Camera name not specified in configuration."):
            self.pipeline_client.create_instance_from_config(
                {"invalid": "config"})

        background_id = self.pipeline_client.collect_background("simulation")
        expected_background_file = os.path.join(self.background_config_folder,
                                                background_id + ".npy")

        # Unfortunately there is not way of knowing (from client side) how many images were collected.
        background_id_2 = self.pipeline_client.collect_background(
            "simulation", 5)

        self.assertNotEqual(background_id, background_id_2,
                            "Background should be different.")

        with self.assertRaisesRegex(Exception, "n_images must be a number."):
            self.pipeline_client.collect_background("simulation",
                                                    "invalid_number")

        pipeline_config = {
            "camera_name": "simulation",
            "background_image": background_id
        }

        instance_id, stream_address = self.pipeline_client.create_instance_from_config(
            pipeline_config)

        instance_config = self.pipeline_client.get_instance_config(instance_id)

        # We need to account for the expended config.
        expected_config = {}
        expected_config.update(PipelineConfig.DEFAULT_CONFIGURATION)
        expected_config.update(pipeline_config)

        self.assertDictEqual(expected_config, instance_config,
                             "Set and retrieved instances are not equal.")

        instance_info = self.pipeline_client.get_instance_info(instance_id)
        self.assertEqual(instance_info["instance_id"], instance_id,
                         "Requested and retireved instances are different.")
        self.assertEqual(instance_info["stream_address"], stream_address,
                         "Different stream address.")
        self.assertTrue(instance_info["is_stream_active"],
                        "Stream should be active.")
        self.assertFalse(instance_info["read_only"],
                         "It should not be read only.")
        self.assertEqual(instance_info["camera_name"], "simulation",
                         "Wrong camera name.")
        self.assertDictEqual(instance_info["config"], expected_config,
                             "Config is not equal")

        self.pipeline_client.stop_instance(instance_id)

        with self.assertRaisesRegex(
                ValueError, "Instance '%s' does not exist." % instance_id):
            self.pipeline_client.get_instance_info(instance_id)

        with self.assertRaisesRegex(
                ValueError, "Instance 'invalid instance' does not exist."):
            self.pipeline_client.get_instance_info("invalid instance")

        self.assertTrue(os.path.exists(expected_background_file))
        os.remove(expected_background_file)

        self.pipeline_client.stop_all_instances()

        self.assertTrue(
            "testing_config" in self.pipeline_client.get_pipelines(),
            "Pre requirement for next test.")

        self.pipeline_client.delete_pipeline_config("testing_config")
        self.assertFalse(
            "testing_config" in self.pipeline_client.get_pipelines(),
            "Pipeline should not exist anymore.")

        instance_id, stream_address = self.pipeline_client.create_instance_from_config(
            {
                "camera_name": "simulation",
                "image_threshold": 10
            }, "custom_instance")

        self.assertEqual(instance_id, "custom_instance",
                         "Custom instance not set properly.")

        self.assertTrue(
            self.pipeline_client.is_instance_running("custom_instance"),
            "Instance with custom instance id not present.")

        self.assertEqual(
            self.pipeline_client.get_instance_config("custom_instance")
            ["image_threshold"], 10, "Config not set on custom instance id.")

        with self.assertRaisesRegex(
                ValueError,
                "Instance with id 'custom_instance' is already present and running. "
                "Use another instance_id or stop the current instance if you want "
                "to reuse the same instance_id."):
            self.pipeline_client.create_instance_from_config(
                {"camera_name": "simulation"}, "custom_instance")

        self.pipeline_client.stop_instance("custom_instance")

        # The instance is now stopped, it should overwrite it.
        self.pipeline_client.create_instance_from_config(
            {
                "camera_name": "simulation",
                "image_threshold": 20
            }, "custom_instance")

        self.assertEqual(
            self.pipeline_client.get_instance_config(
                "custom_instance")["image_threshold"], 20,
            "Instance with custom id not overwritten.")

        self.pipeline_client.save_pipeline_config("testing_config", {
            "camera_name": "simulation",
            "image_threshold": 30
        })

        with self.assertRaisesRegex(
                ValueError,
                "Instance with id 'custom_instance' is already present and running. "
                "Use another instance_id or stop the current instance if you want "
                "to reuse the same instance_id."):
            self.pipeline_client.create_instance_from_name(
                "testing_config", "custom_instance")

        self.assertEqual(
            self.pipeline_client.get_instance_config("custom_instance")
            ["image_threshold"], 20, "Instance should not have changed.")

        data = self.pipeline_client.get_instance_message("custom_instance")
        self.assertIsNotNone(data)
        self.assertTrue("image" in data.data.data)

        self.pipeline_client.stop_instance("custom_instance")

        self.pipeline_client.create_instance_from_name("testing_config",
                                                       "custom_instance")

        self.assertEqual(
            self.pipeline_client.get_instance_config("custom_instance")
            ["image_threshold"], 30, "Instance should have changed.")

        background_1 = self.pipeline_client.collect_background("simulation")
        background_2 = self.pipeline_client.collect_background("simulation")

        self.assertNotEqual(background_1, background_2)

        latest_background = self.pipeline_client.get_latest_background(
            "simulation")

        self.assertEqual(latest_background, background_2,
                         "Wrong background set as latest.")

        with self.assertRaisesRegex(
                ValueError,
                "No background matches for the specified prefix 'does not exist'."
        ):
            self.pipeline_client.get_latest_background("does not exist")

        expected_cameras = [
            'camera_example_1', 'camera_example_3', 'camera_example_2',
            'camera_example_4', 'simulation', 'simulation2'
        ]

        #self.assertEqual(set(self.pipeline_client.get_cameras()), set(expected_cameras),
        #                 "Expected cameras not present.")
        for camera in expected_cameras:
            self.assertIn(camera, set(self.pipeline_client.get_cameras()),
                          "Not getting expected camera: " + camera)

        configuration = {"camera_name": "simulation", "threshold": 50}

        stream_address_1 = self.pipeline_client.get_instance_stream_from_config(
            configuration)
        stream_address_2 = self.pipeline_client.get_instance_stream_from_config(
            configuration)

        self.assertEqual(
            stream_address_1, stream_address_2,
            "Requesting the same config should give you the same instance.")

        self.pipeline_client.stop_all_instances()
        self.cam_client.stop_all_instances()

        instance_id, stream_address = self.pipeline_client.create_instance_from_config(
            {
                "camera_name": "simulation",
                "pipeline_type": "store",
                'stream_port': 10000
            })

        with self.assertRaisesRegex(
                ValueError, "Cannot get message from 'store' pipeline type."):
            self.pipeline_client.get_instance_message(instance_id)

        host, port = get_host_port_from_stream_address(stream_address)

        with source(host=host, port=port, mode=PULL) as stream:
            data = stream.receive()

        self.assertIsNotNone(data)
        self.assertEqual(
            len(data.data.data), 1,
            "Only the image should be present in the received data.")
        self.assertTrue(
            "simulation" + config.EPICS_PV_SUFFIX_IMAGE in data.data.data,
            "Camera name should be used instead of 'image'.")

        self.pipeline_client.stop_all_instances()

        #Transparent pipeline
        instance_id, instance_stream = self.pipeline_client.create_instance_from_name(
            "simulation")
        cfg = self.pipeline_client.get_instance_config(instance_id)
        cfg["function"] = "transparent"
        self.pipeline_client.set_instance_config(instance_id, cfg)
        time.sleep(.5)
        data = self.pipeline_client.get_instance_message(instance_id)
        self.assertIsNotNone(data)
        # Cam_server fields + processing_parameters
        required_fields = set([
            "image", "timestamp", "width", "height", "x_axis", "y_axis",
            "processing_parameters"
        ])
        self.assertSetEqual(required_fields, set(data.data.data.keys()),
                            "Bad transparent pipeline fields.")

        self.pipeline_client.stop_all_instances()
        time.sleep(1.0)
    def test_update_instance_config_with_running(self):
        instance_manager = get_test_pipeline_manager_with_real_cam()
        instance_manager.background_manager = BackgroundImageManager(
            self.background_folder)

        pipeline_config = {"camera_name": "simulation"}

        black_image = numpy.zeros(shape=(960, 1280))

        pipeline_id, pipeline_stream_address = instance_manager.create_pipeline(
            configuration=pipeline_config)
        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:
            normal_image_data = stream.receive()
            self.assertIsNotNone(normal_image_data,
                                 "This should really not happen anymore.")
            self.assertFalse(
                numpy.array_equal(normal_image_data.data.data['image'].value,
                                  black_image),
                "There should be a non black image.")

            self.assertEqual(normal_image_data.data.data["width"].value, 1280,
                             "Incorrect width.")
            self.assertEqual(normal_image_data.data.data["height"].value, 960,
                             "Incorrect height.")

        white_image = numpy.zeros(shape=(960, 1280))
        white_image.fill(99999)

        instance_manager.background_manager.save_background(
            "white_background", white_image, append_timestamp=False)

        instance_manager.update_instance_config(
            pipeline_id, {"image_background": "white_background"})

        # Give it some time to load the background.
        sleep(0.5)

        # Collect from the camera.
        with source(host=pipeline_host, port=pipeline_port,
                    mode=SUB) as stream:
            image_data = stream.receive()
            self.assertIsNotNone(image_data,
                                 "This should really not happen anymore.")

        # Because we didn't set the "image_background_enable" yet.
        self.assertFalse(
            numpy.array_equal(image_data.data.data["image"].value,
                              black_image),
            "Background should not be yet applied.")

        instance_manager.update_instance_config(pipeline_id, {
            "image_background_enable": True,
            "image_threshold": 0
        })

        # Give it some time to load the background.
        sleep(0.5)

        # Collect from the camera.
        with source(host=pipeline_host, port=pipeline_port,
                    mode=SUB) as stream:
            black_image_data = stream.receive()
            self.assertIsNotNone(black_image_data,
                                 "This should really not happen anymore.")

        self.assertTrue(
            numpy.array_equal(black_image_data.data.data["image"].value,
                              black_image), "Now background should work.")

        instance_manager.stop_all_instances()
Beispiel #26
0
    def test_update_instance_config_with_running(self):
        instance_manager = get_test_pipeline_manager_with_real_cam()
        instance_manager.background_manager = BackgroundImageManager(
            self.background_folder)

        pipeline_config = {"camera_name": "simulation"}

        x_size, y_size = get_simulated_camera().get_geometry()
        black_image = numpy.zeros(shape=(y_size,
                                         x_size)).astype(dtype="uint16")

        pipeline_id, pipeline_stream_address = instance_manager.create_pipeline(
            configuration=pipeline_config)
        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:
            normal_image_data = stream.receive()
            self.assertIsNotNone(normal_image_data,
                                 "This should really not happen anymore.")
            self.assertFalse(
                numpy.array_equal(normal_image_data.data.data['image'].value,
                                  black_image),
                "There should be a non black image.")

            self.assertEqual(normal_image_data.data.data["width"].value,
                             x_size, "Incorrect width.")
            self.assertEqual(normal_image_data.data.data["height"].value,
                             y_size, "Incorrect height.")

        white_image = numpy.zeros(shape=(y_size, x_size))
        white_image.fill(65535)

        instance_manager.background_manager.save_background(
            "white_background", white_image, append_timestamp=False)

        instance_manager.update_instance_config(
            pipeline_id, {"image_background": "white_background"})

        # Give it some time to load the background.
        sleep(0.5)

        # Collect from the camera.
        with source(host=pipeline_host, port=pipeline_port,
                    mode=SUB) as stream:
            image_data = stream.receive()
            self.assertIsNotNone(image_data,
                                 "This should really not happen anymore.")

        # Because we didn't set the "image_background_enable" yet.
        self.assertFalse(
            numpy.array_equal(image_data.data.data["image"].value,
                              black_image),
            "Background should not be yet applied.")

        instance_manager.update_instance_config(pipeline_id, {
            "image_background_enable": True,
            "image_threshold": 0
        })

        # Give it some time to load the background.
        sleep(1.0)

        # Collect from the camera.
        with source(host=pipeline_host, port=pipeline_port,
                    mode=SUB) as stream:
            black_image_data = stream.receive()
            print(pipeline_host, pipeline_port)
            self.assertIsNotNone(black_image_data,
                                 "This should really not happen anymore.")

        print("---")
        print(black_image_data.data.data["image"].value.shape,
              black_image_data.data.data["image"].value.dtype)
        print(black_image.shape, black_image.dtype)
        black_image.astype(dtype="uint16")
        for i in range(len(black_image)):
            for j in range(len(black_image[0])):
                if black_image_data.data.data["image"].value[i][
                        j] != black_image[i][j]:
                    print(i, j,
                          black_image_data.data.data["image"].value[i][j],
                          black_image[i][j])
        self.assertTrue(
            numpy.array_equal(black_image_data.data.data["image"].value,
                              black_image), "Now background should work.")

        instance_manager.stop_all_instances()
    def test_camera_settings_change(self):
        stream_address = self.instance_manager.get_instance_stream(
            "simulation")
        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        sim_x, sim_y = simulated_camera.get_geometry()

        camera_host, camera_port = get_host_port_from_stream_address(
            stream_address)

        # Collect from the pipeline.
        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value

            self.assertEqual(x_size, sim_x)
            self.assertEqual(y_size, sim_y)

            x_axis_1 = data.data.data["x_axis"].value
            y_axis_1 = data.data.data["y_axis"].value

            self.assertEqual(x_axis_1.shape[0], sim_x)
            self.assertEqual(y_axis_1.shape[0], sim_y)

        new_config = update_camera_config(
            self.instance_manager.get_instance(
                "simulation").get_configuration(), {"rotate": 1})
        new_config = CameraConfig("simulation", new_config).get_configuration()
        self.instance_manager.set_camera_instance_config(
            "simulation", new_config)

        sleep(0.5)

        # Collect from the pipeline.
        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value

            # We rotate the image for 90 degrees - X and Y size should be inverted.
            self.assertEqual(x_size, sim_y)
            self.assertEqual(y_size, sim_x)

            x_axis_2 = data.data.data["x_axis"].value
            y_axis_2 = data.data.data["y_axis"].value

            # We rotate the image for 90 degrees - X and Y size should be inverted.
            self.assertEqual(x_axis_2.shape[0], sim_y)
            self.assertEqual(y_axis_2.shape[0], sim_x)

        # The axis should just be switched.
        self.assertTrue(numpy.array_equal(x_axis_1, y_axis_2))
        self.assertTrue(numpy.array_equal(y_axis_1, x_axis_2))

        new_config = update_camera_config(
            self.instance_manager.get_instance(
                "simulation").get_configuration(), {"camera_calibration": {}})
        new_config = CameraConfig("simulation", new_config).get_configuration()
        self.instance_manager.set_camera_instance_config(
            "simulation", new_config)

        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value

            # We rotate the image for 90 degrees - X and Y size should be inverted.
            self.assertEqual(x_size, sim_y)
            self.assertEqual(y_size, sim_x)

            x_axis_3 = data.data.data["x_axis"].value
            y_axis_3 = data.data.data["y_axis"].value

            # We rotate the image for 90 degrees - X and Y size should be inverted.
            self.assertEqual(x_axis_3.shape[0], sim_y)
            self.assertEqual(y_axis_3.shape[0], sim_x)

        self.instance_manager.stop_all_instances()
    def test_client(self):
        server_info = self.client.get_server_info()

        self.assertIsNot(server_info["active_instances"],
                         "There should be no running instances.")

        expected_cameras = set([
            "camera_example_1", "camera_example_2", "camera_example_3",
            "camera_example_4", "simulation"
        ])

        self.assertSetEqual(set(self.client.get_cameras()), expected_cameras,
                            "Not getting all expected cameras")

        camera_stream_address = self.client.get_camera_stream("simulation")

        self.assertTrue(bool(camera_stream_address),
                        "Camera stream address cannot be empty.")

        self.assertTrue(
            "simulation" in self.client.get_server_info()["active_instances"],
            "Simulation camera not present in server info.")

        # Check if we can connect to the stream and receive data (in less than 2 seconds).
        host, port = get_host_port_from_stream_address(camera_stream_address)
        with source(host=host, port=port, receive_timeout=2000,
                    mode=SUB) as stream:
            data = stream.receive()
            self.assertIsNotNone(data, "Received data was none.")

            required_fields = set(
                ["image", "timestamp", "width", "height", "x_axis", "y_axis"])
            self.assertSetEqual(required_fields, set(data.data.data.keys()),
                                "Required fields missing.")

            image = data.data.data["image"].value
            x_size, y_size = CameraSimulation(
                CameraConfig("simulation")).get_geometry()
            self.assertListEqual(
                list(image.shape), [y_size, x_size],
                "Original and received image are not the same.")

            self.assertEqual(data.data.data["width"].value, x_size,
                             "Width not correct.")
            self.assertEqual(data.data.data["height"].value, y_size,
                             "Height not correct.")

        # Stop the simulation instance.
        self.client.stop_camera("simulation")

        self.assertTrue(
            "simulation"
            not in self.client.get_server_info()["active_instances"],
            "Camera simulation did not stop.")

        self.client.get_camera_stream("simulation")

        self.assertTrue(
            "simulation" in self.client.get_server_info()["active_instances"],
            "Camera simulation did not start.")

        self.client.stop_all_cameras()

        self.assertTrue(
            "simulation"
            not in self.client.get_server_info()["active_instances"],
            "Camera simulation did not stop.")

        example_1_config = self.client.get_camera_config("camera_example_1")

        self.assertTrue(bool(example_1_config), "Cannot retrieve config.")

        # Change the name to reflect tha camera.
        example_1_config["name"] = "testing_camera"

        self.client.set_camera_config("testing_camera", example_1_config)

        testing_camera_config = self.client.get_camera_config("testing_camera")

        self.assertDictEqual(example_1_config, testing_camera_config,
                             "Saved and loaded configs are not the same.")

        geometry = self.client.get_camera_geometry("simulation")
        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        size_x, size_y = simulated_camera.get_geometry()
        self.assertListEqual(
            geometry, [size_x, size_y],
            'The geometry of the simulated camera is not correct.')

        self.assertTrue("testing_camera" in self.client.get_cameras(),
                        "Testing camera should be present.")

        self.client.delete_camera_config("testing_camera")

        self.assertTrue("testing_camera" not in self.client.get_cameras(),
                        "Testing camera should not be present.")

        # Test if it fails quickly enough.
        with self.assertRaisesRegex(
                ValueError,
                "Camera with prefix EPICS_example_1 not online - Status None"):
            self.client.get_camera_stream("camera_example_1")

        self.assertTrue(self.client.is_camera_online("simulation"),
                        "Simulation should be always online")

        self.assertFalse(self.client.is_camera_online("camera_example_1"),
                         "Epics not working in this tests.")

        self.client.set_camera_config(
            "simulation_temp", self.client.get_camera_config("simulation"))

        stream_address = self.client.get_camera_stream("simulation_temp")
        camera_host, camera_port = get_host_port_from_stream_address(
            stream_address)
        sim_x, sim_y = CameraSimulation(
            CameraConfig("simulation")).get_geometry()

        instance_info = self.client.get_server_info(
        )["active_instances"]["simulation_temp"]
        self.assertTrue("last_start_time" in instance_info)
        self.assertTrue("statistics" in instance_info)

        # Collect from the pipeline.
        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value

            self.assertEqual(x_size, sim_x)
            self.assertEqual(y_size, sim_y)

            x_axis_1 = data.data.data["x_axis"].value
            y_axis_1 = data.data.data["y_axis"].value

            self.assertEqual(x_axis_1.shape[0], sim_x)
            self.assertEqual(y_axis_1.shape[0], sim_y)

        camera_config = self.client.get_camera_config("simulation_temp")
        camera_config["rotate"] = 1
        self.client.set_camera_config("simulation_temp", camera_config)
        sleep(0.5)

        # Collect from the pipeline.
        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value

            # We rotate the image for 90 degrees - X and Y size should be inverted.
            self.assertEqual(x_size, sim_y)
            self.assertEqual(y_size, sim_x)

            x_axis_2 = data.data.data["x_axis"].value
            y_axis_2 = data.data.data["y_axis"].value

            # We rotate the image for 90 degrees - X and Y size should be inverted.
            self.assertEqual(x_axis_2.shape[0], sim_y)
            self.assertEqual(y_axis_2.shape[0], sim_x)

        self.client.delete_camera_config("simulation_temp")

        image = self.client.get_camera_image("simulation")
        self.assertGreater(len(image.content), 0)

        image = self.client.get_camera_image_bytes("simulation")
        dtype = image["dtype"]
        shape = image["shape"]
        bytes = base64.b64decode(image["bytes"].encode())

        x_size, y_size = CameraSimulation(
            CameraConfig("simulation")).get_geometry()
        self.assertEqual(shape, [y_size, x_size])

        image_array = numpy.frombuffer(bytes, dtype=dtype).reshape(shape)
        self.assertIsNotNone(image_array)

        self.client.stop_all_cameras()
Beispiel #29
0
def get_image(Device_params, numImg, angle=None):
    if angle == None:
        angle = 0
    pipeline_client = PipelineClient()
    # pipeline_config = {"camera_name": camName, "image_region_of_interest": ROI}
    pipeline_config = {
        "camera_name": Device_params['FullName'],
        "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_fit_gauss_function = []
    y_fit_gauss_function = []
    x_fit_mean = []
    y_fit_mean = []
    x_fit_standard_deviation = []
    y_fit_standard_deviation = []
    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_fit_gauss_function.append(
                data.data.data["x_fit_gauss_function"].value)
            y_fit_gauss_function.append(
                data.data.data["y_fit_gauss_function"].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)
            y_center_of_mass.append(data.data.data["y_center_of_mass"].value)
            x_fit_mean.append(data.data.data["x_fit_mean"].value)
            y_fit_mean.append(data.data.data["y_fit_mean"].value)
            x_fit_standard_deviation.append(
                data.data.data["x_fit_standard_deviation"].value)
            y_fit_standard_deviation.append(
                data.data.data["y_fit_standard_deviation"].value)
            intensity.append(data.data.data["intensity"].value)
            width.append(data.data.data["width"].value)
            height.append(data.data.data["height"].value)
            max_value.append(data.data.data["max_value"].value)

    print(list(data.data.data.keys()))
    img = np.asarray(img)
    # Take metedata
    PhotonEnergy = ep.caget('SARUN08-UIND030:FELPHOTENE.VAL')
    PulseEnergy = ep.caget('SARFE10-PBPG050:PHOTON-ENERGY-PER-PULSE-US')
    ATT053 = ep.caget('SARFE10-OATT053:TRANS_RB')
    APU044_x_pos = ep.caget('SARFE10-OAPU044:MOTOR_X.RBV')
    APU044_y_pos = ep.caget('SARFE10-OAPU044:MOTOR_Y.RBV')
    APU044_w_pos = ep.caget('SARFE10-OAPU044:MOTOR_W.RBV')
    APU044_h_pos = ep.caget('SARFE10-OAPU044:MOTOR_H.RBV')

    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,
        "camera_name": Device_params['FullName'],
        "Bit_depth": Device_params['BitDepth'],
        "PhotonEnergy": PhotonEnergy,
        "PulseEnergy": PulseEnergy,
        "x_fit_gauss_function": x_fit_gauss_function,
        "y_fit_gauss_function": y_fit_gauss_function,
        "x_fit_mean": x_fit_mean,
        "y_fit_mean": y_fit_mean,
        "x_fit_standard_deviation": x_fit_standard_deviation,
        "y_fit_standard_deviation": y_fit_standard_deviation,
        "x_axis": data.data.data["x_axis"].value,
        "y_axis": data.data.data["y_axis"].value,
        "APU044_x_pos": APU044_x_pos,
        "APU044_y_pos": APU044_y_pos,
        "APU044_w_pos": APU044_w_pos,
        "APU044_h_pos": APU044_h_pos,
        "ATT053": ATT053
    }
    return dataout
    def test_manager(self):
        stream_address_1 = self.client.get_instance_stream("simulation")
        stream_address_2 = self.client.get_instance_stream("simulation2")

        #Check if streams are alive
        camera_host, camera_port = get_host_port_from_stream_address(
            stream_address_1)
        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()
            for key in ["image", "width", "height"]:
                self.assertIn(key, data.data.data.keys())

        camera_host, camera_port = get_host_port_from_stream_address(
            stream_address_2)
        with source(host=camera_host, port=camera_port, mode=SUB) as stream:
            data = stream.receive()
            for key in ["image", "width", "height"]:
                self.assertIn(key, data.data.data.keys())

        server_info = self.proxy_client.get_servers_info()
        status_info = self.proxy_client.get_status_info()
        instance_info = self.proxy_client.get_instances_info()

        #Check if streams are equally distributed
        self.assertEqual(server_info[self.cam_server_address[0]]["load"], 1)
        self.assertEqual(server_info[self.cam_server_address[1]]["load"], 1)

        # Check if instance information is available  for each server instance
        for instance in server_info[self.cam_server_address[0]]["instances"]:
            self.assertIn(instance, instance_info)
        for instance in server_info[self.cam_server_address[1]]["instances"]:
            self.assertIn(instance, instance_info)
        self.client.stop_all_instances()

        #Server Config
        self.assertEqual(
            self.proxy_client.get_config(), {
                'http://0.0.0.0:8880': {
                    'expanding': True
                },
                'http://0.0.0.0:8881': {
                    'expanding': True
                }
            })
        self.proxy_client.set_config({
            'http://0.0.0.0:8880': {
                'expanding': True
            },
            'http://0.0.0.0:8881': {
                "instances": ["DUMMY"],
                'expanding': False
            }
        })

        stream_address_1 = self.client.get_instance_stream("simulation")
        stream_address_2 = self.client.get_instance_stream("simulation2")
        #Check if streams are alive
        camera_host, camera_port = get_host_port_from_stream_address(
            stream_address_1)
        camera_host, camera_port = get_host_port_from_stream_address(
            stream_address_2)
        server_info = self.proxy_client.get_servers_info()
        status_info = self.proxy_client.get_status_info()
        instance_info = self.proxy_client.get_instances_info()
        self.assertEqual(server_info[self.cam_server_address[0]]["load"], 2)
        self.assertEqual(server_info[self.cam_server_address[1]]["load"], 0)