Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(memgraph): compute get_graph_metrics from graph data#123

Open
RajdeepKushwaha5 wants to merge 2 commits into
topoteretes:maintopoteretes/cognee-community:mainfrom
RajdeepKushwaha5:fix/memgraph-graph-metricsRajdeepKushwaha5/cognee-community:fix/memgraph-graph-metricsCopy head branch name to clipboard
Open

fix(memgraph): compute get_graph_metrics from graph data#123
RajdeepKushwaha5 wants to merge 2 commits into
topoteretes:maintopoteretes/cognee-community:mainfrom
RajdeepKushwaha5:fix/memgraph-graph-metricsRajdeepKushwaha5/cognee-community:fix/memgraph-graph-metricsCopy head branch name to clipboard

Conversation

@RajdeepKushwaha5

@RajdeepKushwaha5 RajdeepKushwaha5 commented Jun 23, 2026

Copy link
Copy Markdown

Description

MemgraphAdapter.get_graph_metrics() returned all-zero / fallback metrics for any non-empty graph. This PR computes the metrics correctly from the live graph and adds both unit tests and end-to-end CI coverage.

Closes #122.

Root causes (all three fixed):

  1. Positional indexing of dict-shaped resultsnode_count[0][0] raised KeyError (query() returns dicts keyed by the RETURN name), which the broad except swallowed,returning the all-zeros fallback.
  2. Label/relationship filters the adapter never writes — the metric queries filtered on MATCH (n:Node) / [:EDGE] (copied from the Kuzu adapter), but this adapter writes unlabeled nodes and real relationship-typed edges, so every labelled query matched nothing.
  3. Broken connected-components aggregation — after WITH n.id AS node_id, n went out of scope and was rebound to the whole graph, collapsing every node into a single component.

What changed:

  • memgraph_adapter.py — compute metrics in-process from get_graph_data():
    • num_nodes, num_edges, mean_degree, edge_density
    • connected components via a self-contained union-find helper (weakly_connected_component_sizes)
    • num_selfloops from edge endpoints
    • diameter, avg_shortest_path_length, avg_clustering reported as unsupported (-1), matching the Neptune adapter (cognee#3171), since they need prohibitively expensive all-pairs traversals.
  • tests/test_graph_metrics.py — unit tests for the component helper and get_graph_metrics() with a stubbed get_graph_data() (no live Memgraph required).
  • examples/example.py — call get_graph_metrics(include_optional=True) so the Memgraph CI workflow (which runs examples/example.py) exercises the corrected path end-to-end against a live instance.

Testing:

  • pytest tests/test_graph_metrics.py9 passed (against cognee 1.2.x).
  • ruff check → passed on all changed files.
  • examples/example.py now prints real, non-zero metrics on a live Memgraph run.

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.

MemgraphAdapter.get_graph_metrics() filtered on :Node/:EDGE labels that
this adapter never writes (the queries were copied from the Kuzu adapter,
which uses a fixed :Node/:EDGE schema) and indexed dict-shaped query
results positionally (node_count[0][0]). Both made the method raise and
fall through to the all-zeros fallback, so every metric was wrong for any
non-empty graph. The connected-components query additionally collapsed all
nodes into a single component via a broken COLLECT aggregation.

Compute the metrics in-process from get_graph_data() instead:
- num_nodes, num_edges, mean_degree, edge_density
- connected components via a self-contained union-find helper
- num_selfloops from edge endpoints

diameter, avg_shortest_path_length and avg_clustering require all-pairs
traversals that are prohibitively expensive, so they are reported as
unsupported (-1), matching the Neptune adapter.

Add unit tests covering the component helper and get_graph_metrics with a
stubbed get_graph_data() (no live Memgraph required).

Signed-off-by: Rajdeep Singh <rajdeep.kuswaha@s.amity.edu>
… the fix

The memgraph CI workflow runs examples/example.py (not pytest), and the example never called get_graph_metrics(), so the metrics fix had no end-to-end coverage in CI. Print get_graph_metrics(include_optional=True) after fetching the graph data so a live Memgraph run exercises the corrected code path.

Signed-off-by: Rajdeep Singh <rajdeep.kuswaha@s.amity.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.