Ejemplo n.º 1
0
    def load_graph_from_file(self, file_path):
        """Load a graph file from disk and handle version."""

        graph = libdeps.graph.LibdepsGraph(networkx.read_graphml(file_path))
        if graph.graph['graph_schema_version'] == 1:
            self._dependents_graph = graph
            self._dependency_graph = networkx.reverse_view(
                self._dependents_graph)
        else:
            self._dependency_graph = graph
            self._dependents_graph = networkx.reverse_view(
                self._dependency_graph)
Ejemplo n.º 2
0
    def __init__(self, graphml_dir, frontend_url):
        """Create and setup the state variables."""
        self.app = flask.Flask(__name__)
        self.socketio = SocketIO(self.app, cors_allowed_origins=frontend_url)
        self.app.config['CORS_HEADERS'] = 'Content-Type'
        CORS(self.app, resources={r"/*": {"origins": frontend_url}})

        self.app.add_url_rule("/graph_files", "return_graph_files",
                              self.return_graph_files)
        self.socketio.on_event('git_hash_selected', self.git_hash_selected)
        self.socketio.on_event('row_selected', self.row_selected)

        self.loaded_graphs = {}
        self.current_selected_rows = {}
        self.graphml_dir = Path(graphml_dir)
        self.frontend_url = frontend_url

        self.graph_file_tuple = namedtuple(
            'GraphFile', ['version', 'git_hash', 'graph_file'])
        self.graph_files = self.get_graphml_files()

        try:
            default_selected_graph = list(
                self.graph_files.items())[0][1].graph_file
            self.load_graph_from_file(default_selected_graph)
            self._dependents_graph = networkx.reverse_view(
                self._dependency_graph)
        except (IndexError, AttributeError) as ex:
            print(ex)
            print(
                f"Failed to load read a graph file from {list(self.graph_files.items())} for graphml_dir '{self.graphml_dir}'"
            )
            exit(1)
Ejemplo n.º 3
0
    def get_dependency_graph(graph):
        """Returns the dependency graph of a given graph."""

        if graph.graph['graph_schema_version'] == 1:
            return networkx.reverse_view(graph)
        else:
            return graph
Ejemplo n.º 4
0
    def load_graph(self, message):
        """Load the graph into application memory and kick off threads for analysis on new graph."""

        with self.app.test_request_context():

            current_hash = self._dependents_graph.graph.get(
                'git_hash', 'NO_HASH')[:7]
            if current_hash != message['hash']:
                self.current_selected_rows = {}
                if message['hash'] in self.loaded_graphs:
                    self._dependents_graph = self.loaded_graphs[
                        message['hash']]
                    self._dependents_graph = networkx.reverse_view(
                        self._dependency_graph)
                else:
                    print(
                        f'loading new graph {current_hash} because different than {message["hash"]}'
                    )

                    self.load_graph_from_file(
                        self.graph_files[message['hash']].graph_file)
                    self.loaded_graphs[
                        message['hash']] = self._dependents_graph

            self.socketio.start_background_task(self.analyze_counts)
            self.socketio.start_background_task(self.send_node_list)
            self.socketio.emit("graph_data",
                               {'graphData': {
                                   'nodes': [],
                                   'links': []
                               }})
Ejemplo n.º 5
0
def count_ngrams(
    word_graph: nx.DiGraph,
    start_node: int,
    end_node: int,
    pad_start="<s>",
    pad_end="</s>",
    label="word",
    order=3,
) -> Counter:
    """Compute n-gram counts in a word graph."""
    assert order > 0, "Order must be greater than zero"
    n_data = word_graph.nodes(data=True)

    # Counts from a node to <s>
    up_counts: Counter = Counter()

    # Counts from a node to </s>
    down_counts: Counter = Counter()

    # Top/bottom = 1
    up_counts[start_node] = 1
    down_counts[end_node] = 1

    # Skip start node
    for n in itertools.islice(nx.topological_sort(word_graph), 1, None):
        for n2 in word_graph.predecessors(n):
            up_counts[n] += up_counts[n2]

    # Down
    reverse_graph = nx.reverse_view(word_graph)

    # Skip end node
    for n in itertools.islice(nx.topological_sort(reverse_graph), 1, None):
        for n2 in reverse_graph.predecessors(n):
            down_counts[n] += down_counts[n2]

    # Compute counts
    ngram_counts: Counter = Counter()
    for n in word_graph:
        # Unigram
        word = n_data[n][label]
        ngram = [word]
        ngram_counts[tuple(ngram)] += up_counts[n] * down_counts[n]

        if order == 1:
            continue

        # Higher order
        q = deque([(n, ngram)])
        while q:
            current_node, current_ngram = q.popleft()
            for n2 in word_graph.predecessors(current_node):
                word_n2 = n_data[n2][label]
                ngram_n2 = [word_n2] + current_ngram
                ngram_counts[tuple(ngram_n2)] += up_counts[n2] * down_counts[n]

                if len(ngram_n2) < order:
                    q.append((n2, ngram_n2))

    return ngram_counts
Ejemplo n.º 6
0
def main():
    """Perform graph analysis based on input args."""

    args = setup_args_parser()
    graph = load_graph_data(args.graph_file, args.format)
    libdeps_graph = LibdepsGraph(graph=graph)
    build_dir = libdeps_graph.graph['build_dir']

    if libdeps_graph.graph['graph_schema_version'] == 1:
        libdeps_graph = networkx.reverse_view(libdeps_graph)

    analysis = libdeps_analyzer.counter_factory(libdeps_graph, args.counts)

    for analyzer_args in args.direct_depends:
        analysis.append(
            libdeps_analyzer.DirectDependents(
                libdeps_graph, strip_build_dir(build_dir, analyzer_args)))

    for analyzer_args in args.common_depends:
        analysis.append(
            libdeps_analyzer.CommonDependents(
                libdeps_graph, strip_build_dirs(build_dir, analyzer_args)))

    for analyzer_args in args.exclude_depends:
        analysis.append(
            libdeps_analyzer.ExcludeDependents(
                libdeps_graph, strip_build_dirs(build_dir, analyzer_args)))

    for analyzer_args in args.graph_paths:
        analysis.append(
            libdeps_analyzer.GraphPaths(
                libdeps_graph, strip_build_dir(build_dir, analyzer_args[0]),
                strip_build_dir(build_dir, analyzer_args[1])))

    for analyzer_args in args.critical_edges:
        analysis.append(
            libdeps_analyzer.CriticalEdges(
                libdeps_graph, strip_build_dir(build_dir, analyzer_args[0]),
                strip_build_dir(build_dir, analyzer_args[1])))

    if args.indegree_one:
        analysis.append(libdeps_analyzer.InDegreeOne(libdeps_graph))

    analysis += libdeps_analyzer.linter_factory(libdeps_graph, args.lint)

    if args.build_data:
        analysis.append(libdeps_analyzer.BuildDataReport(libdeps_graph))

    ga = libdeps_analyzer.LibdepsGraphAnalysis(analysis)

    if args.format == 'pretty':
        ga_printer = libdeps_analyzer.GaPrettyPrinter(ga)
    elif args.format == 'json':
        ga_printer = libdeps_analyzer.GaJsonPrinter(ga)
    else:
        return

    ga_printer.print()
Ejemplo n.º 7
0
    def _libdeps(graph_file):
        libdeps_graph = LibdepsGraph(graph=networkx.read_graphml(graph_file))

        if libdeps_graph.graph['graph_schema_version'] == 1:
            libdeps_graph = networkx.reverse_view(libdeps_graph)

        return GaJsonPrinter(
            LibdepsGraphAnalysis(
                counter_factory(libdeps_graph,
                                CountTypes.ALL.name))).get_json()
def reverseWeightedDG(dg):
    rdg = nx.reverse_view(dg)

    for e in rdg.edges():
        dg_edge = Reverse(e)
        dg_weight = dg[e[1]][e[0]]['weight']

        rdg_weight = reverseEdgeWeight(dg_weight)
        rdg[e[0]][e[1]]['weight'] = rdg_weight

    return rdg
Ejemplo n.º 9
0
    def test_subclass(self):
        class MyGraph(nx.DiGraph):
            def my_method(self):
                return "me"
            def to_directed_class(self):
                return MyGraph()

        M = MyGraph()
        M.add_edge(1, 2)
        RM = nx.reverse_view(M)
        print("RM class", RM.__class__)
        RMC = RM.copy()
        print("RMC class", RMC.__class__)
        print(RMC.edges)
        assert RMC.has_edge(2, 1)
        assert RMC.my_method() == "me"
Ejemplo n.º 10
0
    def test_subclass(self):
        class MyGraph(nx.DiGraph):
            def my_method(self):
                return "me"
            def to_directed_class(self):
                return MyGraph()

        M = MyGraph()
        M.add_edge(1, 2)
        RM = nx.reverse_view(M)
        print("RM class",RM.__class__)
        RMC = RM.copy()
        print("RMC class",RMC.__class__)
        print(RMC.edges)
        assert_true(RMC.has_edge(2, 1))
        assert_equal(RMC.my_method(), "me")
Ejemplo n.º 11
0
    def __init__(self, rob):
        '''
        The `rob` argument must be a kinematic tree model with a numbering
        scheme.
        '''
        if not hasattr(rob, 'dgraph'):
            raise RuntimeError('''Tree-utils can only be constructed with
            an ordered robot model, i.e. one whose treeModel graph is
            directed''')
        if rob.hasLoops():
            raise RuntimeError(
                "Kinematic loops detected, TreeUtils only accepts kinematic trees"
            )

        self.robot = rob
        self.parentToChild = rob.dgraph
        self.childToParent = nx.reverse_view(self.parentToChild)
Ejemplo n.º 12
0
    def run(self):
        """Find all paths between the two nodes in the graph."""

        # We can really help out networkx path finding algorithm by striping the graph down to
        # just a graph containing only paths between the source and target node. This is done by
        # getting a subtree from the target down, and then getting a subtree of that tree from the
        # source up.
        dependents_tree = self._dependents_graph.get_direct_nonprivate_graph(
        ).get_node_tree(self._to_node)

        if self._from_node not in dependents_tree:
            return []

        path_tree = networkx.reverse_view(dependents_tree).get_node_tree(
            self._from_node)
        return list(
            networkx.all_simple_paths(G=path_tree,
                                      source=self._from_node,
                                      target=self._to_node))
Ejemplo n.º 13
0
def export(output, graph, infrastructures, demand, budget):
  """
  Export the solver data in mathprog data format.
  """
  w = partial(write, output)
  w('data;\n')

  ctx = SolverContext(graph, infrastructures, demand, budget)
  reverse_graph = nx.reverse_view(graph)

  nodes = graph.nodes()
  edges = list(map(edge_name, graph.edges()))
  w(f'set nodes := {fl(nodes)};')
  w(f'set edges := {fl(edges)};')
  w()

  for n in nodes:
    adjs = reverse_graph.adj[n]
    adj_edges = map(edge_name, [(a, n) for a in adjs])
    w(f'set inbound[{n}] := {fl(adj_edges)};')
  w()
  
  for n in nodes:
    adjs = graph.adj[n]
    adj_edges = map(edge_name, [(n, a) for a in adjs])
    w(f'set outbound[{n}] := {fl(adj_edges)};')
  w()
  
  infras_count = len(infrastructures.get('cost_factors')) + 1
  w(f'set infras := {fl(map(str, range(infras_count)))};')
  w()

  ods = fl([f'({o}, {d})' for o, d in demand.keys()])
  w(f'set ods := {ods};')
  w()

  w(f'param demand := ', line_break=False)
  for od, value in demand.items():
    o, d = od
    w()
    w(f'  [{o}, {d}] {value}', line_break=False)
  w(';\n')

  w(f'param budget := {budget};')

  w(f'param weight :=', line_break=False)
  for edge in ctx.current_graph.edges(keys=True):
    n1, n2, infra = edge
    weight = ctx.current_graph.edges[edge][configuration.arc_weight_key]
    w()
    w(f'  [{edge_name([n1, n2])}, {infra}] {weight}', line_break=False)
  w(';\n')

  w(f'param construction_cost :=', line_break=False)
  for edge in ctx.current_graph.edges(keys=True):
    n1, n2, infra = edge
    construction_cost = ctx.current_graph.edges[edge][configuration.arc_cost_key]
    w()
    w(f'  [{edge_name([n1, n2])}, {infra}] {construction_cost}', line_break=False)
  w(';\n')

  w('end;')
Ejemplo n.º 14
0
 def _dependency_graph(self):
     if not hasattr(self, 'graph'):
         setattr(self, 'graph',
                 networkx.reverse_view(self._dependents_graph))
     return self.graph
Ejemplo n.º 15
0
 def setup(self):
     self.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.G.add_edge(4, 5)
     self.rv = nx.reverse_view(self.G)
Ejemplo n.º 16
0
 def setup(self):
     self.G = nx.path_graph(9, create_using=nx.DiGraph())
     self.rv = nx.reverse_view(self.G)
Ejemplo n.º 17
0
 def setup(self):
     self.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.G.add_edge(4, 5)
     self.rv = nx.reverse_view(self.G)
Ejemplo n.º 18
0
 def setup(self):
     self.G = nx.path_graph(9, create_using=nx.DiGraph())
     self.rv = nx.reverse_view(self.G)
Ejemplo n.º 19
0
if components_count == 1:
    print('Слабосвязный')
else:
    print('Не слабосвязный')

print('-' * 100)
print('Доля узлов наибольшей компоненты слабой связности:',
      len(component_max) / len(G.nodes()))

# Сильная связность

print('-' * 100)
print('Компоненты сильной связности')

Gt = nx.reverse_view(G)
conn_strong_trans = {i: list(Gt.neighbors(i)) for i in Gt}
# conn_strong_trans = {i: conn_strong[i][::-1] for i in conn_strong}

nodes_tout = {i: -1 for i in G.nodes()}
components_count = 0
tout = 0

for i in G.nodes():
    if nodes_tout[i] == -1:
        components_count += 1
        component = dfs(i, conn_strong_trans)

# nodes_visited = {i: False for i in G.nodes()}
fu, nodes_tout = nodes_tout, {i: -1 for i in G.nodes()}