Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homophily inference #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/source/graphs.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Graphs
=======

.. autoclass:: netin.Graph
:members:

.. toctree::
:maxdepth: 2
:caption: Contents:
Expand Down
30 changes: 29 additions & 1 deletion netin/generators/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def __init__(self, n: int, f_m: float, seed: object = None):
self.node_class_values = None # dictionary of node class values
self.gen_start_time = None # start time of the generation
self.gen_duration = None # duration of the generation

############################################################
# Init
############################################################
Expand Down Expand Up @@ -921,6 +920,35 @@ def copy(self) -> Union[nx.Graph, nx.DiGraph]:
g.n_M = self.n_M
return g

def homophily_inference(self) -> float:
"""
Calculates the inferred homophily based on https://arxiv.org/abs/2401.13642

##Experimental: For now only undirected and canonical

Returns
-------
h_infer: float
Inferred homophily
Raises
------
NotImplementedError
If the graph is directed
"""
e = self.calculate_edge_type_counts()

if self.is_directed():
raise NotImplementedError("This function only supports undirected graphs")

else:
e_rs = e['mM'] + e['Mm']
e_rr = 2 * e['MM']
e_ss = 2 * e['mm']
alpha = e_rs / np.sqrt(e_rr * e_ss)

h_infer = 1 / (1 + alpha)

return h_infer

######################################################################################################################
# Static functions
Expand Down
70 changes: 70 additions & 0 deletions netin/generators/tests/test_homophily_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from netin import PA
from netin import PAH
from netin import PATC
from netin import PATCH
from netin.utils import constants as const
import netin
import numpy as np


class TestHomophilyInference(object):

def test_patch_case_pah(self):
n = 1000
k = 2
f_m = 0.1
h_MM = 0.7
h_mm = 0.7
# seed = 1234
# g = netin.PAH(n=n, k=k, f_m=f_m, h_MM=h_MM, h_mm=h_mm, seed=seed)
g = netin.PAH(n=n, k=k, f_m=f_m, h_MM=h_MM, h_mm=h_mm)
g.generate()
inferred_homophily = g.homophily_inference()

c1 = np.abs( inferred_homophily - h_MM ) < 0.1

assert c1, "Incorrect value of homophily inference"

def test_patch_case_pah_neutral(self):
n = 1000
k = 2
f_m = 0.1
h_MM = 0.5 # Changed from 0.7 to 0.5
h_mm = 0.5 # Changed from 0.7 to 0.5
g = netin.PAH(n=n, k=k, f_m=f_m, h_MM=h_MM, h_mm=h_mm)
g.generate()
inferred_homophily = g.homophily_inference()

c1 = np.abs(inferred_homophily - h_MM) < 0.1

assert c1, "Incorrect value of homophily inference"


def test_patch_case_pa(self):
n = 2000 # Increased network size
k = 4 # Increased average degree
f_m = 0.15
# h_MM = 0.8
# h_mm = 0.8
g = netin.PA(n=n, k=k, f_m=f_m)
g.generate()
inferred_homophily = g.homophily_inference()

c1 = np.abs(inferred_homophily - 0.5) < 0.1

assert c1, "Incorrect value of homophily inference"


def test_patch_case_patch_hetero(self):
n = 2000 # Increased network size
k = 4 # Increased average degree
f_m = 0.15
h_MM = 0.3
h_mm = 0.3
g = netin.PATCH(n=n, k=k, f_m=f_m, h_MM=h_MM, h_mm=h_mm, tc = 0.0)
g.generate()
inferred_homophily = g.homophily_inference()

c1 = np.abs(inferred_homophily - h_mm) < 0.1

assert c1, "Incorrect value of homophily inference"
Loading