Skip to content

Realize dev features #162

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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
69 changes: 2 additions & 67 deletions molpipeline/abstract_pipeline_elements/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@
import copy
import inspect
from collections.abc import Iterable
from typing import Any, NamedTuple, Union

try:
from typing import Self # type: ignore[attr-defined]
except ImportError:
from typing_extensions import Self

from typing import Any, NamedTuple, Self
from uuid import uuid4

import numpy as np
from joblib import Parallel, delayed
from loguru import logger
from rdkit import Chem
Expand Down Expand Up @@ -55,7 +48,7 @@ def __repr__(self) -> str:
)


OptionalMol = Union[RDKitMol, InvalidInstance]
OptionalMol = RDKitMol | InvalidInstance


class RemovedInstance: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -209,11 +202,6 @@ def set_params(self, **parameters: Any) -> Self:
setattr(self, att_name, att_value)
return self

@property
def additional_attributes(self) -> dict[str, Any]:
"""Any attribute relevant for recreating and exact copy, which is not a parameter."""
return {}

@property
def n_jobs(self) -> int:
"""Get the number of cores."""
Expand All @@ -236,12 +224,6 @@ def requires_fitting(self) -> bool:
"""Return whether the object requires fitting or not."""
return self._requires_fitting

def finish(self) -> None:
"""Inform object that iteration has been finished. Does in most cases nothing.

Called after all transform singles have been processed. From MolPipeline
"""

def fit(self, values: Any, labels: Any = None) -> Self:
"""Fit object to input_values.

Expand Down Expand Up @@ -391,29 +373,6 @@ def parameters(self, parameters: Any) -> None:
"""
self.set_params(**parameters)

def copy(self) -> Self:
"""Copy the object.

Raises
------
AssertionError
If the object cannot be copied.

Returns
-------
Self
Copy of the object.

"""
recreated_object = self.__class__(**self.parameters)
for key, value in self.additional_attributes.items():
if not hasattr(recreated_object, key):
raise AssertionError(
f"Cannot set attribute {key} on {self.__class__.__name__}. This should not happen!"
)
setattr(recreated_object, key, copy.copy(value))
return recreated_object

def fit_to_result(self, values: Any) -> Self:
"""Fit object to result of transformed values.

Expand Down Expand Up @@ -589,32 +548,8 @@ def transform(self, values: Any) -> Any:
output_rows = self.pretransform(values)
output_rows = self.finalize_list(output_rows)
output = self.assemble_output(output_rows)
self.finish()
return output

def to_json(self) -> dict[str, Any]:
"""Return all defining attributes of object as dict.

Returns
-------
dict[str, Any]
A dictionary with all attributes necessary to initialize a object with same parameters.
"""
json_dict: dict[str, Any] = {
"__name__": self.__class__.__name__,
"__module__": self.__class__.__module__,
}
json_dict.update(self.parameters)
if self.additional_attributes:
adittional_attributes = {}
for key, value in self.additional_attributes.items():
if isinstance(value, np.ndarray):
adittional_attributes[key] = value.tolist()
else:
adittional_attributes[key] = value
json_dict["additional_attributes"] = adittional_attributes
return json_dict


class MolToMolPipelineElement(TransformingPipelineElement, abc.ABC):
"""Abstract PipelineElement where input and outputs are molecules."""
Expand Down
Loading
Loading