Queries as Function Arguments#

In all other sections, we have used queries with the method query_nodes() for representation purposes of its capacities. However, queries can also be used as function arguments to other methods or indexers from the MedRecord that take edge/node indices or the queries that result on those indices as arguments. Here are some examples of those functions:

  • Using the add_group() to create groups in the MedRecord out of a chosen subset of patients. We need to unfreeze_schema() first, since this new group does not exist in the schema and we have a provided schema in the example dataset.

medrecord.unfreeze_schema()
medrecord.add_group("old_male_patient", nodes=query_node_patient_older_than_30)
medrecord.groups
['diagnosis', 'patient_procedure', 'patient_diagnosis', 'old_male_patient', 'drug', 'procedure', 'patient', 'patient_drug']
  • Using the node[] indexer, which retrieves the attributes for the given node indices.

medrecord.node[query_node_either_or]
{'pat_3': {'age': 96, 'gender': 'F'}, 'pat_5': {'gender': 'M', 'age': 37}}
  • Using groups_of_node(), a method that retrieves the groups to which a specific node index belongs to.

medrecord.groups_of_node(query_node_patient_older_than_30)
{'pat_1': ['patient'], 'pat_5': ['patient'], 'pat_3': ['patient']}
  • Using edge_endpoints(), a method that retrieves the source and target nodes of the specified edge(s) in the MedRecord.

medrecord.edge_endpoints(query_edge_old_patient_cheap_insulin)
{76: ('pat_3', 'drug_311034')}
Methods used in the snippet
  • unfreeze_schema() : Unfreezes the schema. Changes are automatically inferred.

  • add_group() : Adds a group to the MedRecord, optionally with node and edge indices.

  • groups : Lists the groups in the MedRecord instance.

  • node[] : Provides access to node information within the MedRecord instance via an indexer, returning a dictionary with node indices as keys and node attributes as values.

  • groups_of_node() : Retrieves the groups associated with the specified node(s) in the MedRecord.

  • edge_endpoints() : Retrieves the source and target nodes of the specified edge(s) in the MedRecord.

1.  Full example Code#

The full code examples for this chapter can be found here:

from medmodels import MedRecord
from medmodels.medrecord.querying import (
    EdgeIndicesOperand,
    EdgeOperand,
    NodeIndicesOperand,
    NodeOperand,
)

medrecord = MedRecord().from_simple_example_dataset()


def query_node_patient_older_than_30(node: NodeOperand) -> NodeIndicesOperand:
    node.in_group("patient")
    node.index().contains("pat")

    node.has_attribute("age")
    node.attribute("age").greater_than(30)

    return node.index()


def query_edge_either(edge: EdgeOperand) -> None:
    edge.in_group("patient_drug")
    edge.attribute("cost").less_than(200)
    edge.attribute("quantity").equal_to(1)


def query_edge_or(edge: EdgeOperand) -> None:
    edge.in_group("patient_drug")
    edge.attribute("cost").less_than(200)
    edge.attribute("quantity").equal_to(12)


def query_node_either_or(node: NodeOperand) -> NodeIndicesOperand:
    node.in_group("patient")
    node.attribute("age").greater_than(30)

    node.edges().either_or(query_edge_either, query_edge_or)

    return node.index()


def query_edge_old_patient_cheap_insulin(edge: EdgeOperand) -> EdgeIndicesOperand:
    edge.in_group("patient_drug")
    edge.attribute("cost").less_than(200)

    edge.source_node().attribute("age").is_max()
    edge.target_node().attribute("description").contains("insulin")
    return edge.index()


medrecord.unfreeze_schema()
medrecord.add_group("old_male_patient", nodes=query_node_patient_older_than_30)
medrecord.groups

medrecord.node[query_node_either_or]
medrecord.groups_of_node(query_node_patient_older_than_30)
medrecord.edge_endpoints(query_edge_old_patient_cheap_insulin)