def random_network( network, usemetropolis=False, ): # initialize the heinous input struct web = cmfinder.mfinder_input() # setup the network info network, stats, web.Edges, web.NumEdges = mfinder_network_setup(network) # parameterize the analysis if not usemetropolis: web.UseMetropolis = 0 else: web.UseMetropolis = 1 return randomized_network(web)
def motif_roles(network, links=False, motifsize=3, randomize=False, usemetropolis=False, stoufferIDs=False, networktype="unipartite", allroles=False, weighted=False, fweight=None): if motifsize < 2: sys.stderr.write("Error: this is not a valid motif size.\n") sys.exit() if stoufferIDs and motifsize > 3: sys.stderr.write( "Warning: 'stoufferIDs' can only be true when 'motifsize=3' in unipartite networks.\n" ) stoufferIDs = False if (networktype == "unipartite" and motifsize > 3) or (networktype == "bipartite" and motifsize > 6): sys.stderr.write( "Error: the analysis of the motif-role profiles can only be done for motif size 2 and 3 in unipartite networks and up to motif size 6 in bipartite networks.\n" ) sys.exit() # do we want to randomize the network first? if randomize: #This will restart the whole object network = random_network(network, usemetropolis=usemetropolis) # initialize the heinous input struct web = cmfinder.mfinder_input() # setup the network info network, stats, web.Edges, web.NumEdges = mfinder_network_setup(network) # add or check some basics of stats object if stats.motifsize: if stats.motifsize != motifsize: sys.stderr.write("You're trying to mix motif sizes.\n") sys.exit() else: stats.motifsize = motifsize if stats.networktype: if stats.networktype != networktype: sys.stderr.write("You're trying to mix network types.\n") sys.exit() else: stats.networktype = networktype if stats.weighted != None: if stats.weighted != weighted: sys.stderr.write( "You're trying to mix two different motif analyses (weighted and not weighted). Be careful!\n" ) stats.weighted = weighted if fweight == None: fweight = default_fweight # parameterize the analysis web.MotifSize = motifsize web.Randomize = 0 web.UseMetropolis = 0 if len(stats.motifs) == 0: web.NRandomizations = 0 web.UseMetropolis = 0 motif_stats(web, stats, stoufferIDs, allroles) web.MaxMembersListSz = max([stats.motifs[x].real for x in stats.motifs]) + 1 #TODO I can also run this inside role_stats if stats.weighted: weighted_motif_stats(web, stats, stoufferIDs, fweight, allroles) #check if this function has already been run if len(stats.nodes[stats.nodes.keys()[0]].roles) != 0: for x in stats.nodes.keys(): stats.nodes[x].roles = dict() if len(stats.links[stats.links.keys()[0]].roles) != 0: for x in stats.links.keys(): stats.links[x].roles = dict() # determine all nodes' role statistics return role_stats(web, stats, links, networktype, stoufferIDs, allroles, fweight)
def motif_structure(network, motifsize=3, nrandomizations=0, usemetropolis=False, allmotifs=False, stoufferIDs=False, weighted=False, fweight=None): if motifsize < 2: sys.stderr.write("Error: this is not a valid motif size.\n") sys.exit() if motifsize > 8: sys.stderr.write("Warning: this is not a recommended motif size.\n") sys.exit() if weighted and nrandomizations > 0: sys.stderr.write( "Warning: the analysis of weighted motifs won't be performed for the randomized networks, only for the real one. There are different ways to randomize weighted networks, you could define your own and run motif_structure multiple times to find the random distribution of weighted motifs.\n" ) if stoufferIDs and motifsize > 3: sys.stderr.write( "Warning: 'stoufferIDs' can only be true when 'motifsize=3' in unipartite networks.\n" ) stoufferIDs = False # initialize the heinous input struct web = cmfinder.mfinder_input() # setup the network info network, stats, web.Edges, web.NumEdges = mfinder_network_setup(network) # add or check some basics of stats object if stats.motifsize: if stats.motifsize != motifsize: sys.stderr.write("Error: you're trying to mix motif sizes.\n") sys.exit() else: stats.motifsize = motifsize if stats.networktype: stats.networktype = "unipartite" if stats.weighted != None: if stats.weighted != weighted: sys.stderr.write( "Error: you're trying to mix two different motif analyses.\n") stats.weighted = weighted if fweight == None: fweight = default_fweight # parameterize the analysis web.MotifSize = motifsize web.NRandomizations = nrandomizations if not usemetropolis: web.UseMetropolis = 0 else: web.UseMetropolis = 1 #check if this function has already been run if len(stats.motifs) != 0: stats.motifs = dict() # determine all nodes' role statistics if stats.weighted: if len(stats.motifs) == 0: motif_stats(web, stats, stoufferIDs, allmotifs) web.MaxMembersListSz = max( [stats.motifs[x].real for x in stats.motifs]) + 1 return weighted_motif_stats(web, stats, stoufferIDs, fweight, allmotifs) else: return motif_stats(web, stats, stoufferIDs, allmotifs)
def motif_participation(network, links=False, motifsize=3, randomize=False, usemetropolis=False, stoufferIDs=False, allmotifs=False, weighted=False, fweight=None): if motifsize < 2: sys.stderr.write("Error: this is not a valid motif size.\n") sys.exit() if motifsize > 8: sys.stderr.write("Error: this is not a recommended motif size.\n") sys.exit() if motifsize > 4 and allmotifs: sys.stderr.write( "Warning: 'allmotifs' will be ignored for this motif size and motif_participation will only register existing motifs in the real network.\n" ) allmotifs = False # do we want to randomize the network first? if randomize: #This will restart the whole object network = random_network(network, usemetropolis=usemetropolis) # initialize the heinous input struct web = cmfinder.mfinder_input() # setup the network info network, stats, web.Edges, web.NumEdges = mfinder_network_setup(network) # add or check some basics of stats object if stoufferIDs and motifsize != 3: sys.stderr.write( "Warning: 'stoufferIDs' can only be true when 'motifsize=3' in unipartite networks.\n" ) stats.stoufferIDs = False else: stats.stoufferIDs = stoufferIDs if stats.motifsize: if stats.motifsize != motifsize: sys.stderr.write("Error: you're trying to mix motif sizes.\n") sys.exit() else: stats.motifsize = motifsize if stats.networktype: stats.networktype = "unipartite" if stats.weighted != None: if stats.weighted != weighted: sys.stderr.write( "Warning: you're trying to mix two different motif analyses (weighted and not weighted). Be careful!\n" ) stats.weighted = weighted if fweight == None: fweight = default_fweight # parameterize the analysis web.MotifSize = motifsize web.Randomize = 0 web.UseMetropolis = 0 if len(stats.motifs) == 0: web.NRandomizations = 0 web.UseMetropolis = 0 motif_stats(web, stats, allmotifs) web.MaxMembersListSz = max([stats.motifs[x].real for x in stats.motifs]) + 1 #TODO I can also run this inside participation if stats.weighted: weighted_motif_stats(web, stats, fweight, allmotifs) #check if this function has already been run if len(stats.nodes[stats.nodes.keys()[0]].motifs) != 0: for x in stats.nodes.keys(): stats.nodes[x].motifs = dict() if len(stats.links[stats.links.keys()[0]].motifs) != 0: for x in stats.links.keys(): stats.links[x].motifs = dict() return participation_stats(web, stats, links, allmotifs, fweight)