<jupyter_start><jupyter_text>Unpermuted values<jupyter_code>num_perms = 100

perm_edges = edges.copy()
indicator = np.zeros_like(mat.toarray(), dtype=int)
for i in tqdm.tnrange(num_perms):
    perm_edges, _ = xswap.permute_edge_list(perm_edges, allow_self_loops=True, allow_antiparallel=False)
    perm_mat = xswap.network_formats.edges_to_matrix(perm_edges, add_reverse_edges=True, 
                                                     shape=(4083, 4083), sparse=False)
    indicator += perm_mat
prior_df = (
    pd.DataFrame({
        'id_a': np.repeat(np.arange(mat.shape[0]), mat.shape[1]),
        'id_b': np.repeat(np.arange(mat.shape[1]).reshape((1, mat.shape[1])), mat.shape[0], axis=0).flatten(),
        'edge': mat.toarray().flatten(),
        'source_degree': np.array(np.repeat(mat.sum(axis=1).reshape((mat.shape[0], 1)), 
                                            mat.shape[0], axis=1)).flatten(),
        'target_degree': np.array(np.repeat(mat.sum(axis=0).reshape((1, mat.shape[1])), 
                                            mat.shape[1], axis=0)).flatten(),
        'indicator': indicator.flatten(),
    })
    .assign(
        dgp_edges = lambda df: df.groupby(['source_degree', 'target_degree']).transform(sum)['indicator'],
        num_dgp = lambda df: df.groupby(['source_degree', 'target_degree']).transform('count')['indicator'],
        edge_prior = lambda df: df['dgp_edges'] / df['num_dgp']
    )
    .filter(items=['id_a', 'id_b', 'edge', 'edge_prior'])
    .assign(
        jaccard = np.array(analysis.jaccard(mat, degree_matrix)).flatten(),
        preferential_attachment = analysis.preferential_attachment_index(mat).flatten(),
        rwr = np.array(analysis.invertible_rwr(mat.toarray(), 0.25)).flatten(),
        resource_allocation = analysis.resource_allocation_index(mat).flatten(),
        adamic = analysis.adamic_adar_index(mat).flatten(),
    )
    .filter(items=['id_a', 'id_b', 'edge', 'edge_prior', 'adamic', 'jaccard', 
                   'preferential_attachment', 'resource_allocation', 'rwr'])
)

prior_df.to_csv('../../data/feature-degree/ppi_feature_values.tsv', sep='\t', index=False)

prior_df.head()
auroc_df = (
    prior_df
    .filter(items=['edge', 'edge_prior', 'adamic', 'jaccard', 'preferential_attachment', 
                   'resource_allocation', 'rwr'])
    .melt(id_vars=['edge'], var_name='feature')
    .groupby('feature')
    .apply(lambda df: sklearn.metrics.roc_auc_score(df['edge'].values, df['value'].values))
    .reset_index()
    .rename(columns={0: 'auroc'})
)

auroc_df.to_csv('../../data/feature-degree/unpermuted_auroc_value.tsv', sep='\t', index=False)

auroc_df.head()<jupyter_output><empty_output><jupyter_text>Permuted values<jupyter_code>num_perms = 100

true_edges = mat.toarray().flatten()
perm_edges = edges.copy()

rows = list()
for i in tqdm.tnrange(num_perms):
    perm_edges, _ = xswap.permute_edge_list(perm_edges, allow_self_loops=True, allow_antiparallel=False)
    perm_mat = xswap.network_formats.edges_to_matrix(perm_edges, add_reverse_edges=True, shape=(4083, 4083))
    
    row = {    
        'adamic': sklearn.metrics.roc_auc_score(
            true_edges, analysis.adamic_adar_index(perm_mat).flatten()
        ),
        'jaccard': sklearn.metrics.roc_auc_score(
            true_edges, np.array(analysis.jaccard(perm_mat, degree_matrix)).flatten()
        ),
        'preferential_attachment': sklearn.metrics.roc_auc_score(
            true_edges, analysis.preferential_attachment_index(perm_mat).flatten()
        ),
        'resource_allocation': sklearn.metrics.roc_auc_score(
            true_edges, analysis.resource_allocation_index(perm_mat).flatten()
        ),
        'rwr': sklearn.metrics.roc_auc_score(
            true_edges, np.array(analysis.invertible_rwr(perm_mat.toarray(), 0.25)).flatten()
        ),
    }
    rows.append(row)
permuted_auroc_df = (
    pd.DataFrame(rows)
)

permuted_auroc_df.to_csv('../../data/feature-degree/permuted_auroc_values.tsv', sep='\t', index=False)

permuted_auroc_df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15957708384558641, 'fraction_numerical': 0.011310548315711827, 'mean_word_length': 3.5099778270509976, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '134549', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1421, 'n_words': 168, 'path': 'nb/7.fig5.feature_degree/1.compute_ppi_features.ipynb', 'repo_name': 'greenelab/xswap-analysis', 'size': '4067'}