Skip to content

Commit

Permalink
Merge pull request #118 from pyiron/function
Browse files Browse the repository at this point in the history
Annotate Functions
  • Loading branch information
samwaseda authored Feb 3, 2025
2 parents 6b216df + 89e6363 commit aaf2a6f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
19 changes: 17 additions & 2 deletions pyiron_ontology/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def get_source_output(var: Node) -> str | None:
return f"{connection.owner.label}.outputs.{connection.label}"


def _get_function_dict(function):
result = {
"label": function.__name__,
}
if hasattr(function, "_semantikon_metadata"):
result.update(function._semantikon_metadata)
return result


def get_inputs_and_outputs(node: Node) -> dict:
"""
Read input and output arguments with their type hints and return a
Expand Down Expand Up @@ -59,7 +68,7 @@ def get_inputs_and_outputs(node: Node) -> dict:
return {
"inputs": inputs,
"outputs": outputs,
"function": node.node_function.__name__,
"function": _get_function_dict(node.node_function),
"label": node.label,
}

Expand Down Expand Up @@ -168,7 +177,13 @@ def get_triples(
graph = Graph()
node_label = workflow_namespace + data["label"]
graph.add((URIRef(node_label), RDF.type, PROV.Activity))
graph.add((URIRef(node_label), PNS.hasSourceFunction, URIRef(data["function"])))
graph.add(
(URIRef(node_label), PNS.hasSourceFunction, URIRef(data["function"]["label"]))
)
if data["function"].get("uri", None) is not None:
graph.add((URIRef(node_label), RDF.type, URIRef(data["function"]["uri"])))
for t in _get_triples_from_restrictions(data["function"]):
graph.add(_parse_triple(t, ns=node_label, label=URIRef(node_label)))
for io_ in ["inputs", "outputs"]:
for key, d in data[io_].items():
channel_label = URIRef(node_label + f".{io_}." + key)
Expand Down
20 changes: 8 additions & 12 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def calculate_speed(


@Workflow.wrap.as_function_node("result")
@u(uri=EX.Addition)
def add(a: float, b: float) -> u(float, triples=(EX.HasOperation, EX.Addition)):
return a + b

Expand Down Expand Up @@ -170,19 +171,14 @@ def test_parse_workflow(self):
wf = Workflow("correct_analysis")
wf.addition = add(a=1.0, b=2.0)
graph = parse_workflow(wf)
tag = "correct_analysis.addition.inputs.a"
self.assertEqual(
len(
list(
graph.triples(
(
URIRef("correct_analysis.addition.inputs.a"),
RDFS.label,
Literal("correct_analysis.addition.inputs.a"),
)
)
)
),
1,
len(list(graph.triples((URIRef(tag), RDFS.label, Literal(tag))))), 1,
)
self.assertTrue(
EX.Addition in list(
graph.objects(URIRef("correct_analysis.addition"), RDF.type)
)
)

def test_macro(self):
Expand Down

0 comments on commit aaf2a6f

Please sign in to comment.