Skip to content

Commit

Permalink
Fix for QU-fitting: PA0 angle wrapping (#105)
Browse files Browse the repository at this point in the history
* Fixed PA wrap of QU-fitting

* Fixed ref before assignment bug

* Fixed corner plots

* Minor optimisation for creating the corner plot

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
jackieykma and pre-commit-ci[bot] authored Jan 11, 2024
1 parent a9c83dc commit cd5f397
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions RMtools_1D/do_QUfit_1D_mnest.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,31 @@ def run_qufit(

# Do the post-processing on one processor
endTime = time.time()

# Shift the angles by 90 deg, if necessary
# This is to get around the angle wrap issue
rotated_parNames = [] ## Store parameters that have been rotated
for i in range(nDim):
if (
parNames[i][-3:] == "deg"
and bounds[i][0] == 0.0
and bounds[i][1] == 180.0
and wraps[i] == "periodic"
):
# Only try to do it if the units is in deg, bounded within [0., 180.], and is a periodic variable
summary_tmp = result.get_one_dimensional_median_and_error_bar(parNames[i])
if summary_tmp.median < 45.0 or summary_tmp.median >= 135.0:
# Shift PA by 90 deg
result.samples[:, i] += 90.0
# Keep all values within [0, 180)
result.samples[:, i] -= (result.samples[:, i] >= 180.0) * 180.0
# Keep track of which parameters have been rotated
rotated_parNames.append(parNames[i])

# Update the posterior values
if len(rotated_parNames) > 0:
result.samples_to_posterior()

# Best guess here - taking the maximum likelihood value
lnLike = np.max(result.log_likelihood_evaluations)
lnEvidence = result.log_evidence
Expand All @@ -418,10 +443,15 @@ def run_qufit(
for i in range(nDim):
summary = result.get_one_dimensional_median_and_error_bar(parNames[i])
# Get stats around modal value
# Shift back by 90 deg if necessary
median_val = summary.median - 90.0 * (parNames[i] in rotated_parNames)
median_val += 180.0 * (median_val < 0.0) * (parNames[i] in rotated_parNames)
plus_val = summary.plus
minus_val = summary.minus
p[i], errPlus[i], errMinus[i] = (
summary.median,
summary.plus,
summary.minus,
median_val,
plus_val,
minus_val,
)

# Calculate goodness-of-fit parameters
Expand Down Expand Up @@ -512,6 +542,21 @@ def run_qufit(
# del(labels[i])
# del(p[i])

# Tricky to get correct PA on the corner plot because of PA wrap!
# Solution: Shift PA back to original, and ignore limit of [0, 180]

for i in range(nDim):
if parNames[i] in rotated_parNames:
# Rotate back by the 90 deg
result.samples[:, i] -= 90.0
if p[i] > 135.0:
# In case the PA shown would be << 0 deg
result.samples[:, i] += 180.0

# Resampling to make sure the results show
if len(rotated_parNames) > 0:
result.samples_to_posterior()

cornerFig = result.plot_corner()

# Save the posterior chains to ASCII file
Expand Down

0 comments on commit cd5f397

Please sign in to comment.