bctpy icon indicating copy to clipboard operation
bctpy copied to clipboard

bct.randomize_graph_partial_und does not converge?

Open JohannesWiesner opened this issue 3 years ago • 1 comments

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)

JohannesWiesner avatar Jul 29 '21 14:07 JohannesWiesner

Sorry for the long delay, can you provide the example inputs that do not converge

aestrivex avatar Aug 25 '22 21:08 aestrivex