def test2(): import test_trees G = preprocess_tree(test_trees.test2(), 'sid', creators={}, cnt=Counter(99)) G = upbranch(G, 1, 5, 'sid', cnt=Counter(100)) save_and_draw_graph(G)
def run_pipeline(T, sparse_edges=False, draw=False, save_list=None): cnt = Counter(1000) #check #save_and_draw_graph(T) #sys.exit(0) G = T #filter_edges = [(U, V, k) for U, V, k in G. # edges(keys=True) if # k.startswith('H') or k.startswith('*H*')] #G.remove_edges_from(filter_edges) #save_and_draw_graph(G) #sys.exit(0) G = preprocess_tree(T, 'sid', creators={}, cnt=cnt) G = processing(G, cnt=cnt, phase=1) G = processing(G, cnt=cnt, phase=2) save_and_draw_graph(G) sys.exit(0) G = processing(G, cnt=cnt, phase=3) G = processing(G, cnt=cnt, phase=4) G = processing(G, cnt=cnt, phase=5) if sparse_edges: filter_edges = [(U, V, k) for U, V, k in G.edges(keys=True) if k == 'h'] G.remove_edges_from(filter_edges) filter_edges = [ (U, V, k) for U, V, k in G.edges(keys=True) if k.startswith('pred') or k.startswith('parent') or k.startswith( '*H*') or k.startswith('creator_') or k.startswith('Pre') or k.startswith('H') or k.startswith('_H') or k.startswith('h-s') or k.startswith('h-i') or k.startswith('h-r') or k.startswith('rev') ] G.remove_edges_from(filter_edges) if draw: #G.add_node(4) #G.add_node(5) #G.nodes[4].update({'pid': 4, 'sid': 1, 'pgid': 2}) #G.nodes[5].update({'pid':5,'sid':1,'pgid':3}) #G.add_edge(1003, 5,'fork()') #G.add_edge(1004, 4, 'fork()') #G.add_edge(5, 2, 'setpgid(3,2)') #G.add_edge(4, 3, 'setpgid(2,3)') #G.remove_edge(1003,2) #G.remove_edge(1004, 3) save_and_draw_graph(G) if save_list: pass return G
def test3(): import test_trees G = preprocess_tree(test_trees.test3(), 'sid', creators={}, cnt=Counter(99)) ed = list(G.edges()) G = processing(G, cnt=Counter(100), phase=1) G = processing(G, cnt=Counter(200), phase=2) G = processing(G, cnt=Counter(300), phase=3) G = processing(G, cnt=Counter(400), phase=4) G = processing(G, cnt=Counter(500), phase=5) save_and_draw_graph(G) pass
def run_pipeline(source_tree, save=False, perform=False, show=True, rearrange=False): colors = prepare_colors_from_range() if source_tree == 'runtime': T = get_pstree() elif not source_tree: T = test_trees.test4() else: pass # load_tree_from_file print("TODO loading from file...") return None G = pipeline(T, ctx=Context(), save=save, perform_actions=perform, rearrange=rearrange, color_scheme=colors) if show: save_and_draw_graph(G, num_palette=colors) return G
def test1(): import test_trees # test preprocessing G = preprocess_tree(test_trees.test1(), 'sid', creators={}, cnt=Counter(99)) G = processing(G, cnt=Counter(100), phase=1) G = processing(G, cnt=Counter(200), phase=2) G = processing(G, cnt=Counter(300), phase=3) G = processing(G, cnt=Counter(400), phase=4) save_and_draw_graph(G) sys.exit(0) G = processing(G, cnt=Counter(500), phase=5) save_and_draw_graph(G)
def test4(): import test_trees G = preprocess_tree(test_trees.test4(), 'sid', creators={}, cnt=Counter(50)) ed = list(G.edges()) G = processing(G, cnt=Counter(100), phase=1) G = processing(G, cnt=Counter(200), phase=2) G = processing(G, cnt=Counter(300), phase=3) G = processing(G, cnt=Counter(400), phase=4) G = processing(G, cnt=Counter(500), phase=5) filter_edges = [(U, V, k) for U, V, k in G.edges(keys=True) if k == 'h'] G.remove_edges_from(filter_edges) filter_edges = [(U, V, k) for U, V, k in G.edges(keys=True) if k.startswith('pred') or k.startswith('parent') or k.startswith('creator_') or k.startswith('Pre')] G.remove_edges_from(filter_edges) save_and_draw_graph(G)
def print_fig(G, ctx, suffix): if ctx.per_step_show: save_and_draw_graph(G, num_palette=ctx.colors_dict, pic_name=ctx.compose_name(suffix), show_graph=False) ctx.op_inc()
def pipeline(T, ctx=Context(), A=[],K=[],L=[], CR=dict(), save=False, perform_actions=True, bin_name='native_code/treemaker', rearrange=False, color_scheme = prepare_colors_from_range()): ctx.colors_dict = color_scheme if save: png_name = "graph_" + str(int(time())) save_and_draw_graph(T, num_palette=ctx.colors_dict, save_png=save, pic_name=png_name+"tree.png", show_graph=False) T=preprocess_tree(T, 'sid', creators={}, cnt=Counter(99), ctx=ctx) if save: save_and_draw_graph(T,save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_0.png", show_graph=False) G=process1(T, A, K, L, CR, ctx=ctx) if save: save_and_draw_graph(G,save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_1.png", show_graph=False) G=process2(G, A, K, L, CR, ctx=ctx) if save: save_and_draw_graph(G,save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_2.png", show_graph=False) G=process3(G, A, K, L, CR, ctx=ctx) if save: save_and_draw_graph(G,save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_3.png", show_graph=False) G=process4(G, A, K, L, CR, ctx=ctx) if save: save_and_draw_graph(G,save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_4.png", show_graph=False) G=process5(G, A, K, L, CR, ctx=ctx) if save: save_and_draw_graph(G,save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_5.png", show_graph=False) filter_edges = [(U, V, k) for U, V, k in G.edges(keys=True) if k == 'h'] G.remove_edges_from(filter_edges) filter_edges = [(U, V, k) for U, V, k in G. edges(keys=True) if k.startswith('parent') or k.startswith('*H*') or k.startswith('creator_') or k.startswith('pre') or k.startswith('H') or k.startswith('_H') or k.startswith( 'h-s') or k.startswith('h-i') or k.startswith('h-r') or k.startswith('rev') ] G.remove_edges_from(filter_edges) cnt = Counter(6000) G = post_corr(G, ctx=ctx, cnt=cnt) if rearrange == 1: print("R", rearrange) G = rearrange_indexes(G, 1, full_tree=True) # 1==init, then 2 if save: save_and_draw_graph(G, save_png=save, num_palette=ctx.colors_dict, pic_name=png_name+"_final.png", show_graph=False) if perform_actions: perform(G, bin_name=bin_name) return G
def preprocess_tree(T, attr_name, ctx, creators={}, cnt=Counter(100)): # test it before usage! # setting tree into consistent state (see reparent manual) init = [x for x in T.nodes() if T.in_degree(x) == 0 or x == 1][0] for p in list(T.nodes): try: if not T.nodes[p]['ppid'] in list(T.nodes): T.nodes[p]['ppid'] = init # init value except: print('catch', T.nodes[p], p) print('azaza') # now pstree is consistent subroots = list(T.successors(init)) creators[1] = {init: init} for subroot in subroots: for item_ptr in dfs_tree(T, subroot): attr_val = T.nodes[item_ptr][attr_name] if attr_val == T.nodes[item_ptr][ 'pid']: # creator criteria - replace if generalized creators[attr_val] = { item_ptr: subroot } # check correctness also for subroot in subroots: for item_ptr in dfs_tree(T, subroot): attr_val = T.nodes[item_ptr][attr_name] if not attr_val == T.nodes[item_ptr][ 'pid']: # handle holder - this node is not in current tree creator_location = creators.get(attr_val, None) if creator_location is None: creator = attr_val #cnt.inc() - must be checket to unduplication creators[attr_val] = {creator, creator} T.add_node(creator) T.nodes[creator].update(T.nodes[init]) T.nodes[creator].update({ 'ppid': init, 'pid': attr_val, }) # append entry if T.nodes[item_ptr]['sid'] == attr_val: T.nodes[creator].update({ 'pgid': attr_val, 'sid': attr_val }) else: T.nodes[creator].update({ 'pgid': attr_val, }) T.nodes[creator].update({'status': 0}) T.add_edge(init, creator, key='h-intermediate') try: T.remove_edge(T.nodes[subroot]['ppid'], subroot, key='h') except: pass T.nodes[subroot].update({'ppid': attr_val}) T.add_edge(creator, subroot, 'h-rev_reparent') # append node to root pass elif attr_name in ['sid', 'pgid'] and attr_val == 1: pass else: try: creator_subroot_val = creator_location[next( iter(creator_location))] except TypeError: creator_subroot_val = next(iter(creator_location)) if not creator_subroot_val == subroot: # not from this subtree - condition (*) from scratch creator = next( iter(creator_location )) # eject subtree which contains creator intermediate_node = cnt.inc() T.add_node(intermediate_node) T.nodes[intermediate_node].update(T.nodes[creator]) T.nodes[intermediate_node].update({ 'pid': intermediate_node, 'ppid': creator }) T.nodes[intermediate_node].update({'status': 0}) try: T.remove_edge(T.nodes[subroot]['ppid'], subroot, 'h') except: pass T.nodes[subroot].update( {'ppid': intermediate_node} ) # connect current_subroot_val node to creator via intermediate state T.add_edge(creator, intermediate_node, 'h-intermediate') T.add_edge(intermediate_node, subroot, 'h-stitching_to_subtree') break # dependency is handled now else: # everything is ok continue if ctx.per_step_show: save_and_draw_graph( T, num_palette=ctx.colors_dict, pic_name=ctx.compose_name("_preprocess"), show_graph=False) ctx.op_inc() return T