コード例 #1
0
    def test_default_config(self):
        configuration = {
            "camera_name": "simulation",
            "image_background": "test_background",
            "image_good_region": {}
        }

        configuration = PipelineConfig("test", configuration)
        complete_config = configuration.get_configuration()

        self.assertIsNone(complete_config["image_slices"],
                          "This section should be None.")

        self.assertSetEqual(set(complete_config["image_good_region"].keys()),
                            set(["threshold", "gfscale"]))

        configuration = {
            "camera_name": "simulation",
            "image_good_region": {},
            "image_slices": {}
        }

        configuration = PipelineConfig("test", configuration)
        complete_config = configuration.get_configuration()

        self.assertSetEqual(set(complete_config["image_good_region"].keys()),
                            set(["threshold", "gfscale"]),
                            "Missing keys in camera calibration.")

        self.assertSetEqual(set(complete_config["image_slices"].keys()),
                            set(["number_of_slices", "scale", "orientation"]),
                            "Missing keys in camera calibration.")
コード例 #2
0
    def test_image_threshold(self):
        simulated_camera = get_simulated_camera()
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()
        x_size, y_size = simulated_camera.get_geometry()

        pipeline_parameters = {
            "camera_name": "simulation",
            "image_threshold": 9999999
        }

        pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
        parameters = pipeline_config.get_configuration()

        result = process_image(image, 0, time.time(), x_axis, y_axis,
                               parameters)

        expected_image = numpy.zeros(shape=(y_size, x_size), dtype="uint16")
        self.assertTrue(numpy.array_equal(result["image"], expected_image),
                        "An image of zeros should have been produced.")

        pipeline_parameters = {
            "camera_name": "simulation",
            "image_threshold": 0
        }

        pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
        parameters = pipeline_config.get_configuration()

        result = process_image(image, 0, time.time(), x_axis, y_axis,
                               parameters)

        self.assertTrue(numpy.array_equal(result["image"], image),
                        "The image should be the same as the original image.")
コード例 #3
0
    def test_image_background(self):
        pipeline_parameters = {
            "camera_name": "simulation",
            "image_background": "white_background"
        }

        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()

        background_provider = MockBackgroundManager()
        x_size, y_size = simulated_camera.get_geometry()
        background_provider.save_background("white_background", numpy.zeros(shape=(y_size, x_size)),
                                            append_timestamp=False)

        pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
        parameters = pipeline_config.get_configuration()
        image_background_array = background_provider.get_background(parameters.get("image_background"))

        result = process_image(image=image,
                               timestamp=time.time(),
                               x_axis=x_axis,
                               y_axis=y_axis,
                               parameters=parameters,
                               image_background_array=image_background_array)

        self.assertTrue(numpy.array_equal(result["image"], image),
                        "A zero background should not change the image.")

        max_value_in_image = result["max_value"]

        pipeline_parameters = {
            "camera_name": "simulation",
            "image_background": "max_background",
            "image_threshold": 0
        }

        max_background = numpy.zeros(shape=(y_size, x_size), dtype="uint16")
        max_background.fill(max_value_in_image)
        background_provider.save_background("max_background", max_background, append_timestamp=False)

        pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
        parameters = pipeline_config.get_configuration()
        image_background_array = background_provider.get_background(parameters.get("image_background"))

        expected_image = numpy.zeros(shape=(y_size, x_size))

        result = process_image(image=image,
                               timestamp=time.time(),
                               x_axis=x_axis,
                               y_axis=y_axis,
                               parameters=parameters,
                               image_background_array=image_background_array)

        self.assertTrue(numpy.array_equal(result["image"], expected_image),
                        "The image should be all zeros - negative numbers are not allowed.")
コード例 #4
0
    def test_noop_pipeline(self):
        pipeline_config = PipelineConfig("test_pipeline")

        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()
        parameters = pipeline_config.get_configuration()

        result = process_image(image=image,
                               timestamp=time.time(),
                               x_axis=x_axis,
                               y_axis=y_axis,
                               parameters=parameters)

        required_fields_in_result = ['x_center_of_mass', 'x_axis', 'y_axis', 'x_profile', 'y_fit_standard_deviation',
                                     'y_rms', 'timestamp', 'y_profile', 'image', 'max_value', 'x_fit_offset',
                                     'x_fit_gauss_function', 'y_center_of_mass', 'min_value', 'y_fit_mean',
                                     'x_fit_mean', 'x_rms', 'y_fit_amplitude', 'x_fit_amplitude',
                                     'y_fit_gauss_function', 'x_fit_standard_deviation', 'y_fit_offset',
                                     "processing_parameters", "intensity"]

        self.assertSetEqual(set(required_fields_in_result), set(result.keys()),
                            "Not all required keys are present in the result")

        self.assertTrue(numpy.array_equal(result["image"], image),
                        "The input and output image are not the same, but the pipeline should not change it.")

        self.assertDictEqual(parameters, json.loads(result["processing_parameters"]),
                             "The passed and the received processing parameters are not the same.")
コード例 #5
0
    def test_get_instance_stream(self):
        instance_manager = get_test_pipeline_manager_with_real_cam()
        instance_manager.stop_all_instances()

        self.assertTrue(
            len(instance_manager.get_info()["active_instances"]) == 0,
            "There should be no running instances.")

        pipeline_id = "test_pipeline"
        pipeline_config = PipelineConfig(
            pipeline_id, parameters={"camera_name": "simulation"})

        instance_manager.config_manager.save_pipeline_config(
            pipeline_id, pipeline_config.get_configuration())
        instance_stream_1 = instance_manager.get_instance_stream(pipeline_id)

        self.assertTrue(
            instance_manager.get_info()["active_instances"][pipeline_id]
            ["read_only"], "Instance should be read only.")

        with self.assertRaisesRegex(
                ValueError, "Cannot set config on a read only instance."):
            instance_manager.get_instance(pipeline_id).set_parameter({})

        instance_stream_2 = instance_manager.get_instance_stream(pipeline_id)

        self.assertEqual(instance_stream_1, instance_stream_2,
                         "Only one instance should be present.")

        instance_manager.stop_all_instances()
コード例 #6
0
    def test_both_rois(self):
        pipeline_config = PipelineConfig("test_pipeline")

        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()
        parameters = pipeline_config.get_configuration()
        camera_name = simulated_camera.get_name()

        parameters["roi_signal"] = [0, 200, 0, 200]
        parameters["roi_background"] = [0, 200, 0, 200]

        for i in range(10):
            result = process_image(image=image,
                                   pulse_id=i,
                                   timestamp=time.time(),
                                   x_axis=x_axis,
                                   y_axis=y_axis,
                                   parameters=parameters)

        required_fields_in_result = [
            camera_name + ".processing_parameters",
            camera_name + '.roi_signal_x_profile',
            # camera_name + '.edge_position',
            # camera_name + '.cross_correlation_amplitude',
            camera_name + '.roi_background_x_profile'
        ]

        self.assertSetEqual(set(required_fields_in_result), set(result.keys()),
                            "Not all required keys are present in the result")
コード例 #7
0
    def test_background_roi(self):
        pipeline_config = PipelineConfig("test_pipeline")

        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()
        parameters = pipeline_config.get_configuration()
        camera_name = simulated_camera.get_name()

        parameters["roi_background"] = [0, 200, 0, 200]

        result = process_image(image=image,
                               pulse_id=0,
                               timestamp=time.time(),
                               x_axis=x_axis,
                               y_axis=y_axis,
                               parameters=parameters)

        required_fields_in_result = [
            camera_name + ".processing_parameters",
            camera_name + '.roi_background_x_profile'
        ]

        self.assertSetEqual(set(required_fields_in_result), set(result.keys()),
                            "Not all required keys are present in the result")
コード例 #8
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()
コード例 #9
0
output_stream_port = 9999
output_stream_mode = PUB
# output_stream_mode = PUSH

simulated_camera = CameraSimulation(camera_config=CameraConfig("simulation"),
                                    size_x=image_size_x, size_y=image_size_y)
x_axis, y_axis = simulated_camera.get_x_y_axis()
x_size, y_size = simulated_camera.get_geometry()

# Documentation: https://github.com/datastreaming/cam_server#pipeline_configuration
pipeline_parameters = {
    "camera_name": "simulation"
}

pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
parameters = pipeline_config.get_configuration()

image_number = 0

with sender(port=output_stream_port, mode=output_stream_mode) as output_stream:
    # Get simulated image.
    image = simulated_camera.get_image()

    # Generate timestamp.
    timestamp = time.time()

    # Pass data to processing pipeline.
    processed_data = process_image(image, 0, timestamp, x_axis, y_axis, pipeline_parameters)

    # Set height and width.
    processed_data["width"] = processed_data["image"].shape[1]