Exemple #1
0
    def test_actions(self):
        positions = [[1, 1], [2, 2]]
        positioner = VectorPositioner(positions)

        writables = [
            epics_pv("PYSCAN:TEST:MOTOR1:SET", "PYSCAN:TEST:MOTOR1:GET"),
            epics_pv("PYSCAN:TEST:MOTOR2:SET", "PYSCAN:TEST:MOTOR2:GET")
        ]

        readables = [epics_pv("PYSCAN:TEST:OBS1")]

        # MOTOR1 initial values should be -11, MOTOR2 -22.
        cached_initial_values["PYSCAN:TEST:MOTOR1:SET"] = -11
        cached_initial_values["PYSCAN:TEST:MOTOR2:SET"] = -22
        initialization = [action_set_epics_pv("PYSCAN:TEST:OBS1", -33)]
        finalization = [action_restore(writables)]

        result = scan(positioner=positioner,
                      readables=readables,
                      writables=writables,
                      initialization=initialization,
                      finalization=finalization,
                      settings=scan_settings(measurement_interval=0.25,
                                             n_measurements=1))

        self.assertEqual(pv_cache["PYSCAN:TEST:MOTOR1:SET"][0].value, -11,
                         "Finalization did not restore original value.")
        self.assertEqual(pv_cache["PYSCAN:TEST:MOTOR2:SET"][0].value, -22,
                         "Finalization did not restore original value.")

        self.assertEqual(result[0][0], -33,
                         "Initialization action did not work.")
Exemple #2
0
    def test_bs_read_filter(self):
        config.bs_connection_mode = "pull"

        n_images = 10
        positioner = StaticPositioner(n_images)
        readables = ["bs://CAMERA1:X"]

        # Count how many messages passed.
        def mock_filter(message):
            if message:
                nonlocal filter_pass
                filter_pass += 1
            return True

        filter_pass = 0

        settings = scan_settings(bs_read_filter=mock_filter)

        # settings = scan_settings(bs_read_filter=mock_filter)
        result = scan(positioner=positioner,
                      readables=readables,
                      settings=settings)

        # The length should still be the same - the filter just throws away messages we do not want.
        self.assertEqual(len(result), n_images)

        self.assertTrue(filter_pass >= n_images,
                        "The filter passed less then the received messages.")
Exemple #3
0
    def test_progress_monitor(self):

        current_index = []
        total_positions = 0
        current_percentage = []

        def progress(current_position, max_position):
            current_index.append(current_position)
            current_percentage.append(100 * (current_position / max_position))

            nonlocal total_positions
            total_positions = max_position

        positions = [1, 2, 3, 4, 5]
        positioner = VectorPositioner(positions)
        writables = epics_pv("PYSCAN:TEST:MOTOR1:SET",
                             "PYSCAN:TEST:MOTOR1:GET")
        readables = epics_pv("PYSCAN:TEST:OBS1")
        settings = scan_settings(progress_callback=progress)

        scan(positioner, readables, writables, settings=settings)

        self.assertEqual(
            len(positions) + 1, len(current_index),
            "The number of reported positions is wrong.")
        self.assertEqual(total_positions, 5,
                         "The number of total positions is wrong.")
        self.assertEqual(current_index, [0, 1, 2, 3, 4, 5],
                         "The reported percentage is wrong.")
        self.assertEqual(current_percentage, [0, 20, 40, 60, 80, 100],
                         "The reported percentage is wrong.")
Exemple #4
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
Exemple #5
0
    def test_mixed_sources(self):
        config.bs_connection_mode = "pull"

        positions = [[1, 1], [2, 2], [3, 3], [4, 4]]
        positioner = VectorPositioner(positions)

        writables = [
            epics_pv("PYSCAN:TEST:MOTOR1:SET", "PYSCAN:TEST:MOTOR1:GET"),
            epics_pv("PYSCAN:TEST:MOTOR2:SET", "PYSCAN:TEST:MOTOR2:GET")
        ]

        readables = [
            bs_property("CAMERA1:X"),
            bs_property("CAMERA1:Y"),
            epics_pv("PYSCAN:TEST:OBS1")
        ]

        conditions = [
            epics_condition("PYSCAN:TEST:VALID1", 10),
            bs_condition("CAMERA1:VALID", 10)
        ]

        initialization = [
            action_set_epics_pv("PYSCAN:TEST:PRE1:SET", 1,
                                "PYSCAN:TEST:PRE1:GET")
        ]

        finalization = [
            action_set_epics_pv("PYSCAN:TEST:PRE1:SET", 0,
                                "PYSCAN:TEST:PRE1:GET"),
            action_restore(writables)
        ]

        result = scan(positioner=positioner,
                      readables=readables,
                      writables=writables,
                      conditions=conditions,
                      initialization=initialization,
                      finalization=finalization,
                      settings=scan_settings(measurement_interval=0.25,
                                             n_measurements=1))

        self.assertEqual(len(result), len(positions),
                         "Not the expected number of results.")

        # The first 2 attributes are from bs_read, they should be equal to the pulse ID processed.
        self.assertTrue(all(x[0] == x[1] and x[2] == 1 for x in result),
                        "The result is wrong.")
Exemple #6
0
def data_streaker_offset(streaker,
                         offset_range,
                         screen,
                         n_images,
                         dry_run,
                         beamline='Aramis'):

    print(
        'Start data_streaker_offset for streaker %s, screen %s, beamline %s, dry_run %s'
        % (streaker, screen, beamline, dry_run))
    meta_dict_1 = get_meta_data(screen)

    pipeline_client = PipelineClient('http://sf-daqsync-01:8889/')
    offset_pv = streaker + ':CENTER'

    current_val = caget(offset_pv + '.RBV')

    # Start from closer edge of scan
    if abs(current_val - offset_range[0]) > abs(current_val -
                                                offset_range[-1]):
        offset_range = offset_range[::-1]

    if dry_run:
        screen = 'simulation'
        writables = None
        positioner = pyscan.TimePositioner(time_interval=(1.1 * n_images),
                                           n_intervals=len(offset_range))

    else:
        positions = offset_range * 1e3  # convert to mm
        positioner = pyscan.VectorPositioner(positions=positions.tolist())
        writables = [
            pyscan.epics_pv(pv_name=offset_pv,
                            readback_pv_name=offset_pv + '.RBV',
                            tolerance=0.005)
        ]

    cam_instance_name = 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)

    settings = pyscan.scan_settings(settling_time=1,
                                    n_measurements=n_images,
                                    write_timeout=60)

    readables = get_readables(beamline)

    raw_output = pyscan.scan(positioner=positioner,
                             readables=readables,
                             settings=settings,
                             writables=writables)
    result_dict = pyscan_result_to_dict(readables, raw_output, scrap_bs=True)
    #import pdb; pdb.set_trace()
    for ax in ['x_axis', 'y_axis']:
        arr = result_dict[ax] * 1e-6
        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 = {
        'pyscan_result': result_dict,
        'streaker_offsets': offset_range,
        'screen': screen,
        'n_images': n_images,
        'dry_run': dry_run,
        'streaker': streaker,
        'meta_data_begin': meta_dict_1,
        'meta_data_end': meta_dict_2,
    }
    print('End data_streaker_offset')
    return output