import networkx as nx
import math as math
import numpy as np
import matplotlib.pyplot as plt


#Partitionfile
partitionfile = "data/partitions/final_partitions_p100_200_0.2.csv"
project = "584"

#Read in Networks    
FF_all = nx.read_edgelist('data/networks/%s_FF.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph())
AT_all = nx.read_edgelist('data/networks/%s_solr_AT.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph()) 
RT_all = nx.read_edgelist('data/networks/%s_solr_RT.edgelist' % project, nodetype=str, data=(('weight',float),),create_using=nx.DiGraph())

# Read in the partitions
tmp = hp.get_partition(partitionfile)
partitions = tmp[0]
groups = tmp[1]

#thresholds
THRESHOLDS = range(1,50)

#How much information do the ties carry according to their strength
result = []

for partition in partitions:
    # Some tie strengths
    thresholds = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    for threshold in thresholds:   
        at_edges = []
        for n,nbrs in AT.adjacency_iter():
            for nbr,eattr in nbrs.items():
                data=eattr['weight'] 
                if data==threshold: #if the ties have a specific strength
                    at_edges.append((n,nbr,data)) # create a tuple of from_node,to_node,strength
        
        rt_edges = []
        for edge in at_edges:
            try:
                value = RT[edge[0]][edge[1]]['weight'] #if I can find this same pair of nodes in the RT graph capture how many retweets have been exchanged here.
            except KeyError:
                value = 0
            if value > 0:
                rt_edges.append((edge[0],edge[1],value))    #if retweets have been exchanged between those actors add them to the rt edges
        result.append([len(at_edges), math.fsum([x[2] for x in rt_edges]),threshold]) # sum up over the retweets and save the result
        
        
    #Get the "0" strong tie which is when a retweet happend although there is no at_tie (in either direction)
    AT_undir = nx.Graph(AT) # We make it undirected to search for @replies in both directions
    rt_0_edges = []
    for n,nbrs in RT.adjacency_iter():
            for nbr,eattr in nbrs.items():
                data=eattr['weight']
                try:
                    value = AT_undir[n][nbr]['weight']
                except KeyError:
                    rt_0_edges.append((n,nbr,data))
    
    #insert our results to the array as the first datapoints
    result.insert(0,[0,math.fsum([x[2] for x in rt_0_edges]),0])
    thresholds.insert(0,0) 
    #percentages = np.array([rt[1] for rt in result],dtype="float32")/np.array([at[0] for at in result]) # convert it to floats for division

#Plot AT and RT in same direction
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(thresholds, [at[0] for at in result],'b-', label='# of AT ties with strength x')
ax1.plot(thresholds, [at[1] for at in result], 'g-', label='# of retweets flowing through these ties')
ax1.legend(loc=2)
#ax2 = ax1.twinx()
#ax2.plot(thresholds, percentages, 'r-', label='% of #RT/#AT')
#ax2.legend()
plt.savefig("results/individual_edges.png")
plt.close()

