ioftools / networkxMiCe / networkxmaster / networkx / classes / filters.py @ 5cef0f13
History  View  Annotate  Download (2.01 KB)
1 
# Copyright (C) 20042019 by


2 
# Aric Hagberg <hagberg@lanl.gov>

3 
# Dan Schult <dschult@colgate.edu>

4 
# Pieter Swart <swart@lanl.gov>

5 
# All rights reserved.

6 
# BSD license.

7 
#

8 
# Author: Aric Hagberg (hagberg@lanl.gov),

9 
# Pieter Swart (swart@lanl.gov),

10 
# Dan Schult(dschult@colgate.edu)

11 
"""Filter factories to hide or show sets of nodes and edges.

12 

13 
These filters return the function used when creating `SubGraph`.

14 
"""

15 
__all__ = ['no_filter', 'hide_nodes', 
16 
'hide_edges', 'hide_multiedges', 
17 
'hide_diedges', 'hide_multidiedges', 
18 
'show_nodes',

19 
'show_edges', 'show_multiedges', 
20 
'show_diedges', 'show_multidiedges', 
21 
] 
22  
23  
24 
def no_filter(*items): 
25 
return True 
26  
27  
28 
def hide_nodes(nodes): 
29 
nodes = set(nodes)

30 
return lambda node: node not in nodes 
31  
32  
33 
def hide_diedges(edges): 
34 
edges = {(u, v) for u, v in edges} 
35 
return lambda u, v: (u, v) not in edges 
36  
37  
38 
def hide_edges(edges): 
39 
alledges = set(edges)  {(v, u) for (u, v) in edges} 
40 
return lambda u, v: (u, v) not in alledges 
41  
42  
43 
def hide_multidiedges(edges): 
44 
edges = {(u, v, k) for u, v, k in edges} 
45 
return lambda u, v, k: (u, v, k) not in edges 
46  
47  
48 
def hide_multiedges(edges): 
49 
alledges = set(edges)  {(v, u, k) for (u, v, k) in edges} 
50 
return lambda u, v, k: (u, v, k) not in alledges 
51  
52  
53 
# write show_nodes as a class to make SubGraph pickleable

54 
class show_nodes(object): 
55 
def __init__(self, nodes): 
56 
self.nodes = set(nodes) 
57  
58 
def __call__(self, node): 
59 
return node in self.nodes 
60  
61  
62 
def show_diedges(edges): 
63 
edges = {(u, v) for u, v in edges} 
64 
return lambda u, v: (u, v) in edges 
65  
66  
67 
def show_edges(edges): 
68 
alledges = set(edges)  {(v, u) for (u, v) in edges} 
69 
return lambda u, v: (u, v) in alledges 
70  
71  
72 
def show_multidiedges(edges): 
73 
edges = {(u, v, k) for u, v, k in edges} 
74 
return lambda u, v, k: (u, v, k) in edges 
75  
76  
77 
def show_multiedges(edges): 
78 
alledges = set(edges)  {(v, u, k) for (u, v, k) in edges} 
79 
return lambda u, v, k: (u, v, k) in alledges 