Skip to content

Commit

Permalink
Added stopping criteria in RL and MAP_RL
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyoneda committed Jul 24, 2024
1 parent c00ece1 commit 874c6a2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
34 changes: 32 additions & 2 deletions cosipy/image_deconvolution/MAP_RichardsonLucy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class MAP_RichardsonLucy(RichardsonLucySimple):
activate: True
directory: "./results"
only_final_result: True
stopping_criteria:
statistics: "loglikelihood"
threshold: 1e-2
prior:
TSV:
coefficient: 1.e+6
Expand Down Expand Up @@ -74,6 +77,13 @@ def __init__(self, initial_model, dataset, mask, parameter):
coefficient = parameter['prior'][prior_name]['coefficient']
self.priors[prior_name] = self.prior_classes[prior_name](coefficient, initial_model)

# stopping criteria
self.stopping_criteria_statistics = parameter.get('stopping_criteria:statistics', "posterior")
self.stopping_criteria_threshold = parameter.get('stopping_criteria:threshold', 1e-2)

if not self.stopping_criteria_statistics in ["loglikelihood", "posterior"]:
raise ValueError

def load_gamma_prior(self, parameter):

if parameter is None:
Expand Down Expand Up @@ -255,9 +265,29 @@ def check_stopping_criteria(self):
-------
bool
"""
if self.iteration_count < self.iteration_max:

if self.iteration_count == 1:
return False
return True
elif self.iteration_count == self.iteration_max:
return True

if self.stopping_criteria_statistics == "loglikelihood":

loglikelihood = np.sum(self.results[-1]["loglikelihood"])
loglikelihood_before = np.sum(self.results[-2]["loglikelihood"])

if loglikelihood - loglikelihood_before < self.stopping_criteria_threshold:
return True

elif self.stopping_criteria_statistics == "posterior":

posterior = self.results[-1]["posterior"]
posterior_before = self.results[-2]["posterior"]

if posterior - posterior_before < self.stopping_criteria_threshold:
return True

return False

def finalization(self):
"""
Expand Down
25 changes: 23 additions & 2 deletions cosipy/image_deconvolution/RichardsonLucy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class RichardsonLucy(RichardsonLucySimple):
FWHM:
value: 2.0
unit: "deg"
stopping_criteria:
statistics: "loglikelihood"
threshold: 1e-2
background_normalization_optimization:
activate: True
range: {"albedo": [0.9, 1.1]}
Expand All @@ -56,6 +59,13 @@ def __init__(self, initial_model, dataset, mask, parameter):
self.smoothing_fwhm = parameter.get('smoothing:FWHM:value') * u.Unit(parameter.get('smoothing:FWHM:unit'))
logger.info(f"Gaussian filter with FWHM of {self.smoothing_fwhm} will be applied to delta images ...")

# stopping criteria
self.stopping_criteria_statistics = parameter.get('stopping_criteria:statistics', "loglikelihood")
self.stopping_criteria_threshold = parameter.get('stopping_criteria:threshold', 1e-2)

if not self.stopping_criteria_statistics in ["loglikelihood"]:
raise ValueError

def initialization(self):
"""
initialization before running the image deconvolution
Expand Down Expand Up @@ -157,9 +167,20 @@ def check_stopping_criteria(self):
-------
bool
"""
if self.iteration_count < self.iteration_max:
if self.iteration_count == 1:
return False
return True
elif self.iteration_count == self.iteration_max:
return True

if self.stopping_criteria_statistics == "loglikelihood":

loglikelihood = np.sum(self.results[-1]["loglikelihood"])
loglikelihood_before = np.sum(self.results[-2]["loglikelihood"])

if loglikelihood - loglikelihood_before < self.stopping_criteria_threshold:
return True

return False

def finalization(self):
"""
Expand Down

0 comments on commit 874c6a2

Please sign in to comment.