Commit f2c64271 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add missing file

parent e4b5ee4c
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# graph_tool -- a general graph manipulation python module
# Copyright (C) 2006-2015 Tiago de Paula Peixoto <>
# 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
# 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 <>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
import os.path
import tempfile
import urllib.request
import tarfile
import warnings
import numpy
from .. import Graph
def load_koblenz_dir(dirname):
g = Graph() = g.new_graph_property("string") = g.new_graph_property("string")
for root, dirs, files in os.walk(dirname):
for file in files:
if file.startswith("README"): = open(os.path.join(root,file)).read()
if file.startswith("meta."): = open(os.path.join(root,file)).read()
if file.startswith("out."):
edges = numpy.loadtxt(os.path.join(root,file), comments="%")
line = next(open(os.path.join(root,file)))
if "asym" not in line:
edges[:,:2] -= 1 # we need zero-based indexing
if edges.shape[1] > 2:
g.ep.weight = g.new_edge_property("int")
g.ep.weight.a = edges[:,2]
if edges.shape[1] > 3:
g.ep.time = g.new_edge_property("int")
g.ep.time.a = edges[:,3]
for file in files:
if file.startswith("ent."):
g.vp.meta = g.new_vertex_property("string")
meta = g.vp.meta
count = 0
for line in open(os.path.join(root,file)):
vals = line.split()
if len(vals) == 1 and vals[0] == "%":
if vals[0] == "%": = g.new_graph_property("string", line)
v = g.vertex(count)
meta[v] = line.strip()
count += 1
except ValueError as e:
warnings.warn("error automatically reading node metadata from file '%s': %s" % (file, str(e)))
return g
def get_koblenz_network_data(name):
with tempfile.TemporaryFile(mode='w+b') as ftemp:
with urllib.request.urlopen('' % name) as response:
buflen = 1 << 20
while True:
buf =
if len(buf) < buflen:
with tempfile.TemporaryDirectory(suffix=name) as tempdir:
with, mode='r:bz2') as tar:
g = load_koblenz_dir(tempdir)
return g
class LazyKoblenzDataDict(dict):
def __getitem__(self, k):
if k not in self:
g = get_koblenz_network_data(k)
dict.__setitem__(self, k, g)
return g
return dict.__getitem__(self, k)
konect_data = LazyKoblenzDataDict()
Supports Markdown
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