def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'
    experiment = 'fmril'
    nor_method = 'no'
    clf_method = 'svm'
    source_id = np.arange(5)
    target_id = 6
    op_function = 'l1l2'  # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data

    indices_sub = fucs.split_subjects(subjects)

    S = len(source_id)
    xs = [x[indices_sub[i]] for i in source_id]
    ys = [y_target[indices_sub[i]] for i in source_id]
    xt = x[indices_sub[target_id]]
    yt = y_target[indices_sub[target_id]]
    Cs = [sp.spatial.distance.cdist(xs[s], xs[s]) for s in range(S)]
    Cs = [cs / cs.max() for cs in Cs]
    ns = [len(xs[s]) for s in range(S)]
    ps = [ot.unif(ns[s]) for s in range(S)]
    p = ot.unif(len(xt))
    lambdas = ot.unif(S)
    C, T = ot.gromov.gromov_barycenters(len(xt),
                                        Cs,
                                        ps,
                                        p,
                                        lambdas,
                                        'square_loss',
                                        5e-4,
                                        max_iter=100,
                                        tol=1e-5)
    ot_source = []

    note = 'barycenter_{}s_{}t_{}_{}_{}_{}'.format(source_id, target_id,
                                                   experiment, nor_method,
                                                   clf_method, op_function)
    print(note)
Exemple #2
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    args = sys.argv[1:]
    # experiment = args[0]
    # nor_method = args[1]
    # clf_method = args[2]
    # source_id = int(args[3])
    # target_id = int(args[4])
    # op_function = args[5] # 'lpl1' 'l1l2' 'maplin'

    experiment = 'fmril'
    nor_method = 'no'
    clf_method = 'svm'
    source_id = 3
    target_id = 2
    op_function = 'maplin'  # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data

    indices_sub = fucs.split_subjects(subjects)

    i = source_id
    j = target_id
    xs = x[indices_sub[i]]
    ys = y_target[indices_sub[i]]
    xt = x[indices_sub[j]]
    yt = y_target[indices_sub[j]]
    note = 'kfold_{}s_{}t_{}_{}_{}_{}_circulaire'.format(
        i, j, experiment, nor_method, clf_method, op_function)
    print(note)
    reg, eta = fucs.pairsubs_circular_kfold(xs,
                                            ys,
                                            xt,
                                            yt,
                                            ot_method=op_function,
                                            clf_method=clf_method)
    print('best reg and eta', reg, eta)
Exemple #3
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    # args = sys.argv[1:]
    # experiment = args[0]
    # nor_method = args[1]
    # clf_method = args[2]
    # source_id = int(args[3])
    # target_id = int(args[4])
    # op_function = args[5] # 'lpl1' 'l1l2'

    metric_xst = 'h'

    experiment = 'fmrir'
    nor_method = 'no'
    clf_method = 'logis'
    source_id = 1
    target_id = 2
    op_function = 'l1l2'  # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data

    indices_sub = fucs.split_subjects(subjects)
    nb_subs = len(indices_sub)

    i = source_id
    j = target_id
    xs = x[indices_sub[i]]
    ys = y_target[indices_sub[i]]
    xt = x[indices_sub[j]]
    yt = y_target[indices_sub[j]]
    note = '{}s_{}t_{}_{}_{}_sinkhorn_{}_{}'.format(i, j, experiment,
                                                    nor_method, clf_method,
                                                    op_function, metric_xst)
    accs = fucs.get_accuracy(xs, ys, xt, yt, clf_method='logis')
    print(accs)
Exemple #4
0
if experiment == 'fmril':
    source = fmril
elif experiment == 'fmrir':
    source = fmrir
else:
    source = meg

y_target = source.y_target
subjects = np.array(source.subjects)
nb_samples_each = source.nb_samples_each
x_data = source.x_data_pow if experiment == 'meg' else source.x_data
x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
x = x_indi if nor_method == 'indi' else x_data

indices_sub = models.split_subjects(subjects)
nb_subs = len(indices_sub)

# for i in range(len(indices_sub)-1):
#     for j in range(i+1, len(indices_sub)):
i = source_id
for j in range(nb_subs):
    if i != j:
        scores_params = {
            'source': [],
            'target': [],
            'ori_score': [],
            'ot_score': [],
            'params': []
        }
        note = '{}s_{}t_{}_{}_{}_skinkhorn_{}'.format(i, j, experiment,
Exemple #5
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    # args = sys.argv[1:]
    # experiment = args[0]
    # nor_method = args[1]
    # clf_method = args[2]
    # source_id = int(args[3])
    # target_id = int(args[4])
    # op_function = args[5] # 'lpl1' 'l1l2'

    metric_xst = "cityblock"

    experiment = 'fmril'
    nor_method = 'no'
    clf_method = 'svm'
    source_id = 1
    target_id = 2
    op_function = 'l1l2'  # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data

    indices_sub = fucs.split_subjects(subjects)
    nb_subs = len(indices_sub)

    i = source_id
    j = target_id
    xs = x[indices_sub[i]]
    ys = y_target[indices_sub[i]]
    xt = x[indices_sub[j]]
    yt = y_target[indices_sub[j]]
    note = '{}s_{}t_{}_{}_{}_sinkhorn_{}_{}'.format(i, j, experiment,
                                                    nor_method, clf_method,
                                                    op_function, metric_xst)
    print(note)

    # cityblock sqeuclidian,
    best_xst, ot_params = fucs.pairsubs_sinkhorn_lables(xs,
                                                        ys,
                                                        xt,
                                                        ot_method=op_function,
                                                        metric=metric_xst,
                                                        clf_method=clf_method)
    # train on xst, test on xt
    ot_accs = fucs.get_accuracy(best_xst, ys, xt, yt, clf_method=clf_method)
    ot_score = np.mean(ot_accs)
    print('ot source data predicts on original target, accuracy', ot_score,
          ot_accs)

    # train on xs, test on xt
    ori_accs = fucs.get_accuracy(xs, ys, xt, yt, clf_method=clf_method)
    ori_score = np.mean(ori_accs)
    print('original source data predicts on original target, accuracy',
          ori_score, ori_accs)
    # train on xs indi-nor, test on xt indi-nor
    x_base = x_indi.copy()
    xs_base = x_base[indices_sub[i]]
    ys_base = y_target[indices_sub[i]]
    xt_base = x_base[indices_sub[j]]
    yt_base = y_target[indices_sub[j]]
    base_accs = fucs.get_accuracy(xs_base,
                                  ys_base,
                                  xt_base,
                                  yt_base,
                                  clf_method=clf_method)
    base_score = np.mean(base_accs)
    print(
        'indi-normalized source predicts on indi-normalized target, accuracy',
        base_score, base_accs)

    pair_params = {}
    pair_params['source'] = i
    pair_params['target'] = j
    pair_params['ori_score'] = round(ori_score, 4)
    pair_params['ori_accs'] = ori_accs
    pair_params['ot_accs'] = ot_accs
    pair_params['ot_score'] = round(ot_score, 4)
    pair_params['base_accs'] = base_accs
    pair_params['base_score'] = round(base_score, 4)
    pair_params['params'] = ot_params
    ttest = stats.ttest_rel(base_accs, ot_accs)
    pair_params['ttest'] = (ttest.pvalue, ttest.statistic)
    print('pair_params', pair_params)
    # if ttest.statistic > 0 and ttest.pvalue < 0.05:
    #     joblib.dump(pair_params, result_dir+'/big_ori/{}_pair_params.pkl'.format(note))
    # elif ttest.statistic > 0 and ttest.pvalue >= 0.05:
    #     joblib.dump(pair_params, result_dir + '/big_ori_non_significant/{}_pair_params.pkl'.format(note))
    # elif ttest.statistic <= 0 and ttest.pvalue >= 0.05:
    #     joblib.dump(pair_params, result_dir + '/big_ot_non_significant/{}_pair_params.pkl'.format(note))
    # else:
    #     joblib.dump(pair_params, result_dir + '/big_ot/{}_pair_params.pkl'.format(note))

    print(
        '-----------------------------------------------------------------------------------------'
    )
    print(best_xst, xs)
    # print(scores_params)
    # note = '{}s_{}ts_{}_{}_{}_sinkhorn_{}_{}'.format(i, len(target_ids), experiment,
    #                                                 nor_method, clf_method, op_function, metric_xst)
    # joblib.dump(scores_params, result_dir + '/{}_score_params.pkl'.format(note))
    # print('unsuccessful transport', unsec)
    print(time.strftime('%Y-%m-%d %A %X %Z', time.localtime(time.time())))
Exemple #6
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    args = sys.argv[1:]
    experiment = args[0]
    nor_method = args[1]
    clf_method = args[2]
    source_id = int(args[3])
    target_id = int(args[4])
    op_function = args[5] # 'lpl1' 'l1l2' 'maplin'



    # experiment = 'fmril'
    # nor_method = 'no'
    # clf_method = 'svm'
    # source_id = 3
    # target_id = 2
    # op_function = 'maplin' # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data

    indices_sub = fucs.split_subjects(subjects)

    i = source_id
    j = target_id
    xs = x[indices_sub[i]]
    ys = y_target[indices_sub[i]]
    xt = x[indices_sub[j]]
    yt = y_target[indices_sub[j]]
    note = '{}s_{}t_{}_{}_{}_{}_circulaire'.format(i, j, experiment,
                                           nor_method, clf_method, op_function)
    print(note)

    # cityblock sqeuclidian, minkowski
    reg, eta = fucs.pairsubs_circular(xs, ys, xt, yt, ot_method=op_function, clf_method=clf_method)
    print('best reg and eta', reg, eta)
    if op_function == 'l1l2':
        transport = ot.da.SinkhornL1l2Transport(reg_e=reg, reg_cl=eta, norm='max')
    elif op_function == 'lpl1':
        transport = ot.da.SinkhornLpl1Transport(reg_e=reg, reg_cl=eta, norm='max')
    elif op_function == 'maplin':
        transport = ot.da.MappingTransport(kernel="linear", mu=reg, eta=eta, bias=True, norm='max')
    else:
        print('Warning: need to choose among "l1l2", "lpl1" and "maplin"', op_function)
    # train on xst, test on xt
    transport.fit(Xs=xs, Xt=xt, ys=ys)
    xst = transport.transform(Xs=xs)
    ot_accs = fucs.get_accuracy(xst, ys, xt, yt, clf_method=clf_method)
    ot_score = np.mean(ot_accs)
    print('train on ot source data, predict on original target, accuracy', ot_score, ot_accs)

    # train on xs, test on xt
    ori_accs = fucs.get_accuracy(xs, ys, xt, yt, clf_method=clf_method)
    ori_score = np.mean(ori_accs)
    print('train on original source data, predict on original target, accuracy', ori_score, ori_accs)
    # train on xs indi-nor, test on xt indi-nor
    x_base = x_indi.copy()
    xs_base = x_base[indices_sub[i]]
    ys_base = y_target[indices_sub[i]]
    xt_base = x_base[indices_sub[j]]
    yt_base = y_target[indices_sub[j]]
    base_accs = fucs.get_accuracy(xs_base, ys_base, xt_base, yt_base, clf_method=clf_method)
    base_score = np.mean(base_accs)
    print('base: indi-normalized source predicts on indi-normalized target, accuracy', base_score, base_accs)

    pair_params = {}
    pair_params['source'] = i
    pair_params['target'] = j
    pair_params['ori_score'] = round(ori_score, 4)
    pair_params['ori_accs'] = ori_accs
    pair_params['ot_accs'] = ot_accs
    pair_params['ot_score'] = round(ot_score, 4)
    pair_params['base_accs'] = base_accs
    pair_params['base_score'] = round(base_score, 4)
    pair_params['params'] = (reg, eta)
    ttest_base_ot = stats.ttest_rel(base_accs,ot_accs)
    pair_params['ttest_base_ot'] = (ttest_base_ot.pvalue, ttest_base_ot.statistic)
    ttest_ori_ot = stats.ttest_rel(ori_accs, ot_accs)
    pair_params['ttest_ori_ot'] = (ttest_ori_ot.pvalue, ttest_ori_ot.statistic)
    print('pair_params', pair_params)
    if ttest_base_ot .statistic > 0:
        joblib.dump(pair_params, result_dir+'/big_base/{}_pair_params.pkl'.format(note))
    else:
        joblib.dump(pair_params, result_dir + '/small_base/{}_pair_params.pkl'.format(note))
    if ttest_ori_ot .statistic > 0:
        joblib.dump(pair_params, result_dir+'/big_ori/{}_pair_params.pkl'.format(note))
    else:
        joblib.dump(pair_params, result_dir + '/small_ori/{}_pair_params.pkl'.format(note))
    # if ttest.statistic > 0 and ttest.pvalue < 0.05:
    #     joblib.dump(pair_params, result_dir+'/big_ori/{}_pair_params.pkl'.format(note))
    # elif ttest.statistic > 0 and ttest.pvalue >= 0.05:
    #     joblib.dump(pair_params, result_dir + '/big_ori_non_significant/{}_pair_params.pkl'.format(note))
    # elif ttest.statistic <= 0 and ttest.pvalue >= 0.05:
    #     joblib.dump(pair_params, result_dir + '/big_ot_non_significant/{}_pair_params.pkl'.format(note))
    # else:
    #     joblib.dump(pair_params, result_dir + '/big_ot/{}_pair_params.pkl'.format(note))

    print('-----------------------------------------------------------------------------------------')
    # print(scores_params)
    # note = '{}s_{}ts_{}_{}_{}_sinkhorn_{}_{}'.format(i, len(target_ids), experiment,
    #                                                 nor_method, clf_method, op_function, metric_xst)
    # joblib.dump(scores_params, result_dir + '/{}_score_params.pkl'.format(note))
    # print('unsuccessful transport', unsec)
    print(time.strftime('%Y-%m-%d %A %X %Z', time.localtime(time.time())))
Exemple #7
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    # args = sys.argv[1:]
    # experiment = args[0]
    # nor_method = args[1]
    # clf_method = args[2]
    # source_id = int(args[3])
    # target_id = int(args[4])
    # op_function = args[5] # 'lpl1' 'l1l2'

    metric_xst = "h"

    experiment = 'newfmri'
    nor_method = 'no'
    clf_method = 'svm'
    source_id = 1
    target_id = 2
    op_function = 'l1l2'  # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    elif experiment == 'meg':
        source = meg
    else:
        source = newfmri

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data

    indices_sub = fucs.split_subjects(subjects)
    nb_subs = len(indices_sub)
    pca = PCA(n_components=144)
    i = source_id
    j = target_id
    xs = x[indices_sub[i]]
    xs_pca = pca.fit_transform(xs)
    ys = y_target[indices_sub[i]]
    xt = x[indices_sub[j]]
    xt_pca = pca.fit_transform(xt)
    yt = y_target[indices_sub[j]]
    note = 'new_pca_h_{}s_{}t_{}_{}_{}_{}'.format(i, j, experiment, nor_method,
                                                  clf_method, op_function)
    print(note)

    # cityblock sqeuclidian, minkowski
    best_xst, ot_params, params_acc = fucs.pairsubs_sinkhorn_lables_h(
        xs, ys, xt, ot_method=op_function, clf_method=clf_method)

    # train on xst, test on xt
    ot_accs = fucs.get_accuracy(best_xst, ys, xt, yt, clf_method=clf_method)
    ot_score = np.mean(ot_accs)
    print('ot source data predicts on original target, accuracy', ot_score,
          ot_accs)

    # train on xs, test on xt
    ori_accs = fucs.get_accuracy(xs, ys, xt, yt, clf_method=clf_method)
    ori_score = np.mean(ori_accs)
    print('original source data predicts on original target, accuracy',
          ori_score, ori_accs)
    # train on xs indi-nor, test on xt indi-nor
    x_base = x_indi.copy()
    xs_base = x_base[indices_sub[i]]
    ys_base = y_target[indices_sub[i]]
    xt_base = x_base[indices_sub[j]]
    yt_base = y_target[indices_sub[j]]
    base_accs = fucs.get_accuracy(xs_base,
                                  ys_base,
                                  xt_base,
                                  yt_base,
                                  clf_method=clf_method)
    base_score = np.mean(base_accs)
    print(
        'base: indi-normalized source predicts on indi-normalized target, accuracy',
        base_score, base_accs)

    pair_params = {}
    pair_params['source'] = i
    pair_params['target'] = j
    pair_params['ori_score'] = round(ori_score, 4)
    pair_params['ori_accs'] = ori_accs
    pair_params['ot_accs'] = ot_accs
    pair_params['ot_score'] = round(ot_score, 4)
    pair_params['base_accs'] = base_accs
    pair_params['base_score'] = round(base_score, 4)
    pair_params['params'] = ot_params
    pair_params['params_acc'] = params_acc
    ttest_base_ot = stats.ttest_rel(base_accs, ot_accs)
    pair_params['ttest'] = (ttest_base_ot.pvalue, ttest_base_ot.statistic)
    print('pair_params', pair_params)
    ttest_ori_ot = stats.ttest_rel(ori_accs, ot_accs)
    pair_params['ttest_ori_ot'] = (ttest_ori_ot.pvalue, ttest_ori_ot.statistic)
    if ttest_base_ot.statistic > 0:
        joblib.dump(pair_params,
                    result_dir + '/big_base/{}_pair_params.pkl'.format(note))
    else:
        joblib.dump(pair_params,
                    result_dir + '/small_base/{}_pair_params.pkl'.format(note))
    if ttest_ori_ot.statistic > 0:
        joblib.dump(pair_params,
                    result_dir + '/big_ori/{}_pair_params.pkl'.format(note))
    else:
        joblib.dump(pair_params,
                    result_dir + '/small_ori/{}_pair_params.pkl'.format(note))
    reg_coor = np.log10(params_acc['params'])
    zero_coor = reg_coor[params_acc['acc'] == 0]
    other_coor = reg_coor[params_acc['acc'] != 0]
    plt.figure(figsize=(9, 5))
    plt.subplot(1, 2, 1)
    plt.plot(zero_coor[:, 0], zero_coor[:, 1], '+r', label='acc is 0.5')
    plt.plot(other_coor[:, 0], other_coor[:, 1], 'ob', label='acc not 0.5')
    plt.legend(loc=0)
    plt.title(note)

    plt.subplot(1, 2, 2)
    accs = params_acc['acc'].reshape((6, 6))
    accs2 = np.empty((accs.T.shape))
    for k in range(accs.shape[0]):
        accs2[:, k] = accs[k][::-1]

    plt.imshow(accs2)
    plt.colorbar()
    plt.title('imshow')
    plt.savefig(main_dir + '/figs/acc_imshow_{}.png'.format(note))
    print(
        '-----------------------------------------------------------------------------------------'
    )
    # print(scores_params)
    # note = '{}s_{}ts_{}_{}_{}_sinkhorn_{}_{}'.format(i, len(target_ids), experiment,
    #                                                 nor_method, clf_method, op_function, metric_xst)
    # joblib.dump(scores_params, result_dir + '/{}_score_params.pkl'.format(note))
    # print('unsuccessful transport', unsec)
    print(time.strftime('%Y-%m-%d %A %X %Z', time.localtime(time.time())))
Exemple #8
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    experiment = 'fmril'
    nor_method = 'indi'
    clf_method = 'svm'

    op_function = 'maplin' # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data
    indices_sub = fucs.split_subjects(subjects)


    sub_ids = np.arange(5)
    xs1 = [x_data[indices_sub[i]][:20] for i in sub_ids]
    xs2 = [x_data[indices_sub[i]][20:] for i in sub_ids]
    xs_indi1 = [x_indi[indices_sub[i]][:20] for i in sub_ids]
    xs_indi2 = [x_indi[indices_sub[i]][20:] for i in sub_ids]

    def barycenter(Xs, loss_fun, reg, max_iter, tol):
        S = len(Xs)
        Xs = [np.asarray(Xs[s], dtype=np.float64) for s in range(S)]
        ps = [ot.utils.unif(Xs[s].shape[0]) for s in range(S)]
        p = ot.utils.unif(Xs[0].shape[0])
        Cs = [ot.utils.dist(Xs[s], Xs[s], metric='euclidean') for s in range(S)]
        Cs = [cs / np.max(cs) for cs in Cs]
        weights = ot.utils.unif(S)
        C = ot.gromov.gromov_barycenters(Xs[0].shape[0], Cs,
                                             ps, p, weights, loss_fun, reg,
                                             max_iter=max_iter, tol=tol, log=False)
        return C
    reg = 0.1
    C1 = barycenter(xs_indi1, 'square_loss', reg, 100, 1e-5)
    C2 = barycenter(xs_indi2, 'square_loss', reg, 100, 1e-5)

    xt_indi = x_indi[indices_sub[8]]
    p1 = ot.utils.unif(xs_indi1[0].shape[0])
    p_t = ot.utils.unif(xs_indi1[0].shape[0])

    data1 = xt_indi[:20]
    data2 = xt_indi[20:]
    c_t1 = ot.utils.dist(data1, data1, metric='euclidean')
    eta = 1
    gw_distance1 = ot.gromov.gromov_wasserstein2(c_t1, C1, p_t, p1, 'square_loss', eta, 100, 1e-5)
    gw_distance2 = ot.gromov.gromov_wasserstein2(c_t1, C2, p_t, p1, 'square_loss', eta, 100, 1e-5)
    print(gw_distance1, gw_distance2)
    c_t2 = ot.utils.dist(data2, data2, metric='euclidean')
    gw_distance1 = ot.gromov.gromov_wasserstein2(c_t2, C1, p_t, p1, 'square_loss', eta, 100, 1e-5)
    gw_distance2 = ot.gromov.gromov_wasserstein2(c_t2, C2, p_t, p1, 'square_loss', eta, 100, 1e-5)
    print(gw_distance1, gw_distance2)
if experiment == 'fmril':
    source = fmril
elif experiment == 'fmrir':
    source = fmrir
else:
    source = meg

result_dir = result_dir + '/{}'.format(experiment)
y_target = source.y_target
subjects = np.array(source.subjects)
x_data = source.x_data_pow if experiment == 'meg' else source.x_data
x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
x = x_indi.copy() if nor_method == 'indi' else x_data.copy()

indices_sub = fucs.split_subjects(subjects)
nb_subs = len(indices_sub)

i = source_id
j = target_id
xs = x[indices_sub[i]]
ys = y_target[indices_sub[i]]
xt = x[indices_sub[j]]

N = 160
A = fmril.x_data[:N]
# A2 = np.empty(N,60,90)
# for i in range(len(A)):
#     A2[i] = A[i].reshape(60,90)
B = np.empty(A.shape)
for i in range(A.shape[0]):
Exemple #10
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    # args = sys.argv[1:]
    # experiment = args[0]
    # nor_method = args[1]
    # clf_method = args[2]
    # source_id = int(args[3])
    # target_id = int(args[4])
    # op_function = args[5] # 'lpl1' 'l1l2'

    metric_xst = 'null'

    experiment = 'fmril'
    nor_method = 'no'
    clf_method = 'logis'
    source_id = 1
    target_id = 2
    op_function = 'l1l2' # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi.copy() if nor_method == 'indi' else x_data.copy()

    indices_sub = fucs.split_subjects(subjects)
    nb_subs = len(indices_sub)

    i = source_id
    j = target_id
    xs = x[indices_sub[i]]
    ys = y_target[indices_sub[i]]
    xt = x[indices_sub[j]]
    # np.savez(result_dir +'/1s_2t_{}_{}_{}.npz'.format(experiment,nor_method,clf_method),xs=xs,xt=xt)
    yt = y_target[indices_sub[j]]
    note = '{}s_{}t_{}_{}_{}_sinkhorn_{}_{}'.format(i, j, experiment,
                                                 nor_method, clf_method, op_function,metric_xst)
    print(note)
    reg = 0.1
    eta = 0.1
    print('reg,eta',reg,eta)
    xsts = []
    gammas = []
    for reg in [1e-2, 1e-1, 1, 10]:
        for eta in [1e-3,1e-2, 1e-1, 1,10]:

            transport = ot.da.SinkhornL1l2Transport
            trans_fuc = transport(reg_e=reg, reg_cl=eta, norm='max')
            trans_fuc.fit(Xs=xs, Xt=xt, ys=ys)
            xst = trans_fuc.transform(Xs=xs)

            xsts.append(xst)
    np.save(result_dir +'/different_xst.npy',xsts)
Exemple #11
0
def main():
    main_dir = os.path.split(os.getcwd())[0]
    result_dir = main_dir + '/results'

    args = sys.argv[1:]
    # experiment = args[0]
    # nor_method = args[1]
    # clf_method = args[2]
    # source_id = int(args[3])
    # target_id = int(args[4])
    # op_function = args[5] # 'lpl1' 'l1l2' 'maplin'

    experiment = 'fmril'
    nor_method = 'indi'
    clf_method = 'svm'

    op_function = 'maplin'  # 'lpl1' 'l1l2'

    if experiment == 'fmril':
        source = fmril
    elif experiment == 'fmrir':
        source = fmrir
    else:
        source = meg

    result_dir = result_dir + '/{}'.format(experiment)
    y_target = source.y_target
    subjects = np.array(source.subjects)
    x_data = source.x_data_pow if experiment == 'meg' else source.x_data
    x_indi = source.x_indi_pow if experiment == 'meg' else source.x_indi
    x = x_indi if nor_method == 'indi' else x_data
    indices_sub = fucs.split_subjects(subjects)

    # j = target_id
    # xs = [x[indices_sub[i]] for i in source_id]
    # ys = [y_target[indices_sub[i]] for i in source_id]
    # xs = np.vstack(xs)
    # ys = np.concatenate(ys)
    # xt = x[indices_sub[j]]
    # yt = y_target[indices_sub[j]]
    # xsxt = xs.append(xt)

    sub_ids = np.arange(3)
    xsxt = [x_data[indices_sub[i]] for i in sub_ids]
    xsxt_indi = [x_indi[indices_sub[i]] for i in sub_ids]
    ysyt = [y_target[indices_sub[i]] for i in sub_ids]
    trans_xsxt = fucs.inverse_xc_barycenters(xsxt_indi,
                                             'square_loss',
                                             0.1,
                                             max_iter=1000,
                                             tol=1e-9)

    # trans_xsxt = [StandardScaler().fit_transform(trans_xsxt[i]) for i in sub_ids]

    def split_data(data, i, ids):
        data = np.asarray(data)
        test = data[i]
        train = [data[j] for j in ids if j != i]
        train = np.vstack(train) if len(
            train[0].shape) > 1 else np.concatenate(train)
        return train, test

    accs = {'trans_accs': [], 'ori_accs': [], 'indi_accs': []}
    for i in sub_ids:

        trans_xs, trans_xt = split_data(trans_xsxt, i, sub_ids)
        ori_xs, ori_xt = split_data(xsxt, i, sub_ids)
        indi_xs, indi_xt = split_data(xsxt_indi, i, sub_ids)
        ys, yt = split_data(ysyt, i, sub_ids)

        _, _, trans_acc, _ = fucs.gridsearch_persub(trans_xs,
                                                    ys,
                                                    trans_xt,
                                                    yt,
                                                    clf_method='svm',
                                                    njobs=3)
        _, _, ori_acc, _ = fucs.gridsearch_persub(ori_xs,
                                                  ys,
                                                  ori_xt,
                                                  yt,
                                                  clf_method='svm',
                                                  njobs=3)
        _, _, indi_acc, _ = fucs.gridsearch_persub(indi_xs,
                                                   ys,
                                                   indi_xt,
                                                   yt,
                                                   clf_method='svm',
                                                   njobs=3)

        accs['trans_accs'].append((i, trans_acc))
        accs['ori_accs'].append((i, ori_acc))
        accs['indi_accs'].append((i, indi_acc))
        print('xt_id, trans_acc, ori_acc, indi_acc', i, trans_acc, ori_acc,
              indi_acc)
    print(accs)
    print(time.strftime('%Y-%m-%d %A %X %Z', time.localtime(time.time())))
Exemple #12
0
import numpy as np
import time

import scipy as sp

import scipy.ndimage as spi
from scipy.spatial.distance import cdist
import matplotlib.pylab as pl
from sklearn import manifold
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.metrics.pairwise import rbf_kernel
import ot

x = fmril.x_indi
ids = fucs.split_subjects(fmril.subjects)

for source in range(5):
    for target in range(5):
        if source != target:

            data1 = x[ids[source]]
            y1 = fmril.y_target[ids[source]]
            data2 = x[ids[target]]
            y2 = fmril.y_target[ids[target]]
            C = ot.utils.dist(data1, data1)

            max_iter = 3000
            eps = 1e-9
            dim = 5400