Exemple #1
0
def plot_2d(property):

    # Plot average degree on 2d-graph
    figure()
    for g in GRAPH_TYPES:

        fname = '%s/%s_%sdeg%d.txt' % (results_dir,
                                       snap.GetAttributeAbbr(property),
                                       snap.GetGraphAbbr(g), AVG_DEG)
        A = loadtxt(fname)
        A = sort(A, 0)
        Y = A[:, -1]  # Last column
        X = A[:, :-1]  # Columns 0-(n-1)

        loglog(X[:, 0], Y, 'o', label=snap.GetGraphDesc(g))

        legend(loc='lower right')
        xlabel('Num Nodes (d_avg = %.1f)' % AVG_DEG)
        ylabel('time')
        title('%s runtime (avg degree = %d)' %
              (snap.GetAttributeDesc(property), AVG_DEG))
        pname = '%s/plot2d_%s.png' % (results_dir,
                                      snap.GetAttributeAbbr(property))
        print "Saving figure %s" % pname
        savefig(pname)
Exemple #2
0
def plot_3d(property):

    import mpl_toolkits.mplot3d.axes3d as p3
    fig3d = figure()
    ax = fig3d.add_subplot(111, projection='3d')

    for g in GRAPH_TYPES:
        fname = '%s/%s_%s.txt' % (results_dir, snap.GetAttributeAbbr(property),
                                  snap.GetGraphAbbr(g))

        if not os.path.exists(fname):
            print "No such file: %s" % fname
            return

        A = loadtxt(fname)
        A = sort(A, 0)
        Y = A[:, -1]  # Last column
        X = A[:, :-1]  # Columns 0-(n-1)
        #      savetxt(fname+'_sorted.txt', A)

        Nodes = X[:, 0]
        Edges = X[:, 1]
        ax.plot(Nodes,
                Edges,
                Y,
                'o',
                label="%s-%s" %
                (snap.GetAttributeAbbr(property), snap.GetGraphAbbr(g)))

    ax.set_xlabel('# of nodes', fontsize=9)
    ax.set_ylabel('# of edges', fontsize=9)
    ax.set_zlabel('Run time %s (sec)' % snap.GetAttributeAbbr(property),
                  fontsize=9)
    ax.legend()

    #  ax.set_xlim3d([0, 10**max_nodes_exponent])
    #  ax.set_ylim3d([0, 10**max_nodes_exponent*AVG_DEGREE_RANGE[1]])
    #  ax.set_zlim3d([0, max(Y)])
    #  ax.set_xscale('log')
    #  ax.w_xaxis.set_scale('log')
    #  ax.w_yaxis.set_scale('log')
    #  ax.set_zscale('log')
    #  ax.auto_scale_xyz([0, max(Nodes)], [0, max(Edges)], [0, max(Y)])
    #  ax.title("%s run time" % snap.GetAttributeAbbr(property))
    pname = '%s/plot3d_%s.png' % (results_dir, snap.GetAttributeAbbr(property))
    print "Saving figure %s" % pname

    fig3d.savefig(pname)
Exemple #3
0
def calc_stats():

    for g in GRAPH_TYPES:

        for e in range(min_nodes_exponent, max_nodes_exponent + 1):

            # Random number of nodes of degree i
            NNodes = randrange(10**e, 10**(e + 1))

            for avg in DEGREE_TYPES:

                if avg:
                    # Use average degree
                    NEdges = NNodes * AVG_DEG

                else:
                    # Random number of edges (from 1-3x nodes)
                    NEdges = NNodes * choice(AVG_DEGREE_RANGE)

                print "%s graph: NNodes=%.2e, %.2e" % \
                      (snap.GetGraphDesc(g), NNodes, NEdges)

                fname = "%s%s" % (snap.GetGraphAbbr(g),
                                  'deg%d' % AVG_DEG if avg else '')
                # Repeat for all graph types
                for j in PROPERTY_TYPES:
                    print "Calculating %s..." % snap.GetAttributeDesc(j)
                    t = snap.GetStats(NNodes, NEdges, j, g)
                    f = open(
                        '%s/%s_%s.txt' %
                        (results_dir, snap.GetAttributeAbbr(j), fname), 'a')
                    f.write("%d %d %.5f\n" % (NNodes, NEdges, t))

                    f_all = open(
                        '%s/%s_all.txt' %
                        (results_dir, snap.GetAttributeAbbr(j)), 'a')
                    f_all.write("%d %d %.5f\n" % (NNodes, NEdges, t))

        # For each characteristic:
        # Write out test data to same file (linear fit using matlab?)
        # NNodes NEdges Time

    print "-" * 75
Exemple #4
0
def plot_residuals(property):

    # Calculate residuals for all graph types, and combined
    figure()

    # All graphs
    fname = '%s/%s_all.txt' % (results_dir, snap.GetAttributeAbbr(property))

    A = loadtxt(fname)
    A = sort(A, 0)
    Y = A[:, -1]  # Last column
    X = A[:, :-1]  # Columns 0-(n-1)
    #      savetxt(fname+'_sorted.txt', A)

    best_r2 = 0.0
    best_model = None
    best_p = None

    desc = 'all'
    abbr = 'all'

    print "Fitting %s for %s" % (snap.GetAttributeDesc(property), desc)

    fname = '%s/coeff_%s.txt' % (results_dir, snap.GetAttributeAbbr(property))
    f = open(fname, 'w')

    cname = '%s/coeff_%s.txt' % (combined_dir, snap.GetAttributeAbbr(property))
    combined_file = open(cname, 'a+')

    for model in ['poly', 'exp', 'log']:
        # Plot residuals with multiple fitting types
        rsquared, pfinal = plot_fit(X, Y, desc, model)

        f.write("%s, model=%s r2=%.4f pfinal=%s\n" %
                (abbr, model, rsquared, pfinal))

        if (rsquared > best_r2):
            best_r2 = rsquared
            best_model = model
            best_p = pfinal

    title('Residual error for approx. of run-time, %s (%s)' %
          (snap.GetAttributeDesc(property).title(), desc))
    xscale('log')
    yscale('symlog')
    grid(True)
    xlabel('Number of Nodes')
    ylabel('Residual')
    legend(loc='lower right')
    pname = '%s/residuals_%s_%s.png' % (results_dir,
                                        snap.GetAttributeAbbr(property), abbr)
    print "Saving figure %s" % pname
    savefig(pname)
    print "Best model: %s, r2 = %.3f, pfinal: %s" % \
            (best_model, best_r2, repr(best_p))

    # TODO: Get most recent date of data
    print "Best results to %s" % cname
    combined_file.write(
            'hostname=%s, model=%s, type=%s, n=%d, r2=%.4f, pfinal=%s\n' % \
            (hostname, best_model, abbr, len(X), best_r2,
             ["%.4e" % p for p in best_p]))