def ensure_dependencies(request): r""" CommandLine: python -m dtool.base --exec-BaseRequest.ensure_dependencies Example: >>> # ENABLE_DOCTEST >>> from dtool.base import * # NOQA >>> from dtool.example_depcache import testdata_depc >>> depc = testdata_depc() >>> request = depc.new_request('vsmany', [1, 2], [2, 3, 4]) >>> request.ensure_dependencies() """ import networkx as nx depc = request.depc if False: dependencies = nx.ancestors(depc.graph, request.tablename) subgraph = depc.graph.subgraph(set.union(dependencies, {request.tablename})) dependency_order = nx.topological_sort(subgraph) root = dependency_order[0] [nx.algorithms.dijkstra_path(subgraph, root, start)[:-1] + nx.algorithms.dijkstra_path(subgraph, start, request.tablename) for start in dependency_order] graph = depc.graph root = list(nx.topological_sort(graph))[0] edges = graph.edges() #parent_to_children = ut.edges_to_adjacency_list(edges) child_to_parents = ut.edges_to_adjacency_list([t[::-1] for t in edges]) to_root = {request.tablename: ut.paths_to_root(request.tablename, root, child_to_parents)} from_root = ut.reverse_path(to_root, root, child_to_parents) dependency_levels_ = ut.get_levels(from_root) dependency_levels = ut.longest_levels(dependency_levels_) true_order = ut.flatten(dependency_levels)[1:-1] #print('[req] Ensuring %s request dependencies: %r' % (request, true_order,)) ut.colorprint( '[req] Ensuring request %s dependencies: %r' % (request, true_order,), 'yellow') for tablename in true_order: table = depc[tablename] if table.ismulti: pass else: # HACK FOR IBEIS all_aids = ut.flat_unique(request.qaids, request.daids) depc.get_rowids(tablename, all_aids) pass pass #zip(depc.get_implicit_edges()) #zip(depc.get_implicit_edges()) #raise NotImplementedError('todo') #depc = request.depc #parent_rowids = request.parent_rowids #config = request.config #rowid_dict = depc.get_all_descendant_rowids( # request.tablename, root_rowids, config=config) pass
def get_ancestor_levels(graph, tablename): import networkx as nx import utool as ut root = nx.topological_sort(graph)[0] reverse_edges = [(e2, e1) for e1, e2 in graph.edges()] child_to_parents = ut.edges_to_adjacency_list(reverse_edges) to_root = ut.paths_to_root(tablename, root, child_to_parents) from_root = ut.reverse_path(to_root, root, child_to_parents) ancestor_levels_ = ut.get_levels(from_root) ancestor_levels = ut.longest_levels(ancestor_levels_) return ancestor_levels
def get_dependencies(depc, tablename): """ gets level dependences from root to tablename CommandLine: python -m ibeis.depends_cache --exec-get_dependencies --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.depends_cache import * # NOQA >>> depc = testdata_depc() >>> tablename = 'fgweight' >>> result = ut.repr3(depc.get_dependencies(tablename), nl=1) >>> print(result) [ ['dummy_annot'], ['chip', 'probchip'], ['keypoint'], ['fgweight'], ] Example: >>> # ENABLE_DOCTEST >>> from ibeis.depends_cache import * # NOQA >>> depc = testdata_depc() >>> tablename = 'spam' >>> result = ut.repr3(depc.get_dependencies(tablename), nl=1) >>> print(result) [ ['dummy_annot'], ['chip', 'probchip'], ['keypoint'], ['fgweight'], ['spam'], ] """ root = depc.root_tablename children_, parents_ = list(zip(*depc.get_edges())) child_to_parents = ut.group_items(children_, parents_) to_root = {tablename: ut.paths_to_root(tablename, root, child_to_parents)} from_root = ut.reverse_path(to_root, root, child_to_parents) dependency_levels_ = ut.get_levels(from_root) dependency_levels = ut.longest_levels(dependency_levels_) #print('child_to_parents = %s' % (ut.repr3(child_to_parents),)) #print('to_root = %r' % (to_root,)) #print('from_root = %r' % (from_root,)) return dependency_levels