'''
Created on 27/08/2014

@author: javgar119
'''
# general imports
from pprint import pprint
from project_1 import *
from hw_1 import *
import matplotlib.pyplot as plt
import networkx as nx
###################################
# Code for loading citation graph


def load_graph(graph_path):
    """
    Function that loads a graph given the URL
    for a text representation of the graph

    Returns a dictionary that models a graph
    """

    graph_file = open(graph_path)
    graph_text = graph_file.read()
    graph_lines = graph_text.split('\n')
    graph_lines = graph_lines[ :-1]


    print("Loaded graph with",
          len(graph_lines), "nodes")

    answer_graph = {}
    for line in graph_lines:
        neighbors = line.split(' ')
        if neighbors[0] != '':
            node = int(neighbors[0])
            answer_graph[node] = set([])
        for neighbor in neighbors[1 :-1]:
            answer_graph[node].add(int(neighbor))

    return answer_graph

def compare_several_distr():
    """
    debugging
    """

    node = 5000

    prob = 0.1
    step = 0.1
    result = {}
    while prob <= 0.6:
        nx_graph = nx.fast_gnp_random_graph(node, prob, directed=True)
        digraph = nx.to_dict_of_lists(nx_graph)
        norm_dist = normal_degree_distribution(digraph, 'in')
        result[prob] = norm_dist
        prob += step
        prob = round(prob, 1)
        print(prob)
    # charting

    x1 = list(result[0.1].keys())
    y1 = list(result[0.1].values())

    x2 = list(result[0.2].keys())
    y2 = list(result[0.2].values())

    x3 = list(result[0.3].keys())
    y3 = list(result[0.3].values())

    x4 = list(result[0.4].keys())
    y4 = list(result[0.4].values())

    x5 = list(result[0.5].keys())
    y5 = list(result[0.5].values())

    x6 = list(result[0.6].keys())
    y6 = list(result[0.6].values())

    plt.loglog(x1, y1, 'ro',
               x2, y2, '^g',
               x3, y3, 'sr',
               x4, y4, 'r',
               x5, y5, 'bo',
               x6, y6, 'yo')

    plt.show()

def w1_q1():
    """
    plot of normilized in-degree distribution
    of citation graph
    """
    # FILE_PATH = '/Users/Javi/Documents/GIT_luna/Algorithmic_Thinking/data/alg_phys-cite.txt'
    FILE_PATH = 'C:/Users/javgar119/Documents/Python/LocalGit/Algorithmic_Thinking/data/alg_phys-cite.txt'
    citation_graph = load_graph(FILE_PATH)

    normal_citation = normal_degree_distribution(citation_graph, 'in')

    plt.loglog(list(normal_citation.keys()), list(normal_citation.values()),
               'yo', label='n:27770')

    plt.title('Log10 chart\nDistribution in-degree for Citation Graph')
    plt.xlabel('degree')
    plt.ylabel('frequency')
    plt.yscale('log')
    plt.xscale('log')
    plt.grid(True)
    plt.legend()
    plt.show()
def w1_q2():
    """
    question2
    """
    nx_graph = nx.fast_gnp_random_graph(5000, 0.1, directed=True)
    nx_graph = nx.to_dict_of_lists(nx_graph)


    norm_dist = normal_degree_distribution(nx_graph, 'in')
    plt.loglog(list(norm_dist.keys()), list(norm_dist.values()),
               'co', label='n:5000 p:0.1')
    plt.title('Log10 chart\nDistribution in-degrees ER Algorithm')
    plt.xlabel('degree')
    plt.ylabel('frequency')
    plt.legend()
    plt.grid(True)
    plt.show()

def w1_q4():
    """
    Question 4
    """

    dpa = dpa_algorith(27770, 13)
    normal_dpa = normal_degree_distribution(dpa, 'in')

    plt.loglog(list(normal_dpa.keys()), list(normal_dpa.values()),
               'mo', label='n=27770 m=13')


    plt.title('Log10 chart\nDistribution in-degrees DPA Algorithm')
    plt.xlabel('degree')
    plt.ylabel('frequency')
    plt.grid(True)
    plt.legend()
    plt.show()


if __name__ == '__main__':
    w1_q1()
    w1_q2()
    w1_q4()




