graph-tool 47.2 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
from math import *
Tiago Peixoto's avatar
Tiago Peixoto committed
38

39
40
41
#import gc # garbage collector
#gc.enable()
#gc.set_debug(gc.DEBUG_LEAK|gc.DEBUG_STATS)
Tiago Peixoto's avatar
Tiago Peixoto committed
42
43
44
45
46
47

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():
48
49
50
51
52
    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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    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")
85
86
modification.add_option("--edit-vertex-property", action="callback", callback=push_option, type="string", metavar="PROPERTY[|TYPE]|EXPRESSION", help="edit the selected vertex property")
modification.add_option("--edit-edge-property", action="callback", callback=push_option, type="string", metavar="PROPERTY[|TYPE]|EXPRESSION", help="edit the selected edge property")
Tiago Peixoto's avatar
Tiago Peixoto committed
87
modification.add_option("--edit-graph-property", action="callback", callback=push_option, type="string", metavar="PROPERTY[|TYPE]|EXPRESSION", help="edit the selected graph property")
Tiago Peixoto's avatar
Tiago Peixoto committed
88
89
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")
Tiago Peixoto's avatar
Tiago Peixoto committed
90
modification.add_option("--remove-graph-property", action="callback", callback=push_option, type="string", metavar="PROPERTY|RANGE", help="remove graph property from graph")
Tiago Peixoto's avatar
Tiago Peixoto committed
91
92
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")
93
modification.add_option("--list-properties", action="callback", callback=push_option, help="list all properties")
Tiago Peixoto's avatar
Tiago Peixoto committed
94
95
96
97

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")
98
99
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")
100
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")
101
statistics.add_option("--distance-histogram", action="callback", callback=push_option, type="string", metavar="[WEIGHT|]FILE", help="get the distance histogram")
102
103
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")
104
105
106
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)")
107
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
108
statistics.add_option("--label-parallel-edges", action="callback", callback=push_option, type="string", metavar="PROPERTY", help="label parallel edges to PROPERTY")
109
110
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")
111
statistics.add_option("--reciprocity", action="callback", callback=push_option, type="string", metavar="FILE", help="get the edge reciprocity")
112
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
113
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
114
115

correlations =  parser.add_option_group("Correlations")
116
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")
117
118
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")
119
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")
120
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")
121
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
122
123
124
125

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")
126
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
127
128
129
130
131

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
132
community =  parser.add_option_group("Community")
Tiago Peixoto's avatar
Tiago Peixoto committed
133
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'.")
134
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")
135
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
136

Tiago Peixoto's avatar
Tiago Peixoto committed
137
layout =  parser.add_option_group("History")
138
139
140
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
141

142
143
144
145
146
147
try:
    import optcomplete
    optcomplete.autocomplete(parser)
except ImportError:
    pass

Tiago Peixoto's avatar
Tiago Peixoto committed
148
149
150
151
152
153
(options, args) = parser.parse_args()

graph = GraphInterface()

def parse_values(value):
    "this will parse the options with multiple values"
Tiago Peixoto's avatar
Tiago Peixoto committed
154
155
    p = re.compile(r"((('[^']*')|(\"[^\"']*\"))|([^\|]+)(?=\||$))")
    values = [x[0].strip() for x in p.findall(value)]
Tiago Peixoto's avatar
Tiago Peixoto committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    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

180
181
def get_mean(hist):
    avg,dev,count = 0.0,0.0,0.0
182
    try:
Tiago Peixoto's avatar
Tiago Peixoto committed
183
184
185
        for k,v in hist.iteritems():
            avg += k*v
            count += v
186
        avg /= count
Tiago Peixoto's avatar
Tiago Peixoto committed
187
188
189
        for k,v in hist.iteritems():
            dev += (k - avg)**2
        dev = sqrt(dev/(count**2))
190
191
    except ZeroDivisionError:
        avg = dev = float("nan") # nans are ok, since graph can be empty
192
193
    return (avg,dev)

Tiago Peixoto's avatar
Tiago Peixoto committed
194
195
196
197
198
199
class OptionError(Exception):
    def __init__(self, option, error):
        self.what = "error parsing option %s: %s" % (option,error)
    def __str__(self):
        return self.what

200
201
202
203
204
205
206
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"
207
    inv_pjk_ceil = "(inv_poisson(p,2), inv_poisson(p,2))"
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
    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)
239

240
    # some predefined functions
241
242
243
244
245
    try:
        from scipy.special import gammaincc
        from scipy.optimize import fsolve
    except ImportError:
        pass
246
    
247
248
    variables = globals()

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    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

268
    # read main funtions
269
270
271
272
273
274
275
276
277
278
    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
279

280
281
282
    if "file:" in inv_pjk_ceil:
        exec open(inv_pjk_ceil.replace("file:","").strip()).read() in variables
    else:
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
        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
        
303
304
305
    if "file:" in inv_corr_ceil:
        exec open(inv_corr_ceil.replace("file:","").strip()).read() in variables
    else:
306
307
308
309
310
        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

311
312
313
314
    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
315
def parse_option(opt, just_file=False):
Tiago Peixoto's avatar
Tiago Peixoto committed
316
    "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
317
    if opt.name == "load":
318
319
320
        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
321
322
        if just_file:
            return None
323
324
        if len(values) == 1:
            graph.ReadFromFile(values[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
325
326
        else:
            graph.ReadFromFile(values[0], values[1])
Tiago Peixoto's avatar
Tiago Peixoto committed
327
    elif opt.name == "save":
328
        values = parse_values(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
329
        if len(values) > 2 or len(values) < 1:
330
            raise OptionError(opt.name, "invalid value '$s'" % opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
331
332
        if just_file:
            return None
333
334
        if len(values) == 1:
            graph.WriteToFile(values[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
335
336
        else:
            graph.WriteToFile(values[0], values[1])
Tiago Peixoto's avatar
Tiago Peixoto committed
337
338
339
    elif opt.name == "correlated-configurational-model":
        if just_file:
            return None
340
        generate_graph(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
341
342
343
344
345
346
347
348
    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)
349
350
351
352
    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
353
        if just_file:
354
355
            return values[2]
        return (graph.GetCombinedVertexHistogram(degree(values[0]),degree(values[1])), values[2])
356
357
358
359
360
361
362
    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])
363
364
365
366
367
368
369
370
371
372
373
    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
374
    elif opt.name == "average-distance":
375
376
377
378
379
380
381
        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
382
        if just_file:
383
            return file_name
384
        return ("%f\t%f" % get_mean(graph.GetDistanceHistogram(weight)), file_name)
Tiago Peixoto's avatar
Tiago Peixoto committed
385
    elif opt.name == "average-harmonic-distance":
386
387
388
389
390
391
392
        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
393
        if just_file:
394
            return file_name
395
        hist = graph.GetDistanceHistogram(weight)
Tiago Peixoto's avatar
Tiago Peixoto committed
396
397
        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)
398
399
    elif opt.name == "sampled-distance-histogram":
        values = parse_values(opt.value)
400
        if len(values) != 2:
401
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
402
403
404
405
406
407
408
409
410
411
        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]))
412
        else:
413
            seed = int(time())
414
        if just_file:
415
416
            return values[1]
        return (graph.GetSampledDistanceHistogram(weight, samples, seed), values[1])
417
418
    elif opt.name == "average-sampled-distance":
        values = parse_values(opt.value)
419
        if len(values) != 2:
420
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
421
422
423
424
425
426
        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]))
427
        else:
428
429
430
431
432
            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())
433
        if just_file:
434
435
            return values[1]
        return ("%f\t%f" % get_mean(graph.GetSampledDistanceHistogram(weight, samples, seed)), values[1])
436
437
    elif opt.name == "average-sampled-harmonic-distance":
        values = parse_values(opt.value)
438
        if len(values) != 2:
439
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
440
441
        if get_suboption("weight", values[0]) != False:
            weight = get_suboption("weight", values[0])
442
        else:
443
444
445
446
447
448
449
450
451
            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())
452
        if just_file:
453
            return values[1]
454
        hist = graph.GetSampledDistanceHistogram(weight, samples, seed)
Tiago Peixoto's avatar
Tiago Peixoto committed
455
        avg, err = get_mean(dict((1.0/k,v) for k,v in hist.iteritems()))
456
        return ("%f\t%f" % (1.0/avg,1.0/err) , values[1])
457
    elif opt.name == "label-components":
Tiago Peixoto's avatar
Tiago Peixoto committed
458
        if just_file:
459
460
            return
        graph.LabelComponents(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
461
462
463
464
    elif opt.name == "label-parallel-edges":
        if just_file:
            return
        graph.LabelParallelEdges(opt.value)
465
    elif opt.name == "vertex-histogram":
Tiago Peixoto's avatar
Tiago Peixoto committed
466
467
468
469
470
471
        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]
472
473
474
475
476
477
478
479
        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])
480
481
482
483
    elif opt.name == "reciprocity":
        if just_file:
            return opt.value
        return (graph.GetReciprocity(), opt.value)
484
485
486
487
488
489
490
491
492
493
494
    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)
495
496
497
498
499
500
501
502
503
504
505
    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)
506
    elif opt.name == "vertex-correlation-histogram":
Tiago Peixoto's avatar
Tiago Peixoto committed
507
        values = parse_values(opt.value)
508
        if len(values) < 3 or len(values) > 4:
Tiago Peixoto's avatar
Tiago Peixoto committed
509
510
511
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        deg1 = degree(values[0])
        deg2 = degree(values[1])
512
513
514
515
516
517
        if len(values) == 4:
            weight = values[2]
            file_name = values[3]
        else:
            weight = ""
            file_name = values[2]
Tiago Peixoto's avatar
Tiago Peixoto committed
518
        if just_file:
519
520
            return file_name
        return (graph.GetVertexCorrelationHistogram(deg1, deg2, weight), file_name)
521
    elif opt.name == "edge-vertex-correlation-histogram":
Tiago Peixoto's avatar
Tiago Peixoto committed
522
523
524
525
526
527
528
        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]
529
530
        return (graph.GetEdgeVertexCorrelationHistogram(deg1,values[1],deg2), values[3])
    elif opt.name == "average-nearest-neighbours-correlation":
Tiago Peixoto's avatar
Tiago Peixoto committed
531
        values = parse_values(opt.value)
532
        if len(values) < 3 or len(values) > 4:
Tiago Peixoto's avatar
Tiago Peixoto committed
533
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
534
535
        deg1 = degree(values[0])
        deg2 = degree(values[1])
536
537
538
539
540
541
        if len(values) == 4:
            weight = values[2]
            file_name = values[3]
        else:
            weight = ""
            file_name = values[2]
Tiago Peixoto's avatar
Tiago Peixoto committed
542
        if just_file:
543
544
            return file_name
        return (graph.GetAverageNearestNeighboursCorrelation(deg1, deg2, weight), file_name)
Tiago Peixoto's avatar
Tiago Peixoto committed
545
546
    elif opt.name == "assortativity-coefficient":
        values = parse_values(opt.value)
547
        if len(values) != 2:
Tiago Peixoto's avatar
Tiago Peixoto committed
548
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
549
        deg = degree(values[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
550
        if just_file:
551
            return values[1]
552
553
554
555
556
557
558
559
560
561
562
563
564
        (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])
565
566
567
568
569
570
571
572
    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:
573
            hist = graph.GetEdgeHistogram(values[0])
574
        ret = "%f\t%f" % get_mean(hist)
Tiago Peixoto's avatar
Tiago Peixoto committed
575
        if just_file:
576
577
            return values[1]
        return (ret,values[1])
Tiago Peixoto's avatar
Tiago Peixoto committed
578
579
580
    elif opt.name == "global-clustering-coefficient":
        if just_file:
            return opt.value
581
582
583
        (avg, dev) = graph.GetGlobalClustering()
        ret = "%f\t%f" % (avg, dev)
        return (ret, opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
584
585
586
587
    elif opt.name == "set-local-clustering-to-property":
        if just_file:
            return None
        graph.SetLocalClusteringToProperty(opt.value)
588
589
590
591
592
593
    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)
594
        graph.SetExtendedClusteringToProperty(values[0],int(values[1]))
Tiago Peixoto's avatar
Tiago Peixoto committed
595
596
597
598
    elif opt.name == "community-structure":
        if just_file:
            return None
        values = parse_values(opt.value)
599
        if len(values) < 1 or len(values) > 2:
Tiago Peixoto's avatar
Tiago Peixoto committed
600
601
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        prop = values[0]
602
603
        if len(values) > 1:
            param = values[1]
Tiago Peixoto's avatar
Tiago Peixoto committed
604
        else:
605
            param = ""
Tiago Peixoto's avatar
Tiago Peixoto committed
606
        try:
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
            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
623
624
625
626
            if  get_suboption("spins", param) != False:
                spins = int( get_suboption("spins", param))
            else:
                spins = 0
627
628
            if  get_suboption("corr_type", param) != False:
                if  get_suboption("corr_type", param)  == "random":
629
                    corr = CommCorr.ErdosReyni
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
                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
647
648
649
650
651
            if  get_suboption("history", param) != False:
                history =  get_suboption("history", param)
            else:
                history = ""
                                                    
Tiago Peixoto's avatar
Tiago Peixoto committed
652
653
        except ValueError,e:
            raise OptionError(opt.name, "invalid value '%s': %s" % (opt.value, e))
Tiago Peixoto's avatar
Tiago Peixoto committed
654
        graph.GetCommunityStructure(g, corr, N, Tmin, Tmax, spins, seed, verbose, history, weight, prop)
655
    elif opt.name == "modularity":
Tiago Peixoto's avatar
Tiago Peixoto committed
656
657
658
659
660
661
662
663
664
665
        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)
666
667
668
669
670
671
672
673
674
675
676
677
    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
678
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
    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()
714
715
        def filter_function():
            return not (eval(opt.value,filter_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
716
        if opt.name == "vertex-filter":
717
            graph.SetGenericVertexFilter((filter_function,filter_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
718
        else:
719
            graph.SetGenericEdgeFilter((filter_function,filter_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
720
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
    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())
Tiago Peixoto's avatar
Tiago Peixoto committed
762
    elif opt.name in ["edit-vertex-property", "edit-edge-property",  "edit-graph-property"]:
763
        values = parse_values(opt.value)
764
        if len(values) < 2 or len(values) > 3:
765
766
767
            raise OptionError(opt.name, "invalid value '%s'" % opt.value)
        if just_file:
            return None
768
769
770
771
772
773
        if len(values) == 2:
            val_type = "double"
            expr = values[1]
        else:
            val_type = values[1].strip()
            expr = values[2]
774
        edit_vars = dict()
775
776
        if "file:" in expr:
            expressions = expr.split("file:")
777
778
779
780
            if len(expressions) > 1:
                exec expressions[0] in edit_vars
                exec open(expressions[1].strip()).read() in edit_vars
            else:
Tiago Peixoto's avatar
Tiago Peixoto committed
781
                exec open(expressions[0].strip()).read() in edit_vars    
782
783
784
            edit_function = edit_vars["edit_function"]
        else:
            def edit_function():
785
                return (eval(expr,edit_vars))
786
        if opt.name == "edit-vertex-property":
787
            graph.EditVertexProperty(values[0], val_type, (edit_function,edit_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
788
        elif opt.name == "edit-edge-property":
789
            graph.EditEdgeProperty(values[0], val_type, (edit_function,edit_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
790
791
        else:
            graph.EditGraphProperty(values[0], val_type, (edit_function,edit_vars))
Tiago Peixoto's avatar
Tiago Peixoto committed
792
793
794
795
796
797
798
799
    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)
Tiago Peixoto's avatar
Tiago Peixoto committed
800
801
802
803
    elif opt.name == "remove-graph-property":
        if just_file:
            return None
        graph.RemoveGraphProperty(opt.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
804
805
806
807
808
809
810
811
    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)          
812
813
814
815
    elif opt.name == "list-properties":
        if just_file:
            return None
        graph.ListProperties()          
Tiago Peixoto's avatar
Tiago Peixoto committed
816
817
818
819
820
821
822
823
824
825
        
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)

826
827
def write_data(data, file, prefix=None):
    if prefix != None:
Tiago Peixoto's avatar
Tiago Peixoto committed
828
        prefix = "%s\t" % prefix
829
830
    else:
        prefix = ""
Tiago Peixoto's avatar
Tiago Peixoto committed
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
    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)))
852
853
    if file.__class__ != bz2.BZ2File:
        file.flush()
Tiago Peixoto's avatar
Tiago Peixoto committed
854
855
856
857
858
859
860
861
862
863
864

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:
865
    def __init__(self, file_name, overwrite):
Tiago Peixoto's avatar
Tiago Peixoto committed
866
        self.file_name = os.path.expanduser(file_name)
867
        self.last_prefix = None
Tiago Peixoto's avatar
Tiago Peixoto committed
868
        self.file = None
869
        self.time = clock()
Tiago Peixoto's avatar
Tiago Peixoto committed
870
871
872
873
874
        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"
875
        match = re.compile("([0-9]+)([d,h,m,s])").match(options.refresh_rate)
Tiago Peixoto's avatar
Tiago Peixoto committed
876
877
878
879
880
881
882
883
884
885
        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:
886
887
            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
888
889
890
891
892
893
894
895
896
897
898
            # 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)
899
            #upadate last_prefix
Tiago Peixoto's avatar
Tiago Peixoto committed
900
            for line in open_file(self.file_name, "r"):
901
                self.last_prefix = line.split()[0]
Tiago Peixoto's avatar
Tiago Peixoto committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
            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)
930
        if clock() - self.time > self.rate:
Tiago Peixoto's avatar
Tiago Peixoto committed
931
932
            self.file.close()
            self.refresh()
933
            self.time = clock()
Tiago Peixoto's avatar
Tiago Peixoto committed
934
935
936
937
938
939
940

# signal handling
graph.InitSignalHandling()

# parse each option in order
try:
    history_range = None
941
    overwrite_history = False
Tiago Peixoto's avatar
Tiago Peixoto committed
942
    for opt in option_list:
943
        if opt.name == "for" or opt.name == "history":
Tiago Peixoto's avatar
Tiago Peixoto committed
944
945
            # the rest of the commands are to be treated as history template
            history_range = opt
946
947
            if opt.name == "for":
                overwrite_history = True
Tiago Peixoto's avatar
Tiago Peixoto committed
948
949
950
951
            break
        retval = parse_option(opt)
        if retval != None:
            if retval[1] != None:
952
                prefix = None
Tiago Peixoto's avatar
Tiago Peixoto committed
953
                if retval[1] == "-":
954
955
956
957
                    if opt.value.strip() != "-":
                        print "#",opt.name.replace("-"," "), "("+opt.value.replace("|",", ").replace(", -","")+"):"
                    else:
                        print "#",opt.name.replace("-"," ")+":"
Tiago Peixoto's avatar
Tiago Peixoto committed
958
                write_data(retval[0], open_file(retval[1]), prefix)
959
960
                if retval[1] == "-":
                    print
Tiago Peixoto's avatar
Tiago Peixoto committed
961
962
963
                
    # deal with history
    if history_range != None:
964
965
        from string import Template
        class MyTemplate(Template):
Tiago Peixoto's avatar
Tiago Peixoto committed
966
967
968
            delimiter = "%"
        values = parse_values(history_range.value)
        if len(values) != 3:
969
            raise OptionError("history", "invalid value '%s'" % history_range.value)
Tiago Peixoto's avatar
Tiago Peixoto committed
970
971
972
973
        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
974
        exec "___continue = %s" % values[1] in variables # condition
Tiago Peixoto's avatar
Tiago Peixoto committed
975
976
977
978
        count = 0
        while variables["___continue"]:
            for opt in histories:
                new_opt = Opt(opt.name,opt.value)
979
                uses_prefix = True
Tiago Peixoto's avatar
Tiago Peixoto committed
980
                if new_opt.value != None:
981
982
                    if "%prefix" in opt.value:
                        uses_prefix = False
Tiago Peixoto's avatar
Tiago Peixoto committed
983
984
985
986
987
988
989
990
991
                    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"):
Tiago Peixoto's avatar
Tiago Peixoto committed
992
993
994
995
                        if variables["prefix"] == None:
                            prefix = None
                        else:
                            prefix = str(variables["prefix"])
Tiago Peixoto's avatar
Tiago Peixoto committed
996
997
998
                    hist_key = opt.name + opt.value
                    if not history_files.has_key(hist_key):
                        history_files[hist_key] = HistoryFile(file_name, overwrite_history)
999
                    else:
Tiago Peixoto's avatar
Tiago Peixoto committed
1000
1001
                        if history_files[hist_key].file_name != os.path.expanduser(file_name):
                            history_files[hist_key] = HistoryFile(file_name, overwrite_history)
1002
                    try:
Tiago Peixoto's avatar
Tiago Peixoto committed
1003
1004
                        if history_files[hist_key].last_prefix != None:
                            is_new = float(prefix) > float(history_files[hist_key].last_prefix)
1005
1006
1007
1008
                        else:
                            is_new = True
                    except ValueError:
                        is_new = True
1009
                    if is_new or overwrite_history:
Tiago Peixoto's avatar
Tiago Peixoto committed
1010
                        data = parse_option(new_opt)[0]
1011
                        if uses_prefix:
Tiago Peixoto's avatar
Tiago Peixoto committed
1012
                            history_files[hist_key].write(data, prefix)
1013
                        else:
Tiago Peixoto's avatar
Tiago Peixoto committed
1014
                            history_files[hist_key].write(data, "")
Tiago Peixoto's avatar
Tiago Peixoto committed
1015
1016
                else:
                    parse_option(new_opt)
1017
1018
            exec values[2] in variables # step
            exec "___continue = %s" % values[1] in variables # condition
Tiago Peixoto's avatar
Tiago Peixoto committed
1019
            count += 1
1020
        del history_files
Tiago Peixoto's avatar
Tiago Peixoto committed
1021
            
1022
1023
except (OptionError, HistoryException, IOError, RuntimeError), e:
    print "graph-tool error:", e
Tiago Peixoto's avatar
Tiago Peixoto committed
1024
1025
except KeyboardInterrupt:
    pass