Skip to content

Commit

Permalink
Added both traditional to compressed and compressed to traditional co…
Browse files Browse the repository at this point in the history
…nverter
  • Loading branch information
choubeyy committed Nov 15, 2024
1 parent a14b3d2 commit 3c863d2
Showing 1 changed file with 85 additions and 9 deletions.
94 changes: 85 additions & 9 deletions PAMI/extras/graph/convertFormat.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,113 @@
# 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):
self.iFile = iFile
self.oFile = 'oFile.txt'
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())
Expand Down

0 comments on commit 3c863d2

Please sign in to comment.