medmodels.medrecord.schema#

This module contains the schema classes for the medrecord module.

Classes

AttributeType(value[, names, module, ...])

Enumeration of attribute types.

GroupSchema(*[, nodes, edges])

A schema for a group of nodes and edges.

Schema(*[, groups, ungrouped, schema_type])

A schema for a collection of groups.

SchemaType(value[, names, module, qualname, ...])

Enumeration of schema types.

class AttributeType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

Enumeration of attribute types.

Categorical = 1#
Continuous = 2#
Temporal = 3#
Unstructured = 4#
classmethod __contains__(value)#

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

__eq__(value)[source]#

Compares the AttributeType instance to another object for equality.

Parameters:

value (object) – The object to compare against.

Return type:

bool

Returns:

bool – True if the objects are equal, False otherwise.

classmethod __getitem__(name)#

Return the member matching name.

__hash__()[source]#

Returns the hash of the AttributeType instance.

Return type:

int

Returns:

int – The hash of the AttributeType instance.

classmethod __iter__()#

Return members in definition order.

classmethod __len__()#

Return the number of members (no aliases)

__repr__()[source]#

Returns a string representation of the AttributeType instance.

Return type:

str

Returns:

str – String representation of the attribute type.

__str__()[source]#

Returns a string representation of the AttributeType instance.

Return type:

str

Returns:

str – String representation of the attribute type.

static infer(data_type)[source]#

Infers the attribute type from the data type.

Parameters:

data_type (DataType) – The data type to infer the attribute type from.

Return type:

AttributeType

Returns:

AttributeType – The inferred attribute type.

class GroupSchema(*, nodes=None, edges=None)[source]#

Bases: object

A schema for a group of nodes and edges.

property edges: Dict[str | int, Tuple[DataType, Literal[AttributeType.Categorical]] | Tuple[Int | Float | Null | Option[Int | Float | Null | Option[ContinuousType] | ContinuousType | ContinuousType] | Int | Float | Null | Option[ContinuousType] | ContinuousType | ContinuousType | Int | Float | Null | Option[ContinuousType] | ContinuousType | ContinuousType, Literal[AttributeType.Continuous]] | Tuple[DateTime | Duration | Null | Option[DateTime | Duration | Null | Option[TemporalType] | TemporalType | TemporalType] | DateTime | Duration | Null | Option[TemporalType] | TemporalType | TemporalType | DateTime | Duration | Null | Option[TemporalType] | TemporalType | TemporalType, Literal[AttributeType.Temporal]] | Tuple[DataType, Literal[AttributeType.Unstructured]]]#

Returns the edge attributes in the GroupSchema instance.

Returns:

AttributesSchema

An AttributesSchema object containing the edge attributes

and their data types.

property nodes: Dict[str | int, Tuple[DataType, Literal[AttributeType.Categorical]] | Tuple[Int | Float | Null | Option[Int | Float | Null | Option[ContinuousType] | ContinuousType | ContinuousType] | Int | Float | Null | Option[ContinuousType] | ContinuousType | ContinuousType | Int | Float | Null | Option[ContinuousType] | ContinuousType | ContinuousType, Literal[AttributeType.Continuous]] | Tuple[DateTime | Duration | Null | Option[DateTime | Duration | Null | Option[TemporalType] | TemporalType | TemporalType] | DateTime | Duration | Null | Option[TemporalType] | TemporalType | TemporalType | DateTime | Duration | Null | Option[TemporalType] | TemporalType | TemporalType, Literal[AttributeType.Temporal]] | Tuple[DataType, Literal[AttributeType.Unstructured]]]#

Returns the node attributes in the GroupSchema instance.

Returns:

AttributesSchema

An AttributesSchema object containing the node attributes

and their data types.

validate_edge(index, attributes)[source]#

Validates the attributes of an edge against the schema.

Parameters:
  • index (EdgeIndex) – The index of the edge.

  • attributes (Attributes) – The attributes of the edge.

Return type:

None

validate_node(index, attributes)[source]#

Validates the attributes of a node against the schema.

Parameters:
  • index (NodeIndex) – The index of the node.

  • attributes (Attributes) – The attributes of the node.

Return type:

None

class Schema(*, groups=None, ungrouped=None, schema_type=None)[source]#

Bases: object

A schema for a collection of groups.

add_group(group, group_schema)[source]#

Adds a new group to the schema.

Parameters:
  • group (Group) – The name of the group.

  • group_schema (GroupSchema) – The schema for the group.

Return type:

None

freeze()[source]#

Freezes the schema. No changes are automatically inferred.

Return type:

None

group(group)[source]#

Retrieves the schema for a specific group.

Parameters:

group (Group) – The name of the group.

Return type:

GroupSchema

Returns:

GroupSchema – The schema for the specified group.

Raises:

ValueError – If the group does not exist in the schema.

property groups: List[str | int]#

Lists all the groups in the Schema instance.

Returns:

List[Group] – A list of groups.

classmethod infer(medrecord)[source]#

Infers a schema from a MedRecord instance.

Parameters:

medrecord (MedRecord) – The MedRecord instance to infer the schema from.

Return type:

Schema

Returns:

Schema – The inferred schema.

remove_edge_attribute(attribute, group=None)[source]#

Removes an edge attribute from the schema.

Parameters:
  • attribute (MedRecordAttribute) – The name of the attribute to remove.

  • group (Optional[Group], optional) – The group to remove the attribute from. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

remove_group(group)[source]#

Removes a group from the schema.

Parameters:

group (Group) – The name of the group to remove.

Return type:

None

remove_node_attribute(attribute, group=None)[source]#

Removes a node attribute from the schema.

Parameters:
  • attribute (MedRecordAttribute) – The name of the attribute to remove.

  • group (Optional[Group], optional) – The group to remove the attribute from. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

property schema_type: SchemaType#

Retrieves the schema type.

Returns:

SchemaType – The schema type.

set_edge_attribute(attribute, data_type, attribute_type=None, group=None)[source]#

Sets the data type and attribute type of an edge attribute.

If a data type for the attribute already exists, it is overwritten.

Parameters:
  • attribute (MedRecordAttribute) – The name of the attribute.

  • data_type (DataType) – The data type of the attribute.

  • attribute_type (Optional[AttributeType], optional) – The attribute type of the attribute. If not provided, the attribute type is inferred from the data type. Defaults to None.

  • group (Optional[Group], optional) – The group to set the attribute for. If no schema for this group exists, a new schema is created. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

set_node_attribute(attribute, data_type, attribute_type=None, group=None)[source]#

Sets the data type and attribute type of a node attribute.

If a data type for the attribute already exists, it is overwritten.

Parameters:
  • attribute (MedRecordAttribute) – The name of the attribute.

  • data_type (DataType) – The data type of the attribute.

  • attribute_type (Optional[AttributeType], optional) – The attribute type of the attribute. If not provided, the attribute type is inferred from the data type. Defaults to None.

  • group (Optional[Group], optional) – The group to set the attribute for. If no schema for the group exists, a new schema is created. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

unfreeze()[source]#

Unfreezes the schema. Changes are automatically inferred.

Return type:

None

property ungrouped: GroupSchema#

Retrieves the group schema for all ungrouped nodes and edges.

Returns:

GroupSchema – The ungrouped group schema.

update_edge_attribute(attribute, data_type, attribute_type=None, group=None)[source]#

Updates the data type and attribute type of an edge attribute.

If a data type for the attribute already exists, it is merged with the new data type.

Parameters:
  • attribute (MedRecordAttribute) – The name of the attribute.

  • data_type (DataType) – The data type of the attribute.

  • attribute_type (Optional[AttributeType], optional) – The attribute type of the attribute. If not provided, the attribute type is inferred from the data type. Defaults to None.

  • group (Optional[Group], optional) – The group to update the attribute for. If no schema for this group exists, a new schema is created. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

update_node_attribute(attribute, data_type, attribute_type=None, group=None)[source]#

Updates the data type and attribute type of a node attribute.

If a data type for the attribute already exists, it is merged with the new data type.

Parameters:
  • attribute (MedRecordAttribute) – The name of the attribute.

  • data_type (DataType) – The data type of the attribute.

  • attribute_type (Optional[AttributeType], optional) – The attribute type of the attribute. If not provided, the attribute type is inferred from the data type. Defaults to None.

  • group (Optional[Group], optional) – The group to update the attribute for. If no schema for this group exists, a new schema is created. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

validate_edge(index, attributes, group=None)[source]#

Validates the attributes of an edge against the schema.

Parameters:
  • index (EdgeIndex) – The index of the edge.

  • attributes (Attributes) – The attributes of the edge.

  • group (Optional[Group], optional) – The group to validate the edge against. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

validate_node(index, attributes, group=None)[source]#

Validates the attributes of a node against the schema.

Parameters:
  • index (NodeIndex) – The index of the node.

  • attributes (Attributes) – The attributes of the node.

  • group (Optional[Group], optional) – The group to validate the node against. If not provided, the ungrouped schema is used. Defaults to None.

Return type:

None

class SchemaType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

Enumeration of schema types.

Inferred = 2#
Provided = 1#
classmethod __contains__(value)#

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members. 3) value is a pseudo-member (flags)

classmethod __getitem__(name)#

Return the member matching name.

classmethod __iter__()#

Return members in definition order.

classmethod __len__()#

Return the number of members (no aliases)