sp1w = cube1.get_spectrum(55,55) sp1w.data = wtdavg1 sp2w = cube2.get_spectrum(55,55) sp2w.data = wtdavg2 sp1w.crop(-10,20) sp2w.crop(-10,20) both1dW = pyspeckit.Spectra([sp1w,sp2w]) both1dW.plot_special = types.MethodType(fith2co.plotter_override, both1dW, both1dW.__class__) both1dW.plot_special_kwargs = {'vrange':[-10,20],'fignum':14,'reset_xlimits':True} both1dW.Registry.add_fitter('formaldehyde_radex_tau',formaldehyde_radex_tau_fitter,4,multisingle='multi') both1dW.specfit(fittype='formaldehyde_radex_tau',guesses=[4,12,5,1],verbose=True,quiet=False) both1dW.plot_special(vrange=[-10,20]) # two IRDCs on the west side that have no fits; they are apparently surprisingly low column and density irdc1 = both.get_apspec((48.921,-0.28267,0.0247*3600),coordsys='galactic') irdc2 = both.get_apspec((48.99173,-0.304066,0.0247*3600),coordsys='galactic') for ii,irdc in enumerate((irdc1,irdc2)): irdc.error[irdc.xarr.as_unit('GHz') > 10] = irdc[irdc.xarr.as_unit('GHz') > 10].stats()['std'] irdc.error[irdc.xarr.as_unit('GHz') < 10] = irdc[irdc.xarr.as_unit('GHz') < 10].stats()['std'] irdc.plot_special = types.MethodType(fith2co.plotter_override, irdc, irdc.__class__) irdc.plot_special_kwargs = {'vrange':[30,90],'fignum':145+ii,'reset_xlimits':True} irdc.Registry.add_fitter('formaldehyde_radex_tau',formaldehyde_radex_tau_fitter,4,multisingle='multi') irdc.specfit(fittype='formaldehyde_radex_tau',guesses=[4.5,13.1,47,2.0,4,13,67,2],verbose=True,quiet=False) irdc.plot_special(vrange=[30,90],fignum=145+ii,reset_xlimits=True) print "Something is clearly wrong with how the model is being calculated... the residuals are right, but the models just aren't plotting right." # tooling around with pymc from pymodelfit import FunctionModel1DAuto
def do_indiv_fits(regfilename, outpfx, ncomp=1, dobaseline=False, table=None, tableprefix="", **kwargs): regions = pyregion.open(regfilename) spectra = [both.get_apspec(r.coord_list, coordsys=r.coord_format, wunit='degree') for r in regions] cont11hdu = fits.open(cont11filename)[0] pl.ioff() for ii,(sp,r) in enumerate(zip(spectra,regions)): if hasattr(ncomp,'__len__'): nc = ncomp[ii] else: nc = ncomp chi2, parinfo = {},{} sp.header['REGION'] = "{shape}({ra},{dec},{radius})".format(shape=r.name, ra=r.coord_list[0], dec=r.coord_list[1], radius=r.coord_list[2]) ## this is pretty hacky =( #if dobaseline: # spdict = fith2co.BigSpectrum_to_H2COdict(sp) # for n,s in spdict.items(): # s.baseline(exclude=[2,8,43,75]) # sp = pyspeckit.Spectra(spdict.values()) # spectra[ii] = sp for cont in ('front','back',): print("Continuum in the %s" % cont) fig = pl.figure(ii, figsize=(12,8)) fig.clf() name = r.attr[1]['text'] if cont == 'back': M = pyregion.ShapeList([r]).get_mask(cont11hdu) c11 = cont11[M].mean() c22 = cont22[M].mean() sp.header['CONT11'] = c11 sp.header['CONT22'] = c22 elif cont == 'front': c11 = 2.7315 c22 = 2.7315 dofit(sp, c11, c22, nc, **kwargs) sp.specname = name sp.header['OBJECT'] = name print("ap%i %s: ni=%i, X^2%0.1f, X^2/n=%0.1f, n=%0.1f" % (ii, sp.specname, sp.specfit.fitter.mp.niter, sp.specfit.chi2, sp.specfit.chi2/sp.specfit.dof, sp.specfit.parinfo.DENSITY0.value)) #if sp.specfit.chi2 / sp.specfit.dof > 2: # gg[4] = sp.specfit.parinfo.CENTER0.value # fixed[4] = True # sp.specfit(fittype='formaldehyde_radex',guesses=gg, # fixed=fixed, multifit=True,quiet=False,verbose=True, # limits=limits, # limited=limited, # use_window_limits=False, fit_plotted_area=False) sp.plotter.autorefresh=False plotitem(sp, ii) sp.plotter.savefig(os.path.join(datapath, outpfx+'_aperture_%s_%s.pdf' % (name,cont))) plotitem(sp, ii, dolegend=True) # seriously, something aint'n't right here pl.figure(sp.plotter.figure.number) pl.savefig(os.path.join(datapath, outpfx+'_aperture_%s_%s_legend.pdf' % (name,cont)), bbox_extra_artists=[sp.specfit.fitleg]) # for writing to file, select the best-fit chi2[cont] = sp.specfit.chi2 parinfo[cont] = sp.specfit.parinfo if cont == 'back': # second one... if sp.specfit.chi2 > chi2['front']: print("Back chi^2 > front chi^2. Replacing parameters with Continuum in the Front") best = 'front' else: best = 'back' # set the previous, unset to match this... # since we're guaranteed to be in the 2nd of 2 in a loop here, # no danger table[-1]['frontbackbest'] = best if nc == 2: # or previous *two* if twocomp table[-2]['frontbackbest'] = best # at this stage, 'frontbackbest' should be universally assigned.... if np.any(table['frontbackbest'] == ''): import ipdb; ipdb.set_trace() else: best = '' if table is not None: add_parinfo_to_table(table, sp.specfit.parinfo, sp.specfit.chi2, sp.specfit.dof, sp.specfit.optimal_chi2(reduced=False), sp.specfit.optimal_chi2(reduced=True), ra=r.coord_list[0], dec=r.coord_list[1], radius=r.coord_list[2], frontback=cont, frontbackbest=best, name=tableprefix+sp.specname) if best != '': sp.specfit.parinfo = parinfo[best] sp.specfit.chi2 = chi2[best] sp.write(os.path.join(datapath,outpfx+"_aperture_%s.fits" % name)) table.write(os.path.join(datapath_spectra, tableprefix+"spectralfit_table.ipac"), format='ascii.ipac') pl.ion() return spectra