graph-tool 45.8 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#! /usr/bin/env python
# (C) 2006 by Tiago de Paula Peixoto <tiago@forked.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
Tiago Peixoto's avatar
Tiago Peixoto committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import sys

sys.path.append(".")

from libgraph_tool import *

from optparse import *
import os
import os.path
import re
import struct
import fcntl
import termios
import gzip
import bz2
import string
import time
import signal
from time import *
37
38
39
40
41
42
43
44
45
46

from math import *
try:
    from scipy import *
    from scipy.special import *
    from scipy.optimize import *
    from scipy.optimize.minpack import *
except ImportError:
    pass

Tiago Peixoto's avatar
Tiago Peixoto committed
47

48
49
50
#import gc # garbage collector
#gc.enable()
#gc.set_debug(gc.DEBUG_LEAK|gc.DEBUG_STATS)
Tiago Peixoto's avatar
Tiago Peixoto committed
51
52
53
54
55
56

prog_info = mod_info()
version_string = "%s %s\nWritten by %s\n\n%s" % (prog_info.name, prog_info.version, \
                                               prog_info.author,prog_info.copyright)

def getheightwidth():
57
58
59
60
61
    height, width = 20, 80
    try:
        height, width = struct.unpack("hhhh", fcntl.ioctl(0, termios.TIOCGWINSZ ,"\000"*8))[0:2]
    except IOError:
        pass
Tiago Peixoto's avatar
Tiago Peixoto committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    return height, width

option_list = list()
class Opt:
    def __init__(self, name, value):
        self.name = name
        self.value = value

def push_option(option, opt_str, value, parser):
    option_list.append(Opt(opt_str.lstrip("--"), value))

parser = OptionParser(usage="%s [options]" % sys.argv[0], version=version_string, \
                      formatter=IndentedHelpFormatter(max_help_position=getheightwidth()[1]/2,width=getheightwidth()[1]))
basic =  parser.add_option_group("Basic options")
basic.add_option("--load", action="callback", callback=push_option, type="string", metavar="FILE", help="load graph from file")
basic.add_option("--save", action="callback", callback=push_option, type="string", metavar="FILE", help="save graph to file")

generation =  parser.add_option_group("Graph generation")
generation.add_option("--correlated-configurational-model", action="callback", type="string", metavar="OPTIONS", callback=push_option, help="generate graph using the configurational model with arbitrary degree correlations. Options are: N, pjk, pjk_ceil, pjk_m, inv_pjk_ceil, corr, corr_ceil, corr_m, inv_corr_ceil, undirected, directed, progress, seed. See documentation for details.")

filtering =  parser.add_option_group("Filtering")
filtering.add_option("--directed", action="callback", callback=push_option, help="treat graph as directed (default)")
filtering.add_option("--undirected", action="callback", callback=push_option, help="treat graph as undirected")
filtering.add_option("--reverse", action="callback", callback=push_option, help="reverse edges")
filtering.add_option("--vertex-filter", action="callback", callback=push_option, type="string", metavar="FILTER RULE", help="set generic vertex filter")
filtering.add_option("--vertex-range-filter", action="callback", callback=push_option, type="string", metavar="PROPERTY|RANGE", help="choose vertex property and range to filter")
filtering.add_option("--reset-vertex-filter", action="callback", callback=push_option, help="remove edge filter")
filtering.add_option("--edge-filter", action="callback", callback=push_option, type="string", metavar="FILTER RULE", help="set generic edge filter")
filtering.add_option("--edge-range-filter", action="callback", callback=push_option, type="string", metavar="PROPERTY|RANGE", help="choose edge property and range to filter")
filtering.add_option("--reset-edge-filter", action="callback", callback=push_option, help="remove edge filter")

modification =  parser.add_option_group("Graph Modification")
94
95
modification.add_option("--edit-vertex-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|EXPRESSION", help="edit the selected vertex property")
modification.add_option("--edit-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|EXPRESSION", help="edit the selected edge property")
Tiago Peixoto's avatar
Tiago Peixoto committed
96
97
98
99
100
101
102
103
modification.add_option("--remove-vertex-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="remove vertex property from graph")
modification.add_option("--remove-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|RANGE", help="remove edge property from graph")
modification.add_option("--insert-vertex-index-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="insert vertex index as property")
modification.add_option("--insert-edge-index-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="insert edge index as property")

statistics =  parser.add_option_group("Basic Statistics")
statistics.add_option("--number-of-vertices", action="callback", callback=push_option, type="string", metavar="FILE", help="get the number of vertices")
statistics.add_option("--number-of-edges", action="callback", callback=push_option, type="string", metavar="FILE", help="get the number of edges")
104
105
statistics.add_option("--vertex-histogram", action="callback", callback=push_option, type="string", metavar="DEGREE|FILE", help="get the vertex degree/property histogram")
statistics.add_option("--edge-histogram", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the edge property histogram")
106
statistics.add_option("--combined-vertex-histogram", action="callback", callback=push_option, type="string", metavar="DEGREE1|DEGREE2|FILE", help="get the combined (DEGREE1,DEGREE2) histogram. Scalar properties are also accepted as DEGREE1 or DEGREE2")
107
statistics.add_option("--distance-histogram", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the distance histogram")
108
109
statistics.add_option("--average-distance", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the averarge distance")
statistics.add_option("--average-harmonic-distance", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the averarge harmonic distance")
110
111
112
statistics.add_option("--sampled-distance-histogram", action="callback", callback=push_option, type="string", metavar="OPTIONS|FILE", help="get the sampled distance histogram. Options are samples, weight (optional), seed (default: from clock)")
statistics.add_option("--average-sampled-distance", action="callback", callback=push_option, type="string", metavar="OPTIONS|FILE", help="get the average sampled distance. Options are samples, weight (optional), seed (default: from clock)")
statistics.add_option("--average-sampled-harmonic-distance", action="callback", callback=push_option, type="string", metavar="OPTIONS|FILE", help="get the average sampled harmonic distance. Options are samples, weight (optional), seed (default: from clock)")
113
statistics.add_option("--label-components", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="label components to PROPERTY")
Tiago Peixoto's avatar
Tiago Peixoto committed
114
statistics.add_option("--label-parallel-edges", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="label parallel edges to PROPERTY")
115
116
statistics.add_option("--average-vertex-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the average of the vertex property")
statistics.add_option("--average-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE", help="get the average of the edge property")
117
statistics.add_option("--reciprocity", action="callback", callback=push_option, type="string", metavar="FILE", help="get the edge reciprocity")
118
statistics.add_option("--minimum-spanning-tree", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]PROPERTY", help="mark the minimum spanning tree edges in PROPERTY")
Tiago Peixoto's avatar
Tiago Peixoto committed
119
statistics.add_option("--line-graph", action="callback", callback=push_option, type="string", metavar="FILE[|FORMAT]", help="save the corresponding line graph to FILE")
Tiago Peixoto's avatar
Tiago Peixoto committed
120
121

correlations =  parser.add_option_group("Correlations")
122
correlations.add_option("--average-combined-vertex-correlation", action="callback", callback=push_option, type="string", metavar="DEGREE1|DEGREE2|FILE", help="get the average of DEGREE2 in function of DEGREE1. Scalar properties are also accepted as DEGREE1 or DEGREE2")
123
124
correlations.add_option("--vertex-correlation-histogram", action="callback", callback=push_option, type="string", metavar="DEGREE1|DEGREE2[|WEIGHT]|FILE", help="get the degree correlation histogram. Scalar properties are also accepted in place of DEGREE. An optional edge weight property can be passed by WEIGHT")
correlations.add_option("--average-nearest-neighbours-correlation", action="callback", callback=push_option, type="string", metavar="ORIGIN-DEGREE|DEGREE[|WEIGHT]|FILE", help="get the average nearest neighbours correlation. An optional edge weight property can be passed by WEIGHT")
125
correlations.add_option("--edge-vertex-correlation-histogram", action="callback", callback=push_option, type="string", metavar="DEGREE1|EDGE-PROP|DEGREE2|FILE", help="get the source degree vs. edge scalar vs. target degree correlation histogram. Scalar properties are also accepted in place of DEGREE-TYPE")
126
correlations.add_option("--assortativity-coefficient", action="callback", callback=push_option, type="string", metavar="DEGREE|FILE", help="get the assortativity coefficient. Scalar properties are also accepted in place of DEGREE")
127
correlations.add_option("--scalar-assortativity-coefficient", action="callback", callback=push_option, type="string", metavar="DEGREE|FILE", help="get the scalar assortativity coefficient. Scalar properties are also accepted in place of DEGREE")
Tiago Peixoto's avatar
Tiago Peixoto committed
128
129
130
131

clustering =  parser.add_option_group("Clustering")
clustering.add_option("--set-local-clustering-to-property", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="set the local clustering coefficient to vertex property")
clustering.add_option("--global-clustering-coefficient", action="callback", callback=push_option, type="string", metavar="FILE", help="get the global clustering coefficient")
132
clustering.add_option("--set-extended-clustering-to-property", action="callback", callback=push_option, type="string", metavar="PREFIX|MAX", help="set the extended clustering coefficients c1 to cMAX to vertex properties PREFIX1 to PREFIXMAX")
Tiago Peixoto's avatar
Tiago Peixoto committed
133
134
135
136
137

layout =  parser.add_option_group("Layout")
layout.add_option("--compute-spring-block-layout", action="callback", callback=push_option, type="string", metavar="ITERATIONS[|SEED]", help="compute the spring block layout")
layout.add_option("--compute-gursoy-atun-layout", action="callback", callback=push_option, type="string", metavar="ITERATIONS[|SEED]", help="compute the Gursoy-Atun layout")

Tiago Peixoto's avatar
Tiago Peixoto committed
138
community =  parser.add_option_group("Community")
Tiago Peixoto's avatar
Tiago Peixoto committed
139
community.add_option("--community-structure", action="callback", callback=push_option, type="string", metavar="PROPERTY|OPTIONS", help="calculate the community structure and assign it to PROPERTY. Options are: g (default: 1.0), N (default: 1000), Tmin (default: 0.01), Tmax (default: 1.0), spins (default: number of vertices), corr_type (default: uncorrelated), weight, seed (default: from clock), verbose, history.  The value of corr_type can be 'random', 'uncorrelated' or 'correlated'.")
140
community.add_option("--modularity", action="callback", callback=push_option, type="string", metavar="PROPERTY[|WEIGHT]|FILE", help="calculate the modularity, given a community partition specified by PROPERTY")
141
community.add_option("--community-graph", action="callback", callback=push_option, type="string", metavar="PROPERTY|FILE[|FORMAT]", help="obtain the graph of communities, given a community partition specified by PROPERTY")
Tiago Peixoto's avatar
Tiago Peixoto committed
142

Tiago Peixoto's avatar
Tiago Peixoto committed
143
layout =  parser.add_option_group("History")
144
145
146
layout.add_option("--for", action="callback", callback=push_option, type="string", metavar="INIT|CONDITION|STEP", help="simplified scripting")
layout.add_option("--history", action="callback", callback=push_option, type="string", metavar="INIT|CONDITION|STEP", help="simplified scripting (does not overwrite previous results)")
layout.add_option("--refresh-rate", type="string", metavar="TIME", default="1h", help="for/history files refresh rate")
Tiago Peixoto's avatar
Tiago Peixoto committed
147

148
149
150
151
152
153
try:
    import optcomplete
    optcomplete.autocomplete(parser)
except ImportError:
    pass

Tiago Peixoto's avatar
Tiago Peixoto committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(options, args) = parser.parse_args()

graph = GraphInterface()

def parse_values(value):
    "this will parse the options with multiple values"
    p = re.compile(r"([^\|'\"]+|[^\|]+'[^']*'|[^\|]+\"[^\"]*\")(?:\||$)")
    values = p.findall(value)
    values = [x.strip() for x in values]
    return values

def get_suboption(suboption, string):
    p = re.compile(r"([^,'\"]+|[^,]+'[^']*'|[^,]+\"[^\"]*\")(?:,|$)")
    opts = p.findall(string)
    for opt in opts:
        if opt.split("=",1)[0].strip() == suboption:
            if "=" in opt:            
                return opt.split("=",1)[1].strip("\"").strip("'")
            else:
                return True
    return False

def degree(name):
    "this will retrieve the degree type from string"
    deg = name
    if name == "in-degree" or name == "in":
        deg = Degree.In
    if name == "out-degree" or name == "out":
        deg = Degree.Out
    if name == "total-degree" or name == "total":
        deg = Degree.Total
    return deg

187
188
def get_mean(hist):
    avg,dev,count = 0.0,0.0,0.0
189
    try:
Tiago Peixoto's avatar
Tiago Peixoto committed
190
191
192
        for k,v in hist.iteritems():
            avg += k*v
            count += v
193
        avg /= count
Tiago Peixoto's avatar
Tiago Peixoto committed
194
195
196
        for k,v in hist.iteritems():
            dev += (k - avg)**2
        dev = sqrt(dev/(count**2))
197
198
    except ZeroDivisionError:
        avg = dev = float("nan") # nans are ok, since graph can be empty
199
200
    return (avg,dev)

Tiago Peixoto's avatar
Tiago Peixoto committed
201
202
203
204
205
206
class OptionError(Exception):
    def __init__(self, option, error):
        self.what = "error parsing option %s: %s" % (option,error)
    def __str__(self):
        return self.what

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def generate_graph(parameters):
    "this option will generate a graph with the given parameters"
    seed = int(time())
    N = 10000
    pjk = "1.0"
    pjk_ceil = "pjk(j,k)"
    m_pjk = "1.0"
    inv_pjk_ceil = ""
    corr = "1.0"
    corr_ceil = "corr(jl,kl,j,k)"
    m_corr = "1.0"
    inv_corr_ceil = "inv_pjk_ceil(p,r)"
    try:
        if get_suboption("N",parameters) != False:
            N = int(get_suboption("N",parameters))
        if get_suboption("pjk",parameters) != False:
            pjk = get_suboption("pjk",parameters)
            corr = "pjk(j,k)"
        if get_suboption("pjk_ceil",parameters) != False:
            pjk_ceil = get_suboption("pjk_ceil",parameters)
        if get_suboption("pjk_m",parameters) != False:
            m_pjk = get_suboption("pjk_m",parameters)
            m_corr = m_pjk
        if get_suboption("inv_pjk_ceil",parameters) != False:
            inv_pjk_ceil = get_suboption("inv_pjk_ceil",parameters)
        if get_suboption("corr",parameters) != False:
            corr = get_suboption("corr",parameters)
        if get_suboption("corr_ceil",parameters) != False:
            corr_ceil = get_suboption("corr_ceil",parameters)
        if get_suboption("corr_m",parameters) != False:
            corr_m = get_suboption("corr_m",parameters)
        if get_suboption("inv_corr_ceil",parameters) != False:
            inv_corr_ceil = get_suboption("inv_corr_ceil",parameters)            
        undirected = get_suboption("undirected",parameters)
        verbose = get_suboption("progress", parameters)
        if get_suboption("seed",parameters) != False:
            seed = int(get_suboption("seed",parameters))
    except ValueError, e:
        raise OptionError(opt.name, e)
246

247
    # some predefined functions
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    variables = globals()
    
    def inv_poisson(p,m):
        return round(fsolve(lambda k,l: gammaincc(k,l)-p, m, (m))-0.5+1e-15)
    variables["inv_poisson"] = inv_poisson
    
    def inv_exponential(p,m):
        return round(log(1-p)/log(float(m)/(m+1))-0.5+1e-15)
    variables["inv_exponential"] = inv_exponential
    
    def inv_power_law(p,b):
        return round((1-p)**(-1/(b-1)) - 1)
    variables["inv_power_law"] = inv_power_law
    
    def step(x):
        if x >= 0:
            return 1.0
        else:
            return 0.0
    variables["step"] = step

269
    # read main funtions
270
271
272
273
274
275
276
277
278
279
280
    def pjk_f(j,k):
        return eval("%s" % pjk, variables, locals())
    variables["pjk"] = pjk_f
    
    def pjk_ceil_f(j,k):
        return eval("%s" % pjk_ceil, variables, locals())
    variables["pjk_ceil"] = pjk_ceil_f

    m_pjk = float(m_pjk)
    variables["m_pjk"] = m_pjk
    
281
282
283
    if "file:" in inv_pjk_ceil:
        exec open(inv_pjk_ceil.replace("file:","").strip()).read() in variables
    else:
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
        def inv_pjk_ceil_f(p,r):
            variables["p"] = p
            variables["r"] = r
            retval = eval("%s" % inv_pjk_ceil, variables, locals())
            del(variables["p"])
            del(variables["r"])
            return (int(round(retval[0])), int(round(retval[1])))
        variables["inv_pjk_ceil"] = inv_pjk_ceil_f
        
    def corr_f(jl,kl,j,k):
        return eval("%s" % corr, variables, locals())
    variables["corr"] = corr_f
    
    def corr_ceil_f(jl,kl,j,k):
        return eval("%s" % corr_ceil, variables, locals())
    variables["corr_ceil"] = corr_ceil_f
    
    m_corr = float(m_corr)
    variables["m_corr"] = m_corr
        
304
305
306
    if "file:" in inv_corr_ceil:
        exec open(inv_corr_ceil.replace("file:","").strip()).read() in variables
    else:
307
308
309
310
311
        def inv_corr_ceil_f(p,r,j,k):
            retval = eval("%s" % inv_corr_ceil, variables, locals())
            return (int(round(retval[0])), int(round(retval[1])))
        variables["inv_corr_ceil"] = inv_corr_ceil_f

312
313
314
315
    graph.GenerateCorrelatedConfigurationalModel(N, variables["pjk"], variables["pjk_ceil"], variables["inv_pjk_ceil"], m_pjk, variables["corr"],
                                                 variables["corr_ceil"], variables["inv_corr_ceil"], m_corr, undirected, seed, verbose)


Tiago Peixoto's avatar
Tiago Peixoto committed
316
def parse_option(opt, just_file=False):
Tiago Peixoto's avatar
Tiago Peixoto committed
317
    "this will execute an option, and return either None, or a tuple with the result and the respective file name, if it exists"
Tiago Peixoto's avatar
Tiago Peixoto committed
318
    if opt.name == "load":
319
320
321
        values = parse_values(opt.value)
        if len(values) > 2 or len(values) < 1:
            raise OptionError(opt.name, "invalid value '$s'" % opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
322
323
        if just_file:
            return None
324
325
        if len(values) == 1:
            graph.ReadFromFile(values[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
326
327
        else:
            graph.ReadFromFile(values[0], values[1])
Tiago Peixoto's avatar
Tiago Peixoto committed
328
    elif opt.name == "save":
329
        values = parse_values(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
330
        if len(values) > 2 or len(values) < 1:
331
            raise OptionError(opt.name, "invalid value '$s'" % opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
332
333
        if just_file:
            return None
334
335
        if len(values) == 1:
            graph.WriteToFile(values[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
336
337
        else:
            graph.WriteToFile(values[0], values[1])
Tiago Peixoto's avatar
Tiago Peixoto committed
338
339
340
    elif opt.name == "correlated-configurational-model":
        if just_file:
            return None
341
        generate_graph(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
342
343
344
345
346
347
348
349
    elif opt.name == "number-of-vertices":
        if just_file:
            return opt.value
        return (graph.GetNumberOfVertices(), opt.value)
    elif opt.name == "number-of-edges":
        if just_file:
            return opt.value
        return (graph.GetNumberOfEdges(), opt.value)
350
351
352
353
    elif opt.name == "combined-vertex-histogram":
        values = parse_values(opt.value)
        if len(values) != 3:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
354
        if just_file:
355
356
            return values[2]
        return (graph.GetCombinedVertexHistogram(degree(values[0]),degree(values[1])), values[2])
357
358
359
360
361
362
363
    elif opt.name == "average-combined-vertex-correlation":
        values = parse_values(opt.value)
        if len(values) != 3:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if just_file:
            return values[2]
        return (graph.GetAverageCombinedVertexCorrelation(degree(values[0]),degree(values[1])), values[2])
364
365
366
367
368
369
370
371
372
373
374
    elif opt.name == "distance-histogram":
        values = parse_values(opt.value)
        if len(values) > 2 or len(values) < 1:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        file_name, weight = values[0],""
        if len(values) > 1:
            weight = values[0]
            file_name = values[1]
        if just_file:
            return file_name
        return (graph.GetDistanceHistogram(weight), file_name)
Tiago Peixoto's avatar
Tiago Peixoto committed
375
    elif opt.name == "average-distance":
376
377
378
379
380
381
382
        values = parse_values(opt.value)
        if len(values) > 2 or len(values) < 1:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        file_name, weight = values[0],""
        if len(values) > 1:
            weight = values[0]
            file_name = values[1]
Tiago Peixoto's avatar
Tiago Peixoto committed
383
        if just_file:
384
            return file_name
385
        return ("%f\t%f" % get_mean(graph.GetDistanceHistogram(weight)), file_name)
Tiago Peixoto's avatar
Tiago Peixoto committed
386
    elif opt.name == "average-harmonic-distance":
387
388
389
390
391
392
393
        values = parse_values(opt.value)
        if len(values) > 2 or len(values) < 1:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        file_name, weight = values[0],""
        if len(values) > 1:
            weight = values[0]
            file_name = values[1]
Tiago Peixoto's avatar
Tiago Peixoto committed
394
        if just_file:
395
            return file_name
396
        hist = graph.GetDistanceHistogram(weight)
Tiago Peixoto's avatar
Tiago Peixoto committed
397
398
        avg, err = get_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
        return ("%f\t%f" % (1.0/avg,err/(avg**2)) , file_name)
399
400
    elif opt.name == "sampled-distance-histogram":
        values = parse_values(opt.value)
401
        if len(values) != 2:
402
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
403
404
405
406
407
408
409
410
411
412
        if get_suboption("weight", values[0]) != False:
            weight = get_suboption("weight", values[0])
        else:
            weight = ""
        if get_suboption("samples", values[0]) != False:
            samples = int(get_suboption("samples", values[0]))
        else:
            raise OptionError(opt.name, "you must provide the number of samples")
        if get_suboption("seed", values[0]) != False:
            seed = int(get_suboption("seed", values[0]))
413
        else:
414
            seed = int(time())
415
        if just_file:
416
417
            return values[1]
        return (graph.GetSampledDistanceHistogram(weight, samples, seed), values[1])
418
419
    elif opt.name == "average-sampled-distance":
        values = parse_values(opt.value)
420
        if len(values) != 2:
421
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
422
423
424
425
426
427
        if get_suboption("weight", values[0]) != False:
            weight = get_suboption("weight", values[0])
        else:
            weight = ""
        if get_suboption("samples", values[0]) != False:
            samples = int(get_suboption("samples", values[0]))
428
        else:
429
430
431
432
433
            raise OptionError(opt.name, "you must provide the number of samples")
        if get_suboption("seed", values[0]) != False:
            seed = int(get_suboption("seed", values[0]))
        else:
            seed = int(time())
434
        if just_file:
435
436
            return values[1]
        return ("%f\t%f" % get_mean(graph.GetSampledDistanceHistogram(weight, samples, seed)), values[1])
437
438
    elif opt.name == "average-sampled-harmonic-distance":
        values = parse_values(opt.value)
439
        if len(values) != 2:
440
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
441
442
        if get_suboption("weight", values[0]) != False:
            weight = get_suboption("weight", values[0])
443
        else:
444
445
446
447
448
449
450
451
452
            weight = ""
        if get_suboption("samples", values[0]) != False:
            samples = int(get_suboption("samples", values[0]))
        else:
            raise OptionError(opt.name, "you must provide the number of samples")
        if get_suboption("seed", values[0]) != False:
            seed = int(get_suboption("seed", values[0]))
        else:
            seed = int(time())
453
        if just_file:
454
            return values[1]
455
        hist = graph.GetSampledDistanceHistogram(weight, samples, seed)
Tiago Peixoto's avatar
Tiago Peixoto committed
456
        avg, err = get_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
457
        return ("%f\t%f" % (1.0/avg,1.0/err) , values[1])
458
    elif opt.name == "label-components":
Tiago Peixoto's avatar
Tiago Peixoto committed
459
        if just_file:
460
461
            return
        graph.LabelComponents(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
462
463
464
465
    elif opt.name == "label-parallel-edges":
        if just_file:
            return
        graph.LabelParallelEdges(opt.value)
466
    elif opt.name == "vertex-histogram":
Tiago Peixoto's avatar
Tiago Peixoto committed
467
468
469
470
471
472
        values = parse_values(opt.value)
        if len(values) != 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        deg = degree(values[0])
        if just_file:
            return values[1]
473
474
475
476
477
478
479
480
        return (graph.GetVertexHistogram(deg), values[1])
    elif opt.name == "edge-histogram":
        values = parse_values(opt.value)
        if len(values) != 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if just_file:
            return values[1]
        return (graph.GetEdgeHistogram(values[0]), values[1])
481
482
483
484
    elif opt.name == "reciprocity":
        if just_file:
            return opt.value
        return (graph.GetReciprocity(), opt.value)
485
486
487
488
489
490
491
492
493
494
495
    elif opt.name == "minimum-spanning-tree":
        values = parse_values(opt.value)
        if len(values) > 2 or len(values) < 1:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if just_file:
            return None
        prop, weight = values[0],""
        if len(values) > 1:
            weight = values[0]
            prop = values[1]
        graph.GetMinimumSpanningTree(weight,prop)
496
497
498
499
500
501
502
503
504
505
506
    elif opt.name == "line-graph":
        values = parse_values(opt.value)
        if len(values) < 1 or len(values) > 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if just_file:
            return None
        if len(values) > 1:
            format = values[1]
        else:
            format = ""
        graph.GetLineGraph(values[0], format)
507
    elif opt.name == "vertex-correlation-histogram":
Tiago Peixoto's avatar
Tiago Peixoto committed
508
        values = parse_values(opt.value)
509
        if len(values) < 3 or len(values) > 4:
Tiago Peixoto's avatar
Tiago Peixoto committed
510
511
512
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        deg1 = degree(values[0])
        deg2 = degree(values[1])
513
514
515
516
517
518
        if len(values) == 4:
            weight = values[2]
            file_name = values[3]
        else:
            weight = ""
            file_name = values[2]
Tiago Peixoto's avatar
Tiago Peixoto committed
519
        if just_file:
520
521
            return file_name
        return (graph.GetVertexCorrelationHistogram(deg1, deg2, weight), file_name)
522
    elif opt.name == "edge-vertex-correlation-histogram":
Tiago Peixoto's avatar
Tiago Peixoto committed
523
524
525
526
527
528
529
        values = parse_values(opt.value)
        if len(values) != 4:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        deg1 = degree(values[0])
        deg2 = degree(values[2])
        if just_file:
            return values[3]
530
531
        return (graph.GetEdgeVertexCorrelationHistogram(deg1,values[1],deg2), values[3])
    elif opt.name == "average-nearest-neighbours-correlation":
Tiago Peixoto's avatar
Tiago Peixoto committed
532
        values = parse_values(opt.value)
533
        if len(values) < 3 or len(values) > 4:
Tiago Peixoto's avatar
Tiago Peixoto committed
534
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
535
536
        deg1 = degree(values[0])
        deg2 = degree(values[1])
537
538
539
540
541
542
        if len(values) == 4:
            weight = values[2]
            file_name = values[3]
        else:
            weight = ""
            file_name = values[2]
Tiago Peixoto's avatar
Tiago Peixoto committed
543
        if just_file:
544
545
            return file_name
        return (graph.GetAverageNearestNeighboursCorrelation(deg1, deg2, weight), file_name)
Tiago Peixoto's avatar
Tiago Peixoto committed
546
547
    elif opt.name == "assortativity-coefficient":
        values = parse_values(opt.value)
548
        if len(values) != 2:
Tiago Peixoto's avatar
Tiago Peixoto committed
549
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
550
        deg = degree(values[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
551
        if just_file:
552
            return values[1]
553
554
555
556
557
558
559
560
561
562
563
564
565
        (r,err) = graph.GetAssortativityCoefficient(deg)
        ret = "%f\t%f" % (r,err)
        return (ret, values[1])
    elif opt.name == "scalar-assortativity-coefficient":
        values = parse_values(opt.value)
        if len(values) != 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        deg = degree(values[0])
        if just_file:
            return values[1]
        (r,err) = graph.GetScalarAssortativityCoefficient(deg)
        ret = "%f\t%f" % (r,err)
        return (ret, values[1])
566
567
568
569
570
571
572
573
    elif opt.name == "average-vertex-property" or opt.name == "average-edge-property":
        values = parse_values(opt.value)
        if len(values) != 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if opt.name == "average-vertex-property":
            deg = degree(values[0])
            hist = graph.GetVertexHistogram(deg)
        else:
574
            hist = graph.GetEdgeHistogram(values[0])
575
        ret = "%f\t%f" % get_mean(hist)
Tiago Peixoto's avatar
Tiago Peixoto committed
576
        if just_file:
577
578
            return values[1]
        return (ret,values[1])
Tiago Peixoto's avatar
Tiago Peixoto committed
579
580
581
    elif opt.name == "global-clustering-coefficient":
        if just_file:
            return opt.value
582
583
584
        (avg, dev) = graph.GetGlobalClustering()
        ret = "%f\t%f" % (avg, dev)
        return (ret, opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
585
586
587
588
    elif opt.name == "set-local-clustering-to-property":
        if just_file:
            return None
        graph.SetLocalClusteringToProperty(opt.value)
589
590
591
592
593
594
    elif opt.name == "set-extended-clustering-to-property":
        if just_file:
            return None
        values = parse_values(opt.value)
        if len(values) == 0 or len(values) > 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
595
        graph.SetExtendedClusteringToProperty(values[0],int(values[1]))
Tiago Peixoto's avatar
Tiago Peixoto committed
596
597
598
599
    elif opt.name == "community-structure":
        if just_file:
            return None
        values = parse_values(opt.value)
600
        if len(values) < 1 or len(values) > 2:
Tiago Peixoto's avatar
Tiago Peixoto committed
601
602
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        prop = values[0]
603
604
        if len(values) > 1:
            param = values[1]
Tiago Peixoto's avatar
Tiago Peixoto committed
605
        else:
606
            param = ""
Tiago Peixoto's avatar
Tiago Peixoto committed
607
        try:
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
            if  get_suboption("g", param) != False:
                g = float( get_suboption("g", param))
            else:
                g = 1.0
            if  get_suboption("N", param) != False:
                N = int( get_suboption("N", param))
            else:
                N = 1000
            if  get_suboption("Tmin", param) != False:
                Tmin = float( get_suboption("Tmin", param))
            else:
                Tmin = 0.0
            if  get_suboption("Tmax", param) != False:
                Tmax = float( get_suboption("Tmax", param))
            else:
                Tmax = 100.0
624
625
626
627
            if  get_suboption("spins", param) != False:
                spins = int( get_suboption("spins", param))
            else:
                spins = 0
628
629
            if  get_suboption("corr_type", param) != False:
                if  get_suboption("corr_type", param)  == "random":
630
                    corr = CommCorr.ErdosReyni
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
                elif  get_suboption("corr_type", param)  == "uncorrelated":
                    corr = CommCorr.Uncorrelated
                elif  get_suboption("corr_type", param)  == "correlated":
                    corr = CommCorr.Correlated
                else:
                    raise OptionError(opt.name, "invalid correlation type value '%s'" %  get_suboption("corr_type", param) )
            else:
                corr = CommCorr.Uncorrelated
            if  get_suboption("seed", param) != False:
                seed = int( get_suboption("seed", param))
            else:
                seed = int(clock())
            if  get_suboption("weight", param) != False:
                weight =  get_suboption("weight", param)
            else:
                weight = ""
            verbose =  get_suboption("verbose", param) != False
Tiago Peixoto's avatar
Tiago Peixoto committed
648
649
650
651
652
            if  get_suboption("history", param) != False:
                history =  get_suboption("history", param)
            else:
                history = ""
                                                    
Tiago Peixoto's avatar
Tiago Peixoto committed
653
654
        except ValueError,e:
            raise OptionError(opt.name, "invalid value '%s': %s" % (opt.value, e))
Tiago Peixoto's avatar
Tiago Peixoto committed
655
        graph.GetCommunityStructure(g, corr, N, Tmin, Tmax, spins, seed, verbose, history, weight, prop)
656
    elif opt.name == "modularity":
Tiago Peixoto's avatar
Tiago Peixoto committed
657
658
659
660
661
662
663
664
665
666
        values = parse_values(opt.value)
        if len(values) < 2 or len(values) > 3:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        prop, weight, file_name = values[0], "", values[1]
        if len(values) > 2:
            weight = values[1]
            file_name = values[2]
        if just_file:
            return file_name
        return (graph.GetModularity(weight, prop), file_name)
667
668
669
670
671
672
673
674
675
676
677
678
    elif opt.name == "community-graph":
        values = parse_values(opt.value)
        if len(values) < 2 or len(values) > 3:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        prop, file_name = values[0], values[1]
        if len(values) > 2:
            format = values[2]
        else:
            format = ""
        if just_file:
            return
        return (graph.GetCommunityNetwork(prop, file_name, format))
Tiago Peixoto's avatar
Tiago Peixoto committed
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
    elif opt.name == "compute-spring-block-layout":
        if just_file:
            return None
        values = parse_values(opt.value)
        if len(values) == 0 or len(values) > 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        iterations,seed = 0,0
        try:
            iterations = int(values[0])
            if len(values) > 1:
                seed = int(values[1])
            else:
                seed = int(time())
        except ValueError:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        graph.ComputeGraphLayoutSpringBlock(iterations,seed)
    elif opt.name == "compute-gursoy-atun-layout":
        if just_file:
            return None
        values = parse_values(opt.value)
        if len(values) == 0 or len(values) > 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        iterations,seed = 0,0
        try:
            iterations = int(values[0])
            if len(values) > 1:
                seed = int(values[1])
            else:
                seed = int(time())
        except ValueError:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        graph.ComputeGraphLayoutGursoy(iterations,seed)
    elif opt.name == "vertex-filter" or opt.name == "edge-filter":
        if just_file:
            return None
        filter_vars = dict()
715
716
        def filter_function():
            return not (eval(opt.value,filter_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
717
        if opt.name == "vertex-filter":
718
            graph.SetGenericVertexFilter((filter_function,filter_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
719
        else:
720
            graph.SetGenericEdgeFilter((filter_function,filter_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
    elif opt.name == "vertex-range-filter" or opt.name == "edge-range-filter":
        if just_file:
            return None
        values = parse_values(opt.value)
        if len(values) != 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if (len(values[1].split()) != 2):
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        filter_range = (0,0)
        try:
            vals = values[1].split()
            filter_range = (float(vals[0]), float(vals[1]))
        except ValueError:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if opt.name == "vertex-range-filter":
            graph.SetVertexFilterProperty(values[0])
            graph.SetVertexFilterRange(filter_range)
        if opt.name == "edge-range-filter":
            graph.SetEdgeFilterProperty(values[0])
            graph.SetEdgeFilterRange(filter_range)
    elif opt.name == "reset-vertex-filter":
        if just_file:
            return None
        graph.SetVertexFilterProperty("")
        graph.SetGenericVertexFilter(None)
    elif opt.name == "reset-edge-filter":
        if just_file:
            return None
        graph.SetEdgeFilterProperty("")
        graph.SetGenericEdgeFilter(None)
    elif opt.name == "directed":
        if just_file:
            return None
        graph.SetDirected(True)
    elif opt.name == "undirected":
        if just_file:
            return None
        graph.SetDirected(False)
    elif opt.name == "reverse":
        if just_file:
            return None
        graph.SetReversed(not graph.GetReversed())
763
764
765
766
767
768
769
    elif opt.name == "edit-vertex-property" or opt.name == "edit-edge-property":
        values = parse_values(opt.value)
        if len(values) != 2:
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if just_file:
            return None
        edit_vars = dict()
770
771
772
773
774
775
        if "file:" in values[1]:
            exec open(values[1].replace("file:","").strip()).read() in edit_vars
            edit_function = edit_vars["edit_function"]
        else:
            def edit_function():
                return (eval(values[1],edit_vars))
776
        if opt.name == "edit-vertex-property":
777
            graph.EditVertexProperty(values[0],(edit_function,edit_vars))
778
        else:
779
            graph.EditEdgeProperty(values[0],(edit_function,edit_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
    elif opt.name == "remove-vertex-property":
        if just_file:
            return None
        graph.RemoveVertexProperty(opt.value)
    elif opt.name == "remove-edge-property":
        if just_file:
            return None
        graph.RemoveEdgeProperty(opt.value)
    elif opt.name == "insert-edge-index-property":
        if just_file:
            return None
        graph.InsertEdgeIndexProperty(opt.value)
    elif opt.name == "insert-vertex-index-property":
        if just_file:
            return None
        graph.InsertVertexIndexProperty(opt.value)          
        
def open_file(name, mode="w"):
    if name == "-":
        return sys.stdout
    if name.endswith("bz2"):
        return bz2.BZ2File(name, mode)
    if name.endswith("gz"):
        return gzip.GzipFile(name, mode)
    return file(name, mode)

806
807
def write_data(data, file, prefix=None):
    if prefix != None:
Tiago Peixoto's avatar
Tiago Peixoto committed
808
        prefix = "%s\t" % prefix
809
810
    else:
        prefix = ""
Tiago Peixoto's avatar
Tiago Peixoto committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
    if data.__class__ == dict:
        # it is a histogram
        keys = data.keys()
        keys.sort()
        for k in keys:
            file.write(prefix)
            if k.__class__ == tuple:
                # higher dimensional histogram
                for i in xrange(0,len(k)):
                    file.write("%s\t" % str(k[i]))                
            else:
                file.write("%s\t" % (str(k)))
            if data[k].__class__ == tuple:
                for i in xrange(0,len(data[k])):
                    file.write("%s\t" % str(data[k][i]))
                file.write("\n")
            else:
                file.write("%s\n" % str(data[k]))
    else:
        # single value
        file.write("%s%s\n" % (prefix,str(data)))
832
833
    if file.__class__ != bz2.BZ2File:
        file.flush()
Tiago Peixoto's avatar
Tiago Peixoto committed
834
835
836
837
838
839
840
841
842
843
844

class HistoryException (Exception):
    def __init__(self, file=None, old=None, what=None):
        if what == None:
            self.what = "error copying contents from file '%s' to file '%s'" % (old,file)
        else:
            self.what = what
    def __str__(self):
        return self.what

class HistoryFile:
845
    def __init__(self, file_name, overwrite):
Tiago Peixoto's avatar
Tiago Peixoto committed
846
        self.file_name = os.path.expanduser(file_name)
847
        self.last_prefix = None
Tiago Peixoto's avatar
Tiago Peixoto committed
848
        self.file = None
849
        self.time = clock()
Tiago Peixoto's avatar
Tiago Peixoto committed
850
851
852
853
854
        self.temp = self.file_name + "___temp"
        if self.file_name.endswith(".bz2"):
            self.temp = self.file_name.rstrip(".bz2") + "___temp.bz2"
        if self.file_name.endswith(".gz"):
            self.temp = self.file_name.rstrip(".gz") + "___temp.gz"
855
        match = re.compile("([0-9]+)([d,h,m,s])").match(options.refresh_rate)
Tiago Peixoto's avatar
Tiago Peixoto committed
856
857
858
859
860
861
862
863
864
865
        if match != None:
            self.rate = int(match.group(1))
            modifier = match.group(2)
            if modifier == "m":
                self.rate *= 60
            if modifier == "h":
                self.rate *= 60
            if modifier == "d":
                self.rate *= 24
        else:
866
867
            raise HistoryException(what="invalid history refresh rate value: %s" % options.represh_rate)
        if os.path.exists(self.file_name) and self.file_name != "-" and not overwrite:            
Tiago Peixoto's avatar
Tiago Peixoto committed
868
869
870
871
872
873
874
875
876
877
878
            # test the integrity of the compressed files
            try:
                if self.file_name.endswith(".bz2") or self.file_name.endswith(".gz"):
                    for line in open_file(self.file_name,"r"):
                        pass
            except (IOError, EOFError):
                if not os.path.exists(self.temp):
                    old = open_file(self.temp, "w")
                    old.write("")
                    old.close()
                os.rename(self.temp, self.file_name)
879
            #upadate last_prefix
Tiago Peixoto's avatar
Tiago Peixoto committed
880
            for line in open_file(self.file_name, "r"):
881
                self.last_prefix = line.split()[0]
Tiago Peixoto's avatar
Tiago Peixoto committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
            self.refresh()
        else:
            self.file = open_file(self.file_name, "w")

    def __del__(self):
        if self.file != None:
            self.file.close()
        if self.temp != None:
            if os.path.exists(self.temp):
                os.remove(self.temp)

    def refresh(self):
        try:                                                                        
            if self.file_name.endswith(".bz2") or  self.file_name.endswith(".gz"):
                os.rename(self.file_name, self.temp)
                # copy history from old file to new file
                self.file = open_file(self.file_name, "w")
                for line in open_file(self.temp, "r"):
                    self.file.write(line)
            else:
                self.file = open_file(self.file_name, "a")
        except:
            os.rename(self.old, self.file_name)
            raise HistoryException(self.file_name, self.temp)
    
            
    def write(self, data, prefix):
        write_data(data, self.file, prefix)
910
        if clock() - self.time > self.rate:
Tiago Peixoto's avatar
Tiago Peixoto committed
911
912
            self.file.close()
            self.refresh()
913
            self.time = clock()
Tiago Peixoto's avatar
Tiago Peixoto committed
914
915
916
917
918
919
920

# signal handling
graph.InitSignalHandling()

# parse each option in order
try:
    history_range = None
921
    overwrite_history = False
Tiago Peixoto's avatar
Tiago Peixoto committed
922
    for opt in option_list:
923
        if opt.name == "for" or opt.name == "history":
Tiago Peixoto's avatar
Tiago Peixoto committed
924
925
            # the rest of the commands are to be treated as history template
            history_range = opt
926
927
            if opt.name == "for":
                overwrite_history = True
Tiago Peixoto's avatar
Tiago Peixoto committed
928
929
930
931
            break
        retval = parse_option(opt)
        if retval != None:
            if retval[1] != None:
932
                prefix = None
Tiago Peixoto's avatar
Tiago Peixoto committed
933
                if retval[1] == "-":
934
935
936
937
                    if opt.value.strip() != "-":
                        print "#",opt.name.replace("-"," "), "("+opt.value.replace("|",", ").replace(", -","")+"):"
                    else:
                        print "#",opt.name.replace("-"," ")+":"
Tiago Peixoto's avatar
Tiago Peixoto committed
938
                write_data(retval[0], open_file(retval[1]), prefix)
939
940
                if retval[1] == "-":
                    print
Tiago Peixoto's avatar
Tiago Peixoto committed
941
942
943
                
    # deal with history
    if history_range != None:
944
945
        from string import Template
        class MyTemplate(Template):
Tiago Peixoto's avatar
Tiago Peixoto committed
946
947
948
            delimiter = "%"
        values = parse_values(history_range.value)
        if len(values) != 3:
949
            raise OptionError("history", "invalid value '%s'" % history_range.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
950
951
952
953
        histories = option_list[option_list.index(history_range)+1:]
        history_files = dict() # open history files
        variables = dict() # template variables
        exec values[0] in variables #init
954
        exec "___continue = %s" % values[1] in variables # condition
Tiago Peixoto's avatar
Tiago Peixoto committed
955
956
957
958
        count = 0
        while variables["___continue"]:
            for opt in histories:
                new_opt = Opt(opt.name,opt.value)
959
                uses_prefix = True
Tiago Peixoto's avatar
Tiago Peixoto committed
960
                if new_opt.value != None:
961
962
                    if "%prefix" in opt.value:
                        uses_prefix = False
Tiago Peixoto's avatar
Tiago Peixoto committed
963
964
965
966
967
968
969
970
971
972
                    t = MyTemplate(new_opt.value)
                    try:
                        new_opt.value = t.substitute(variables)
                    except KeyError:
                        raise OptionError(opt.name, "invalid value '%s'" % opt.value)
                file_name = parse_option(new_opt, just_file=True)
                if file_name != None:
                    prefix = str(count)
                    if variables.has_key("prefix"):
                        prefix = str(variables["prefix"])
Tiago Peixoto's avatar
Tiago Peixoto committed
973
974
975
                    hist_key = opt.name + opt.value
                    if not history_files.has_key(hist_key):
                        history_files[hist_key] = HistoryFile(file_name, overwrite_history)
976
                    else:
Tiago Peixoto's avatar
Tiago Peixoto committed
977
978
                        if history_files[hist_key].file_name != os.path.expanduser(file_name):
                            history_files[hist_key] = HistoryFile(file_name, overwrite_history)
979
                    try:
Tiago Peixoto's avatar
Tiago Peixoto committed
980
981
                        if history_files[hist_key].last_prefix != None:
                            is_new = float(prefix) > float(history_files[hist_key].last_prefix)
982
983
984
985
                        else:
                            is_new = True
                    except ValueError:
                        is_new = True
986
                    if is_new or overwrite_history:
Tiago Peixoto's avatar
Tiago Peixoto committed
987
                        data = parse_option(new_opt)[0]
988
                        if uses_prefix:
Tiago Peixoto's avatar
Tiago Peixoto committed
989
                            history_files[hist_key].write(data, prefix)
990
                        else:
Tiago Peixoto's avatar
Tiago Peixoto committed
991
                            history_files[hist_key].write(data, "")
Tiago Peixoto's avatar
Tiago Peixoto committed
992
993
                else:
                    parse_option(new_opt)
994
995
            exec values[2] in variables # step
            exec "___continue = %s" % values[1] in variables # condition
Tiago Peixoto's avatar
Tiago Peixoto committed
996
            count += 1
997
        del history_files
Tiago Peixoto's avatar
Tiago Peixoto committed
998
            
999
1000
except (OptionError, HistoryException, IOError, RuntimeError), e:
    print "graph-tool error:", e
Tiago Peixoto's avatar
Tiago Peixoto committed
1001
1002
except KeyboardInterrupt:
    pass