bctpy
bctpy copied to clipboard
bct.randomize_graph_partial_und does not converge?
I wanted to use bct.randomize_graph_partial_und
to shuffle my connectivity matrix while keeping the diagonal untouched. However, the algorithm does not seems to converge (?). Instead, I wrote my own function that does this job. Did I do something wrong?
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 29 14:30:02 2021
@author: Johannes.Wiesner
"""
import numpy as np
import bct
# initialize inputs
np.random.seed(42)
A = np.random.rand(5,5)
np.fill_diagonal(A,0, wrap=False)
B = np.eye(5,dtype=bool)
def shuffle_array(A):
A_copy = A.copy()
m = ~np.eye(len(A), dtype=bool) # mask of non-diagonal elements
# Extract non-diagonal elements as a new array and shuffle in-place
Am = A[m]
np.random.shuffle(Am)
# Assign back the shuffled values into non-diag positions of input
A_copy[m] = Am
return A_copy
A_shuffled = shuffle_array(A)
# this does not work
# A_shuffled_bct = bct.randomize_graph_partial_und(A,B,maxswap=2)
Sorry for the long delay, can you provide the example inputs that do not converge