Transformer fundamentals
 
Loading...
Searching...
No Matches
Graph_Dataset.GraphDataset Class Reference

Public Member Functions

 __init__ (self, file_path)
 constructor for GraphDataset object
 
 load_graphs (self)
 Load all graphs from the data file into an adjacency list representation.
 

Public Attributes

 file_path = file_path
 
list graphs = []
 

Detailed Description

Definition at line 31 of file Graph_Dataset.py.

Constructor & Destructor Documentation

◆ __init__()

Graph_Dataset.GraphDataset.__init__ ( self,
file_path )

constructor for GraphDataset object

Parameters
file_path(string): file path where graph data is stored
Exceptions
ParseErrorIf filepath is not valid or format is invalid

Definition at line 32 of file Graph_Dataset.py.

32 def __init__(self, file_path):
33 """
34 @brief constructor for GraphDataset object
35
36 @param file_path (string): file path where graph data is stored
37 @throws ParseError: If filepath is not valid or format is invalid
38 """
39 self.file_path = file_path
40 self.graphs = []
41 self.load_graphs()
42

Member Function Documentation

◆ load_graphs()

Graph_Dataset.GraphDataset.load_graphs ( self)

Load all graphs from the data file into an adjacency list representation.

Definition at line 43 of file Graph_Dataset.py.

43 def load_graphs(self):
44 """Load all graphs from the data file into an adjacency list representation."""
45 try:
46 with open(self.file_path, "r") as f:
47 content = f.read()
48
49 # Split the content by 'Graph ['
50 graph_blocks = content.split("Graph [")
51
52 # Skip the first element if it's empty
53 if not graph_blocks[0].strip():
54 graph_blocks = graph_blocks[1:]
55
56 for block in graph_blocks:
57 # Remove trailing ']' and split by lines
58 block = block.strip()
59 if block.endswith("]"):
60 block = block[:-1]
61
62 lines = [line.strip() for line in block.split("\n") if line.strip()]
63
64 # Create a new graph as an adjacency list
65 graph = {}
66
67 for line in lines:
68 parts = line.split()
69
70 # Handle singleton node (just one node identifier on the line)
71 if len(parts) == 1:
72 node = parts[0]
73 if node not in graph:
74 graph[node] = []
75 # Handle regular edge definition
76 elif len(parts) >= 3:
77 node1, node2, edge_cost = parts[0], parts[1], float(parts[2])
78
79 # Add nodes if they don't exist
80 if node1 not in graph:
81 graph[node1] = []
82 if node2 not in graph:
83 graph[node2] = []
84
85 # Add edge with cost
86 graph[node1].append((node2, edge_cost))
87
88 if graph: # Only add non-empty graphs
89 self.graphs.append(graph)
90
91 except Exception as e:
92 print(f"Error loading graphs: {e}")

References file_path, and graphs.

Member Data Documentation

◆ file_path

Graph_Dataset.GraphDataset.file_path = file_path

Definition at line 39 of file Graph_Dataset.py.

Referenced by load_graphs().

◆ graphs

list Graph_Dataset.GraphDataset.graphs = []

Definition at line 40 of file Graph_Dataset.py.

Referenced by load_graphs().


The documentation for this class was generated from the following file: