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

Add type annotations #244

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
50143e5
Add type-annotations to Runner
basnijholt Oct 12, 2022
6cf01d8
Add type-hints to adaptive/_version.py
basnijholt Oct 12, 2022
1786f80
Add type-hints to adaptive/learner/__init__.py
basnijholt Oct 12, 2022
9490093
Add type-hints to adaptive/learner/balancing_learner.py
basnijholt Oct 12, 2022
6b3209f
Add type-hints to adaptive/learner/base_learner.py
basnijholt Oct 12, 2022
8363aa6
Add type-hints to adaptive/learner/data_saver.py
basnijholt Oct 12, 2022
2b0497f
Add type-hints to adaptive/learner/integrator_coeffs.py
basnijholt Oct 12, 2022
c2b6cf3
Add type-hints to adaptive/learner/integrator_learner.py
basnijholt Oct 12, 2022
3293f60
Add type-hints to adaptive/learner/learner2D.py
basnijholt Oct 12, 2022
2b5e0d1
Add type-hints to adaptive/learner/learnerND.py
basnijholt Oct 12, 2022
08ddfb2
Add type-hints to adaptive/learner/sequence_learner.py
basnijholt Oct 12, 2022
5d8110f
Add type-hints to adaptive/learner/skopt_learner.py
basnijholt Oct 12, 2022
7fd0588
Add type-hints to adaptive/learner/triangulation.py
basnijholt Oct 12, 2022
2e0efc1
Add type-hints to adaptive/notebook_integration.py
basnijholt Oct 12, 2022
96e186f
Add type-hints to adaptive/tests/algorithm_4.py
basnijholt Oct 12, 2022
ec3649a
Add type-hints to adaptive/tests/test_average_learner1d.py
basnijholt Oct 12, 2022
54265bb
Add type-hints to adaptive/tests/test_learner1d.py
basnijholt Oct 12, 2022
9e52ecb
Add type-hints to adaptive/tests/test_learners.py
basnijholt Oct 12, 2022
b4ba66e
Add type-hints to adaptive/types.py
basnijholt Oct 12, 2022
3928c3d
Add type-hints to adaptive/utils.py
basnijholt Oct 12, 2022
95cc29b
Add type-hints to setup.py
basnijholt Oct 12, 2022
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
Prev Previous commit
Next Next commit
Add type-hints to adaptive/learner/base_learner.py
basnijholt committed Oct 12, 2022
commit 6b3209f3f4bb643b38b6feef4a8cd896f6edb9a5
30 changes: 19 additions & 11 deletions adaptive/learner/base_learner.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from __future__ import annotations

import abc
from contextlib import suppress
from typing import Any, Callable

import cloudpickle

from adaptive.utils import _RequireAttrsABCMeta, load, save


def uses_nth_neighbors(n: int):
def uses_nth_neighbors(n: int) -> Callable:
"""Decorator to specify how many neighboring intervals the loss function uses.

Wraps loss functions to indicate that they expect intervals together
@@ -82,10 +85,15 @@ class BaseLearner(metaclass=_RequireAttrsABCMeta):
"""

data: dict
npoints: int
pending_points: set
function: Callable

@property
@abc.abstractmethod
def npoints(self) -> int:
"""Number of learned points."""

def tell(self, x, y):
def tell(self, x: Any, y) -> None:
"""Tell the learner about a single value.

Parameters
@@ -95,7 +103,7 @@ def tell(self, x, y):
"""
self.tell_many([x], [y])

def tell_many(self, xs, ys):
def tell_many(self, xs: Any, ys: Any) -> None:
"""Tell the learner about some values.

Parameters
@@ -116,7 +124,7 @@ def remove_unfinished(self):
"""Remove uncomputed data from the learner."""

@abc.abstractmethod
def loss(self, real=True):
def loss(self, real: bool = True) -> float:
"""Return the loss for the current state of the learner.

Parameters
@@ -128,7 +136,7 @@ def loss(self, real=True):
"""

@abc.abstractmethod
def ask(self, n, tell_pending=True):
def ask(self, n: int, tell_pending: bool = True):
"""Choose the next 'n' points to evaluate.

Parameters
@@ -146,7 +154,7 @@ def _get_data(self):
pass

@abc.abstractmethod
def _set_data(self):
def _set_data(self, data: Any):
pass

@abc.abstractmethod
@@ -164,7 +172,7 @@ def copy_from(self, other):
"""
self._set_data(other._get_data())

def save(self, fname, compress=True):
def save(self, fname: str, compress: bool = True) -> None:
"""Save the data of the learner into a pickle file.

Parameters
@@ -178,7 +186,7 @@ def save(self, fname, compress=True):
data = self._get_data()
save(fname, data, compress)

def load(self, fname, compress=True):
def load(self, fname: str, compress: bool = True) -> None:
"""Load the data of a learner from a pickle file.

Parameters
@@ -193,8 +201,8 @@ def load(self, fname, compress=True):
data = load(fname, compress)
self._set_data(data)

def __getstate__(self):
def __getstate__(self) -> dict[str, Any]:
return cloudpickle.dumps(self.__dict__)

def __setstate__(self, state):
def __setstate__(self, state: dict[str, Any]) -> None:
self.__dict__ = cloudpickle.loads(state)