Commit 5fa50e88 authored by Tiago Peixoto's avatar Tiago Peixoto

Re-include graph rewiring code in the new scheme

This also creates the misc sub-module. This may be re-organized in the
future.
parent b1e1bc5e
......@@ -251,6 +251,7 @@ src/graph/generation/Makefile
src/graph/stats/Makefile
src/graph/clustering/Makefile
src/graph/util/Makefile
src/graph/misc/Makefile
src/graph_tool/Makefile
])
## Process this file with automake to produce Makefile.in
SUBDIRS = generation correlations stats clustering util
SUBDIRS = generation correlations stats clustering util misc
AM_CPPFLAGS =\
-I$(srcdir)/.. \
......
## Process this file with automake to produce Makefile.in
AM_CPPFLAGS =\
-I$(srcdir) -I$(srcdir)/.. \
-I$(srcdir)/../boost-workaround \
-DHAVE_CONFIG_H
AM_CFLAGS=$(AM_CXXFLAGS)
libgraph_tool_miscdir = $(pythondir)/graph_tool/misc
libgraph_tool_misc_LTLIBRARIES = libgraph_tool_misc.la
libgraph_tool_misc_la_includedir = $(pythondir)/graph_tool/include
libgraph_tool_misc_la_SOURCES = \
graph_rewiring.cc \
graph_misc.cc
libgraph_tool_misc_la_include_HEADERS = \
graph_rewiring.hh
libgraph_tool_misc_la_LIBADD = \
$(PYTHON_LDFLAGS) \
$(BOOST_LDFLAGS) \
$(OPENMP_LDFLAGS) \
-lboost_python \
-lboost_iostreams \
-lexpat
# needed for typeinfo objects to work across DSO boundaries.
# see http://gcc.gnu.org/faq.html#dso
libgraph_tool_misc_la_LDFLAGS = \
-module \
-avoid-version \
-export-dynamic \
-no-undefined \
-Wl,-E
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007 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 3
// 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
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <boost/python.hpp>
#include "graph.hh"
using namespace boost;
using namespace boost::python;
using namespace graph_tool;
void random_rewire(GraphInterface& gi, string strat, bool self_loops,
bool parallel_edges, size_t seed);
BOOST_PYTHON_MODULE(libgraph_tool_misc)
{
def("random_rewire", &random_rewire);
}
......@@ -19,29 +19,33 @@
#include "graph.hh"
#include "graph_filtering.hh"
#include <boost/random.hpp>
typedef boost::mt19937 rng_t;
#include "graph_rewiring.hh"
using namespace graph_tool;
using namespace boost;
using namespace boost::lambda;
void GraphInterface::RandomRewire(string strat, bool self_loops,
bool parallel_edges, size_t seed)
void random_rewire(GraphInterface& gi, string strat, bool self_loops,
bool parallel_edges, size_t seed)
{
bool reversed = GetReversed();
SetReversed(false);
rng_t rng(seed);
GraphInterface::edge_index_map_t edge_index =
any_cast<GraphInterface::edge_index_map_t>(gi.GetEdgeIndex());
if (strat == "uncorrelated")
run_action<detail::never_reversed>()
(*this, bind<void>(graph_rewire<RandomRewireStrategy>(),
_1, _edge_index, seed, self_loops,
parallel_edges))();
run_action<graph_tool::detail::never_reversed>()
(gi, bind<void>(graph_rewire<RandomRewireStrategy>(),
_1, edge_index, ref(rng), self_loops,
parallel_edges))();
else if (strat == "correlated")
run_action<detail::never_reversed>()
(*this, bind<void>(graph_rewire<CorrelatedRewireStrategy>(),
_1, _edge_index, seed, self_loops,
parallel_edges))();
run_action<graph_tool::detail::never_reversed>()
(gi, bind<void>(graph_rewire<CorrelatedRewireStrategy>(),
_1, edge_index, ref(rng), self_loops,
parallel_edges))();
else
throw GraphException("invalid random rewire stategy: " + strat);
SetReversed(reversed);
}
......@@ -30,8 +30,6 @@ namespace graph_tool
using namespace std;
using namespace boost;
typedef boost::mt19937 rng_t;
// this will get the source of an edge for directed graphs and the target for
// undirected graphs, i.e. "the source of an in-edge"
struct source_in
......@@ -231,14 +229,13 @@ template <template <class Graph, class EdgeIndexMap> class RewireStrategy>
struct graph_rewire
{
template <class Graph, class EdgeIndexMap>
void operator()(Graph* gp, EdgeIndexMap edge_index, size_t seed,
void operator()(Graph* gp, EdgeIndexMap edge_index, rng_t& rng,
bool self_loops, bool parallel_edges) const
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
Graph& g = *gp;
rng_t rng(static_cast<rng_t::result_type>(seed));
if (!self_loops)
{
......
......@@ -44,3 +44,7 @@ graph_tool_drawdir = $(pythondir)/graph_tool/draw
graph_tool_util_PYTHON = \
util/__init__.py
graph_tool_utildir = $(pythondir)/graph_tool/util
graph_tool_misc_PYTHON = \
misc/__init__.py
graph_tool_miscdir = $(pythondir)/graph_tool/misc
......@@ -41,5 +41,7 @@ from graph_tool.clustering import *
import graph_tool.clustering
from graph_tool.run_action import *
graph_tool.run_action
from graph_tool.misc import *
import graph_tool.misc
from graph_tool.util import *
import graph_tool.util
#! /usr/bin/env python
# graph_tool.py -- a general graph manipulation python module
#
# Copyright (C) 2007 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 3 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
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
# RTLD_GLOBAL needs to be set in dlopen() if we want typeinfo and friends to
# work properly across DSO boundaries. See http://gcc.gnu.org/faq.html#dso
# The "except" is because the dl module raises a system error on ia64 and x86_64
# systems because "int" and addresses are different sizes.
try:
from dl import RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL
except ImportError:
RTLD_LAZY = 1
RTLD_NOW = 2
RTLD_GLOBAL = 256
_orig_dlopen_flags = sys.getdlopenflags()
sys.setdlopenflags(RTLD_LAZY|RTLD_GLOBAL)
import libgraph_tool_misc
sys.setdlopenflags(_orig_dlopen_flags) # reset it to normal case to avoid
# unnecessary symbol collision
import random, sys
__all__ = ["random_rewire"]
def random_rewire(g, strat="uncorrelated", self_loops = False,
parallel_edges = False, seed = 0):
if seed != 0:
seed = random.randint(0, sys.maxint)
if g.is_reversed():
was_reversed = True
else:
was_reversed = False
g.set_reversed(False)
libgraph_tool_misc.random_rewire(g._Graph__graph, strat, self_loops,
parallel_edges, seed)
if was_reversed:
g.set_reversed(True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment