Skip to main content

A powerful graph database with Cypher query support and advanced visualization capabilities

Project description

ContextGraph

PyPI version Python Support License: MIT Tests

A powerful graph database with Cypher query support and advanced visualization capabilities, built on igraph.

๐Ÿš€ Features

  • ๐Ÿ” Cypher Query Language: Full support for Cypher queries including MATCH, CREATE, WHERE, RETURN, and more
  • ๐Ÿ“Š Advanced Visualization: Multiple backends (matplotlib, plotly, graphviz) with interactive capabilities
  • ๐Ÿ”— Variable-Length Paths: Support for path queries with depth limits (*1..3, *2, *)
  • ๐Ÿ”ค String Operations: Comprehensive string search and manipulation functions
  • ๐Ÿ’พ Data Import/Export: High-performance CSV import and multiple serialization formats
  • โšก Transactions: ACID-compliant transaction support with rollback capabilities
  • ๐ŸŽฏ High Performance: Built on igraph for efficient graph operations
  • ๐Ÿ Pythonic API: Clean, intuitive Python interface

๐Ÿ“ฆ Installation

Basic Installation

pip install contextgraph

With Visualization Support

pip install contextgraph[visualization]

With All Optional Dependencies

pip install contextgraph[all]

Development Installation

git clone https://github.com/yourusername/contextgraph.git
cd contextgraph
pip install -e .[dev]

๐Ÿƒโ€โ™‚๏ธ Quick Start

from contextgraph import GraphDB

# Create a new graph database
db = GraphDB()

# Create nodes
alice_id = db.create_node(['Person'], {'name': 'Alice', 'age': 30})
bob_id = db.create_node(['Person'], {'name': 'Bob', 'age': 25})
company_id = db.create_node(['Company'], {'name': 'TechCorp'})

# Create relationships
db.create_relationship(alice_id, bob_id, 'KNOWS', {'since': 2020})
db.create_relationship(alice_id, company_id, 'WORKS_FOR')

# Query with Cypher
result = db.execute("""
    MATCH (p:Person)-[:KNOWS]->(friend:Person)
    WHERE p.age > 25
    RETURN p.name, friend.name, p.age
""")

for record in result:
    print(f"{record['p.name']} knows {record['friend.name']}")

# Visualize the graph
db.visualize(node_labels=True, node_color_property='age')

๐Ÿ“š Documentation

Core Operations

Creating Nodes and Relationships

# Create nodes with labels and properties
person_id = db.create_node(['Person', 'Employee'], {
    'name': 'Alice',
    'age': 30,
    'department': 'Engineering'
})

# Create relationships with properties
rel_id = db.create_relationship(
    source_id=alice_id,
    target_id=bob_id,
    relationship_type='MANAGES',
    properties={'since': '2023-01-01', 'level': 'senior'}
)

Cypher Queries

# Basic pattern matching
result = db.execute("""
    MATCH (manager:Person)-[:MANAGES]->(employee:Person)
    WHERE manager.department = 'Engineering'
    RETURN manager.name, employee.name
""")

# Variable-length paths
result = db.execute("""
    MATCH (start:Person)-[:KNOWS*1..3]->(end:Person)
    WHERE start.name = 'Alice'
    RETURN start.name, end.name
""")

# String operations
result = db.execute("""
    MATCH (p:Person)
    WHERE p.name CONTAINS 'Ali' AND p.email =~ '.*@company\\.com'
    RETURN UPPER(p.name) as name, LENGTH(p.name) as name_length
""")

Advanced Features

Transactions

# Using transactions for data consistency
with db.transaction():
    node1 = db.create_node(['Person'], {'name': 'Charlie'})
    node2 = db.create_node(['Person'], {'name': 'Diana'})
    db.create_relationship(node1, node2, 'FRIENDS')
    # Automatically committed on success, rolled back on exception

CSV Import

# High-performance CSV import
stats = db.import_nodes_from_csv(
    'people.csv',
    labels=['Person'],
    property_columns=['name', 'age', 'email']
)

stats = db.import_relationships_from_csv(
    'relationships.csv',
    relationship_type='KNOWS',
    source_column='person1_id',
    target_column='person2_id'
)

Visualization

# Basic visualization
db.visualize(
    node_labels=True,
    layout='spring',
    title='My Graph'
)

# Advanced styling
db.visualize(
    backend='plotly',  # Interactive visualization
    node_size_property='age',
    node_color_property='department',
    edge_width_property='strength',
    layout='circular'
)

# Query result visualization
from contextgraph import GraphVisualizer
viz = GraphVisualizer(db)
viz.plot_query_result("""
    MATCH (p:Person)-[:WORKS_FOR]->(c:Company)
    RETURN p, c
""")

๐Ÿ”ง Supported Cypher Features

Query Clauses

  • โœ… MATCH - Pattern matching
  • โœ… CREATE - Node and relationship creation
  • โœ… WHERE - Filtering conditions
  • โœ… RETURN - Result projection
  • โœ… ORDER BY - Result sorting
  • โœ… LIMIT - Result limiting
  • โœ… WITH - Query chaining

Pattern Matching

  • โœ… Node patterns: (n:Label {property: value})
  • โœ… Relationship patterns: -[:TYPE {property: value}]->
  • โœ… Variable-length paths: -[:TYPE*1..3]->
  • โœ… Optional patterns and complex matching

Functions and Operators

  • โœ… String functions: UPPER(), LOWER(), TRIM(), SUBSTRING(), etc.
  • โœ… String operators: CONTAINS, STARTS WITH, ENDS WITH, =~ (regex)
  • โœ… Aggregate functions: COUNT(), SUM(), AVG(), MIN(), MAX()
  • โœ… Comparison operators: =, <>, <, >, <=, >=

๐Ÿ“Š Visualization Backends

Backend Type Features
matplotlib Static Publication-quality plots, customizable styling
plotly Interactive Web-ready, hover details, zoom/pan
graphviz Vector High-quality layouts, perfect for documentation

๐Ÿ”„ Data Import/Export

Supported Formats

  • CSV: High-performance bulk import
  • JSON: Human-readable serialization
  • Pickle: Fast binary serialization with full Python object support

Performance

  • CSV Import: 10,000+ nodes/relationships per second
  • Query Performance: Optimized for complex graph traversals
  • Memory Efficient: Streaming operations for large datasets

๐Ÿงช Testing

Run the test suite:

# Run all tests
pytest

# Run with coverage
pytest --cov=igraph_cypher_db

# Run specific test categories
pytest -m "not slow"  # Skip slow tests
pytest tests/test_cypher.py  # Specific test file

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

git clone https://github.com/yourusername/contextgraph.git
cd contextgraph
pip install -e .[dev]
pre-commit install

Code Quality

# Format code
black igraph_cypher_db tests

# Lint code
flake8 igraph_cypher_db tests

# Type checking
mypy igraph_cypher_db

๐Ÿ“ˆ Performance Benchmarks

Operation Performance
Node Creation 50,000+ nodes/sec
Relationship Creation 25,000+ relationships/sec
CSV Import 10,000+ records/sec
Simple Queries 1,000+ queries/sec
Complex Path Queries 100+ queries/sec

๐Ÿ—บ๏ธ Roadmap

  • Query Optimization: Advanced query planning and optimization
  • Distributed Queries: Support for distributed graph operations
  • Graph Algorithms: Built-in graph analysis algorithms
  • Schema Validation: Optional schema enforcement
  • REST API: HTTP interface for remote access
  • Streaming: Real-time graph updates and queries

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ž Support


Made with โค๏ธ for the graph database community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextgraph-0.3.0.tar.gz (91.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contextgraph-0.3.0-py3-none-any.whl (37.5 kB view details)

Uploaded Python 3

File details

Details for the file contextgraph-0.3.0.tar.gz.

File metadata

  • Download URL: contextgraph-0.3.0.tar.gz
  • Upload date:
  • Size: 91.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for contextgraph-0.3.0.tar.gz
Algorithm Hash digest
SHA256 afa35c52b932b3f8b6440fed240942f7b3125a8384c2cb3fe7c12f373d5f2c16
MD5 c1e1d199efc78ff42b8f17688f528f06
BLAKE2b-256 1b49770f2d03714811a2fd19d06653dd6418fea910c58ab4d12818ed16917f8b

See more details on using hashes here.

File details

Details for the file contextgraph-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: contextgraph-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 37.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for contextgraph-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a376a9594fb13329b19e8f135bcaf222cbeb631a964e6b71443787fe7d9c483
MD5 763aa7eff516c4b45a40b6249667f2c9
BLAKE2b-256 879180ea6587906ef02d73759b57aaa1bb6be92b8ee25749de65790b358f92cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page