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
PSF rebinning (fix #43) #607
Conversation
I just pushed a basic implementation of the fix. Some of the tests will fail for a number of reasons:
The bulk of the change is in Two other interesting places to visit are in the same file, in It will be important to add a mechanism for disabling this behavior, because the performance impact is significant. We are also using a Generally speaking, I think I should get better results. I had to set the tolerance to 3% for all the tests I expect to pass to actually pass. Nothing obviously wrong is happening, and maybe that is all I can hope for considering the effects of the convolution, but the feeling is that the results are somewhat biased, with the source's variance always ending up being overestimated by as much as 3%. I'll see if I can find some diagnostics for Sherpa to produce to give me a better idea of where things might be getting off, but again maybe it's all good to begin with. When comparing pixel sizes I am assuming pixels are squares, and I am only using the first component of PSFSpace2D might be a little bit too simplistic. I am not sure whether setting the end of the linspace to I would be surprised if I didn't introduced any issues that have to with WCS or any other thing. I will need to collect more realistic test cases for assessing this. @juramaga and @anetasie I'd appreciate comments on this, and I'll get in touch to get additional requirements and test cases. |
I was under the impression that convolution is commutative, see for example: https://ccrma.stanford.edu/~jos/st/Commutativity_Convolution.html so I don't understand the following statement: "...If the PSF has a coarser resolution then Sherpa will issue an Exception, as that behavior is not currently defined...." but hey what do I know |
@dtnguyen2 I don't understand what commutativity has to do with my statement. The implementation evaluates the model and convolves it with the kernel in the "PSF space" (as per comments) when the PSF has a better resolution. If the PSF has a worse resolution the implementation would evaluate the model in a space that has a worse resolution than the data, which would be wrong anyway, so I just stopped there and hard-coded an exception: https://github.com/sherpa/sherpa/pull/607/files#diff-35f9d411d670ea181d14083553f381e5R565 |
The convolution is commutative means that there is nothing special about psf vs data-space, you can switch the models around and you should get the same result. If you were able to get satisfactory result if the data-space is more coarse then then the psf, then switch the names around then you should get the same result. If not then I would say there is an issue with the implementation, if you don't think that is true then perhaps you should publish it, just make sure to leave my name off the paper. |
Dan, it's not just convolution that is going on here, but rebinning of the result to match the data space. This is where the constraint on the pixel size comes in: we support degrading the resolution of the convolution (in other words, the convolution is done with smaller pixels than the data) but do not support the case where the convolution is to be done with larger pixels than the data (where we'd have to interpolate or come up with some other scheme). It is this latter case where we the code is going to throw an exception. |
That is my point: whatever you do when the data-space is more coarse then PSF then you should do the same when the PSF is more coarse then data-space. PSF folding wrt to data-space is the same as the other way As for the 3% off, if you are not adding noise to you model and PSF then it doesn't sound right. BTW, I you are using LevMar try setting epsfcn to double_epsilon, the default is float_epsilon cause Xspec models are mostly single precision, at least they used to be |
Dan, if I understand you are suggesting that when the PSF is coarser than the data we rebin the PSF to match the data space and evaluate the model (with the convolution) in that space. Well yes, I expect that to be one of the options. I had even tried to implement it, as mentioned in my notes:
Note that this is not really symmetrical, because the evaluation+convolution in the higher resolution PSF space and the rebinning back to the data space must happen at each step of the minimization, while the rebinning of the coarser PSF to the higher resolution data space can be done only once at the beginning of the fit. If instead you are suggesting that we should just apply the same logic to both cases, I don't think that's the right approach, because then the model would be evaluated in a much coarser space and then rebinned to match the "high-res" data. I'd expect that to introduce a lot of noise and in any case to reduce the accuracy of the fit. Maybe for a noiseless Gaussian I should still get good results, but that is why I am publishing the WIP. Note that I only get "perfect" results when I have plenty of bins in the image, so the source and the PSF are effectively 0 at the edges. But then the convolution and the rebinning would take forever, so I reduced the number of bins, and so boundary effects might become more significant. I'll run some more tests. It's really up to SDS to define the behavior anyway. Sherpa might even just error out and ask the user to do something, e.g. rebin the PSF themselves or instruct Sherpa to assume the PSF has the same pixel size as the data. In any case yes, I am concerned that I only get the fit parameters to be 3% off the true ones. It could be something in the rebinning function, or some wrong assumptions in my implementation, or it could be expected given rebinning is involved. And yes, as I mentioned I am looking for a critical review of the current implementation, because there might be errors that a) introduce that 3% "error" and b) make the inverse logic of the coarser PSF to work even worse than it should, whatever the desired behavior in that case should be. |
Is the failure an indication that the PR is still a work in progress? Is this ready for review? |
As far as I know this is still in progress. Omar was going to implement it
based on the conclusion from my report.
On Fri, Jun 7, 2019 at 12:30 PM Aneta Siemiginowska < ***@***.***> wrote:
Is the failure an indication that the PR is still a work in progress? Is
this ready for review?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#607?email_source=notifications&email_token=ABJ7SA64Y6EJH4SYT2W6E5LPZKLLLA5CNFSM4G6AXJLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXGPNDA#issuecomment-499971724>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABJ7SA4ARG76C3ANBIINH7TPZKLLLANCNFSM4G6AXJLA>
.
--
Rafael Martínez-Galarza
Staff Astrophysicist
Chandra X-Ray Data Center
Center for Astrophysics | Harvard & Smithsonian
Office: (617) 4957 7027 | Cell: (617) 945 6397
60 Garden Street | MS 66 | Cambridge, MA 02138
cfa.harvard.edu | Facebook <http://cfa.harvard.edu/facebook> | Twitter
<http://cfa.harvard.edu/twitter> | YouTube <http://cfa.harvard.edu/youtube>
| Newsletter <http://cfa.harvard.edu/newsletter>
|
@anetasie for the description of the failure, and for a number of open questions, please see the first comment I added above. The only thing not current in there is that we now think the loss in precision is expected because of the rebinning. All the other comments, questions, and the request for review still stand and should be addressed. |
I get AssertionError when I tried to use the higher resolution psf in sherpa build in this branch.
|
The test data set is in local directory poo14/aneta/PSF |
Regarding point 3 above: I believe the PSF resolution is always at least as good as the data resolution. But the user could always (in purpose or by mistake) input a PSF that is coarser. In that case we have two options:
I prefer the second option, as I can imagine a case in which an user would like to know how the fit would behave in the case of a poor resolution (with another instrument, for example). Also, I just discussed with Omar that we should also be testing that the MCMC part is improved by the use of a finer grid. I showed this in my report, but Omar has been checking on the optimization part only, not the sampling. I believe, however, that we should get the improvement in MCMC sampling for free, because the sampling is performed on the convolved model. Am I right? Finally, I do believe that we should keep the finer grid fit as optional, due to concerns in computing time. Namely, if the PSF resolution is finer than the data, we should ask the user if they want to perform the convolution in the PSF grid, but warning them that this might take a while to compute. We should also ask them to provide a PSF whose resolution is an integer multiple of the data resolution, as the rebining code we have implemented requires this. |
@anetasie that code is in the rebin function and it checks that the rebinning is not "leaking" flux. You can reduce the error by changing this line: https://github.com/sherpa/sherpa/pull/607/files#diff-7bd52aa2f9422f0c42ca70db9ef0038bR647 The original code had 0.1 |
@olaurino I updated the line and retested. I don't get the error when I run fit for the first time in the session with the default method |
Here is the screen output - I updated the test script in /pool14/aneta/PSF
|
I haven't trace through the code, but it looks like failure is from the assert statement to compare two floating numbers if they are close. If this is the case then one has a lot of options, choose your poison: https://docs.scipy.org/doc/numpy/reference/generated/numpy.isclose.html sao_fcmp or Knuth_close defined in sherpa/utils/init.py (note: GitHub somehow removes the __ prefix and suffix in the init.py) |
Oops, just realized that there is also the function Knuth_boost_close which I think is the boost interpretation of the Knuth's algorithm. BTW, it looks like I forgot to Knuth_boost_close in the definition of all in the sherpa/ui/init.py file. |
@anetasie you can also just comment out the assertion to begin with, and maybe print out the difference. I'll add this to the things to investigate, but yesterday @juramaga was telling me we could probably switch the rebinning function to use one that does not do additional calculations and just adds the bins together rather than the |
@olaurino I can get the fit to run with the assertion commented in the code. |
I expected |
I'm wondering if the hit in the performance when working with higher resolution psf is mainly due to |
My tests indicate that rebin_flux is very inefficient. Rebin_int is much
faster and the results are almost identical.
On Wed, Jun 19, 2019 at 5:18 PM Aneta Siemiginowska < ***@***.***> wrote:
I'm wondering if the hit in the performance when working with higher
resolution psf is mainly due to rebin_flux step and the performance will
improve with the simple summation. Now, the convergence is significantly
slower (with a similar number of evals, 912 vs. 818) for the psf image with
a factor of 2 smaller pixel size than the data (~550sec) vs. the same
binning of the psf and the data (~1 sec).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#607?email_source=notifications&email_token=ABJ7SAZMO67BTSRA3GE3CYTP3KPDBA5CNFSM4G6AXJLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYDJ6QA#issuecomment-503750464>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABJ7SA2SKCDYZSTE6SDP56LP3KPDBANCNFSM4G6AXJLA>
.
--
Rafael Martínez-Galarza
Staff Astrophysicist
Chandra X-Ray Data Center
Center for Astrophysics | Harvard & Smithsonian
Office: (617) 4957 7027 | Cell: (617) 945 6397
60 Garden Street | MS 66 | Cambridge, MA 02138
cfa.harvard.edu | Facebook <http://cfa.harvard.edu/facebook> | Twitter
<http://cfa.harvard.edu/twitter> | YouTube <http://cfa.harvard.edu/youtube>
| Newsletter <http://cfa.harvard.edu/newsletter>
|
What does "much faster" translate to: factor of 10, 100, 1000 etc...? |
@anetasie For the computation times (550 vs 1 sec) what size image were you using? I haven't read through the rebin_flux, nor the much faster Rebin_int, but I suspect that they contain two nested loops (since we are talking about an image after all) so there is a reason to suspect that it will scale quadratically. Shouldn't the aforementioned rebin functions be translated to C/C++ land? If for some crazy reason the rebin function must be in python then shouldn't it be parallelize it? |
@anetasie - with regards to the binning choice for As we're not using integrated models, then if you display the background (e.g. |
Here is rebin_int: def rebin_int(array,factorx,factory):
|
without testing rebin_int, but it looks like it should be practically instantaneous |
@DougBurke - well, I want to display the image of the 'source' at the resolution of the psf used in the convolution, so before the rebinning is applied. It will be more complex if you have no convolved background component, or use expmap. For expmap - does the resolution of expmap need to match the psf resolution or the data image resolution? |
I performed additional tests using Frank's simulated sources. In my tests, I used both the case when the data pixel is an integer multiple of the PSF pixel, and the case when it is not (so, Sherpa in that case selects between rebin_int and rebin_flux as appropriate). I ran fits with data and psf having the same pixel size, and also with different combinations of the pixel size ratio. Results look good. MCMC sampling is still not too efficient, but it runs fine in al cases (as long as a good covariance matrix is available). So you have my green light for freezing the code. |
@DougBurke requested a way to get the information on the data/psf pixel ratio. I added it to the PSFSpace2D class, so that:
will give a tuple with the x/y ratios ( |
@juramaga yes I tested |
I will add a separate issue on the access to the model evaluation on high res psf - so one can get the images of model components at the high or low resolution. |
I pushed a couple of changes. First, I renamed the Secondly, I added code that rounds the pixel size ratio to the closest integer. The default tolerance is 0.1, and users can change it (see https://github.com/sherpa/sherpa/pull/607/files#diff-dd7454052b970f18eea424de5396e853R225) |
8431e60
to
261c4ed
Compare
Data2D does not have WCS information, so I am assuming that it is ok to consider the pixel size to be the same. This should be the equivalent of a regula application of kernel in image manipulation. When working with photographs there is no such concept as a "pixel size" and the kernle is just applied pixel-by-pixel. So I changed the test to use DataIMG instead of Data2D. The tests are failing for the same reason. However, now there is a warning issued because the PSF we created does not have any pixel size information.
Note the tests now also issue a warning, which is expected given the current implementation. Once the bug is fixed, the warning will be gone.
When the ratio is an integer use rebin_int, which is faster. Otherwise use rebin_flux. Note rebin_flux is useful when the model is using an arbitrary grid with a non-integer ratio with the data grid.
more counts where introduces by evaluating the model on a bigger grid without scaling the results to account for that. In principle we would be better off integrating the models, but that is not possible because some of the models are not available as integrated functions. I also updated PSFSpace2D to produce a better spacing between the pixels as well as decoupling the x vs y pixel size. Finally, when the PSF has a worse resolution than the data, Sherpa errors out. Tests were updated to pass now.
1bf762a
to
698eadf
Compare
698eadf
to
63791ae
Compare
Release Note
Sherpa now supports using a PSF with a finer resolution than 2D images. If Sherpa detects that the PSF has a smaller pixel size than the data, it will evaluate the model on a "PSF Space" that has the same resolution as the PSF and the same footprint as the data, then rebin the evaluated model back to the data space for calculating the statistic.
This PR fixes #43.
See the comments for more details