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

Added AbstractGridFilter #271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions pyrecest/filters/abstract_grid_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import warnings
import numpy as np
from .abstract_filter_type import AbstractFilterType
from pyrecest.distributions.abstract_grid_distribution import AbstractGridDistribution

class AbstractGridFilter(AbstractFilterType):
def __init__(self, gd):
if not isinstance(gd, AbstractGridDistribution):
raise ValueError("gd must be an instance of AbstractGridDistribution")
self.filter_state = gd

@property
def filter_state(self) -> AbstractGridDistribution:
"""
Get the current estimate of the grid distribution.
Returns : AbstractGridDistribution : the current grid distribution
"""
return self._filter_state

@filter_state.setter
def filter_state(self, new_state):
if not isinstance(new_state, AbstractGridDistribution):
warnings.warn("gd_ is not a GridDistribution. Transforming the distribution with a number of coefficients equal to that of the filter.", RuntimeWarning)
new_state = self.filter_state.from_distribution(new_state, len(self.filter_state.grid_values), self.filter_state.enforce_pdf_nonnegative)

elif np.shape(self.filter_state.grid_values) != np.shape(new_state.grid_values):
warnings.warn("New grid has a different number of grid points.", RuntimeWarning)

self._filter_state = new_state

def update_nonlinear(self, likelihood, z):
grid_vals_new = self.filter_state.grid_values * np.reshape(likelihood(z, self.filter_state.get_grid()), (-1, 1))
assert np.shape(grid_vals_new) == np.shape(self.filter_state.grid_values)

self.filter_state.grid_values = grid_vals_new
self.filter_state = self.filter_state.normalize(suppress_warning = True)

def plot_filter_state(self):
self.filter_state.plot_state()