Skip to content

Commit

Permalink
Degree Corrected SBM
Browse files Browse the repository at this point in the history
  • Loading branch information
aubinbnf committed May 27, 2024
1 parent 2c4a39e commit 7ec5bf4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .traditional.Spectral import Spectral
from .traditional.SBM_em import SBM_em
from .traditional.SBM_metropolis import SBM_metropolis
from .traditional.DCSBM import DCSBM
39 changes: 39 additions & 0 deletions src/algorithms/traditional/DCSBM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
from graph import Graph
from algorithms.Algorithm import Algorithm

sys.path.append('..\\library\\')
import pysbm


class DCSBM(Algorithm):
"""Stochastic block model clustering algorithm
"""

def __init__(self, graph: Graph, num_clusters: int, iterations: int = 10000):
"""Constructor method
"""
super(DCSBM, self).__init__(graph)
self.iterations: int = iterations
self.num_clusters: int = num_clusters
self.graph = graph

def run(self) -> None:
"""Runs the algorithm
"""
degree_corrected_partition = pysbm.NxPartition(
graph=self.graph.nx_graph,
number_of_blocks=self.num_clusters)
degree_corrected_objective_function = pysbm.DegreeCorrectedUnnormalizedLogLikelyhood(is_directed=False)
degree_corrected_inference = pysbm.PeixotoInference(self.graph.nx_graph, degree_corrected_objective_function, degree_corrected_partition)
degree_corrected_inference.infer_stochastic_block_model()

self.clusters = [node[1] for node in sorted(degree_corrected_inference.partition.partition.items())]

def __str__(self):
"""Returns the string representation of the algorithm object
:return: String representation of the algorithm object
:rtype: str
"""
return "SBM Metropolis algorithm object"
1 change: 1 addition & 0 deletions src/algorithms/traditional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .SBM_metropolis import SBM_metropolis
from .SBM_em import SBM_em
from .Spectral import Spectral
from .DCSBM import DCSBM
4 changes: 3 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np

from graph import Graph
from algorithms import GAE, ARGA, MVGRL, Markov, Louvain, Leiden, SBM_em, SBM_metropolis, Spectral
from algorithms import GAE, ARGA, MVGRL, Markov, Louvain, Leiden, SBM_em, SBM_metropolis, Spectral, DCSBM


def main():
Expand Down Expand Up @@ -85,6 +85,8 @@ def main():
algo = SBM_metropolis(graph, num_clusters=args.num_clusters, iterations=args.iterations)
case "sbm_em":
algo = SBM_em(graph, num_clusters=args.num_clusters, iterations=args.iterations)
case "dcsbm":
algo = DCSBM(graph, num_clusters=args.num_clusters, iterations=args.iterations)
case "spectral":
algo = Spectral(graph, num_clusters=args.num_clusters)
case _:
Expand Down

0 comments on commit 7ec5bf4

Please sign in to comment.