Skip to content

Commit 7e67e44

Browse files
committed
Cleanup
1 parent dd4ab92 commit 7e67e44

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

src/diffupy/kernels.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def commute_time_kernel(graph: nx.Graph, normalized: bool = False) -> Matrix:
3737
"""
3838
# Apply pseudo-inverse (moore-penrose) of laplacian matrix
3939

40-
L = LaplacianMatrix(graph, normalized)
41-
L.mat = np.linalg.pinv(L.mat)
40+
laplacian = LaplacianMatrix(graph, normalized)
41+
laplacian.mat = np.linalg.pinv(laplacian.mat)
4242

43-
return L
43+
return laplacian
4444

4545

4646
def diffusion_kernel(graph: nx.Graph, sigma2: float = 1, normalized: bool = True) -> Matrix:
@@ -58,10 +58,10 @@ def diffusion_kernel(graph: nx.Graph, sigma2: float = 1, normalized: bool = True
5858
:param normalized: Indicates if Laplacian transformation is normalized or not.
5959
:return: Laplacian representation of the graph
6060
"""
61-
L = LaplacianMatrix(graph, normalized)
62-
L.mat = sp.linalg.expm(-sigma2 / 2 * L.mat)
61+
laplacian = LaplacianMatrix(graph, normalized)
62+
laplacian.mat = sp.linalg.expm(-sigma2 / 2 * laplacian.mat)
6363

64-
return L
64+
return laplacian
6565

6666

6767
def inverse_cosine_kernel(graph: nx.Graph) -> Matrix:
@@ -77,12 +77,12 @@ def inverse_cosine_kernel(graph: nx.Graph) -> Matrix:
7777
:return: Laplacian representation of the graph
7878
"""
7979
# Decompose matrix (Singular Value Decomposition)
80-
L = LaplacianMatrix(graph, normalized=True)
80+
laplacian = LaplacianMatrix(graph, normalized=True)
8181
# Decompose matrix (Singular Value Decomposition)
82-
U, S, _ = np.linalg.svd(L.mat * (pi / 4))
83-
L.mat = np.matmul(np.matmul(U, np.diag(np.cos(S))), np.transpose(U))
82+
U, S, _ = np.linalg.svd(laplacian.mat * (pi / 4))
83+
laplacian.mat = np.matmul(np.matmul(U, np.diag(np.cos(S))), np.transpose(U))
8484

85-
return L
85+
return laplacian
8686

8787

8888
def p_step_kernel(graph: nx.Graph, a: int = 2, p: int = 5) -> Matrix:
@@ -101,8 +101,8 @@ def p_step_kernel(graph: nx.Graph, a: int = 2, p: int = 5) -> Matrix:
101101
:param p: p-step kernels can be cheaper to compute and have been successful in biological tasks.
102102
:return: Laplacian repr'esentation of the graph.
103103
"""
104-
M = LaplacianMatrix(graph, normalized=True)
105-
M.mat = -M.mat
104+
laplacian = LaplacianMatrix(graph, normalized=True)
105+
laplacian.mat = -laplacian.mat
106106

107107
# Not optimal but keep for clarity
108108
# here we restrict to the normalised version, as the eigenvalues are
@@ -113,14 +113,14 @@ def p_step_kernel(graph: nx.Graph, a: int = 2, p: int = 5) -> Matrix:
113113
if p < 0:
114114
sys.exit('p must be greater than 0')
115115

116-
M.mat = set_diagonal_matrix(M.mat, [x + a for x in np.diag(M.mat)])
116+
laplacian.mat = set_diagonal_matrix(laplacian.mat, [x + a for x in np.diag(laplacian.mat)])
117117

118118
if p == 1:
119-
return M
119+
return laplacian
120120

121-
M.mat = np.linalg.matrix_power(M.mat, p)
121+
laplacian.mat = np.linalg.matrix_power(laplacian.mat, p)
122122

123-
return M
123+
return laplacian
124124

125125

126126
def regularised_laplacian_kernel(

src/diffupy/utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import numpy as np
1111
import pybel
1212

13-
1413
log = logging.getLogger(__name__)
1514

1615

@@ -28,7 +27,7 @@ def get_laplacian(graph: nx.Graph, normalized: bool = False) -> np.ndarray:
2827

2928

3029
def set_diagonal_matrix(matrix, d):
31-
""" """
30+
"""Set diagonal matrix."""
3231
for j, row in enumerate(matrix):
3332
for i, x in enumerate(row):
3433
if i == j:
@@ -39,6 +38,7 @@ def set_diagonal_matrix(matrix, d):
3938

4039

4140
def get_label_node(node: nx.Graph.node) -> str:
41+
"""Get label node."""
4242
if hasattr(node, 'name') and node.name is not None:
4343
if node.name.lower() == "":
4444
log.debug(f'Empty attribute name: {node.as_bel()}')
@@ -86,7 +86,7 @@ def get_label_list_graph(graph: nx.Graph, label: str) -> List:
8686

8787

8888
def get_repeated_labels(labels):
89-
89+
"""Get duplicate labels."""
9090
seen = set()
9191
rep = []
9292
for x in labels:
@@ -145,16 +145,16 @@ def print_dict_dimensions(entities_db, title):
145145

146146

147147
def get_simplegraph_from_multigraph(multigraph):
148-
149-
G = nx.Graph()
148+
"""Convert undirected graph from multigraph."""
149+
graph = nx.Graph()
150150
for u, v, data in multigraph.edges(data=True):
151151
u = get_label_node(u)
152152
v = get_label_node(v)
153153

154154
w = data['weight'] if 'weight' in data else 1.0
155-
if G.has_edge(u, v):
156-
G[u][v]['weight'] += w
155+
if graph.has_edge(u, v):
156+
graph[u][v]['weight'] += w
157157
else:
158-
G.add_edge(u, v, weight=w)
158+
graph.add_edge(u, v, weight=w)
159159

160-
return G
160+
return graph

0 commit comments

Comments
 (0)