def main():  # type: () -> None
    """Open a plot."""
    datalist = [(1, 2), (2, 3), (5, 4), (8, 3), (9, 2)]
    dataset = {1: 3, 2: 4, 5: 5, 8: 4, 9: 3}

    filename = None

    if SIMPLEGUICS2PYGAME:
        from sys import argv  # pylint: disable=import-outside-toplevel

        if len(argv) == 2:
            filename = argv[1]

    if filename is None:
        simpleplot.plot_scatter('Test plot_scatter', 400, 400, 'x', 'y',
                                (datalist, dataset), ('datalist', 'dataset'))
    else:
        simpleplot.plot_scatter('Test plot_scatter',
                                400,
                                400,
                                'x',
                                'y', (datalist, dataset),
                                ('datalist', 'dataset'),
                                _filename=filename)

    if SIMPLEGUICS2PYGAME and (len(argv) != 2):
        simpleplot._block()  # pylint: disable=protected-access
def plot_indeg_distribution(graph_dictionary, ref_str, plot_type):
    """
    Part 1: graphs the log-log plot of the 
    normalized in-degree distribution for
    the input dictionary called ref_str
    """

    # gets the unnormalized distribution and normalizes it
    graph_dist = deg_analysis.in_degree_distribution(graph_dictionary)

    sum_total = 0.0
    for key in graph_dist.keys():
        sum_total += graph_dist[key]

    graph_dist_norm = {}
    for key in graph_dist.keys():
        graph_dist_norm[key] = graph_dist[key]/sum_total

    # creates the log/log data set
    graph_dist_log = {}
    for key in graph_dist_norm.keys():
        graph_dist_log[math.log(key, 10)] = math.log(graph_dist_norm[key], 10)

    # graph types    
    if plot_type == "LOGLOG":
        simpleplot.plot_scatter("LogLog of Normalized In-Degree Distribution for " + ref_str,
                                800, 600, "Log Degree", "Log Amount", [graph_dist_log])
    elif plot_type == "NORMAL":
        simpleplot.plot_scatter("Normalized In-Degree Distribution for " + ref_str,
                                800, 600, "Degree", "Amount", [graph_dist_norm])
    elif plot_type == "BARS":
        simpleplot.plot_bars("Bar Plot of Normalized In-Degree Distribution for " + ref_str,
                             800, 600, "Degree", "Amount", [graph_dist_norm])
def distribution(graphurl, plot_type=LOGLOG):
    """
    :param void: 
    :return: void:
    The main function to get the indegree distribution 
    LOGLOG plot for a given citation digraph.
    1) Load the citation digraph to dictionary structure 
    2) Get the in_degree distribution for the citation digraph
    3) Normalize the indegree Distribution for the given digraph
    4) Plot the indegree distribution on a LOGLOG type plot
    """
    
    # Step 1
    if isinstance(graphurl, str):
        citation_graph = load_graph(graphurl)
    else:
        citation_graph = graphurl
    
    # Step 2
    in_degdist = in_degree_distribution(citation_graph)
    
    # Step 3
    normalized_dist = normalize_dist(in_degdist)
    
    # Step 4   
    plot = []
    
    for input_val in normalized_dist:
        norm_degree = normalized_dist[input_val]
        if plot_type == LOGLOG:
            plot.append([math.log(input_val), math.log(norm_degree)])
        else:
            plot.append([input_val, norm_degree])
    
    simpleplot.plot_scatter("Degree Distribution", 400, 400, 
                      "log(indegree)", "log(normalized distribution)", [plot])
    
    print "Avg Out Degree For this graph is = ", avgOutDegree(citation_graph)
Example #4
0
# Normalize distributions
normdist_1 = dict_to_normalized_lists(indegdist_1)
normdist_2 = dict_to_normalized_lists(indegdist_2)
normdist_3 = dict_to_normalized_lists(indegdist_3)
normdist_0 = dict_to_normalized_lists(indegdist_0)

# Compute log/log plots from normalized dist lists
plot_1 = build_loglog_plot(normdist_1)
plot_2 = build_loglog_plot(normdist_2)
plot_3 = build_loglog_plot(normdist_3)
plot_0 = build_loglog_plot(normdist_0)

# Scatter plots
simpleplot.plot_scatter(
    "ER Digraphs: Normalized In-degree Distribution (log/log)", 700, 600,
    "log10(In-degree Number)", "log10(Normalized Distribution)",
    [plot_0, plot_1, plot_2, plot_3], ["p = .1", "p = .3", "p = .6", "p = .9"])

## ANSWER
'''
1.) The expected in-degree is not the same for every node in an ER 
graph. The normalized distribution from one in-degree number to the
next is not constant.

2.) The in-degree distribution for an ER digraph plot looks like an inverted 
parabola when probability p is low (below 0.5) and a spike when probability 
p is high (above 0.5). The in-degree numbers increased along with probability 
that an edge would be made between 2 nodes. See attached graph. It appears 
to follow a Poisson distribution. For each of the ER digraphs in the attached file, 
n = 300. It is of note that increasing n from 300 to 700 (not shown) decreased 
the spread of each curve, creating narrower spikes. 
    
#nor_ls.pop(0)
#print nor_ls

#simpleplot.plot_scatter("Log/Log plot of the points of the normalized in_degree distribution (Question 1)", 600, 600, 
#                      "log of in_degree, base is 10", "log of number of nodes, base is 10", [nor_ls])

#simpleplot.plot_scatter("Log/Log plot of the points of the normalized in_degree distribution (Question 4 - DPA algorithm)", 600, 600, 
#                      "log of in_degree, base is 10", "log of number of nodes, base is 10", [nor_ls])


ran_graph = make_directed_graph(2000, 0.1)
#print ran_graph


in_ran_graph = in_degree_distribution(ran_graph)
#print in_ran_graph

in_ls = []
for key in in_ran_graph:
    in_ls.append([math.log(key, 10), math.log(in_ran_graph[key] / 2000., 10)])
    
    
#in_ls.pop(0)
#print nor_ls

simpleplot.plot_scatter("Log/Log plot of the points of the normalized in_degree distribution (Question 2, nodes = 2000, p = 0.1)", 600, 600, 
                        "log of in_degree, base is 10", "log of number of nodes, base is 10", [in_ls])

Example #6
0
            degree_distribution_dict[key] = float(degree_distribution_dict[key])/float(num_nodes)
        return degree_distribution_dict
        #create a dictionary from the degree set initialized with 0 values
        #for node in in_degree_dict.keys():
            #deg_distribution_dict[in_degree_dict[node]] += 1
            #calculating the number of nodes having same degree
        #return deg_distribution_dict
    return {}
    

#loading citation graph from url
citation_graph = alg_load_graph.load_graph(alg_load_graph.CITATION_URL)


def build_plot():
    """
    Build log log plot of normalized indegree distribution
    """
    #calculate in degree distribution (normalized) for citation graph
    distribution_dict = in_degree_distribution(citation_graph)
    #create the log/log plot of normalized indegree distribution
    plot = []
    for input_val in distribution_dict.keys():
        if input_val != 0:
            plot.append([math.log(input_val), math.log(distribution_dict[input_val])])
    return plot

plot1 = build_plot()

simpleplot.plot_scatter("log/log plot of Normalized in-degree distribution(using simpleplot)", 600, 600,"log(indegree) base e", "log(Normalized Distribution) base e", [plot1],['plot of citation graph'])
Example #7
0
        for key in degree_distribution_dict.keys():
            degree_distribution_dict[key] = float(degree_distribution_dict[key])/float(num_nodes)
        return degree_distribution_dict        
    return {}

def DPAalgo(n,m,add):
    m_graph = make_complete_graph(m)
    random_connect = DPA(m)
    for i in xrange(m,n):
        m_graph[i]=random_connect.run_trial(add)
    distribution = in_degree_distribution(m_graph)
    distribution.pop(0)
    return distribution

def build_plot(distribution_dict):
    """
    Build log log plot of normalized indegree distribution
    """
    #create the log/log plot of normalized indegree distribution
    plot = []
    for input_val in distribution_dict.keys():
        if input_val != 0:
            plot.append([math.log(input_val), math.log(distribution_dict[input_val])])
    return plot

citation_graph = alg_load_graph.load_graph(alg_load_graph.CITATION_URL)
plot1 = build_plot(in_degree_distribution(citation_graph))
plot2 = build_plot(DPAalgo(27770,12,12))

simpleplot.plot_scatter("log/log plot of Normalized in-degree distribution(using simpleplot)", 600, 600,"log(indegree) base e", "log(Normalized Distribution) base e", [plot1,plot2],['plot of citation graph','plot of DPA graph (n=27770,m=12)'])
Example #8
0
    dataset2.append(element.y)
z = zip(dataset1, dataset2)

for element in k2:
    dataset3.append(element.x)

for element in k2:
    dataset4.append(element.y)
y = zip(dataset3, dataset4)
                  
for element in k3:
    dataset5.append(element.x)

for element in k3:
    dataset6.append(element.y)
u = zip(dataset5, dataset6)                  
                  
l2.append(m1.x)
l3.append(m1.y)
dataset1_center = zip(l2, l3)

l4.append(m2.x)
l5.append(m2.y)
dataset2_center = zip(l4, l5)
                  
l6.append(m3.x)
l7.append(m3.y)
dataset3_center = zip(l6, l7)                  
simpleplot.plot_scatter('K Means', 1280, 780, 'x', 'y', [z, y,u, dataset1_center, dataset2_center,dataset3_center],
                        ['dataset1', 'dataset2','dataset3','center 1', 'center 2','center3'])
Example #9
0
import simpleplot
import random

dataset = [(i + random.randint(0, 2), i + random.randint(0, 2))
           for i in range(50)]
simpleplot.plot_scatter('Sample', 400, 300, 'x', 'y', [dataset])
if SIMPLEGUICS2PYGAME:
    from sys import version as python_version
    from matplotlib import __version__ as matplotlib_version

    PYTHON_VERSION = "Python " + python_version.split()[0]
    MATPLOTLIB_VERSION = "matplotlib " + matplotlib_version
else:
    PYTHON_VERSION = "CodeSkulptor"  # http://www.codeskulptor.org/
    MATPLOTLIB_VERSION = ""

datalist = [(1, 2), (2, 3), (5, 4), (8, 3), (9, 2)]
dataset = {1: 3, 2: 4, 5: 5, 8: 4, 9: 3}

filename = None

if SIMPLEGUICS2PYGAME:
    from sys import argv

    if len(argv) == 2:
        filename = argv[1]

if filename is None:
    simpleplot.plot_scatter("Test plot_scatter", 400, 400, "x", "y", (datalist, dataset), ("datalist", "dataset"))
else:
    simpleplot.plot_scatter(
        "Test plot_scatter", 400, 400, "x", "y", (datalist, dataset), ("datalist", "dataset"), _filename=filename
    )

if SIMPLEGUICS2PYGAME and (len(argv) != 2):
    simpleplot._block()
Example #11
0

def in_degree_distribution(digraph):
    distr = dict()
    digraph = compute_in_degrees(digraph)
    for i in digraph.values():
        distr[i] = 0
    nodes = len(digraph.keys())
    for node in digraph:
        distr[digraph[node]] = distr[digraph[node]] + 1
    for node in distr:
        distr[node] = float(distr[node]) / nodes
    return distr


#print sum(in_degree_distribution(DPA_Graph(27770,12)).values())
def build_plot():
    plot = {}
    digraph = in_degree_distribution(DPA_Graph(27770, 12))
    for node in digraph:
        if node != 0:
            plot[math.log(node, 10)] = math.log(digraph[node], 10)
    return plot


plot1 = build_plot()
print plot1
simpleplot.plot_scatter("log/log plot of in_degree_distribution of DPA_graph",
                        600, 600, "Number of edges", "Fraction of nodes",
                        [plot1])
Example #12
0
    for index in range(len(normdata)):
        if normdata[index][0] > 0 and normdata[index][1] > 0:
            plot.append([
                math.log(normdata[index][0], 10),
                math.log(normdata[index][1], 10)
            ])
    return plot


# Create DPA graph
graph = create_dpa_graph(28000, 12)
#for key, value in graph.items()[:200]:
#    print str(key) + ":" + str(value)

# Compute in-degree dist of DPA graph
indegdist = in_degree_distribution(graph)

# Normalize distribution
normdist = dict_to_normalized_lists(indegdist)

# Build loglog plot
plot = build_loglog_plot(normdist)

# Plot scatter of graph
simpleplot.plot_scatter(
    "DPA Graph: Normalized In-Degree Distribution (log/log)", 700, 600,
    "log10(In-degree Number)", "log10(Normalized Distribution)", [plot],
    ["n=28000, m=12"])

## ANSWER
## Graph attached, see file AlgThink_App1-4_Plot
Example #13
0

## Write a function to build the log/log plot.
def build_loglog_plot(normdata):
    """
    Build loglog plot from normalized dict data.
    """
    plot = []
    for index in range(len(normdata)):
        if normdata[index][0] > 0 and normdata[index][1] > 0:
            plot.append([
                math.log(normdata[index][0], 10),
                math.log(normdata[index][1], 10)
            ])
    return plot


# Load graph from URL
citation_graph = load_graph(CITATION_URL)
# Compute in-degree distribution of graph
indegdist = in_degree_distribution(citation_graph)
# Normalize the distribution
normdist = dict_to_normalized_lists(indegdist)
# Compute a log/log plot of the points in the distribution
nordisplot = build_loglog_plot(normdist)
simpleplot.plot_scatter(
    "Citation Graph: Normalized In-degree Distribution (log/log)", 700, 600,
    "log10(In-degree Number)", "log10(Normalized Distribution)", [nordisplot])

## ANSWER
## See file AlgThink_App1-1_Plot
    return graph


def logify(in_graph):
    '''Creates a log of the in_graph
    '''
    graph = {}
    for in_degree in in_graph.keys():
        log_in_degree = math.log(in_degree, 10)
        log_dist = math.log(in_graph[in_degree], 10)
        graph[log_in_degree] = log_dist
    return graph


# Part A
#print make_complete_graph(4)
#print compute_in_degrees(EX_GRAPH2)
#print in_degree_distribution(EX_GRAPH2)

# Part B
citation_graph = load_graph(CITATION_URL)
in_degree_dist = in_degree_distribution(citation_graph)
normalized_in_dist = normalize(in_degree_dist)
log_norm_in_dist = logify(normalized_in_dist)

import simpleplot
simpleplot.plot_scatter("Application #1: Question 1", 400, 300,
                        'log # in-degrees (number of citations)',
                        'log dist in_degrees (fraction of citations)',
                        [log_norm_in_dist])
    PYTHON_VERSION = 'Python ' + python_version.split()[0]
    MATPLOTLIB_VERSION = 'matplotlib ' + matplotlib_version
else:
    PYTHON_VERSION = 'CodeSkulptor'  # http://www.codeskulptor.org/
    MATPLOTLIB_VERSION = ''

datalist = [(1, 2), (2, 3), (5, 4), (8, 3), (9, 2)]
dataset = {1: 3, 2: 4, 5: 5, 8: 4, 9: 3}

filename = None

if SIMPLEGUICS2PYGAME:
    from sys import argv

    if len(argv) == 2:
        filename = argv[1]

if filename is None:
    simpleplot.plot_scatter('Test plot_scatter', 400, 400, 'x', 'y',
                            (datalist, dataset), ('datalist', 'dataset'))
else:
    simpleplot.plot_scatter('Test plot_scatter',
                            400,
                            400,
                            'x',
                            'y', (datalist, dataset), ('datalist', 'dataset'),
                            _filename=filename)

if SIMPLEGUICS2PYGAME and (len(argv) != 2):
    simpleplot._block()