def spectral_bipartivity(G, nodes=None, weight='weight'):
 try:
  import scipy.linalg
 except ImportError:
  raise ImportError('spectral_bipartivity() requires SciPy: ', 'http://scipy.org/')
 nodelist = list(G)
 A = nx.to_numpy_matrix(G, nodelist, weight=weight)
 expA = scipy.linalg.expm(A)
 expmA = scipy.linalg.expm((- A))
 coshA = (0.5 * (expA + expmA))
 if (nodes is None):
  return (coshA.diagonal().sum() / expA.diagonal().sum())
 else:
  index = dict(zip(nodelist, range(len(nodelist))))
  sb = {}
  for n in nodes:
   i = index[n]
   sb[n] = (coshA[(i, i)] / expA[(i, i)])
  return sb

def spectral_bipartivity ( G , nodes = None , weight = 'weight' ) : try : import scipy . linalg except ImportError : raise ImportError ( 'spectral_bipartivity() requires SciPy: ' , 'http://scipy.org/' ) nodelist = list ( G ) A = nx . to_numpy_matrix ( G , nodelist , weight = weight ) expA = scipy . linalg . expm ( A ) expmA = scipy . linalg . expm ( ( - A ) ) coshA = ( 0.5 * ( expA + expmA ) ) if ( nodes is None ) : return ( coshA . diagonal ( ) . sum ( ) / expA . diagonal ( ) . sum ( ) ) else : index = dict ( zip ( nodelist , range ( len ( nodelist ) ) ) ) sb = { } for n in nodes : i = index [ n ] sb [ n ] = ( coshA [ ( i , i ) ] / expA [ ( i , i ) ] ) return sb
returns the spectral bipartivity .