bento_meta.mdb.mdb_tools.mdb_tools

ToolsMDB: subclass of ‘WriteableMDB’ to support interactions with the MDB.

EntityValidator: validates that entities have required attributes.

Attributes

log_ini_path

log_file_path

logger

Classes

ToolsMDB

bento_meta.mdb.writeable.WriteableMDB subclass with mdb-tools.

EntityValidator

Class to validate that bento-meta entities have all required attributes.

Functions

_get_nlp_model()

Install and import spacy & scispacy nlp model if any not already installed.

Module Contents

bento_meta.mdb.mdb_tools.mdb_tools.log_ini_path
bento_meta.mdb.mdb_tools.mdb_tools.log_file_path
bento_meta.mdb.mdb_tools.mdb_tools.logger
class bento_meta.mdb.mdb_tools.mdb_tools.ToolsMDB(uri: str | None, user: str | None, password: str | None)[source]

Bases: bento_meta.mdb.writeable.WriteableMDB

bento_meta.mdb.writeable.WriteableMDB subclass with mdb-tools.

exception EntityNotUniqueError[source]

Bases: Exception

Entity’s attributes identify more than 1 property graph node in an MDB.

exception EntityNotFoundError[source]

Bases: Exception

Entity’s attributes fail to identify a property graph node in an MDB.

exception PatternNotUniqueError[source]

Bases: Exception

Match pattern’s attributes identify more than 1 triple in an MDB.

exception PatternNotFoundError[source]

Bases: Exception

Match pattern’s attributes fail to identify a triple in an MDB.

_get_entity_count(entity: bento_meta.entity.Entity) list[neo4j.Record][source]

Return count of given entity found in an MDB.

  • If count = 0, entity with given attributes not found in MDB.

  • If count = 1, entity with given attributes is unique in MDB.

  • If count > 1, more attributes needed to uniquely id entity in MDB.

Parameters:

entity – The entity to count.

Returns:

List with count value.

_get_pattern_count(pattern: minicypher.entities.T | minicypher.entities.G) list[neo4j.Record][source]

Return count of given match pattern (triple or path) found in an MDB.

Property graph triple: (n)-[r]->(m) Path: set of overlapping triples

  • If count = 0, pattern with given attributes not found in MDB.

  • If count = 1, pattern with given attributes is unique in MDB.

  • If count > 1, more attributes needed to uniquely id pattern in MDB.

Parameters:

pattern – The pattern (triple or path) to count.

Returns:

List with count value.

validate_entity_unique(entity: bento_meta.entity.Entity) None[source]

Validate that the given entity occurs once (& only once) in an MDB.

Note: doesn’t validate the entity itself because not all entity attributes are necessarily required to locate an entity in the MDB. (e.g. handle and model OR nanoid alone can identify a node)

Parameters:

entity – The entity to validate.

Raises:
validate_entities_unique(entities: collections.abc.Iterable[bento_meta.entity.Entity]) None[source]

Run self.validate_entity_unique() over multiple entities.

validate_pattern_unique(pattern: minicypher.entities.T | minicypher.entities.G) None[source]

Validate that the given match pattern occurs once (& only once) in an MDB.

Parameters:

pattern – The pattern to validate.

Raises:
remove_entity_from_mdb(entity: bento_meta.entity.Entity) list[neo4j.Record][source]

Remove given Entity node from the database.

Accepts the following bento-meta Entities:

Concept, Node, Predicate, Property, Edge, Term

Parameters:

entity – The entity to remove.

Returns:

List of Records from the transaction.

add_entity_to_mdb(entity: bento_meta.entity.Entity, _commit: str | None = None) list[neo4j.Record][source]

Add given Entity node to MDB instance.

Parameters:
  • entity – The entity to add.

  • _commit – Optional commit string to tag the entity.

Returns:

List of Records from the transaction.

get_concept_nanoids_linked_to_entity(entity: bento_meta.entity.Entity, mapping_source: str | None = None) list[neo4j.Record][source]

Return list of concept nanoids linked to given entity.

The concept(s) have “represents” or “has_concept” relationships tagged with the given mapping source. If no mapping source is provided, return all.

Parameters:
  • entity – The entity to get concepts for.

  • mapping_source – Optional mapping source to filter by.

Returns:

List of concept nanoids.

add_relationship_to_mdb(relationship_type: str, src_entity: bento_meta.entity.Entity, dst_entity: bento_meta.entity.Entity, _commit: str = '') list[neo4j.Record][source]

Add relationship between given entities in MDB.

Parameters:
  • relationship_type – The type of relationship to create.

  • src_entity – Source entity for the relationship.

  • dst_entity – Destination entity for the relationship.

  • _commit – Optional commit string to tag the relationship.

Returns:

List of Records from the transaction.

Link two synonymous entities in the MDB via a Concept node.

Takes two synonymous Entities (as determined by user/SME) as bento-meta objects and connects them to a Concept node via a ‘represents’ relationship.

Entities must both exist in the MDB instance and given entity attributes must uniquely identify property graph nodes in the MDB.

Parameters:
  • entity_1 – First entity to link.

  • entity_2 – Second entity to link.

  • mapping_source – Source of the mapping relationship.

  • _commit – Optional commit string. If set, the _commit property of any node created is set to this value.

get_entity_nanoid(entity: bento_meta.entity.Entity) list[neo4j.Record][source]

Take a unique entity in the MDB and return its nanoid.

get_or_make_entity_nanoid(entity: bento_meta.entity.Entity) str[source]

Obtain existing entity’s nanoid or create one for new entity.

get_term_nanoids(concept: bento_meta.objects.Concept, mapping_source: str = '') list[neo4j.Record][source]

Return list of term nanoids representing given concept.

get_predicate_nanoids(concept: bento_meta.objects.Concept, mapping_source: str = '') list[neo4j.Record][source]

Return list of predicate nanoids with relationship to given concept.

get_relationship_between_entities(src_entity: bento_meta.entity.Entity, dst_entity: bento_meta.entity.Entity) list[neo4j.Record][source]

Return relationship type between given entities.

Relationship pattern: (src)-[:rel_type]->(dst).

Link two synonymous Concepts via a Predicate.

This function takes two synonymous Concepts as bento-meta objects and links them via a Predicate node and has_subject and has_object relationships.

Parameters:
  • subject_concept – The subject concept.

  • object_concept – The object concept.

  • predicate – The predicate linking them.

  • _commit – Optional commit string to tag created nodes.

merge_two_concepts(concept_1: bento_meta.objects.Concept, concept_2: bento_meta.objects.Concept, mapping_source: str = '', _commit: str = '') None[source]

Combine two synonymous Concepts into a single Concept.

This function takes two synonymous Concepts as bento-meta objects and merges them into a single Concept along with any connected Terms and Predicates.

Parameters:
  • concept_1 – First concept (will remain after merge).

  • concept_2 – Second concept (will be deleted after merge).

  • mapping_source – Source of the mapping relationship.

  • _commit – Optional commit string to tag relationships.

_get_all_terms() list[neo4j.Record][source]

Return list of all terms in an MDB.

get_potential_term_synonyms(term: bento_meta.objects.Term, threshhold: float = 0.8) list[dict][source]

Return list of dicts representing potential synonymous Term nodes.

Parameters:
  • term – The term to find synonyms for.

  • threshhold – Similarity threshold (0-1) for considering terms synonymous.

Returns:

List of dicts with value, origin_name, nanoid, similarity, valid_synonym.

potential_synonyms_to_csv(input_data: list[dict], output_path: str) None[source]

Write CSV with list of synonymous Terms as dicts to given output path.

Parameters:
  • input_data – List of dicts representing potential synonyms.

  • output_path – Path to write CSV file to.

Link Terms in a CSV of synonymous Terms to given Term via a Concept node.

Parameters:
  • term – The term to link synonyms to.

  • csv_path – Path to CSV file containing potential synonyms.

  • mapping_source – Source of the mapping relationship.

  • _commit – Optional commit string to tag relationships.

get_property_synonyms_direct(entity: bento_meta.objects.Property, mapping_source: str = '') list[neo4j.Record][source]

Return list of properties linked by concept to given property.

Parameters:
  • entity – The property to get synonyms for.

  • mapping_source – Optional mapping source to filter by.

Returns:

List of Records with property synonyms.

_get_property_synonyms_direct_as_list(entity: bento_meta.objects.Property) list[bento_meta.objects.Property][source]

Convert results of read_txn_data-wrapped function with one Property to a list.

Parameters:

entity – The property to get synonyms for.

Returns:

List of bento_meta.objects.Property entities.

get_property_synonyms_all(entity: bento_meta.objects.Property) list[bento_meta.objects.Property][source]

Return list of properties linked by concept to given property or its synonyms.

Chains through synonyms of given property (and so on).

Parameters:

entity – The property to get all synonyms for.

Returns:

List of Property entities that are synonyms.

_get_property_parents_data(entity: bento_meta.objects.Property) list[neo4j.Record][source]

Get list of nodes/edges connected to given property.

Parents are Nodes or Edges via the “has_property” relationship.

Parameters:

entity – The property to get parents for.

Returns:

List of Records with nodes and edges.

get_property_parents(entity: bento_meta.objects.Property) list[bento_meta.objects.Node | bento_meta.objects.Edge][source]

Get parents of a property as a list of bento_meta Nodes or Edges.

Parameters:

entity – The property to get parents for.

Returns:

List of Node or Edge entities.

add_tag_to_mdb_entity(tag: bento_meta.objects.Tag, entity: bento_meta.entity.Entity) None[source]

Add a tag to an existing entity in an MDB.

Parameters:
  • tag – The tag to add.

  • entity – The entity to tag.

class bento_meta.mdb.mdb_tools.mdb_tools.EntityValidator[source]

Class to validate that bento-meta entities have all required attributes.

required_attrs_by_entity_type: ClassVar[dict[type[bento_meta.entity.Entity], list[str]]]
valid_attrs: ClassVar[dict[tuple[type[bento_meta.entity.Entity], str], set[str]]]
exception MissingAttributeError[source]

Bases: Exception

An entity doesn’t have the attributes required for unique identification.

exception InvalidAttributeError[source]

Bases: Exception

An entity attribute is invalid.

static validate_entity_has_attribute(entity: bento_meta.entity.Entity, attr_name: str) None[source]

Validate the presence of an entity’s attribute.

static _validate_entity_attribute(entity_type: type[bento_meta.entity.Entity], attr_name: str) None[source]

Check that an entity attribute is in a set of valid attributes.

static validate_entity(entity: bento_meta.entity.Entity) None[source]

Check if entity has all attributes required by MDB for its type.

Also checks validates all of those attributes are valid themselves if they are entities or if they have a fixed set of possible values.

If looking for a unique identifier for the entity (i.e. nanoid), ensures entity has all the required attributes for unique identification.

Parameters:

entity – The entity to validate.

Raises:
bento_meta.mdb.mdb_tools.mdb_tools._get_nlp_model()[source]

Install and import spacy & scispacy nlp model if any not already installed.