Beispiel #1
0
 def onFit(self, event):
     wx.BusyCursor()
     model = event.model
     fit_model, converged, chi2 = fit_image(self.flux, self.noise, model, self.psf, mode='NM')
     logger.debug('Fit converged: %s, chi2 = %f' % (converged, chi2))
     self.controlPanel.resetParams(fit_model, reset_plot=False)
     self.updatePlots(fit_model)
     self.controlPanel.Enable()
Beispiel #2
0
 def onSaveButton(self, event):
     self.originalModel = deepcopy(self.model)
     with open(self.modelFile, 'w') as f:
         logger.debug('Saving model file %s.' % self.modelFile)
         try:
             f.write(str(self.originalModel))
         except:
             logger.warn('Could not write cache model file %s' % self.modelFile)
Beispiel #3
0
def load_line_mask(line_file, wl):
    import atpy
    import pystarlight.io  # @UnusedImport
    t = atpy.Table(line_file, type='starlight_mask')
    masked_wl = np.zeros(wl.shape, dtype='bool')
    for i in xrange(len(t)):
        l_low, l_upp, line_w, line_name = t[i]
        if line_w > 0.0: continue
        logger.debug('Masking region: %s' % line_name)
        masked_wl |= (wl > l_low) & (wl < l_upp)
    return masked_wl
Beispiel #4
0
wl = g.wl[...]
true_psf = np.ma.array(g.psf[...])
tau_image = g.tau_image[...]
age_base = g.age_base[...]

flux_unit = g.full_ifs.attrs.fluxUnit
true_psf_FWHM = g.full_ifs.attrs.psfFWHM
norm_params = g.full_ifs.attrs.model
norm_model = BDModel.fromParamVector(norm_params)
norm_x0 = norm_params['x0']
norm_y0 = norm_params['y0']

db.close()

logger.debug('Plotting original model.')
#index_norm = find_nearest_index(l_ssp, 5635.0)
vmin = np.log10(full_image.min())
vmax = np.log10(full_image.max())
fig = plt.figure(figsize=(8, 6))
gs = plt.GridSpec(2, 3, height_ratios=[2.0, 3.0])
ax = plt.subplot(gs[0,0])
ax.imshow(np.log10(full_image), vmin=vmin, vmax=vmax)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r'Total')

ax = plt.subplot(gs[0,1])
ax.imshow(np.log10(bulge_image), vmin=vmin, vmax=vmax)
ax.set_xticks([])
ax.set_yticks([])
Beispiel #5
0
def decomp(cube, sampleId, args):
    galaxyId = califa_id_from_cube(cube)
    c = DecompContainer()
    if not args.overwrite:
        logger.info('Checking if the decomposition is already done for %s ...' % galaxyId)
        try:
            c.loadHDF5(args.db, sampleId, galaxyId)
            logger.warn('Previous data found, skipping decomposition.')
            return c
        except Exception as e:
            print e
            logger.info('No previous data found, continuing decomposition.')
            
    logger.info('Starting fit for %s...' % galaxyId)
    dec = CALIFADecomposer(cube, grating=args.grating, nproc=args.nproc)
    npix = dec.K.qMask.sum()
    dec.minNPix = npix / 2
    logger.info('Minimum number of pixels for fitting: %d' % dec.minNPix)
    dec.useEstimatedVariance = args.estVar
    dec.setSynthPSF(FWHM=args.psfFWHM, beta=args.psfBeta, size=args.psfSize)
    
    logger.warn('Computing initial model using DE algorithm (takes a LOT of time).')
    t1 = time.time()
    if not path.exists(args.maskFile):
        logger.error('Mask file %s not found.' % args.maskFile)
        exit(1)
    logger.info('Using mask file %s.' % args.maskFile)
    masked_wl = load_line_mask(args.maskFile, dec.wl)
    
    l1 = find_nearest_index(dec.wl, 4500.0)
    l2 = dec.Nl_obs
    cache_file = cube + '.initmodel'
    if not path.exists(cache_file):
        logger.info('Creating gray image for initial model.')
        gray_image, gray_noise, _ = dec.getSpectraSlice(l1, l2, masked_wl)
    else:
        gray_image = None
        gray_noise = None
    initial_model = bd_initial_model(gray_image, gray_noise, dec.PSF, quiet=False, nproc=args.nproc,
                                            cache_model_file=cache_file)
    logger.debug('Refined initial model:\n%s\n' % initial_model)
    logger.warn('Initial model time: %.2f\n' % (time.time() - t1))
    
    t1 = time.time()
    c.zones = np.ma.array(dec.K.qZones, mask=dec.K.qZones < 0)
    c.initialParams = initial_model.getParams()
    c.attrs = dict(PSF_FWHM=args.psfFWHM,
                   PSF_beta=args.psfBeta,
                   PSF_size=args.psfSize,
                   box_step=args.boxStep,
                   box_radius=args.boxRadius,
                   orig_file=cube,
                   mask_file=args.maskFile, 
                   object_name=dec.K.galaxyName,
                   flux_unit=dec.flux_unit,
                   distance_Mpc=dec.K.distance_Mpc,
                   x0=dec.K.x0,
                   y0=dec.K.y0,
                   target_vd=dec.targetVd,
                   wl_FWHM=dec.wlFWHM)
    
    models = dec.fitSpectra(step=50*args.boxStep, box_radius=25*args.boxStep,
                            initial_model=initial_model, mode=args.fitAlgorithm, masked_wl=masked_wl)
    c.firstPassParams = np.array([m.getParams() for m in models], dtype=models[0].dtype)
    logger.info('Done first pass modeling, time: %.2f' % (time.time() - t1))
    
    t1 = time.time()
    logger.info('Smoothing parameters.')
    models = smooth_models(models, dec.wl, degree=1)
    
    logger.info('Starting second pass modeling...')
    models = dec.fitSpectra(step=args.boxStep, box_radius=args.boxRadius,
                            initial_model=models, mode=args.fitAlgorithm, insist=False, masked_wl=masked_wl)
    logger.info('Done second pass modeling, time: %.2f' % (time.time() - t1))
    
    t1 = time.time()
    logger.info('Computing model spectra...')
    c.total.f_obs = dec.flux[::args.boxStep]
    c.total.f_err = dec.error[::args.boxStep]
    c.total.f_flag = dec.flags[::args.boxStep]
    c.total.mask = dec.K.qMask
    c.total.wl = dec.wl[::args.boxStep]
    
    c.bulge.f_obs, c.disk.f_obs = dec.getModelSpectra(models, args.nproc)
    c.bulge.mask = dec.K.qMask
    c.bulge.wl = dec.wl[::args.boxStep]
    c.disk.mask = dec.K.qMask
    c.disk.wl = dec.wl[::args.boxStep]

    # TODO: better array and dtype handling.
    c.fitParams = np.array([m.getParams() for m in models], dtype=models[0].dtype)
    
    flag_bad_fit = c.fitParams['flag'][:, np.newaxis, np.newaxis] > 0.0
    c.updateErrorsFlags(flag_bad_fit)
    c.updateIntegratedSpec()
    
    logger.info('Saving qbick planes...')
    fname = path.join(args.zoneFileDir, '%s_%s-planes.fits' % (galaxyId, sampleId))
    save_qbick_images(c.total, dec, fname, overwrite=args.overwrite)
    fname = path.join(args.zoneFileDir, '%s_%s-bulge-planes.fits' % (galaxyId, sampleId))
    save_qbick_images(c.bulge, dec, fname, overwrite=args.overwrite)
    fname = path.join(args.zoneFileDir, '%s_%s-disk-planes.fits' % (galaxyId, sampleId))
    save_qbick_images(c.disk, dec, fname, overwrite=args.overwrite)
    
    logger.info('Saving to storage...')
    c.writeHDF5(args.db, sampleId, galaxyId, args.overwrite)
    logger.info('Storage complete, time: %.2f' % (time.time() - t1))
    
    return c
Beispiel #6
0
                        help='PSF beta parameter for Moffat profile. If not set, use Gaussian.')
    parser.add_argument('--psf-size', dest='psfSize', type=int, default=15,
                        help='PSF size, in pixels. Must be an odd number.')
    parser.add_argument('--overwrite', dest='overwrite', action='store_true',
                        help='Overwrite data.')
    parser.add_argument('--nproc', dest='nproc', type=int, default=None,
                        help='Number of processors to use.')
    
    return parser.parse_args()
################################################################################


################################################################################
if __name__ =='__main__':
    args = parse_args()
    if args.verbose:
        logger.setLevel(-1)
        logger.debug('Verbose output enabled.')

    sample = load_sample(args.sample)
    sampleId = path.basename(args.sample)

    for gal in sample:
        cube = gal['cube']
        try:
            c = decomp(cube, sampleId, args)
        except Exception as e:
            logger.error('Error decomposing cube %s' % cube)
            logger.error('Exception: %s.' % str(e))
            logger.warn('Skipping to next galaxy.')
Beispiel #7
0
disk_ifs = disk_spec[..., np.newaxis, np.newaxis] * (flux_unit * disk_image)
full_ifs = bulge_ifs + disk_ifs
full_ifs_noise = full_ifs * noise

# FIXME: How to add gaussian noise to spectra?
logger.info("Adding gaussian noise to IFS.")
tmp_noise = np.zeros(ifsshape)
for i in xrange(ifsshape[0]):
    for j in xrange(ifsshape[1]):
        for k in xrange(ifsshape[2]):
            if flagged[j, k]:
                continue
            tmp_noise[i, j, k] = np.random.normal(0.0, full_ifs_noise.data[i, j, k])
full_ifs += tmp_noise

logger.debug("Plotting original model.")
# index_norm = find_nearest_index(l_ssp, 5635.0)
vmin = np.log10(full_image.min())
vmax = np.log10(full_image.max())
fig = plt.figure(figsize=(8, 6))
gs = plt.GridSpec(2, 3, height_ratios=[2.0, 3.0])
ax = plt.subplot(gs[0, 0])
ax.imshow(np.log10(full_image), vmin=vmin, vmax=vmax)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r"Total")

ax = plt.subplot(gs[0, 1])
ax.imshow(np.log10(bulge_image), vmin=vmin, vmax=vmax)
ax.set_xticks([])
ax.set_yticks([])