GraphRecords
GraphRecords stores entities and their relationships as a graph. Nodes hold attributes. Edges connect nodes and can also hold attributes. Groups organize subsets of nodes and edges.
When to Use GraphRecords
GraphRecords fits problems where:
- Data has natural relationships (users and products, documents and citations, components and dependencies)
- You need to query based on relationships ("find all users connected to products over $100")
- Different entity types have different attributes (users have age, products have price)
Installation
Building a Graph
# Add nodes as tuples: (id, {attributes})
=
# Add edges as tuples: (source, target, {attributes})
You can also use Pandas or Polars DataFrames:
=
=
Accessing Data
# Get all nodes
# ['alice', 'bob', 'carol', 'widget', 'gadget']
# Get attributes of a node
# {'age': 30}
# Get nodes in a group
# ['alice', 'bob', 'carol']
# Get edges connected to a node
# [0, 2]
# Get edge attributes
# {'quantity': 1}
Query Engine
The query engine finds nodes and edges based on their attributes and relationships.
Queries are functions that receive an operand, apply conditions, and return results:
return
# ['alice', 'carol']
Queries can follow relationships:
# Follow edges to products, check price
return
# ['alice', 'bob']
Queries can aggregate:
return
# 30.0
See the Query Engine Guide for the full API.
Schema
Schemas define what attributes are allowed and their types.
Inferred mode (default): The schema learns from data as you add it. Any attribute is allowed.
Provided mode: The schema is fixed. Data that doesn't match is rejected.
=
=
# Switch to provided mode
# Now adding a user without 'age' or 'name' raises an error
See the Schema Guide for details.
Serialization
Save and load graphs using RON format:
=
Export to DataFrames:
= # or record.to_polars()
Documentation
Background
GraphRecords started as MedRecord in the medmodels library. We realized it has applications beyond the medical domain and published it as a standalone library.
License
MIT. See LICENSE.