Skip to content

adaptive.SequenceLearner

SequenceLearner

SequenceLearner(function: Callable[[Any], Any], sequence: Sequence[Any])

Bases: BaseLearner

A learner that will learn a sequence. It simply returns the points in the provided sequence when asked.

This is useful when your problem cannot be formulated in terms of another adaptive learner, but you still want to use Adaptive's routines to run, save, and plot.

Parameters:

Name Type Description Default
function callable

The function to learn. Must take a single element sequence.

required
sequence sequence

The sequence to learn.

required

Attributes:

Name Type Description
data dict

The data as a mapping from "index of element in sequence" => value.

Notes

From primitive tests, the SequenceLearner appears to have a similar performance to ipyparallel\s load_balanced_view().map. With the added benefit of having results in the local kernel already.

Source code in adaptive/learner/sequence_learner.py
def __init__(
    self,
    function: Callable[[Any], Any],
    sequence: Sequence[Any],
):
    self._original_function = function
    self.function = _IgnoreFirstArgument(function)
    # prefer range(len(...)) over enumerate to avoid slowdowns
    # when passing lazy sequences
    indices = range(len(sequence))
    self._to_do_indices = SortedSet(indices)
    self._ntotal = len(sequence)
    self.sequence = copy(sequence)
    self.data = SortedDict()
    self.pending_points = set()

function instance-attribute

function = _IgnoreFirstArgument(function)

sequence instance-attribute

sequence = copy(sequence)

data instance-attribute

data = SortedDict()

pending_points instance-attribute

pending_points = set()

npoints property

npoints: int

new

new() -> SequenceLearner

Return a new SequenceLearner without the data.

Source code in adaptive/learner/sequence_learner.py
def new(self) -> SequenceLearner:
    """Return a new `~adaptive.SequenceLearner` without the data."""
    return SequenceLearner(self._original_function, self.sequence)

ask

ask(n: int, tell_pending: bool = True) -> tuple[list[PointType], list[float]]
Source code in adaptive/learner/sequence_learner.py
def ask(
    self, n: int, tell_pending: bool = True
) -> tuple[list[PointType], list[float]]:
    indices = []
    points: list[PointType] = []
    loss_improvements = []
    for index in self._to_do_indices:
        if len(points) >= n:
            break
        point = self.sequence[index]
        indices.append(index)
        points.append((index, point))
        loss_improvements.append(1 / self._ntotal)

    if tell_pending:
        for i, p in zip(indices, points):
            self.tell_pending((i, p))

    return points, loss_improvements

loss

loss(real: bool = True) -> float
Source code in adaptive/learner/sequence_learner.py
@cache_latest
def loss(self, real: bool = True) -> float:
    if not (self._to_do_indices or self.pending_points):
        return 0.0
    else:
        npoints = self.npoints + (0 if real else len(self.pending_points))
        return (self._ntotal - npoints) / self._ntotal

remove_unfinished

remove_unfinished() -> None
Source code in adaptive/learner/sequence_learner.py
def remove_unfinished(self) -> None:
    for i in self.pending_points:
        self._to_do_indices.add(i)
    self.pending_points = set()

tell

tell(point: PointType, value: Any) -> None
Source code in adaptive/learner/sequence_learner.py
def tell(self, point: PointType, value: Any) -> None:
    index, _ = point
    self.data[index] = value
    self.pending_points.discard(index)
    self._to_do_indices.discard(index)

tell_pending

tell_pending(point: PointType) -> None
Source code in adaptive/learner/sequence_learner.py
def tell_pending(self, point: PointType) -> None:
    index, _ = point
    self.pending_points.add(index)
    self._to_do_indices.discard(index)

done

done() -> bool
Source code in adaptive/learner/sequence_learner.py
def done(self) -> bool:
    return not self._to_do_indices and not self.pending_points

result

result() -> list[Any]

Get the function values in the same order as sequence.

Source code in adaptive/learner/sequence_learner.py
def result(self) -> list[Any]:
    """Get the function values in the same order as ``sequence``."""
    if not self.done():
        raise Exception("Learner is not yet complete.")
    return list(self.data.values())

to_dataframe

to_dataframe(with_default_function_args: bool = True, function_prefix: str = 'function.', index_name: str = 'i', x_name: str = 'x', y_name: str = 'y', *, full_sequence: bool = False) -> DataFrame

Return the data as a pandas.DataFrame.

Parameters:

Name Type Description Default
with_default_function_args bool

Include the learner.function's default arguments as a column, by default True

True
function_prefix str

Prefix to the learner.function's default arguments' names, by default "function."

'function.'
index_name str

Name of the index parameter, by default "i"

'i'
x_name str

Name of the input value, by default "x"

'x'
y_name str

Name of the output value, by default "y"

'y'
full_sequence bool

If True, the returned dataframe will have the full sequence where the y_name values are pd.NA if not evaluated yet.

False

Returns:

Type Description
DataFrame

Raises:

Type Description
ImportError

If pandas is not installed.

Source code in adaptive/learner/sequence_learner.py
def to_dataframe(  # type: ignore[override]
    self,
    with_default_function_args: bool = True,
    function_prefix: str = "function.",
    index_name: str = "i",
    x_name: str = "x",
    y_name: str = "y",
    *,
    full_sequence: bool = False,
) -> pandas.DataFrame:
    """Return the data as a `pandas.DataFrame`.

    Parameters
    ----------
    with_default_function_args : bool, optional
        Include the ``learner.function``'s default arguments as a
        column, by default True
    function_prefix : str, optional
        Prefix to the ``learner.function``'s default arguments' names,
        by default "function."
    index_name : str, optional
        Name of the index parameter, by default "i"
    x_name : str, optional
        Name of the input value, by default "x"
    y_name : str, optional
        Name of the output value, by default "y"
    full_sequence : bool, optional
        If True, the returned dataframe will have the full sequence
        where the y_name values are pd.NA if not evaluated yet.

    Returns
    -------
    pandas.DataFrame

    Raises
    ------
    ImportError
        If `pandas` is not installed.
    """
    if not with_pandas:
        raise ImportError("pandas is not installed.")
    import pandas as pd

    if full_sequence:
        indices = list(range(len(self.sequence)))
        sequence = list(self.sequence)
        ys = [self.data.get(i, pd.NA) for i in indices]
    else:
        indices, ys = zip(*self.data.items()) if self.data else ([], [])  # type: ignore[assignment]
        sequence = [self.sequence[i] for i in indices]

    df = pandas.DataFrame(indices, columns=[index_name])
    df[x_name] = sequence
    df[y_name] = ys
    df.attrs["inputs"] = [index_name]
    df.attrs["output"] = y_name
    if with_default_function_args:
        assign_defaults(self._original_function, df, function_prefix)
    return df

load_dataframe

load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', index_name: str = 'i', x_name: str = 'x', y_name: str = 'y', *, full_sequence: bool = False)

Load data from a pandas.DataFrame.

If with_default_function_args is True, then learner.function's default arguments are set (using functools.partial) from the values in the pandas.DataFrame.

Parameters:

Name Type Description Default
df DataFrame

The data to load.

required
with_default_function_args bool

The with_default_function_args used in to_dataframe(), by default True

True
function_prefix str

The function_prefix used in to_dataframe, by default "function."

'function.'
index_name str

The index_name used in to_dataframe, by default "i"

'i'
x_name str

The x_name used in to_dataframe, by default "x"

'x'
y_name str

The y_name used in to_dataframe, by default "y"

'y'
full_sequence bool

The full_sequence used in to_dataframe, by default False

False
Source code in adaptive/learner/sequence_learner.py
def load_dataframe(  # type: ignore[override]
    self,
    df: pandas.DataFrame,
    with_default_function_args: bool = True,
    function_prefix: str = "function.",
    index_name: str = "i",
    x_name: str = "x",
    y_name: str = "y",
    *,
    full_sequence: bool = False,
):
    """Load data from a `pandas.DataFrame`.

    If ``with_default_function_args`` is True, then ``learner.function``'s
    default arguments are set (using `functools.partial`) from the values
    in the `pandas.DataFrame`.

    Parameters
    ----------
    df : pandas.DataFrame
        The data to load.
    with_default_function_args : bool, optional
        The ``with_default_function_args`` used in ``to_dataframe()``,
        by default True
    function_prefix : str, optional
        The ``function_prefix`` used in ``to_dataframe``, by default "function."
    index_name : str, optional
        The ``index_name`` used in ``to_dataframe``, by default "i"
    x_name : str, optional
        The ``x_name`` used in ``to_dataframe``, by default "x"
    y_name : str, optional
        The ``y_name`` used in ``to_dataframe``, by default "y"
    full_sequence : bool, optional
        The ``full_sequence`` used in ``to_dataframe``, by default False
    """
    if not with_pandas:
        raise ImportError("pandas is not installed.")
    import pandas as pd

    indices = df[index_name].values
    xs = df[x_name].values
    ys = df[y_name].values

    if full_sequence:
        evaluated_indices = [i for i, y in enumerate(ys) if y is not pd.NA]
        xs = xs[evaluated_indices]
        ys = ys[evaluated_indices]
        indices = indices[evaluated_indices]

    self.tell_many(zip(indices, xs), ys)

    if with_default_function_args:
        self.function = partial_function_from_dataframe(
            self._original_function, df, function_prefix
        )