Skip to content

Commit

Permalink
feat: improving performance on algorithm substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Aug 30, 2021
1 parent 8fd25e4 commit 72d1376
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 13 additions & 6 deletions libkisao/python/kisao/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Utilities for working with the KiSAO ontology
""" Utilities for working with the KiSAO onto7y
:Author: Jonathan Karr <[email protected]>
:Date: 2021-04-28
Expand Down Expand Up @@ -445,6 +445,12 @@ def get_preferred_substitute_algorithm(algorithm, alt_algorithms, substitution_p
if algorithm in alt_algorithms:
return algorithm

if (
ALGORITHM_SUBSTITUTION_POLICY_LEVELS[substitution_policy]
<= ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.SAME_METHOD]
):
raise AlgorithmCannotBeSubstitutedException("Algorithms cannot be substituted at policy '{}'.".format(substitution_policy.name))

sub_algorithms = get_substitutable_algorithms_for_policy(algorithm, substitution_policy=substitution_policy)
alt_algorithm = None
for possible_alt_algorithm in alt_algorithms:
Expand Down Expand Up @@ -488,14 +494,15 @@ def get_preferred_substitute_algorithm_by_ids(algorithm, alt_algorithms,
Returns:
:obj:`str`: KiSAO id of the preferred algorithm to execute (e.g., ``KISAO_0000088``)
"""
if (
if algorithm in alt_algorithms:
return algorithm

elif (
ALGORITHM_SUBSTITUTION_POLICY_LEVELS[substitution_policy]
<= ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.SAME_METHOD]
):
if algorithm in alt_algorithms:
return algorithm
else:
raise AlgorithmCannotBeSubstitutedException("Algorithms cannot be substituted at policy '{}'.".format(substitution_policy.name))
raise AlgorithmCannotBeSubstitutedException("Algorithms cannot be substituted at policy '{}'.".format(substitution_policy.name))

else:
kisao = Kisao()
algorithm_term = kisao.get_term(algorithm)
Expand Down
3 changes: 3 additions & 0 deletions libkisao/python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ def test_get_preferred_substitute_algorithm(self):
with self.assertWarns(AlgorithmSubstitutedWarning):
self.assertEqual(utils.get_preferred_substitute_algorithm(lsoda, [cvode, lsoda_lsodar_hybrid, euler_method]),
cvode)
with self.assertRaises(AlgorithmCannotBeSubstitutedException):
self.assertEqual(utils.get_preferred_substitute_algorithm(lsoda, [cvode], substitution_policy=AlgorithmSubstitutionPolicy.SAME_METHOD),
cvode)
with self.assertRaises(AlgorithmCannotBeSubstitutedException):
utils.get_preferred_substitute_algorithm(lsoda, [])

Expand Down

0 comments on commit 72d1376

Please sign in to comment.