예제 #1
0
    def __init__(self, roi, nbands, folder):
        """ fit the front and back"""
        from uw.like import Models
        iso_source = roi.get_source('isotrop')
        old_model = iso_source.model.copy()
        roi.sources.set_model(Models.FrontBackConstant(), 'isotrop')
        iso_model = iso_source.model
        roi.reinitialize()

        cx = []
        dx = []
        for eband in range(nbands):
            roi.select(eband)
            print '*** Energy Band {}: iso counts {}'.format(
                eband, [t[1].counts.round() for t in roi.selected])

            iso_model[0] = 1
            iso_model[1] = 1
            n = len(roi.selected)
            roi.fit(range(n))
            u = iso_model.get_all_parameters()
            du = np.array([iso_model.error(i) for i in range(2)])

            cx.append(u)
            dx.append(du)
        roi.select()
        roi.sources.set_model(old_model)
        if folder is not None:
            if not os.path.exists(folder): os.mkdir(folder)
            filename = '{}/{}.pickle'.format(folder, roi.name)
            pickle.dump(dict(val=cx, err=dx), open(filename, 'w'))
            print 'wrote file {}'.format(filename)
        self.val = cx
        self.err = dx
예제 #2
0
def fit_isotropic(roi, nbands=8, folder='isotropic_fit'):
    """ fit only the front and back"""
    from uw.like import Models
    iso_source = roi.get_source('isotrop')
    old_model=iso_source.model.copy()
    roi.sources.set_model(Models.FrontBackConstant(), 'isotrop')
    iso_model=iso_source.model
    roi.reinitialize()

    cx = []
    for eband in range(nbands):
        print '*** Energy Band {}: iso counts {}'.format( eband,
                                [t[1].counts.round() for t in roi[2*eband:2*eband+2]])
        roi.select(eband)
        iso_model[0]=1; iso_model[1]=1
        roi.fit([0,1]); 
        u = iso_model.get_all_parameters();
        cx.append(u)
    roi.select()
    roi.sources.set_model(old_model)
    if folder is not None:
        if not os.path.exists(folder): os.mkdir(folder)
        filename= '{}/{}.pickle'.format(folder, roi.name)
        pickle.dump(np.array(cx), open(filename, 'w'))
        print 'wrote file {}'.format(filename)
    return np.array(cx)
예제 #3
0
def fit_diffuse(roi, nbands=8, select=[0,1,2], restore=False, folder='diffuse_fit'):
    """
    Perform indpendent fits to the gal, iso_front, and iso_back for each of the first nbands bands
    select: None or list of variables
    """
    from uw.like import Models
    # freeze all free sources, thaw gal and iso
    roi.thaw('Norm', 'ring')
    iso_model =roi.sources.find_source('isotrop').model.copy()
    roi.sources.set_model(Models.FrontBackConstant(), 'isotrop')
    roi.reinitialize()
    
    # do the fitting
    dpars=[]
    energies = []
    for ie in range(nbands):
        roi.select(ie); 
        roi.fit(select, ignore_exception=True)
        energies.append(int(roi.energies[0]))
        dpars.append( roi.sources.parameters.get_parameters()[:3])
    t =np.power(10, dpars)
    df = pd.DataFrame(t, columns=['gal iso_front iso_back'.split()])
    df.index=energies
    
    if restore:
        # does not seem to work, comment this out for now
        # restore sources
        roi.sources.diffuse_normalization *= df
        for s,f in zip(free_sources, saved_free):
            s.model.free=f
        roi.sources.find_source('ring').model.free[0]=False
        roi.sources.set_model(iso_model, 'isotrop')
        roi.reinitialize()
        roi.select()
        roi.fit() # needed to restore gradient, at least.
        
        # update the pickle file
        write_pickle(roi)
    elif folder is not None:
        # simply save results
        if not os.path.exists(folder):
            os.mkdir(folder)
        filename= '{}/{}.pickle'.format(folder, roi.name)
        pickle.dump(df, open(filename, 'w'))
        print 'wrote file {}'.format(filename)
    return df
예제 #4
0
파일: sources.py 프로젝트: kerrm/pointlike
def FBconstant(f, b, **kw):
    return Models.FrontBackConstant(f, b, **kw)