Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mol2any: add mol2bool element #59

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions molpipeline/mol2any/mol2bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Pipeline elements for converting instances to bool."""

from typing import Any

from molpipeline.abstract_pipeline_elements.core import (
MolToAnyPipelineElement,
InvalidInstance,
)


class MolToBool(MolToAnyPipelineElement):
"""Element to generate a bool array from input."""

def __init__(
JochenSiegWork marked this conversation as resolved.
Show resolved Hide resolved
self,
name: str = "Mol2Bool",
n_jobs: int = 1,
uuid: str | None = None,
) -> None:
"""Initialize MolToBinaryPipelineElement.

Parameters
----------
name: str, optional (default="Mol2Bool")
name of PipelineElement
n_jobs: int, optional (default=1)
number of jobs to use for parallelization
uuid: Optional[str], optional (default=None)
uuid of PipelineElement, by default None

Returns
-------
None
"""
super().__init__(name=name, n_jobs=n_jobs, uuid=uuid)

def pretransform_single(self, value: Any) -> bool:
"""Transform a value to a bool representation.

Parameters
----------
value: Any
Value to be transformed to bool representation.

Returns
-------
str
Binary representation of molecule.
"""
if isinstance(value, InvalidInstance):
return False
return True
24 changes: 24 additions & 0 deletions tests/test_elements/test_mol2any/test_mol2bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Test mol to bool conversion."""

import unittest

from molpipeline.abstract_pipeline_elements.core import InvalidInstance
from molpipeline.mol2any.mol2bool import MolToBool


class TestMolToBool(unittest.TestCase):
"""Unittest for MolToBool."""

def test_bool_conversion(self) -> None:
"""Test if the invalid instances are converted to bool."""

mol2bool = MolToBool()
result = mol2bool.transform(
frederik-sandfort1 marked this conversation as resolved.
Show resolved Hide resolved
[
1,
2,
InvalidInstance(element_id="test", message="test", element_name="Test"),
4,
]
)
self.assertEqual(result, [True, True, False, True])
Loading