question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider adding a `KeplerTargetPixelFile.overlay_on_sky()` function

See original GitHub issue

This would not necessarily replace k2-pix, because the latter provides a command-line interface etc… Instead, perhaps lightkurve could provide the very basic functionality which k2-pix imports.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
keatonbcommented, Aug 16, 2018

I took a stab at this and I’m pretty happy with the aesthetics of the result. There are probably more clever ways to do some of this. I will note that these pixels don’t appear dead-on position when I compare to where the flux is distributed in the TPFs. Perhaps I’ve been a bit careless in the translation from RA,Dec to image location, or perhaps get_coordinates() has limited accuracy.

import numpy as np
import matplotlib.pyplot as plt
from lightkurve import KeplerTargetPixelFile
from astroquery.skyview import SkyView
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.wcs import WCS
from astropy.visualization import ImageNormalize,PercentileInterval,SqrtStretch

#What to inspect?
kic = 11090674
quarter = 4
tpf = KeplerTargetPixelFile.from_archive(kic, quarter=quarter)

#Requires some wcs projection for the axis
#I get one from the image header that I want to plot pixel locations over
c = SkyCoord(ra=tpf.ra*u.degree, dec=tpf.dec*u.degree, frame='icrs')
images = SkyView.get_images(position=c, survey=['2MASS-J'], radius=1.*u.arcmin)
wcs = WCS(images[0][0].header)

#Get median RA,Dec for each pixel
pixra = np.median(tpf.get_coordinates()[0],axis=0)
pixdec = np.median(tpf.get_coordinates()[1],axis=0)

#Convert RA,DEC to pixel coordinates in image frame
pixels = wcs.wcs_world2pix(pixra*u.degree,pixdec*u.degree,1)
#Reshape for plotting
xy = np.reshape(pixels,(2,pixels[0].size)).T
npixels = len(xy)

#Spacing between pixels is also spacing between corners of pixels
dx = np.median(np.diff(pixels)[0])
dy = np.median(np.diff(pixels)[1])

#Define locations of corners relative to pixel centers
corners = np.array([[1.,1.],[1.,-1.],[-1.,-1.],[-1.,1],[1.,1.]])
offsetmatrix = np.array(((dx,-dy), (dy, dx)))/2.
#There must be a more elegant way to do this...
for i in range(len(corners)):
    corners[i] = np.cross(offsetmatrix,corners[i])

#Which pixels have data or are in mask
# 0 = no data; 1 = data, 2 = mask
d = np.zeros(npixels,dtype=int)
d[np.isfinite(tpf.flux[0]).flatten()] += 1
d[tpf.pipeline_mask.flatten()] += 1
colors = ['None','lightblue','red']
lws = [0,1,2]
zorders = [0,1,2]

#Plot reference image
f = plt.figure(figsize=(4.2,4))
ax = plt.subplot(projection=wcs)
norm = ImageNormalize(images[0][0].data, interval=PercentileInterval(99.9),
                      stretch=SqrtStretch())
ax.imshow(images[0][0].data,origin='lower',norm=norm,cmap='gray_r')

#Plot boundaries of each pixel
for i in range(npixels):
    ccoords = xy[i]+corners
    ax.plot(ccoords[:,0],ccoords[:,1],c=colors[d[i]],lw=lws[d[i]],zorder=zorders[d[i]])

ax.set_title('KIC'+str(kic)+', Q'+str(quarter),size=14,pad=15)
ax.set_xlabel('Right Ascension',size=12)
ax.set_ylabel('Declination',size=12)
ax.set_aspect(1)
plt.tight_layout(rect=[.22,-.3,1,1.3]) #matplotlib problem
plt.show()

result: kic11090674_q4_keplerpixels

Let me know if you’d like me to proceed to implement an KeplerTargetPixelFile.overlay_on_sky() function like this.

0reactions
keatonbcommented, Oct 8, 2018

So, any suggestions on how or whether to implement this?

Has anyone quantified the accuracy of the get_coordinates() function? Is this affected by the same systematic offsets of up to 10 pixels described in https://github.com/KeplerGO/K2fov/issues/15? Unless we could provide ~sub-pixel accuracy with get_coordinates(), I would be hesitant to provide this overlay functionality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

lightkurve.KeplerTargetPixelFile
This class offers a user-friendly way to open a Kepler Target Pixel File (TPF), access its meta data, visualize its contents, extract light...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found