Skip to content

Commit

Permalink
ex16 - UltraNest, added Radek comments, except 8
Browse files Browse the repository at this point in the history
  • Loading branch information
rapoliveira committed Sep 9, 2024
1 parent 74d944b commit 38372d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
15 changes: 13 additions & 2 deletions examples/example_16/ob03235_2_full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ starting_parameters:
# file: ob08092-o4_starting_file_input.txt
# parameters: t_0 u_0 t_E
# See also ob08092-o4_starting_file.yaml
# prior_limits:
# t_0: [2452847.5, 2452848.5]
# u_0: [0.08, 0.18]
# t_E: 58.7 64.7
# rho: [0.0004, 0.0018]
# pi_E_N: [-0.05, 0.05]
# pi_E_E: [-0.05, 0.05]
# s: [1.077, 1.125]
# q: [0.001, 0.02]
# alpha: [219.4, 229.4]
# # Only MultiNest and UltraNest, cannot be given with starting_parameters
fit_constraints:
negative_blending_flux_sigma_mag: 20.
#one can specify for which dataset soft constrain on blending flux should be applied: sigma dataset_label(s)
Expand Down Expand Up @@ -94,10 +105,10 @@ fitting_parameters:
# basename: out_ob08092_O4_MN-
# multimodal: True
# evidence tolerance: 0.5
## UltraNest only (log directory, derived parameter names, show_status,
## UltraNest only (log directory, derived param names, show_status,
## dlogz=0.5, frac_remain=0.01, max_num_improvement_loops=-1)
# log directory: ultranest_outputs/
# derived parameter names: source_flux blending_flux
# derived param names: flux_s_1 flux_b_1
# show_status: True
# dlogz: 2.
# frac_remain: 0.5
Expand Down
2 changes: 1 addition & 1 deletion examples/example_16/ob08092-o4_UN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ prior_limits:
t_E: 16. 19.6
fitting_parameters:
log directory: ultranest_outputs/
derived parameter names: source_flux blending_flux
derived param names: flux_s_1 flux_b_1
show_status: True
n_live_points: 1000
# `n_live_points` can also be named `min_num_live_points`
Expand Down
42 changes: 15 additions & 27 deletions examples/example_16/ulens_model_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class UlensModelFit(object):
there is a check if directory exists. If not given, no outputs
are saved.
``derived parameter names`` (*str*) - names of additional derived
``derived param names`` (*str*) - names of additional derived
parameters created by transform. In microlensing, they are usually
the source(s) and blending fluxes. If not given, they are ignored
in the transform function.
Expand Down Expand Up @@ -578,7 +578,7 @@ def _guess_MultiNest_or_UltraNest(self):
if all([key in args_MultiNest for key in self._fitting_parameters]):
return "MultiNest"

args_UltraNest = ['log directory', 'derived parameter names',
args_UltraNest = ['log directory', 'derived param names',
'show_status', 'dlogz', 'frac_remain',
'max_num_improvement_loops', 'n_live_points']
if all([key in args_UltraNest for key in self._fitting_parameters]):
Expand Down Expand Up @@ -1397,12 +1397,6 @@ def _parse_fitting_parameters_MultiNest(self):
floats = ['sampling efficiency', 'evidence tolerance']
allowed = strings + bools + ints + floats

only_UltraNest = ['log directory', 'derived parameter names',
'show_status', 'dlogz', 'frac_remain',
'max_num_improvement_loops']
for item in only_UltraNest:
settings.pop(item, None)

self._check_required_and_allowed_parameters(required, allowed)
self._check_parameters_types(settings, bools, ints, floats, strings)

Expand Down Expand Up @@ -1478,7 +1472,7 @@ def _parse_fitting_parameters_UltraNest(self):
self._kwargs_UltraNest = dict()
self._kwargs_UltraNest['viz_callback'] = False

settings = self._fitting_parameters
settings = self._fitting_parameters.copy()
if settings is None:
settings = dict()

Expand All @@ -1487,14 +1481,10 @@ def _parse_fitting_parameters_UltraNest(self):
ints = ['min_num_live_points', 'max_num_improvement_loops']
if 'n_live_points' in settings:
ints[0] = 'n_live_points'
strings = ['log directory', 'derived parameter names']
strings = ['log directory', 'derived param names']
floats = ['dlogz', 'frac_remain']
allowed = strings + bools + ints + floats

only_MultiNest = ['basename', 'multimodal', 'evidence tolerance']
for item in only_MultiNest:
settings.pop(item, None)

self._check_required_and_allowed_parameters(required, allowed)
self._check_parameters_types(settings, bools, ints, floats, strings)
self._log_dir_UltraNest = settings.pop("log directory", None)
Expand All @@ -1505,8 +1495,8 @@ def _parse_fitting_parameters_UltraNest(self):
elif not path.isdir(self._log_dir_UltraNest):
raise ValueError("log directory value in fitting_parameters"
"exists, but it is a file.")
value = settings.pop("derived parameter names", "")
self._derived_params_UltraNest = value.split()
value = settings.pop("derived param names", "")
self._derived_param_names_UltraNest = value.split()

keys = {"n_live_points": "min_num_live_points"}
same_keys = ["min_num_live_points", 'max_num_improvement_loops',
Expand Down Expand Up @@ -2585,21 +2575,19 @@ def _setup_fit_UltraNest(self):
"""
Prepare UltraNest fit, declaring sampler instance.
If the names of the derived parameters are not given, the source
and blending fluxes are assigned, with the former containing _1
and _2 in case of multiple sources.
and blending fluxes are assigned, with indexes depending on the
number of sources (s1, s2) and datasets (_1, _2).
"""
if self._return_fluxes and len(self._derived_params_UltraNest) == 0:
self._derived_params_UltraNest = ["source_flux", "blending_flux"]
if self._n_fluxes > 2:
self._derived_params_UltraNest[0] += "_1"
for i in range(2, self._n_fluxes):
name = "source_flux_{:}".format(i)
self._derived_params_UltraNest.insert(i-1, name)
if self._return_fluxes:
if len(self._derived_param_names_UltraNest) == 0:
if self._flux_names is None:
self._flux_names = self._get_fluxes_names_to_print()
self._derived_param_names_UltraNest = self._flux_names

self._sampler = ultranest.ReactiveNestedSampler(
self._fit_parameters,
self._ln_like, transform=self._transform_unit_cube_UltraNest,
derived_param_names=self._derived_params_UltraNest,
derived_param_names=self._derived_param_names_UltraNest,
log_dir=self._log_dir_UltraNest
)

Expand Down Expand Up @@ -2678,7 +2666,7 @@ def _transform_unit_cube_UltraNest(self, cube):
n_params = n_dims + self._n_fluxes_per_dataset * self._return_fluxes
cube_out = self._min_values + cube[:n_dims] * self._range_values

# Check: are "x_caustic_in" lines needed as in line 2597?
# TBD: add "x_caustic_in" checks as in line 2610...

if self._return_fluxes:
fluxes = self._get_fluxes()
Expand Down

0 comments on commit 38372d4

Please sign in to comment.