pixell icon indicating copy to clipboard operation
pixell copied to clipboard

enplot(map[i]) and enplot(np.array(map)[i]) show mirrored images

Open kwolz opened this issue 7 months ago • 1 comments

Discovered this when plotting TQU maps and saving them to file using enplot.write. The array itself remains unaffected, only the plot flips its x-parity.

from pixell import enmap, enplot, curvedsky, utils
import numpy as np
import healpy as hp


ls = np.arange(500)
ps = np.array([1.] + [1./l**2 for l in ls[1:]]) 
ps_tuple = (0*ps, 0*ps, 0*ps, ps)
alm = hp.synalm(ps_tuple)

box = np.array([[-5, 10], [5, -10]]) * utils.degree
shape, wcs = enmap.geometry(pos=box, res=0.5 * utils.arcmin, proj='car')

map = enmap.zeros((3,) + shape, wcs)
map = curvedsky.alm2map(alm, map)

for ip, p in enumerate("TQU"):
    enplot.write(f"test_map_{p}", enplot.plot(map[ip], colorbar=True))
    enplot.write(f"test_map_arr_{p}", enplot.plot(np.array(map)[ip], colorbar=True))

Resulting plots:

test_map_Q.png: Image test_map_arr_Q.png:

Image

kwolz avatar Jun 14 '25 12:06 kwolz

Hi,

This is because np.array removes the WCS and returns a pure numpy array. This is vaguely alluded to here: https://pixell.readthedocs.io/en/latest/usage.html#passing-maps-through-functions-that-act-on-numpy-arrays

If for example I did


...

omap = np.array(map)
print(omap.wcs)

for ip, p in enumerate("TQU"):
    enplot.write(f"test_map_{p}", enplot.plot(map[ip], colorbar=True))
    enplot.write(f"test_map_arr_{p}", enplot.plot(omap[ip], colorbar=True))

I get

$ python pltest.py 
Traceback (most recent call last):
  File "/home/msyriac/Dropbox/sandbox/pltest.py", line 18, in <module>
    print(omap.wcs)
          ^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute 'wcs'

It seems that enplot does not have any problems with accepting a pure numpy array; it will then just attached a default WCS to the array, which has RA increasing to the right, unlike your specified geometry with negative cdelt and RA decreasing to the right (the astro convention). This is why the image looks flipped. I'll leave it up to @amaurea whether we should have enplot reject pure numpy inputs.

msyriac avatar Jun 17 '25 20:06 msyriac