From 5b52a3b004ef2c8b3910ed350636d70c1f2e39f4 Mon Sep 17 00:00:00 2001 From: frederik-sandfort1 <129401811+frederik-sandfort1@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:44:54 +0200 Subject: [PATCH] Init refactoring (#70) * remove unnecessary inits and refactor * Fix wrong typing that caused thousands of type ignores * linting * set correct typing --- .../abstract_pipeline_elements/core.py | 75 +------- .../mol2any/mol2bitvector.py | 16 +- .../mol2any/mol2floatvector.py | 6 +- molpipeline/any2mol/bin2mol.py | 21 --- molpipeline/any2mol/sdf2mol.py | 6 +- molpipeline/any2mol/smiles2mol.py | 21 --- molpipeline/error_handling.py | 8 +- molpipeline/mol2any/mol2bin.py | 25 --- molpipeline/mol2any/mol2bool.py | 25 +-- .../mol2any/mol2concatinated_vector.py | 6 +- molpipeline/mol2any/mol2inchi.py | 23 --- .../mol2any/mol2maccs_key_fingerprint.py | 29 --- molpipeline/mol2any/mol2morgan_fingerprint.py | 6 +- molpipeline/mol2any/mol2net_charge.py | 6 +- molpipeline/mol2any/mol2path_fingerprint.py | 22 +-- molpipeline/mol2any/mol2rdkit_phys_chem.py | 4 +- molpipeline/mol2any/mol2smiles.py | 25 --- molpipeline/mol2mol/filter.py | 6 +- molpipeline/mol2mol/reaction.py | 8 +- molpipeline/mol2mol/scaffolds.py | 48 ----- molpipeline/mol2mol/standardization.py | 177 +----------------- molpipeline/pipeline/_molpipeline.py | 8 +- tests/utils/mock_element.py | 4 +- 23 files changed, 67 insertions(+), 508 deletions(-) diff --git a/molpipeline/abstract_pipeline_elements/core.py b/molpipeline/abstract_pipeline_elements/core.py index 7fe430a9..276a9fa5 100644 --- a/molpipeline/abstract_pipeline_elements/core.py +++ b/molpipeline/abstract_pipeline_elements/core.py @@ -97,7 +97,7 @@ class ABCPipelineElement(abc.ABC): def __init__( self, - name: str = "ABCPipelineElement", + name: Optional[str] = None, n_jobs: int = 1, uuid: Optional[str] = None, ) -> None: @@ -105,13 +105,15 @@ def __init__( Parameters ---------- - name: str + name: Optional[str], optional (default=None) Name of PipelineElement n_jobs: int Number of cores used for processing. uuid: Optional[str] Unique identifier of the PipelineElement. """ + if name is None: + name = self.__class__.__name__ self.name = name self.n_jobs = n_jobs if uuid is None: @@ -182,12 +184,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: "uuid": self.uuid, } - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """As the setter function cannot be assessed with super(), this method is implemented for inheritance. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameters to be set. Returns @@ -338,7 +340,7 @@ class TransformingPipelineElement(ABCPipelineElement): def __init__( self, - name: str = "ABCPipelineElement", + name: Optional[str] = None, n_jobs: int = 1, uuid: Optional[str] = None, ) -> None: @@ -346,7 +348,7 @@ def __init__( Parameters ---------- - name: str + name: Optional[str], optional (default=None) Name of PipelineElement n_jobs: int Number of cores used for processing. @@ -377,12 +379,12 @@ def parameters(self) -> dict[str, Any]: return self.get_params() @parameters.setter - def parameters(self, **parameters: dict[str, Any]) -> None: + def parameters(self, **parameters: Any) -> None: """Set the parameters of the object. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Object parameters as a dictionary. Returns @@ -616,25 +618,6 @@ class MolToMolPipelineElement(TransformingPipelineElement, abc.ABC): _input_type = "RDKitMol" _output_type = "RDKitMol" - def __init__( - self, - name: str = "MolToMolPipelineElement", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MolToMolPipelineElement. - - Parameters - ---------- - name: str - Name of the PipelineElement. - n_jobs: int - Number of cores used for processing. - uuid: Optional[str] - Unique identifier of the PipelineElement. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def transform(self, values: list[OptionalMol]) -> list[OptionalMol]: """Transform list of molecules to list of molecules. @@ -700,25 +683,6 @@ class AnyToMolPipelineElement(TransformingPipelineElement, abc.ABC): _output_type = "RDKitMol" - def __init__( - self, - name: str = "AnyToMolPipelineElement", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize AnyToMolPipelineElement. - - Parameters - ---------- - name: str - Name of the PipelineElement. - n_jobs: int - Number of cores used for processing. - uuid: Optional[str] - Unique identifier of the PipelineElement. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def transform(self, values: Any) -> list[OptionalMol]: """Transform list of instances to list of molecules. @@ -756,25 +720,6 @@ class MolToAnyPipelineElement(TransformingPipelineElement, abc.ABC): _input_type = "RDKitMol" - def __init__( - self, - name: str = "MolToAnyPipelineElement", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MolToAnyPipelineElement. - - Parameters - ---------- - name: str - Name of the PipelineElement. - n_jobs: int - Number of cores used for processing. - uuid: Optional[str] - Unique identifier of the PipelineElement. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - @abc.abstractmethod def pretransform_single(self, value: RDKitMol) -> Any: """Transform the molecule, but skip parameters learned during fitting. diff --git a/molpipeline/abstract_pipeline_elements/mol2any/mol2bitvector.py b/molpipeline/abstract_pipeline_elements/mol2any/mol2bitvector.py index 66317966..7de2cbab 100644 --- a/molpipeline/abstract_pipeline_elements/mol2any/mol2bitvector.py +++ b/molpipeline/abstract_pipeline_elements/mol2any/mol2bitvector.py @@ -140,12 +140,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set object parameters relevant for copying the class. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary of parameter names and values. Returns @@ -160,7 +160,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: raise ValueError( f"return_as has to be one of {get_args(OutputDatatype)}! (Received: {return_as})" ) - self._return_as = return_as # type: ignore + self._return_as = return_as super().set_params(**parameter_dict_copy) return self @@ -300,12 +300,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set object parameters relevant for copying the class. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary of parameter names and values. Returns @@ -398,12 +398,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: parameters.pop("fill_value", None) return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary of parameter names and values. Returns @@ -417,7 +417,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: # explicitly check for None, since 0 is a valid value if radius is not None: - self._radius = radius # type: ignore + self._radius = radius # explicitly check for None, since False is a valid value if use_features is not None: self._use_features = bool(use_features) diff --git a/molpipeline/abstract_pipeline_elements/mol2any/mol2floatvector.py b/molpipeline/abstract_pipeline_elements/mol2any/mol2floatvector.py index 3f25e2a4..3c0711c3 100644 --- a/molpipeline/abstract_pipeline_elements/mol2any/mol2floatvector.py +++ b/molpipeline/abstract_pipeline_elements/mol2any/mol2floatvector.py @@ -107,12 +107,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["standardizer"] = self._standardizer return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary with parameter names and corresponding values. Returns @@ -123,7 +123,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: parameter_copy = dict(parameters) standardizer = parameter_copy.pop("standardizer", None) if standardizer is not None: - self._standardizer = standardizer # type: ignore + self._standardizer = standardizer super().set_params(**parameter_copy) return self diff --git a/molpipeline/any2mol/bin2mol.py b/molpipeline/any2mol/bin2mol.py index 9e2c94f9..90c70cbf 100644 --- a/molpipeline/any2mol/bin2mol.py +++ b/molpipeline/any2mol/bin2mol.py @@ -2,8 +2,6 @@ from __future__ import annotations -from typing import Optional - from rdkit import Chem from molpipeline.abstract_pipeline_elements.core import ( @@ -16,25 +14,6 @@ class BinaryToMol(AnyToMolPipelineElement): """Transforms binary string representation to RDKit Mol objects.""" - def __init__( - self, - name: str = "bin2mol", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize BinaryToMol. - - Parameters - ---------- - name: str, optional (default="bin2mol") - Name of PipelineElement. - n_jobs: int, optional (default=1) - Number of cores used. - uuid: str | None, optional (default=None) - UUID of the pipeline element. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: str) -> OptionalMol: """Transform binary string to molecule. diff --git a/molpipeline/any2mol/sdf2mol.py b/molpipeline/any2mol/sdf2mol.py index ded4567e..660ed413 100644 --- a/molpipeline/any2mol/sdf2mol.py +++ b/molpipeline/any2mol/sdf2mol.py @@ -71,12 +71,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["identifier"] = self.identifier return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters of the object. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary containing all parameters defining the object. Returns @@ -86,7 +86,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: """ super().set_params(**parameters) if "identifier" in parameters: - self.identifier = parameters["identifier"] # type: ignore + self.identifier = parameters["identifier"] return self def finish(self) -> None: diff --git a/molpipeline/any2mol/smiles2mol.py b/molpipeline/any2mol/smiles2mol.py index 40a0ac80..79db23bd 100644 --- a/molpipeline/any2mol/smiles2mol.py +++ b/molpipeline/any2mol/smiles2mol.py @@ -2,8 +2,6 @@ from __future__ import annotations -from typing import Optional - from rdkit import Chem from molpipeline.abstract_pipeline_elements.any2mol.string2mol import ( @@ -16,25 +14,6 @@ class SmilesToMol(_StringToMolPipelineElement): """Transforms Smiles to RDKit Mol objects.""" - def __init__( - self, - name: str = "smiles2mol", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize SmilesToMol. - - Parameters - ---------- - name: str, optional (default="smiles2mol") - Name of PipelineElement. - n_jobs: int, optional (default=1) - Number of cores used. - uuid: str | None, optional (default=None) - UUID of the pipeline element. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: str) -> OptionalMol: """Transform Smiles string to molecule. diff --git a/molpipeline/error_handling.py b/molpipeline/error_handling.py index 4ef5efa1..147ef8d7 100644 --- a/molpipeline/error_handling.py +++ b/molpipeline/error_handling.py @@ -127,12 +127,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["element_ids"] = self.element_ids return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters for this element. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dict of arameters to set. Returns @@ -508,12 +508,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["fill_value"] = self.fill_value return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters for this element. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameter dict. Returns diff --git a/molpipeline/mol2any/mol2bin.py b/molpipeline/mol2any/mol2bin.py index 61edb9d1..3f14b8b7 100644 --- a/molpipeline/mol2any/mol2bin.py +++ b/molpipeline/mol2any/mol2bin.py @@ -1,7 +1,5 @@ """Converter element for molecules to binary string representation.""" -from typing import Optional - from rdkit import Chem from molpipeline.abstract_pipeline_elements.core import MolToAnyPipelineElement @@ -10,29 +8,6 @@ class MolToBinary(MolToAnyPipelineElement): """PipelineElement to transform a molecule to a binary.""" - def __init__( - self, - name: str = "Mol2Binary", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MolToBinaryPipelineElement. - - Parameters - ---------- - name: str, optional (default="Mol2Binary") - 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: Chem.Mol) -> str: """Transform a molecule to a binary string. diff --git a/molpipeline/mol2any/mol2bool.py b/molpipeline/mol2any/mol2bool.py index e372fcb1..55d473f9 100644 --- a/molpipeline/mol2any/mol2bool.py +++ b/molpipeline/mol2any/mol2bool.py @@ -3,37 +3,14 @@ from typing import Any from molpipeline.abstract_pipeline_elements.core import ( - MolToAnyPipelineElement, InvalidInstance, + MolToAnyPipelineElement, ) class MolToBool(MolToAnyPipelineElement): """Element to generate a bool array from input.""" - def __init__( - 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. diff --git a/molpipeline/mol2any/mol2concatinated_vector.py b/molpipeline/mol2any/mol2concatinated_vector.py index a0a24406..09c2e3db 100644 --- a/molpipeline/mol2any/mol2concatinated_vector.py +++ b/molpipeline/mol2any/mol2concatinated_vector.py @@ -113,12 +113,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameters to set. Returns @@ -129,7 +129,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: parameter_copy = dict(parameters) element_list = parameter_copy.pop("element_list", None) if element_list is not None: - self._element_list = element_list # type: ignore + self._element_list = element_list step_params: dict[str, dict[str, Any]] = {} step_dict = dict(self._element_list) to_delete_list = [] diff --git a/molpipeline/mol2any/mol2inchi.py b/molpipeline/mol2any/mol2inchi.py index d0174f9a..20dcf0e9 100644 --- a/molpipeline/mol2any/mol2inchi.py +++ b/molpipeline/mol2any/mol2inchi.py @@ -15,29 +15,6 @@ class MolToInchi(_MolToStringPipelineElement): """PipelineElement to transform a molecule to an INCHI string.""" - def __init__( - self, - name: str = "Mol2Inchi", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MolToInchiPipelineElement. - - Parameters - ---------- - name: str - name of PipelineElement - n_jobs: int - number of jobs to use for parallelization - uuid: Optional[str], optional - uuid of PipelineElement, by default None - - Returns - ------- - None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> str: """Transform a molecule to a INCHI-key string. diff --git a/molpipeline/mol2any/mol2maccs_key_fingerprint.py b/molpipeline/mol2any/mol2maccs_key_fingerprint.py index 701b5ef9..9b70773e 100644 --- a/molpipeline/mol2any/mol2maccs_key_fingerprint.py +++ b/molpipeline/mol2any/mol2maccs_key_fingerprint.py @@ -1,7 +1,5 @@ """Implementation of MACCS key fingerprint.""" -from typing import Literal - import numpy as np from numpy import typing as npt from rdkit.Chem import MACCSkeys @@ -24,33 +22,6 @@ class MolToMACCSFP(MolToFingerprintPipelineElement): _n_bits = 167 # MACCS keys have 166 bits + 1 bit for an all-zero vector (bit 0) - def __init__( - self, - return_as: Literal["sparse", "dense", "explicit_bit_vect"] = "sparse", - name: str = "MolToMACCS", - n_jobs: int = 1, - uuid: str | None = None, - ) -> None: - """Initialize MolToMACCS. - - Parameters - ---------- - return_as: Literal["sparse", "dense", "explicit_bit_vect"], optional (default="sparse") - Type of output. When "sparse" the fingerprints will be returned as a - scipy.sparse.csr_matrix holding a sparse representation of the bit vectors. - With "dense" a numpy matrix will be returned. - With "explicit_bit_vect" the fingerprints will be returned as a list of RDKit's - rdkit.DataStructs.cDataStructs.ExplicitBitVect. - name: str, optional (default="MolToMACCS") - Name of PipelineElement - n_jobs: int, optional (default=1) - Number of cores to use. - uuid: str | None, optional (default=None) - UUID of the PipelineElement. - - """ - super().__init__(return_as=return_as, name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single( self, value: RDKitMol ) -> dict[int, int] | npt.NDArray[np.int_] | ExplicitBitVect: diff --git a/molpipeline/mol2any/mol2morgan_fingerprint.py b/molpipeline/mol2any/mol2morgan_fingerprint.py index 2f079e38..1c93295d 100644 --- a/molpipeline/mol2any/mol2morgan_fingerprint.py +++ b/molpipeline/mol2any/mol2morgan_fingerprint.py @@ -104,12 +104,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: parameters["n_bits"] = self._n_bits return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary of parameter names and values. Returns @@ -120,7 +120,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: parameter_copy = dict(parameters) n_bits = parameter_copy.pop("n_bits", None) if n_bits is not None: - self._n_bits = n_bits # type: ignore + self._n_bits = n_bits super().set_params(**parameter_copy) return self diff --git a/molpipeline/mol2any/mol2net_charge.py b/molpipeline/mol2any/mol2net_charge.py index 6636f48c..759ec84b 100644 --- a/molpipeline/mol2any/mol2net_charge.py +++ b/molpipeline/mol2any/mol2net_charge.py @@ -145,12 +145,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: parent_dict["charge_policy"] = self._charge_method return parent_dict - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameters to set Returns @@ -161,6 +161,6 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: parameters_shallow_copy = dict(parameters) charge_policy = parameters_shallow_copy.pop("charge_policy", None) if charge_policy is not None: - self._charge_method = charge_policy # type: ignore + self._charge_method = charge_policy super().set_params(**parameters_shallow_copy) return self diff --git a/molpipeline/mol2any/mol2path_fingerprint.py b/molpipeline/mol2any/mol2path_fingerprint.py index 368e8cb7..38e98d4a 100644 --- a/molpipeline/mol2any/mol2path_fingerprint.py +++ b/molpipeline/mol2any/mol2path_fingerprint.py @@ -152,12 +152,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: parameters["n_bits"] = self._n_bits return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary of parameter names and values. Returns @@ -168,28 +168,28 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: parameter_copy = dict(parameters) min_path = parameter_copy.pop("min_path", None) if min_path is not None: - self._min_path = min_path # type: ignore + self._min_path = min_path max_path = parameter_copy.pop("max_path", None) if max_path is not None: - self._max_path = max_path # type: ignore + self._max_path = max_path use_hs = parameter_copy.pop("use_hs", None) if use_hs is not None: - self._use_hs = use_hs # type: ignore + self._use_hs = use_hs branched_paths = parameter_copy.pop("branched_paths", None) if branched_paths is not None: - self._branched_paths = branched_paths # type: ignore + self._branched_paths = branched_paths use_bond_order = parameter_copy.pop("use_bond_order", None) if use_bond_order is not None: - self._use_bond_order = use_bond_order # type: ignore + self._use_bond_order = use_bond_order count_simulation = parameter_copy.pop("count_simulation", None) if count_simulation is not None: - self._count_simulation = count_simulation # type: ignore + self._count_simulation = count_simulation count_bounds = parameter_copy.pop("count_bounds", None) if count_bounds is not None: - self._count_bounds = count_bounds # type: ignore + self._count_bounds = count_bounds num_bits_per_feature = parameter_copy.pop("num_bits_per_feature", None) if num_bits_per_feature is not None: - self._num_bits_per_feature = num_bits_per_feature # type: ignore + self._num_bits_per_feature = num_bits_per_feature atom_invariants_generator = parameter_copy.pop( "atom_invariants_generator", None ) @@ -197,7 +197,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: self._atom_invariants_generator = atom_invariants_generator n_bits = parameter_copy.pop("n_bits", None) # pylint: disable=duplicate-code if n_bits is not None: - self._n_bits = n_bits # type: ignore + self._n_bits = n_bits super().set_params(**parameter_copy) return self diff --git a/molpipeline/mol2any/mol2rdkit_phys_chem.py b/molpipeline/mol2any/mol2rdkit_phys_chem.py index 968b0d1a..251b6b3c 100644 --- a/molpipeline/mol2any/mol2rdkit_phys_chem.py +++ b/molpipeline/mol2any/mol2rdkit_phys_chem.py @@ -170,12 +170,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: parent_dict["log_exceptions"] = self._log_exceptions return parent_dict - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameters to set Returns diff --git a/molpipeline/mol2any/mol2smiles.py b/molpipeline/mol2any/mol2smiles.py index 3638e711..756beb0e 100644 --- a/molpipeline/mol2any/mol2smiles.py +++ b/molpipeline/mol2any/mol2smiles.py @@ -2,8 +2,6 @@ from __future__ import annotations -from typing import Optional - from rdkit import Chem from molpipeline.abstract_pipeline_elements.mol2any.mol2string import ( @@ -14,29 +12,6 @@ class MolToSmiles(_MolToStringPipelineElement): """PipelineElement to transform a molecule to a SMILES string.""" - def __init__( - self, - name: str = "Mol2Smiles", - n_jobs: int = 1, - uuid: Optional[str] = None, - ): - """Initialize MolToSmilesPipelineElement. - - Parameters - ---------- - name: str - name of PipelineElement - n_jobs: int - number of jobs to use for parallelization - uuid: Optional[str], optional - uuid of PipelineElement, by default None - - Returns - ------- - None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: Chem.Mol) -> str: """Transform a molecule to a SMILES string. diff --git a/molpipeline/mol2mol/filter.py b/molpipeline/mol2mol/filter.py index 2b384ebf..f0b4f24a 100644 --- a/molpipeline/mol2mol/filter.py +++ b/molpipeline/mol2mol/filter.py @@ -88,12 +88,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["allowed_element_numbers"] = self.allowed_element_numbers return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters of ElementFilter. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameters to set. Returns @@ -103,7 +103,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: """ parameter_copy = dict(parameters) if "allowed_element_numbers" in parameter_copy: - self.allowed_element_numbers = parameter_copy.pop("allowed_element_numbers") # type: ignore + self.allowed_element_numbers = parameter_copy.pop("allowed_element_numbers") super().set_params(**parameter_copy) return self diff --git a/molpipeline/mol2mol/reaction.py b/molpipeline/mol2mol/reaction.py index aad5bfd7..97b13500 100644 --- a/molpipeline/mol2mol/reaction.py +++ b/molpipeline/mol2mol/reaction.py @@ -91,12 +91,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: parameters["handle_multi"] = self.handle_multi return parameters - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set the parameters. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary containing parameters to be set. Returns @@ -108,9 +108,9 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: if "reaction" in parameters: self.reaction = parameters["reaction"] if "additive_list" in parameters: - self.additive_list = parameters["additive_list"] # type: ignore + self.additive_list = parameters["additive_list"] if "handle_multi" in parameters: - self.handle_multi = parameters["handle_multi"] # type: ignore + self.handle_multi = parameters["handle_multi"] return self @property diff --git a/molpipeline/mol2mol/scaffolds.py b/molpipeline/mol2mol/scaffolds.py index 08f4674a..00cb1768 100644 --- a/molpipeline/mol2mol/scaffolds.py +++ b/molpipeline/mol2mol/scaffolds.py @@ -2,8 +2,6 @@ from __future__ import annotations -from typing import Optional - from rdkit.Chem.Scaffolds import MurckoScaffold as RDKIT_MurckoScaffold from molpipeline.abstract_pipeline_elements.core import ( @@ -18,29 +16,6 @@ class MurckoScaffold(_MolToMolPipelineElement): The Murcko-scaffold is composed of all rings and the linker atoms between them. """ - def __init__( - self, - name: str = "MurckoScaffold", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MurckoScaffold. - - Parameters - ---------- - name: str - Name of pipeline element. - n_jobs: int - Number of jobs to use for parallelization. - uuid: Optional[str] - UUID of pipeline element. - - Returns - ------- - None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Extract Murco-scaffold of molecule. @@ -63,29 +38,6 @@ class MakeScaffoldGeneric(_MolToMolPipelineElement): Done to make scaffolds less speciffic. """ - def __init__( - self, - name: str = "MakeScaffoldGeneric", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MakeScaffoldGeneric. - - Parameters - ---------- - name: str - Name of pipeline element. - n_jobs: int - Number of jobs to use for parallelization. - uuid: Optional[str] - UUID of pipeline element. - - Returns - ------- - None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Set all atoms to carbon and all bonds to single bond and return mol object. diff --git a/molpipeline/mol2mol/standardization.py b/molpipeline/mol2mol/standardization.py index 336e6231..bda92773 100644 --- a/molpipeline/mol2mol/standardization.py +++ b/molpipeline/mol2mol/standardization.py @@ -46,25 +46,6 @@ class TautomerCanonicalizer(_MolToMolPipelineElement): """MolToMolPipelineElement which canonicalizes tautomers of a molecule.""" - def __init__( - self, - name: str = "TautomerCanonicalizer", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize TautomerCanonicalizer. - - Parameters - ---------- - name: str, optional (default="TautomerCanonicalizer") - 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. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Canonicalize tautomers of molecule. @@ -93,25 +74,6 @@ class ChargeParentExtractor(_MolToMolPipelineElement): The charge-parent is the largest fragment after neutralization. """ - def __init__( - self, - name: str = "ChargeParentExtractor", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize ChargeParentExtractor. - - Parameters - ---------- - name: str - Name of PipelineElement - n_jobs: int - Number of jobs to use for parallelization - uuid: Optional[str], optional - uuid of PipelineElement, by default None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Return charge-parent of molecule, which is the largest fragment after neutralization. @@ -200,25 +162,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class LargestFragmentChooser(_MolToMolPipelineElement): """MolToMolPipelineElement which returns the largest fragment of a molecule.""" - def __init__( - self, - name: str = "LargestFragmentChooser", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize LargestFragmentChooser. - - Parameters - ---------- - name: str, optional (default="LargestFragmentChooser") - 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. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Return largest fragment of molecule. @@ -238,25 +181,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class MetalDisconnector(_MolToMolPipelineElement): """MolToMolPipelineElement which removes bonds between organic compounds and metals.""" - def __init__( - self, - name: str = "MetalDisconnector", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize MetalDisconnector. - - Parameters - ---------- - name: str - Name of PipelineElement - n_jobs: int - Number of jobs to use for parallelization - uuid: Optional[str], optional - uuid of PipelineElement, by default None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Cleave bonds with metals. @@ -280,25 +204,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class IsotopeRemover(_MolToMolPipelineElement): """MolToMolPipelineElement which removes isotope information of atoms in a molecule.""" - def __init__( - self, - name: str = "IsotopeRemover", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize IsotopeRemover. - - Parameters - ---------- - name: str, optional (default="IsotopeRemover") - 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. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Remove isotope information of each atom. @@ -323,25 +228,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class ExplicitHydrogenRemover(_MolToMolPipelineElement): """MolToMolPipelineElement which removes explicit hydrogen atoms from a molecule.""" - def __init__( - self, - name: str = "ExplicitHydrogenRemover", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize ExplicitHydrogenRemover. - - Parameters - ---------- - name: str, optional (default="ExplicitHydrogenRemover") - 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. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Remove explicit hydrogen atoms. @@ -361,25 +247,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class StereoRemover(_MolToMolPipelineElement): """MolToMolPipelineElement which removes stereo-information from the molecule.""" - def __init__( - self, - name: str = "StereoRemover", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize StereoRemover. - - Parameters - ---------- - name: str - Name of PipelineElement - n_jobs: int - Number of jobs to use for parallelization - uuid: Optional[str], optional - uuid of PipelineElement, by default None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Remove stereo-information in molecule. @@ -401,25 +268,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class SaltRemover(_MolToMolPipelineElement): """MolToMolPipelineElement which removes metal ions from molecule.""" - def __init__( - self, - name: str = "SaltRemover", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize SaltRemover. - - Parameters - ---------- - name: str - Name of PipelineElement - n_jobs: int - Number of jobs to use for parallelization - uuid: Optional[str], optional - uuid of PipelineElement, by default None - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Remove metal ions. @@ -546,12 +394,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["solvent_smiles_list"] = self.solvent_smiles_list return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters of pipeline element. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Parameters to set. Returns @@ -562,7 +410,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self: param_copy = dict(parameters) solvent_smiles_list = param_copy.pop("solvent_smiles_list", None) if solvent_smiles_list is not None: - self.solvent_smiles_list = solvent_smiles_list # type: ignore + self.solvent_smiles_list = solvent_smiles_list super().set_params(**param_copy) return self @@ -605,25 +453,6 @@ def pretransform_single(self, value: RDKitMol) -> OptionalMol: class Uncharger(_MolToMolPipelineElement): """MolToMolPipelineElement which removes charges in a molecule, if possible.""" - def __init__( - self, - name: str = "Uncharger", - n_jobs: int = 1, - uuid: Optional[str] = None, - ) -> None: - """Initialize Uncharger. - - Parameters - ---------- - name: str, optional (default="Uncharger") - Name of PipelineElement. - n_jobs: int, optional (default=1) - Number of jobs to use for parallelization. - uuid: str | None, optional (default=None) - UUID of the pipeline element. If None, a random UUID is generated. - """ - super().__init__(name=name, n_jobs=n_jobs, uuid=uuid) - def pretransform_single(self, value: RDKitMol) -> OptionalMol: """Remove charges of molecule. diff --git a/molpipeline/pipeline/_molpipeline.py b/molpipeline/pipeline/_molpipeline.py index 2771b7e3..3ddb7c9b 100644 --- a/molpipeline/pipeline/_molpipeline.py +++ b/molpipeline/pipeline/_molpipeline.py @@ -166,12 +166,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: "raise_nones": self.raise_nones, } - def set_params(self, **parameter_dict: dict[str, Any]) -> Self: + def set_params(self, **parameter_dict: Any) -> Self: """Set parameters of the pipeline and pipeline elements. Parameters ---------- - parameter_dict: dict[str, Any] + parameter_dict: Any Dictionary containing the parameter names and corresponding values to be set. Returns @@ -180,9 +180,9 @@ def set_params(self, **parameter_dict: dict[str, Any]) -> Self: MolPipeline object with updated parameters. """ if "element_list" in parameter_dict: - self._element_list = parameter_dict["element_list"] # type: ignore + self._element_list = parameter_dict["element_list"] if "n_jobs" in parameter_dict: - self.n_jobs = int(parameter_dict["n_jobs"]) # type: ignore + self.n_jobs = int(parameter_dict["n_jobs"]) if "name" in parameter_dict: self.name = str(parameter_dict["name"]) if "raise_nones" in parameter_dict: diff --git a/tests/utils/mock_element.py b/tests/utils/mock_element.py index 8b0ba64f..4bf8eee4 100644 --- a/tests/utils/mock_element.py +++ b/tests/utils/mock_element.py @@ -73,12 +73,12 @@ def get_params(self, deep: bool = True) -> dict[str, Any]: params["return_as_numpy_array"] = self.return_as_numpy_array return params - def set_params(self, **parameters: dict[str, Any]) -> Self: + def set_params(self, **parameters: Any) -> Self: """Set parameters of the object. Parameters ---------- - parameters: dict[str, Any] + parameters: Any Dictionary containing all parameters defining the object. Returns