-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3495073
commit f74189e
Showing
1 changed file
with
49 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,55 @@ | ||
from typing import Callable | ||
|
||
from numpy import ndarray | ||
from torch import Tensor | ||
|
||
from acegen.scoring_functions.base import Task | ||
from acegen.scoring_functions.chemistry import QED | ||
|
||
custom_scoring_functions = { | ||
"QED": QED, | ||
} | ||
|
||
|
||
def check_scoring_function(scoring_function): | ||
"""Check if the scoring function is a valid scoring function.""" | ||
# Check it is a callable | ||
if not isinstance(scoring_function, Callable): | ||
raise ValueError( | ||
f"scoring_function must be a callable, got {type(scoring_function)}" | ||
) | ||
|
||
# Check it accepts a single smiles and returns typing number of a tensor | ||
if not isinstance(scoring_function("CCO"), (int, float, list, Tensor, ndarray)): | ||
raise ValueError( | ||
f"scoring_function must return a number, got {type(scoring_function('CCO'))}" | ||
) | ||
|
||
# Check it accepts a single smiles and returns a list of number or a tensor | ||
scores = scoring_function(["CCO", "CCC"]) | ||
if not isinstance(scores, (list, Tensor, ndarray)): | ||
raise ValueError( | ||
f"scoring_function must return a list of number, got {type(scoring_function(['CCO', 'CCC']))}" | ||
) | ||
|
||
# If scores is a list, check that each element is a number | ||
if isinstance(scores, list): | ||
for score in scores: | ||
if not isinstance(score, (int, float)): | ||
raise ValueError( | ||
f"scoring_function must return a list of number, got {type(scoring_function(['CCO', 'CCC']))}" | ||
) | ||
|
||
|
||
def register_custom_scoring_function(name, scoring_function): | ||
"""Register a custom scoring function. | ||
Example: | ||
>>> from acegen import register_custom_scoring_function, custom_scoring_functions | ||
>>> from my_module import my_scoring_function | ||
>>> register_custom_scoring_function("my_scoring_function", my_scoring_function) | ||
>>> custom_scoring_functions["my_scoring_function"] | ||
""" | ||
check_scoring_function(scoring_function) | ||
custom_scoring_functions[name] = scoring_function | ||
|