Session
momapy_kb.lpg.session
LPG session for momapy_kb.
Provides a Session class that wraps a fieldz_kb LPG session and adds momapy-specific functionality: file loading, collection management, and layout element queries.
Classes:
| Name | Description |
|---|---|
Session |
Session for saving and retrieving momapy objects via a graph database. |
Session
Session for saving and retrieving momapy objects via a graph database.
Wraps a fieldz_kb LPG session and registers momapy-specific type plugins. Use as a context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
A pylpg backend instance (Neo4jBackend, FalkorDBBackend, etc.) |
required |
Example
import momapy_kb.lpg.session import momapy_kb.lpg.backends.neo4j backend = momapy_kb.lpg.backends.neo4j.Neo4jBackend(hostname="localhost") with momapy_kb.lpg.session.Session(backend) as session: ... session.save_from_file("model.xml", integration_mode="hash")
Methods:
| Name | Description |
|---|---|
cypher_query_as_layout_elements |
Execute a Cypher query and return results as layout elements. |
delete_all |
Delete all nodes and relationships from the database. |
execute_query |
Execute a Cypher query against the database. |
execute_query_as_objects |
Execute a Cypher query and convert results to Python objects. |
get_layout_element_nodes_from_model_element_node |
Get layout element nodes linked to a model element node. |
make_layout_elements_from_model_element_node |
Get layout elements as Python objects for a model element node. |
save_collections_from_entries |
Save collections from pre-built CollectionEntry objects. |
save_collections_from_file_paths |
Load maps from files and save them as named collections. |
save_from_file |
Load a map from a file and save it to the database. |
save_from_object |
Save a single object to the database. |
save_from_objects |
Save multiple objects to the database. |
Source code in src/momapy_kb/lpg/session.py
cypher_query_as_layout_elements
cypher_query_as_layout_elements(query: str, params: dict | None = None) -> list[list[LayoutElement]]
Execute a Cypher query and return results as layout elements.
For each row in the query results, model element nodes are resolved to their corresponding layout elements via the LayoutModelMapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The Cypher query string. |
required |
params
|
dict | None
|
Optional query parameters. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[LayoutElement]]
|
A list of rows, where each row is a list of LayoutElement objects. |
Source code in src/momapy_kb/lpg/session.py
delete_all
execute_query
Execute a Cypher query against the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The Cypher query string. |
required |
params
|
dict | None
|
Optional query parameters. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
Query results as a list of dicts. |
Source code in src/momapy_kb/lpg/session.py
execute_query_as_objects
execute_query_as_objects(query: str, params: dict | None = None, node_id_to_object: dict | None = None) -> list[list[object]]
Execute a Cypher query and convert results to Python objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The Cypher query string. |
required |
params
|
dict | None
|
Optional query parameters. |
None
|
node_id_to_object
|
dict | None
|
Optional cache mapping node database IDs to objects. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[object]]
|
A list of rows, where each row is a list of Python objects. |
Source code in src/momapy_kb/lpg/session.py
get_layout_element_nodes_from_model_element_node
Get layout element nodes linked to a model element node.
Queries the LayoutModelMapping in the database to find layout elements corresponding to the given model element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_element_node
|
BaseNode
|
A saved model element node. |
required |
Returns:
| Type | Description |
|---|---|
list[BaseNode]
|
A list of layout element nodes. |
Source code in src/momapy_kb/lpg/session.py
make_layout_elements_from_model_element_node
Get layout elements as Python objects for a model element node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_element_node
|
BaseNode
|
A saved model element node. |
required |
Returns:
| Type | Description |
|---|---|
list[LayoutElement]
|
A list of momapy LayoutElement objects. |
Source code in src/momapy_kb/lpg/session.py
save_collections_from_entries
save_collections_from_entries(collection_names_and_entries: list[tuple[str, list[CollectionEntry]]], delete_all: bool = False) -> None
Save collections from pre-built CollectionEntry objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_names_and_entries
|
list[tuple[str, list[CollectionEntry]]]
|
List of (name, entries) tuples. |
required |
delete_all
|
bool
|
If True, clear the database before saving. |
False
|
Source code in src/momapy_kb/lpg/session.py
save_collections_from_file_paths
save_collections_from_file_paths(collection_names_and_file_paths: list[tuple[str, list]], return_type: Literal['map', 'model', 'layout'] = 'map', delete_all: bool = False) -> None
Load maps from files and save them as named collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_names_and_file_paths
|
list[tuple[str, list]]
|
List of (name, file_paths) tuples. |
required |
return_type
|
Literal['map', 'model', 'layout']
|
What to extract from files ("map", "model", or "layout"). |
'map'
|
delete_all
|
bool
|
If True, clear the database before saving. |
False
|
Source code in src/momapy_kb/lpg/session.py
save_from_file
save_from_file(file_path: str, return_type: Literal['map', 'model', 'layout'] = 'map', with_layout: bool = True, with_model: bool = True, integration_mode: Literal['hash', 'id'] | None = None) -> None
Load a map from a file and save it to the database.
Supports CellDesigner, SBGN, and SBML file formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the input file. |
required |
return_type
|
Literal['map', 'model', 'layout']
|
What to extract from the file ("map", "model", or "layout"). |
'map'
|
with_layout
|
bool
|
Whether to include layout data. |
True
|
with_model
|
bool
|
Whether to include model data. |
True
|
integration_mode
|
Literal['hash', 'id'] | None
|
How to handle duplicate objects ("hash" or "id"). |
None
|
Source code in src/momapy_kb/lpg/session.py
save_from_object
save_from_object(object_: object, integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None) -> None
Save a single object to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_
|
object
|
The object to save. |
required |
integration_mode
|
Literal['hash', 'id']
|
How to handle duplicate objects ("hash" or "id"). |
'id'
|
exclude_from_integration
|
tuple[type, ...] | None
|
Types to exclude from integration logic. |
None
|
Source code in src/momapy_kb/lpg/session.py
save_from_objects
save_from_objects(objects: list[object], integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None) -> None
Save multiple objects to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[object]
|
The objects to save. |
required |
integration_mode
|
Literal['hash', 'id']
|
How to handle duplicate objects ("hash" or "id"). |
'id'
|
exclude_from_integration
|
tuple[type, ...] | None
|
Types to exclude from integration logic. |
None
|