Beispiel #1
0
def main():
    args = sys.argv
    if(len(args)<2):
        print("error: the number of input parameters is not correct; input command must be")
        print(">$ python post.py input_file_name[e.g. dist.ini]")
        sys.exit(1)

    Ini = inifile.IniFile(args[1])
    Ini.Dump()

    section = "POSTPROCESS"
    postroot = Ini.ReadString(section,"postroot")
    Ini.Dump(postroot+"_dist.ini")

    chains = Ini.ReadString(section,"chains").split(",")
    chainlabels = Ini.ReadString(section,"chainlabels").split(",")
    names = Ini.ReadString(section,"paramnames").split(",")
    labels = Ini.ReadString(section,"paramlabels").split(",")
    dnames = Ini.ReadString(section,"dparamnames").split(",")
    dlabels = Ini.ReadString(section,"dparamlabels").split(",")

    PP = PostProcess(chains,chainlabels,names,labels,dnames,dlabels)
    PP.ReadChains()

    # triangle plots
    g = plots.getSubplotPlotter()
    g.triangle_plot(PP.getdist_samples,filled=True)
    g.export(postroot+"_triangle.pdf")
def read_file():
    game_state = {}
    data_text, time = encrypt.decrypt()
    data_file = os.path.join(
        os.getenv('TMP'), '.save.ini') if os.name == 'nt' else '/tmp/.save.cfg'

    with open(data_file, 'w') as open_file:
        open_file.write(data_text)

    ini_file = inifile.IniFile(data_file)
    #print(ini_file.to_dict())

    keys = ini_file.keys()
    values = ini_file.values()

    for key, value in zip(keys, values):
        if '.' in key:
            # value is in a category
            klass, prop = key.split('.')
            #print(klass, prop)
            try:
                game_state[klass]
            except KeyError:
                game_state[klass] = {}

            game_state[klass][prop] = value

        else:
            game_state[key] = value

    return game_state
Beispiel #3
0
def main(paramfile,verbose):
    
    Ini = inifile.IniFile(paramfile)
    if(verbose>0):
        Ini.Dump()

    section = "OUTPUT"
    root = Ini.ReadString(section,"root")
    
    # fiducial cosmology
    BG = background.Background(verbose=verbose)
    section = "COSMOLOGY"
    paramsfid = np.array([Ini.ReadFloat(section,"ob"),Ini.ReadFloat(section,"odm"),Ini.ReadFloat(section,"ode"),
                          Ini.ReadFloat(section,"nnu"),Ini.ReadFloat(section,"mnu")])
    BG.SetParams(paramsfid)

    # clumpiness
    section = "NBODY"
    path = Ini.ReadString(section,"clumpiness")
    if(verbose>0):
        print("\n# cluminess (ignored when intype==2)")
        # clumz is ln(B) as function of ln(1+z) constructed from a lookup table of "z B(z)".
    try:
        tab = np.loadtxt(path)
        clumz = interpolate.make_interp_spline(np.log(1+tab[:,0]),np.log(1+tab[:,1]))
        if(verbose>0):
            print(" cluminess factor is read from ",path)
    except:
        if(verbose>0):
            print(" table file for clumpiness factor is missing; cluminess is ignored:")
        clumz = lambda x:0

    # energy deposition
    DE = deposition.Deposit(verbose=verbose)
    section = "DEPOSITION"
    DE.SetParams(Ini.ReadString(section,"epspath"),Ini.ReadInt("INJECTION","intype"))
    DE.Calcfc(BG.dtauda,clumz)

    # injection spectrum phythia
    INJ = injection.Injection(verbose=verbose)
    section = "INJECTION"
    mspec = 5
    INJ.SetParams(DE.intype,Ini.ReadFloat(section,"mass"),Ini.ReadInt(section,"mult"),Ini.ReadInt(section,"mode"),
                  Ini.ReadBoolean(section,"use_prec"),DE.nerg*mspec,DE.erg[0]*const.eV/const.GeV,DE.erg[DE.nerg-1]*const.eV/const.GeV)
    INJ.SetRate(Ini.ReadFloat(section,"sigmav") if INJ.intype==1 else Ini.ReadFloat(section,"gamma"))
    INJ.GetBinnedNumber()

    # IGM evolution
    TH = therm.Therm(verbose=verbose)
    TH.SetParams(root)
    TH.IntegEnergy(DE,INJ)
    TH.ThermInput(BG,DE,INJ)
    TH.EvolveTspin(BG,DE,INJ)

    #for EDGES???
    if(verbose>0):
        print(INJ.mass,INJ.sigmav/const.cm**3,TH.dT21cm_edges)
    return TH.dT21cm_edges
Beispiel #4
0
    async def create_site(self, lektor, name, owner, site_id):
        site_workdir = self.workdir / site_id
        if site_workdir.exists():
            raise ValueError('workdir for such site-id already exists')

        site_repo = await self.create_site_repo(site_id)
        run_local = functools.partial(run, cwd=site_workdir)
        theme_repo = os.environ.get('LEKTORIUM_LEKTOR_THEME', None)
        if theme_repo is not None:
            with tempfile.TemporaryDirectory() as theme_dir:
                await async_run(run, f'git clone {theme_repo} {theme_dir}')
                example_site = pathlib.Path(theme_dir) / 'example-site'
                if example_site.exists():
                    shutil.rmtree(example_site / 'themes', ignore_errors=True)
                    shutil.copytree(example_site, site_workdir)
            site_config = list(site_workdir.glob('*.lektorproject'))
            if site_config:
                site_config = one(site_config)
                config_data = inifile.IniFile(site_config)
                config_data['project.name'] = name
                del config_data['project.url']
                config_data.save()
                site_config.rename(site_workdir / f'{name}.lektorproject')
            else:
                shutil.rmtree(site_workdir, ignore_errors=True)
        if theme_repo is None or not site_workdir.exists():
            lektor.quickstart(name, owner, site_workdir)
            if theme_repo is not None:
                shutil.rmtree(site_workdir / 'templates')
                shutil.rmtree(site_workdir / 'models')
        await async_run(run_local, 'git init')
        await async_run(run_local, 'git lfs install')
        await async_run(run_local, f'git remote add origin {site_repo}')
        await async_run(run_local, 'git fetch')
        await async_run(run_local, 'git reset origin/master')
        (site_workdir / '.gitattributes').write_text(
            os.linesep.join(f'{m} filter=lfs diff=lfs merge=lfs -text'
                            for m in LFS_MASKS), )
        theme_repo = os.environ.get('LEKTORIUM_LEKTOR_THEME', None)
        if theme_repo is not None:
            site_repo_host, _, site_repo_path = str(site_repo).partition(':')
            theme_repo_host, _, theme_repo_path = str(theme_repo).partition(
                ':')
            if site_repo_host == theme_repo_host:
                site_repo_path = len(pathlib.Path(site_repo_path).parts)
                site_repo_path = ('..', ) * site_repo_path
                theme_repo = pathlib.Path(*site_repo_path) / theme_repo_path
            await async_run(
                run_local,
                f'git submodule add {theme_repo} themes/iarcrp-lektor-theme',
            )
        await async_run(run_local, 'git add .')
        await async_run(run_local, 'git commit -m quickstart')
        await async_run(run_local, 'git push --set-upstream origin master')

        return site_workdir, dict(repo=str(site_repo))
Beispiel #5
0
    def test_load_config_file(self):
        with TemporaryDirectory() as tempdir:
            os.mkdir(os.path.join(tempdir, "configs"))
            config = inifile.IniFile(os.path.join(tempdir, "configs",
                                                  "s3.ini"))
            config["section.key"] = "value"
            config.save()

            publisher = lektor_s3.S3Publisher(MockEnvironment(tempdir), "")
            have = publisher.get_config().section_as_dict("section")
            self.assertEqual(have["key"], "value")
Beispiel #6
0
def get_line(filename, pathedname):
    ini = inifile.IniFile(pathedname)

    name = filename    
    desc = ini.get('vars.desc', '<NULL>')
    filesize = ini.get('vars.filesize', '<NULL>')
    log_lines = get_git_log(pathedname)
    created = get_created_commit(log_lines).split(',')[1]
    last_modified = get_last_modified_commit(log_lines).split(',')[1]
    commits = len(log_lines)

    return [ name, desc, filesize, created, last_modified, commits]
Beispiel #7
0
    def ReadChains(self):
        
        for i in range(len(self.chains)):
            fname = self.chains[i]
            label = self.chainlabels[i]
            
            print("\n#chain name:",fname)

            Ini = inifile.IniFile(self.chains[i].replace(".h5","_params.ini"))
            section = "MCMC"
            paramranges = {}
            for key in self.paramnames:
                paramranges[key] = Ini.ReadFloatArray(section,key)[0:2]
            reader = emcee.backends.HDFBackend(fname,read_only=True)
            #tau = reader.get_autocorr_time()
            
            samples = reader.get_chain(flat=True)
            log_prob_samples = reader.get_log_prob(flat=True)
            weights = np.array(log_prob_samples>-np.inf).astype(np.float)
            blob_samples = reader.get_blobs(flat=True)
            blobnames = blob_samples.dtype.names

            flag = False
            for dname in self.dparamnames:
                if not dname in blobnames:
                    print("error: not found a derived parameter ",dname)
                    flag = True
            if(flag): sys.exit(1)
            derived_samples = np.array([blob_samples[dname] for dname in self.dparamnames]).transpose()

            a = np.nanmin(derived_samples,axis=0)
            b = np.nanmax(derived_samples,axis=0)
            dparamranges = np.concatenate([a[:,None],b[:,None]],axis=1)
            
            print(" flat chain shape: {0}".format(samples.shape))
            print(" flat log prob shape: {0}".format(log_prob_samples.shape))
            print(" flat blob shape: {0}".format(blob_samples.shape))
            print(" flat derived chain shape: {0}".format(derived_samples.shape))

            paramnames = []
            paramnames.extend(self.paramnames)
            paramnames.extend(self.dparamnames)
            paramlabels = []
            paramlabels.extend(self.paramlabels)
            paramlabels.extend(self.dparamlabels)
            for j in range(len(self.dparamnames)):
                paramranges[self.dparamnames[j]] = dparamranges[j,:]

            self.getdist_samples.append(
                MCSamples(samples=np.concatenate([samples,derived_samples],axis=1),
                          names=paramnames,loglikes=-log_prob_samples,
                          labels=paramlabels,label=label,ranges=paramranges,weights=weights)
            )

            # derived parameters?
            #print("###")
            #pidx = {}
            #for p in ("ob", "odm", "ol", "decay_rate", "mratio", "nnu", "mnu"):
            #    try:
            #        pidx[p] = self.paramnames.index(p)
            #    except ValueError:
            #        pass
            #if("odm" in self.paramnames and "ol" in self.paramnames):
            #    self.getdist_samples[i].addDerived(np.sqrt(samples[:,pidx["odm"]]+samples[:,pidx["ol"]])*100,name="h_nodecay",label="H_\emptyset",range=None)
            #if("decay_rate" in self.paramnames):
            #    self.getdist_samples[i].addDerived(samples[:,pidx["decay_rate"]],name="lt",label="\\tau {\\rm [Gyr]}",range=None)

            #self.getdist_samples[i].getConvergeTests()

            tau = np.array([self.getdist_samples[i].getCorrelationLength(j) for j in range(len(self.paramnames))])
            burnin = int(2.*np.max(tau))
            thin = max(int(0.5*np.min(tau)),1)
            print(" correlation lengthes: {}".format(tau))
            print(" burn-in: {0}".format(burnin))
            print(" thin: {0}".format(thin))
            print(" burn-in fraction: {0}".format(burnin/len(log_prob_samples)))
            print(" thinned sample number: {0}".format(int(len(log_prob_samples)/thin)))
            self.getdist_samples[i].deleteZeros()
            self.getdist_samples[i].removeBurn(burnin)
            self.getdist_samples[i].thin(thin)

            # constraints
            for j, mean in enumerate(self.getdist_samples[i].getMeans()):
                #print(" %s = %f +/- %f +/- %f" % (self.getdist_samples[i].parLabel(j),\
                #                                  mean, mean - self.getdist_samples[i].confidence(j,upper=True,limfrac=0.16),\
                #                                  mean - self.getdist_samples[i].confidence(j,upper=False,limfrac=0.16)) )
                print(" %s mean = %f" % (self.getdist_samples[i].parLabel(j),mean))
                print("  two-tail error 68 C.L. = +%f, -%f" %(self.getdist_samples[i].confidence(j,upper=True,limfrac=0.16)-mean,\
                                                                  mean-self.getdist_samples[i].confidence(j,upper=False,limfrac=0.16)))
                upper = self.getdist_samples[i].confidence(j,upper=True,limfrac=0.05)
                print("  one-tail upper limit 95 C.L. = %f" % upper)
                lower = self.getdist_samples[i].confidence(j, upper=False, limfrac=0.05)
                print("  one-tail lower limit 95 C.L. = %f" % lower)
Beispiel #8
0
def main():
    args = sys.argv
    if (len(args) < 2):
        print(
            "error: the number of input parameters is not correct; input command must be"
        )
        print(">$ python inifiles.py input_file_name")
        sys.exit(1)

    Ini = inifile.IniFile(args[1])
    Ini.Dump()

    section = "OUTPUT"
    root = Ini.ReadString(section, "root")
    Ini.Dump(root + "_params.ini")

    # model calculation with fiducial parameters
    section = "FIDUCIAL COSMOLOGY"
    paramsfid = np.array([
        Ini.ReadFloat(section, "ob"),
        Ini.ReadFloat(section, "odm"),
        Ini.ReadFloat(section, "ol"),
        Ini.ReadFloat(section, "decay_rate"),
        Ini.ReadFloat(section, "mratio"),
        Ini.ReadFloat(section, "nnu"),
        Ini.ReadFloat(section, "mnu")
    ])
    section = "DDM SETUP"
    BG = mddm.Background(Ini.ReadInt(section, "num_a"),
                         Ini.ReadInt(section, "max_it"),
                         Ini.ReadFloat(section, "tol"),
                         verbose=1)
    BG.SetParams(paramsfid)
    BG.Iterate()

    # parameters for output scale factors
    #BG.Output(np.logspace(np.log10(1e-3),np.log10(1),50,base=10))

    # likelihood calculation
    section = "LIKELIHOODS"
    LF = likelihoods.Likelihood(Ini.ReadBoolean(section, "use_BAO"),
                                Ini.ReadBoolean(section, "use_H0"),
                                Ini.ReadBoolean(section, "use_CMB"),
                                verbose=1)
    lnL = LF.LnLike(BG, 0)
    print(" ln(L)=", lnL[:])

    # MCMC
    #inp = input("\nDoes everything seem going fine? Then let's run MCMC [Y/n]:")
    #if(inp=='n'):
    #    sys.exit()
    section = 'MCMC'
    BG.verbose = LF.verbose = 0
    paramrange = [
        Ini.ReadFloatArray(section, "ob"),
        Ini.ReadFloatArray(section, "odm"),
        Ini.ReadFloatArray(section, "ol"),
        Ini.ReadFloatArray(section, "decay_rate"),
        Ini.ReadFloatArray(section, "mratio"),
        Ini.ReadFloatArray(section, "nnu"),
        Ini.ReadFloatArray(section, "mnu")
    ]
    map_varied = np.array(
        [i for i in range(len(paramrange)) if len(paramrange[i] > 0)])
    range_varied = np.array([paramrange[i] for i in map_varied])
    driver = mcmc.MCMC(paramsfid, Ini.ReadBoolean(section, "parallel"))
    driver.SetParams(map_varied,
                     range_varied,
                     Ini.ReadInt(section, "nwalkers"),
                     verbose=1)
    chain = root + ".h5"
    driver.Run(chain, Ini.ReadInt(section, "nsteps"), BG, LF)
Beispiel #9
0
 def site_config(self, site_id):
     site_root = self._site_dir(site_id)
     config = list(site_root.glob('*.lektorproject'))
     if config:
         return inifile.IniFile(one(config))
     return collections.defaultdict(type(None))
Beispiel #10
0
 def get_config(self):
     return inifile.IniFile(self.config_filename)
Beispiel #11
0
 def get_config(self,ini_file):
     path = os.path.join(self.env.root_path,'configs',ini_file)
     return inifile.IniFile(path)