From 4d53ecd725b9a1fc5c9bc039ba675f7c43f98017 Mon Sep 17 00:00:00 2001 From: Craig Milo Rogers Date: Thu, 8 Oct 2020 14:15:29 -0700 Subject: [PATCH] Add debugging feedback. Assume that the input KGTK file will have the property names. --- kgtk/gt/gt_load.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/kgtk/gt/gt_load.py b/kgtk/gt/gt_load.py index 4755947dc..8cef553bb 100644 --- a/kgtk/gt/gt_load.py +++ b/kgtk/gt/gt_load.py @@ -1,5 +1,6 @@ from graph_tool import Graph import itertools +import sys import typing from kgtk.io.kgtkreader import KgtkReader @@ -7,10 +8,12 @@ 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 @@ -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 @@ -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) @@ -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 @@ -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