Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/readwrite/leda.py: 24%

37 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1""" 

2Read graphs in LEDA format. 

3 

4LEDA is a C++ class library for efficient data types and algorithms. 

5 

6Format 

7------ 

8See http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html 

9 

10""" 

11# Original author: D. Eppstein, UC Irvine, August 12, 2003. 

12# The original code at http://www.ics.uci.edu/~eppstein/PADS/ is public domain. 

13 

14__all__ = ["read_leda", "parse_leda"] 

15 

16import networkx as nx 

17from networkx.exception import NetworkXError 

18from networkx.utils import open_file 

19 

20 

21@open_file(0, mode="rb") 

22@nx._dispatch(graphs=None) 

23def read_leda(path, encoding="UTF-8"): 

24 """Read graph in LEDA format from path. 

25 

26 Parameters 

27 ---------- 

28 path : file or string 

29 File or filename to read. Filenames ending in .gz or .bz2 will be 

30 uncompressed. 

31 

32 Returns 

33 ------- 

34 G : NetworkX graph 

35 

36 Examples 

37 -------- 

38 G=nx.read_leda('file.leda') 

39 

40 References 

41 ---------- 

42 .. [1] http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html 

43 """ 

44 lines = (line.decode(encoding) for line in path) 

45 G = parse_leda(lines) 

46 return G 

47 

48 

49@nx._dispatch(graphs=None) 

50def parse_leda(lines): 

51 """Read graph in LEDA format from string or iterable. 

52 

53 Parameters 

54 ---------- 

55 lines : string or iterable 

56 Data in LEDA format. 

57 

58 Returns 

59 ------- 

60 G : NetworkX graph 

61 

62 Examples 

63 -------- 

64 G=nx.parse_leda(string) 

65 

66 References 

67 ---------- 

68 .. [1] http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html 

69 """ 

70 if isinstance(lines, str): 

71 lines = iter(lines.split("\n")) 

72 lines = iter( 

73 [ 

74 line.rstrip("\n") 

75 for line in lines 

76 if not (line.startswith(("#", "\n")) or line == "") 

77 ] 

78 ) 

79 for i in range(3): 

80 next(lines) 

81 # Graph 

82 du = int(next(lines)) # -1=directed, -2=undirected 

83 if du == -1: 

84 G = nx.DiGraph() 

85 else: 

86 G = nx.Graph() 

87 

88 # Nodes 

89 n = int(next(lines)) # number of nodes 

90 node = {} 

91 for i in range(1, n + 1): # LEDA counts from 1 to n 

92 symbol = next(lines).rstrip().strip("|{}| ") 

93 if symbol == "": 

94 symbol = str(i) # use int if no label - could be trouble 

95 node[i] = symbol 

96 

97 G.add_nodes_from([s for i, s in node.items()]) 

98 

99 # Edges 

100 m = int(next(lines)) # number of edges 

101 for i in range(m): 

102 try: 

103 s, t, reversal, label = next(lines).split() 

104 except BaseException as err: 

105 raise NetworkXError(f"Too few fields in LEDA.GRAPH edge {i+1}") from err 

106 # BEWARE: no handling of reversal edges 

107 G.add_edge(node[int(s)], node[int(t)], label=label[2:-2]) 

108 return G