<jupyter_start><jupyter_text>Functions<jupyter_code>def getModularityScoreMultilevel(A):
    
    #construct it so that igraph can read it, make it undirected
    g = ig.Graph(directed=False)
    #make it weighted
    g.es["weight"] = 1.0                
                
    g.add_vertices(len(A))
    ix, jx = A.nonzero()
    for i,j in zip(ix, jx):
        if i<j:
            g.add_edge(i, j, weight=A[i,j])
            
    #calculate the clusters and their modularity score    
    clusters = g.community_multilevel(weights=g.es['weight'])
    modularity_score = g.modularity(clusters.membership,weights=g.es['weight'])
    
    return modularity_score
#calculates Qrandom and Q after rewirings
def getQRandomQ1Q2(adjDict):
    
    repetitions = len(adjDict)
    keyTuple = list(adjDict[1].keys())
    
    Q = {}
    for keyT in keyTuple:
        Q0 = np.zeros(repetitions)
        Q1 = np.zeros(repetitions)
        Q2 = np.zeros(repetitions)
        for ind, k in enumerate(adjDict):

            #load the specific rewired matrices -random and rewired
            A0 = adjDict[k][keyT][0]
            A1 = adjDict[k][keyT][1]
            A2 = adjDict[k][keyT][2]
        
            #store them in the Q vectors
            Q0[ind] = getModularityScoreMultilevel(A0)
            Q1[ind] = getModularityScoreMultilevel(A1)
            Q2[ind] = getModularityScoreMultilevel(A2)
        
        Q[keyT] = (Q0,Q1,Q2)
               
    return Q<jupyter_output><empty_output><jupyter_text>Calculate Qrandom , Q4000 and Q8000 from A and save it  This is for a few taus but for many repetitions 1000 we do it to get the scatter plots for typical examples of transition, modular and centralized regions<jupyter_code>weightDist = ['normal','lognormal']
pRand =[0, 0.2]

directoryLoad ='data/ArandA/1000iterations/QrandQ4000Q8000/'

Q = {}
for wD in weightDist:
    for p in pRand:
        filePathLoad = directoryLoad + 'A0A1A2' +wD+'_pRand'+str(p)+'.pckl'
        ArandA1A2 = hf.loadVar(filePathLoad)
        Q[wD,p] = getQRandomQ1Q2(ArandA1A2)


directorySave = 'data/ModularityValues/1000iterations/QrandQ4000Q8000/'
filePathQSave = directorySave + 'Q0Q1Q2.pckl'
hf.saveVarSimple(Q, filePathQSave)<jupyter_output><empty_output><jupyter_text>Calculate Qrandom , Q4000 and Q8000 from A and save it - This is for many different taus fewer reperitions (100)<jupyter_code>rewiringsTuple= (4000,4000)
weightDist = ['normal','lognormal']

pRand = [0, 0.2]
tauFirst = {}
tauFirst['normal'] = [4.2, 4.15]
tauFirst['lognormal'] = [5.8, 5.5]
tauDiffSecond = np.array([-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,
                          0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2])

tauSecond = {}
for indP,p in enumerate(pRand):
    for wD in weightDist:
        temp = tauFirst[wD][indP] + tauDiffSecond
        tauSecond[(wD,p)] = np.around(temp,decimals=2)

    
tauTupleList = {}
for indP,p in enumerate(pRand):
    for wD in weightDist:
        tauTupleList[(wD,p)] = []   
        for ind in np.arange(len(tauSecond[(wD,p)])):
            tauTupleList[(wD,p)].append((tauFirst[wD][indP],tauSecond[(wD,p)][ind]))


repetitions = 100

directoryLoad ='data/ArandA/2tauRewirings/'

QAll = {}
for wD in weightDist:
    for p in pRand:
        filePathLoad = directoryLoad + 'ArandA1A2_'+wD+'_pRand'+str(p)+'.pckl'
        ArandA1A2 = hf.loadVar(filePathLoad)
        QAll[(wD,p)] = getQRandomQ1Q2(ArandA1A2)
        
        del ArandA1A2
        
directoryQSave ='data/ModularityValues/2tauRewirings/'
descr = 'Q0Q4000Q8000'

filePathQSave = directoryQSave + descr+'.pckl'
hf.saveVarSimple((QAll,tauTupleList), filePathQSave)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1304585152838428, 'fraction_numerical': 0.04667030567685589, 'mean_word_length': 3.0363436123348015, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '629882', 'license': 'MIT', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1380, 'n_words': 266, 'path': '2QGetSaveAtDifferentRewiringStages.ipynb', 'repo_name': 'rentzi/netRewireSpecFlexRob', 'size': '3664'}