def mantel_test(data_1, data_2, perms=10000, tail='upper',
                dist_metric='euclidean', mantel_method='pearson',
                pairwise=False):

  import Mantel
  from scipy.stats import pearsonr

  print('compare ' + data_1 + ' to ' + data_2)

  # calculate distance matrices of both matrices
  dist_metric_1 = dist_metric
  dist_metric_2 = dist_metric
  if data_1 == 'exp-plex':
    dist_metric_1 = 'jaccard'
  if data_2 == 'exp-plex':
    dist_metric_2 = 'jaccard'

  dist_mat_1 = calc_cl_dist(data_type=data_1, dist_metric=dist_metric_1,
                           pairwise=pairwise)
  dist_mat_2 = calc_cl_dist(data_type=data_2, dist_metric=dist_metric_2,
                            pairwise=pairwise)


  pr_results = pearsonr(dist_mat_1, dist_mat_2)

  print('checking that mantel corr is equal to pearsonr')
  print([pr_results])

  # pearson or spearman
  results = Mantel.test(dist_mat_1, dist_mat_2, perms=perms, tail='upper', method=mantel_method)

  print(results)
  print('\n')

  return results
def mantel_elsim_r_average_and_errors(some_participants):

    global column_category_label, all_data, cards, total_participants, perms_of_each_n, perms_of_mantel_test
    dis2 = dissimilarity_matrix(total_participants)

    c2 = clustering_with_clusim(dis2)

    mantel_SUM = 0
    elsim_SUM = 0
    # r is between -1 and 1
    mantel_minimum = 10
    mantel_maximum = -10

    elsim_minimum = 10
    elsim_maximum = -10

    for i in range(perms_of_each_n):
        dis1 = dissimilarity_matrix(some_participants)

        # Mantel Method
        mantel = Mantel.test(dis1,
                             dis2,
                             perms_of_mantel_test,
                             method='pearson',
                             tail='two-tail')
        mantel_r = mantel[0]

        #find errors (minimum and maximum)
        if mantel_r < mantel_minimum:
            mantel_minimum = mantel_r
        elif mantel_r > mantel_maximum:
            mantel_maximum = mantel_r
        mantel_SUM = mantel_SUM + mantel_r

        c1 = clustering_with_clusim(dis1)

        # Element-centric Similarity
        elsim_r = sim.element_sim(c1, c2, r=1.0, alpha=0.9)

        #find errors (minimum and maximum)
        if elsim_r < elsim_minimum:
            elsim_minimum = elsim_r
        elif elsim_r > elsim_maximum:
            elsim_maximum = elsim_r
        elsim_SUM = elsim_SUM + elsim_r

    mantel_average = mantel_SUM / perms_of_each_n  #average of mantel_r
    mantel_l_error = mantel_average - mantel_minimum  #mantel_lower_error
    mantel_u_error = mantel_maximum - mantel_average  #mantel_upper_error

    elsim_average = elsim_SUM / perms_of_each_n  #average of elsim_r
    elsim_l_error = elsim_average - elsim_minimum  #mantel_lower_error
    elsim_u_error = elsim_maximum - elsim_average  #mantel_upper_error

    return mantel_average, mantel_l_error, mantel_u_error, elsim_average, elsim_l_error, elsim_u_error
def sim():
    """
    REDUNDANT FUNCTION, need to edit
    simulate data and run Mantel with 1 dimensional data
    length must be equal to redundant distance matrix
    """
    x_condensed = np.array(list(np.random.random(91)))
    y_condensed = np.array(list(np.random.random(91)))
    sim = Mantel.test(x_condensed, y_condensed)
    # if args.1d == '1'
    return sim
def p_mantel(x, y, z):
    """
    takes three 2-dimensional matrices; x, y, and z
    calculates the linear regression of x and z, and y and z
    and runs a Mantel test on these new matrices of residuals
    using 2 dimensional arrays
    """
    reg1, a1, b1, c1 = np.linalg.lstsq(x, z)  # linear regression
    reg2, a2, b2, c2 = np.linalg.lstsq(y, z)  # linear regression
    r1, r2 = matrix_man(reg1, reg2)
    result = Mantel.test(r1, r2)
    return(result)
Пример #5
0
def generation_results(chain, generation, sublexical=False, permutations=1000, meaning_distances=False, experiment=False):
  if type(meaning_distances) == bool and meaning_distances == False:
    meaning_distances = rater_analysis.reliable_distance_array
  if type(experiment) == bool and experiment == False:
    experiment = basics.determine_experiment_number(chain)
  strings = basics.getWords(experiment, chain, generation, 's')
  if len(set(strings)) > 1:
    if sublexical == True:
      return sublexical_structure.test(strings, meaning_distances, permutations)
    string_distances = basics.stringDistances(strings)
    return Mantel.test(string_distances, meaning_distances, permutations)[2]
  return None
def mant(x=[0.2, 0.4, 0.3, 0.6, 0.9, 0.4],
         y=[0.3, 0.3, 0.2, 0.7, 0.8, 0.3]):
    """
    calculate a Mantel test with x and y
    has default of sample data from jwcarr
    converts to condensed matrix if needed
    """
    if x[0][1] == 0:  # checks if matrix is condensed
        x, y = matrix_man(x, y)
    else:
        pass
    result = Mantel.test(x, y)
    return(result)
def sim_full():
    """
    simulates data and runs Mantel with 2 dimensional data
    for one population
    """
    x_full = np.array(list(np.random.random(100)))
    y_full = np.array(list(np.random.random(100)))
    r1 = x_full.reshape(10, 10)
    r2 = y_full.reshape(10, 10)

    # make matrices non-redundant
    r1 = convert(r1)
    r2 = convert(r2)

    m1, m2 = matrix_man(r1, r2)
    res = Mantel.test(m1, m2)
    return(res, m1, m2)
def mantel_test(data_1,
                data_2,
                perms=10000,
                tail='upper',
                dist_metric='euclidean',
                mantel_method='pearson',
                pairwise=False):

    import Mantel
    from scipy.stats import pearsonr

    print('compare ' + data_1 + ' to ' + data_2)

    # calculate distance matrices of both matrices
    dist_metric_1 = dist_metric
    dist_metric_2 = dist_metric
    if data_1 == 'exp-plex':
        dist_metric_1 = 'jaccard'
    if data_2 == 'exp-plex':
        dist_metric_2 = 'jaccard'

    dist_mat_1 = calc_cl_dist(data_type=data_1,
                              dist_metric=dist_metric_1,
                              pairwise=pairwise)
    dist_mat_2 = calc_cl_dist(data_type=data_2,
                              dist_metric=dist_metric_2,
                              pairwise=pairwise)

    pr_results = pearsonr(dist_mat_1, dist_mat_2)

    print('checking that mantel corr is equal to pearsonr')
    print([pr_results])

    # pearson or spearman
    results = Mantel.test(dist_mat_1,
                          dist_mat_2,
                          perms=perms,
                          tail='upper',
                          method=mantel_method)

    print(results)
    print('\n')

    return results
def mantel_elsim_r_average_and_errors(some_participants):

    global column_category_label, all_data, cards, total_participants, count_of_samples_for_each_n, perms_of_mantel_test
    dis2 = dissimilarity_matrix(total_participants)

    c2 = clustering_with_clusim(dis2)

    mantel_r_table = []
    elsim_r_table = []

    for i in range(count_of_samples_for_each_n):
        dis1 = dissimilarity_matrix(some_participants)

        # Mantel Method
        mantel = Mantel.test(dis1,
                             dis2,
                             perms_of_mantel_test,
                             method='pearson',
                             tail='two-tail')
        mantel_r = mantel[0]

        mantel_r_table.append(mantel_r)

        c1 = clustering_with_clusim(dis1)

        # Element-centric Similarity
        elsim_r = sim.element_sim(c1, c2, r=1.0, alpha=0.9)

        elsim_r_table.append(elsim_r)

    mantel_average = statistics.mean(mantel_r_table)  # average of mantel_r
    mantel_l_error = mantel_average - min(mantel_r_table)  # mantel_lower_error
    mantel_u_error = max(mantel_r_table) - mantel_average  # mantel_upper_error
    mantel_sd = statistics.stdev(mantel_r_table)

    elsim_average = statistics.mean(elsim_r_table)  # average of elsim_r
    elsim_l_error = elsim_average - min(elsim_r_table)  # mantel_lower_error
    elsim_u_error = max(elsim_r_table) - elsim_average  # mantel_upper_error
    elsim_sd = statistics.stdev(elsim_r_table)

    return mantel_average, mantel_l_error, mantel_u_error, mantel_sd, elsim_average, elsim_l_error, elsim_u_error, elsim_sd
Пример #10
0


## Running the Mantel test on the CIFAR dataset
from sklearn.metrics.pairwise import euclidean_distances
from scipy import spatial
from scipy.spatial.distance import pdist, squareform
# First create pairwise distance matrix of the dataset
distance_mat = pairwise_distances(trpc, metric='euclidean')
# now compute the distance matrix on the reduced dataset
distance_pca = pdist(umap50, metric='euclidean')
distance_pca = squareform(distance_pca)
spatial.distance.is_valid_dm(distance_pca)
# check if symmetric
(distance_pca.transpose() == distance_pca).all()

# now can run the mantel test on both these distance matrices
Mantel.test(distance_mat, distance_pca, perms=100, method='pearson', tail='upper')







## Davies-Bouldin Index
from sklearn.metrics import davies_bouldin_score
davies_bouldin_score(Xt_red, trpc_y)
davies_bouldin_score(projection, trpc_y)
davies_bouldin_score(umap50, trpc_y)
Пример #11
0
                total_data = np.concatenate((act_data, pass_data), axis=0)
                total_labels = np.concatenate((labels_act, labels_pass), axis=0)

                rdm_by_time_list = []
                for t in range(0, total_data.shape[2]):
                    locs = [i for i, x in enumerate(sorted_reg) if x == region]
                    reshaped_data = np.squeeze(total_data[:, locs, t])
                    rdm = squareform(pdist(reshaped_data, metric=dist))
                    rdm_by_time_list.append(rdm[None, :, :])
                time_rdm = np.concatenate(rdm_by_time_list)
                print(time_rdm.shape)
                rdm_by_sub_list.append(time_rdm[None, ...])
            brain_rdm = np.concatenate(rdm_by_sub_list)
            print(brain_rdm.shape)

            np.savez_compressed(meg_fname, rdm=brain_rdm, labels=total_labels)

        model_rdm = load_model_rdm(experiment, word, mode, model, dist, noUNK)

        num_time = brain_rdm.shape[0]
        rdm_scores = np.empty(num_time)
        rdm_pvals = np.empty(num_time)
        for i_t in range(num_time):
            if score == 'kendalltau':
                rdm_scores[i_t], rdm_pvals[i_t] = ktau_rdms(np.squeeze(brain_rdm[i_t, :, :]), model_rdm)
            else:
                rdm_scores[i_t], rdm_pvals[i_t] = Mantel.test(np.squeeze(brain_rdm[i_t, :, :]), model_rdm, tail='upper')
        bh_thresh = bhy_multiple_comparisons_procedure(rdm_pvals)
        np.savez_compressed(results_fname, rdm_scores=rdm_scores, rdm_pvals=rdm_pvals, bh_thresh=bh_thresh)

Пример #12
0
    print(matrix_rank(semantic_rdm))
    fig, ax = plt.subplots()
    h = ax.imshow(semantic_rdm, interpolation='nearest')
    plt.colorbar(h)
    ax.set_title('Approx Semantic RDM over Active and Passive Sentences')
    fig.savefig('semantic_rdm_actpass.pdf', bbox_inches='tight')

    ktau, pval = rank_correlate_rdms(vec_rdm, ap_rdm)

    print('Syntactic Kendall tau is {} with pval {}'.format(ktau, pval))

    ktau, pval = rank_correlate_rdms(vec_rdm, semantic_rdm)

    print('Semantic Kendall tau is {} with pval {}'.format(ktau, pval))

    r, p, z = Mantel.test(vec_rdm, ap_rdm)

    print('Syntactic Pearson is {} with pval {} and zval {} from Mantel test'.format(r, p, z))

    r, p, z = Mantel.test(vec_rdm, semantic_rdm)

    print('Semantic Pearson is {} with pval {} and zval {} from Mantel test'.format(r, p, z))

    ktau, pval = rank_correlate_rdms(ap_rdm, semantic_rdm)

    print('Semantic vs Syntax Kendall tau is {} with pval {}'.format(ktau, pval))

    r, p, z = Mantel.test(ap_rdm, semantic_rdm)

    print('Semantic vs Syntax Pearson is {} with pval {} and zval {} from Mantel test'.format(r, p, z))
                               var=var,
                               save_path=hotspot_save_path)
        # plot
        SAC_objects[var].plot_null_hypo(var)
        SAC_objects[var].plot_moran_scatter(hotspot_dict['median'][var], var)
        SAC_objects[var].plot_spot_map(hotspot_dict['median'], var)

# matel test
columns = ['MHIINDX3', 'support']
distance_matrix['feature space'] = pd.DataFrame(
    pair_dist(hotspot_dict['median'].loc[:, columns]),
    index=hotspot_dict['median'].index,
    columns=hotspot_dict['median'].index)
Mantel_test['veridical correlation'], Mantel_test['p-value'], Mantel_test[
    'z-score'] = Mantel.test(distance_matrix['geometry'].values,
                             distance_matrix['feature space'].values,
                             perms=10000)

#%%
# autocorrelation after gridification
# gridify
df_grid = deepcopy(df_geo.loc[df_geo.District == 'ETH', :])
df_grid['ward'] = df_grid.geometry
df_grid['geometry'] = df_grid['points']
xmin, ymin, xmax, ymax = df_grid.total_bounds
width = 0.005
height = 0.005
rows = int(np.ceil(np.divide((ymax - ymin), height)))
cols = int(np.ceil(np.divide((xmax - xmin), width)))
XleftOrigin = xmin
XrightOrigin = xmin + width