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

Add ability to generate graphs based on correlations of sequences #25933 #35009

Merged
merged 19 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
32 changes: 32 additions & 0 deletions src/sage/graphs/generators/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# import from Sage library
from sage.graphs.graph import Graph
from math import sin, cos, pi
from numpy import corrcoef
from sage.all import Matrix


def BullGraph():
Expand Down Expand Up @@ -393,6 +395,36 @@ def CompleteGraph(n):
G.add_edges(((i, j) for i in range(n) for j in range(i + 1, n)))
return G

def CorrelationGraph(seqs, alpha, include_anticorrelation):
"""
Constructs and returns a correlation graph with a node corresponding to each sequence in `seqs`.

Edges are added between nodes where the corresponding sequences have a correlation coeffecient greater than alpha.

If include_anticorrelation is true, then edges are also added between nodes with correlation coeffecient less than -alpha.

EXAMPLES:

sage: from sage.graphs.correlation_graph import CorrelationGraph
sage: data=[[1,2,3],[4,5,6],[7,8,9999]]
sage: CG = CorrelationGraph(data, 0.9, False)
sage: CG
Looped graph on 3 vertices
"""

# compute pairwise correlation coeffecients
corrs = corrcoef(seqs)

# compare against alpha to get adjacency matrix
if include_anticorrelation:
boolean_adjacency_matrix = abs(corrs)>=alpha
else:
boolean_adjacency_matrix = corrs>=alpha

adjacency_matrix = Matrix(boolean_adjacency_matrix.astype(int))

# call graph constructor
return Graph(adjacency_matrix, format="adjacency_matrix", name="Correlation Graph")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the linter error (trailing space) in this line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def CompleteBipartiteGraph(p, q, set_position=True):
r"""
Expand Down
2 changes: 2 additions & 0 deletions src/sage/graphs/graph_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def wrap_name(x):
"CompleteBipartiteGraph",
"CompleteGraph",
"CompleteMultipartiteGraph",
"CorrelationGraph",
"DiamondGraph",
"GemGraph",
"DartGraph",
Expand Down Expand Up @@ -2355,6 +2356,7 @@ def quadrangulations(self, order, minimum_degree=None, minimum_connectivity=None
CompleteGraph = staticmethod(basic.CompleteGraph)
CompleteBipartiteGraph = staticmethod(basic.CompleteBipartiteGraph)
CompleteMultipartiteGraph = staticmethod(basic.CompleteMultipartiteGraph)
CorrelationGraph = staticmethod(basic.CorrelationGraph)
DiamondGraph = staticmethod(basic.DiamondGraph)
GemGraph = staticmethod(basic.GemGraph)
DartGraph = staticmethod(basic.DartGraph)
Expand Down