Beispiel #1
0
def main():
    parser = optparse.OptionParser(
        description='Fits a hierarchical random graph (HRG) model to a network.  Saves the model to a file in graph markup language (GML) format.',
        prog='hrg-fit.py',
        usage='%prog [options] GRAPH_EDGELIST_FILE')

    parser.add_option('-s', '--num-steps', action='store', type=int,
        default=100000,
        help='The number of MCMC steps to take (default=100000).')

    parser.add_option('-t', '--nodetype', action='store', type='choice',
        choices=[int,str],
        default=int,
        help='The type of the nodes in the edgelist file; "int" or "str" (default="int")')

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
        return 1

    G=nx.read_edgelist(args[0], nodetype=options.nodetype)
    name=os.path.splitext(args[0])[0]
    hrg_file = name + '-hrg.gml'
    print("HRG model will be saved as " + hrg_file + ".")

    D=Dendrogram.from_graph(G)

    bestL=initL=D.graph['L']
    prevL=bestL
    bestI=0

    print_status("step", "L", "best L", "MC step", "deltaL")

    for i in range(1, options.num_steps):
        taken=D.monte_carlo_move()
        t = ''
        if taken:
            t = '*'
        if D.graph['L'] > bestL:
            bestL=D.graph['L']
            bestI=i
            nx.write_gml(D, hrg_file)
            print_status("["+str(i)+"]", "%.3f" % bestL, "%.3f" % bestL, t, "%.3f"%D.deltaL)
        elif i % 4096 == 0:
            print_status("["+str(i)+"]", "%.3f" % D.graph['L'], "%.3f" % bestL, t, "%.3f"%D.deltaL)

        prevL=D.graph['L']

        if i % 10 == 0:
            sys.stdout.flush()

    print("Step number of last best fit "+str(bestI) + ".")
    print("HRG model was saved as " + hrg_file + ".")

    return 0
Beispiel #2
0
def main():
    parser = optparse.OptionParser(
        description=
        'Fits a hierarchical random graph (hrg) model to a network.  Saves the model to a file in graph markup language (GML) format.',
        prog='hrg-fit.py',
        usage='%prog [options] GRAPH_EDGELIST_FILE')

    parser.add_option(
        '-s',
        '--num-steps',
        action='store',
        type=int,
        default=100000,
        help='The number of MCMC steps to take (default=100000).')

    parser.add_option(
        '-t',
        '--nodetype',
        action='store',
        type='choice',
        choices=[int, str],
        default=int,
        help=
        'The type of the nodes in the edgelist file; "int" or "str" (default="int")'
    )

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
        return 1

    G = nx.read_edgelist(args[0], nodetype=options.nodetype)
    name = os.path.splitext(args[0])[0]
    hrg_file = name + '-hrg.gml'
    print("hrg model will be saved as " + hrg_file + ".")

    D = Dendrogram.from_graph(G)

    bestL = initL = D.graph['L']
    prevL = bestL
    bestI = 0

    print_status("step", "L", "best L", "MC step", "deltaL")

    for i in range(1, options.num_steps):
        taken = D.monte_carlo_move()
        t = ''
        if taken:
            t = '*'
        if D.graph['L'] > bestL:
            bestL = D.graph['L']
            bestI = i
            nx.write_gml(D, hrg_file)
            print_status("[" + str(i) + "]", "%.3f" % bestL, "%.3f" % bestL, t,
                         "%.3f" % D.deltaL)
        elif i % 4096 == 0:
            print_status("[" + str(i) + "]", "%.3f" % D.graph['L'],
                         "%.3f" % bestL, t, "%.3f" % D.deltaL)

        prevL = D.graph['L']

        if i % 10 == 0:
            sys.stdout.flush()

    print("Step number of last best fit " + str(bestI) + ".")
    print("hrg model was saved as " + hrg_file + ".")

    return 0
Beispiel #3
0
def main():
    parser = OptionParser(
        description=
        "Finds a consensus dendrogram from an HRG model of a network.  Saves the consensus dendrogram to a graph markup language (GML) file.  Saves the histogram of splits in the consensus dendrogram to a file in Python's pickle format.",
        prog='hrg-consensus.py',
        usage='%prog [options] GRAPH_EDGELIST_FILE DENDROGRAM_GML_FILE')

    parser.add_option(
        '-s',
        '--num-samples',
        action='store',
        type=int,
        default=10000,
        help=
        'The number of times to sample the dendrogram\'s splits (default=10000).'
    )

    parser.add_option('-t',
                      '--temperature',
                      action='store',
                      type=float,
                      default=2.0,
                      help='The temperature at which to run (default=2.0).')

    (options, args) = parser.parse_args()

    if len(args) != 2:
        parser.print_help()
        return 1

    graph_edgelist = args[0]
    G = nx.read_edgelist(graph_edgelist, nodetype=int)
    filename = os.path.basename(graph_edgelist)
    G.name = os.path.splitext(filename)[0]

    gml_file = args[1]
    D = Dendrogram.from_gml_file(gml_file, G)

    bestL = initL = D.graph['L']
    bestI = 0

    print_status("step", "L", "best L", "% complete", "consensus size")

    threshold = 1 / (50.0 * G.number_of_nodes())
    burnin = 200 * G.number_of_nodes()
    i = 1

    out = os.path.splitext(graph_edgelist)[0]
    out += '-consensus-temp-%0.2f' % options.temperature
    dendro_file = out + '-dendrogram.gml'
    hist_file = out + '-histogram.dat'
    print("HRG consensus dendrogram will be saved as " + dendro_file)
    print("Split histogram will be saved as " + hist_file)

    while D.num_samples < options.num_samples:
        taken = D.monte_carlo_move(T=options.temperature, debug=False)

        if i > burnin and random.random() < threshold:
            D.sample_splits()

        t = ''
        if taken:
            t = '*'
        if D.graph['L'] > bestL:
            bestL = D.graph['L']

        if i % 4096 == 0:
            nsplits = D.num_samples
            pct_complete = 100 * D.num_samples / float(options.num_samples)
            print_status("[" + str(i) + "]", "%.3f" % D.graph['L'],
                         "%.3f" % bestL, "%8.2f" % pct_complete,
                         "%10d" % nsplits)

        if i % 10 == 0:
            sys.stdout.flush()

        i += 1

    # Save the histogram to a file.
    D.split_histogram['num_samples'] = D.num_samples
    pickle.dump(D.split_histogram, open(hist_file, mode='wb'))
    del D.split_histogram['num_samples']
    print("Saved split histogram to " + hist_file)

    # Build the consensus dendrogram, save it to a file.
    builder = ConsensusDendrogramBuilder()
    C = builder.build(D.graph_nodes_list, D.split_histogram, D.num_samples)
    nx.write_gml(C, out + '-dendrogram.gml')
    print("Saved consensus dendrogram to " + dendro_file)

    return 0
Beispiel #4
0
def main():
    parser = OptionParser(
        description="Finds a consensus dendrogram from an HRG model of a network.  Saves the consensus dendrogram to a graph markup language (GML) file.  Saves the histogram of splits in the consensus dendrogram to a file in Python's pickle format.",
        prog='hrg-consensus.py',
        usage='%prog [options] GRAPH_EDGELIST_FILE DENDROGRAM_GML_FILE')

    parser.add_option('-s', '--num-samples', action='store', type=int,
        default=10000, help='The number of times to sample the dendrogram\'s splits (default=10000).')

    parser.add_option('-t', '--temperature', action='store', type=float,
        default=2.0, help='The temperature at which to run (default=2.0).')

    (options, args) = parser.parse_args()

    if len(args) != 2:
        parser.print_help()
        return 1

    graph_edgelist=args[0]
    G=nx.read_edgelist(graph_edgelist, nodetype=int)
    filename=os.path.basename(graph_edgelist)
    G.name=os.path.splitext(filename)[0]

    gml_file=args[1]
    D=Dendrogram.from_gml_file(gml_file, G)

    bestL=initL=D.graph['L']
    bestI=0

    print_status("step", "L", "best L", "% complete", "consensus size")

    threshold = 1/(50.0*G.number_of_nodes())
    burnin = 200*G.number_of_nodes()
    i=1

    out = os.path.splitext(graph_edgelist)[0]
    out += '-consensus-temp-%0.2f' % options.temperature
    dendro_file = out + '-dendrogram.gml'
    hist_file = out + '-histogram.dat'
    print("HRG consensus dendrogram will be saved as " + dendro_file)
    print("Split histogram will be saved as " + hist_file)

    while D.num_samples < options.num_samples:
        taken=D.monte_carlo_move(T=options.temperature, debug=False)

        if i > burnin and random.random() < threshold:
            D.sample_splits()

        t = ''
        if taken:
            t = '*'
        if D.graph['L'] > bestL:
            bestL=D.graph['L']

        if i % 4096 == 0:
            nsplits = D.num_samples
            pct_complete = 100 * D.num_samples / float(options.num_samples)
            print_status(
                "[" + str(i) + "]",
                "%.3f" % D.graph['L'],
                "%.3f" % bestL,
                "%8.2f" % pct_complete,
                "%10d" % nsplits)

        if i % 10 == 0:
            sys.stdout.flush()

        i+=1

    # Save the histogram to a file.
    D.split_histogram['num_samples'] = D.num_samples
    pickle.dump(D.split_histogram, open(hist_file, mode='wb'))
    del D.split_histogram['num_samples']
    print("Saved split histogram to " + hist_file)

    # Build the consensus dendrogram, save it to a file.
    builder = ConsensusDendrogramBuilder()
    C = builder.build(D.graph_nodes_list, D.split_histogram, D.num_samples)
    nx.write_gml(C, out + '-dendrogram.gml')
    print("Saved consensus dendrogram to " + dendro_file)

    return 0