def SDI_RDI_4_VIP(cube, angle_list, verbose, **kwargs): from vip_hci import pca wsamples = np.linspace(tp.band[0], tp.band[1], tp.w_bins) scale_list = tp.band[0]/wsamples # cube = np.mean(cube, axis=1)/ap.exposure_time SDI_tar = pca.pca(np.mean(cube, axis=1)/ap.exposure_time, angle_list=np.zeros((cube.shape[0])), scale_list=scale_list, mask_center_px=None) SDI_ref = pca.pca(np.mean(kwargs['cube_ref'], axis=1)/ap.exposure_time, angle_list=np.zeros((cube.shape[0])), scale_list=scale_list, mask_center_px=None) # quicklook_im(SDI) return SDI_tar-SDI_ref
def fit(self, dataset): pcs, cube_h, res, res_d, pf = pca(dataset.cube, dataset.angles, ncomp=self.rank, verbose=False, full_output=True) self.cube_h = cube_h
def SDI_4_VIP(cube, angle_list, verbose, **kwargs): dprint(cube.shape) wsamples = np.linspace(tp.band[0], tp.band[1], tp.w_bins) scale_list = tp.band[0]/wsamples cube = np.mean(cube, axis=1)/ap.exposure_time # SDI = phot.SDI_each_exposure(cube, binning=len(cube))[0] print(len(angle_list)) from vip_hci import pca SDI = pca.pca(cube, angle_list=np.zeros((len(cube))), scale_list=scale_list, mask_center_px=None) # quicklook_im(SDI) return SDI
def do_SDI(datacube, plot=False): wsamples = np.linspace(tp.band[0], tp.band[1], tp.w_bins) scale_list = tp.band[0] / wsamples # print scale_list from vip_hci import pca dprint((datacube.shape, scale_list.shape)) fr_pca1 = pca.pca(datacube, angle_list=np.zeros((len(scale_list))), scale_list=scale_list, mask_center_px=None) # fr_pca1 = fr_pca1[:,:-4] if plot: quicklook_im(fr_pca1) # dprint(fr_pca1.shape) return fr_pca1
def get_frame_snrmap(example_dataset_adi): """ Inject a fake companion into an example cube. Parameters ---------- example_dataset_adi : fixture Taken automatically from ``conftest.py``. Returns ------- frame : VIP Frame planet_position : tuple(y, x) """ dsi = copy.copy(example_dataset_adi) # we chose a shallow copy, as we will not use any in-place operations # (like +=). Using `deepcopy` would be safer, but consume more memory. print("producing a final frame...") res_frame = pca(dsi.cube, dsi.angles, ncomp=10) return res_frame, (63, 63), dsi.fwhm
# # annos= ['0.01s Exp.', '0.01s Dark', '0.1s Dark', '2s Dark', '2s Light', '2s Exp.'], # # vmins=[1,0.1,1,10,0.01,0.001], vmaxs=[100,1,10,100, 0.5,10], logAmp=True) # grid([Dmaps[0], Dmaps[1], Dmaps[2], Dmaps[3], Dmap, np.mean(diff_cube,axis=(0,1))], # annos= ['0.1s Exp.', '0.1s Dark', '1s Dark', '3s Dark', '3s DSI', '3s Exp.'], # vmins=[1,0.1,0.1,0.1,0.01,0.01], vmaxs=[1000,10,10,10, 100,100], logAmp=True) wsamples = np.linspace(tp.band[0], tp.band[1], tp.w_bins) scale_list = tp.band[0] / wsamples datacube = np.mean(diff_cube, axis=0) / ap.exposure_time # dprint(datacube.shape) # loop_frames(datacube) SDI = pca.pca(datacube, angle_list=np.zeros((len(diff_cube[0]))), scale_list=scale_list, mask_center_px=None) LCcube = np.transpose(diff_cube, (2, 3, 0, 1)) algo_dict = {'thresh': 0} # Lmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh'], plot=False, binning=1) # Lmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh'], plot=False, binning=2) # Lmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh'], plot=False, binning=5) Lmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh'], plot=False, binning=50) # Lmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh'], plot=False, binning=20) # rmap = Analysis.stats.get_skew(LCcube) BBmap = Analysis.stats.get_LmapBB(LCcube, binning=50, plot=False)
def pca_rebinned_cubes_old(self, comps=True): maps = [] if comps: for cam in self.metric.cams['comp']: dprint(cam.rebinned_cube.shape) SDI = pca.pca(cam.rebinned_cube, angle_list=np.zeros( (cam.rebinned_cube.shape[1])), scale_list=self.scale_list, mask_center_px=None, adimsdi='double', ncomp=self.ncomp, ncomp2=None, collapse='median') maps.append(SDI) return maps else: rad_samps, thruputs, noises, conts = [], [], [], [] for cam in self.metric.cams['star']: frame_nofc = pca.pca(cam.rebinned_cube, angle_list=np.zeros( (cam.rebinned_cube.shape[1])), scale_list=self.scale_list, mask_center_px=None, adimsdi='double', ncomp=7, ncomp2=None, collapse='median') # quick2D(frame_nofc, logZ=False, title='frame_nofc', show=True) fwhm = cam.lod if hasattr(cam, 'lod') else mp.lod mask = cam.QE_map == 0 noise_samp, rad_samp = contrcurve.noise_per_annulus( frame_nofc, separation=1, fwhm=fwhm, mask=mask) if metric_name == 'array_size': _, vector_radd = contrcurve.noise_per_annulus( frame_nofc, separation=fwhm, fwhm=fwhm, wedge=(0, 360), mask=mask) else: vector_radd = self.vector_radd # crop the noise and radial sampling measurements the limits of the throughput measurement radmin = vector_radd.astype(int).min() cutin1 = np.where(rad_samp.astype(int) == radmin)[0][0] noise_samp = noise_samp[cutin1:] rad_samp = rad_samp[cutin1:] radmax = vector_radd.astype(int).max() cutin2 = np.where(rad_samp.astype(int) == radmax)[0][0] noise_samp = noise_samp[:cutin2 + 1] rad_samp = rad_samp[:cutin2 + 1] if metric_name == 'array_size': throughput = np.interp( rad_samp * cam.platescale, self.vector_radd.values * self.master_cam.platescale, self.throughput) # elif metric_name == 'dark_bright': # throughput = self.manual_throughput(cam.rebinned_cube, frame_nofc) else: throughput = self.throughput win = min(noise_samp.shape[0] - 2, int(2 * fwhm)) if win % 2 == 0: win += 1 noise_samp_sm = savgol_filter(noise_samp, polyorder=2, mode='nearest', window_length=win) starphot = 1.1 sigma = 5 cont_curve_samp = ( (sigma * noise_samp_sm) / throughput) / starphot cont_curve_samp[cont_curve_samp < 0] = 1 cont_curve_samp[cont_curve_samp > 1] = 1 thruputs.append(throughput) noises.append(noise_samp_sm) conts.append(cont_curve_samp) rad_samp = cam.platescale * rad_samp rad_samps.append(rad_samp) maps.append(frame_nofc) return maps, rad_samps, thruputs, noises, conts
def pca_rebinned_cubes(self): maps, rad_samps, thruputs, noises, conts = [], [], [], [], [] cams = self.metric.cams colors = [f'C{i}' for i in range(len(self.metric.cams['star']))] # fig, axes = plt.subplots(1,6) fig, axes = plt.subplots(1, 3) dprint(np.shape(axes)) for i in range(len(cams['comp'])): comp_cube = cams['comp'][i].rebinned_cube dprint(comp_cube.shape) frame_comp = pca.pca(comp_cube, angle_list=np.zeros((comp_cube.shape[1])), scale_list=self.scale_list, mask_center_px=None, adimsdi='double', ncomp=self.ncomp, ncomp2=None, collapse=self.collapse) maps.append(frame_comp) nocomp_cube = cams['star'][i].rebinned_cube frame_nocomp = pca.pca(nocomp_cube, angle_list=np.zeros((nocomp_cube.shape[1])), scale_list=self.scale_list, mask_center_px=None, adimsdi='double', ncomp=self.ncomp, ncomp2=None, collapse=self.collapse) median_fwhm = cams['star'][i].lod if hasattr( cams['star'][i], 'lod') else mp.lod median_wave = (self.wsamples[-1] + self.wsamples[0]) / 2 fwhm = median_fwhm * self.wsamples / median_wave mask = cams['star'][i].QE_map == 0 print('check whether to be using mask here?!') noise_samp, rad_samp = contrcurve.noise_per_annulus( frame_nocomp, separation=1, fwhm=median_fwhm) # mask=mask) xy = np.array( ap.companion_xy ) * 20 * cams['star'][i].sampling / cams['star'][i].platescale px, py = (cams['star'][i].array_size[::-1] / 2 - xy).T cx, cy = cams['star'][i].array_size[::-1] / 2 dists = np.sqrt((py - cy)**2 + (px - cx)**2) # grid(np.sum(comp_cube, axis=1), title='in comp time collapse', show=False) # injected_flux=[contrcurve.aperture_flux(np.sum(comp_cube-nocomp_cube, axis=1)[i], py, px, fwhm[i]/1.5, ap_factor=1) for i in range(comp_cube.shape[0])] injected_flux = [ contrcurve.aperture_flux(np.sum(comp_cube, axis=1)[i], py, px, fwhm[i] / 1.5, ap_factor=1, plot=False) for i in range(comp_cube.shape[0]) ] # [axes[i+3].plot(dists, influx_wave) for influx_wave in injected_flux] injected_flux = np.mean(injected_flux, axis=0) # if i ==0 or i == len(self.metric.cams['star'])-1: # grid(comp_cube, title='comp', show=False) # grid(nocomp_cube, title='no', show=False) # grid(comp_cube-nocomp_cube, title='diff', show=False) # grid([np.sum(comp_cube, axis=(0,1))], title='sum comp', logZ=True, show=False) # grid([np.sum(nocomp_cube, axis=(0,1))], title='sum nocomp', logZ=True, show=False) # grid([np.sum(comp_cube, axis=(0,1))-np.sum(nocomp_cube, axis=(0,1))], title='sum then diff', logZ=True, show=False) # grid([], title='comp', logZ=True, show=False) grid([frame_comp, frame_nocomp, frame_comp - frame_nocomp], title='comp, nocomp, diff', logZ=False, show=False, vlim=(-2e-7, 2e-7)) # vlim=(-2,2))#, vlim=(-2e-7,2e-7)) grid([ np.sum(comp_cube, axis=1)[::4], np.sum(nocomp_cube, axis=1)[::4] ], title='input: comp, no comp', logZ=False, show=False, vlim=(-1e-5, 1e-5)) # vlim=(-2,2))#, vlim=(-1e-5,1e-5)) grid(np.sum(comp_cube - nocomp_cube, axis=1)[::4], title='diiff input cube', logZ=False, show=False) #, vlim=(-2,2)) # grid([(frame_comp-frame_nocomp)/(np.sum(comp_cube-nocomp_cube, axis=(0,1)))], title='throughput', logZ=True, show=False) # recovered_flux = contrcurve.aperture_flux((frame_comp - frame_nocomp), py, px, median_fwhm/1.5, ap_factor=1, ) recovered_flux = contrcurve.aperture_flux((frame_comp), py, px, median_fwhm / 1.5, ap_factor=1, plot=False) # thruput = recovered_flux*1e6 #/ injected_flux thruput = recovered_flux / injected_flux thruput[np.where(thruput < 0)] = 0 # plt.figure() axes[0].plot(dists, thruput, c=colors[i]) axes[1].plot(dists, injected_flux, c=colors[i]) axes[2].plot(dists, recovered_flux, c=colors[i], label=f'{self.metric.vals[i]}') axes[2].legend() # plt.plot(rad_samp, noise_samp) # plt.show() win = min(noise_samp.shape[0] - 2, int(2 * median_fwhm)) if win % 2 == 0: win += 1 noise_samp_sm = savgol_filter(noise_samp, polyorder=2, mode='nearest', window_length=win) # thruput_mean_log = np.log10(thruput + 1e-5) # f = InterpolatedUnivariateSpline(dists, thruput_mean_log, k=2) # thruput_interp_log = f(rad_samp) # thruput_interp = 10 ** thruput_interp_log # thruput_interp[thruput_interp <= 0] = np.nan # 1e-5 # thruput_interp = np.interp(rad_samp, dists, thruput) from scipy.interpolate import interp1d # f = interp1d(dists, thruput, fill_value='extrapolate') # thruput_interp = f(rad_samp) thruput_com = thruput.reshape(4, -1, order='F').mean(axis=0) dists_com = dists.reshape(4, -1, order='F').mean(axis=0) thruput_com_log = np.log10(thruput_com + 1e-5) f = interp1d(dists_com, thruput_com_log, fill_value='extrapolate') thruput_interp_log = f(rad_samp) thruput_interp = 10**thruput_interp_log axes[0].plot(dists_com, thruput_com, marker='o', c=colors[i]) print(thruput, thruput_interp) axes[0].plot(rad_samp, thruput_interp, c=colors[i]) starphot = 1.1 / 2 sigma = 5 cont_curve_samp = ( (sigma * noise_samp_sm) / thruput_interp) / starphot cont_curve_samp[cont_curve_samp < 0] = 1 cont_curve_samp[cont_curve_samp > 1] = 1 thruputs.append(thruput_interp) noises.append(noise_samp_sm) conts.append(cont_curve_samp) rad_samp = cams['star'][i].platescale * rad_samp rad_samps.append(rad_samp) # maps.append(frame_nocomp) plt.show(block=True) return maps, rad_samps, thruputs, noises, conts
Dmap = Analysis.stats.get_Dmap(LCcube, algo_dict['thresh']) # DSI indep_images([Dmap], vmins=[0.01], vmaxs=[0.5], logAmp=True) #SDI +DSI iop.hyperFile = iop.datadir + '/noWnoRollHyperWcomp1000cont_Aug_1st.pkl' #5 simple_hypercube = read.get_integ_hypercube(plot=False) #/ap.numframes wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) # scale_list = tp.band[0] / wsamples scale_list = wsamples / tp.band[0] angle_list = np.zeros((tp.nwsamp)) print np.mean(simple_hypercube, axis=0).shape static_psf = pca.pca(np.mean(simple_hypercube, axis=0), angle_list=angle_list, scale_list=scale_list, mask_center_px=None, full_output=True) # quicklook_im(pca.pca(np.mean(simple_hypercube,axis=0), angle_list=angle_list, scale_list=scale_list[::-1], # mask_center_px=None)) quicklook_im(np.sum(simple_hypercube, axis=(0, 1))) loop_frames(np.sum(simple_hypercube, axis=0)) # scale_list = np.linspace(scale_list[-1],scale_list[0],8) scale_list = tp.band[1] / wsamples # scale_list = scale_list[::-1] print scale_list # loop_frames(static_psf[0], logAmp=False) # loop_frames(static_psf[1], logAmp=False) # loop_frames(static_psf[2], logAmp=False) # loop_frames(static_psf[3], logAmp=False)
def postprocessing(self, do_adi=True, do_adi_contrast=True, do_pca_full=True, do_pca_ann=True, cropped=True, do_snr_map=True, do_snr_map_opt=True, delta_rot=(0.5, 3), mask_IWA=1, overwrite=True, plot=True, verbose=True, debug=False): """ For post processing the master cube via median ADI, full frame PCA-ADI, or annular PCA-ADI. Includes constrast curves and SNR maps. Parameters: *********** do_adi : bool Whether to do a median-ADI processing do_adi_contrast : bool Whether to compute contrast curve associated to median-ADI do_pca_full : bool Whether to apply PCA-ADI on full frame do_pca_ann : bool, default is False Whether to apply annular PCA-ADI (more computer intensive). Only runs if cropped=True cropped : bool whether the master cube was cropped in pre-processing do_snr_map : bool whether to compute an SNR map (warning: computer intensive); useful only when point-like features are seen in the image do_snr_map_opt : bool Whether to compute a non-conventional (more realistic) SNR map delta_rot : tuple Threshold in rotation angle used in pca_annular to include frames in the PCA library (provided in terms of FWHM). See description of pca_annular() for more details mask_IWA : int, default 1 Size of the numerical mask that hides the inner part of post-processed images. Provided in terms of fwhm overwrite : bool, default True whether to overwrite pre-exisiting output files from previous reductions plot : bool Whether to save plots to the output path (PDF file, print quality) verbose : bool prints more output when True debug : bool, default is False Saves extra output files """ # make directories if they don't exist print("======= Starting post-processing....=======") outpath_sub = self.outpath + "sub_npc{}/".format(self.npc) if not isdir(outpath_sub): os.system("mkdir " + outpath_sub) if verbose: print('Input path is {}'.format(self.inpath)) print('Output path is {}'.format(outpath_sub)) source = self.dataset_dict['source'] tn_shift = 0.568 # Launhardt et al. 2020, true North offset for NACO ADI_cube_name = '{}_master_cube.fits' # template name for input master cube derot_ang_name = 'derot_angles.fits' # template name for corresponding input derotation angles ADI_cube = open_fits(self.inpath + ADI_cube_name.format(source), verbose=verbose) derot_angles = open_fits(self.inpath + derot_ang_name, verbose=verbose) + tn_shift if do_adi_contrast: psfn_name = "master_unsat_psf_norm.fits" # normalised PSF flux_psf_name = "master_unsat-stellarpsf_fluxes.fits" # flux in a FWHM aperture found in calibration psfn = open_fits(self.inpath + psfn_name, verbose=verbose) starphot = open_fits(self.inpath + flux_psf_name, verbose=verbose)[1] # scaled fwhm flux is the second entry mask_IWA_px = mask_IWA * self.fwhm if verbose: print("adopted mask size: {:.0f}".format(mask_IWA_px)) ann_sz = 3 # if PCA-ADI in a single annulus or in concentric annuli, this is the size of the annulus/i in FWHM svd_mode = 'lapack' # python package used for Singular Value Decomposition for PCA reductions n_randsvd = 3 # if svd package is set to 'randsvd' number of times we do PCA rand-svd, before taking the # median of all results (there is a risk of significant self-subtraction when just doing it once) ref_cube = None # if any, load here a centered calibrated cube of reference star observations - would then be # used for PCA instead of the SCI cube itself # TEST number of principal components # PCA-FULL if do_pca_full: test_pcs_full = list(range(1, self.npc + 1)) # PCA-ANN if do_pca_ann: test_pcs_ann = list(range(1, self.npc + 1)) # needs a cropped cube ######################### Simple ADI ########################### if do_adi: if not isfile(outpath_sub + 'final_ADI_simple.fits') or overwrite: if debug: # saves the residuals tmp, _, tmp_tmp = median_sub(ADI_cube, derot_angles, fwhm=self.fwhm, radius_int=0, asize=ann_sz, delta_rot=delta_rot, full_output=debug, verbose=verbose) tmp = mask_circle(tmp, mask_IWA_px) write_fits(outpath_sub + 'TMP_ADI_simple_cube_der.fits', tmp, verbose=verbose) # make median combination of the de-rotated cube. tmp_tmp = median_sub(ADI_cube, derot_angles, fwhm=self.fwhm, radius_int=0, asize=ann_sz, delta_rot=delta_rot, full_output=False, verbose=verbose) tmp_tmp = mask_circle(tmp_tmp, mask_IWA_px) # we mask the IWA write_fits(outpath_sub + 'final_ADI_simple.fits', tmp_tmp, verbose=verbose) ## SNR map if (not isfile(outpath_sub + 'final_ADI_simple_snrmap.fits') or overwrite) and do_snr_map: tmp = open_fits(outpath_sub + 'final_ADI_simple.fits', verbose=verbose) tmp = mask_circle(tmp, mask_IWA_px) tmp_tmp = snrmap(tmp, self.fwhm, nproc=self.nproc, verbose=debug) write_fits(outpath_sub + 'final_ADI_simple_snrmap.fits', tmp_tmp, verbose=verbose) ## Contrast curve if (not isfile(outpath_sub + 'contrast_adi.pdf') or overwrite) and do_adi_contrast: _ = contrast_curve(ADI_cube, derot_angles, psfn, self.fwhm, pxscale=self.pixel_scale, starphot=starphot, algo=median_sub, sigma=5., nbranch=1, theta=0, inner_rad=1, wedge=(0, 360), student=True, transmission=None, smooth=True, plot=plot, dpi=300, debug=debug, save_plot=outpath_sub + 'contrast_adi.pdf', verbose=verbose) if verbose: print("======= Completed Median-ADI =======") ####################### PCA-ADI full ########################### if do_pca_full: test_pcs_str_list = [str(x) for x in test_pcs_full] ntest_pcs = len(test_pcs_full) test_pcs_str = "npc" + "-".join(test_pcs_str_list) PCA_ADI_cube = ADI_cube.copy() tmp_tmp = np.zeros([ntest_pcs, PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) if do_snr_map_opt: tmp_tmp_tmp_tmp = np.zeros([ntest_pcs, PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) for pp, npc in enumerate(test_pcs_full): if svd_mode == 'randsvd': tmp_tmp_tmp = np.zeros([n_randsvd, PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) for nr in range(n_randsvd): tmp_tmp_tmp[nr] = pca(PCA_ADI_cube, angle_list=derot_angles, cube_ref=ref_cube, scale_list=None, ncomp=int(npc), svd_mode='randsvd', scaling=None, mask_center_px=mask_IWA_px, delta_rot=delta_rot, fwhm=self.fwhm, collapse='median', check_memory=True, full_output=False, verbose=verbose) tmp_tmp[pp] = np.median(tmp_tmp_tmp, axis=0) else: if not isfile(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '.fits') or overwrite: tmp_tmp[pp] = pca(PCA_ADI_cube, angle_list=derot_angles, cube_ref=ref_cube, scale_list=None, ncomp=int(npc), svd_mode=svd_mode, scaling=None, mask_center_px=mask_IWA_px, delta_rot=delta_rot, fwhm=self.fwhm, collapse='median', check_memory=True, full_output=False, verbose=verbose) if (not isfile(outpath_sub + 'final_PCA-ADI_full_' +test_pcs_str+'_snrmap_opt.fits') or overwrite) \ and do_snr_map_opt: tmp_tmp_tmp_tmp[pp] = pca(PCA_ADI_cube, angle_list=-derot_angles, cube_ref=ref_cube, scale_list=None, ncomp=int(npc), svd_mode=svd_mode, scaling=None, mask_center_px=mask_IWA_px, delta_rot=delta_rot, fwhm=self.fwhm, collapse='median', check_memory=True, full_output=False, verbose=verbose) if not isfile(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '.fits') or overwrite: write_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '.fits', tmp_tmp, verbose=verbose) if (not isfile(outpath_sub + 'neg_PCA-ADI_full_' + test_pcs_str + '.fits') or overwrite) and do_snr_map_opt: write_fits(outpath_sub + 'neg_PCA-ADI_full_' + test_pcs_str + '.fits', tmp_tmp_tmp_tmp, verbose=verbose) ### Convolution if not isfile(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '_conv.fits') or overwrite: tmp = open_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '.fits', verbose=verbose) for nn in range(tmp.shape[0]): tmp[nn] = frame_filter_lowpass(tmp[nn], fwhm_size=self.fwhm, gauss_mode='conv') write_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '_conv.fits', tmp, verbose=verbose) ### SNR map if (not isfile(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '_snrmap.fits') or overwrite) \ and do_snr_map: tmp = open_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '.fits', verbose=verbose) for pp in range(ntest_pcs): tmp[pp] = snrmap(tmp[pp], self.fwhm, nproc=self.nproc, verbose=debug) tmp[pp] = mask_circle(tmp[pp], mask_IWA_px) write_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '_snrmap.fits', tmp, verbose=verbose) ### SNR map optimized if (not isfile(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '_snrmap_opt.fits') or overwrite) \ and do_snr_map_opt: tmp = open_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '.fits', verbose=verbose) for pp in range(ntest_pcs): tmp[pp] = snrmap(tmp[pp], self.fwhm, array2=tmp_tmp_tmp_tmp[pp], incl_neg_lobes=False, nproc=self.nproc, verbose=debug) tmp[pp] = mask_circle(tmp[pp], mask_IWA_px) write_fits(outpath_sub + 'final_PCA-ADI_full_' + test_pcs_str + '_snrmap_opt.fits', tmp, verbose=verbose) if verbose: print("======= Completed PCA Full Frame =======") ######################## PCA-ADI annular ####################### if do_pca_ann: if cropped == False: raise ValueError('PCA-ADI annular requires a cropped cube!') PCA_ADI_cube = ADI_cube.copy() del ADI_cube test_pcs_str_list = [str(x) for x in test_pcs_ann] ntest_pcs = len(test_pcs_ann) test_pcs_str = "npc" + "-".join(test_pcs_str_list) tmp_tmp = np.zeros([ntest_pcs, PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) if debug: array_der = np.zeros([ntest_pcs, PCA_ADI_cube.shape[0], PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) array_out = np.zeros([ntest_pcs, PCA_ADI_cube.shape[0], PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) if do_snr_map_opt: tmp_tmp_tmp_tmp = np.zeros([ntest_pcs, PCA_ADI_cube.shape[1], PCA_ADI_cube.shape[2]]) for pp, npc in enumerate(test_pcs_ann): if debug and ((not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_residuals.fits') and not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_residuals-derot.fits')) or overwrite): # saves residuals and median if debug is true and they either dont exist or are to be overwritten array_out[pp], array_der[pp], tmp_tmp[pp] = pca_annular(PCA_ADI_cube, derot_angles, cube_ref=ref_cube, scale_list=None, radius_int=mask_IWA_px, fwhm=self.fwhm, asize=ann_sz * self.fwhm, n_segments=1, delta_rot=delta_rot, ncomp=int(npc), svd_mode=svd_mode, nproc=self.nproc, min_frames_lib=max(npc, 10), max_frames_lib=200, tol=1e-1, scaling=None, imlib='opencv', interpolation='lanczos4', collapse='median', ifs_collapse_range='all', full_output=debug, verbose=verbose) else: if not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '.fits') or overwrite: tmp_tmp[pp] = pca_annular(PCA_ADI_cube, derot_angles, cube_ref=ref_cube, scale_list=None, radius_int=mask_IWA_px, fwhm=self.fwhm, asize=ann_sz * self.fwhm, n_segments=1, delta_rot=delta_rot, ncomp=int(npc), svd_mode=svd_mode, nproc=self.nproc, min_frames_lib=max(npc, 10), max_frames_lib=200, tol=1e-1, scaling=None, imlib='opencv', interpolation='lanczos4', collapse='median', ifs_collapse_range='all', full_output=False, verbose=verbose) if (not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_snrmap_opt.fits') or overwrite) and do_snr_map_opt: tmp_tmp_tmp_tmp[pp] = pca_annular(PCA_ADI_cube, -derot_angles, cube_ref=ref_cube, scale_list=None, radius_int=mask_IWA_px, fwhm=self.fwhm, asize=ann_sz * self.fwhm, n_segments=1, delta_rot=delta_rot, ncomp=int(npc), svd_mode=svd_mode, nproc=self.nproc, min_frames_lib=max(npc, 10), max_frames_lib=200, tol=1e-1, scaling=None, imlib='opencv', interpolation='lanczos4', collapse='median', ifs_collapse_range='all', full_output=False, verbose=verbose) if not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '.fits') or overwrite: write_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '.fits', tmp_tmp, verbose=verbose) if debug and ((not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_residuals.fits') and not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_residuals-derot.fits')) or overwrite): write_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_residuals.fits', array_out, verbose=verbose) write_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_residuals-derot.fits', array_der, verbose=verbose) if (not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_snrmap_opt.fits') or overwrite) and do_snr_map_opt: write_fits(outpath_sub + 'neg_PCA-ADI_ann_' + test_pcs_str + '.fits', tmp_tmp_tmp_tmp, verbose=verbose) ### Convolution if not isfile(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_conv.fits') or overwrite: tmp = open_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '.fits', verbose=verbose) for nn in range(tmp.shape[0]): tmp[nn] = frame_filter_lowpass(tmp[nn], fwhm_size=self.fwhm, gauss_mode='conv') write_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_conv.fits', tmp, verbose=verbose) ### SNR map if (not isfile( outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_snrmap.fits') or overwrite) and do_snr_map: tmp = open_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '.fits', verbose=verbose) for pp in range(ntest_pcs): tmp[pp] = snrmap(tmp[pp], self.fwhm, nproc=self.nproc, verbose=debug) tmp[pp] = mask_circle(tmp[pp], mask_IWA_px) write_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_snrmap.fits', tmp, verbose=verbose) ### SNR map optimized if (not isfile( outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_snrmap_opt.fits') or overwrite) and do_snr_map_opt: tmp = open_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '.fits', verbose=verbose) for pp in range(ntest_pcs): tmp[pp] = snrmap(tmp[pp], self.fwhm, plot=plot, array2=tmp_tmp_tmp_tmp[pp], nproc=self.nproc, verbose=debug) tmp[pp] = mask_circle(tmp[pp], mask_IWA_px) write_fits(outpath_sub + 'final_PCA-ADI_ann_' + test_pcs_str + '_snrmap_opt.fits', tmp, verbose=verbose) if verbose: print("======= Completed PCA Annular =======")
# for image in images[3:]: # print np.shape(image) # for imag in image: # quicklook_im(imag, logAmp=False, show=True, vmin= 1) # # print image[::50] wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) scale_list = tp.band[0] / wsamples print wsamples, tp.band, scale_list from Detector.analysis import make_SNR_plot, make_cont_plot from vip_hci import pca SDIs = [] for i in range(4): SDI = pca.pca(images[i], angle_list=np.zeros((len(images[i]))), scale_list=scale_list[::-1]**-1, mask_center_px=None) SDIs.append(SDI) SDIs = np.array(SDIs) all_curves = np.vstack((images[:, 4], SDIs)) for i in range(len(all_curves)): quicklook_im(all_curves[i], vmin=1) # make_cont_plot(SDIs, ['Stack', 'Med', 'Short', 'Ratio']) # make_cont_plot(images[:,0], ['Stack', 'Med', 'Short', 'Ratio']) make_cont_plot(all_curves, [ 'J-Stack', 'J-Median', 'J-Lucky', 'J-SSD', 'SDI-Stack', 'SDI-Median', 'SDI-Lucky', 'SDI-SSD' ]) # norms = [1,2000,20,1,1,2000,20,1] plt.show() # mp.obsfile = occultdirs[1] + '/' + 'r0varyObsfile_piston_colors.h5' # LCmapFile = os.path.join(mp.rootdir, mp.proc_dir, mp.date, occultdirs[1], 'LCvaryR0_piston_colors.pkl')
# plt.show() # phot.contrcurve.contrast_curve(cube=hypercube[0], angle_list=np.zeros((len(hypercube[0]))), psf_template=psf_template, fwhm=10, pxscale=0.13, starphot=star_phots, algo=pca.pca, debug=True, **algo_dict) # from vip_hci import pca # loop_frames(hypercube[:,0]) # hypercube[:,:,56,124] = 0 # hypercube[:,:,119,104] = 0 datacube = np.mean(hypercube, axis=0) / ap.exposure_time # datacube = np.transpose(np.transpose(datacube) / np.max(datacube, axis=(1, 2))) / float(tp.nwsamp) dprint(np.sum(datacube, axis=(1, 2))) loop_frames(datacube) dprint(scale_list) SDI = pca.pca(datacube, angle_list=np.zeros((len(hypercube[0]))), scale_list=scale_list, mask_center_px=None) # dprint(SDI.shape) print hypercube.shape # SDI[SDI<=0] = 1e-5 # quicklook_im(np.sum(hypercube, axis=(0,1)), logAmp=True) # quicklook_im(SDI, logAmp=True) SDI = SDI.reshape(1, SDI.shape[0], SDI.shape[1]) # wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) # scale_list = wsamples/tp.band[0] dprint(scale_list) print scale_list, scale_list[-1], 'SL', 1. / scale_list[-1] wframe = clipped_zoom(datacube[0], scale_list[0]) # quicklook_im(wframe)
phot.contrcurve.contrast_curve( cube=hypercube[:, 0], angle_list=-1 * np.arange(0, num_exp * tp.rot_rate * cp.frame_time, tp.rot_rate * cp.frame_time), psf_template=psf_template, fwhm=10, pxscale=0.13, starphot=star_phot, algo=pca.pca, debug=True) # from vip_hci import pca # loop_frames(hypercube[:,0]) ADI = pca.pca( hypercube[:, 0], angle_list=-1 * np.arange(0, num_exp * tp.rot_rate * cp.frame_time, tp.rot_rate * cp.frame_time) ) #, scale_list=np.ones((len(hypercube[:,0]))), mask_center_px=None) quicklook_im(ADI) ADI = ADI.reshape(1, 128, 128) wsamples = np.linspace(tp.band[0], tp.band[1], tp.nwsamp) scale_list = tp.band[0] / wsamples print scale_list, scale_list[-1], 'SL', 1. / scale_list[-1] wframe = clipped_zoom(hypercube[0, 0], 1. / scale_list[-1]) quicklook_im(wframe) wframe = wframe.reshape(1, 128, 128) cube = np.vstack((hypercube[0, [0, -1]], wframe, SDI)) # cube = np.dstack((hypercube[0,0],wframe,hypercube[0,-1],SDI)).transpose() annos = [
def get_reduced_images(ind=1, use_spec=True, plot=False, use_pca=True, verbose=False): """ Looks for reduced images file in the home folder and returns if it exists. If you're on the local machine and the file has not been transferred it will throw a FileNotFoundError """ all_photons, star_photons, planet_photons = get_photons(amount=-ind) all_tess, star_tess, planet_tess = ( get_tess(photons) for photons in [all_photons, star_photons, planet_photons]) all_tess = np.transpose(all_tess, axes=(1, 0, 2, 3)) star_tess = np.transpose(star_tess, axes=(1, 0, 2, 3)) planet_tess = np.transpose(planet_tess, axes=(1, 0, 2, 3)) all_raw = np.sum(all_tess, axis=(0, 1)) star_raw = np.sum(star_tess, axis=(0, 1)) planet_raw = np.sum(planet_tess, axis=(0, 1)) sp.numframes = 80 sp.sample_time = 15 angle_list = -np.linspace( 0, sp.numframes * sp.sample_time * config['data']['rot_rate'] / 60, all_tess.shape[1]) print(angle_list, 'angle') if use_spec: wsamples = np.linspace(ap.wvl_range[0], ap.wvl_range[1], all_tess.shape[0]) scale_list = wsamples / (ap.wvl_range[1] - ap.wvl_range[0]) all_pca = pca.pca(all_tess, angle_list=angle_list, scale_list=scale_list, mask_center_px=None, adimsdi='double', ncomp=(None, 1), collapse='sum', verbose=verbose) star_pca = pca.pca(star_tess, angle_list=angle_list, scale_list=scale_list, mask_center_px=None, adimsdi='double', ncomp=(None, 1), collapse='sum', verbose=verbose) planet_pca = pca.pca(planet_tess, angle_list=angle_list, scale_list=scale_list, mask_center_px=None, adimsdi='double', ncomp=(None, 1), collapse='sum', verbose=verbose) # reduced_images = np.array([[all_raw, star_raw, planet_raw], [all_pca, star_pca, planet_pca]]) all_med = medsub_source.median_sub(all_tess, angle_list=angle_list, scale_list=scale_list, collapse='sum') star_med = medsub_source.median_sub(star_tess, angle_list=angle_list, scale_list=scale_list, collapse='sum') planet_med = medsub_source.median_sub(planet_tess, angle_list=angle_list, scale_list=scale_list, collapse='sum') else: pass # all_med = medsub_source.median_sub(np.sum(all_tess, axis=0), angle_list=angle_list, collapse='sum') # star_med = medsub_source.median_sub(np.sum(star_tess, axis=0), angle_list=angle_list, collapse='sum') # planet_med = medsub_source.median_sub(np.sum(planet_tess, axis=0), angle_list=angle_list, # collapse='sum') all_derot = np.sum(cube_derotate(np.sum(all_tess, axis=0), angle_list, imlib='opencv', interpolation='lanczos4'), axis=0) star_derot = np.sum(cube_derotate(np.sum(star_tess, axis=0), angle_list, imlib='opencv', interpolation='lanczos4'), axis=0) planet_derot = np.sum(cube_derotate(np.sum(planet_tess, axis=0), angle_list, imlib='opencv', interpolation='lanczos4'), axis=0) # all_snr = snrmap(all_pca, fwhm=5, plot=True) # star_snr = snrmap(star_pca, fwhm=5, plot=True) # planet_snr = snrmap(planet_pca, fwhm=5, plot=True) reduced_images = np.array([[all_raw, star_raw, planet_raw], [all_derot, star_derot, planet_derot], [all_pca, star_pca, planet_pca], [all_med, star_med, planet_med] ]) #, ) # [all_snr, star_snr, planet_snr] planet_loc = (92, 60) fwhm = config['data']['fwhm'] nproc = 8 # reduced_images = np.array([[all_med, snrmap(all_med, fwhm, known_sources=planet_loc, nproc=nproc)], # [all_pca, snrmap(all_pca, fwhm, known_sources=planet_loc, nproc=nproc)], # [planet_derot, snrmap(planet_derot, fwhm, known_sources=planet_loc, nproc=nproc)], # [planet_pca, snrmap(planet_pca, fwhm, known_sources=planet_loc, nproc=nproc)]]) # plt.imshow(planet_derot) # plt.imshow(snrmap(planet_derot, fwhm, nproc=nproc)) # plt.show() # reduced_images = np.array([[all_raw, star_raw, planet_raw]]) # grid(reduced_images, logZ=True, vlim=(1,50)) #, vlim=(1,70) if plot: grid(reduced_images, vlim=(-10, 106)) return reduced_images