Esempio n. 1
0
    def all_shortest_paths(self, src_sid=None, dst_sid=None):
        nv = GraphData()
        if src_sid is None and dst_sid is None:
            raise Exception('src_sid or dst_sid must be set')
        elif src_sid is None and dst_sid is not None:
            dst = self.__resolve_sid_to_id(dst_sid)
            if dst is None:
                raise Exception('SID not found!')

            total = self.dbsession.query(func.count(
                EdgeLookup.id)).filter_by(ad_id=self.domain_id).filter(
                    EdgeLookup.oid != self.domain_sid + '-513').scalar()
            q = self.dbsession.query(
                EdgeLookup.id).filter_by(ad_id=self.domain_id).filter(
                    EdgeLookup.oid != self.domain_sid + '-513')
            for nodeid in tqdm(windowed_query(q, EdgeLookup.id, 1000),
                               desc='running',
                               total=total):
                for path in all_shortest_paths(self.graph, nodeid[0], dst):
                    print(path)
                    self.__result_path_add(nv, path)

        elif src_sid is not None and dst_sid is not None:
            print(1)
            print(src_sid)
            print(dst_sid)
            src = self.__resolve_sid_to_id(src_sid)
            if src is None:
                raise Exception('SID not found!')

            dst = self.__resolve_sid_to_id(dst_sid)
            if dst is None:
                raise Exception('SID not found!')

            print(src)
            print(dst)

            for path in all_shortest_paths(self.graph, src, dst):
                print(path)
                self.__result_path_add(nv, path)

        return nv
Esempio n. 2
0
def statistics_experiments(sentence_graphs):

    sentence_graph = sentence_graphs[0]
    print("sentence_graph graph: %s" % str(sentence_graph.get_graph()))

    shortest_paths = dict()
    for vertex_1 in sentence_graph.get_vertices():
        for vertex_2 in sentence_graph.get_vertices():
            if vertex_1 == vertex_2:
                continue

            all_shortest_paths_iterator = topology.all_shortest_paths(
                sentence_graph.get_graph(), vertex_1, vertex_2)
            #print("Iterating on path from %s to %s" % (sentence_graph.get_word_pos_tuple(vertex_1), sentence_graph.get_word_pos_tuple(vertex_2)))
            #print("all_shortest_paths_iterator: %s" % str(all_shortest_paths_iterator))
            saved_shortest_paths = []
            for shortest_path in all_shortest_paths_iterator:
                #print("shortest_path: %s" % str(shortest_path))
                shortest_path_length = 0
                for shortest_path_vertex_index in shortest_path:
                    #print("word_pos_tuple: %s" % str(sentence_graph.get_word_pos_tuple_by_index(shortest_path_vertex_index)))
                    shortest_path_length += 1
                if shortest_path_length <= MAX_PATH_LENGTH:
                    saved_shortest_paths.append(shortest_path.copy())

            if len(saved_shortest_paths) > 0:
                shortest_paths[(vertex_1, vertex_2)] = saved_shortest_paths

    print("\n\n\n=========================\n\n\n")
    for vertex_tuple, saved_shortest_paths in shortest_paths.items():
        vertex_1 = vertex_tuple[0]
        vertex_2 = vertex_tuple[1]
        print("Iterating on paths from %s to %s" %
              (sentence_graph.get_word_pos_tuple(vertex_1),
               sentence_graph.get_word_pos_tuple(vertex_2)))
        for saved_shortest_path in saved_shortest_paths:
            print("Path: %s" % str(saved_shortest_path))
            for shortest_path_vertex_index in saved_shortest_path:
                print("word_pos_tuple: %s" % str(
                    sentence_graph.get_word_pos_tuple_by_index(
                        shortest_path_vertex_index)))
    """ 
Esempio n. 3
0
def run(
        input_file: KGTKFiles,
        path_file: KGTKFiles,
        output_file: KGTKFiles,
        statistics_only: bool,
        undirected: bool,
        max_hops: int,
        source_column_name: typing.Optional[str],
        target_column_name: typing.Optional[str],
        shortest_path: bool,
        errors_to_stdout: bool,
        errors_to_stderr: bool,
        show_options: bool,
        verbose: bool,
        very_verbose: bool,
        **kwargs,  # Whatever KgtkFileOptions and KgtkValueOptions want.
):
    # import modules locally
    from pathlib import Path
    import sys

    from graph_tool.all import find_vertex
    from graph_tool.topology import all_paths
    from graph_tool.topology import all_shortest_paths

    from kgtk.gt.gt_load import load_graph_from_kgtk
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions
    from kgtk.io.kgtkwriter import KgtkWriter
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    from kgtk.exceptions import KGTKException
    try:

        # Select where to send error messages, defaulting to stderr.
        error_file: typing.TextIO = sys.stdout if errors_to_stdout else sys.stderr

        # Build the option structures.
        input_reader_options: KgtkReaderOptions = KgtkReaderOptions.from_dict(
            kwargs, who="input", fallback=True)
        path_reader_options: KgtkReaderOptions = KgtkReaderOptions.from_dict(
            kwargs, who="path", fallback=True)
        value_options: KgtkValueOptions = KgtkValueOptions.from_dict(kwargs)

        input_kgtk_file: Path = KGTKArgumentParser.get_input_file(input_file)
        path_kgtk_file: Path = KGTKArgumentParser.get_input_file(path_file)
        output_kgtk_file: Path = KGTKArgumentParser.get_output_file(
            output_file)

        id_col = 'name'

        if verbose:
            print("Reading the path file: %s" % str(path_kgtk_file),
                  file=error_file,
                  flush=True)
        pairs = []
        pkr: KgtkReader = KgtkReader.open(
            path_kgtk_file,
            error_file=error_file,
            options=path_reader_options,
            value_options=value_options,
            verbose=verbose,
            very_verbose=very_verbose,
        )
        path_source_idx: int = pkr.get_node1_column_index(source_column_name)
        if path_source_idx < 0:
            print("Missing node1 (source) column name in the path file.",
                  file=error_file,
                  flush=True)

        path_target_idx: int = pkr.get_node2_column_index(target_column_name)
        if path_target_idx < 0:
            print("Missing node1 (target) column name in the path file.",
                  file=error_file,
                  flush=True)
        if path_source_idx < 0 or path_target_idx < 0:
            pkr.close()
            raise KGTKException("Exiting due to missing columns.")

        paths_read: int = 0
        path_row: typing.List[str]
        for path_row in pkr:
            paths_read += 1
            if len(path_row) != pkr.column_count:
                raise KGTKException(
                    "Exiting because line %d in the path file (%s) is the wrong length: %d columns expected, %d were read."
                    % (paths_read, str(path_kgtk_file), pkr.column_count,
                       len(path_row)))
            src: str = path_row[path_source_idx]
            tgt: str = path_row[path_target_idx]
            pairs.append((src, tgt))
        pkr.close()
        if verbose:
            print("%d path rows read" % paths_read,
                  file=error_file,
                  flush=True)
        if len(pairs) == 0:
            print("No path pairs found, the output will be empty.",
                  file=error_file,
                  flush=True)
        elif verbose:
            print("%d path pairs found" % len(pairs),
                  file=error_file,
                  flush=True)

        if verbose:
            print("Reading the input file: %s" % str(input_kgtk_file),
                  file=error_file,
                  flush=True)
        kr: KgtkReader = KgtkReader.open(
            input_kgtk_file,
            error_file=error_file,
            options=input_reader_options,
            value_options=value_options,
            verbose=verbose,
            very_verbose=very_verbose,
        )

        sub_index: int = kr.get_node1_column_index()
        if sub_index < 0:
            print("Missing node1 (subject) column.",
                  file=error_file,
                  flush=True)
        pred_index: int = kr.get_label_column_index()
        if pred_index < 0:
            print("Missing label (predicate) column.",
                  file=error_file,
                  flush=True)
        obj_index: int = kr.get_node2_column_index()
        if obj_index < 0:
            print("Missing node2 (object) column", file=error_file, flush=True)
        id_index: int = kr.get_id_column_index()
        if id_index < 0:
            print("Missing id column", file=error_file, flush=True)
        if sub_index < 0 or pred_index < 0 or obj_index < 0 or id_index < 0:
            kr.close()
            raise KGTKException("Exiting due to missing columns.")

        predicate: str = kr.column_names[pred_index]
        id_col_name: str = kr.column_names[id_index]

        G = load_graph_from_kgtk(kr,
                                 directed=not undirected,
                                 ecols=(sub_index, obj_index),
                                 verbose=verbose,
                                 out=error_file)

        output_columns: typing.List[str] = ['node1', 'label', 'node2', 'id']
        kw: KgtkWriter = KgtkWriter.open(output_columns,
                                         output_kgtk_file,
                                         mode=KgtkWriter.Mode.EDGE,
                                         require_all_columns=True,
                                         prohibit_extra_columns=True,
                                         fill_missing_columns=False,
                                         verbose=verbose,
                                         very_verbose=very_verbose)

        id_count = 0
        if not statistics_only:
            for e in G.edges():
                sid, oid = e
                lbl = G.ep[predicate][e]
                kw.write([
                    G.vp[id_col][sid], lbl, G.vp[id_col][oid],
                    '{}-{}-{}'.format(G.vp[id_col][sid], lbl, id_count)
                ])
                id_count += 1
            if verbose:
                print("%d edges found." % id_count,
                      file=error_file,
                      flush=True)

        id_count = 0
        path_id = 0
        for pair in pairs:
            source_node, target_node = pair
            source_ids = find_vertex(G,
                                     prop=G.properties[('v', id_col)],
                                     match=source_node)
            target_ids = find_vertex(G,
                                     prop=G.properties[('v', id_col)],
                                     match=target_node)
            if len(source_ids) == 1 and len(target_ids) == 1:
                source_id = source_ids[0]
                target_id = target_ids[0]
                if shortest_path:
                    _all_paths = all_shortest_paths(G,
                                                    source_id,
                                                    target_id,
                                                    edges=True)
                else:
                    _all_paths = all_paths(G,
                                           source_id,
                                           target_id,
                                           cutoff=max_hops,
                                           edges=True)

                for path in _all_paths:
                    for edge_num, an_edge in enumerate(path):
                        edge_id = G.properties[('e', 'id')][an_edge]
                        node1: str = 'p%d' % path_id
                        kw.write([
                            node1,
                            str(edge_num), edge_id,
                            '{}-{}-{}'.format(node1, edge_num, id_count)
                        ])
                        id_count += 1
                    path_id += 1

        if verbose:
            print("%d paths contining %d edges found." % (path_id, id_count),
                  file=error_file,
                  flush=True)

        kw.close()
        kr.close()

    except Exception as e:
        raise KGTKException('Error: ' + str(e))
Esempio n. 4
0
 def get_optimal_action(self, v_curr, v_goal):
     paths = list(topology.all_shortest_paths(self.graph, v_curr, v_goal))
     return paths[0][1]
Esempio n. 5
0
def all_shortest_paths(g,
                       source,
                       target,
                       directed=None,
                       weights=None,
                       combine_weights="mean"):
    '''
    Yields all shortest paths from `source` to `target`.
    The algorithms returns an empty generator if there is no path between the
    nodes.

    Parameters
    ----------
    g : :class:`~nngt.Graph`
        Graph to analyze.
    source : int
        Node from which the paths starts.
    target : int, optional (default: all nodes)
        Node where the paths ends.
    directed : bool, optional (default: ``g.is_directed()``)
        Whether the edges should be considered as directed or not
        (automatically set to False if `g` is undirected).
    weights : str or array, optional (default: binary)
        Whether to use weighted edges to compute the distances. By default,
        all edges are considered to have distance 1.
    combine_weights : str, optional (default: 'mean')
        How to combine the weights of reciprocal edges if the graph is directed
        but `directed` is set to False. It can be:

        * "sum": the sum of the edge attribute values will be used for the new
          edge.
        * "mean": the mean of the edge attribute values will be used for the
          new edge.
        * "min": the minimum of the edge attribute values will be used for the
          new edge.
        * "max": the maximum of the edge attribute values will be used for the
          new edge.

    Returns
    -------
    all_paths : generator
        Generator yielding paths as lists of ints.

    References
    ----------
    .. [gt-sd] :gtdoc:`topology.all_shortest_paths`
    '''
    # source == target case
    if source == target:
        return ([source] for _ in range(1))

    # not trivial cases
    g, graph, w = _get_gt_graph(g,
                                directed,
                                weights,
                                combine_weights,
                                return_all=True)

    w = _get_gt_weights(g, w)

    all_paths = gtt.all_shortest_paths(graph, source, target, weights=w)

    return ([int(v) for v in path] for path in all_paths)