Esempio n. 1
0
def model_objects_galfit(dataimage,galfitparamfile,show_residualimg=False,verbose=True):
    """
    ~ ~ ~ ~ STILL UNDER CONSTRUCTION/TESTING ~ ~ ~ ~

    Function template for modeling of objects with galfit

    --- INPUT ---
    dataimage
    galfitparamfile
    show_residualimg
    verbose

    --- EXAMPLE OF USE ---
    import tdose_model_FoV as tmf


    """
    print(' # # # # # # # # # model_objects_galfit() still under development/testing # # # # # # # # #')
    if verbose: print(' - Use GALFIT model output to obtain model parameters for residual between (GALFIT) model and data')
    if verbose: print('   ----------- Started on '+tu.get_now_string()+' ----------- ')

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    galfitparams = tu.galfit_loadoutput(galfitparamfile)
    Nmodels      = len(galfitparams)
    imgsize      = dataimage.shape
    xgrid, ygrid = tu.gen_gridcomponents(imgsize)

    for oo, obj in enumerate(Nmodels):

        xxx = yyy

        output = param_optimized, param_cov

        image       = '/Volumes/DATABCKUP3/MUSE/candels-cdfs-02/acs_814w_candels-cdfs-02_cut_v1.0.fits'
        sexcatalog  = '/Volumes/DATABCKUP3/MUSE/candels-cdfs-02/catalog_photometry_candels-cdfs-02.fits'
        fileout     = '/Volumes/DATABCKUP3/MUSE/candels-cdfs-02/galfit_inputfile_acs_814w_candels-cdfs-02-sextractor.txt'
        tu.galfit_buildinput_fromssextractoroutput(fileout,sexcatalog,image,objecttype='gaussian',verbose=verbose)

        galfitoutput = tu.galfit_run(fileout,verbose=verbose)
        param_optimized  = tu.galfit_results2paramlist(galfitoutput,verbose=verbose)

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if verbose: print('   ----------- Finished on '+tu.get_now_string()+' ----------- ')
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if show_residualimg:
        if verbose: print(' - Displaying the residual image between data image and galfit model (assuming gaussians)')
        galfit_img = tmf.modelimage_multigauss((xgrid,ygrid), output[0]  , showmodelimg=False)
        res_img    = dataimage-galfit_img
        plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm())
        plt.title('Data Residual = Data Image - Galfit Model Image ')
        plt.show()
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    return output
Esempio n. 2
0
def remove_object(datacube,sourcemodelcube,objects=[1,3],remove=True,dataext=1,sourcemodelext=1,
                  savecube=False,savedir=None,clobber=False,verbose=True):
    """
    Use source model cube to remove object(s) from data cube

    --- INPUT ---
    datacube            Datacube to modify
    sourcemodelcube     Source model cube of data cube defining models of each source in the datacube
    objects             The objects to remove from data cube. Provide number of source sub-cube in
                        "sourcemodelcube".
    remove              If true objects in "objects" will be removed from "datacube". If remove=False
                        everything but the objects listed in "objects" will be removed.
    dataext             Extension of datacube containing actual data
    sourcemodelext      Extension of source model cube containing the models
    savecube            If a string is provided the modified cube will be stored in a new fits file
                        appending the provided string to the data cube file name.
    savedir             If a directory path is provided, the modified cube will be stored here. Otherwise
                        it will be stored at the same location as the datacube.
    clobber             If true any existing fits file will be overwritten if modified cube is saved
    verbose             Toggle verbosity

    --- EXAMPLE OF USE ---
    import tdose_modify_cube

    datacube        = 'datacube_spectra_are_extracted_from.fits'
    sourcemodelcube = 'tdose_source_modelcube.fits'

    modified_cube   = tdose_modify_cube.remove_object(datacube,sourcemodelcube,savecube='source1and3removed')


    """
    if verbose: print(' - Loading data and source model cubes ')
    datacubehdu     = fits.open(datacube)
    dataarr         = datacubehdu[dataext].data
    dataarr_hdr     = datacubehdu[dataext].header

    sourcemodelhdu  = fits.open(sourcemodelcube)
    sourcemodel     = sourcemodelhdu[sourcemodelext].data
    sourcemodel_hdr = sourcemodelhdu[sourcemodelext].header
    sourcemodelhdu.close()

    Nmodels         = sourcemodel.shape[0]
    models          = np.arange(Nmodels)

    if verbose: print(' - Check that all objects indicated are present in source model cube')
    objects  = np.asarray(objects)

    maxobj = np.max(np.abs(objects))
    if maxobj >= Nmodels:
        sys.exit(' ---> Object model "'+str(maxobj)+'" is not included in source model cube (models start at 0) ')
    else:
        if verbose: print(('   All object models appear to be included in the '+str(Nmodels)+' source models found in cube'))

    if verbose: print(' - Determining objects (source models) to remove from data cube ')
    if remove:
        obj_remove = objects
        obj_keep   = np.setdiff1d(models,obj_remove)
    else:
        obj_keep   = objects
        obj_remove = np.setdiff1d(models,obj_keep)

    remove_cube = np.sum(sourcemodel[obj_remove.astype(int),:,:,:],axis=0)

    modified_cube = dataarr - remove_cube

    if savecube:
        datacubehdu[dataext].data = modified_cube # Replacing original data with modified cube
        if savedir is None:
            outname = datacube.replace('.fits','_'+str(savecube)+'.fits')
        else:
            outname = savedir+datacube.split('/')[-1].replace('.fits','_'+str(savecube)+'.fits')
        if verbose: print((' - Saving modified cube to \n   '+outname))
        if verbose: print('   (Using datacube header with modification keywords appended) ')
        # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        # hducube = fits.PrimaryHDU(modified_cube)       # default HDU with default minimal header
        # hducube.header = dataarr_hdr

        # adding hdrkeys:   '---KEY--',                       '----------------MAX LENGTH COMMENT-------------'
        dataarr_hdr.append(('MODIFIED',                'True','Cube is modified with tdose_modify_cube.py?'),end=True)
        dataarr_hdr.append(('MODTIME ',   tu.get_now_string(),'Date and time of cube modification'),end=True)
        dataarr_hdr.append(('SMC     ',       sourcemodelcube,'Name of Source Model Cube'),end=True)
        dataarr_hdr.append(('NREMOVE ',       len(obj_remove),'Number of sources removed from cube'),end=True)
        dataarr_hdr.append(('NKEEP   ',         len(obj_keep),'Number of sources kept in cube'),end=True)
        dataarr_hdr.append(('SREMOVE ',','.join([str(oo) for oo in obj_remove])),'Source indexes removed',end=True)
        dataarr_hdr.append(('SKEEP   ',','.join([str(oo) for oo in obj_keep])),'Source indexes kept',end=True)
        # dataarr_hdr.append(('COMMENT ','Source indexes removed:'+','.join([str(oo) for oo in obj_remove])),end=True)
        # dataarr_hdr.append(('COMMENT ','Source indexes kept:'+','.join([str(oo) for oo in obj_keep])),end=True)

        # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        datacubehdu.writeto(outname,overwrite=clobber)

    return modified_cube

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Esempio n. 3
0
def perform_modification(setupfile='./tdose_setup_template_modify.txt',clobber=False,verbose=True):
    """
    Modify a data cube using the information stored in a generated TDOSE source model cube.

    --- INPUT ---
    setupfile       TDOSE modification setup file. Template can be generated
                    with tdose_utilities.generate_setup_template_modify()
    clobber         Overwrite existing output?
    verbose         Toggle verbosity

    --- EXAMPLE OF USE ---
    import tdose_modify_cube as tmc
    setupfile = './tdose_setup_template_modify.txt'
    tmc.perform_modification(setupfile=setupfile)

    """

    start_time = time.clock()
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if verbose: print('==================================================================================================')
    if verbose: print(' TDOSE: Loading setup                                       '+\
                      '      ( Total runtime = '+str("%10.4f" % (time.clock() - start_time))+' seconds )')

    setupdic        = tu.load_setup(setupfile,verbose=verbose)

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if verbose: print('==================================================================================================')
    if verbose: print(' TDOSE: Logging setup                                       '+\
                      '      ( Total runtime = '+str("%10.4f" % (time.clock() - start_time))+' seconds )')

    setuplog = setupdic['modified_cube_dir']+setupfile.split('/')[-1].replace('.txt','_logged.txt')
    if os.path.isfile(setuplog) & (clobber == False):
        if verbose: print(' - WARNING Logged setupfile exists and clobber = False. Not storing setup ')
    else:
        if verbose: print(' - Writing setup and command to spec1D_directory to log extraction setup and command that was run')
        setupinfo    = open(setupfile,'r')
        setupcontent = setupinfo.read()
        setupinfo.close()

        cmdthatwasrun = "import tdose_modify_cube as tmc; tmc.perform_modification(setupfile='%s',clobber=%s,verbose=%s)" % \
                        (setuplog,clobber,verbose)

        loginfo = open(setuplog, 'w')
        loginfo.write("# The setup file appended below was run with the command: \n# "+cmdthatwasrun+
                      " \n# on "+tu.get_now_string()+'\n# ')

        loginfo.write(setupcontent)
        loginfo.close()

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if verbose: print('==================================================================================================')
    if verbose: print(' TDOSE: Modifying data cube                                 '+\
                      '      ( Total runtime = '+str("%10.4f" % (time.clock() - start_time))+' seconds )')

    savestring = 'satelitesremoved'

    if setupdic['sources_action'].lower() == 'remove':
        removing = True
    elif setupdic['sources_action'].lower() == 'keep':
        removing = False
    else:
        if verbose: print(' - WARNING "sources_action" keyword in setup ('+setupdic['sources_action']+') is invalid. Removing source.')
        removing = True

    modified_cube = tmc.remove_object(setupdic['data_cube'],setupdic['source_model_cube'],
                                      objects=setupdic['modify_sources_list'],remove=removing,
                                      dataext=setupdic['cube_extension'],
                                      sourcemodelext=setupdic['source_extension'],savecube=setupdic['modified_cube'],
                                      savedir=setupdic['modified_cube_dir'],clobber=clobber,verbose=verbose)
Esempio n. 4
0
def model_objects_gauss(param_init,dataimage,optimizer='curve_fit',max_centroid_shift=None,
                        datanoise=None,show_residualimg=True,verbose=True):
    """
    Optimize residual between model (multiple Gaussians) and data with least squares in 2D

    --- INPUT ---
    param_init            Initial guess on parameters (defines the number of gaussians to fit for)
    dataimage             Image to model with multiple Gaussians
    optimizer             Chose the optimizer to use
                              leastsq     scipy.optimize.leastsq(); not ideal for 2D images...
                                          Tries to optimize the residual function
                              curve_fit   scipy.optimize.curve_fit()
                                          Tries to optimize using the model function
    max_centroid_shift    Maximum offset in pixels of (x,y) centroid position of sources when modeling
                          I.e. impose  ypix_centroid +/-  max_centroid_shift and xpix_centroid +/-  max_centroid_shift
                          bounds om parameters when fitting.
    datanoise             Image of sigmas, i.e., sqrt(variance) to use as weights when optimizing fit
                          using curve_fit
    verbose               Toggle verbosity

    --- EXAMPLE OF USE ---
    import tdose_model_FoV as tmf
    param_init = [18,31,1*0.3,2.1*0.3,1.2*0.3,30*0.3,    110,90,200*0.5,20.1*0.5,15.2*0.5,0*0.5]
    dataimg    = afits.open('/Users/kschmidt/work/TDOSE/mock_cube_sourcecat161213_tdose_mock_cube.fits')[0].data[0,:,:]
    param_optimized, param_cov  = tmf.model_objects_gauss(param_init,dataimg,verbose=True)

    """
    if verbose: print(' - Optimize residual between model (multiple Gaussians) and data with the optimizer "'+optimizer+'"')
    if verbose: print('   ----------- Started on '+tu.get_now_string()+' ----------- ')
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if optimizer == 'leastsq':
        best_param, cov, info, message, succesflag = opt.leastsq(tmf.residual_multigauss,param_init, args=(dataimage),
                                                full_output=True)
        output = best_param, cov, info, message, succesflag
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    elif optimizer == 'curve_fit':
        imgsize      = dataimage.shape
        xgrid, ygrid = tu.gen_gridcomponents(imgsize)
        if datanoise is not None:
            sigma = datanoise.ravel()
        else:
            sigma = datanoise

        maxfctcalls  = 30000
        Nsources     = int(len(param_init)/6.)
        #               [yposition,xposition,fluxscale,sigmay  ,sigmax  ,angle]
        param_bounds = ([0        ,0        ,0        ,1./2.355,1./2.355,-360 ]*Nsources,
                        [np.inf   , np.inf  ,np.inf   , np.inf , np.inf ,360  ]*Nsources)

        Nnonfinite = len(dataimage[np.where(~np.isfinite(dataimage))])
        if Nnonfinite > 0:
            dataimage[np.where(~np.isfinite(dataimage))] = 0.0
            if verbose: print((' WARNING: '+str(Nnonfinite)+' Pixels in dataimage that are not finite; '
                                                           'setting them to 0 to prevent curve_fit crash'))

        if max_centroid_shift is not None:
            init_y       = param_init[0::6]
            init_x       = param_init[1::6]

            bound_xlow   = init_x-max_centroid_shift
            bound_xhigh  = init_x+max_centroid_shift

            bound_ylow   = init_y-max_centroid_shift
            bound_yhigh  = init_y+max_centroid_shift

            param_bounds[0][0::6] = bound_ylow
            param_bounds[1][0::6] = bound_yhigh

            param_bounds[0][1::6] = bound_xlow
            param_bounds[1][1::6] = bound_xhigh

        try:
            param_optimized, param_cov = opt.curve_fit(tmf.curve_fit_function_wrapper, (xgrid, ygrid),dataimage.ravel(),
                                                       p0 = param_init, sigma=sigma,maxfev=maxfctcalls, bounds=param_bounds)
            output = param_optimized, param_cov
        except:
            print(' WARNING: Curve_fit failed so returning param_init [ypos,xpos,fluxscale,sigmay,sigmax,angle]: ')
            print(('          '+str(param_init)))
            print('          i.e. the intiial guess of the parameters')
            print(('          Likely using "maximum function call" of '+str(maxfctcalls)+')'))
            output = param_init, None
            #pdb.set_trace()

    else:
        sys.exit(' ---> Invalid optimizer ('+optimizer+') chosen in model_objects_gauss()')
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if verbose: print('\n   ----------- Finished on '+tu.get_now_string()+' ----------- ')
    #if verbose: print ' - The returned best-fit parameters are \n   ',output[0]
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if show_residualimg:
        if verbose: print(' - Displaying the residual image between initial guess and optimized parameters')
        init_img = tmf.modelimage_multigauss((xgrid,ygrid), param_init , showmodelimg=False)
        best_img = tmf.modelimage_multigauss((xgrid,ygrid), output[0]  , showmodelimg=False)
        res_img  = init_img-best_img

        plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm())
        plt.title('Model Residual = Initial Parameter Image - Optimized Parameter Image')
        plt.show()

        res_img  = best_img-dataimage

        plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm())
        plt.title('Data Residual = Optimized Parameter Image - Data Image')
        plt.show()
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    return output