Skip to content

Commit

Permalink
Make compatible with numpy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w-feldmann authored Aug 9, 2024
1 parent cc98b76 commit c539825
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 92 deletions.
1 change: 0 additions & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install "numpy<2.0.0"
pip install mypy
mypy . || exit_code=$?
mypy --install-types --non-interactive
Expand Down
32 changes: 17 additions & 15 deletions molpipeline/abstract_pipeline_elements/mol2any/mol2floatvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ def n_features(self) -> int:

def assemble_output(
self,
value_list: Iterable[npt.NDArray[np.float_]],
) -> npt.NDArray[np.float_]:
value_list: Iterable[npt.NDArray[np.float64]],
) -> npt.NDArray[np.float64]:
"""Transform output of all transform_single operations to matrix.
Parameters
----------
value_list: Iterable[npt.NDArray[np.float_]]
value_list: Iterable[npt.NDArray[np.float64]]
List of numpy arrays with calculated descriptor values of each molecule.
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
Matrix with descriptor values of each molecule.
"""
return np.vstack(list(value_list))
Expand Down Expand Up @@ -127,7 +127,7 @@ def set_params(self, **parameters: dict[str, Any]) -> Self:
super().set_params(**parameter_copy)
return self

def fit_to_result(self, values: list[npt.NDArray[np.float_]]) -> Self:
def fit_to_result(self, values: list[npt.NDArray[np.float64]]) -> Self:
"""Fit object to data.
Parameters
Expand All @@ -146,25 +146,25 @@ def fit_to_result(self, values: list[npt.NDArray[np.float_]]) -> Self:
return self

def _normalize_matrix(
self, value_matrix: npt.NDArray[np.float_]
) -> npt.NDArray[np.float_]:
self, value_matrix: npt.NDArray[np.float64]
) -> npt.NDArray[np.float64]:
"""Normalize matrix with descriptor values.
Parameters
----------
value_matrix: npt.NDArray[np.float_]
value_matrix: npt.NDArray[np.float64]
Matrix with descriptor values of molecules.
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
Normalized matrix with descriptor values of molecules.
"""
if self._standardizer is not None:
return self._standardizer.transform(value_matrix)
return value_matrix

def transform(self, values: list[RDKitMol]) -> npt.NDArray[np.float_]:
def transform(self, values: list[RDKitMol]) -> npt.NDArray[np.float64]:
"""Transform the list of molecules to sparse matrix.
Parameters
Expand All @@ -174,13 +174,15 @@ def transform(self, values: list[RDKitMol]) -> npt.NDArray[np.float_]:
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
Matrix with descriptor values of molecules.
"""
descriptor_matrix: npt.NDArray[np.float_] = super().transform(values)
descriptor_matrix: npt.NDArray[np.float64] = super().transform(values)
return descriptor_matrix

def finalize_single(self, value: npt.NDArray[np.float_]) -> npt.NDArray[np.float_]:
def finalize_single(
self, value: npt.NDArray[np.float64]
) -> npt.NDArray[np.float64]:
"""Finalize single value. Here: standardize vector.
Parameters
Expand All @@ -201,7 +203,7 @@ def finalize_single(self, value: npt.NDArray[np.float_]) -> npt.NDArray[np.float
@abc.abstractmethod
def pretransform_single(
self, value: RDKitMol
) -> Union[npt.NDArray[np.float_], InvalidInstance]:
) -> Union[npt.NDArray[np.float64], InvalidInstance]:
"""Transform mol to dict, where items encode columns indices and values, respectively.
Parameters
Expand All @@ -211,6 +213,6 @@ def pretransform_single(
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
Vector with descriptor values of molecule.
"""
4 changes: 2 additions & 2 deletions molpipeline/estimators/chemprop/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ def _update_trainer(
def fit(
self,
X: MoleculeDataset, # pylint: disable=invalid-name
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float_],
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float64],
) -> Self:
"""Fit the model to the data.
Parameters
----------
X : MoleculeDataset
The input data.
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float_]
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float64]
The target data.
Returns
Expand Down
16 changes: 8 additions & 8 deletions molpipeline/estimators/chemprop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _is_classifier(self) -> bool:

def _predict(
self, X: MoleculeDataset # pylint: disable=invalid-name
) -> npt.NDArray[np.float_]:
) -> npt.NDArray[np.float64]:
"""Predict the labels.
Parameters
Expand All @@ -138,7 +138,7 @@ def _predict(
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
The predictions for the input data.
"""
self.model.eval()
Expand All @@ -164,15 +164,15 @@ def _predict(
def fit(
self,
X: MoleculeDataset,
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float_],
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float64],
) -> Self:
"""Fit the model to the data.
Parameters
----------
X : MoleculeDataset
The input data.
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float_]
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float64]
The target data.
Returns
Expand All @@ -186,7 +186,7 @@ def fit(

def predict(
self, X: MoleculeDataset # pylint: disable=invalid-name
) -> npt.NDArray[np.float_]:
) -> npt.NDArray[np.float64]:
"""Predict the output.
Parameters
Expand All @@ -196,7 +196,7 @@ def predict(
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
The predictions for the input data.
"""
predictions = self._predict(X)
Expand All @@ -213,7 +213,7 @@ def predict(
@available_if(_is_classifier)
def predict_proba(
self, X: MoleculeDataset # pylint: disable=invalid-name
) -> npt.NDArray[np.float_]:
) -> npt.NDArray[np.float64]:
"""Predict the probabilities.
Parameters
Expand All @@ -223,7 +223,7 @@ def predict_proba(
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
The probabilities of the input data.
"""
if self._is_binary_classifier():
Expand Down
16 changes: 8 additions & 8 deletions molpipeline/estimators/chemprop/neural_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ def __init__(
def fit(
self,
X: MoleculeDataset, # pylint: disable=invalid-name
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float_],
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float64],
) -> Self:
"""Fit the model.
Parameters
----------
X : MoleculeDataset
The input data.
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float_]
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float64]
The target data.
Returns
Expand All @@ -75,7 +75,7 @@ def fit(

def transform(
self, X: MoleculeDataset # pylint: disable=invalid-name
) -> npt.NDArray[np.float_]:
) -> npt.NDArray[np.float64]:
"""Transform the input.
Parameters
Expand All @@ -85,7 +85,7 @@ def transform(
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
The neural fingerprint of the input data.
"""
self.model.eval()
Expand All @@ -95,20 +95,20 @@ def transform(
def fit_transform(
self,
X: MoleculeDataset, # pylint: disable=invalid-name
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float_],
) -> npt.NDArray[np.float_]:
y: Sequence[int | float] | npt.NDArray[np.int_ | np.float64],
) -> npt.NDArray[np.float64]:
"""Fit the model and transform the input.
Parameters
----------
X : MoleculeDataset
The input data.
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float_]
y : Sequence[int | float] | npt.NDArray[np.int_ | np.float64]
The target data.
Returns
-------
npt.NDArray[np.float_]
npt.NDArray[np.float64]
The neural fingerprint of the input data.
"""
self.fit(X, y)
Expand Down
6 changes: 3 additions & 3 deletions molpipeline/estimators/nearest_neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

AllMetrics = Union[
SklearnNativeMetrics,
Callable[[Any, Any], float | npt.NDArray[np.float_] | Sequence[float]],
Callable[[Any, Any], float | npt.NDArray[np.float64] | Sequence[float]],
]


Expand Down Expand Up @@ -153,7 +153,7 @@ def predict(
Returns
-------
tuple[npt.NDArray[Any], npt.NDArray[np.float_]] | npt.NDArray[Any]
tuple[npt.NDArray[Any], npt.NDArray[np.float64]] | npt.NDArray[Any]
The indices of the nearest points in the population matrix and the distances to the points.
"""
if self.learned_names_ is None:
Expand All @@ -179,7 +179,7 @@ def fit_predict(
y: Sequence[Any],
return_distance: bool = False,
n_neighbors: int | None = None,
) -> tuple[npt.NDArray[Any], npt.NDArray[np.float_]] | npt.NDArray[Any]:
) -> tuple[npt.NDArray[Any], npt.NDArray[np.float64]] | npt.NDArray[Any]:
"""Find the k-neighbors of a point.
Parameters
Expand Down
Loading

0 comments on commit c539825

Please sign in to comment.