Skip to content
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

Example on how to plot different model components #28

Closed
ivvv opened this issue Jun 30, 2021 · 6 comments
Closed

Example on how to plot different model components #28

ivvv opened this issue Jun 30, 2021 · 6 comments

Comments

@ivvv
Copy link

ivvv commented Jun 30, 2021

I've tried to follow the docs and the API in order to visualise the different components in my model and did not succeed. I have 3 model parameters. Here is what I tried:

m = xspec.Model("constant*(bknpower + gauss)")
Then I set the priors on some parameters and solved and get a nice corner plot etc. And trying to visualise the two models I'm interested in with this:

data = solver.posterior_predictions_convolved(component_names=['ignore','bknpower','gauss'],nsamples=100)

And I can only see on black curve passing through the data points, no separtion of the components. Adding at the end plt.gca().legend() only showed one legend entry for the data and nothing for the models.

I even tried to add plot_args=[{'color': 'red'},{'color:'green'},{'color':'blue'}] in the call to solver.posterior_predictions_convolved() with no success.

I suppose I'm doing something wrong.

In the same way of insufficient info in the docs, how can I get the actual total model (or per component) with the best-fit parameters? Currently it is plotted with plt.figure() but if I want to use the predicted model in subplots then it's not very convenient. I can see the dataset data['models'] with shape (100,1,1306) but I cannot make any use of it with the available info.

Can you please add some examples in the documentations?

Thanks in advance,

@JohannesBuchner
Copy link
Owner

Can you check what

Plot.addCommand('wdata foo')
xspec.Plot('counts')

writes to the file? Those columns are what is being plotted. (expecting 6 or 4 columns, depending on Plot.background).

Is there a better way to get plot info from pyxspec?

@ivvv
Copy link
Author

ivvv commented Jun 30, 2021

Yes, the two commands did write to a file foo.qdp and as far as I know, there is no better way to get this from pyXspec (I've learned this trick from Andy Beardmore).

@ivvv
Copy link
Author

ivvv commented Jun 30, 2021

But I have some troubles to understand what's in the QDP file though, when used with xspec.Plot('counts').

It seems to me that the last column in the QDP file is the total model but strangely the columns for the individual components are all zeroes. Will try to understand it.

I used it for xspec.Plot('ldata')(for another problem) and the content was quite different: energy,dE,rate,rate_err,total,m1,m2,...

@JohannesBuchner
Copy link
Owner

Probably the code should be simplified (replacing the callback function with code duplication).

Essentially what you need is:

for k, row in enumerate(analyser.posterior):
     analyser.set_parameters(values=row, transformations=analyser.transformations)

     # do something with xspec.Plot.* and matplotlib.*

@JohannesBuchner
Copy link
Owner

Everything else in the code is just working around xspec weirdness

@JohannesBuchner
Copy link
Owner

Please see https://johannesbuchner.github.io/BXA/tutorial_usage_plotbxa.html for a very nice new plotting class provided by David Homan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants