def main(overide={}):
    # get command line args and config
    sc = 'make_reference'

    # search the current directory for *.ini files if not present in cxi directory
    config_dirs = [os.path.split(os.path.abspath(__file__))[0]]

    # extract the first paragraph from the doc string
    des = st.make_object_map.__doc__.split('\n\n')[0]

    # now load the necessary data
    args, params = cmdline_config_cxi_reader.get_all(sc,
                                                     des,
                                                     config_dirs=config_dirs,
                                                     roi=True)
    params = params['make_reference']

    # overide with input params (if any)
    params.update(overide)

    O, n0, m0 = st.make_object_map(
        params['data'].astype(params['whitefield'].dtype), params['mask'],
        params['whitefield'], params['pixel_translations'],
        params['pixel_map'], params['ls'])

    out = {'reference_image': O, 'n0': n0, 'm0': m0}
    cmdline_config_cxi_reader.write_all(params,
                                        args.filename,
                                        out,
                                        apply_roi=True)

    # output display for gui
    with open('.log', 'w') as f:
        print('display: ' + params['h5_group'] + '/reference_image', file=f)
示例#2
0
def defocus_sweep(z1_min, z1_max, N, z, data, mask, whitefield, basis,
                  x_pixel_size, y_pixel_size, translations, ls):
    """
    Sweep over possible defocus values
    """
    z1s = np.linspace(z1_min, z1_max, N)
    Os = []

    it = tqdm.trange(z1s.shape[0], desc='sweeping defocus')

    for i in it:
        z1 = z1s[i]
        # generate pixel mapping
        pixel_map, pixel_translations, res = st.generate_pixel_map(
            mask.shape,
            translations,
            basis,
            x_pixel_size,
            y_pixel_size,
            z,
            z1,
            None,
            None,
            None,
            verbose=False)

        # generate reference image
        I0 = st.make_object_map(data.astype(whitefield.dtype), mask,
                                whitefield, pixel_translations, pixel_map,
                                ls)[0]

        Os.append(np.mean(gaussian_gradient_magnitude(I0, sigma=ls)**2))

    z1 = z1s[np.argmax(Os)]
    return np.array(Os), z1
示例#3
0
def defocus_sweep(z1_min, z1_max, N, z, roi, data, mask, whitefield, basis,
                  x_pixel_size, y_pixel_size, translations):
    """
    Sweep over possible defocus values
    """
    z1s = np.linspace(z1_min, z1_max, N)
    Os = []

    it = tqdm.trange(z1s.shape[0], desc='sweeping defocus')

    for i in it:
        z1 = z1s[i]
        # generate pixel mapping
        pixel_map, pixel_translations, res = st.generate_pixel_map(
            mask.shape,
            translations,
            basis,
            x_pixel_size,
            y_pixel_size,
            z,
            z1,
            None,
            None,
            None,
            verbose=False)

        # generate reference image
        Iref, m0, n0 = st.make_object_map(data,
                                          mask,
                                          whitefield,
                                          pixel_translations,
                                          pixel_map,
                                          roi=None,
                                          subpixel=False,
                                          verbose=False,
                                          minimum_overlap=None,
                                          sig=None)

        Os.append(np.squeeze(Iref).copy()[:10000])

    # make an array with all Os
    s = max([o.shape[0] for o in Os])
    Os_ar = np.zeros((len(z1s), s), dtype=np.float)
    for i, o in enumerate(Os):
        Os_ar[i, :o.shape[0]] = o

    # find the z1 value with the greatest variance
    vs = []
    for o in Os_ar:
        vs.append(np.var(o[o > 0]))

    i = np.argmax(vs)
    z1 = z1s[i]
    print('i, optimum', i, z1)

    return np.squeeze(Os_ar), z1
示例#4
0
def main(overide={}):
    # get command line args and config
    sc = 'make_pixel_map_poly'

    # search the current directory for *.ini files if not present in cxi directory
    config_dirs = [os.path.split(os.path.abspath(__file__))[0]]

    # extract the first paragraph from the doc string
    #des = st.generate_pixel_map.__doc__.split('\n\n')[0]
    des = ""

    # now load the necessary data
    args, params = cmdline_config_cxi_reader.get_all(sc,
                                                     des,
                                                     config_dirs=config_dirs,
                                                     roi=True)
    params = params[sc]

    # overide with input params (if any)
    params.update(overide)

    # evaluate the polynomial expreesion for the pixel map
    y, x = np.indices(params['whitefield'].shape)
    ufs = eval(params['pixel_map_fs'])
    uss = eval(params['pixel_map_ss'])
    u = np.array([uss, ufs])
    u = np.clip(u, -1000, 1000)

    # generate the pixel translations
    M = params['z'] / params['defocus']
    dfs = params['x_pixel_size'] / M
    dss = params['y_pixel_size'] / M

    pixel_translations = st.make_pixel_translations(params['translations'],
                                                    params['basis'], dss, dfs)

    O, n0, m0 = st.make_object_map(
        params['data'].astype(params['whitefield'].dtype), params['mask'],
        params['whitefield'], pixel_translations,
        u.astype(params['whitefield'].dtype), params['ls'])

    u0 = np.array(np.indices(params['mask'].shape))
    du = u - u0

    out = {
        'reference_image': O,
        'n0': n0,
        'm0': m0,
        'pixel_map': u,
        'pixel_map_residual': du,
        'pixel_translations': pixel_translations,
        'dfs': dfs,
        'dss': dss
    }
    cmdline_config_cxi_reader.write_all(params,
                                        args.filename,
                                        out,
                                        apply_roi=True)

    # output display for gui
    with open('.log', 'w') as f:
        print('display: ' + params['h5_group'] + '/reference_image', file=f)
示例#5
0
def test_st_update(defocus, length_scale, path, roi):
    with h5py.File(path, 'r') as cxi_file:
        I_n = cxi_file['/entry_1/data_1/data'][:, roi[0]:roi[1],
                                               roi[2]:roi[3]].sum(axis=1)[:,
                                                                          None]
        basis = cxi_file['/entry_1/instrument_1/detector_1/basis_vectors'][...]
        z = cxi_file['/entry_1/instrument_1/detector_1/distance'][...]
        x_ps = cxi_file['/entry_1/instrument_1/detector_1/x_pixel_size'][...]
        y_ps = cxi_file['/entry_1/instrument_1/detector_1/y_pixel_size'][...]
        wl = cxi_file['/entry_1/instrument_1/source_1/wavelength'][...]
        dij = cxi_file['/entry_1/sample_1/geometry/translation'][...]
    M = np.ones((I_n.shape[1], I_n.shape[2]), dtype=bool)
    W = st.make_whitefield(I_n, M, verbose=True).astype(I_n.dtype)
    u, dij_pix, res = st.generate_pixel_map(W.shape,
                                            dij,
                                            basis,
                                            x_ps,
                                            y_ps,
                                            z,
                                            defocus,
                                            verbose=True)
    I0, n0, m0 = st.make_object_map(data=I_n,
                                    mask=M,
                                    W=W,
                                    dij_n=dij_pix,
                                    pixel_map=u,
                                    ls=length_scale)

    es = []
    for i in range(5):

        # calculate errors
        error_total = st.calc_error(data=I_n,
                                    mask=M,
                                    W=W,
                                    dij_n=dij_pix,
                                    O=I0,
                                    pixel_map=u,
                                    n0=n0,
                                    m0=m0,
                                    ls=length_scale,
                                    verbose=True)[0]

        # store total error
        es.append(error_total)
        # update pixel map
        u = st.update_pixel_map(data=I_n,
                                mask=M,
                                W=W,
                                O=I0,
                                pixel_map=u,
                                n0=n0,
                                m0=m0,
                                dij_n=dij_pix,
                                sw_ss=0,
                                sw_fs=10,
                                ls=length_scale)

        # make reference image
        I0, n0, m0 = st.make_object_map(data=I_n,
                                        mask=M,
                                        W=W,
                                        dij_n=dij_pix,
                                        pixel_map=u,
                                        ls=length_scale)