Inferring Missing Values#
Expressions#
LinkML supports the use of expressions to define how slot values can be derived from other slot values. The slot equals_expression specifies how to calculate the value of a slot, as demonstrated here:
class:
Person:
attributes:
age_in_years:
range: decimal
minimum_value: 0
maximum_value: 999
equals_expression: "{age_in_months} / 12"
age_in_months:
range: decimal
equals_expression: "{age_in_years} * 12"
is_juvenile:
range: boolean
equals_expression: "{age_in_years} < 18"
The following code will populate missing values:
from linkml_runtime.utils.inference_utils import infer_all_slot_values
from .personinfo infer Person
p = Person(age_in_years=30)
infer_all_slot_values(p, schemaview=sv)
assert p.age_in_months == 360
assert not p.juvenile
You can also use the linkml-convert
script with the --infer
flag
String serialization#
For simple string based expressions, string_serialization can be used:
Address:
attributes:
street:
city:
full_address:
string_serialization: |-
{street}
{city}
The following code will populate the full address:
from linkml_runtime.utils.inference_utils import infer_all_slot_values
from .personinfo infer Address
a = Address(street="1 Oak street", city="Oaktown")
infer_all_slot_values(p, schemaview=sv)
print(a.full_address)
Configuration#
- class linkml_runtime.utils.inference_utils.Config(use_string_serialization: bool = <factory>, parse_string_serialization: bool = <factory>, use_rules: bool = <factory>, use_expressions: bool = <factory>, resolve_function: ~typing.Callable[[str, ~typing.Any], ~typing.Any] = None)[source]#
Controls which inferences are performed
slot.string_serialization
slot.equals_expression
Code#
- linkml_runtime.utils.inference_utils.infer_slot_value(obj: YAMLRoot, slot_name: str | SlotDefinitionName, schemaview: SchemaView, class_name: str | ClassDefinitionName = None, policy: Policy = Policy.STRICT, config: Config = Config(use_string_serialization=True, parse_string_serialization=False, use_rules=False, use_expressions=False, resolve_function=None))[source]#
Infer the value of a slot for an object
- Parameters:
obj – mutable object to be transformed
slot_name –
schemaview –
class_name –
policy –
config –
- linkml_runtime.utils.inference_utils.infer_all_slot_values(obj: YAMLRoot, schemaview: SchemaView, class_name: str | ClassDefinitionName = None, policy: Policy = Policy.STRICT, config: Config = Config(use_string_serialization=True, parse_string_serialization=False, use_rules=False, use_expressions=False, resolve_function=None))[source]#
Walks object tree inferring all slot values.
if a slot has a string_serialization metaslot, apply this
if a slot has a equals_expression metaslot, apply this. See
eval_expr()
- Parameters:
obj –
schemaview –
class_name –
policy – default is STRICT
config –
- Returns: