def measureOffsetProbabilityInTimeseries(offsets, minNumPoints=2): """ Vet the centroid time series produced by measureDiffOffset() Measure the probablity that the observed difference image centroid- offsets indicate that the transit happened off target. Inputs: -------- offsets (Nca) The return value from measureDiffOffset Optional Inputs: ------------------ minNumPoints (int) Minimum number of points needed before computing result. Currently set to the minimum needed by covar (==2) Returns: ----------- A tuple with 2 values prob Probability the transit happened off target chi2 The chi-squared corresponding to that probability Notes: --------- For highly significant offsets, the probability flatlines at zero for values below ~1e-40. The chi-squared value then can be used to indicate the significance of the offset. """ idx = offsets[:, 'intr_col'] > 0 diffC = offsets[idx, 'diff_col'] - offsets[idx, 'intr_col'] diffR = offsets[idx, 'diff_row'] - offsets[idx, 'intr_row'] if len(diffC) < minNumPoints: return 0, 0 prob, chi2 = covar.computeProbabilityOfObservedOffset(diffC, diffR) return prob, chi2
def dispositionTask(clip): """Decide whether an event is a planet candidate or not """ out = clipboard.Clipboard(isSignificantEvent=True, isCandidate=True, reasonForFail="None") # CENTROID VET minProbForFail = 0.99 centVet = {'Warning':"None"} try: centroids = clip['diffImgCentroids.results'] ootCol_prf, ootRow_prf = np.mean([centroids[:,0],centroids[:,4]], axis = 0), np.mean([centroids[:,1],centroids[:,5]], axis = 0) diffCol_prf, diffRow_prf = centroids[:,2], centroids[:,3] diffC, diffR = (ootCol_prf - diffCol_prf), (ootRow_prf - diffRow_prf) prob, chisq = covar.computeProbabilityOfObservedOffset(diffC, diffR) except ValueError, e: centVet['Warning'] = "Probability not computed: %s" %(e) prob = 0 chisq = 0
def plotCentroidOffsets_TESS(clip): centroids = clip['diffImgCentroids.results'] ootCol_prf, ootRow_prf = np.mean([centroids[:, 0], centroids[:, 4]], axis=0), np.mean( [centroids[:, 1], centroids[:, 5]], axis=0) diffCol_prf, diffRow_prf = centroids[:, 2], centroids[:, 3] # itrCol_psf, itrRow_psf, ootCol_psf, ootRow_psf, diffCol_psf, diffRow_psf = psfCentroids_vbk(clip) diffC = (ootCol_prf - diffCol_prf) diffR = (ootRow_prf - diffRow_prf) itr_mean_img_, oot_mean_img_, diff_mean_img_, itr_mean_cube_, oot_mean_cube_, diff_mean_cube_, transit_number_ = generateImages( clip) itr_mean_img_, oot_mean_img_, diff_mean_img_ = np.asarray( itr_mean_img_), np.asarray(oot_mean_img_), np.asarray(diff_mean_img_) hdr_ = clip['serve.tpfHeader'] if clip['config.detrendType'] == "eleanor": col_zero_, row_zero_ = int(hdr_['CRPIX1']), int(hdr_['CRPIX2']) epic_Col, epic_Row = col_zero_ + int(hdr_['TPF_W']), row_zero_ + int( hdr_['TPF_H']) ss_ = itr_mean_img_.shape extent_ = [0, ss_[1], 0, ss_[0]] disp = lambda x: mp.imshow(x, cmap=parula_map, origin="bottom", interpolation="nearest", extent=extent_) disp(diff_mean_img_) mp.plot(np.mean(diffCol_prf), np.mean(diffRow_prf), 'r*', ms=12, mec='k', label="Mean Diff") mp.plot(diffCol_prf, diffRow_prf, 'r*', label="Indiv Diff") mp.plot(np.mean(ootCol_prf), np.mean(ootRow_prf), 'mo', label="OOT") covar.plotErrorEllipse(diffCol_prf, diffRow_prf, color='r', ms=14, marker='*', mfc='r') probOffset, chiSq = covar.computeProbabilityOfObservedOffset(diffC, diffR) titleStr = "Prob. On Target: %.1e; $\chi^2$: %.3f" % (1 - probOffset, chiSq) mp.xlabel("Column") mp.ylabel("Row") mp.legend(loc='best', fontsize=12) mp.title('Diff. Image; ' + titleStr, fontsize=12) return titleStr
def plotCentroidOffsets(centroids): """Plot the centroid offsets in column and row Inputs: ---------- centroids: (Nca) As stored in ``clip['diffImg.centroid_timeseries']`` Returns: ---------- A string containing the probability the measured centroids are consistent with no offset Output: ------------ A plot is added to the current figure. """ idx = centroids[:, 1] > 0 cin = centroids[idx, 0] ootCol = centroids[idx, 1] ootRow = centroids[idx, 2] itrCol = centroids[idx, 3] itrRow = centroids[idx, 4] diffC = -(ootCol - itrCol) diffR = -(ootRow - itrRow) mp.scatter(diffC, diffR, marker='o', c=cin, s=64, linewidths=0, \ cmap=mp.cm.RdYlBu) # mp.plot(diffC, diffR, 'ko', ms=10) mp.axhline(0, color='k', lw=.5) mp.axvline(0, color='k', lw=.5) mp.plot(0, 0, '*', ms=40, color='yellow') covar.plotErrorEllipse(diffC, diffR, color='#888888', ms=20) mp.xlabel(r"$\Delta$ Column (pixels)") mp.ylabel(r"$\Delta$ Row (pixels)") probOffset, chiSq = covar.computeProbabilityOfObservedOffset(diffC, diffR) titleStr = "Prob. On Target: %.1e: $\chi^2$: %.3f" % (1 - probOffset, chiSq) cb = mp.colorbar() cb.set_label("Cadence") #Time (BKJD)") # mp.tight_layout() #Ensure some padding around the origin so the symbol is never #at edge of the plot axl = list(mp.axis()) axl[0] = min(axl[0], -0.1) axl[1] = max(axl[1], +0.1) axl[2] = min(axl[2], -0.1) axl[3] = max(axl[3], +0.1) mp.axis(axl) return titleStr
def PLOT_CENTROIDS_TESS(clip): centroids = clip['diffImgCentroids.results'] ootCol_prf, ootRow_prf = np.mean([centroids[:, 0], centroids[:, 4]], axis=0), np.mean( [centroids[:, 1], centroids[:, 5]], axis=0) diffCol_prf, diffRow_prf = centroids[:, 2], centroids[:, 3] # itrCol_psf, itrRow_psf, ootCol_psf, ootRow_psf, diffCol_psf, diffRow_psf = psfCentroids_vbk(clip) diffC = (ootCol_prf - diffCol_prf) diffR = (ootRow_prf - diffRow_prf) itr_mean_img_, oot_mean_img_, diff_mean_img_, itr_mean_cube_, oot_mean_cube_, diff_mean_cube_, transit_number_ = generateImages( clip) itr_mean_img_, oot_mean_img_, diff_mean_img_ = np.asarray( itr_mean_img_), np.asarray(oot_mean_img_), np.asarray(diff_mean_img_) hdr_ = clip['serve.tpfHeader'] if clip['config.detrendType'] == "eleanor": col_zero_, row_zero_ = int(hdr_['CRPIX1']), int(hdr_['CRPIX2']) epic_Col, epic_Row = col_zero_ + int(hdr_['TPF_W']), row_zero_ + int( hdr_['TPF_H']) ss_ = itr_mean_img_.shape extent_ = [0, ss_[1], 0, ss_[0]] disp = lambda x: mp.imshow(x, cmap=parula_map, origin="bottom", interpolation="nearest", extent=extent_) mp.subplot(221) disp(diff_mean_img_) mp.plot(np.mean(diffCol_prf), np.mean(diffRow_prf), 'r*', ms=12, mec='k', label="Mean Diff") mp.plot(diffCol_prf, diffRow_prf, 'r*', label="Indiv Diff") mp.plot(np.mean(ootCol_prf), np.mean(ootRow_prf), 'mo', label="OOT") # for i in range(len(centroids)): # mp.text(centroids[i,2], centroids[i,3], ' %i' %(i)) covar.plotErrorEllipse(diffCol_prf, diffRow_prf, color='r', ms=14, marker='*', mfc='r') probOffset, chiSq = covar.computeProbabilityOfObservedOffset(diffC, diffR) titleStr = "Prob. On Target: %.1e; $\chi^2$: %.3f" % (1 - probOffset, chiSq) # mp.xlabel("Column") mp.ylabel("Row") mp.legend(loc='best', fontsize=10) mp.title('Diff. Image; ' + titleStr, fontsize=10) mp.subplot(222) disp(oot_mean_img_) mp.plot(ootCol_prf, ootRow_prf, 'mo', label="OOT") # mp.xlabel("Column") mp.ylabel("Row") mp.legend(loc='best', fontsize=10) mp.title('OOT', fontsize=10) mp.subplot(223) disp(itr_mean_img_) mp.xlabel("Column") mp.ylabel("Row") # mp.legend(loc = 'best') mp.subplot(224) tmp_diff_img_ = 0.01 + diff_mean_img_ - np.min(diff_mean_img_.flatten()) idx = np.isfinite(tmp_diff_img_) snr = np.empty_like(tmp_diff_img_) snr[idx] = tmp_diff_img_[idx] / np.sqrt(tmp_diff_img_[idx]) disp(snr) mp.colorbar() mp.title("Diff SNR", fontsize=10) mp.xlabel("Column") return titleStr
def PLOT_CENTROID_OFFSETS_VBK(clip): from astropy import coordinates, units as u, wcs from astroquery.skyview import SkyView from astroquery.vizier import Vizier import astropy.units as u import math from scipy.ndimage import rotate from reproject import reproject_interp, reproject_exact, reproject_to_healpix, reproject_from_healpix from astropy.wcs import WCS # import pywcsgrid2 time = clip['serve.time'] qFlags = clip['serve.flags'] flux = clip['detrend.flux_frac'] flags = clip['detrend.flags'] centroids = clip['diffImg.centroid_timeseries'] # rollPhase = clip['rollPhase.rollPhase'] period_days = clip['trapFit.period_days'] epoch_bkjd = clip['trapFit.epoch_bkjd'] duration_hrs = clip['trapFit.duration_hrs'] epic = clip['value'] cube = clip['serve.cube'] hdr_ = clip['serve.tpfHeader'] col_zero_, row_zero_ = int(hdr_['1CRV4P']), int(hdr_['2CRV4P']) epic_Col, epic_Row = col_zero_ + int(hdr_['1CRPX4']), row_zero_ + int( hdr_['2CRPX4']) def k2_ConvertHeaderWCS(tpf_header): funny_keywords = { '1CTYP4': 'CTYPE1', '2CTYP4': 'CTYPE2', '1CRPX4': 'CRPIX1', '2CRPX4': 'CRPIX2', '1CRVL4': 'CRVAL1', '2CRVL4': 'CRVAL2', '1CUNI4': 'CUNIT1', '2CUNI4': 'CUNIT2', '1CDLT4': 'CDELT1', '2CDLT4': 'CDELT2', '11PC4': 'PC1_1', '12PC4': 'PC1_2', '21PC4': 'PC2_1', '22PC4': 'PC2_2' } mywcs = {} for oldkey, newkey in funny_keywords.items(): mywcs[newkey] = tpf_header[oldkey] return wcs.WCS(mywcs) mywcs_ = k2_ConvertHeaderWCS(hdr_) # # inTransitIndices = kplrfits.markTransitCadences(time, period_days, epoch_bkjd, duration_hrs / 24., flags=flags) oot_cadence_ = np.where((inTransitIndices == False) & (flags == False)) oot_mean_img_ = np.nanmean(cube[oot_cadence_], axis=0) itr_cadence_ = np.where(inTransitIndices == True) itr_mean_img_ = np.nanmean(cube[itr_cadence_], axis=0) diff_mean_img_ = oot_mean_img_ - itr_mean_img_ ss_ = oot_mean_img_.shape # disp = lambda x: mp.imshow(x, cmap=mp.cm.binary, origin = "bottom", interpolation="nearest") extent_ = [col_zero_, col_zero_ + ss_[1], row_zero_, row_zero_ + ss_[0]] disp = lambda x: mp.imshow(x, cmap=mp.get_cmap('binary', 512), origin="bottom", interpolation="nearest", extent=extent_) # # GET CENTROIDS idx = centroids[:, 1] > 0 cin = centroids[idx, 0] ootCol = centroids[idx, 1] # - col_zero_# - 1 ootRow = centroids[idx, 2] # - row_zero_# - 1 #itr => in transit diffCol = centroids[idx, 3] # - col_zero_# - 1 diffRow = centroids[idx, 4] # - row_zero_# - 1 diffC = (ootCol - diffCol) # + np.median(diffCol) diffR = (ootRow - diffRow) # + np.median(diffRow) itrCol, itrRow = diffCol, diffRow # xmin_ = np.min(np.hstack((ootCol, diffCol))) xmax_ = np.max(np.hstack((ootCol, diffCol))) ymin_ = np.min(np.hstack((ootRow, diffRow))) ymax_ = np.max(np.hstack((ootRow, diffRow))) # START PLOTTING # ax1 = mp.subplot(221) disp(oot_mean_img_) ax1.plot(ootCol, ootRow, 'c*', ms=8) #, mec = 'm')#, color='yellow') ax1.plot(np.mean(ootCol), np.mean(ootRow), 'c*', ms=14, label='AVG_OOT') #, mec = 'm') ax1.plot(itrCol, itrRow, 'mo', ms=3) #, mec = 'c') ax1.plot(np.mean(itrCol), np.mean(itrRow), 'mo', ms=6, label='AVG_DIFF') #, mec = 'c') # covar.plotErrorEllipse(ootCol, ootRow, color='c', ms=14, marker='*', mfc='c') #, mec = 'm') covar.plotErrorEllipse(itrCol, itrRow, color='m', ms=14, marker='o', mfc='m') #, mec = 'c') ax1.plot(epic_Col, epic_Row, 'xy', mew=3, ms=10, label='EPIC') mp.xlabel(r"$\Delta$ Column (pixels)") mp.ylabel(r"$\Delta$ Row (pixels)") mp.legend(loc='best', fontsize=8) mp.subplot(222) mp.plot(np.mean(ootCol), np.mean(ootRow), 'c*', ms=20, label='OOT') mp.plot(np.mean(itrCol), np.mean(itrRow), 'mo', ms=20, label='DIFF') mp.scatter(ootCol, ootRow, marker='*', c=cin, s=64, linewidths=0, cmap=mp.cm.RdYlBu) mp.scatter(itrCol, itrRow, marker='o', c=cin, s=64, linewidths=0, cmap=mp.cm.RdYlBu) cb = mp.colorbar() cb.set_label("Cadence") covar.plotErrorEllipse(ootCol, ootRow, color='c', ms=20, marker='*', mfc='c') covar.plotErrorEllipse(itrCol, itrRow, color='m', ms=20, marker='o', mfc='m') mp.xlabel(r"$\Delta$ Column (pixels)") probOffset, chiSq = covar.computeProbabilityOfObservedOffset(diffC, diffR) titleStr = "Prob. On Target: %.1e: $\chi^2$: %.3f" % (1 - probOffset, chiSq) mp.legend(loc='best') mp.xlim(xmin_ - 0.2, xmax_ + 0.2) mp.ylim(ymin_ - 0.2, ymax_ + 0.2) mp.tight_layout() try: ax3 = mp.subplot(223, projection=mywcs_) #mywcs_) ra_, dec_ = hdr_['RA_OBJ'], hdr_['DEC_OBJ'] center_ = coordinates.SkyCoord(ra_, dec_, unit=(u.deg, u.deg), frame='icrs') img_survey = SkyView.get_images(position=center_, survey='2MASS-J', radius=1 * u.arcmin) pix_survey = img_survey[0][0].data hdr_survey = img_survey[0][0].header inverted_pix_survey = np.max(pix_survey) - pix_survey inverted_pix_survey = pix_survey #inverted_pix_survey/np.max(inverted_pix_survey) levels_ = np.linspace(np.min(inverted_pix_survey), np.percentile(inverted_pix_survey, 99), 10) ax3.contourf(inverted_pix_survey, transform=ax3.get_transform(wcs.WCS(hdr_survey)), levels=levels_, cmap=mp.get_cmap('binary', 256)) # # mp.tight_layout() except: mp.subplot(223) mp.subplot(224) titleStr_ = plotCentroidOffsets(centroids) return titleStr
def plotCentroidOffsets(centroids): """Plot the centroid offsets in column and row Inputs: ---------- centroids: (Nca) As stored in ``clip['diffImg.centroid_timeseries']`` Returns: ---------- A string containing the probability the measured centroids are consistent with no offset Output: ------------ A plot is added to the current figure. """ idx =centroids[:,1] > 0 cin = centroids[idx, 0] # ootCol = centroids[idx, 'diff_col'] # ootRow = centroids[idx, 'diff_row'] #Susan changed Nca names to column numbers because clipboards were not saving that information ootCol = centroids[idx, 3] ootRow = centroids[idx, 4] #itr => in transit # itrCol = centroids[idx, 'intr_col'] # itrRow = centroids[idx, 'intr_row'] itrCol = centroids[idx, 1] itrRow = centroids[idx, 2] diffC = ootCol - itrCol diffR = ootRow - itrRow mp.scatter(diffC, diffR, marker='o', c=cin, s=64, linewidths=0, \ cmap=mp.cm.RdYlBu) # mp.plot(diffC, diffR, 'ko', ms=10) mp.axhline(0, color='k', lw=.5) mp.axvline(0, color='k', lw=.5) mp.plot(0,0, '*', ms=40, color='yellow') covar.plotErrorEllipse(diffC, diffR, color='#888888', ms=20) mp.xlabel(r"$\Delta$ Column (pixels)") mp.ylabel(r"$\Delta$ Row (pixels)") probOffset, chiSq = covar.computeProbabilityOfObservedOffset(diffC, diffR) titleStr = "Prob. On Target: %.1e: $\chi^2$: %.3f" %(1-probOffset, chiSq) cb = mp.colorbar() cb.set_label("Time (BKJD)") #Ensure some padding around the origin so the symbol is never #at edge of the plot axl = list(mp.axis()) axl[0] = min(axl[0], -0.1) axl[1] = max(axl[1], +0.1) axl[2] = min(axl[2], -0.1) axl[3] = max(axl[3], +0.1) mp.axis(axl) return titleStr