Skip to content

Commit b17600a

Browse files
committed
Pass flake8
1 parent e5bb624 commit b17600a

8 files changed

Lines changed: 31 additions & 33 deletions

File tree

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ ignore =
1313
F403 # imports not used in init
1414
F401 # imports not used in init
1515
C901 # too complex methods
16+
F841
17+
I201
18+
I202
1619
exclude =
1720
.tox,
1821
.git,

src/diffupy/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def diffuse(
125125
):
126126
"""Run a diffusion method over a network or pregenerated kernel."""
127127
raise NotImplementedError
128-
#TODO : Process arguments and call diffuse
128+
# TODO : Process arguments and call diffuse
129129

130130

131131
if __name__ == '__main__':

src/diffupy/diffuse.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
"""This module provides a generalized function as an interface to interact with the different diffusion methods offered
4-
in this diffuPy package.
5-
"""
3+
"""This module provides a generalized function as an interface to interact with the different diffusion methods."""
64

75
import copy
86
import logging
@@ -28,8 +26,7 @@ def diffuse(
2826
graph: nx.Graph = None,
2927
**kwargs
3028
) -> Matrix:
31-
"""Manage the treatment of the different score diffusion methods applied of/from an path set of labels/scores
32-
of/on a certain network (as a graph format or a graph kernel matrix stemming from a graph).
29+
"""Run diffusion on a network given an input and a diffusion method.
3330
3431
Diffusion methods procedures provided in this package differ on:
3532
(a) How to distinguish positives, negatives and unlabelled examples.
@@ -54,9 +51,9 @@ def diffuse(
5451
nodes introduce null diffusion {y_raw[j] = 0}.
5552
[Vandin, 2011]. They are computed as:
5653
57-
f_{raw} = K · y_{raw}
54+
f_{raw} = k · y_{raw}
5855
59-
where K is a graph kernel, see kernels.py.
56+
where k is a graph kernel, see kernels.py.
6057
These scores treat negative and unlabelled nodes equivalently.
6158
6259
{ml}: same as raw, but negative nodes introduce a negative unit of flow.
@@ -119,10 +116,10 @@ def diffuse(
119116
Possible values ["raw", "ml", "gm", "ber_s", "ber_p", "mc", "z"]
120117
:param graph: A network as a graph. It could be optional if a Kernel is provided
121118
:param kwargs: Optional arguments:
122-
- K: a kernel [matrix] steaming from a graph, thus sparing the graph transformation process
119+
- k: a kernel [matrix] steaming from a graph, thus sparing the graph transformation process
123120
- Other arguments which would differ depending on the chosen method
124121
:return: The diffused scores within the matrix transformation of the network, with the diffusion operation
125-
[K x input_vector] performed
122+
[k x input_vector] performed
126123
127124
"""
128125

@@ -135,8 +132,8 @@ def diffuse(
135132
if graph:
136133
format_network = "graph"
137134
else:
138-
if "K" not in kwargs:
139-
raise ValueError("Neither a graph 'graph' or a kernel 'K' has been provided.")
135+
if "k" not in kwargs:
136+
raise ValueError("Neither a graph 'graph' or a kernel 'k' has been provided.")
140137
format_network = "kernel"
141138

142139
if method == "raw":
@@ -164,7 +161,7 @@ def diffuse(
164161
if format_network == "graph":
165162
names_ordered = get_label_list_graph(graph, 'name')
166163
elif format_network == "kernel":
167-
names_ordered = kwargs['K'].rows_labels
164+
names_ordered = kwargs['k'].rows_labels
168165

169166
# If the graph is defined
170167
ids_nobkgd = set(names_ordered) - set(scores.rows_labels)

src/diffupy/diffuse_raw.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from .kernels import regularised_laplacian_kernel
1111
from .matrix import Matrix
12-
from .validate_input import _validate_scores, _validate_graph, _validate_K
12+
from .validate_input import _validate_scores, _validate_graph, _validate_k
1313

1414
logger = logging.getLogger()
1515

@@ -31,8 +31,8 @@ def calculate_scores(
3131
:param col_ind: background object for the diffusion
3232
:param scores: list of score matrices. For a single path with a single background, supply a list with a vector column
3333
:param diff: bool to indicate if z-scores be computed instead of raw scores
34-
:param const_mean: K optional matrix precomputed diffusion kernel
35-
:param const_var: K optional matrix precomputed diffusion kernel
34+
:param const_mean: k optional matrix precomputed diffusion kernel
35+
:param const_var: k optional matrix precomputed diffusion kernel
3636
:return: Calculated column z-score
3737
"""
3838
col_in = scores[:, col_ind]
@@ -54,24 +54,24 @@ def diffuse_raw(
5454
graph: nx.Graph,
5555
scores: Matrix,
5656
z: bool = False,
57-
K: Matrix = None,
57+
k: Matrix = None,
5858
) -> Matrix:
5959
"""Compute the score diffusion procedure, given an initial state as a set of scores and a network where diffuse it.
6060
6161
:param graph: background network
62-
:param scores: list of score matrices. For a single path with a single background, supply a list with a vector column
63-
:param z-logical: bool to indicate if z-scores be computed instead of raw scores
64-
:param K optional matrix precomputed diffusion kernel
62+
:param scores: list of score matrices. For a single path with a single background, supply a list with a vector col
63+
:param z: bool to indicate if z-scores be computed instead of raw scores
64+
:param k: optional matrix precomputed diffusion kernel
6565
:return: A list of scores, with the same length and dimensions as scores
6666
"""
6767
# Sanity checks
6868
_validate_scores(scores)
6969
logging.info('Scores validated.')
7070

7171
# Get the Kernel
72-
if K:
73-
kernel = copy.copy(K)
74-
_validate_K(kernel)
72+
if k:
73+
kernel = copy.copy(k)
74+
_validate_k(kernel)
7575
logging.info('Using supplied kernel matrix...')
7676
elif graph:
7777
_validate_graph(graph)

src/diffupy/kernels.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def commute_time_kernel(graph: nx.Graph, normalized: bool = False) -> Matrix:
3737
:return: Laplacian representation of the graph.
3838
"""
3939
# Apply pseudo-inverse (moore-penrose) of laplacian matrix
40-
4140
laplacian = LaplacianMatrix(graph, normalized)
4241
laplacian.mat = np.linalg.pinv(laplacian.mat)
4342

@@ -49,7 +48,7 @@ def diffusion_kernel(graph: nx.Graph, sigma2: float = 1, normalized: bool = True
4948
5049
It has a "bandwidth" parameter sigma^2 that controls the extent of the spreading.
5150
Quoting [Smola, 2003]:
52-
K(x1,x2) can be visualized as the quantity of some substance that would accumulate at
51+
k(x1,x2) can be visualized as the quantity of some substance that would accumulate at
5352
vertex x2 after a given amount of time if we injected the substance at vertex x1 and let
5453
it diffuse through the graph along the edges.
5554
@@ -89,7 +88,7 @@ def p_step_kernel(graph: nx.Graph, a: int = 2, p: int = 5) -> Matrix:
8988
"""Compute the inverse cosine kernel, which is based on a cosine transform on the spectrum of the normalized LM.
9089
9190
This kernel is more focused on local properties of the nodes, because random walks
92-
are limited in terms of length. Therefore, if p is small, only a fraction of the values K(x1,x2)
91+
are limited in terms of length. Therefore, if p is small, only a fraction of the values k(x1,x2)
9392
will be non-null if the network is sparse [Smola, 2003]. The parameter a is a regularising term
9493
that is summed to the spectrum of the normalised Laplacian matrix, and has to be 2 or greater.
9594
The p-step kernels can be cheaper to compute and have been successful in biological tasks, see the benchmark in

src/diffupy/matrix.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def __init__(
3333
**kwargs
3434
):
3535
"""Initialize matrix."""
36-
3736
if isinstance(rows_labels, list) or isinstance(rows_labels, set) or isinstance(rows_labels, np.ndarray):
3837
self.rows_labels = list(rows_labels)
3938
elif graph:
@@ -423,7 +422,7 @@ def order_rows(self, reverse=True, col_ref_idx=None):
423422

424423
"""Import"""
425424

426-
def from_csv(csv_path):
425+
def from_csv(self, csv_path):
427426
"""Import matrix from csv file using the headers as a Matrix class."""
428427
m = np.genfromtxt(csv_path, dtype=None, delimiter=',')
429428
return Matrix(
@@ -440,7 +439,7 @@ def from_csv(csv_path):
440439

441440

442441
class LaplacianMatrix(Matrix):
443-
"""Laplacian matrix class"""
442+
"""Laplacian matrix class."""
444443

445444
def __init__(self, graph, normalized=False, name=''):
446445
"""Initialize object."""

src/diffupy/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def decode_labels(labels):
126126

127127

128128
def print_dict_dimensions(entities_db, title):
129-
"""Print dimension of the dictionary"""
129+
"""Print dimension of the dictionary."""
130130
total = 0
131131
print(title)
132132
for k1, v1 in entities_db.items():

src/diffupy/validate_input.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
def _validate_method(method: str) -> None:
17-
"""Ensures that 'method' is a valid character."""
17+
"""Ensure that 'method' is a valid character."""
1818
if not isinstance(method, str):
1919
raise ValueError(f"The supplied 'method' must be a string. The given argument is a {type(method)}")
2020

@@ -101,8 +101,8 @@ def _validate_graph(graph: nx.Graph) -> None:
101101
raise Warning("'graph' should not contain negative edge weights.")
102102

103103

104-
def _validate_K(k: Matrix) -> None:
105-
"""Check kernel sanity: Ensures that 'k' is a formally valid kernel. Does not check for spd"""
104+
def _validate_k(k: Matrix) -> None:
105+
"""Check kernel sanity: Ensures that 'k' is a formally valid kernel."""
106106
if not isinstance(k, Matrix):
107107
raise ValueError("'k' must be a Matrix object")
108108

0 commit comments

Comments
 (0)