l_idx = [i for i in range(len(bands)) if bands[i] == lband][0] #The first index to run run_list = [i for i in range(len(bands))] del(run_list[l_idx]) run_list = [l_idx] + run_list #The list define the order to run cut_radius = np.median([int(len(data_process_list[i].target_stamp)/2) for i in range(len(data_process_list))]) for i in range(len(bands)): data_process_list[i].generate_target_materials(radius=cut_radius, create_mask = False, nsigma=2.8, #detect_tool='sep', exp_sz= 1.2, npixels = 15, if_plot=False) data_process_list[i].checkout() apertures = data_process_list[l_idx].apertures from galight.tools.measure_tools import mask_obj for i in run_list: if i != l_idx: covers = mask_obj(data_process_list[i].target_stamp, apertures, if_plot=False, sum_mask = True) for j in range(len(data_process_list[i].apertures)): new_cover = mask_obj(data_process_list[i].target_stamp, [data_process_list[i].apertures[j]], if_plot=False, sum_mask = True) if np.sum(covers - new_cover*covers) > np.sum(1-new_cover)/2 : #If 1/2 of the area covered by the aperture is new) apertures.append(data_process_list[i].apertures[j]) fit_sepc_l, fit_run_l = [None]*5, [None]*5 for i in run_list: band = bands[i] print("Staring fitting band-"+band+"... ... ...") data_process_list[i].apertures = apertures #Pass apertures to the data fit_sepc_l[i] = FittingSpecify(data_process_list[i]) fix_n_list, fix_Re_list = None, None if i != l_idx: if fix_n == True: fix_n_list = [[0,fit_run_l[l_idx].final_result_galaxy[0]['n_sersic'] ]]
nsigma=2.8, if_select_obj=False, exp_sz=1.2, npixels=300, if_plot=False, bkg_std=bkg_std) data_process.PSF_list = [PSF_rebin] data_process.deltaPix = 1 data_process.noise_map[np.isnan(data_process.noise_map)] = bkg_std from galight.tools.measure_tools import mask_obj import copy apertures = copy.deepcopy(data_process.apertures) apertures[0].a, apertures[0].b = apertures[0].a * expzs, apertures[0].b * expzs apertures[1].a, apertures[1].b = apertures[1].a * expzs, apertures[1].b * expzs masks = mask_obj(data_process.target_stamp, apertures, sum_mask=True) masks = 1 - masks data_process.target_mask = masks # plt.imshow(masks, origin='lower') # plt.show() #%% savename = rebin_name + 'removebkg' #+ '.pkl' #Setting the fitting method and run. import glob, pickle if glob.glob(savename + '*pkl') == []: # apertures = copy.deepcopy(data_process.apertures) # apertures = [copy.deepcopy(apertures[0])] + apertures # apertures[0].a, apertures[0].b = apertures[0].a*0.2, apertures[0].b*0.2 # data_process.apertures = apertures from galight.fitting_specify import FittingSpecify
theta=data_process.apertures[obj_id].theta, # q = 1, # theta = 0, if_plot=True) from galight.tools.cutout_tools import pix_region region = pix_region(pos['x'] + np.array([len(asy_class.img) / 2] * 2), r_p, q=data_process.apertures[obj_id].b / data_process.apertures[obj_id].a, theta=data_process.apertures[obj_id].theta) mask = region.to_mask(mode='exact') from photutils import EllipticalAperture ap = EllipticalAperture(pos['x'] + np.array([len(asy_class.img) / 2] * 2), a=r_p, b=r_p * data_process.apertures[obj_id].b / data_process.apertures[obj_id].a, theta=data_process.apertures[obj_id].theta) from galight.tools.measure_tools import mask_obj mask_ap = mask_obj(asy_class.img, [ap])[0] data = mask.cutout(asy_class.img) plt.imshow(mask.data * data) print(np.sum(mask.data * data)) print(np.sum((1 - mask_ap) * asy_class.img)) plt.imshow(mask_ap, origin='lower') plt.show() # print(r_p)
def generate_target_materials(self, cut_kernel=None, radius=None, radius_list=None, bkg_std=None, create_mask=False, if_plot=None, **kwargs): """ Prepare the fitting materials to used for the fitting, including the image cutout, noise map and masks (optional). Parameter -------- cut_kernel: string or 'None'. The args will be input as kernel into galight.tools.cutout_tools.cut_center_auto() radius: int or float The radius to cutout the image data. The final framesize will be 2*radius+1 cut_kernel: None or 'center_gaussian' or 'center_bright'. - if 'None', directly cut. - if 'center_gaussian', fit central as 2D Gaussian and cut at the Gaussian center. - if 'center_bright', cut the brightest pixel in the center bkg_std: float To input the background noise level. create_mask: bool. 'True' if masks are needed to input. if_plot: bool. If 'True', the plots will made during the cut out. """ if if_plot == None: if_plot = self.if_plot if radius == None: if radius_list == None: radius_list = [30, 35, 40, 45, 50, 60, 70] for rad in radius_list: from galight.tools.measure_tools import fit_data_oneD_gaussian _cut_data = cutout(image=self.fov_image, center=self.target_pos, radius=rad) edge_data = np.concatenate([ _cut_data[0, :], _cut_data[-1, :], _cut_data[:, 0], _cut_data[:, -1] ]) gauss_mean, gauss_1sig = fit_data_oneD_gaussian(edge_data, ifplot=False) up_limit = gauss_mean + 2 * gauss_1sig percent = np.sum(edge_data > up_limit) / float(len(edge_data)) if percent < 0.03: break radius = rad if if_plot == True: print("Plot target cut out zoom in:") if cut_kernel is not None: target_stamp, self.target_pos = cut_center_auto( image=self.fov_image, center=self.target_pos, kernel=cut_kernel, radius=radius, return_center=True, if_plot=if_plot) else: target_stamp = cutout(image=self.fov_image, center=self.target_pos, radius=radius) if self.fov_noise_map is not None: self.noise_map = cutout(image=self.fov_noise_map, center=self.target_pos, radius=radius) else: if bkg_std == None: from galight.tools.measure_tools import esti_bgkstd target_2xlarger_stamp = cutout(image=self.fov_image, center=self.target_pos, radius=radius * 2) self.bkg_std = esti_bgkstd(target_2xlarger_stamp, if_plot=if_plot) _exptime = deepcopy(self.exptime) if _exptime is None: if 'EXPTIME' in self.header.keys(): _exptime = self.header['EXPTIME'] else: raise ValueError( "No Exposure time information in the header, should input a value." ) if isinstance(_exptime, np.ndarray): _exptime = cutout(image=self.exptime, center=self.target_pos, radius=radius) noise_map = np.sqrt(abs(target_stamp / _exptime) + self.bkg_std**2) self.noise_map = noise_map target_mask = np.ones_like(target_stamp) from galight.tools.measure_tools import detect_obj, mask_obj apertures = detect_obj(target_stamp, if_plot=create_mask, **kwargs) if create_mask == True: select_idx = str( input( 'Input directly the a obj idx to mask, use space between each id:\n' )) if sys.version_info.major > 2: select_idx = [ int(select_idx[i]) for i in range(len(select_idx)) if select_idx[i].isnumeric() ] else: select_idx = [ int(select_idx[i]) for i in range(len(select_idx)) if select_idx[i].isdigit() ] apertures_ = [apertures[i] for i in select_idx] apertures = [ apertures[i] for i in range(len(apertures)) if i not in select_idx ] mask_list = mask_obj(target_stamp, apertures_, if_plot=False) for i in range(len(mask_list)): target_mask *= mask_list[i] self.apertures = apertures self.target_stamp = target_stamp self.target_mask = target_mask if if_plot: fig, (ax1, ax3, ax2) = plt.subplots(1, 3, figsize=(14, 10)) im1 = ax1.imshow(target_stamp, origin='lower', norm=LogNorm(), vmax=target_stamp.max(), vmin=1.e-4) ax1.set_title('Cutout target') fig.colorbar(im1, ax=ax1, pad=0.01, orientation="horizontal") ax1.get_xaxis().set_visible(False) ax1.get_yaxis().set_visible(False) im2 = ax2.imshow(self.noise_map, origin='lower', norm=LogNorm()) ax2.set_title('Noise map') fig.colorbar(im2, ax=ax2, pad=0.01, orientation="horizontal") ax2.get_xaxis().set_visible(False) ax2.get_yaxis().set_visible(False) im3 = ax3.imshow(target_stamp * target_mask, origin='lower', norm=LogNorm(), vmax=target_stamp.max(), vmin=1.e-4) ax3.set_title('data * mask') fig.colorbar(im3, ax=ax3, pad=0.01, orientation="horizontal") ax3.get_xaxis().set_visible(False) ax3.get_yaxis().set_visible(False) plt.show()