Example #1
0
def combine_sequence_learners(
    learners: List[adaptive.SequenceLearner],
    big_learner: Optional[adaptive.SequenceLearner] = None,
) -> adaptive.SequenceLearner:
    r"""Combine several `~adaptive.SequenceLearner`\s into a single
    `~adaptive.SequenceLearner` any copy over the data.

    Assumes that all ``learners`` take the same function.

    Parameters
    ----------
    learners : List[adaptive.SequenceLearner]
        List of `~adaptive.SequenceLearner`\s.
    big_learner : Optional[adaptive.SequenceLearner]
        A learner to load, if None, a new learner will be generated.

    Returns
    -------
    adaptive.SequenceLearner
        Big `~adaptive.SequenceLearner` with data from ``learners``.
    """
    if big_learner is None:
        big_sequence = sum((list(learner.sequence) for learner in learners),
                           [])
        big_learner = adaptive.SequenceLearner(learners[0]._original_function,
                                               sequence=big_sequence)

    cnt = 0
    for learner in learners:
        for i, key in enumerate(learner.sequence):
            if i in learner.data:
                x = cnt, key
                y = learner.data[i]
                big_learner.tell(x, y)
            cnt += 1
    return big_learner
Example #2
0
def split_sequence_in_sequence_learners(
    function: Callable[[Any], Any],
    sequence: Sequence[Any],
    n_learners: int,
    folder: Union[str, Path] = "",
) -> Tuple[List[adaptive.SequenceLearner], List[str]]:
    r"""Split a sequenceinto `adaptive.SequenceLearner`\s and fnames.

    Parameters
    ----------
    function : callable
        Function for `adaptive.SequenceLearner`\s.
    sequence : sequence
        The sequence to split into ``n_learners``.
    n_learners : int
        Total number of `~adaptive.SequenceLearner`\s.
    folder : pathlib.Path or str
        Folder to prepend to fnames.

    Returns
    -------
    new_learners : List[adaptive.SequenceLearner]
        List of `~adaptive.SequenceLearner`\s.
    new_fnames : List[Path]
        List of str based on a hash of the sequence.
    """
    folder = Path(folder)
    new_learners = []
    new_fnames = []
    for sequence_part in split(sequence, n_learners):
        learner = adaptive.SequenceLearner(function, sequence_part)
        new_learners.append(learner)
        hsh = hash_anything((sequence_part[0], len(sequence_part)))
        fname = folder / f"{hsh}.pickle"
        new_fnames.append(str(fname))
    return new_learners, new_fnames