Skip to content

Release 0.10.12 #399

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

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions doc/releasenotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ Release Notes
...........


0.10.12 (2021-01-20)
--------------------

* Check components for their adaptivity for correct application of look-ahead
mode (#397).


0.10.11 (2021-01-02)
--------------------

Expand Down
28 changes: 26 additions & 2 deletions pyabc/acceptor/acceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ..distance import Distance, SCALE_LIN, StochasticKernel
from ..epsilon import Epsilon
from ..parameters import Parameter
from .pdf_norm import pdf_norm_max_found
from .pdf_norm import pdf_norm_from_kernel, pdf_norm_max_found
from ..storage import save_dict_to_json


Expand Down Expand Up @@ -171,6 +171,20 @@ def __call__(self,
"""
raise NotImplementedError()

def requires_calibration(self) -> bool:
"""
Whether the class requires an initial calibration, based on
samples from the prior. Default: False.
"""
return False

def is_adaptive(self) -> bool:
"""
Whether the class is dynamically updated after each generation,
based on the last generation's available data. Default: False.
"""
return False

# pylint: disable=R0201
def get_epsilon_config(self, t: int) -> dict:
"""
Expand All @@ -187,7 +201,7 @@ def get_epsilon_config(self, t: int) -> dict:
config: dict
The relevant information.
"""
return None
return {}


class SimpleFunctionAcceptor(Acceptor):
Expand Down Expand Up @@ -373,6 +387,16 @@ def __init__(
self.kernel_scale = None
self.kernel_pdf_max = None

def requires_calibration(self) -> bool:
# this check is rather superficial and may be improved by a re-design
# of `pdf_norm_method`
return self.pdf_norm_method != pdf_norm_from_kernel

def is_adaptive(self) -> bool:
# this check is rather superficial and may be improved by a re-design
# of `pdf_norm_method`
return self.pdf_norm_method != pdf_norm_from_kernel

def initialize(
self,
t: int,
Expand Down
16 changes: 15 additions & 1 deletion pyabc/distance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ def __call__(
observed data.
"""

def requires_calibration(self) -> bool:
"""
Whether the class requires an initial calibration, based on
samples from the prior. Default: False.
"""
return False

def is_adaptive(self) -> bool:
"""
Whether the class is dynamically updated after each generation,
based on the last generation's available data. Default: False.
"""
return False

def get_config(self) -> dict:
"""
Return configuration of the distance.
Expand Down Expand Up @@ -176,7 +190,7 @@ def __call__(self,
t: int = None,
par: dict = None) -> float:
raise AssertionError(
f"{self.__class__.__name__} is not intended to be called.")
f"Distance {self.__class__.__name__} should not be called.")


class AcceptAllDistance(Distance):
Expand Down
30 changes: 28 additions & 2 deletions pyabc/distance/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ def configure_sampler(self,
if self.adaptive:
sampler.sample_factory.record_rejected = True

def requires_calibration(self) -> bool:
return self.initial_weights is None

def is_adaptive(self) -> bool:
return self.adaptive

def initialize(self,
t: int,
get_all_sum_stats: Callable[[], List[dict]],
Expand All @@ -233,7 +239,7 @@ def initialize(self,
self.x_0 = x_0

# initial weights pre-defined
if self.initial_weights is not None:
if not self.requires_calibration():
self.weights[t] = self.initial_weights
return

Expand All @@ -249,7 +255,7 @@ def update(self,
"""
Update weights.
"""
if not self.adaptive:
if not self.is_adaptive():
return False

# execute function
Expand Down Expand Up @@ -407,6 +413,12 @@ def __init__(
self.weights = weights
self.factors = factors

def requires_calibration(self) -> bool:
return any(d.requires_calibration() for d in self.distances)

def is_adaptive(self) -> bool:
return any(d.is_adaptive() for d in self.distances)

def initialize(
self,
t: int,
Expand Down Expand Up @@ -552,6 +564,14 @@ def __init__(
self.scale_function = scale_function
self.log_file = log_file

def requires_calibration(self) -> bool:
return (self.initial_weights is None
or any(d.requires_calibration() for d in self.distances))

def is_adaptive(self) -> bool:
return (self.adaptive
or any(d.is_adaptive() for d in self.distances))

def initialize(self,
t: int,
get_all_sum_stats: Callable[[], List[dict]],
Expand Down Expand Up @@ -716,6 +736,9 @@ def _calculate_whitening_transformation_matrix(self, sum_stats):
self._whitening_transformation_matrix = (
v.dot(np.diag(1. / np.sqrt(w))).dot(v.T))

def requires_calibration(self) -> bool:
return True

def initialize(self,
t: int,
get_all_sum_stats: Callable[[], List[dict]],
Expand Down Expand Up @@ -812,6 +835,9 @@ def _calculate_normalization(self, sum_stats):
- self.lower(measures[measure])
for measure in self.measures_to_use}

def requires_calibration(self) -> bool:
return True

def initialize(self,
t: int,
get_all_sum_stats: Callable[[], List[dict]],
Expand Down
14 changes: 14 additions & 0 deletions pyabc/epsilon/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ def __call__(self,
The epsilon for population t.
"""

def requires_calibration(self) -> bool:
"""
Whether the class requires an initial calibration, based on
samples from the prior. Default: False.
"""
return False

def is_adaptive(self) -> bool:
"""
Whether the class is dynamically updated after each generation,
based on the last generation's available data. Default: False.
"""
return False

def get_config(self):
"""
Return configuration of the distance function.
Expand Down
8 changes: 7 additions & 1 deletion pyabc/epsilon/epsilon.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,19 @@ def get_config(self):

return config

def requires_calibration(self) -> bool:
return self._initial_epsilon == 'from_sample'

def is_adaptive(self) -> bool:
return True

def initialize(self,
t: int,
get_weighted_distances: Callable[[], pd.DataFrame],
get_all_records: Callable[[], List[dict]],
max_nr_populations: int,
acceptor_config: dict):
if self._initial_epsilon != 'from_sample':
if not self.requires_calibration():
# safety check in __call__
return

Expand Down
Loading