예제 #1
0
def addMessages(G_model, step, V, verbose=False):
    '''Expand network `G_model` up to a volume V. V=Number of messages send = sum of weights on each node'''
    t_list = list()
    lcs_list = list()  #Largest component
    acc_list = list()  #Average clustering coefficient

    n = 0  #Number of messages added
    while n < V:
        G_model.iterate()

        #n = len(list(G_model.G.edges()))
        if n % step == 0:
            t_list.append(n)
            lcs = computeLCS(G_model.G)  #Largest component size
            acc, lccs = computeClusteringCoefficient(
                G_model.G
            )  #Calculate average clustering coefficient and local clustering coefficients
            lcs_list.append(lcs)
            acc_list.append(acc)
            #print("-")
            if verbose == True:
                print("Step: {}, LCS = {}, ACC = {:.3g}".format(n, lcs, acc))

        n += 1
    return G_model, t_list, lcs_list, acc_list
def takeMeasurement(network,verbose):
    '''Returns
    ---
    lcs, acc
    '''
    lcs = computeLCS(network)   #Largest component size
    acc = nx.average_clustering(network)   #Calculate average clustering coefficient
    return lcs, acc
예제 #3
0
    def generateEdgesWithProbability(self, nodeCategoryDict, M, iteration):

        modelG = nx.Graph()
        edgesAdded = []
        LCS = []
        m = 0
        while (m < M):
            startLoopTime = time.time()

            sourceNode = self.allNodes[random.randint(0,
                                                      len(self.allNodes) - 1)]
            modelG.add_node(sourceNode)
            sourceNodeNeighbors = nodeCategoryDict[sourceNode][0]

            #Finding Non-Neighbors
            nodeListCopy = copy.copy(self.allNodes)
            nodeListCopy.remove(sourceNode)
            for neighbors in sourceNodeNeighbors:
                nodeListCopy.remove(neighbors)

            sourceNodeOthers = nodeListCopy
            targetNodeCategory = random.choices(
                [sourceNodeNeighbors, sourceNodeOthers],
                weights=[self.p, 1 - self.p])
            targetNode = targetNodeCategory[0][random.randint(
                0,
                len(targetNodeCategory[0]) - 1)]
            #print("Source Node: {}\nTarget Node: {}".format(sourceNode, targetNode))
            if (targetNode not in modelG[sourceNode].keys()):
                modelG.add_edge(sourceNode, targetNode)
                modelG[sourceNode][targetNode]['weight'] = 1
            else:
                modelG[sourceNode][targetNode]['weight'] += 1
            m += 1
            edgesAdded.append(m)

            if (m % 50 == 0):
                LCS_measured = measure_new.computeLCS(modelG)
                LCS.append(LCS_measured)
                #ACC = nx.average_clustering(modelG)
                self.writeDataToFile(
                    str(iteration) + "LCS(no_of_edges_add) P" + str(Model.p),
                    LCS_measured)
                #self.writeDataToFile(str(iteration)+"ACC(no_of_edges_add) P"+str(Model.p), ACC)
                print("{}/{} Edge generated in {}s".format(
                    m, M,
                    time.time() - startLoopTime))

        edges = modelG.edges()

        return modelG, edges
MONDAY = 1
days = 31

#Day 1 = MONDAY
for count in hist_hour:
    n += 1
    day = n % 7
    hourly_interactions_sum[day] += count
hourly_interactions_av = hourly_interactions_sum / days
#TODO: VERIFY
#TODO: PLOT

#TIME_ARRAY = list()
#
#
#xTicks = list(range(1,31*24+1)) # Days of the month
#hist, bin_edges, patches = plt.hist(interaction_times,bins=31*24)
#
#plt.xticks(bin_edges[:-1], xTicks)
#plt.xlim(xmax=bin_edges[24])
print("DONE")

#%%
label = "Largest Component Size"
from measure_new import computeLCS
lcs = computeLCS(loadedGraph)
print("{} {:.3g}".format(label, lcs))
#%%
label = "Average Clustering Coefficient"
acc = nx.average_clustering(loadedGraph)
print("{} {:.3g}".format(label, acc))
예제 #5
0
    def removingEdges(self, modelG, nodeCategoryDict, iteration):
        nodeList = modelG.nodes()
        M = modelG.number_of_edges()
        m = 0
        W = modelG.size(weight='weight')  #TotalWeight Of Graph

        while (W > m):
            startTime = time.time()
            chosenNode = nodeList[random.randint(0, len(nodeList) - 1)]
            chosenNodesEdgeList = modelG.edges(chosenNode)
            nodesInEdges = [node[1] for node in chosenNodesEdgeList]

            nodeNeighbors = nodeCategoryDict[chosenNode][0]
            #print("chosen node: {}".format(chosenNode))
            neighborNodes = []
            nonNeighborNodes = []

            #Categorizes edges in to edges with neighbors or non-neighbors.
            for nodes in nodesInEdges:
                if (nodes in nodeNeighbors):
                    neighborNodes.append(nodes)
                else:
                    nonNeighborNodes.append(nodes)

            nodeRemoval = int()

            #Removing neighborEdges with probability (1-p) and nonNeighborEdges with p
            #NeighborEdges are more likely to remain, as they were most probable to add.
            if (len(nonNeighborNodes) != 0 and len(neighborNodes) != 0):
                edgeRemovalCategory = random.choices(
                    [neighborNodes, nonNeighborNodes],
                    weights=[1 - self.p, self.p])[0]
                #print("Length of category: {}".format(len(edgeRemovalCategory)))
                #Removing the edge containing (ChosenNode & nodeRemoval)
                nodeRemoval = edgeRemovalCategory[random.randint(
                    0,
                    len(edgeRemovalCategory) - 1)]
            #If no nonNeighborNodes exist remove neighborNode Edges (randomly).
            elif (len(nonNeighborNodes) == 0 and len(neighborNodes) != 0):
                nodeRemoval = random.choice(neighborNodes)
            #If no NeighborNodes exist remove nonNeighborNode Edges (randomly).
            elif (len(nonNeighborNodes) != 0 and len(neighborNodes) == 0):
                nodeRemoval = random.choice(nonNeighborNodes)
            #Else if chosenNode has no neighbors or non-neighbors edges, remove node from network.
            else:
                modelG.remove_node(chosenNode)
                nodeList = modelG.nodes()
                continue

            #print("Edges containing node: {}".format(chosenNodesEdgeList))
            #print("Node Removal: {}".format(nodeRemoval))
            #print("NonNeighbor Nodes: {}\nNeighbor Nodes:{}".format(nonNeighborNodes, neighborNodes))
            #print("removing node: {}".format(nodeRemoval))

            #If nodeWeight is greater than 1 then decrement weight, if weight is 1, remove edge.
            if (modelG[chosenNode][nodeRemoval]['weight'] > 1):
                modelG[chosenNode][nodeRemoval]['weight'] -= 1
            elif (modelG[chosenNode][nodeRemoval]['weight'] == 1):
                modelG.remove_edge(chosenNode, nodeRemoval)

            m += 1  #Incrementing number of edges removed.
            if (m % 50 == 0):
                #ACC = measure_new.computeClusteringCoefficient(modelG, isWeighted=True)[0]
                #self.writeDataToFile(str(iteration)+"ACC(no_of_edges_remove) P"+str(Model.p), ACC)
                LCS = measure_new.computeLCS(modelG)
                self.writeDataToFile(
                    str(iteration) + "LCS(no_of_edges_remove) P" +
                    str(Model.p), LCS)
                print("{}/{} Edges removed in {}s".format(
                    no_of_edges_removed, M,
                    time.time() - startTime))

            no_of_edges_removed = M - modelG.number_of_edges()
            #
            #print("{}/{} Weights removed in {}s".format(m, W, time.time()-startTime))

        if (modelG.number_of_edges() == 0):
            print("All edges have been successfully removed.")

        return modelG