graph_io.cc 20 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007-2012 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4 5 6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8 9 10 11 12 13 14 15
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
16 17
// along with this program. If not, see <http://www.gnu.org/licenses/>.

18 19
#include <boost/python/extract.hpp>

Tiago Peixoto's avatar
Tiago Peixoto committed
20
#include "graph.hh"
21
#include "graph_filtering.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
22
#include "graph_properties.hh"
23 24 25
#include "graph_util.hh"

#include <iostream>
26
#include <boost/algorithm/string.hpp>
27
#include <boost/iostreams/categories.hpp>
28 29 30
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
31
#include <boost/iostreams/device/file_descriptor.hpp>
32
#include <boost/iostreams/device/file.hpp>
33
#include <boost/graph/graphml.hpp>
34
#include <boost/graph/graphviz.hpp>
35
#include <boost/lexical_cast.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
36
#include <boost/xpressive/xpressive.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
37

38 39
#include "gml.hh"

40
#include "graph_python_interface.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
41
#include "str_repr.hh"
42

Tiago Peixoto's avatar
Tiago Peixoto committed
43 44 45 46
using namespace std;
using namespace boost;
using namespace graph_tool;

47
// use correct smart pointer type for dynamic properties
48
#if (BOOST_VERSION / 100 % 1000 >= 44)
49 50 51 52 53
    #define DP_SMART_PTR boost::shared_ptr
#else
    #define DP_SMART_PTR std::auto_ptr
#endif

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
//
// Persistent IO of python::object types. All the magic is done in python,
// through the object_pickler and object_unplickler below
//

namespace graph_tool
{
python::object object_pickler;
python::object object_unpickler;
}

namespace boost
{
template <>
string lexical_cast<string,python::object>(const python::object & o)
{
    stringstream s;
    object_pickler(OStream(s), o);
    return s.str();
    return "";
}

template <>
python::object lexical_cast<python::object,string>(const string& ps)
{
    stringstream s(ps);
    python::object o;
    o = object_unpickler(IStream(s));
    return o;
}
}

// the following source & sink provide iostream access to python file-like
// objects

class python_file_device
{
public:
    typedef char                 char_type;
    typedef iostreams::seekable_device_tag  category;

    python_file_device(python::object file): _file(file) {}
    std::streamsize read(char* s, std::streamsize n)
    {
        python::object pbuf = _file.attr("read")(n);
        string buf = python::extract<string>(pbuf);
        for (size_t i = 0; i < buf.size(); ++i)
            s[i] = buf[i];
        return buf.size();
    }

    std::streamsize write(const char* s, std::streamsize n)
    {
        string buf(s, s+n);
        python::object pbuf(buf);
        _file.attr("write")(pbuf);
        return n;
    }

    iostreams::stream_offset seek(iostreams::stream_offset off,
                                  std::ios_base::seekdir way)
    {
        _file.attr("seek")(off, int(way));
        return python::extract<iostreams::stream_offset>(_file.attr("tell")());
    }

private:
    python::object _file;
};
123

124 125 126 127 128 129
// Property Maps
// =============

struct get_python_property
{
    template <class ValueType, class IndexMap>
130
    void operator()(ValueType, IndexMap, dynamic_property_map& map,
131 132
                    python::object& pmap) const
    {
133 134
        typedef typename property_map_type::apply<ValueType, IndexMap>::type
            map_t;
135 136 137 138 139
        try
        {
            pmap = python::object
                (PythonPropertyMap<map_t>
                 (dynamic_cast
140
                  <boost::detail::dynamic_property_map_adaptor<map_t>&>(map)
141 142 143 144 145 146
                  .base()));
        } catch (bad_cast&) {}
    }
};

template <class IndexMap>
147
python::object find_property_map(dynamic_property_map& map, IndexMap)
148 149
{
    python::object pmap;
150 151 152
    mpl::for_each<value_types>(boost::bind<void>(get_python_property(),
                                                 _1, IndexMap(), ref(map),
                                                 boost::ref(pmap)));
153 154 155
    return pmap;
}

156
// this functor will check whether a value is of a specific type, create a
Tiago Peixoto's avatar
Tiago Peixoto committed
157
// corresponding vector_property_map and add the value to it
158

Tiago Peixoto's avatar
Tiago Peixoto committed
159 160 161 162
template <class IndexMap>
struct check_value_type
{
    typedef typename IndexMap::key_type key_t;
163 164
    check_value_type(IndexMap index_map, const key_t& key,
                     const boost::any& value, dynamic_property_map*& map)
165
        :_index_map(index_map), _key(key), _value(value), _map(map) {}
Tiago Peixoto's avatar
Tiago Peixoto committed
166 167 168 169

    template <class ValueType>
    void operator()(ValueType)
    {
170 171
        try
        {
172 173 174
            typedef typename property_map_type::apply<ValueType, IndexMap>::type
                map_t;
            map_t vector_map(_index_map);
175
            vector_map[_key] = any_cast<ValueType>(_value);
176 177
            _map = new boost::detail::dynamic_property_map_adaptor<map_t>
                (vector_map);
178 179
        }
        catch (bad_any_cast) {}
Tiago Peixoto's avatar
Tiago Peixoto committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    }
    IndexMap _index_map;
    const key_t& _key;
    const boost::any& _value;
    dynamic_property_map*& _map;
};

// this functor will check wether a key is a vertex or edge descriptor, and
// generate the corresponding property map, depending on the value type

template <class VertexIndexMap, class EdgeIndexMap>
struct create_dynamic_map
{
    typedef typename VertexIndexMap::key_type vertex_t;
    typedef typename EdgeIndexMap::key_type edge_t;

196 197
    create_dynamic_map(VertexIndexMap vertex_map, EdgeIndexMap edge_map)
        :_vertex_map(vertex_map), _edge_map(edge_map) {}
198 199 200
    DP_SMART_PTR<dynamic_property_map> operator()(const string& name,
                                                  const boost::any& key,
                                                  const boost::any& value)
Tiago Peixoto's avatar
Tiago Peixoto committed
201
    {
202 203 204
        dynamic_property_map* map;
        try
        {
205 206 207 208
            mpl::for_each<value_types>
                (check_value_type<VertexIndexMap>(_vertex_map,
                                                  any_cast<vertex_t>(key),
                                                  value, map));
209 210 211
        }
        catch (bad_any_cast)
        {
212
            try
213
            {
214 215 216 217
                mpl::for_each<value_types>
                    (check_value_type<EdgeIndexMap>(_edge_map,
                                                    any_cast<edge_t>(key),
                                                    value, map));
218 219 220 221
            }
            catch (bad_any_cast)
            {
                ConstantPropertyMap<size_t,graph_property_tag> graph_index(0);
222 223 224 225 226
                mpl::for_each<value_types>
                    (check_value_type<ConstantPropertyMap<size_t,
                                                          graph_property_tag> >
                     (graph_index, any_cast<graph_property_tag>(key),
                      value, map));
227
            }
228
        }
229
        return DP_SMART_PTR<dynamic_property_map>(map);
Tiago Peixoto's avatar
Tiago Peixoto committed
230 231 232 233 234 235 236 237 238 239 240
    }

    VertexIndexMap _vertex_map;
    EdgeIndexMap _edge_map;
};

// this graph wrapper will update the edge index map when edges are added

template <class Graph, class EdgeIndexMap>
struct GraphEdgeIndexWrap
{
241 242
    GraphEdgeIndexWrap(Graph &g, EdgeIndexMap edge_index_map)
        : _g(g), _edge_index_map(edge_index_map), _n_edges(0) {}
Tiago Peixoto's avatar
Tiago Peixoto committed
243 244 245
    Graph &_g;
    EdgeIndexMap _edge_index_map;
    size_t _n_edges;
246 247 248 249 250

    typedef typename Graph::vertex_property_type vertex_property_type;
    typedef typename Graph::edge_property_type edge_property_type;
    typedef typename Graph::graph_tag graph_tag;
    typedef typename Graph::graph_type graph_type;
251 252 253 254 255 256 257

#if (BOOST_VERSION / 100 % 1000 >= 45)
    typedef typename Graph::graph_property_type graph_property_type;
    typedef typename Graph::graph_bundled graph_bundled;
    typedef typename Graph::edge_bundled edge_bundled;
    typedef typename Graph::vertex_bundled vertex_bundled;
#endif
Tiago Peixoto's avatar
Tiago Peixoto committed
258 259 260
};

template <class Graph, class EdgeIndexMap>
261 262 263
inline
typename graph_traits
    <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::vertex_descriptor
Tiago Peixoto's avatar
Tiago Peixoto committed
264 265 266 267 268 269
add_vertex(GraphEdgeIndexWrap<Graph,EdgeIndexMap>& g)
{
    return add_vertex(g._g);
}

template <class Graph, class EdgeIndexMap>
270 271 272 273 274 275 276
inline
pair<typename graph_traits
     <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::edge_descriptor,bool>
add_edge(typename graph_traits
         <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::vertex_descriptor u,
         typename graph_traits
         <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::vertex_descriptor v,
277
         GraphEdgeIndexWrap<Graph,EdgeIndexMap>& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
278
{
279 280 281 282
    Graph& orig = g._g;
    pair<typename graph_traits
         <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::edge_descriptor,
         bool> retval = add_edge(u,v,orig);
Tiago Peixoto's avatar
Tiago Peixoto committed
283
    if (retval.second)
284
        g._edge_index_map[retval.first] = g._n_edges;
Tiago Peixoto's avatar
Tiago Peixoto committed
285 286 287 288 289 290
    ++g._n_edges;
    return retval;
}

namespace boost {
template <class Graph, class EdgeIndexMap>
291 292
class graph_traits<GraphEdgeIndexWrap<Graph,EdgeIndexMap> >
    : public graph_traits<Graph> {};
Tiago Peixoto's avatar
Tiago Peixoto committed
293 294
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
template <class Graph, class EdgeIndexMap>
inline
typename graph_traits
    <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::vertex_descriptor
num_vertices(GraphEdgeIndexWrap<Graph,EdgeIndexMap>& g)
{
    return num_vertices(g._g);
}

template <class Graph, class EdgeIndexMap>
inline
typename graph_traits
    <GraphEdgeIndexWrap<Graph,EdgeIndexMap> >::vertex_descriptor
vertex(size_t i, GraphEdgeIndexWrap<Graph,EdgeIndexMap>& g)
{
    return vertex(i, g._g);
}

Tiago Peixoto's avatar
Tiago Peixoto committed
313 314
// this graph wraps an UndirectedAdaptor, but overrides the underlying
// edge_descriptor type with the original type. This will make the edge property
315
// maps compatible with the original graph, but will break some things which
Tiago Peixoto's avatar
Tiago Peixoto committed
316 317 318 319 320 321 322 323 324 325
// are not relevant here

template <class Graph>
struct FakeUndirGraph: public UndirectedAdaptor<Graph>
{
    FakeUndirGraph(const Graph &g): UndirectedAdaptor<Graph>(g) {}
    FakeUndirGraph(UndirectedAdaptor<Graph> &g): UndirectedAdaptor<Graph>(g) {}
};

template <class Graph>
326 327
struct FakeEdgeIterator:
    public graph_traits<UndirectedAdaptor<Graph> >::edge_iterator
Tiago Peixoto's avatar
Tiago Peixoto committed
328
{
329 330 331 332
    typedef typename graph_traits<FakeUndirGraph<Graph> >::edge_descriptor
        edge_descriptor;
    typedef typename graph_traits<UndirectedAdaptor<Graph> >::edge_iterator
        edge_iterator;
Tiago Peixoto's avatar
Tiago Peixoto committed
333 334 335 336
    FakeEdgeIterator(){}
    FakeEdgeIterator(edge_iterator e): edge_iterator(e) {}
    edge_descriptor operator*() const
    {
337
        return edge_descriptor(*edge_iterator(*this));
338
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
339 340 341

};

342

Tiago Peixoto's avatar
Tiago Peixoto committed
343 344
namespace boost {
template <class Graph>
345 346
struct graph_traits<FakeUndirGraph<Graph> >
    : public graph_traits<UndirectedAdaptor<Graph> >
Tiago Peixoto's avatar
Tiago Peixoto committed
347 348 349 350 351 352 353 354
{
    typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
    typedef FakeEdgeIterator<Graph> edge_iterator;
};
}


//==============================================================================
355
// ReadFromFile(file, pfile, format)
Tiago Peixoto's avatar
Tiago Peixoto committed
356 357
//==============================================================================

358 359 360
void build_stream
    (boost::iostreams::filtering_stream<boost::iostreams::input>& stream,
     const string& file,  python::object& pfile, std::ifstream& file_stream)
Tiago Peixoto's avatar
Tiago Peixoto committed
361
{
362 363 364 365
    stream.reset();
    if (file == "-")
        stream.push(std::cin);
    else
Tiago Peixoto's avatar
Tiago Peixoto committed
366
    {
367
        if (pfile == python::object())
368
        {
369 370
            file_stream.open(file.c_str(), std::ios_base::in |
                             std::ios_base::binary);
371 372
            file_stream.exceptions(ios_base::badbit | ios_base::failbit);
            if (boost::ends_with(file,".gz"))
373
                stream.push(boost::iostreams::gzip_decompressor());
374
            if (boost::ends_with(file,".bz2"))
375
                stream.push(boost::iostreams::bzip2_decompressor());
376 377
            stream.push(file_stream);
        }
378 379 380 381 382 383 384 385 386 387
        else
        {
            python_file_device src(pfile);
            stream.push(src);
        }
    }
    stream.exceptions(ios_base::badbit);
}


388
python::tuple GraphInterface::ReadFromFile(string file, python::object pfile,
389
                                           string format)
390
{
391
    if (format != "dot" && format != "xml" && format != "gml")
Tiago Peixoto's avatar
Tiago Peixoto committed
392
        throw ValueException("error reading from file '" + file +
393 394 395 396 397 398 399
                             "': requested invalid format '" + format + "'");
    try
    {
        boost::iostreams::filtering_stream<boost::iostreams::input>
            stream;
        std::ifstream file_stream;
        build_stream(stream, file, pfile, file_stream);
400

401 402 403
        create_dynamic_map<vertex_index_map_t,edge_index_map_t>
            map_creator(_vertex_index, _edge_index);
        dynamic_properties dp(map_creator);
Tiago Peixoto's avatar
Tiago Peixoto committed
404
        _state->_mg.clear();
405

Tiago Peixoto's avatar
Tiago Peixoto committed
406 407
        GraphEdgeIndexWrap<multigraph_t,edge_index_map_t> wg(_state->_mg,
                                                             _edge_index);
408 409 410 411 412 413 414
        if (format == "dot")
            _directed = read_graphviz(stream, wg, dp, "vertex_name", true);
        else if (format == "xml")
            _directed = read_graphml(stream, wg, dp, true, true);
        else if (format == "gml")
            _directed = read_gml(stream, wg, dp);

Tiago Peixoto's avatar
Tiago Peixoto committed
415 416 417
        _state->_nedges = num_edges(_state->_mg);
        _state->_max_edge_index = (_state->_nedges > 0) ?
            _state->_nedges - 1 : 0;
418

419 420 421 422
        python::dict vprops, eprops, gprops;
        for(typeof(dp.begin()) iter = dp.begin(); iter != dp.end(); ++iter)
        {
            if (iter->second->key() == typeid(vertex_t))
423
                vprops[iter->first] = find_property_map(*iter->second,
424 425
                                                       _vertex_index);
            else if (iter->second->key() == typeid(edge_t))
426
                eprops[iter->first] = find_property_map(*iter->second,
427 428
                                                       _edge_index);
            else
429
                gprops[iter->first] = find_property_map(*iter->second,
430 431 432
                                                        _graph_index);
        }
        return python::make_tuple(vprops, eprops, gprops);
Tiago Peixoto's avatar
Tiago Peixoto committed
433 434 435
    }
    catch (ios_base::failure &e)
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
436
        throw IOException("error reading from file '" + file + "':" + e.what());
Tiago Peixoto's avatar
Tiago Peixoto committed
437
    }
438 439 440 441
    catch (parse_error &e)
    {
        throw IOException("error reading from file '" + file + "':" + e.what());
    }
442 443 444 445
    catch (gml_parse_error &e)
    {
        throw IOException("error reading from file '" + file + "':" + e.what());
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
446 447
};

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
template <class IndexMap>
string graphviz_insert_index(dynamic_properties& dp, IndexMap index_map,
                             bool insert = true)
{
    typedef GraphInterface::vertex_t vertex_t;
    bool found = false;
    for(typeof(dp.begin()) iter = dp.begin(); iter != dp.end();
        ++iter)
        if (iter->first == "vertex_name" &&
            iter->second->key() == typeid(vertex_t))
            found = true;
    if (!found && insert)
        dp.property("vertex_id", index_map);
    if (found)
        return "vertex_name";
    else
        return "vertex_id";
}

467 468
// writes a graph to a file

Tiago Peixoto's avatar
Tiago Peixoto committed
469 470 471
struct write_to_file
{
    template <class Graph, class IndexMap>
472
    void operator()(ostream& stream, Graph& g, IndexMap index_map,
473
                    dynamic_properties& dp, const string& format) const
Tiago Peixoto's avatar
Tiago Peixoto committed
474
    {
475 476
        typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;

477
        if (format == "dot")
478
        {
479
            string name = graphviz_insert_index(dp, index_map, false);
480
            write_graphviz(stream, g, dp, name);
481
        }
482
        else if (format == "xml")
483
        {
484
            write_graphml(stream, g, index_map, dp, true);
485
        }
486 487 488 489
        else if (format == "gml")
        {
            write_gml(stream, g, index_map, dp);
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
490 491 492 493 494 495
    }
};

struct write_to_file_fake_undir: public write_to_file
{
    template <class Graph, class IndexMap>
496
    void operator()(ostream& stream, Graph& g, IndexMap index_map,
497
                    dynamic_properties& dp, const string& format) const
Tiago Peixoto's avatar
Tiago Peixoto committed
498
    {
499
        typedef typename Graph::original_graph_t graph_t;
500
        FakeUndirGraph<graph_t> ug(g);
501
        write_to_file(*this)(stream, ug, index_map, dp, format);
Tiago Peixoto's avatar
Tiago Peixoto committed
502 503 504 505 506 507
    }
};

struct generate_index
{
    template <class Graph, class IndexMap>
508
    void operator()(Graph& g, IndexMap index_map) const
Tiago Peixoto's avatar
Tiago Peixoto committed
509
    {
510 511
        size_t n = 0;
        typename graph_traits<Graph>::vertex_iterator v, v_end;
512
        for( tie(v, v_end) = vertices(g); v != v_end; ++v)
513
            index_map[*v] = n++;
Tiago Peixoto's avatar
Tiago Peixoto committed
514 515 516
    }
};

517
void GraphInterface::WriteToFile(string file, python::object pfile,
518
                                 string format, python::list props)
Tiago Peixoto's avatar
Tiago Peixoto committed
519
{
520
    if (format != "xml" && format != "dot" && format != "gml")
Tiago Peixoto's avatar
Tiago Peixoto committed
521
        throw ValueException("error writing to file '" + file +
522
                             "': requested invalid format '" + format + "'");
Tiago Peixoto's avatar
Tiago Peixoto committed
523 524
    try
    {
525 526 527 528 529 530
        boost::iostreams::filtering_stream<boost::iostreams::output> stream;
        std::ofstream file_stream;
        if (file == "-")
            stream.push(std::cout);
        else
        {
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
            if (pfile == python::object())
            {
                file_stream.open(file.c_str(), std::ios_base::out |
                                 std::ios_base::binary);
                file_stream.exceptions(ios_base::badbit | ios_base::failbit);
                if (boost::ends_with(file,".gz"))
                    stream.push(boost::iostreams::gzip_compressor());
                if (boost::ends_with(file,".bz2"))
                    stream.push(boost::iostreams::bzip2_compressor());
                stream.push(file_stream);
            }
            else
            {
                python_file_device sink(pfile);
                stream.push(sink);
            }
547 548 549
        }
        stream.exceptions(ios_base::badbit | ios_base::failbit);

550 551 552 553 554 555 556 557
        dynamic_properties dp;
        for (int i = 0; i < len(props); ++i)
        {
            dynamic_property_map* pmap =
                any_cast<dynamic_property_map*>
                (python::extract<boost::any>
                 (props[i][1].attr("get_dynamic_map")()));
            dp.insert(python::extract<string>(props[i][0]),
558
                      DP_SMART_PTR<dynamic_property_map>(pmap));
559
        }
560 561 562 563

        if (IsVertexFilterActive())
        {
            // vertex indexes must be between the [0, HardNumVertices(g)] range
564
            typedef tr1::unordered_map<vertex_t, size_t>  map_t;
565 566
            map_t vertex_to_index;
            associative_property_map<map_t> index_map(vertex_to_index);
567 568
            run_action<>()(*this, boost::bind<void>(generate_index(),
                                                    _1, index_map))();
569
            if (format == "dot")
570 571
                graphviz_insert_index(dp, index_map);

572
            if (GetDirected())
573
                run_action<detail::always_directed>()
574 575 576
                    (*this, boost::bind<void>(write_to_file(),
                                              boost::ref(stream), _1,
                                              index_map, boost::ref(dp),
577
                                              format))();
578
            else
579
                run_action<detail::never_directed>()
580 581
                    (*this,boost::bind<void>(write_to_file_fake_undir(),
                                             boost::ref(stream), _1, index_map,
582
                                             boost::ref(dp), format))();
583 584 585
        }
        else
        {
586
            if (format == "dot")
587
                graphviz_insert_index(dp, _vertex_index);
588 589

            if (GetDirected())
590
                run_action<detail::always_directed>()
591 592 593
                    (*this, boost::bind<void>(write_to_file(),
                                              boost::ref(stream), _1,
                                              _vertex_index,  boost::ref(dp),
594
                                              format))();
595
            else
596
                run_action<detail::never_directed>()
597 598 599
                    (*this,boost::bind<void>(write_to_file_fake_undir(),
                                             boost::ref(stream), _1,
                                             _vertex_index, boost::ref(dp),
600
                                             format))();
601 602
        }
        stream.reset();
Tiago Peixoto's avatar
Tiago Peixoto committed
603 604 605
    }
    catch (ios_base::failure &e)
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
606
        throw IOException("error writing to file '" + file + "':" + e.what());
Tiago Peixoto's avatar
Tiago Peixoto committed
607 608
    }
}