-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #554 from choubeyy/main
Made changes to graph format converter, gspan and tkg
- Loading branch information
Showing
3 changed files
with
185 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,121 @@ | ||
# Usage | ||
# obj = ConvertFormat('iFile', 'oFile') | ||
# obj1 = ConvertFormat('iFile', 'oFile') | ||
# | ||
# obj1.convertFromTraditionalToCompressed() | ||
# | ||
# obj1.getMemoryRSS() | ||
# | ||
# obj1.getMemoryUSS() | ||
# | ||
# obj2 = ConvertFormat('iFileCompressed', 'oFileTrad') | ||
# | ||
# obj2.convertFromCompressedToTraditional() | ||
# | ||
# obj2.getMemoryRSS() | ||
# | ||
# obj2.getMemoryUSS() | ||
|
||
import os | ||
import psutil | ||
|
||
|
||
class ConvertFormat: | ||
def __init__(self, iFile, oFile): | ||
def __init__(self, iFile): | ||
self.iFile = iFile | ||
self.oFile = oFile | ||
self.convert() | ||
self.convertedData = [] | ||
|
||
def _writeGraphToFile(self, graph, oFile): | ||
node_str = ' '.join(f"{node} {label}" for node, label in sorted(graph['nodes'])) | ||
def _writeGraphToFileCompressed(self, graph): | ||
node_str = ' '.join(f"{node} {label}" for node, label in sorted(graph['nodes'], key=lambda x: x[0])) | ||
edge_str = ' '.join(f"{u} {v} {label}" for u, v, label in graph['edges']) | ||
oFile.write(f"{node_str} : {edge_str}\n") | ||
return f"{node_str} : {edge_str}\n" | ||
|
||
def convert(self): | ||
def _writeGraphToFileTraditional(self, graph, gId): | ||
traditional_lines = [f"t # {gId}\n"] | ||
for node, label in sorted(graph['nodes'], key=lambda x: x[0]): | ||
traditional_lines.append(f"v {node} {label}\n") | ||
for u, v, label in graph['edges']: | ||
traditional_lines.append(f"e {u} {v} {label}\n") | ||
return ''.join(traditional_lines) | ||
|
||
def convertFromTraditionalToCompressed(self): | ||
graph = {} | ||
with open(self.iFile, 'r') as iFile, open(self.oFile, 'w') as oFile: | ||
self.convertedData = [] | ||
with open(self.iFile, 'r') as iFile: | ||
for line in iFile: | ||
parts = line.strip().split() | ||
if not parts: | ||
continue | ||
if parts[0] == 't': | ||
if graph: | ||
self._writeGraphToFile(graph, oFile) | ||
compressedGraph = self._writeGraphToFileCompressed(graph) | ||
self.convertedData.append(compressedGraph) | ||
graph = {'nodes': [], 'edges': []} | ||
elif parts[0] == 'v': | ||
graph['nodes'].append((int(parts[1]), parts[2])) | ||
elif parts[0] == 'e': | ||
graph['edges'].append((int(parts[1]), int(parts[2]), parts[3])) | ||
if graph: | ||
self._writeGraphToFile(graph, oFile) | ||
compressedGraph = self._writeGraphToFileCompressed(graph) | ||
self.convertedData.append(compressedGraph) | ||
|
||
def convertFromCompressedToTraditional(self): | ||
self.convertedData = [] | ||
gId = 0 | ||
with open(self.iFile, 'r') as iFile: | ||
for line in iFile: | ||
if not line.strip(): | ||
continue # Skip empty lines | ||
if ':' not in line: | ||
print(f"Invalid format in line: {line.strip()}") | ||
continue | ||
nodes_part, edges_part = line.strip().split(':') | ||
nodes_tokens = nodes_part.strip().split() | ||
edges_tokens = edges_part.strip().split() | ||
|
||
# Parse nodes | ||
nodes = [] | ||
for i in range(0, len(nodes_tokens), 2): | ||
node_id = int(nodes_tokens[i]) | ||
node_label = nodes_tokens[i + 1] | ||
nodes.append((node_id, node_label)) | ||
|
||
# Parse edges | ||
edges = [] | ||
for i in range(0, len(edges_tokens), 3): | ||
if i + 2 >= len(edges_tokens): | ||
print(f"Incomplete edge information in line: {line.strip()}") | ||
break | ||
u = int(edges_tokens[i]) | ||
v = int(edges_tokens[i + 1]) | ||
label = edges_tokens[i + 2] | ||
edges.append((u, v, label)) | ||
|
||
graph = {'nodes': nodes, 'edges': edges} | ||
traditionalGraph = self._writeGraphToFileTraditional(graph, gId) | ||
self.convertedData.append(traditionalGraph) | ||
gId += 1 | ||
|
||
def save(self, oFile): | ||
""" | ||
Saves the converted data to the specified output file. | ||
:param oFile: Path to the output file. | ||
""" | ||
if not self.convertedData: | ||
print("No converted data to save. Please perform a conversion first.") | ||
return | ||
|
||
with open(oFile, 'w') as file: | ||
for graphData in self.convertedData: | ||
file.write(graphData) | ||
|
||
def getMemoryRSS(self): | ||
process = psutil.Process(os.getpid()) | ||
rss = process.memory_info().rss | ||
return rss | ||
|
||
def getMemoryUSS(self): | ||
process = psutil.Process(os.getpid()) | ||
uss = process.memory_full_info().uss | ||
return uss | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters