Skip to content

Commit

Permalink
Update docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
chinyitan committed Mar 8, 2022
1 parent e7a77f5 commit 01993d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
27 changes: 16 additions & 11 deletions dolphin/analysis/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ def plot_model_overview(self, lens_name, model_id=None,
:type v_min: `float` or `int`
:param v_max: maximum plotting scale for the model, data, & source plot
:type v_max: `float` or `int`
:param show_source_light: if true, replaces convergence plot with
source light convolved lens decomposition plot
:type show_source_light: `bool`
:return: `matplotlib.pyplot.figure` instance with the plots
:rtype: `matplotlib.pyplot.figure`
"""
Expand Down Expand Up @@ -404,7 +407,7 @@ def get_reshaped_emcee_chain(self, lens_name, model_id, walker_ratio,

def plot_mcmc_trace(self, lens_name, model_id, walker_ratio,
burn_in=-100, verbose=True, fig_width=16,
show_variables=[]):
parameters_to_plot=[]):
"""
Plot the trace of MCMC walkers.
Expand All @@ -421,6 +424,8 @@ def plot_mcmc_trace(self, lens_name, model_id, walker_ratio,
:type verbose: `bool`
:param fig_width: width of the figure
:type fig_width: `float`
:param parameters_to_plot: if not empty, list of parameters to plot
:type fig_width: `list`
:return: `matplotlib.pyplot.figure` instance with the plots
:rtype: `matplotlib.pyplot.figure`
"""
Expand All @@ -431,12 +436,12 @@ def plot_mcmc_trace(self, lens_name, model_id, walker_ratio,
num_params = self.num_params_mcmc
num_step = chain.shape[1]

if len(show_variables) == 0:
variable_list = np.arange(num_params)
if len(parameters_to_plot) == 0:
parameter_list = np.arange(num_params)
else:
variable_list = []
for i in show_variables:
variable_list.append(self.params_mcmc.index(i))
parameter_list = []
for i in parameters_to_plot:
parameter_list.append(self.params_mcmc.index(i))

mean_pos = np.zeros((num_params, num_step))
median_pos = np.zeros((num_params, num_step))
Expand All @@ -445,27 +450,27 @@ def plot_mcmc_trace(self, lens_name, model_id, walker_ratio,
q84_pos = np.zeros((num_params, num_step))

# chain = np.empty((nwalker, nstep, ndim), dtype = np.double)
for i in variable_list:
for i in parameter_list:
for j in np.arange(num_step):
mean_pos[i][j] = np.mean(chain[:, j, i])
median_pos[i][j] = np.median(chain[:, j, i])
std_pos[i][j] = np.std(chain[:, j, i])
q16_pos[i][j] = np.percentile(chain[:, j, i], 16.)
q84_pos[i][j] = np.percentile(chain[:, j, i], 84.)

fig, ax = plt.subplots(len(variable_list), sharex='all',
fig, ax = plt.subplots(len(parameter_list), sharex='all',
figsize=(fig_width, int(fig_width/8) *
len(variable_list)))
len(parameter_list)))
last = num_step
medians = []

for n, i in enumerate(variable_list):
for n, i in enumerate(parameter_list):
if verbose:
print(self.params_mcmc[i],
'{:.4f} ± {:.4f}'.format(median_pos[i][last - 1],
(q84_pos[i][last - 1] -
q16_pos[i][last - 1]) / 2))
if len(show_variables) != 1:
if len(parameter_list) != 1:
# ax[i].plot(mean_pos[i][:3000], c='b')
ax[n].plot(median_pos[i][:last], c='g')
# ax[i].axhline(np.mean(mean_pos[i][burnin:2900]), c='b')
Expand Down
4 changes: 2 additions & 2 deletions dolphin/processor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def custom_logL_addition(self, kwargs_lens=None, kwargs_source=None,
diff = min(abs(pa_light - pa_mass),
180 - abs(pa_light - pa_mass))
if diff > np.abs(max_delta):
return -10**-15
prior += -1e15

# Ensure q_mass is smaller than q_light for the lensing galaxy, where
# q is the ratio between the minor axis to the major axis of a profile
Expand Down Expand Up @@ -387,7 +387,7 @@ def custom_logL_addition(self, kwargs_lens=None, kwargs_source=None,
q_light = ellipticity2phi_q(kwargs_lens_light[0]['e1'],
kwargs_lens_light[0]['e2'])[1]
if q_mass/q_light < max_ratio:
return -10**-15
prior += -1e15

# Provide logarithmic_prior on the source light profile
if 'source_light_option' in self.settings and \
Expand Down
2 changes: 1 addition & 1 deletion test/test_analysis/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_plot_mcmc_trace(self):

fig2 = self.output.plot_mcmc_trace('lens_system2', 'example', 2,
verbose=True, burn_in=0,
show_variables=["gamma_lens"])
parameters_to_plot=["gamma_lens"])

plt.close(fig2)

Expand Down
8 changes: 4 additions & 4 deletions test/test_processor/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ def test_custom_logL_addition(self):
prior = self.config.custom_logL_addition(
kwargs_lens=[{'e1': 0.111, 'e2': 0.0}],
kwargs_lens_light=[{'e1': 0.0526, 'e2': 0.0}])
assert prior == -1e-15
assert prior == -10.**15

# Angle out of sync (phi_L = 20 deg, q_L = 0.8)
prior = self.config.custom_logL_addition(
kwargs_lens=[{'e1': 0.111, 'e2': 0.0}],
kwargs_lens_light=[{'e1': 0.0851, 'e2': 0.0714}])
assert prior == -1e-15
assert prior == -10.**15

# Settings set to False (phi_L = 20 deg, q_L = 0.9)
config2 = deepcopy(self.config)
Expand All @@ -273,15 +273,15 @@ def test_custom_logL_addition(self):
prior = config3a.custom_logL_addition(
kwargs_lens=[{'e1': 0.111, 'e2': 0.0}],
kwargs_lens_light=[{'e1': 0.0851, 'e2': 0.0714}])
assert prior == -1e-15
assert prior == -10.**15

config3b = deepcopy(self.config)
config3b.settings['lens_option'][
'limit_mass_eccentricity_from_light'] = 1.0
prior = config3b.custom_logL_addition(
kwargs_lens=[{'e1': 0.111, 'e2': 0.0}],
kwargs_lens_light=[{'e1': 0.0526, 'e2': 0.0}])
assert prior == -1e-15
assert prior == -10.**15

# Raise error when settings are not bool, int or float
config4a = deepcopy(self.config)
Expand Down

0 comments on commit 01993d4

Please sign in to comment.