-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mapping function plugin system (#152)
# Description: Closes #128 See #128 for design and reasoning behind this PR. Needed for using mapping functions in OTEAPI pipelines. ## Type of change: - [ ] Bug fix. - [x] New feature. - [ ] Documentation update. - [ ] Testing.
- Loading branch information
Showing
9 changed files
with
390 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Test cost.""" | ||
import pytest | ||
|
||
pytest.importorskip("rdflib") | ||
|
||
# pylint: disable=wrong-import-position | ||
from tripper import Triplestore | ||
|
||
ts = Triplestore(backend="rdflib") | ||
|
||
# Define some prefixed namespaces | ||
CHEM = ts.bind("chem", "http://onto-ns.com/onto/chemistry#") | ||
MOL = ts.bind("mol", "http://onto-ns.com/meta/0.1/Molecule#") | ||
SUB = ts.bind("sub", "http://onto-ns.com/meta/0.1/Substance#") | ||
|
||
|
||
def formula_cost(ts, input_iris, output_iri): | ||
"""Returns a cost.""" | ||
# pylint: disable=unused-argument | ||
return 2.72 | ||
|
||
|
||
# Add mappings from data models to ontology | ||
ts.map(MOL.name, CHEM.Identifier, cost=3.14) | ||
ts.map(SUB.formula, CHEM.Formula, cost=formula_cost) | ||
|
||
# pylint: disable=protected-access | ||
assert ts._get_cost(CHEM.Identifier) == 3.14 | ||
assert ts._get_cost(CHEM.Formula) == 2.72 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Test Triplestore.eval_function()""" | ||
import pytest | ||
|
||
from tripper import Triplestore | ||
|
||
pytest.importorskip("rdflib") | ||
|
||
|
||
def func(a, b): | ||
"""Returns the sum of `a` and `b`.""" | ||
# pylint: disable=invalid-name | ||
return a + b | ||
|
||
|
||
ts = Triplestore(backend="rdflib") | ||
EX = ts.bind("ex", "http://example.com/ex#") | ||
|
||
# Test to add function in current scope | ||
iri = ts.add_function( | ||
func, | ||
expects=[EX.arg1, EX.arg2], | ||
returns=EX.sum, | ||
standard="emmo", | ||
) | ||
assert ts.eval_function(func_iri=iri, args=(2, 3)) == 5 | ||
|
||
|
||
# Test to add a function from the standard library. The hashlib module | ||
# is not expected to be imported in the current scope | ||
iri2 = ts.add_function( | ||
EX.shape256, | ||
expects=[EX.Bytes], | ||
returns=EX.ShakeVar, | ||
func_name="shake_256", | ||
module_name="hashlib", | ||
) | ||
shakevar = ts.eval_function(iri2, (b"a",)) | ||
assert shakevar.hexdigest(4) == "867e2cb0" | ||
|
||
|
||
# Test to add a function from a pypi package. | ||
iri3 = ts.add_function( | ||
EX.UFloat, | ||
expects=[EX.Uncertainty], | ||
returns=EX.UUID, | ||
func_name="ufloat", | ||
module_name="uncertainties", | ||
# package_name="uncertainties", | ||
pypi_package_name="uncertainties==3.1.7", | ||
) | ||
val = ts.eval_function(iri3, args=(1, 0.1)) | ||
assert val.n == 1 | ||
assert val.s == 0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.