Repository navigation
Fix bug in map .fits.gz write (change map data transpose) - #1346
Conversation
|
I tried to read a bit through the code and understand this. The write code seems simple and correct, just calling gammapy/gammapy/maps/wcsmap.py Line 249 in faa698a which does basically this: I think the problem originates from here, where Line 73 in ca3b99d and then sometimes in the gammapy.map code (e.g. in read) code paths are taken where first the map object is created with data=None (and thus this call to WcsNDMap. _init_data triggered from WcsNDMap. init), and then later assignments like map.data = hdu.data are made, which effectively transpose the data.
@woodmd - If you have time for a quick look, what would you advise? Is a quite extensive change in the way map objects are created needed that avoids this data transpose issue? |
|
I don't think this is a problem with the write method. The transpose operation in I had a brief look at your example but what I found is truly bizarre. If I run your example but change the output file name to 'diffuse_model_gc.fits' then I no longer see the orientation flip. Peeking directly at the file it looks like the data array orientation written by |
|
So the I'm not sure what the solution is here, but I'm +1 to not have transposed arrays as data member always, and instead to transpose or treat axes order as needed on access. @woodmd - can that be achieved, e.g. by removing the I'll start working on this soon, but I'm not sure yet which changes to make (or only docstring improvements as you suggest). I'll try to produce a minimal test case for the different behaviour when writing |
The data array is column-major (i.e. transposed with respect to row-major) for all map objects and all map init methods expect a data array with this ordering. This was the same convention used in
This would be cleaner but I don't think it would change the behavior of the code in any way. A transposed array and a transposed view should be totally equivalent (but perhaps there are exceptions to this).
Switching the axis order internally would be a major architectural change and probably deserves some dedicated discussion in a separate issue thread if you want to go this route. Maybe you can move that to #1180 where we discussed the same issue.
It may be that astropy does something weird when getting a transposed view. In that case changing to |
|
Thanks for the advice!
I will try that now. |
|
The fact that FITS write fails on transposed Numpy data arrays, only (or at least?) for I tried this patch in Gammapy maps But this is making these I/O tests fail: here: I guess I should go hunt for another place where we currently transpose data? |
|
I attached a commit with a regression test (all of the asserts are failing before the fix), and then the attempted fix to avoid having transposed Numpy arrays as map data objects. It fixes the regression tests, and most things still work. However sparse map I/O is failing for some existing tests (but not the new one). I haven't figured out yet how to trigger the sparse map I/O issue with a simple test case or how to fix it. I'm offline now, but I'll try to fully resolve this tomorrow. @woodmd - If you could have a look and comment if you're OK with the changes so far, that would be very helpful. |
|
@woodmd - might the issue be here? gammapy/gammapy/maps/wcsmap.py Line 209 in b949c8d Looks like in |
|
I can confirm that changing at the top of resolves the remaining failing tests: Is that the proper fix or is there a better way? |
I think I had assumed
Yes that would be fine although somewhat inefficient for the non-sparse case. A copy isn't needed when I noticed that we also initialize with a transposed array in Lines 37 to 52 in 62e2cc6 so maybe you should switch to initializing with |
|
I've done some more small fixes in d3cf7ce and e1fd764 and then removed the data transpose in the default HPX map data init in 8fb4b4d as you suggested. The only remaining cases where we transpose map data arrays are those: @woodmd - I think all of those are OK, and while probably they could be rewritten to avoid the transpose, there's no need? Is this PR ready to merge? |
Yes this was an oversight. There should be some logic here to generate the appropriate FITS data type keyword from the numpy array dtype. This somewhat goes to the question of whether we want to support integer data types at all. At present the code doesn't really support them because we use
Yes I agree that these don't need to be rewritten. Looks ready to merge. |
@woodmd - I've opened #1350 . It's not clear to me if introducing mask arrays is necessary or a better solution, so I've put it as an open question. |
|
@woodmd - Thank you for the advice and review here! |
Fix bug in map .fits.gz write (change map data transpose)
I think I've found a bug in WCSMapND write.
The image orientation is incorrect in this example:
https://nbviewer.jupyter.org/gist/cdeil/2feb9e784d128d43867a707c0ca23092
At first I thought it's a bug in reproject, but maybe it's just in write and I was always looking at incorrectly images from DS9.
@woodmd - You're busy with new things, right?
If so, I'll have a look at this tomorrow and try to debug / fix it.