aestrivex/bctpy

non-broadcast error participation coefficient

grace-shearrer opened this issue · 1 comments

I kept getting a ValueError: non-broadcastable output operand with shape (100,1) doesn't match the broadcast shape (100,100).
Similar to https://stackoverflow.com/questions/47493559/valueerror-non-broadcastable-output-operand-with-shape-3-1-doesnt-match-the

I am able to get it to work like this:

def participation_coef(W, ci, degree='undirected'):
'''
Participation coefficient is a measure of diversity of intermodular
connections of individual nodes.
Parameters
----------
W : NxN np.ndarray
binary/weighted directed/undirected connection matrix
ci : Nx1 np.ndarray
community affiliation vector
degree : str
Flag to describe nature of graph 'undirected': For undirected graphs
'in': Uses the in-degree
'out': Uses the out-degree
Returns
-------
P : Nx1 np.ndarray
participation coefficient
'''
if degree == 'in':
W = W.T

_, ci = np.unique(ci, return_inverse=True)
ci += 1

n = len(W)  # number of vertices
Ko = np.sum(W, axis=1)  # (out) degree
Gc = np.dot((W != 0), np.diag(ci))  # neighbor community affiliation
Kc2 = np.zeros((n,))  # community-specific neighbors

#changed here
for i in range(1, int(np.max(ci)) + 1):
Kc2 = Kc2 + np.square(np.sum(W * (Gc == i), axis=1))
#change ends
P = np.ones((n,)) - Kc2 / np.square(Ko)
# P=0 if for nodes with no (out) neighbors
P[np.where(np.logical_not(Ko))] = 0

return P

Sorry for the very long delay. I don't know what input matrix would possibly be causing this problem, but putting in your fix should be harmless.