-
Notifications
You must be signed in to change notification settings - Fork 101
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
Phases calculation using the Designmatrix feature #1850
Open
devbhakt
wants to merge
4
commits into
nanograv:master
Choose a base branch
from
devbhakt:designmatrix_eo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
86c8140
Phases calculation using the Designmatrix feature
devbhakt d7938cf
Merge branch 'master' into designmatrix_eo
devbhakt eb43e6c
Renamed calc_phase option to linearize_model option within event_optimze
devbhakt 827bcbb
Changelog entry update
devbhakt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -414,6 +414,16 @@ def __init__( | |
self.model, phs, phserr | ||
) | ||
self.n_fit_params = len(self.fitvals) | ||
self.M, _, _ = self.model.designmatrix(self.toas) | ||
self.M = self.M.transpose() * -self.model.F0.value | ||
self.phases = self.get_event_phases() | ||
self.calc_phase = False | ||
|
||
def calc_phase_matrix(self, theta): | ||
d_phs = np.zeros(len(self.toas)) | ||
for i in range(len(theta) - 1): | ||
d_phs += self.M[i + 1] * (self.fitvals[i] - theta[i]) | ||
return (self.phases - d_phs) % 1 | ||
|
||
def get_event_phases(self): | ||
""" | ||
|
@@ -446,7 +456,10 @@ def lnposterior(self, theta): | |
return -np.inf, -np.inf, -np.inf | ||
|
||
# Call PINT to compute the phases | ||
phases = self.get_event_phases() | ||
if self.calc_phase: | ||
phases = self.calc_phase_matrix(theta) | ||
else: | ||
phases = self.get_event_phases() | ||
lnlikelihood = profile_likelihood( | ||
theta[-1], self.xtemp, phases, self.template, self.weights | ||
) | ||
|
@@ -686,6 +699,13 @@ def main(argv=None): | |
action="store_true", | ||
dest="noautocorr", | ||
) | ||
parser.add_argument( | ||
"--calc_phase", | ||
help="Calculates the phase at each MCMC step using the designmatrix", | ||
default=False, | ||
action="store_true", | ||
dest="calc_phase", | ||
) | ||
|
||
args = parser.parse_args(argv) | ||
pint.logging.setup( | ||
|
@@ -862,6 +882,9 @@ def main(argv=None): | |
# This way, one walker should always be in a good position | ||
pos[0] = ftr.fitvals | ||
|
||
# How phase will be calculated at each step (either with the designmatrix or ) | ||
ftr.calc_phase = True if args.calc_phase else False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can just be |
||
|
||
import emcee | ||
|
||
# Setting up a backend to save the chains into an h5 file | ||
|
@@ -925,7 +948,7 @@ def chains_to_dict(names, sampler): | |
|
||
def plot_chains(chain_dict, file=False): | ||
npts = len(chain_dict) | ||
fig, axes = plt.subplots(npts, 1, sharex=True, figsize=(8, 9)) | ||
fig, axes = plt.subplots(npts, 1, sharex=True, figsize=(8, npts * 1.5)) | ||
for ii, name in enumerate(chain_dict.keys()): | ||
axes[ii].plot(chain_dict[name], color="k", alpha=0.3) | ||
axes[ii].set_ylabel(name) | ||
|
@@ -950,6 +973,7 @@ def plot_chains(chain_dict, file=False): | |
lnprior_samps = blobs["lnprior"] | ||
lnlikelihood_samps = blobs["lnlikelihood"] | ||
lnpost_samps = lnprior_samps + lnlikelihood_samps | ||
maxpost = lnpost_samps[:][burnin:].max() | ||
ind = np.unravel_index( | ||
np.argmax(lnpost_samps[:][burnin:]), lnpost_samps[:][burnin:].shape | ||
) | ||
|
@@ -1000,8 +1024,15 @@ def plot_chains(chain_dict, file=False): | |
] | ||
ftr.set_param_uncertainties(dict(zip(ftr.fitkeys[:-1], errors[:-1]))) | ||
|
||
# Calculating the AIC and BIC | ||
n_params = len(ftr.model.free_params) | ||
AIC = 2 * (n_params - maxpost) | ||
BIC = n_params * np.log(len(ts)) - 2 * maxpost | ||
ftr.model.NTOA.value = ts.ntoas | ||
f = open(filename + "_post.par", "w") | ||
f.write(ftr.model.as_parfile()) | ||
f.write(f"\n#The AIC is {AIC}") | ||
f.write(f"\n#The BIC is {BIC}") | ||
f.close() | ||
|
||
# Print the best MCMC values and ranges | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest changing this option to "--linearize_model" or something like that. I don't think "--calc_phase" accurately reflects what it is doing.