1515
1616
1717def _iter_edges (G : Graph ) -> Iterable [Tuple [int , int , float ]]:
18- """
19- Iterates over all edges in the given graph.
18+ """Iterate over all edges in the given graph.
2019
2120 Args:
22- G (Graph) : The graph object containing adjacency information.
21+ G: The graph object containing adjacency information.
2322
2423 Yields:
25- Tuple[int, int, float]: A tuple representing an edge in the graph,
26- where the first element is the source node index (u),
27- the second element is the destination node index (v),
28- and the third element is the edge weight (w).
24+ A tuple representing an edge in the graph, where the first element
25+ is the source node index (u), the second element is the destination
26+ node index (v), and the third element is the edge weight (w).
2927 """
3028 for u in range (G .n ):
3129 for v , w in G .adj [u ]:
3230 yield u , v , w
3331
3432
3533def _read_csv (path : Path ) -> Tuple [int , EdgeList ]:
36- """
37- Reads a CSV file containing edge data and returns the number of nodes
38- and the edge list.
34+ """Read a CSV file containing edge data and return the number of nodes and edge list.
3935
4036 Each line in the file should contain at least three columns: source node,
41- target node, and edge weight.
42- Lines starting with '#' or empty lines are ignored. Columns can be
43- separated by commas or tabs.
37+ target node, and edge weight. Lines starting with '#' or empty lines are
38+ ignored. Columns can be separated by commas or tabs.
4439
4540 Args:
46- path (Path) : The path to the CSV file.
41+ path: The path to the CSV file.
4742
4843 Returns:
49- Tuple[int, EdgeList]: A tuple containing the number of nodes
50- (max node id + 1) and a list of edges,
51- where each edge is represented as a tuple (u, v, w).
44+ A tuple containing the number of nodes (max node id + 1) and a list
45+ of edges, where each edge is represented as a tuple (u, v, w).
5246
5347 Raises:
5448 GraphFormatError: If no edges are parsed from the file.
@@ -77,38 +71,32 @@ def _read_csv(path: Path) -> Tuple[int, EdgeList]:
7771
7872
7973def _write_csv (path : Path , G : Graph ) -> None :
80- """
81- Writes the edges of a graph to a CSV file.
74+ """Write the edges of a graph to a CSV file.
8275
8376 Each row in the CSV file represents an edge in the graph, with columns for
8477 the source node, target node, and edge weight.
8578
8679 Args:
87- path (Path): The file path where the CSV will be written.
88- G (Graph): The graph whose edges will be written to the CSV file.
89-
90- Returns:
91- None
80+ path: The file path where the CSV will be written.
81+ G: The graph whose edges will be written to the CSV file.
9282 """
9383 with path .open ("w" , encoding = "utf-8" ) as fh :
9484 for u , v , w in _iter_edges (G ):
9585 fh .write (f"{ u } ,{ v } ,{ w } \n " )
9686
9787
9888def _read_jsonl (path : Path ) -> Tuple [int , EdgeList ]:
99- """
100- Reads a JSON Lines (JSONL) file containing graph edges and returns the
101- number of nodes and the edge list.
89+ """Read a JSON Lines (JSONL) file containing graph edges.
10290
10391 Each line in the file should be a JSON object with keys "u", "v", and "w",
10492 representing the source node, target node, and edge weight, respectively.
10593
10694 Args:
107- path (Path) : Path to the JSONL file.
95+ path: Path to the JSONL file.
10896
10997 Returns:
110- Tuple[int, EdgeList]: A tuple containing the number of nodes
111- (max node id + 1) and a list of edges as (u, v, w) tuples.
98+ A tuple containing the number of nodes (max node id + 1) and a list
99+ of edges as (u, v, w) tuples.
112100
113101 Raises:
114102 GraphFormatError: If no edges are parsed from the file.
@@ -132,45 +120,37 @@ def _read_jsonl(path: Path) -> Tuple[int, EdgeList]:
132120
133121
134122def _write_jsonl (path : Path , G : Graph ) -> None :
135- """
136- Writes the edges of a graph to a file in JSON Lines (JSONL) format.
123+ """Write the edges of a graph to a file in JSON Lines (JSONL) format.
137124
138- Each line in the output file represents an edge as a JSON object
125+ Each line in the output file represents an edge as a JSON object
139126 with keys 'u', 'v', and 'w', corresponding to the source node, target node,
140127 and edge weight, respectively.
141128
142129 Args:
143- path (Path): The file path where the JSONL data will be written.
144- G (Graph): The graph object containing the edges to be serialized.
145-
146- Returns:
147- None
130+ path: The file path where the JSONL data will be written.
131+ G: The graph object containing the edges to be serialized.
148132 """
149133 with path .open ("w" , encoding = "utf-8" ) as fh :
150134 for u , v , w in _iter_edges (G ):
151135 fh .write (json .dumps ({"u" : u , "v" : v , "w" : w }) + "\n " )
152136
153137
154138def _read_mtx (path : Path ) -> Tuple [int , EdgeList ]:
155- """
156- Reads a Matrix Market (.mtx) file and extracts the edge list.
139+ """Read a Matrix Market (.mtx) file and extract the edge list.
157140
158141 Args:
159- path (Path) : Path to the Matrix Market file.
142+ path: Path to the Matrix Market file.
160143
161144 Returns:
162- Tuple[int, EdgeList]: (
163- A tuple containing the number of nodes (n) and the edge list.
164- The edge list is a list of tuples (u, v, w), where u and v
165- are zero-based node indices, and w is the edge weight.
166- )
145+ A tuple containing the number of nodes (n) and the edge list.
146+ The edge list is a list of tuples (u, v, w), where u and v
147+ are zero-based node indices, and w is the edge weight.
167148
168149 Notes:
169- - Lines starting with '%' are treated as comments and skipped.
170- - Assumes the file contains at least three columns: source, target,
171- and weight.
172- - Node indices in the file are assumed to be 1-based and are
173- converted to 0-based.
150+ Lines starting with '%' are treated as comments and skipped.
151+ Assumes the file contains at least three columns: source, target,
152+ and weight. Node indices in the file are assumed to be 1-based
153+ and are converted to 0-based.
174154 """
175155 edges : EdgeList = []
176156 it = path .open ("r" , encoding = "utf-8" )
@@ -195,22 +175,16 @@ def _read_mtx(path: Path) -> Tuple[int, EdgeList]:
195175
196176
197177def _write_mtx (path : Path , G : Graph ) -> None :
198- """
199- Writes the given graph `G` to a Matrix Market (.mtx) file at the specified
200- path.
178+ """Write the given graph to a Matrix Market (.mtx) file.
201179
202180 The output file will contain the graph's adjacency matrix in coordinate
203181 format, where each line represents an edge with its source node, target
204182 node, and weight. Node indices are written as 1-based (Matrix Market
205183 convention).
206184
207185 Args:
208- path (Path): The file path where the Matrix Market file will be
209- written.
210- G (Graph): The graph object containing nodes and weighted edges.
211-
212- Returns:
213- None
186+ path: The file path where the Matrix Market file will be written.
187+ G: The graph object containing nodes and weighted edges.
214188 """
215189 edges = list (_iter_edges (G ))
216190 with path .open ("w" , encoding = "utf-8" ) as fh :
@@ -221,16 +195,16 @@ def _write_mtx(path: Path, G: Graph) -> None:
221195
222196
223197def _read_graphml (path : Path ) -> Tuple [int , EdgeList ]:
224- """
225- Parse a GraphML file and extract the edge list.
198+ """Parse a GraphML file and extract the edge list.
226199
227200 Args:
228- path (Path): Path to the GraphML file.
201+ path: Path to the GraphML file.
202+
229203 Returns:
230- Tuple[int, EdgeList]: A tuple containing the number of nodes
231- (as max node id + 1) and a list of edges, where each edge is
232- represented as a tuple (u, v, w) with integer node IDs and
233- float weights.
204+ A tuple containing the number of nodes (as max node id + 1) and a
205+ list of edges, where each edge is represented as a tuple (u, v, w)
206+ with integer node IDs and float weights.
207+
234208 Raises:
235209 GraphFormatError: If no edges are parsed from the file.
236210 """
@@ -265,20 +239,18 @@ def _read_graphml(path: Path) -> Tuple[int, EdgeList]:
265239
266240
267241def _write_graphml (path : Path , G : Graph ) -> None :
268- """
269- Writes the given graph `G` to a GraphML file at the specified `path`.
242+ """Write the given graph to a GraphML file.
270243
271244 Args:
272- path (Path) : The file path where the GraphML output will be written.
273- G (Graph) : The graph object to serialize, expected to have an
274- attribute `n` for the number of nodes.
245+ path: The file path where the GraphML output will be written.
246+ G: The graph object to serialize, expected to have an attribute `n`
247+ for the number of nodes.
275248
276249 Notes:
277- - The function assumes the graph is directed.
278- - Each node is assigned an ID in the format "n{i}".
279- - Edges are written with source, target, and weight attributes.
280- - The helper function `_iter_edges(G)` should yield tuples of (u, v, w)
281- for each edge.
250+ The function assumes the graph is directed. Each node is assigned an
251+ ID in the format "n{i}". Edges are written with source, target, and
252+ weight attributes. The helper function `_iter_edges(G)` should yield
253+ tuples of (u, v, w) for each edge.
282254 """
283255 lines : List [str ] = []
284256 lines .append ('<?xml version="1.0" encoding="UTF-8"?>' )
@@ -309,16 +281,14 @@ def _write_graphml(path: Path, G: Graph) -> None:
309281
310282
311283def _detect_format (path : Path ) -> Optional [str ]:
312- """
313- Detects the file format based on the file extension.
284+ """Detect the file format based on the file extension.
314285
315286 Args:
316- path (Path) : The path to the file whose format is to be detected.
287+ path: The path to the file whose format is to be detected.
317288
318289 Returns:
319- Optional[str]: The detected format as a string
320- ("csv", "jsonl", "mtx", "graphml"), or None if the format is not
321- recognized.
290+ The detected format as a string ("csv", "jsonl", "mtx", "graphml"),
291+ or None if the format is not recognized.
322292 """
323293 ext = path .suffix .lower ()
324294 if ext in {".csv" , ".tsv" }:
@@ -333,16 +303,14 @@ def _detect_format(path: Path) -> Optional[str]:
333303
334304
335305def read_graph (path : str , fmt : Optional [str ] = None ) -> Graph :
336- """
337- Reads a graph from a file in the specified format.
306+ """Read a graph from a file in the specified format.
338307
339308 Args:
340- path (str): The path to the graph file.
341- fmt (Optional[str], optional): The format of the graph file. If None,
342- the format is auto-detected.
309+ path: The path to the graph file.
310+ fmt: The format of the graph file. If None, the format is auto-detected.
343311
344312 Returns:
345- Graph: The graph object constructed from the file.
313+ The graph object constructed from the file.
346314
347315 Raises:
348316 GraphFormatError: If the graph format is unknown or unsupported.
@@ -356,14 +324,13 @@ def read_graph(path: str, fmt: Optional[str] = None) -> Graph:
356324
357325
358326def write_graph (G : Graph , path : str , fmt : Optional [str ] = None ) -> None :
359- """
360- Writes a graph to a file in the specified format.
327+ """Write a graph to a file in the specified format.
361328
362- Parameters :
363- G (Graph) : The graph object to be written.
364- path (str) : The file path where the graph will be saved.
365- fmt (Optional[str], optional) : The format to use for writing the graph.
366- If None, the format is auto-detected from the file extension.
329+ Args :
330+ G: The graph object to be written.
331+ path: The file path where the graph will be saved.
332+ fmt: The format to use for writing the graph. If None, the format is
333+ auto-detected from the file extension.
367334
368335 Raises:
369336 GraphFormatError: If the format is unknown or unsupported.
@@ -376,17 +343,16 @@ def write_graph(G: Graph, path: str, fmt: Optional[str] = None) -> None:
376343
377344
378345def load_graph (path : str , fmt : Optional [str ] = None ) -> Graph :
379- """
380- Loads a graph from the specified file path.
346+ """Load a graph from the specified file path.
381347
382348 This function is deprecated; use `read_graph` instead.
383349
384350 Args:
385- path (str) : The path to the graph file.
386- fmt (Optional[str], optional) : The format of the graph file. Defaults to None.
351+ path: The path to the graph file.
352+ fmt: The format of the graph file. Defaults to None.
387353
388354 Returns:
389- Graph: The loaded graph object.
355+ The loaded graph object.
390356
391357 Deprecated:
392358 Since version 0.1.0. Will be removed in version 0.2.0. Use `read_graph` instead.
0 commit comments