Fix diameter and shortest path calculation in networkx adapter [COG-1201] (#507)
…nnected graph <!-- .github/pull_request_template.md --> ## Description <!-- Provide a clear description of the changes in this PR --> ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Bug Fixes - Enhanced reliability of graph metric calculations to gracefully handle unexpected inputs, ensuring smoother and uninterrupted graph analysis for end-users. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
parent
460691b76a
commit
2e842652be
1 changed files with 8 additions and 6 deletions
|
|
@ -404,15 +404,17 @@ class NetworkXAdapter(GraphDBInterface):
|
|||
return edge_density
|
||||
|
||||
def _get_diameter(graph):
|
||||
if nx.is_strongly_connected(graph):
|
||||
return nx.diameter(graph.to_undirected())
|
||||
else:
|
||||
try:
|
||||
return nx.diameter(nx.DiGraph(graph.to_undirected()))
|
||||
except Exception as e:
|
||||
logger.warning("Failed to calculate diameter: %s", e)
|
||||
return None
|
||||
|
||||
def _get_avg_shortest_path_length(graph):
|
||||
if nx.is_strongly_connected(graph):
|
||||
return nx.average_shortest_path_length(graph)
|
||||
else:
|
||||
try:
|
||||
return nx.average_shortest_path_length(nx.DiGraph(graph.to_undirected()))
|
||||
except Exception as e:
|
||||
logger.warning("Failed to calculate average shortest path length: %s", e)
|
||||
return None
|
||||
|
||||
def _get_avg_clustering(graph):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue