Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for enum mapping for slots with multiple enum ranges #31

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
identity and expression, level of experience, education, socioeconomic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.

Expand Down
59 changes: 34 additions & 25 deletions src/linkml_map/transformer/object_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,15 @@ def map_object(
elif target_type == "curie":
return self.compress_uri(source_obj)
return source_obj
if source_type in sv.all_enums():
# TODO: enum derivations
return self.transform_enum(source_obj, source_type, source_obj)
# return str(source_obj)

# Do enumeration transform if source_type has enumeration name(s)
source_type_enums = yaml.safe_load(source_type)
if not isinstance(source_type_enums, list):
source_type_enums = [source_type_enums]
source_type_enums = [enum for enum in source_type_enums if enum in sv.all_enums()]
if len(source_type_enums) > 0:
return self.transform_enum(source_obj, source_type_enums, source_obj)

source_obj_typed = None
if isinstance(source_obj, (BaseModel, YAMLRoot)):
# ensure dict
Expand Down Expand Up @@ -424,24 +429,28 @@ def transform_object(
tr_obj_dict = self.map_object(source_obj, source_type_name)
return target_class(**tr_obj_dict)

def transform_enum(self, source_value: str, enum_name: str, source_obj: Any) -> Optional[str]:
enum_deriv = self._get_enum_derivation(enum_name)
if enum_deriv.expr:
try:
if enum_deriv.expr:
v = eval_expr(enum_deriv.expr, **source_obj, NULL=None)
except Exception:
aeval = Interpreter(usersyms={"src": source_obj, "target": None})
aeval(enum_deriv.expr)
v = aeval.symtable["target"]
if v is not None:
return v
for pv_deriv in enum_deriv.permissible_value_derivations.values():
if source_value == pv_deriv.populated_from:
return pv_deriv.name
if source_value in pv_deriv.sources:
return pv_deriv.name
if enum_deriv.mirror_source:
return str(source_value)
else:
return None
def transform_enum(
self, source_value: str, enum_name: Union[str, List[str]], source_obj: Any
) -> Optional[str]:
if isinstance(enum_name, str):
enum_name = [enum_name]
for cur_enum in enum_name:
enum_deriv = self._get_enum_derivation(cur_enum)
if enum_deriv.expr:
try:
if enum_deriv.expr:
v = eval_expr(enum_deriv.expr, **source_obj, NULL=None)
except Exception:
aeval = Interpreter(usersyms={"src": source_obj, "target": None})
aeval(enum_deriv.expr)
v = aeval.symtable["target"]
if v is not None:
return v
for pv_deriv in enum_deriv.permissible_value_derivations.values():
if source_value == pv_deriv.populated_from:
return pv_deriv.name
if source_value in pv_deriv.sources:
return pv_deriv.name
if enum_deriv.mirror_source:
return str(source_value)
return None
21 changes: 21 additions & 0 deletions tests/input/examples/multi_enum/data/Container-001.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
lights:
- colors:
- light_red
- dark_green
- dark_blue
- light_magenta
- not_available
main_color: dark_green
mirror_test_slot: light_red
- colors:
- light_green
main_color: light_green
mirror_test_slot: light_magenta
- colors:
- other
main_color: other
mirror_test_slot: dark_purple
- colors:
- light_blue
main_color: light_blue
mirror_test_slot: not_available
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
lights_inventory:
- light_colors:
- red
- green
- blue
- magenta
- na
light_main_color: green
mirror_test_slot: red
- light_colors:
- green
light_main_color: green
mirror_test_slot: magenta
- light_colors:
- oth
light_main_color: oth
mirror_test_slot: purple
- light_colors:
- blue
light_main_color: blue
mirror_test_slot: not_available
65 changes: 65 additions & 0 deletions tests/input/examples/multi_enum/source/lights.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: light_bulbs
id: light_bulbs
description: Light Bulbs
imports:
- linkml:types
default_range: string
enums:
primary_colors:
permissible_values:
light_red:
dark_red:
light_green:
dark_green:
light_blue:
dark_blue:
secondary_colors:
permissible_values:
light_cyan:
dark_cyan:
light_magenta:
dark_magenta:
light_yellow:
dark_yellow:
mirror_test_colors:
permissible_values:
dark_purple:
dark_turquoise:
dark_fuchsia:
missingness:
permissible_values:
not_available:
other:
classes:
lights:
slots:
- colors
- main_color
- mirror_test_slot
Container:
tree_root: True
attributes:
lights:
range: lights
multivalued: True
inlined_as_list: True
slots:
colors:
range:
- primary_colors
- secondary_colors
- missingness
multivalued: True
inlined_as_list: True
main_color:
range:
- primary_colors
- secondary_colors
- missingness
multivalued: False
mirror_test_slot:
range:
- primary_colors
- secondary_colors
- mirror_test_colors
- missingness
61 changes: 61 additions & 0 deletions tests/input/examples/multi_enum/target/lights_inventory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: light_bulbs_simple
id: light_bulbs_simple
description: Light Bulbs (Simplified)
imports:
- linkml:types
default_range: string
enums:
light_size_enum:
permissible_values:
size_0:
size_1:
size_2:
light_primary_colors_enum:
permissible_values:
red:
green:
blue:
light_secondary_colors_enum:
permissible_values:
cyan:
magenta:
yellow:
missingness_enum:
permissible_values:
na:
oth:
classes:
lights_inventory:
slots:
- light_sizes
- light_colors
- light_mirror_test_slot
Container:
tree_root: True
attributes:
all_lights:
range: lights_inventory
multivalued: True
inlined_as_list: True
slots:
light_sizes:
range:
- light_size_enum
- missingness_enum
multivalued: True
inlined_as_list: True
light_colors:
range:
- light_primary_colors_enum
- light_secondary_colors_enum
- missingness_enum
multivalued: True
inlined_as_list: True
light_main_color:
range:
- light_primary_colors_enum
- light_secondary_colors_enum
- missingness_enum
multivalued: False
light_mirror_test_slot:
range: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class_derivations:
lights_inventory:
name: lights_inventory
populated_from: lights
slot_derivations:
light_colors:
name: light_colors
populated_from: colors
light_main_color:
name: light_main_color
populated_from: main_color
light_mirror_test_slot:
name: mirror_test_slot
populated_from: mirror_test_slot
Container:
name: Container
slot_derivations:
lights_inventory:
populated_from: lights
range: string
enum_derivations:
missingness_enum:
name: missingness_enum
mirror_source: false
populated_from: missingness
permissible_value_derivations:
na:
name: na
populated_from: not_available
oth:
name: oth
populated_from: other
light_primary_colors_enum:
name: light_primary_colors_enum
mirror_source: false
populated_from: primary_colors
permissible_value_derivations:
red:
name: red
sources:
- light_red
- dark_red
green:
name: green
sources:
- light_green
- dark_green
blue:
name: blue
sources:
- light_blue
- dark_blue
light_secondary_colors_enum:
name: light_secondary_colors_enum
mirror_source: false
populated_from: secondary_colors
permissible_value_derivations:
cyan:
name: cyan
sources:
- light_cyan
- dark_cyan
magenta:
name: magenta
sources:
- light_magenta
- dark_magenta
yellow:
name: yellow
sources:
- light_yellow
- dark_yellow
light_test_colors_enum:
# This derivation is for testing mirror_source=True. When transforming a slot with multiple enumerations in the range,
# and we encounter an enum derivation with mirror_source=True, then if no permissible value derivation is matched we return
# the source value unchanged, and ignore any further enum derivations.
name: light_test_colors_enum
mirror_source: true
populated_from: mirror_test_colors
permissible_value_derivations:
purple:
name: purple
populated_from: dark_purple
turquoise:
name: turquoise
populated_from: dark_turquoise
fuchsia:
name: fuchsia
populated_from: dark_fuchsia

1 change: 1 addition & 0 deletions tests/test_transformer/test_transformer_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"personinfo_basic",
"type_coercion",
"biolink",
"multi_enum",
]


Expand Down
Loading