Skip to content

Commit

Permalink
fixed bug, source-type was not passed down
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall committed Mar 7, 2023
1 parent 716e19c commit b1e35dd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 22 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ Commands:
map-data Map data in a source schema using a transformation.
```

### map-data

```
cd tests/input/examples/personinfo_basic
linkml-tr map-data -T transform/personinfo-to-agent.transform.yaml -s source/personinfo.yaml data/Container-001.yaml
```

### derive-schema

```
cd tests/input/examples/personinfo_basic
linkml-tr derive-schema -T transform/personinfo-to-agent.transform.yaml source/personinfo.yaml
```


## Examples

See the tests folder for most up to date examples
Expand Down
57 changes: 35 additions & 22 deletions src/linkml_transformer/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,6 @@ def main(verbose: int, quiet: bool):
logger.info(f"Logger {logger.name} set to level {logger.level}")


@main.command()
@output_option
@transformer_specification_option
@output_format_options
@click.argument("schema")
def derive_schema(schema, transformer_specification, output, output_format, **kwargs):
"""Derive a schema from a source schema and a mapping."""
logging.info(f"Transforming {schema} using {transformer_specification}")
tr = SchemaMapper()
tr.source_schemaview = SchemaView(schema)
specification = yaml_loader.load(
transformer_specification, target_class=TransformationSpecification
)
target_schema = tr.derive_schema(specification)
if output:
file = open(output, "w", encoding="utf-8")
else:
file = sys.stdout
file.write(yaml_dumper.dumps(target_schema))


@main.command()
@output_option
@transformer_specification_option
Expand Down Expand Up @@ -106,13 +85,47 @@ def map_data(
with open(input) as file:
input_obj = yaml.safe_load(file)
tr.index(input_obj, source_type)
tr_obj = tr.transform(input_obj)
tr_obj = tr.transform(input_obj, source_type)
if output:
file = open(output, "w", encoding="utf-8")
else:
file = sys.stdout
file.write(yaml_dumper.dumps(tr_obj))


@main.command()
@output_option
@transformer_specification_option
@output_format_options
@click.argument("schema")
def derive_schema(schema, transformer_specification, output, output_format, **kwargs):
"""Derive a schema from a source schema and a transformation specification.
This can be thought of as "copying" the source to a target, using the transformation
specification as a "patch"
Notes:
the implementation is currently incomplete; the derived schema may not be valid
linkml, e.g. there may be "dangling" references.
Example:
linkml-tr derive-schema -T transform/personinfo-to-agent.transform.yaml source/personinfo.yaml
"""
logging.info(f"Transforming {schema} using {transformer_specification}")
tr = SchemaMapper()
tr.source_schemaview = SchemaView(schema)
specification = yaml_loader.load(
transformer_specification, target_class=TransformationSpecification
)
target_schema = tr.derive_schema(specification)
if output:
file = open(output, "w", encoding="utf-8")
else:
file = sys.stdout
file.write(yaml_dumper.dumps(target_schema))


if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions src/linkml_transformer/utils/inverter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from linkml_transformer.datamodel.transformer_model import TransformationSpecification


def invert_transformation_specification(specification: TransformationSpecification) -> TransformationSpecification:
raise NotImplementedError
31 changes: 31 additions & 0 deletions tests/test_transformer/test_object_transformer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import itertools
import unittest

import yaml
from linkml_runtime import SchemaView
from linkml_runtime.dumpers import yaml_dumper
from linkml_runtime.linkml_model import ClassDefinition, SlotDefinition, SchemaDefinition
from linkml_runtime.loaders import yaml_loader

import tests.input.examples.flattening.model.denormalized_model as sssom_tgt_dm
Expand Down Expand Up @@ -224,6 +226,35 @@ def test_denormalized_object_transform(self):
self.assertEqual(mapping.object_id, "Y:1")
self.assertEqual(mapping.object_name, "y1")

@unittest.skip("TODO")
def test_cardinalities(self):
tf = [True, False]
class_name = "MyClass"
att_name = "my_att"
val = "v1"
for source_multivalued, target_multivalued in itertools.product(tf, tf):
def mk(mv: bool):
cls = ClassDefinition(class_name)
att = SlotDefinition(att_name, multivalued=mv)
cls.attributes[att.name] = att
schema = SchemaDefinition(name="test", id="test", classes=[cls])
return schema
source_schema = mk(source_multivalued)
target_schema = mk(target_multivalued)
specification = TransformationSpecification("test")
cd = ClassDerivation(class_name, populated_from=class_name)
specification.class_derivations[class_name] = cd
cd.slot_derivations[att_name] = SlotDerivation(att_name, populated_from=class_name)
source_instance = {att_name: [val] if source_multivalued else val}
tr = ObjectTransformer(specification=specification,
source_schemaview=SchemaView(source_schema))
target_instance = tr.transform(source_instance, class_name)
print(target_instance)






if __name__ == "__main__":
unittest.main()

0 comments on commit b1e35dd

Please sign in to comment.