-
Notifications
You must be signed in to change notification settings - Fork 1k
Test: test descriptive graph metric calculation in neo4j and networkx adapters [COG-1188] #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
458eeac
Count the number of tokens in documents
alekszievr 51eadef
Merge branch 'COG-970-refactor-tokenizing' into feat/cog-1071-input-t…
alekszievr ba608a4
Merge branch 'COG-970-refactor-tokenizing' into feat/cog-1071-input-t…
alekszievr f6663ab
save token count to relational db
alekszievr 9182be8
Merge branch 'COG-970-refactor-tokenizing' into feat/cog-1132-add-num…
alekszievr 72dfec4
Add metrics to metric table
alekszievr 9bd5917
Merge branch 'dev' into feat/cog-1071-input-token-counting
dexters1 227d94e
Merge branch 'feat/cog-1071-input-token-counting' into feat/cog-1132-…
alekszievr 22b6459
Store list as json instead of array in relational db table
alekszievr 9764441
Merge branch 'dev' into feat/cog-1132-add-num-tokens-to-metric-table
alekszievr 100e7d7
Sum in sql instead of python
alekszievr c182d47
Unify naming
alekszievr 44fa2cd
Return data_points in descriptive metric calculation task
alekszievr 06030ff
Graph metrics getter template in graph db interface and adapters
alekszievr 67d9908
Calculate descriptive metrics in networkx adapter
alekszievr 252ac7f
neo4j metrics
alekszievr 48a51a3
Merge branch 'dev' into feat/cog-1082-metrics-in-graphdb-interface
alekszievr 9a94db8
remove _table from table name
alekszievr 57fb338
Merge branch 'dev' into feat/cog-1082-metrics-in-graphdb-interface
alekszievr e8dcef1
Merge branch 'dev' into feat/cog-1082-metrics-in-graphdb-interface
alekszievr b0f6ba7
Merge branch 'dev' into feat/cog-1082-metrics-in-graphdb-interface
alekszievr 05138fa
Use modules for adding to db instead of infrastructure
alekszievr f064f52
Merge branch 'feat/cog-1082-metrics-in-graphdb-interface' into feat/c…
alekszievr c9ee1bc
Merge branch 'feat/cog-1082-metrics-in-networkx-adapter' into feat/co…
alekszievr af8e798
Merge branch 'dev' into feat/cog-1082-metrics-in-networkx-adapter
alekszievr 406057f
Merge branch 'feat/cog-1082-metrics-in-networkx-adapter' into feat/co…
alekszievr d93b5f5
minor fixes
alekszievr c13fdec
minor cleanup
alekszievr f2ad1d4
Merge branch 'dev' into feat/cog-1082-metrics-in-neo4j-adapter
alekszievr 3e67828
Remove graph metric calculation from the default cognify pipeline
alekszievr 34ce4f8
descriptive metrics tests
alekszievr 1bc55f9
networkx metrics test
alekszievr c102f26
all descriptive metrics tests
alekszievr 92ae1d0
Merge branch 'dev' into test/metrics_in_adapters
alekszievr eddfef0
remove neo4j metrics test due to lack of gds plugin
alekszievr eb63421
Merge branch 'dev' into test/metrics_in_adapters
borisarzentar e842de6
Merge branch 'dev' into test/metrics_in_adapters
alekszievr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
descriptive metrics tests
- Loading branch information
commit 34ce4f847cd1479b35db3c7e807f1ea9df2aed9e
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
cognee/tests/tasks/descriptive_metrics/metric_consistency_test.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from cognee.tests.tasks.descriptive_metrics.networkx_metrics_test import get_networkx_metrics | ||
| from cognee.tests.tasks.descriptive_metrics.neo4j_metrics_test import get_neo4j_metrics | ||
| import asyncio | ||
|
|
||
|
|
||
| async def check_graph_metrics_consistency_across_adapters(): | ||
| neo4j_metrics = await get_neo4j_metrics(include_optional=False) | ||
| networkx_metrics = await get_networkx_metrics(include_optional=False) | ||
| assert networkx_metrics == neo4j_metrics | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(check_graph_metrics_consistency_across_adapters()) |
25 changes: 25 additions & 0 deletions
25
cognee/tests/tasks/descriptive_metrics/metrics_test_utils.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from cognee.tests.unit.interfaces.graph.get_graph_from_model_test import ( | ||
| Document, | ||
| DocumentChunk, | ||
| Entity, | ||
| EntityType, | ||
| ) | ||
| from cognee.tasks.storage.add_data_points import add_data_points | ||
|
|
||
|
|
||
| async def create_disconnected_test_graph(): | ||
| doc = Document(path="test/path") | ||
| doc_chunk = DocumentChunk(part_of=doc, text="This is a chunk of text", contains=[]) | ||
| entity_type = EntityType(name="Person") | ||
| entity = Entity(name="Alice", is_type=entity_type) | ||
| entity2 = Entity(name="Alice2", is_type=entity_type) | ||
| # the following self-loop is intentional and serves the purpose of testing the self-loop counting functionality | ||
| doc_chunk.contains.extend([entity, entity2, doc_chunk]) | ||
|
|
||
| doc2 = Document(path="test/path2") | ||
| doc_chunk2 = DocumentChunk(part_of=doc2, text="This is a chunk of text", contains=[]) | ||
| entity_type2 = EntityType(name="Person") | ||
| entity3 = Entity(name="Bob", is_type=entity_type2) | ||
| doc_chunk2.contains.extend([entity3]) | ||
|
|
||
| await add_data_points([doc_chunk, doc_chunk2]) |
42 changes: 42 additions & 0 deletions
42
cognee/tests/tasks/descriptive_metrics/neo4j_metrics_test.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from cognee.tests.tasks.descriptive_metrics.metrics_test_utils import create_disconnected_test_graph | ||
| from cognee.infrastructure.databases.graph.get_graph_engine import create_graph_engine | ||
| from cognee.infrastructure.databases.graph import get_graph_engine | ||
| import cognee | ||
| import asyncio | ||
| import pytest | ||
|
|
||
|
|
||
| async def get_neo4j_metrics(include_optional=True): | ||
| create_graph_engine.cache_clear() | ||
| cognee.config.set_graph_database_provider("neo4j") | ||
| graph_engine = await get_graph_engine() | ||
| await graph_engine.delete_graph() | ||
| await create_disconnected_test_graph() | ||
| neo4j_graph_metrics = await graph_engine.get_graph_metrics(include_optional=include_optional) | ||
| return neo4j_graph_metrics | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_neo4j_metrics(): | ||
| neo4j_metrics = await get_neo4j_metrics(include_optional=True) | ||
| assert neo4j_metrics["num_nodes"] == 9, f"Expected 9 nodes, got {neo4j_metrics['num_nodes']}" | ||
| assert neo4j_metrics["num_edges"] == 9, f"Expected 9 edges, got {neo4j_metrics['num_edges']}" | ||
| assert neo4j_metrics["mean_degree"] == 2, ( | ||
| f"Expected mean degree is 2, got {neo4j_metrics['mean_degree']}" | ||
| ) | ||
| assert neo4j_metrics["edge_density"] == 0.125, ( | ||
| f"Expected edge density is 0.125, got {neo4j_metrics['edge_density']}" | ||
| ) | ||
| assert neo4j_metrics["num_connected_components"] == 2, ( | ||
| f"Expected 2 connected components, got {neo4j_metrics['num_connected_components']}" | ||
| ) | ||
| assert neo4j_metrics["sizes_of_connected_components"] == [5, 4], ( | ||
| f"Expected connected components of size [5, 4], got {neo4j_metrics['sizes_of_connected_components']}" | ||
| ) | ||
| assert neo4j_metrics["num_selfloops"] == 1, ( | ||
| f"Expected 1 self-loop, got {neo4j_metrics['num_selfloops']}" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(test_neo4j_metrics()) |
53 changes: 53 additions & 0 deletions
53
cognee/tests/tasks/descriptive_metrics/networkx_metrics_test.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| from cognee.tests.tasks.descriptive_metrics.metrics_test_utils import create_disconnected_test_graph | ||
| from cognee.infrastructure.databases.graph.get_graph_engine import create_graph_engine | ||
| from cognee.infrastructure.databases.graph import get_graph_engine | ||
| import cognee | ||
| import asyncio | ||
|
|
||
|
|
||
| async def get_networkx_metrics(include_optional=True): | ||
| create_graph_engine.cache_clear() | ||
| cognee.config.set_graph_database_provider("networkx") | ||
| graph_engine = await get_graph_engine() | ||
| await graph_engine.delete_graph() | ||
| await create_disconnected_test_graph() | ||
| networkx_graph_metrics = await graph_engine.get_graph_metrics(include_optional=include_optional) | ||
| return networkx_graph_metrics | ||
|
|
||
|
|
||
| async def assert_networkx_metrics(): | ||
| networkx_metrics = await get_networkx_metrics(include_optional=True) | ||
| assert networkx_metrics["num_nodes"] == 9, ( | ||
| f"Expected 9 nodes, got {networkx_metrics['num_nodes']}" | ||
| ) | ||
| assert networkx_metrics["num_edges"] == 9, ( | ||
| f"Expected 9 edges, got {networkx_metrics['num_edges']}" | ||
| ) | ||
| assert networkx_metrics["mean_degree"] == 2, ( | ||
| f"Expected mean degree is 2, got {networkx_metrics['mean_degree']}" | ||
| ) | ||
| assert networkx_metrics["edge_density"] == 0.125, ( | ||
| f"Expected edge density is 0.125, got {networkx_metrics['edge_density']}" | ||
| ) | ||
| assert networkx_metrics["num_connected_components"] == 2, ( | ||
| f"Expected 2 connected components, got {networkx_metrics['num_connected_components']}" | ||
| ) | ||
| assert networkx_metrics["sizes_of_connected_components"] == [5, 4], ( | ||
| f"Expected connected components of size [5, 4], got {networkx_metrics['sizes_of_connected_components']}" | ||
| ) | ||
| assert networkx_metrics["num_selfloops"] == 1, ( | ||
| f"Expected 1 self-loop, got {networkx_metrics['num_selfloops']}" | ||
| ) | ||
| assert networkx_metrics["diameter"] is None, ( | ||
| f"Diameter should be None for disconnected graphs, got {networkx_metrics['diameter']}" | ||
| ) | ||
| assert networkx_metrics["avg_shortest_path_length"] is None, ( | ||
| f"Average shortest path should be None for disconnected graphs, got {networkx_metrics['avg_shortest_path_length']}" | ||
| ) | ||
| assert networkx_metrics["avg_clustering"] == 0, ( | ||
| f"Expected 0 average clustering, got {networkx_metrics['avg_clustering']}" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(assert_networkx_metrics()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.