Meta Knowledge Graph#
The Meta Knowledge Graph operation takes an instance of kgx.graph.base_graph.BaseGraph
and
generates Translator API (TRAPI) Release 1.1 compatible knowledge map for the entire graph.
This operation generates graph summary as a JSON (or YAML) in a format that is compatible with the content metadata standards of the Knowledge Graph Exchange (KGE) Archive.
The main entry point is the kgx.graph_operations.meta_knowledge_graph.generate_meta_knowledge_graph
method.
The tool does detect and logs anomalies in the graph (defaults reporting to stderr, but may be reset to a file using the error_log
parameter)
Note: To generate a summary statistics YAML that is compatible with Knowledge Graph Hub dashboard, refer to Summarize Graph operation.
Streaming Data Processing Mode#
For very large graphs, the Meta Knowledge Graph operation now successfully processes graph data using data streaming (command flag --stream=True
) which significantly minimizes the memory footprint required to process such graphs.
Provenance Statistics#
The Meta Knowledge Graph operation can count numbers of nodes and edges by Biolink 2.0 biolink:knowledge_source
provenance (and related is_a
descendant slot terms). The node_facet_properties
and edge_facet_properties
CLI (and code method) arguments need to be explicitly set to specify which provenance slot names are to be counted in a given graph (by default, provided_by
slots used for nodes and knowledge_source
slots used for edges).
kgx.graph_operations.meta_knowledge_graph#
Translator Reasoner API ‘meta-knowledge-graph’ endpoint analogous graph summary module.
- class kgx.graph_operations.meta_knowledge_graph.MetaKnowledgeGraph(name='', node_facet_properties: Optional[List] = None, edge_facet_properties: Optional[List] = None, progress_monitor: Optional[Callable[[GraphEntityType, List], None]] = None, error_log=None, **kwargs)[source]#
Bases:
ErrorDetecting
Class for generating a TRAPI 1.1 style of “meta knowledge graph” summary.
The optional ‘progress_monitor’ for the validator should be a lightweight Callable which is injected into the class ‘inspector’ Callable, designed to intercepts node and edge records streaming through the Validator (inside a Transformer.process() call. The first (GraphEntityType) argument of the Callable tags the record as a NODE or an EDGE. The second argument given to the Callable is the current record itself. This Callable is strictly meant to be procedural and should not mutate the record. The intent of this Callable is to provide a hook to KGX applications wanting the namesake function of passively monitoring the graph data stream. As such, the Callable could simply tally up the number of times it is called with a NODE or an EDGE, then provide a suitable (quick!) report of that count back to the KGX application. The Callable (function/callable class) should not modify the record and should be of low complexity, so as not to introduce a large computational overhead to validation!
- class Category(category_curie: str, mkg)[source]#
Bases:
object
Internal class for compiling statistics about a distinct category.
- analyse_node_category(n, data) None [source]#
Analyse metadata of a given graph node record of this category.
- Parameters:
n (str) – Curie identifier of the node record (not used here).
data (Dict) – Complete data dictionary of node record fields.
- get_cid()[source]#
- Returns:
Internal MetaKnowledgeGraph index id for tracking a Category.
- Return type:
- get_count_by_source(facet: str = 'provided_by', source: Optional[str] = None) Dict[str, Any] [source]#
- Parameters:
- Returns:
Count of nodes, by node ‘provided_by’ knowledge source, for a given category. Returns dictionary of all source counts, if input ‘source’ argument is not specified.
- Return type:
Dict
- analyse_edge(u, v, k, data) None [source]#
Analyse metadata of one graph edge record. :param u: Subject node curie identifier of the edge. :type u: str :param v: Subject node curie identifier of the edge. :type v: str :param k: Key identifier of the edge record (not used here). :type k: str :param data: Complete data dictionary of edge record fields. :type data: Dict
- analyse_node(n: str, data: Dict) None [source]#
Analyse metadata of one graph node record.
- Parameters:
n (str) – Curie identifier of the node record (not used here).
data (Dict) – Complete data dictionary of node record fields.
- clear_errors()#
Clears the current error log list
- get_category(category_curie: str) Category [source]#
Counts the number of distinct (Biolink) categories encountered in the knowledge graph (not including those of ‘unknown’ category)
- get_edge_count_by_predicate(predicate_curie: str) int [source]#
Counts the number of edges in the graph with the specified predicate.
- Parameters:
predicate_curie (str) – (Biolink) curie identifier for the predicate.
- Returns:
Number of edges for the given predicate.
- Return type:
- Raises:
RuntimeError – Error if predicate identifier is empty string or None.
- get_edge_count_by_source(subject_category: str, predicate: str, object_category: str, facet: str = 'knowledge_source', source: Optional[str] = None) Dict[str, Any] [source]#
Returns count by source for one S-P-O triple (S, O being Biolink categories; P, a Biolink predicate)
- get_edge_mapping_count() int [source]#
Counts the number of distinct edge Subject (category) - P (predicate) -> Object (category) mappings in the knowledge graph.
- Returns:
Count of subject(category) - predicate -> object(category) mappings in the graph.
- Return type:
- get_edge_stats() List[Dict[str, Any]] [source]#
- Returns:
Knowledge map for the list of edges in the graph.
- Return type:
List[Dict[str, Any]]
- get_errors(level: Optional[str] = None) Dict #
Get the index list of distinct error messages.
- Parameters:
level (str) – Optional filter (case insensitive) name of error message level (generally either “Error” or “Warning”)
- Returns:
A raw dictionary of entities indexed by [message_level][error_type][message] or only just [error_type][message] specific to a given message level if the optional level filter is given
- Return type:
Dict
- static get_facet_counts(facets: Optional[List], counts_by_source: Dict, data: Dict)[source]#
Get node or edge facet counts
- get_graph_summary(name: Optional[str] = None, **kwargs) Dict [source]#
Similar to summarize_graph except that the node and edge statistics are already captured in the MetaKnowledgeGraph class instance (perhaps by Transformer.process() stream inspection) and therefore, the data structure simply needs to be ‘finalized’ for saving or similar use.
- Parameters:
name (Optional[str]) – Name for the graph (if being renamed)
kwargs (Dict) – Any additional arguments (ignored in this method at present)
- Returns:
A TRAPI 1.1 compliant meta knowledge graph of the knowledge graph returned as a dictionary.
- Return type:
Dict
- get_node_count_by_category(category_curie: str) int [source]#
Counts the number of edges in the graph with the specified (Biolink) category curie.
- Parameters:
category_curie (str) – Curie identifier for the (Biolink) category.
- Returns:
Number of nodes for the given category.
- Return type:
- Raises:
RuntimeError – Error if category identifier is empty string or None.
- get_number_of_categories() int [source]#
Counts the number of distinct (Biolink) categories encountered in the knowledge graph (not including those of ‘unknown’ category)
- Returns:
Number of distinct (Biolink) categories found in the graph (excluding nodes with ‘unknown’ category)
- Return type:
- get_predicate_count() int [source]#
Counts the number of distinct edge predicates in the knowledge graph.
- Returns:
Number of distinct (Biolink) predicates in the graph.
- Return type:
- get_total_edge_counts_across_mappings() int [source]#
Aggregate count of the edges in the graph for every mapping. Edges with subject and object nodes with multiple assigned categories will have their count replicated under each distinct mapping of its categories.
- Returns:
Number of the edges counted across all mappings.
- Return type:
- get_total_edges_count() int [source]#
Gets the total number of ‘valid’ edges in the data set (ignoring those with ‘unknown’ subject or predicate category mappings)
- Returns:
Total count of edges in the graph.
- Return type:
- get_total_node_counts_across_categories() int [source]#
The aggregate count of all node to category mappings for every category. Note that nodes with multiple categories will have their count replicated under each of its categories.
- Returns:
Total count of node to category mappings for the graph.
- Return type:
- get_total_nodes_count() int [source]#
Counts the total number of distinct nodes in the knowledge graph (not including those ignored due to being of ‘unknown’ category)
- Returns:
Number of distinct nodes in the knowledge.
- Return type:
- log_error(entity: str, error_type: ErrorType, message: str, message_level: MessageLevel = MessageLevel.ERROR)#
Log an error to the list of such errors.
- Parameters:
entity – source of parse error
error_type – ValidationError ErrorType,
message – message string describing the error
message_level – ValidationError MessageLevel
- save(file, name: Optional[str] = None, file_format: str = 'json') None [source]#
Save the current MetaKnowledgeGraph to a specified (open) file (device).
- summarize_graph(graph: BaseGraph, name: Optional[str] = None, **kwargs) Dict [source]#
Generate a meta knowledge graph that describes the composition of the graph.
- Parameters:
graph (kgx.graph.base_graph.BaseGraph) – The graph
name (Optional[str]) – Name for the graph
kwargs (Dict) – Any additional arguments (ignored in this method at present)
- Returns:
A TRAPI 1.1 compliant meta knowledge graph of the knowledge graph returned as a dictionary.
- Return type:
Dict
- summarize_graph_edges(graph: BaseGraph) List[Dict] [source]#
Summarize the edges in a graph.
- Parameters:
graph (kgx.graph.base_graph.BaseGraph) – The graph
- Returns:
The edge stats
- Return type:
List[Dict]
- summarize_graph_nodes(graph: BaseGraph) Dict [source]#
Summarize the nodes in a graph.
- Parameters:
graph (kgx.graph.base_graph.BaseGraph) – The graph
- Returns:
The node stats
- Return type:
Dict
- kgx.graph_operations.meta_knowledge_graph.generate_meta_knowledge_graph(graph: BaseGraph, name: str, filename: str, **kwargs) None [source]#
Generate a knowledge map that describes the composition of the graph and write to
filename
.- Parameters:
graph (kgx.graph.base_graph.BaseGraph) – The graph
name (Optional[str]) – Name for the graph
filename (str) – The file to write the knowledge map to
Deprecated since version 1.5.8: Default is the use streaming graph_summary with inspector
- kgx.graph_operations.meta_knowledge_graph.mkg_default(o)[source]#
JSONEncoder ‘default’ function override to properly serialize ‘Set’ objects (into ‘List’)
- kgx.graph_operations.meta_knowledge_graph.summarize_graph(graph: BaseGraph, name: Optional[str] = None, **kwargs) Dict [source]#
Generate a meta knowledge graph that describes the composition of the graph.
- Parameters:
graph (kgx.graph.base_graph.BaseGraph) – The graph
name (Optional[str]) – Name for the graph
kwargs (Dict) – Any additional arguments
- Returns:
A TRAPI 1.1 compliant meta knowledge graph of the knowledge graph returned as a dictionary.
- Return type:
Dict