From 48a784c17b53e49b3f8237724963ee9c6cda8cd4 Mon Sep 17 00:00:00 2001
From: Yassin <aliyassin4@hotmail.com>
Date: Mon, 8 May 2023 08:55:10 +0200
Subject: [PATCH] Updating df and lans filters

---
 netbone/statistical/disparity.py | 58 ++++++++++++++++++++++----------
 netbone/statistical/lans.py      | 28 ++++++++++-----
 netbone/visualize.py             |  2 +-
 3 files changed, 61 insertions(+), 27 deletions(-)

diff --git a/netbone/statistical/disparity.py b/netbone/statistical/disparity.py
index 99d9d4c..f2592ba 100644
--- a/netbone/statistical/disparity.py
+++ b/netbone/statistical/disparity.py
@@ -1,24 +1,48 @@
 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])
+
+
diff --git a/netbone/statistical/lans.py b/netbone/statistical/lans.py
index d70b1d1..76d4f33 100644
--- a/netbone/statistical/lans.py
+++ b/netbone/statistical/lans.py
@@ -1,16 +1,26 @@
 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)
diff --git a/netbone/visualize.py b/netbone/visualize.py
index 58ebe57..9459f08 100644
--- a/netbone/visualize.py
+++ b/netbone/visualize.py
@@ -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):
-- 
GitLab