Skip to content

Commit

Permalink
Add debugging feedback. Assume that the input KGTK file will have the…
Browse files Browse the repository at this point in the history
… property names.
  • Loading branch information
CraigMiloRogers committed Oct 8, 2020
1 parent efa3c42 commit 4d53ecd
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions kgtk/gt/gt_load.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from graph_tool import Graph
import itertools
import sys
import typing

from kgtk.io.kgtkreader import KgtkReader

def load_graph_from_kgtk(kr: KgtkReader,
directed: bool=False,
eprop_types: typing.Optional[typing.List[str]]=None,
eprop_names: typing.Optional[typing.List[str]]=None,
hashed: bool=True,
hash_type: str="string", # for future support
ecols: typing.Optional[typing.Tuple[int, int]]=None):
ecols: typing.Optional[typing.Tuple[int, int]]=None,
out: typing.TextIO = sys.stderr,
verbose: bool = False,
):
"""Load a graph from a `KgtkReader` file containing a list of edges and edge
properties. This code is based on load_graph_from_csv(...) in
`graph-tool/src/graph_tool/__init__.py`, downloaded from git.skewed.de on
Expand All @@ -27,12 +30,6 @@ def load_graph_from_kgtk(kr: KgtkReader,
List of edge property types to be read from remaining columns (if this
is ``None``, all properties will be of type ``string``.
eprop_names : list of ``str`` (optional, default: ``None``)
List of edge property names to be used for the remaining columns (if
this is ``None``, and ``skip_first`` is ``True`` their values will be
obtained from the first line, otherwise they will be called ``c1, c2,
...``).
hashed : ``bool`` (optional, default: ``True``)
If ``True`` the vertex values in the edge list are not assumed to
correspond to vertex indices directly. In this case they will be mapped
Expand Down Expand Up @@ -60,8 +57,6 @@ def load_graph_from_kgtk(kr: KgtkReader,
"""
r = kr # R may be wrapped for column reordering and/or non-hashed use.

first_line: typing.List[str] = list(kr.column_names)

if ecols is None:
ecols = (kr.node1_column_idx, kr.node2_column_idx)

Expand Down Expand Up @@ -89,8 +84,12 @@ def conv(rows):
g = Graph(directed=directed)

if eprop_types is None:
if verbose:
print("eprop_types is None", file=out, flush=True)
eprops = [g.new_ep("string") for x in line[2:]]
else:
if verbose:
print("eprop_types: [%s]" % (", ".join([repr(x) for x in eprop_types])), file=out, flush=True)
eprops = [g.new_ep(t) for t in eprop_types]

# 29-Jul-2020: This is supported in the git.skewed.de repository, and
Expand All @@ -104,19 +103,18 @@ def conv(rows):
# hash_type=hash_type, eprops=eprops)
name = g.add_edge_list(itertools.chain([line], r), hashed=hashed,
eprops=eprops)
g.vp.name = name

if eprop_names is None and len(first_line) == len(line):
eprop_names = list(first_line)
del eprop_names[min(ecols)]
del eprop_names[max(ecols)-1]
eprop_names: typing.List[str] = list(kr.column_names)
del eprop_names[min(ecols)]
del eprop_names[max(ecols)-1]
if verbose:
print("eprop_names: [%s]" % (", ".join([repr(x) for x in eprop_names])), file=out, flush=True)

for i, p in enumerate(eprops):
if eprop_names is not None:
ename = eprop_names[i]
else:
ename = "c%d" % i
ename = eprop_names[i]
g.ep[ename] = p
if verbose:
print("prop %d name=%s" % (i, repr(ename)), file=out, flush=True)

if name is not None:
g.vp.name = name
return g

0 comments on commit 4d53ecd

Please sign in to comment.