Skip to content
Snippets Groups Projects
Commit 48a784c1 authored by Yassin's avatar Yassin
Browse files

Updating df and lans filters

parent dc405d1a
No related branches found
No related tags found
No related merge requests found
import networkx as nx
import numpy as np
from scipy import integrate
import pandas as pd
from netbone.backbone import Backbone
from netbone.filters import threshold_filter, fraction_filter
def disparity(G: nx.Graph) -> Backbone:
weight='weight'
G = G.copy()
B = nx.Graph()
for u in G:
k = len(G[u])
if k > 1:
sum_w = sum(np.absolute(G[u][v][weight]) for v in G[u])
for v in G[u]:
w = G[u][v][weight]
p_ij = float(np.absolute(w))/sum_w
alpha_ij = 1 - \
(k-1) * integrate.quad(lambda x: (1-x)
** (k-2), 0, p_ij)[0]
# float('%.4f' % alpha_ij)
B.add_edge(u, v, weight=w, p_value=float(alpha_ij))
return Backbone(B, name="Disparity Filter", column="p_value", ascending=True, filters=[threshold_filter, fraction_filter])
\ No newline at end of file
def disparity(data, weight='weight'):
if isinstance(data, pd.DataFrame):
g = nx.from_pandas_edgelist(data, edge_attr='weight', create_using=nx.Graph())
elif isinstance(data, nx.Graph):
g = data.copy()
else:
print("data should be a panads dataframe or nx graph")
return
strength = g.degree(weight=weight)
for node in g.nodes():
k = g.degree[node]
if k>1:
for neighbour in g[node]:
w = float(g[node][neighbour]['weight'])
p_ij = w/strength[node]
alpha_ij = (1-p_ij)**(k-1)
if 'alpha' in g[node][neighbour]:
if alpha_ij < g[node][neighbour]['alpha']:
g[node][neighbour]['alpha'] = alpha_ij
else:
g[node][neighbour]['alpha'] = alpha_ij
return Backbone(g, name="Disparity Filter", column="p_value", ascending=True, filters=[threshold_filter, fraction_filter])
# b = nx.Graph()
# for u in g:
# k = len(g[u])
# if k > 1:
# sum_w = sum(np.absolute(g[u][v][weight]) for v in g[u])
# for v in g[u]:
# w = g[u][v][weight]
# p_ij = float(np.absolute(w))/sum_w
# alpha_ij = 1 - \
# (k-1) * integrate.quad(lambda x: (1-x)
# ** (k-2), 0, p_ij)[0]
# # float('%.4f' % alpha_ij)
# b.add_edge(u, v, weight=w, p_value=float(alpha_ij))
# return Backbone(b, name="Disparity Filter", column="p_value", ascending=True, filters=[threshold_filter, fraction_filter])
import networkx as nx
import pandas as pd
from netbone.backbone import Backbone
from netbone.filters import threshold_filter, fraction_filter
def lans(G :nx.Graph)-> Backbone:
G = G.copy()
for u, v, w in G.edges(data='weight'):
G[u][v]['p_value'] = min(compute_pvalue(G, v, w), compute_pvalue(G, u, w))
return Backbone(G, name="Locally Adaptive Network Sparsification Filter", column="p_value", ascending=True, filters=[threshold_filter, fraction_filter])
def lans(data):
if isinstance(data, pd.DataFrame):
g = nx.from_pandas_edgelist(data, edge_attr='weight', create_using=nx.Graph())
elif isinstance(data, nx.Graph):
g = data.copy()
else:
print("data should be a panads dataframe or nx graph")
return
for u, v, w in g.edges(data='weight'):
g[u][v]['p_value'] = min(compute_pvalue(g, v, w), compute_pvalue(g, u, w))
return Backbone(g, name="Locally Adaptive Network Sparsification Filter", column="p_value", ascending=True,
filters=[threshold_filter, fraction_filter])
def compute_pvalue(G, node, w):
u_degree = G.degree(node, weight='weight')
puv = w/u_degree
puv = w / u_degree
u_n = G[node]
count = len([n for n in u_n if u_n[n]['weight']/u_degree <= puv])
return 1-count/len(u_n)
\ No newline at end of file
count = len([n for n in u_n if u_n[n]['weight'] / u_degree <= puv])
return 1 - count / len(u_n)
......@@ -214,7 +214,7 @@ def plot_radar(graph_properties, title):
radar.use_legend(loc='center left', bbox_to_anchor=(1.04,0.5),ncol=1)
plt.show()
fig.savefig(title + 'props', bbox_inches='tight', dpi=300)
fig.savefig(title + ' properties', bbox_inches='tight', dpi=300)
def plot_distribution(df, title):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment