def run(fits_image, preset='', use_header=True): """Present image for interactive choice of the object to segment dic = run( fits_image ) Given FITS image is presented on an interacive window where the user can click on objects for selection. A segmentation algorithm will run on the image and selected points will be cutout if they correspond to a segmented object. 'preset' argument can be used to use pre-set Sextractor(SE) configuration (see sltools.Package.sextractor for more information). If we don't want to use image's header info 'use_header' argument can be set to "False"; default is "True" Input: - fits_image <str> : FITS file containg the image to use - preset <str> : SE's pre-set configuration - use_header <bool> : whether to use (or not) image's header Output: - {'IDs', 'images', 'headers'} """ # Open given image.. # if (use_header): img, header = pyfits.getdata(fits_image, header=use_header) else: img = pyfits.getdata(fits_image, header=use_header) header = None # Show the image and make "clickable" for objects selection.. # _dic = click(img, header) x = _dic['x'] y = _dic['y'] ra = _dic['ra'] dec = _dic['dec'] del _dic centroids = zip(x, y) # Segment image with Sextractor.. # if preset == 'none': preset = '' _dic = SE.run_segobj(fits_image, preset=preset) if not _dic: return False objimg = pyfits.getdata(_dic['OBJECTS']) segimg = pyfits.getdata(_dic['SEGMENTATION']) cat = pyfits.open(_dic['CATALOG'])[1].data del _dic _dic = select_objects(segimg, objimg, centroids, header) if not _dic: return False return (_dic)
def whole_image(X, Y, imagefile, PARAMS, preset): centroids = zip(X, Y) if len(centroids) == 0: logging.warning("No objects in given (X,Y lists). Finishing.") return False # Run Sextractor over the whole image: _Dsex = sextractor.run_segobj(imagefile, PARAMS, preset=preset) segimg = pyfits.getdata(_Dsex['SEGMENTATION']) objimg = pyfits.getdata(_Dsex['OBJECTS']) objIDs = [segimg[o_o[1], o_o[0]] for o_o in centroids] objIDs = list(set(objIDs) - set([0])) # Take only the objects of interest... # from image: selobjimg = imcp.copy_objects(objimg, segimg, objIDs) del segimg, objimg # and catalog: cathdu = pyfits.open(_Dsex['CATALOG'])[1] selobjhdu = fts.select_entries(cathdu, 'NUMBER', *objIDs) del cathdu # Re-identify each object: for i in range(len(objIDs)): selobjhdu.data.field('NUMBER')[i] = i return (selobjimg, selobjhdu)
def whole_image(X,Y,imagefile,PARAMS,preset): centroids = zip(X,Y); if len(centroids) == 0: logging.warning("No objects in given (X,Y lists). Finishing."); return False; # Run Sextractor over the whole image: _Dsex = sextractor.run_segobj(imagefile,PARAMS,preset=preset); segimg = pyfits.getdata(_Dsex['SEGMENTATION']); objimg = pyfits.getdata(_Dsex['OBJECTS']); objIDs = [ segimg[o_o[1],o_o[0]] for o_o in centroids ]; objIDs = list(set(objIDs)-set([0])); # Take only the objects of interest... # from image: selobjimg = imcp.copy_objects(objimg,segimg,objIDs); del segimg,objimg; # and catalog: cathdu = pyfits.open(_Dsex['CATALOG'])[1]; selobjhdu = fts.select_entries(cathdu,'NUMBER',*objIDs); del cathdu; # Re-identify each object: for i in range(len(objIDs)): selobjhdu.data.field('NUMBER')[i] = i; return (selobjimg,selobjhdu);
def identify_images(frame_name, params=[], args={}, preset='sims'): """ Identify the objects at an image. Currently run SExtractor under sltools.sltools.image.sextractor.run_segobj """ dict_run_segobj = run_segobj(frame_name, params, args, preset) # 'params' are the SExtractor # parameters to output (strings, see SE's default.param). 'args' ({str:str,}) are SE command-line arguments objimgname, segimgname, catfilename = dict_run_segobj['OBJECTS'], dict_run_segobj['SEGMENTATION'], dict_run_segobj['CATALOG'] # catfilename is the SE output catalog with the 'params' columns frame_data = pyfits.getdata(frame_name) # to get the header add 'header=True' nonzero_frame_data = np.nonzero(frame_data) # make a loop over all images on the frame return objimgname, segimgname
def _file_2_arrays(fits_image, use_header, params, args, preset): """SExtract the Image and read the outputs to arrays.""" out = sextractor.run_segobj(fits_image, params, args, preset=preset) if (out == False): print >> sys.stderr, "Error: Sextractor raised and error coded during segmentation. Finishing run." return (False) objimg = pyfits.getdata(out['OBJECTS']) segimg = pyfits.getdata(out['SEGMENTATION']) tbhdu = pyfits.open(out['CATALOG'])[1] if (use_header): header = pyfits.getheader(fits_image) else: header = None return (objimg, segimg, header, tbhdu)
def _file_2_arrays(fits_image, use_header, params, args, preset): """SExtract the Image and read the outputs to arrays.""" out = sextractor.run_segobj(fits_image, params, args, preset=preset); if (out == False): print >> sys.stderr, "Error: Sextractor raised and error coded during segmentation. Finishing run." return (False); objimg = pyfits.getdata( out['OBJECTS'] ); segimg = pyfits.getdata( out['SEGMENTATION'] ); tbhdu = pyfits.open(out['CATALOG'])[1]; if (use_header): header = pyfits.getheader( fits_image ); else: header = None; return (objimg, segimg, header, tbhdu);
def run(regionfile, imagefile, args={}, preset='', shape=(100, 100), tablefile="objectsTb", stampsfile="pstamp_"): """ Runs the pipeline for arc postage stamps creation and segmentation (in this order) For each point inside (DS9) 'regionfile' take a window of size 'shape' around it. Sextractor config parameters can be passed using 'args'. So far, catalog output parameters are declared through the global variable 'PARAMS'. Use 'preset' for optimized settings. See sltools.image.sextractor for more info. Input: - regionfile str : DS9 region file - imagefile str : FITS filename - args {'key',value} : Sextractor config params - preset str : See sltools.image.sextractor - shape (int,int) : Object cutouts shape (dx,dy) - tablefile str : FITS table output filename - stampsfile str : Output file rootnames Output: Write to 'tablefile' FITS table the segmented objects properties ('PARAMS') --- """ rootname = imagefile[:-5] # Object's centroid will be placed over postamp's central point x_i = shape[0] / 2 y_i = shape[1] / 2 x_size, y_size = shape tbList = [] flList = [] # Read DS9 regionfile and input imagefile.. # D_in = asc.read_ds9cat(regionfile) X = asarray(D_in['x']) Y = asarray(D_in['y']) centroids = XY = zip(X, Y) R = asarray(D_in['size']) logging.debug("Centroids: %s", XY) if len(XY) == 0: logging.warning("No objects in given Centroids list. Finishing.") return img, hdr = pyfits.getdata(imagefile, header=True) # For each "centroid": # for i in xrange(len(XY)): logging.info("Process %s, point: %s", i, XY[i]) # Take a snapshot # obj, hdr = imcp.cutout(img, hdr, xo=X[i], yo=Y[i], x_size=x_size, y_size=y_size) file_i = rootname + "_pstamp_" + str(X[i]) + "_" + str(Y[i]) + ".fits" pyfits.writeto(file_i, obj, hdr, clobber=True) logging.info("Snapshot %s created", file_i) flList.append(file_i) # Run Sextractor over newly created sanpshot.. # Dsex = SE.run_segobj(file_i, PARAMS, preset) ### SEXTRACTOR CALL segimg = pyfits.getdata(Dsex['SEGMENTATION']) cathdu = pyfits.open(Dsex['CATALOG'])[1] logging.info("Segmentation done.") logging.info("Files %s,%s,%s created", Dsex['SEGMENTATION'], Dsex['OBJECTS'], Dsex['CATALOG']) objID = segobjs.centroid2id(segimg, (x_i, y_i)) # The following block searchs for the nearest neighbour and check if the IDs are # in accordance. If the given centroid is not part of an object (i.e, ID==0), # the nearest object is taken in place; and if the IDs do not match and non-zero # valued just an warning is printed to "debug" level. # centroids_sex = zip(cathdu.data.field('X_IMAGE'), cathdu.data.field('Y_IMAGE')) [logging.debug("SE Centroids: %s", _c) for _c in centroids_sex] nrst_indx_dist = tbmtch.nearest_neighbour([centroids[i]], centroids_sex) logging.debug("Nearest point index|distance: %s (length:%s)", nrst_indx_dist, len(nrst_indx_dist)) _indx, _dist = nrst_indx_dist[0] nrst_obj = cathdu.data[_indx] _id = nrst_obj.field('NUMBER') if objID == 0: objID = _id logging.warning( "Object ID is 0 for Centroid:%s. Using object (ID) %s, instead.", centroids[i], _id) if objID != _id: logging.debug( "Oops, two different objects were found matching point %s, objIDs %s and %s", centroids[i], objID, _id) logging.info("ObjectID: %s being readout", objID) tbList.append(fts.select_entries(cathdu, 'NUMBER', objID)) arc_i = rootname + "_nrstArc_" + str(X[i]) + "_" + str(Y[i]) + ".fits" arc = imcp.select(segimg, obj, objID) pyfits.writeto(arc_i, arc, hdr, clobber=True) # Create output table new_tbhdu = tbList[0] for i in xrange(1, len(tbList)): new_tbhdu = fts.extend_tbHDU(new_tbhdu, tbList[i]) tb_flList = fts.dict_to_tbHDU({'filename': flList}) new_tbhdu = fts.merge_tbHDU(new_tbhdu, tb_flList) tablefile = rootname + "_arcscat.fits" new_tbhdu.writeto(tablefile, clobber=True) return
def run(regionfile, imagefile, args={}, preset='', shape=(100,100), tablefile="objectsTb",stampsfile="pstamp_"): """ Runs the pipeline for arc postage stamps creation and segmentation (in this order) For each point inside (DS9) 'regionfile' take a window of size 'shape' around it. Sextractor config parameters can be passed using 'args'. So far, catalog output parameters are declared through the global variable 'PARAMS'. Use 'preset' for optimized settings. See sltools.image.sextractor for more info. Input: - regionfile str : DS9 region file - imagefile str : FITS filename - args {'key',value} : Sextractor config params - preset str : See sltools.image.sextractor - shape (int,int) : Object cutouts shape (dx,dy) - tablefile str : FITS table output filename - stampsfile str : Output file rootnames Output: Write to 'tablefile' FITS table the segmented objects properties ('PARAMS') --- """ rootname = imagefile[:-5] # Object's centroid will be placed over postamp's central point x_i = shape[0]/2 y_i = shape[1]/2 x_size,y_size = shape tbList = [] flList = [] # Read DS9 regionfile and input imagefile.. # D_in = asc.read_ds9cat(regionfile) X = asarray(D_in['x']) Y = asarray(D_in['y']) centroids = XY = zip(X,Y) R = asarray(D_in['size']) logging.debug("Centroids: %s",XY) if len(XY) == 0: logging.warning("No objects in given Centroids list. Finishing.") return img,hdr = pyfits.getdata(imagefile,header=True) # For each "centroid": # for i in xrange(len(XY)): logging.info("Process %s, point: %s",i,XY[i]) # Take a snapshot # obj,hdr = imcp.cutout(img,hdr,xo=X[i],yo=Y[i],x_size=x_size,y_size=y_size) file_i = rootname+"_pstamp_"+str(X[i])+"_"+str(Y[i])+".fits" pyfits.writeto(file_i,obj,hdr,clobber=True) logging.info("Snapshot %s created",file_i) flList.append(file_i) # Run Sextractor over newly created sanpshot.. # Dsex = SE.run_segobj(file_i, PARAMS,preset) ### SEXTRACTOR CALL segimg = pyfits.getdata(Dsex['SEGMENTATION']) cathdu = pyfits.open(Dsex['CATALOG'])[1] logging.info("Segmentation done.") logging.info("Files %s,%s,%s created",Dsex['SEGMENTATION'],Dsex['OBJECTS'],Dsex['CATALOG']) objID = segobjs.centroid2id(segimg,(x_i,y_i)) # The following block searchs for the nearest neighbour and check if the IDs are # in accordance. If the given centroid is not part of an object (i.e, ID==0), # the nearest object is taken in place; and if the IDs do not match and non-zero # valued just an warning is printed to "debug" level. # centroids_sex = zip(cathdu.data.field('X_IMAGE'),cathdu.data.field('Y_IMAGE')) [ logging.debug("SE Centroids: %s",_c) for _c in centroids_sex ] nrst_indx_dist = tbmtch.nearest_neighbour([centroids[i]],centroids_sex) logging.debug("Nearest point index|distance: %s (length:%s)",nrst_indx_dist,len(nrst_indx_dist)) _indx,_dist = nrst_indx_dist[0] nrst_obj = cathdu.data[_indx] _id = nrst_obj.field('NUMBER') if objID == 0: objID = _id logging.warning("Object ID is 0 for Centroid:%s. Using object (ID) %s, instead.",centroids[i],_id) if objID != _id: logging.debug("Oops, two different objects were found matching point %s, objIDs %s and %s",centroids[i],objID,_id) logging.info("ObjectID: %s being readout",objID) tbList.append(fts.select_entries(cathdu,'NUMBER',objID)) arc_i = rootname+"_nrstArc_"+str(X[i])+"_"+str(Y[i])+".fits" arc = imcp.select(segimg,obj,objID) pyfits.writeto(arc_i,arc,hdr,clobber=True) # Create output table new_tbhdu = tbList[0] for i in xrange(1,len(tbList)): new_tbhdu = fts.extend_tbHDU(new_tbhdu,tbList[i]) tb_flList = fts.dict_to_tbHDU({'filename':flList}) new_tbhdu = fts.merge_tbHDU(new_tbhdu,tb_flList) tablefile = rootname+"_arcscat.fits" new_tbhdu.writeto(tablefile,clobber=True) return
def run(regionfile, imagefile, args={}, preset='HST_Arcs', shape=(100, 100)): # , tablefile="objectsTb"):# ,stampsfile="pstamp_"): """ Runs the pipeline for postamps creation and segmented run( regionfile, imagefile [,...] ) For each point listed inside DS9 'regionfile', considered object centroids in 'imagefile', a snapshot and object segmentation are done; shape and rootname (suffixed by "objID", fits extension) are passed through 'shape' and 'stampsfile', resp. Sextractor config parameters can be passed using 'args'. So far, catalog output parameters are hardcoded. Use 'preset' for optimized settings. See sltools.image.sextractor for more info. Input: - regionfile : str DS9 region file - imagefile : str FITS filename - args : {'option':'value',} Sextractor config params - preset : str See sltools.image.sextractor - shape : (int,int) Object cutouts shape (pixels,pixels) - tablefile : str FITS table output filename - stampsfile : str Output file rootnames Output: - tbhdu : pyfits.BinTableHDU Output (fits) table with objects' computed parameters --- """ # Sextractor Params: PARAMS = [ 'NUMBER', 'ELONGATION', 'ELLIPTICITY', 'ISOAREA_IMAGE', 'A_IMAGE', 'B_IMAGE', 'THETA_IMAGE', 'X_IMAGE', 'Y_IMAGE', 'X2_IMAGE', 'Y2_IMAGE', 'XY_IMAGE' ] # Object's centroid will be placed over postamp's central point x_i = shape[0] / 2 y_i = shape[1] / 2 _tbList = [] _flList = [] _imList = [] # Read DS9 regionfile and input imagefile.. # D_in = asc.read_ds9cat(regionfile) X = asarray(D_in['x']) Y = asarray(D_in['y']) centroids = zip(X, Y) #################################################################### # Segment the whole image at once # Needs: # X , Y , image(FITS) filename and Sextractor arguments/inputs selobjimg_W, selobjhdu_W = whole_image(X, Y, imagefile, PARAMS, preset) selobjhdu_W.writeto('Whole_image.fit', clobber=True) pyfits.writeto('Whole_image.fits', selobjimg_W, clobber=True) #################################################################### img, hdr = pyfits.getdata(imagefile, header=True) logging.debug("Centroids: %s", centroids) rootname = re.sub(".fits", "", imagefile) # For each centroid, do: # i = -1 xy = centroids[:] for o_o in centroids: i += 1 logging.info("Process %s, point: %s", i, o_o) # Take a snapshot # _obj, _hdr = imcp.snapshot(img, hdr, centroid=o_o, shape=shape) file_i = rootname + "_ps" + str(i) + ".fits" pyfits.writeto(file_i, _obj, _hdr, clobber=True) del _obj, _hdr logging.info("Poststamp %s created", file_i) # Run Sextractor over newly created sanpshot.. # _Dsex = sextractor.run_segobj(file_i, PARAMS, preset=preset) ### SEXTRACTOR CALL segimg = pyfits.getdata(_Dsex['SEGMENTATION']) objimg = pyfits.getdata(_Dsex['OBJECTS']) cathdu = pyfits.open(_Dsex['CATALOG'])[1] objID = segimg[y_i, x_i] if not objID: lixo = xy.remove(o_o) continue logging.info("ObjectID: %s being readout", objID) _tbList.append(fts.select_entries(cathdu, 'NUMBER', objID)) _flList.append(file_i) _imList.append(imcp.copy_objects(objimg, segimg, [objID])) # Initialize output table and image.. # selobjhdu_S = _tbList[0] selobjimg_S = np.zeros(img.shape, dtype=img.dtype) selobjimg_S = combine.add_images(selobjimg_S, _imList[0], x=xy[0][0], y=xy[0][1]) # # and do the same for each object # for i in xrange(1, len(_tbList)): selobjhdu_S = fts.extend_tbHDU(selobjhdu_S, _tbList[i]) selobjimg_S = combine.add_images(selobjimg_S, _imList[i], x=xy[i][0], y=xy[i][1]) # Write down the FITS catalog.. # tb_flList = fts.dict_to_tbHDU({'filename': _flList}) selobjhdu_S = fts.merge_tbHDU(selobjhdu_S, tb_flList) # And the FITS image, composed by the well segmented objects.. # selobjhdu_S.writeto('Stamps_compose.fit', clobber=True) pyfits.writeto('Stamps_compose.fits', selobjimg_S, clobber=True) return
def run(regionfile, imagefile, args={}, preset='HST_Arcs', shape=(100,100)):# , tablefile="objectsTb"):# ,stampsfile="pstamp_"): """ Runs the pipeline for postamps creation and segmented run( regionfile, imagefile [,...] ) For each point listed inside DS9 'regionfile', considered object centroids in 'imagefile', a snapshot and object segmentation are done; shape and rootname (suffixed by "objID", fits extension) are passed through 'shape' and 'stampsfile', resp. Sextractor config parameters can be passed using 'args'. So far, catalog output parameters are hardcoded. Use 'preset' for optimized settings. See sltools.image.sextractor for more info. Input: - regionfile : str DS9 region file - imagefile : str FITS filename - args : {'option':'value',} Sextractor config params - preset : str See sltools.image.sextractor - shape : (int,int) Object cutouts shape (pixels,pixels) - tablefile : str FITS table output filename - stampsfile : str Output file rootnames Output: - tbhdu : pyfits.BinTableHDU Output (fits) table with objects' computed parameters --- """ # Sextractor Params: PARAMS=['NUMBER','ELONGATION','ELLIPTICITY','ISOAREA_IMAGE','A_IMAGE','B_IMAGE','THETA_IMAGE','X_IMAGE','Y_IMAGE','X2_IMAGE','Y2_IMAGE','XY_IMAGE'] # Object's centroid will be placed over postamp's central point x_i = shape[0]/2; y_i = shape[1]/2; _tbList = []; _flList = []; _imList = []; # Read DS9 regionfile and input imagefile.. # D_in = asc.read_ds9cat(regionfile); X = asarray(D_in['x']); Y = asarray(D_in['y']); centroids = zip(X,Y); #################################################################### # Segment the whole image at once # Needs: # X , Y , image(FITS) filename and Sextractor arguments/inputs selobjimg_W,selobjhdu_W = whole_image(X,Y,imagefile,PARAMS,preset); selobjhdu_W.writeto('Whole_image.fit',clobber=True); pyfits.writeto('Whole_image.fits',selobjimg_W,clobber=True); #################################################################### img,hdr = pyfits.getdata(imagefile,header=True); logging.debug("Centroids: %s",centroids); rootname = re.sub(".fits","",imagefile); # For each centroid, do: # i=-1; xy = centroids[:]; for o_o in centroids: i+=1; logging.info("Process %s, point: %s",i,o_o); # Take a snapshot # _obj,_hdr = imcp.snapshot( img, hdr, centroid=o_o, shape=shape ); file_i = rootname+"_ps"+str(i)+".fits"; pyfits.writeto(file_i,_obj,_hdr,clobber=True); del _obj,_hdr; logging.info("Poststamp %s created",file_i); # Run Sextractor over newly created sanpshot.. # _Dsex = sextractor.run_segobj(file_i, PARAMS,preset=preset); ### SEXTRACTOR CALL segimg = pyfits.getdata(_Dsex['SEGMENTATION']); objimg = pyfits.getdata(_Dsex['OBJECTS']); cathdu = pyfits.open(_Dsex['CATALOG'])[1]; objID = segimg[y_i,x_i]; if not objID: lixo = xy.remove(o_o); continue; logging.info("ObjectID: %s being readout",objID); _tbList.append(fts.select_entries(cathdu,'NUMBER',objID)); _flList.append(file_i); _imList.append(imcp.copy_objects(objimg,segimg,[objID])); # Initialize output table and image.. # selobjhdu_S = _tbList[0]; selobjimg_S = np.zeros(img.shape,dtype=img.dtype); selobjimg_S = combine.add_images(selobjimg_S,_imList[0],x=xy[0][0],y=xy[0][1]); # # and do the same for each object # for i in xrange(1,len(_tbList)): selobjhdu_S = fts.extend_tbHDU(selobjhdu_S,_tbList[i]); selobjimg_S = combine.add_images(selobjimg_S,_imList[i],x=xy[i][0],y=xy[i][1]); # Write down the FITS catalog.. # tb_flList = fts.dict_to_tbHDU({'filename':_flList}); selobjhdu_S = fts.merge_tbHDU(selobjhdu_S,tb_flList); # And the FITS image, composed by the well segmented objects.. # selobjhdu_S.writeto('Stamps_compose.fit',clobber=True); pyfits.writeto('Stamps_compose.fits',selobjimg_S,clobber=True) return;
def run(fits_image, preset='', use_header=True): """Present image for interactive choice of the object to segment dic = run( fits_image ) Given FITS image is presented on an interacive window where the user can click on objects for selection. A segmentation algorithm will run on the image and selected points will be cutout if they correspond to a segmented object. 'preset' argument can be used to use pre-set Sextractor(SE) configuration (see sltools.Package.sextractor for more information). If we don't want to use image's header info 'use_header' argument can be set to "False"; default is "True" Input: - fits_image <str> : FITS file containg the image to use - preset <str> : SE's pre-set configuration - use_header <bool> : whether to use (or not) image's header Output: - {'IDs', 'images', 'headers'} """ # Open given image.. # if (use_header): img, header = pyfits.getdata(fits_image, header=use_header); else: img = pyfits.getdata(fits_image, header=use_header); header = None; # Show the image and make "clickable" for objects selection.. # _dic = click(img, header); x = _dic['x']; y = _dic['y']; ra = _dic['ra']; dec = _dic['dec']; del _dic; centroids = zip(x,y); # Segment image with Sextractor.. # if preset=='none': preset=''; _dic = SE.run_segobj(fits_image, preset=preset); if not _dic: return False; objimg = pyfits.getdata( _dic['OBJECTS'] ); segimg = pyfits.getdata( _dic['SEGMENTATION'] ); cat = pyfits.open( _dic['CATALOG'] )[1].data; del _dic; _dic = select_objects(segimg, objimg, centroids, header); if not _dic: return False; return (_dic);