def test_model_image(imsize, nproc, count, chunk=8): psf = None model_orig = create_model() imfit = Imfit(model_orig, psf=psf, quiet=True, nproc=nproc, chunk_size=chunk) shape = (imsize, imsize) imfit.getModelImage(shape) t1 = time.time() imfit._modelObject._testCreateModelImage(count) return time.time() - t1
def fit_spectra(full_spectra, full_noise, flux_unit, init_model): PSF = moffat_psf(fwhm=2.4, size=9) fitter = Imfit(init_model, PSF, quiet=True) models = [] for i in xrange(0, full_spectra.shape[-1]): # print '#'*50, i f__l = full_spectra[..., i] / flux_unit err__l = full_noise[..., i] / flux_unit fitter.fit(f__l, err__l) if fitter.nValidPixels < 1000: print '(%d) Too many masked pixels.' % i if fitter.nPegged < 0: print '(%d) Pegged parameters.' % i m = fitter.getModelDescription() # print m.bulge.I_e.value, m.bulge.r_e.value, m.disk.I_0.value, m.disk.h.value models.append(m) return models
def fit_image(flux, noise, guess_model, PSF=None, mode='LM', insist=False, quiet=True, nproc=None, use_cash_statistics=False): ''' Doc me! ''' imfit = Imfit(guess_model, PSF, quiet=quiet, nproc=nproc) imfit.fit(flux, noise, mode=mode, error_type='sigma', use_cash_statistics=(mode != 'LM' and use_cash_statistics)) logger.debug('Valid pix: %d | Iter.: %d | pegged: %d | fit stat.: %f | BIC: %f' % \ (imfit.nValidPixels, imfit.nIter, imfit.nPegged, imfit.reducedFitStatistic, imfit.BIC)) fitted_model = imfit.getModelDescription() logger.debug(' Fitted model:\n%s\n\n' % str(fitted_model)) if not imfit.fitConverged or imfit.nPegged > 0: logger.warn('Bad fit: did not converge or pegged parameter.') if mode == 'LM' and insist: logger.warn(' Retrying using N-M simplex.') imfit.fit(flux, noise, mode='NM', error_type='sigma', use_cash_statistics=use_cash_statistics) fitted_model = imfit.getModelDescription() if imfit.fitConverged: logger.debug(' N-M simplex fit statistic: %f | BIC: %f' % (imfit.reducedFitStatistic, imfit.BIC)) else: logger.warn(' Bad fit: N-M simplex did not converge.') logger.debug(' Fitted model:\n%s\n\n' % str(fitted_model)) return fitted_model, imfit.fitConverged, imfit.reducedFitStatistic
def test_fitting(): psf = gaussian_psf(2.5, size=9) model_orig = create_model() imfit = Imfit(model_orig, psf=psf, quiet=False) noise_level = 0.1 shape = (100, 100) image = imfit.getModelImage(shape) noise = image * noise_level image += (np.random.random(shape) * noise) mask = np.zeros_like(image, dtype='bool') imfit.fit(image, noise, mask) model_fitted = imfit.getModelDescription() orig_params = get_model_param_array(model_orig) fitted_params = get_model_param_array(model_fitted) assert_allclose(orig_params, fitted_params, rtol=noise_level)
def get_images(db): K = fitsQ3DataCube(db, smooth=False) mask = ~K.qMask qSignal = np.ma.array(K.qSignal, mask=mask) qNoise = np.ma.array(K.qNoise, mask=mask) # PA, ba = K.getEllipseParams() model = galaxy_model(K.x0, K.y0, bulge=True, disk=True, I_e=1, r_e=12, n=1.5, PA_b=90.0, ell_b=0.5, I_0=1, h=12, PA_d=90, ell_d=0.5) PSF = moffat_psf(fwhm=2.4, size=9) fitter = Imfit(model, PSF, quiet=True) fitter.fit(qSignal, qNoise) model_image = fitter.getModelImage() model = fitter.getModelDescription() bulge_image, disk_image = create_model_images(qSignal.shape, PSF, model) bulge_image = np.ma.array(bulge_image, mask=mask) disk_image = np.ma.array(disk_image, mask=mask) bulge_frac = bulge_image / model_image disk_frac = disk_image / model_image bulge_noise = np.sqrt(bulge_frac) * qNoise disk_noise = np.sqrt(disk_frac) * qNoise return model, qSignal, qNoise, model_image, bulge_image, bulge_noise, disk_image, disk_noise
model.x0.setLimitsRel(5, 5) model.y0.setValue(flux[0].shape[0] / 2) model.y0.setLimitsRel(5, 5) model.addFunction(psf_func) print 'Initial PSF model:' print model good = [] chi2 = [] flag = [] fitmodel = [] modelflux = [] for i, f in enumerate(flux): star = path.basename(cubes[i]) print 'Fitting PSF %d of %d (%s)' % (i, len(flux), star) imfit = Imfit(model, quiet=True) imfit.fit(f, mode='NM', use_model_for_errors=False, use_cash_statistics=False) _goodfraction = float(imfit.nValidPixels) / float(f.size) print ' Good pixels: %.1f %%' % (_goodfraction * 100) good.append(_goodfraction) print ' Fit converged? %s' % imfit.fitConverged flag.append(not imfit.fitConverged) print ' Fit statistic: %f' % imfit.fitStatistic chi2.append(imfit.fitStatistic) _fitmodel = imfit.getModelDescription() fitmodel.append(_fitmodel) model_f = imfit.getModelImage() modelflux.append(model_f) imradius = 10
def rad_prof(model, bins): imfit = Imfit(model, quiet=False) image = imfit.getModelImage((model.y0.value * 2, model.x0.value * 2)) return radialProfile(image, bins, x0, y0, pa=0.0, ba=1.0)
def create_image(shape, PSF, model): imfit = Imfit(model, PSF, quiet=True) image = imfit.getModelImage(shape) return image
def model_image(model, shape, PSF=None, nproc=None): imfit = Imfit(model, PSF, quiet=True, nproc=nproc) return imfit.getModelImage(shape)
model.y0.setLimitsRel(3, 3) model.addFunction(psf_func) print 'Initial PSF model:' print model psfmodels = [] psfflags = [] goodfraction = [] chi2 = [] for i in xrange(psfflux.shape[0]): wl = psf_wl[i] pf = psfflux[i] - psfbg[i] pf[pf <= 0] = np.ma.masked print 'Fitting PSF %d of %d (%s \AA)' % (i, fill_flux.shape[0], wl) imfit = Imfit(model, quiet=True) imfit.fit(pf, mode='NM', use_model_for_errors=False) _goodfraction = float(imfit.nValidPixels) / float(psfflux[i].size) goodfraction.append(_goodfraction) flagged = not imfit.fitConverged or _goodfraction < 0.5 psfflags.append(flagged) print ' Fit statistic: %f' % imfit.fitStatistic print ' Reduced fit statistic: %f' % imfit.reducedFitStatistic print ' Valid pix: %.1f %%' % (_goodfraction * 100.0) print ' Flagged? %s' % flagged chi2.append(imfit.fitStatistic) fitmodel = imfit.getModelDescription() modelimage = imfit.getModelImage() psfmodels.append(fitmodel) print fitmodel