Starfish-develop / Starfish

Tools for Flexible Spectroscopic Inference
https://starfish.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
68 stars 22 forks source link

ValueError: Querying emulator outside of original parameter range. #149

Closed Aseman7 closed 1 year ago

Aseman7 commented 1 year ago

Thanks for your attention in advance. As for the examples file, after running of setup. ipynb successfully, I want to run single.ipynb, but the following cell

%time model.train(priors),

it can not run correctly and I got this error (without any change in the previous cells):

ValueError: Querying emulator outside of original parameter range.

mileslucas commented 1 year ago

Sorry for the delayed response,I will take a look at this soon. Can you let me know which versions of Python and Starfish you are using?

This error occurs when you try and retrieve a spectrum outside of your model grid, not sure why it could be happening in the tutorial.

Aseman7 commented 1 year ago

Thanks a lot for your email. Yes, I don't find the problem. I am trying to run it for another sample.

Python 3.9.7 Starfish 0.4.2

On Sun, 8 Jan 2023 at 23:36, Miles Lucas @.***> wrote:

Sorry for the delayed response,I will take a look at this soon. Can you let me know which versions of Python and Starfish you are using?

This error occurs when you try and retrieve a spectrum outside of your model grid, not sure why it could be happening in the tutorial.

— Reply to this email directly, view it on GitHub https://github.com/Starfish-develop/Starfish/issues/149#issuecomment-1374946121, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP5BJSMLD2HDCCL3SQ5S7DLWRM6P5ANCNFSM6AAAAAATDRVAU4 . You are receiving this because you authored the thread.Message ID: @.***>

-- Fatemeh Davoudi Postdoctoral researcher at University of Liège, Belgium

astro-catherine commented 1 year ago

I am running into the same error, though I am using my own data and running Python 3.9.12.

gully commented 1 year ago

This error should arise when you try to evaluate the spectral emulator with values outside of the original grid window, so for example the emulator was trained from [5000, 5700] and you attempt to recreate a 5704.1 Teff.

Usually one prevents an MCMC from wandering past these boundaries by enforcing a prior that does not allow the walkers to reach this point---can you confirm that the prior and likelihood are structured in such a way that would prevent the spectral reconstruction from occurring if handed an out-of-bounds grid point?

astro-catherine commented 1 year ago

The ranges I currently have set are Teff=[3700, 5500], logg=[4.0, 5.0], and Z=[-0.5, 0.5], and the priors I have set are priors = { "T": st.norm(4968, 100), "Z": st.uniform(-0.5, 0.5), "logg": st.uniform(4.0, 5.0)} Though I have tried a few different variations on these values.

AustenWallis commented 1 year ago

Hi, (not apart of the devs but ran into this error before too), ensure your global covariance kernel variables , log_amp and log_ls also match within the 'priors = {...}' and the 'model = SpectrumModel(...)'. In addition, that the two variables give you a reasonable standard deviation fit on the residuals graph before these steps. If not, it will also output the same valueerror. Don't know if this helps!

gully commented 1 year ago

Hi @astro-catherine, a few thoughts:

  1. The "T": st.norm(4968, 100) prior has finite support outside of Teff=[3700, 5500] bounds. Do you still get the same error if you use a Uniform prior on T? Are you progressing any steps before the error is raised, or is it immediate?
  2. I forget what happens with edge cases, if say a logg=5.00000 is proposed, does it violate the boundary? I suppose that's vanishing probability, but would be good to know (would likely only happen if you were initializing samplers on integer numbers somehow).
  3. Agreed with @AustenWallis, even though the covariance kernel variables are not strictly part of the grid dimensions, the error might just be raising the wrong message in response to some other deviance.
  4. How are you doing the initializing? Is it with samples from the prior or some other heuristic? I suppose there could be hidden assumptions in the initialization that send samplers outside the domain.

Do any of those solve the problem?

Aseman7 commented 1 year ago

Thank you very much for your answers, those are useful. Solved my problem, for solving it

  1. I simultaneously assigned a value to "logg" in the priors and did not freeze it. "logg": st.norm (4.9, 0.007),

2: I changed the value for the covariance kernel variables according to https://github.com/Starfish-develop/Starfish/issues/148.

Either one alone didn't solve my problem. I tried them several times.

Now that the training for numerical optimization has been completed correctly, the next problem is that I don't have a correct residual graph even after using an optimized initial model. "In the single.ipynb as Starfish example, I see the residual graph is getting true after this step unlike mine." Do you have any suggestions to correct it? Any help would be appreciated.

After training

I write here the original ranges, SpectrumModel, and priors too, thanks a lot:

ranges = [[2400, 3900], [4.5, 5.5], [-1, 1]] # T, logg, Z

model = SpectrumModel( "F_SPEX_emu.hdf5", data=data, grid_params=[3080, 4.9, 0], Av=0,
global_cov=dict(log_amp=-11, log_ls=2)) #We want our global_cov:log_amp to be reasonable, so pay attention to the 𝜎-contours in the residuals plots.) model

priors = { "T": st.norm(3050, 20), "logg": st.norm(4.9, 0.0068), "Z": st.uniform(-0.5, 0.5), "Av": st.halfnorm(0, 0.2), "global_cov:log_amp": st.norm(-11, 3), "global_cov:log_ls": st.uniform(0, 10), }

AustenWallis commented 1 year ago

A log_amp value of -11 is okay for the example spectrum given in 'single.ipynb' where the flux values are around ~6x10^-1. Your flux is 1x10^-19, trying changing the log_amp to around -30 (equally with the prior)? My best guess is just playing with this number until you see scale in the residuals appearing.

gully commented 1 year ago

Yay, glad this issue is resolved. And yes, what @AustenWallis said. If you have absolute flux units the absolute value of your noise model terms will have to be adjusted to be at least in the same order of magnitude for initialization. I'm inclined to close this Issue since the proposed solutions have been shown to work, and there is no outstanding bug. @astro-catherine if you are still experiencing problems please reopen the Issue or make a new Issue with any revised friction points. Thank you all 🙏