Last active
February 18, 2023 17:00
-
-
Save nuthanmunaiah/523a5e112f1e1f458e2c to your computer and use it in GitHub Desktop.
Python module to serialize and deserialize networkx DiGraph objects to and from a JSON file.
This file contains hidden or 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
import jsonpickle | |
import networkx | |
from networkx.readwrite import json_graph | |
def serialize(call_graph, file_path): | |
'''Function to serialize a NetworkX DiGraph to a JSON file.''' | |
if not isinstance(call_graph, networkx.DiGraph): | |
raise Exception('call_graph has be an instance of networkx.DiGraph') | |
with open(file_path, 'w+') as _file: | |
_file.write(jsonpickle.encode( | |
json_graph.adjacency_data(call_graph)) | |
) | |
def deserialize(file_path): | |
'''Function to deserialize a NetworkX DiGraph from a JSON file.''' | |
call_graph = None | |
with open(file_path, 'r+') as _file: | |
call_graph = json_graph.adjacency_graph( | |
jsonpickle.decode(_file.read()), | |
directed=True | |
) | |
return call_graph |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment