Exemplo n.º 1
0
def do_scan(S,
            K,
            max_ai_clock_rate,
            ai_task,
            ao_task,
            do_task,
            N,
            M,
            pixel_time,
            scan_speed,
            pixel_size_x,
            alpha,
            roi,
            cache=None):

    key = (S, K, N, M, pixel_time, scan_speed, alpha, roi)

    flyback_ratio = K / N
    flystart_ratio = S / K
    sample_clock = SampleClock(pixel_time=pixel_time * 1e-6,
                               pixel_size=pixel_size_x * 1e-6)
    T, clock_rate, min_clock_rate = sample_clock.get_optimal_scanning_parameters(
        verbose=False)
    mirror = MirrorDriver(image_size=(N, M),
                          T=T,
                          flyback_ratio=flyback_ratio,
                          flystart_ratio=flystart_ratio,
                          alpha=alpha,
                          roi=roi,
                          clock_rate=clock_rate,
                          verbose=False)

    output_input_clock_ratio = min(int(clock_rate / max_ai_clock_rate) + 1, T)
    while mirror.T % output_input_clock_ratio:
        output_input_clock_ratio += 1

    ai_clock_rate = clock_rate // output_input_clock_ratio

    result = None
    if cache is not None:
        result = cache.data.get(key)
    if result is None:
        vx_target = mirror.vx_t_array()
        vy_target = mirror.vy_t_array()
        i_target = mirror.i_t_array()
        j_target = mirror.j_t_array()

        ai_task.configure_timing_sample_clock(
            rate=ai_clock_rate,
            active_edge='rising',
            sample_mode='finite',
            samples_per_channel=vx_target.size)
        ao_task.configure_timing_sample_clock(
            rate=clock_rate,
            active_edge='rising',
            sample_mode='finite',
            samples_per_channel=vx_target.size)

        # first iteration
        vx_input = vx_target
        vy_input = vy_target
        vx_pos, vy_pos, vx_err, vy_err = run_mirrors(ao_task, ai_task, do_task,
                                                     vx_input, vy_input,
                                                     output_input_clock_ratio)
        i_pos = mirror.i_v(vx_pos, vy_pos)
        j_pos = mirror.j_v(vx_pos, vy_pos)
        toffset, ioffset, joffset, ioffset_std, joffset_std, ioffset_max, joffset_max = mirror.estimate_offsets(
            i_target, j_target, i_pos, j_pos)

        # second iteration
        mirror.set_params(t_offset=toffset + mirror.t_offset)
        vx_input = mirror.vx_t_array()
        vy_input = mirror.vy_t_array()
        vx_pos, vy_pos, vx_err, vy_err = run_mirrors(ao_task, ai_task, do_task,
                                                     vx_input, vy_input,
                                                     output_input_clock_ratio)
        i_pos = mirror.i_v(vx_pos, vy_pos)
        j_pos = mirror.j_v(vx_pos, vy_pos)
        toffset, ioffset, joffset, ioffset_std, joffset_std, ioffset_max, joffset_max = mirror.estimate_offsets(
            i_target, j_target, i_pos, j_pos)

        # third iteration
        mirror.set_params(
            i_offset=ioffset + mirror.i_offset,
            j_offset=joffset + mirror.j_offset,
        )
        vx_input = mirror.vx_t_array()
        vy_input = mirror.vy_t_array()
        vx_pos, vy_pos, vx_err, vy_err = run_mirrors(ao_task, ai_task, do_task,
                                                     vx_input, vy_input,
                                                     output_input_clock_ratio)
        i_pos = mirror.i_v(vx_pos, vy_pos)
        j_pos = mirror.j_v(vx_pos, vy_pos)
        i_input = mirror.i_v(vx_input, vy_input)
        j_input = mirror.j_v(vx_input, vy_input)

        result = vx_target, vy_target, i_target, j_target, vx_input, vy_input, i_input, j_input, vx_pos, vy_pos, i_pos, j_pos, vx_err, vy_err
        if cache is not None:
            cache.data[key] = result
            cache.dump()

    else:
        print 'Using cache for key=', key

    vx_target, vy_target, i_target, j_target, vx_input, vy_input, i_input, j_input, vx_pos, vy_pos, i_pos, j_pos, vx_err, vy_err = result

    toffset, ioffset, joffset, ioffset_std, joffset_std, ioffset_max, joffset_max = mirror.estimate_offsets(
        i_target, j_target, i_pos, j_pos)

    print 'XXX:', pixel_time, scan_speed, N, M, K, T, '->', toffset, ioffset, joffset, ioffset_std, joffset_std

    from ioc.microscope.regress import regress
    regress_scale = min(1, 20 / N)
    i_pos, i_pos_grad = regress(i_pos, (regress_scale, ), method='average')
    j_pos, j_pos_grad = regress(j_pos, (regress_scale, ), method='average')
    vx_pos, vx_pos_grad = regress(vx_pos, (regress_scale, ), method='average')
    vy_pos, vy_pos_grad = regress(vy_pos, (regress_scale, ), method='average')

    plt = mk_figure(vx_target, vy_target, i_target, j_target, vx_input,
                    vy_input, i_input, j_input, vx_pos, vy_pos, i_pos, j_pos,
                    vx_err, vy_err, mirror, sample_clock,
                    output_input_clock_ratio)
    plt.xlabel('ioffset=%s(%.3f,%.3f), joffset=%s(%.3f,%.3f), toffset=%s' %
               (ioffset, ioffset_std, ioffset_max, joffset, joffset_std,
                joffset_max, toffset))
    plt.draw()
    plt.show()

    return ioffset_max
Exemplo n.º 2
0
def do_scan(S, K, max_ai_clock_rate, ai_task, ao_task, do_task, N, M, pixel_time, scan_speed, pixel_size_x, alpha, roi,
            cache = None):

    key = (S, K, N, M, pixel_time, scan_speed, alpha, roi)

    

    flyback_ratio = K/N
    flystart_ratio = S/K
    sample_clock = SampleClock(pixel_time = pixel_time * 1e-6,
                               pixel_size = pixel_size_x * 1e-6)
    T, clock_rate, min_clock_rate = sample_clock.get_optimal_scanning_parameters(verbose=False)
    mirror = MirrorDriver(image_size = (N,M),
                          T=T, flyback_ratio = flyback_ratio,
                          flystart_ratio = flystart_ratio,
                          alpha = alpha,
                          roi = roi,
                          clock_rate = clock_rate,
                          verbose=False)

    output_input_clock_ratio = min(int(clock_rate / max_ai_clock_rate)+1, T)
    while mirror.T % output_input_clock_ratio:
        output_input_clock_ratio += 1

    ai_clock_rate = clock_rate // output_input_clock_ratio
    
    result = None
    if cache is not None:
        result = cache.data.get(key)
    if result is None:
        vx_target = mirror.vx_t_array()
        vy_target = mirror.vy_t_array()
        i_target = mirror.i_t_array()
        j_target = mirror.j_t_array()

        ai_task.configure_timing_sample_clock(rate = ai_clock_rate,
                                              active_edge = 'rising',
                                              sample_mode = 'finite',
                                              samples_per_channel = vx_target.size
                                              )
        ao_task.configure_timing_sample_clock(rate = clock_rate,
                                              active_edge = 'rising',
                                              sample_mode = 'finite',
                                              samples_per_channel = vx_target.size
                                              )

        # first iteration
        vx_input = vx_target
        vy_input = vy_target
        vx_pos, vy_pos, vx_err, vy_err = run_mirrors(ao_task, ai_task, do_task, 
                                                     vx_input, vy_input, output_input_clock_ratio)
        i_pos = mirror.i_v(vx_pos, vy_pos)
        j_pos = mirror.j_v(vx_pos, vy_pos)
        toffset, ioffset, joffset, ioffset_std, joffset_std, ioffset_max, joffset_max = mirror.estimate_offsets(i_target, j_target, i_pos, j_pos)
        
        
        # second iteration
        mirror.set_params(t_offset=toffset + mirror.t_offset)
        vx_input = mirror.vx_t_array()
        vy_input = mirror.vy_t_array()
        vx_pos, vy_pos, vx_err, vy_err = run_mirrors(ao_task, ai_task, do_task, 
                                                 vx_input, vy_input, output_input_clock_ratio)                
        i_pos = mirror.i_v(vx_pos, vy_pos)
        j_pos = mirror.j_v(vx_pos, vy_pos)
        toffset, ioffset, joffset, ioffset_std, joffset_std, ioffset_max, joffset_max = mirror.estimate_offsets(i_target, j_target, i_pos, j_pos)
        
        # third iteration
        mirror.set_params(i_offset=ioffset + mirror.i_offset,
                          j_offset=joffset + mirror.j_offset,
                          )
        vx_input = mirror.vx_t_array()
        vy_input = mirror.vy_t_array()
        vx_pos, vy_pos, vx_err, vy_err = run_mirrors(ao_task, ai_task, do_task, 
                                                     vx_input, vy_input, output_input_clock_ratio)
        i_pos = mirror.i_v(vx_pos, vy_pos)
        j_pos = mirror.j_v(vx_pos, vy_pos)
        i_input = mirror.i_v(vx_input, vy_input)
        j_input = mirror.j_v(vx_input, vy_input)

        result = vx_target, vy_target, i_target, j_target, vx_input, vy_input, i_input, j_input, vx_pos, vy_pos, i_pos, j_pos, vx_err, vy_err
        if cache is not None:
            cache.data[key] = result
            cache.dump()

    else:
        print 'Using cache for key=', key

    vx_target, vy_target, i_target, j_target, vx_input, vy_input, i_input, j_input, vx_pos, vy_pos, i_pos, j_pos, vx_err, vy_err = result

    toffset, ioffset, joffset, ioffset_std, joffset_std, ioffset_max, joffset_max = mirror.estimate_offsets(i_target, j_target, i_pos, j_pos)

    print 'XXX:', pixel_time, scan_speed, N,M,K,T,'->',toffset, ioffset, joffset, ioffset_std, joffset_std

    from ioc.microscope.regress import regress
    regress_scale = min(1, 20/N)
    i_pos, i_pos_grad = regress(i_pos, (regress_scale,), method='average')
    j_pos, j_pos_grad = regress(j_pos, (regress_scale,), method='average')
    vx_pos, vx_pos_grad = regress(vx_pos, (regress_scale,), method='average')
    vy_pos, vy_pos_grad = regress(vy_pos, (regress_scale,), method='average')

    plt = mk_figure(vx_target, vy_target, i_target, j_target, vx_input, vy_input,
                    i_input, j_input,
                    vx_pos, vy_pos,  i_pos, j_pos, vx_err, vy_err,
                    mirror, sample_clock, output_input_clock_ratio
                    )
    plt.xlabel('ioffset=%s(%.3f,%.3f), joffset=%s(%.3f,%.3f), toffset=%s' % (ioffset, ioffset_std, ioffset_max, joffset, joffset_std, joffset_max, toffset))
    plt.draw()
    plt.show()

    return ioffset_max
Exemplo n.º 3
0
    scan_speed = pixel_size / pixel_time

    if options.flyback is None:
        flyback = min(2, pixel_size / pixel_time / pixel_flyback_rate)
    else:
        flyback = options.flyback

    assert flyback >= 0, ` flyback `

    mirror_params = {}
    for k, v in options.__dict__.items():
        if k.startswith('param_'):
            mirror_params[k[6:]] = v

    if options.task != 'scan':
        sample_clock = SampleClock(pixel_time=pixel_time,
                                   pixel_size=pixel_size)
        samples_per_pixel, clock_rate, min_clock_rate = sample_clock.get_optimal_scanning_parameters(
        )
        mirror = MirrorDriver(
            image_size=(image_width, options.image_height),
            T=samples_per_pixel,
            flyback_ratio=flyback,
            flystart_ratio=0.05,
            alpha=options.orientation_angle,
            roi=(options.roi_x0, options.roi_y0, options.roi_x1,
                 options.roi_y1),
            clock_rate=clock_rate,
            #params = mirror_params
        )

    if options.task == 'initialize':
Exemplo n.º 4
0
    if options.flyback is None:
        flyback = min(2, pixel_size / pixel_time / pixel_flyback_rate)
    else:
        flyback = options.flyback

    assert flyback>=0, `flyback`



    mirror_params = {}
    for k,v in options.__dict__.items():
        if k.startswith ('param_'):
            mirror_params[k[6:]] = v

    if options.task!='scan':
        sample_clock = SampleClock(pixel_time = pixel_time,
                                   pixel_size = pixel_size)
        samples_per_pixel, clock_rate, min_clock_rate = sample_clock.get_optimal_scanning_parameters()
        mirror = MirrorDriver(image_size = (image_width, options.image_height),
                              T=samples_per_pixel, 
                              flyback_ratio = flyback,
                              flystart_ratio = 0.05,
                              alpha = options.orientation_angle,
                              roi = (options.roi_x0, options.roi_y0, options.roi_x1, options.roi_y1),
                              clock_rate = clock_rate,
                              #params = mirror_params
                              )



    if options.task=='initialize':
        return