Skip to content

Commit d270094

Browse files
committed
[STY] Code cleaning
1 parent 1c4c3b9 commit d270094

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

netneurotools/metrics.py

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,16 @@ def communicability_wei(adjacency):
105105

106106
def rich_feeder_peripheral(x, sc, stat='median'):
107107
"""
108-
Calculates median or mean "connectivity" in rich (hub to hub), feeder
109-
(hub to non-hub), and peripheral (non-hub to non-hub) links.
110-
111-
This function takes a square symmetric correlation/connectivity matrix `x`
112-
and computes the median or mean value within rich, feeder, and peripheral
113-
links. Hubs are defined againt a threshold (from 0 to the maximum degree
114-
`k`, defined on the structural connectivity matrix, `sc`).
108+
Calculates connectivity values in rich, feeder, and peripheral edges.
115109
116110
Parameters
117111
----------
118112
x : (N, N) numpy.ndarray
119113
Symmetric correlation or connectivity matrix
120114
sc : (N, N) numpy.ndarray
121-
binary structural connectivity matrix
122-
stat : str
123-
statistic to use over rich/feeder/peripheral links
124-
'mean' or 'median' (default)
115+
Binary structural connectivity matrix
116+
stat : {'mean', 'median'}, optional
117+
Statistic to use over rich/feeder/peripheral links. Default: 'median'
125118
126119
Returns
127120
-------
@@ -132,56 +125,59 @@ def rich_feeder_peripheral(x, sc, stat='median'):
132125
p-value for each link, computed using Welch's t-test.
133126
Rich links are compared against non-rich links. Feeder links are
134127
compared against peripheral links. Peripheral links are compared
135-
against feeder links. T-test is two-sided.
128+
against feeder links. T-test is one-sided.
136129
137130
Author
138131
------
139132
This code was written by Justine Hansen who promises to fix and even
140133
optimize the code should any issues arise, provided you let her know.
141134
"""
142135

136+
stats = ['mean', 'median']
137+
if stat not in stats:
138+
raise ValueError(f'Provided stat {stat} not valid.\
139+
Must be one of {stats}')
140+
143141
nnodes = len(sc)
144142
mask = np.triu(np.ones(nnodes), 1) > 0
145143
node_degree = degrees_und(sc)
146144
k = np.max(node_degree).astype(np.int64)
147145
rfp_label = np.zeros((len(sc[mask]), k))
148146

149-
for i in range(k): # for each degree threshold
150-
hub_idx = np.where(node_degree >= i) # find the hubs
147+
for degthresh in range(k): # for each degree threshold
148+
hub_idx = np.where(node_degree >= degthresh) # find the hubs
151149
hub = np.zeros([nnodes, 1])
152150
hub[hub_idx, :] = 1
153151

154-
rfp = np.zeros([nnodes, nnodes]) # for each link, define rfp
155-
for ii in range(nnodes):
156-
for iii in range(nnodes):
157-
if hub[ii] + hub[iii] == 2:
158-
rfp[ii, iii] = 1 # rich
159-
if hub[ii] + hub[iii] == 1:
160-
rfp[ii, iii] = 2 # feeder
161-
if hub[ii] + hub[iii] == 0:
162-
rfp[ii, iii] = 3 # peripheral
163-
rfp_label[:, i] = rfp[mask]
152+
rfp = np.zeros([nnodes, nnodes]) # for each link, define rfp
153+
for edge1 in range(nnodes):
154+
for edge2 in range(nnodes):
155+
if hub[edge1] + hub[edge2] == 2:
156+
rfp[edge1, edge2] = 1 # rich
157+
if hub[edge1] + hub[edge2] == 1:
158+
rfp[edge1, edge2] = 2 # feeder
159+
if hub[edge1] + hub[edge2] == 0:
160+
rfp[edge1, edge2] = 3 # peripheral
161+
rfp_label[:, degthresh] = rfp[mask]
164162

165163
rfp = np.zeros([3, k])
166164
pvals = np.zeros([3, k])
167-
for i in range(k):
168-
169-
if stat == 'median':
170-
rfp[0, i] = np.median(x[rfp_label[:, i] == 1]) # rich
171-
rfp[1, i] = np.median(x[rfp_label[:, i] == 2]) # feeder
172-
rfp[2, i] = np.median(x[rfp_label[:, i] == 3]) # peripheral
173-
174-
if stat == 'mean':
175-
rfp[0, i] = np.mean(x[rfp_label[:, i] == 1]) # rich
176-
rfp[1, i] = np.mean(x[rfp_label[:, i] == 2]) # feeder
177-
rfp[2, i] = np.mean(x[rfp_label[:, i] == 3]) # peripheral
178-
179-
# p-value (Welch's t-test)
180-
_, pvals[0, i] = ttest_ind(x[rfp_label[:, i] == 1],
181-
x[rfp_label[:, i] != 1], equal_var=False)
182-
_, pvals[1, i] = ttest_ind(x[rfp_label[:, i] == 2],
183-
x[rfp_label[:, i] == 3], equal_var=False)
184-
_, pvals[2, i] = ttest_ind(x[rfp_label[:, i] == 3],
185-
x[rfp_label[:, i] == 2], equal_var=False)
165+
for degthresh in range(k):
166+
167+
redfunc = np.median if stat == 'median' else np.mean
168+
for linktype in range(3):
169+
rfp[linktype, degthresh] = redfunc(x[rfp_label[:, degthresh] ==
170+
linktype + 1])
171+
172+
# p-value (one-sided Welch's t-test)
173+
_, pvals[0, degthresh] = ttest_ind(x[rfp_label[:, degthresh] == 1],
174+
x[rfp_label[:, degthresh] != 1],
175+
equal_var=False, alternative='greater')
176+
_, pvals[1, degthresh] = ttest_ind(x[rfp_label[:, degthresh] == 2],
177+
x[rfp_label[:, degthresh] == 3],
178+
equal_var=False, alternative='greater')
179+
_, pvals[2, degthresh] = ttest_ind(x[rfp_label[:, degthresh] == 3],
180+
x[rfp_label[:, degthresh] == 2],
181+
equal_var=False, alternative='greater')
186182

187183
return rfp, pvals

0 commit comments

Comments
 (0)