Skip to content

Commit 2e74c65

Browse files
committed
apply pre-commit linting & blacken
1 parent b06765f commit 2e74c65

19 files changed

+1919
-1124
lines changed

hetmatpy/degree_group.py

+62-38
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import pandas
66
import scipy.sparse
77

8-
from hetmatpy.matrix import metaedge_to_adjacency_matrix
98
import hetmatpy.degree_weight
9+
from hetmatpy.matrix import metaedge_to_adjacency_matrix
1010

1111

1212
def degrees_to_degree_to_ind(degrees):
@@ -18,16 +18,22 @@ def degrees_to_degree_to_ind(degrees):
1818

1919
def metapath_to_degree_dicts(graph, metapath):
2020
metapath = graph.metagraph.get_metapath(metapath)
21-
_, _, source_adj_mat = metaedge_to_adjacency_matrix(graph, metapath[0], dense_threshold=0.7)
22-
_, _, target_adj_mat = metaedge_to_adjacency_matrix(graph, metapath[-1], dense_threshold=0.7)
21+
_, _, source_adj_mat = metaedge_to_adjacency_matrix(
22+
graph, metapath[0], dense_threshold=0.7
23+
)
24+
_, _, target_adj_mat = metaedge_to_adjacency_matrix(
25+
graph, metapath[-1], dense_threshold=0.7
26+
)
2327
source_degrees = source_adj_mat.sum(axis=1).flat
2428
target_degrees = target_adj_mat.sum(axis=0).flat
2529
source_degree_to_ind = degrees_to_degree_to_ind(source_degrees)
2630
target_degree_to_ind = degrees_to_degree_to_ind(target_degrees)
2731
return source_degree_to_ind, target_degree_to_ind
2832

2933

30-
def generate_degree_group_stats(source_degree_to_ind, target_degree_to_ind, matrix, scale=False, scaler=1):
34+
def generate_degree_group_stats(
35+
source_degree_to_ind, target_degree_to_ind, matrix, scale=False, scaler=1
36+
):
3137
"""
3238
Yield dictionaries with degree grouped stats
3339
"""
@@ -41,31 +47,37 @@ def generate_degree_group_stats(source_degree_to_ind, target_degree_to_ind, matr
4147
# row_matrix = scipy.sparse.csc_matrix(row_matrix)
4248
for target_degree, col_inds in target_degree_to_ind.items():
4349
row = {
44-
'source_degree': source_degree,
45-
'target_degree': target_degree,
50+
"source_degree": source_degree,
51+
"target_degree": target_degree,
4652
}
47-
row['n'] = len(row_inds) * len(col_inds)
53+
row["n"] = len(row_inds) * len(col_inds)
4854
if source_degree == 0 or target_degree == 0:
49-
row['sum'] = 0
50-
row['nnz'] = 0
51-
row['sum_of_squares'] = 0
55+
row["sum"] = 0
56+
row["nnz"] = 0
57+
row["sum_of_squares"] = 0
5258
yield row
5359
continue
5460

5561
slice_matrix = row_matrix[:, col_inds]
56-
values = slice_matrix.data if scipy.sparse.issparse(slice_matrix) else slice_matrix
62+
values = (
63+
slice_matrix.data
64+
if scipy.sparse.issparse(slice_matrix)
65+
else slice_matrix
66+
)
5767
if scale:
5868
values = numpy.arcsinh(values / scaler)
59-
row['sum'] = values.sum()
60-
row['sum_of_squares'] = (values ** 2).sum()
69+
row["sum"] = values.sum()
70+
row["sum_of_squares"] = (values**2).sum()
6171
if scipy.sparse.issparse(slice_matrix):
62-
row['nnz'] = slice_matrix.nnz
72+
row["nnz"] = slice_matrix.nnz
6373
else:
64-
row['nnz'] = numpy.count_nonzero(slice_matrix)
74+
row["nnz"] = numpy.count_nonzero(slice_matrix)
6575
yield row
6676

6777

68-
def dwpc_to_degrees(graph, metapath, damping=0.5, ignore_zeros=False, ignore_redundant=True):
78+
def dwpc_to_degrees(
79+
graph, metapath, damping=0.5, ignore_zeros=False, ignore_redundant=True
80+
):
6981
"""
7082
Yield a description of each cell in a DWPC matrix adding source and target
7183
node degree info as well as the corresponding path count.
@@ -78,26 +90,32 @@ def dwpc_to_degrees(graph, metapath, damping=0.5, ignore_zeros=False, ignore_red
7890
the same DWPC.
7991
"""
8092
metapath = graph.metagraph.get_metapath(metapath)
81-
_, _, source_adj_mat = metaedge_to_adjacency_matrix(graph, metapath[0], dense_threshold=0.7)
82-
_, _, target_adj_mat = metaedge_to_adjacency_matrix(graph, metapath[-1], dense_threshold=0.7)
93+
_, _, source_adj_mat = metaedge_to_adjacency_matrix(
94+
graph, metapath[0], dense_threshold=0.7
95+
)
96+
_, _, target_adj_mat = metaedge_to_adjacency_matrix(
97+
graph, metapath[-1], dense_threshold=0.7
98+
)
8399
source_degrees = source_adj_mat.sum(axis=1).flat
84100
target_degrees = target_adj_mat.sum(axis=0).flat
85101
del source_adj_mat, target_adj_mat
86102

87-
source_path = graph.get_nodes_path(metapath.source(), file_format='tsv')
88-
source_node_df = pandas.read_csv(source_path, sep='\t')
89-
source_node_names = list(source_node_df['name'])
103+
source_path = graph.get_nodes_path(metapath.source(), file_format="tsv")
104+
source_node_df = pandas.read_csv(source_path, sep="\t")
105+
source_node_names = list(source_node_df["name"])
90106

91-
target_path = graph.get_nodes_path(metapath.target(), file_format='tsv')
92-
target_node_df = pandas.read_csv(target_path, sep='\t')
93-
target_node_names = list(target_node_df['name'])
107+
target_path = graph.get_nodes_path(metapath.target(), file_format="tsv")
108+
target_node_df = pandas.read_csv(target_path, sep="\t")
109+
target_node_names = list(target_node_df["name"])
94110

95-
row_names, col_names, dwpc_matrix = graph.read_path_counts(metapath, 'dwpc', damping)
111+
row_names, col_names, dwpc_matrix = graph.read_path_counts(
112+
metapath, "dwpc", damping
113+
)
96114
dwpc_matrix = numpy.arcsinh(dwpc_matrix / dwpc_matrix.mean())
97115
if scipy.sparse.issparse(dwpc_matrix):
98116
dwpc_matrix = dwpc_matrix.toarray()
99117

100-
_, _, path_count = graph.read_path_counts(metapath, 'dwpc', 0.0)
118+
_, _, path_count = graph.read_path_counts(metapath, "dwpc", 0.0)
101119
if scipy.sparse.issparse(path_count):
102120
path_count = path_count.toarray()
103121

@@ -110,14 +128,14 @@ def dwpc_to_degrees(graph, metapath, damping=0.5, ignore_zeros=False, ignore_red
110128
if ignore_zeros and dwpc_value == 0:
111129
continue
112130
row = {
113-
'source_id': row_names[row_ind],
114-
'target_id': col_names[col_ind],
115-
'source_name': source_node_names[row_ind],
116-
'target_name': target_node_names[col_ind],
117-
'source_degree': source_degrees[row_ind],
118-
'target_degree': target_degrees[col_ind],
119-
'path_count': path_count[row_ind, col_ind],
120-
'dwpc': dwpc_value,
131+
"source_id": row_names[row_ind],
132+
"target_id": col_names[col_ind],
133+
"source_name": source_node_names[row_ind],
134+
"target_name": target_node_names[col_ind],
135+
"source_degree": source_degrees[row_ind],
136+
"target_degree": target_degrees[col_ind],
137+
"path_count": path_count[row_ind, col_ind],
138+
"dwpc": dwpc_value,
121139
}
122140
yield collections.OrderedDict(row)
123141

@@ -127,13 +145,19 @@ def single_permutation_degree_group(permuted_hetmat, metapath, dwpc_mean, dampin
127145
Compute degree-grouped permutations for a single permuted_hetmat,
128146
for one metapath.
129147
"""
130-
_, _, matrix = hetmatpy.degree_weight.dwpc(permuted_hetmat, metapath, damping=damping, dense_threshold=0.7)
131-
source_deg_to_ind, target_deg_to_ind = hetmatpy.degree_group.metapath_to_degree_dicts(permuted_hetmat, metapath)
148+
_, _, matrix = hetmatpy.degree_weight.dwpc(
149+
permuted_hetmat, metapath, damping=damping, dense_threshold=0.7
150+
)
151+
(
152+
source_deg_to_ind,
153+
target_deg_to_ind,
154+
) = hetmatpy.degree_group.metapath_to_degree_dicts(permuted_hetmat, metapath)
132155
row_generator = hetmatpy.degree_group.generate_degree_group_stats(
133-
source_deg_to_ind, target_deg_to_ind, matrix, scale=True, scaler=dwpc_mean)
156+
source_deg_to_ind, target_deg_to_ind, matrix, scale=True, scaler=dwpc_mean
157+
)
134158
degree_grouped_df = (
135159
pandas.DataFrame(row_generator)
136-
.set_index(['source_degree', 'target_degree'])
160+
.set_index(["source_degree", "target_degree"])
137161
.assign(n_perms=1)
138162
)
139163
return degree_grouped_df

0 commit comments

Comments
 (0)