def cdi_recon(diffracted_pattern, sample_obj, sup,
              beta=1.15, start_avg=0.8, pi_modulus_flag='Complex',
              sw_flag=True, sw_sigma=0.5, sw_threshold=0.1, sw_start=0.2,
              sw_end=0.8, sw_step=10, n_iterations=1000,
              cb_function=None, cb_step=10):

    diffracted_pattern = np.array(diffracted_pattern)     # diffraction data
    diffracted_pattern = np.fft.fftshift(diffracted_pattern)

    real_operation = False
    if pi_modulus_flag.lower() == 'real':
        real_operation = True

    gamma_1 = -1/beta
    gamma_2 = 1/beta

    # get support index
    outside_sup_index = sup != 1

    error_dict = {}
    obj_error = np.zeros(n_iterations)
    diff_error = np.zeros(n_iterations)
    sup_error = np.zeros(n_iterations)

    sup_old = np.zeros_like(diffracted_pattern)
    obj_avg = np.zeros_like(diffracted_pattern).astype(complex)
    avg_i = 0

    time_start = time.time()
    for n in range(n_iterations):
        obj_old = np.array(sample_obj)

        obj_a = pi_modulus(sample_obj, diffracted_pattern)
        if real_operation:
            obj_a = np.abs(obj_a)

        obj_a = (1 + gamma_2) * obj_a - gamma_2 * sample_obj
        obj_a[outside_sup_index] = 0  # define support

        obj_b = np.array(sample_obj)
        obj_b[outside_sup_index] = 0  # define support
        obj_b = (1 + gamma_1) * obj_b - gamma_1 * sample_obj

        obj_b = pi_modulus(obj_b, diffracted_pattern)
        if real_operation:
            obj_b = np.abs(obj_b)

        sample_obj += beta * (obj_a - obj_b)

        # calculate errors
        obj_error[n] = (np.linalg.norm(sample_obj - obj_old) /
                        np.linalg.norm(obj_old))
        diff_error[n] = cal_diff_error(sample_obj, diffracted_pattern)

        if sw_flag:
            if((n >= (sw_start * n_iterations)) and
               (n <= (sw_end * n_iterations))):
                if np.mod(n, sw_step) == 0:
                    logger.info('Refine support with shrinkwrap')
                    sup_index = find_support(sample_obj, sw_sigma,
                                             sw_threshold)
                    sup = np.zeros_like(diffracted_pattern)
                    sup[sup_index] = 1
                    outside_sup_index = sup != 1
                    sup_error[n] = np.sum(sup_old)
                    sup_old = np.array(sup)

        if cb_function and n_iterations % cb_step == 0:
            cb_function(sample_obj, obj_error, diff_error, sup_error)

        if n > start_avg*n_iterations:
            obj_avg += sample_obj
            avg_i += 1

        logger.info('%d object_chi= %f, diff_chi=%f' % (n, obj_error[n],
                                                        diff_error[n]))

    obj_avg = obj_avg / avg_i
    time_end = time.time()

    logger.info('%d iterations takes %f sec' % (n_iterations,
                                                time_end - time_start))

    error_dict['obj_error'] = obj_error
    error_dict['diff_error'] = diff_error
    error_dict['sup_error'] = sup_error

    return obj_avg, error_dict
Run reconstruction with difference map algorithm.
dataset/ETHPy150Open scikit-beam/scikit-beam/skbeam/core/cdi.py/cdi_recon