Similarity
The similarity
submodule allows to create custom Similarity calculations
for comparison of single terms or term-sets.
It provides a simple interface to register custom Similarity handler, so
that they can be called directly on an pyhpo.term.HPOTerm
or an
pyhpo.set.HPOSet
.
SimilarityBase
- class pyhpo.similarity.base.SimilarityBase[source]
Base class to use for custom similarity calculations.
Custom implementation must inherit from
pyhpo.similarity.base.SimilarityBase
and provide a__call__
method with the same signature aspyhpo.similarity.base.SimilarityBase.__call__()
You can also provide a list of
dependencies
of other similarity methods that should be called beforehand. Results of these calls will be passed asdependencies
parameter to the__call__
method.
__call__
- SimilarityBase.__call__(term1, term2, kind, dependencies)[source]
This method does the actual calculation of the similarity. This method must be provided in all custom similarity classes
- Parameters:
term1 (
HPOTerm
) – One of the two terms to compareterm2 (
HPOTerm
) – The other of the two terms to comparekind (
str
) – This can be an extra parameter, ususallyomim
orgene
to specify which annotations to consider for similaritydepdencies – A list of other calculation-results that should be calculated beforehand. This is not needed at all, but helpful if your implementation builds on an already existing similarity calculation.
- Return type:
float
Examples
from pyhpo.similarity.base import SimScore, SimilarityBase
from pyhpo import Ontology
class CustomSimscore(SimilarityBase):
# For demo purposes, we will just check for equality
def __call__(
self,
term1: 'pyhpo.HPOTerm',
term2: 'pyhpo.HPOTerm',
kind: str,
dependencies: List[float]
) -> float:
if term1 == term2:
return 1
else:
return 0
SimScore.register('custom_method', CustomSimscore)
_ = Ontology()
term1 = Ontology.get_hpo_object('Scoliosis')
term2 = Ontology.get_hpo_object('Thoracic scoliosis')
sim_score = term1.similarity_score(
other=term2,
kind='omim', # actually doesn't matter in this example
method='custom_method'
)
assert sim_score == 0
# Now comparing the same term to each other
sim_score = term1.similarity_score(
other=term1,
kind='omim', # actually doesn't matter in this example
method='custom_method'
)
assert sim_score == 1