示例#1
0
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
示例#2
0
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
示例#3
0
文件: eyescan.py 项目: chbrandt/bit
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
示例#4
0
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;