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:
alekszievr 2025-02-08 00:15:26 +01:00 committed by GitHub
parent 460691b76a
commit 2e842652be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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):