<commit_before>import numpy as np
from time import sleep
import dask
from distributed import Client

from elfi.bo.gpy_model import GPyModel
from elfi.bo.acquisition import LcbAcquisition, SecondDerivativeNoiseMixin, RbfAtPendingPointsMixin
from elfi.utils import stochastic_optimization
from elfi.posteriors import BolfiPosterior
from .async import wait

"""
These are sketches of how to use the ABC graphical model in the algorithms
"""

class ABCMethod(object):
    def __init__(self, n_samples, distance_node=None, parameter_nodes=None, batch_size=10):

        if distance_node is None or parameter_nodes is None:
            raise ValueError("Need to give the distance node and list of parameter nodes")

        self.n_samples = n_samples
        self.distance_node = distance_node
        self.parameter_nodes = parameter_nodes
        self.n_params = len(parameter_nodes)
        self.batch_size = batch_size

    def infer(self, spec, *args, **kwargs):
        raise NotImplementedError

    # Run the all-accepting sampler.
    def _get_distances(self, n_samples):

        distances = self.distance_node.acquire(n_samples, batch_size=self.batch_size).compute()
        parameters = [p.acquire(n_samples, batch_size=self.batch_size).compute()
                      for p in self.parameter_nodes]

        return distances, parameters


class Rejection(ABCMethod):
    """
    Rejection sampler.
    """
    def infer(self, threshold=None, quantile=None):
        """
        Run the rejection sampler. Inference can be repeated with a different
        threshold without rerunning the simulator.
        - 1st run with threshold: run simulator and apply threshold
        - next runs with threshold: only apply threshold to existing distances
        - all runs with quantile: run simulator
        """

        n_samples = self.n_samples if quantile is None else int(self.n_samples / quantile)

        distances, parameters = self._get_distances(n_samples)
        distances = distances.ravel()  # avoid unnecessary indexing

        if quantile is not None:    # filter with quantile
            sorted_inds = np.argsort(distances)
            threshold = distances[ sorted_inds[self.n_samples-1] ]
            accepted = sorted_inds[:self.n_samples]

        else:  # filter with predefined threshold
            accepted = distances < threshold

        posteriors = [p[accepted] for p in parameters]

        return {'samples': posteriors, 'threshold': threshold}
<commit_msg>ABCMethod.infer renamed to sample. Moved n_samples as its first parameter. Updated notebooks.<commit_after>import numpy as np
from time import sleep
import dask
from distributed import Client

from elfi.bo.gpy_model import GPyModel
from elfi.bo.acquisition import LcbAcquisition, SecondDerivativeNoiseMixin, RbfAtPendingPointsMixin
from elfi.utils import stochastic_optimization
from elfi.posteriors import BolfiPosterior
from .async import wait

"""
Implementations of some ABC algorithms
"""

class ABCMethod(object):
    def __init__(self, distance_node=None, parameter_nodes=None, batch_size=10):

        if distance_node is None or parameter_nodes is None:
            raise ValueError("Need to give the distance node and list of parameter nodes")

        self.distance_node = distance_node
        self.parameter_nodes = parameter_nodes
        self.n_params = len(parameter_nodes)
        self.batch_size = batch_size

    def sample(self, *args, **kwargs):
        raise NotImplementedError

    # Run the all-accepting sampler.
    def _get_distances(self, n_samples):

        distances = self.distance_node.acquire(n_samples, batch_size=self.batch_size).compute()
        parameters = [p.acquire(n_samples, batch_size=self.batch_size).compute()
                      for p in self.parameter_nodes]

        return distances, parameters


class Rejection(ABCMethod):
    """
    Rejection sampler.
    """
    def sample(self, n_samples, threshold=None, quantile=None):
        """
        Run the rejection sampler. Inference can be repeated with a different
        threshold without rerunning the simulator.
        - 1st run with threshold: run simulator and apply threshold
        - next runs with threshold: only apply threshold to existing distances
        - all runs with quantile: run simulator
        """

        n_sim = n_samples if quantile is None else int(n_samples / quantile)

        distances, parameters = self._get_distances(n_sim)
        distances = distances.ravel()  # avoid unnecessary indexing

        if quantile is not None:  # filter with quantile
            sorted_inds = np.argsort(distances)
            threshold = distances[ sorted_inds[n_samples-1] ]
            accepted = sorted_inds[:n_samples]

        else:  # filter with predefined threshold
            accepted = distances < threshold

        posteriors = [p[accepted] for p in parameters]

        return {'samples': posteriors, 'threshold': threshold}

{'fraction_non_alphanumeric': 0.06876386368219399, 'fraction_numerical': 0.001613228473482557, 'mean_word_length': 3.0991735537190084, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7342018', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1319, 'n_words': 429}