Ejemplo n.º 1
0
  def __init__(self,infile='/home/troxel/destest/params.ini'):
    from cosmosis.runtime.config import Inifile
    from cosmosis.runtime.pipeline import LikelihoodPipeline

    ini=Inifile(infile)
    self.pipeline=LikelihoodPipeline(ini)
    self.data=self.pipeline.run_parameters([])
Ejemplo n.º 2
0
def _make_cosmosis_pipeline(data, values, pool):
    """ Build a CosmoSIS pipeline.

    Parameters
    ----------
    data: dict
        The data object parse'd from an input yaml file.
        This is passed as-is to the likelihood function

    values: Inifile
        Cosmosis object representing the input parameter values

    pool: MPIPool or None
        If using MPI parallelism, a CosmoSIS pool object.

    Returns
    -------
    pipeline: CosmoSIS pipeline objects
        Instantiated pipeline ready to run.
    """

    # Lie to CosmoSIS about where it is installed.
    os.environ['COSMOSIS_SRC_DIR'] = '.'

    # Build the pipeline that evaluates the likelihood.
    # We avoid printing various bits of output info by silencing stdout on
    # worker nodes.
    if (pool is None) or pool.is_master():
        pipeline = LikelihoodPipeline(load=False, values=values,
                                      priors=None)
    else:
        with stdout_redirected():
            pipeline = LikelihoodPipeline(load=False, values=values,
                                          priors=None)

    # Flush now to print out the master node's setup stdout
    # before printing the worker likelihoods
    sys.stdout.flush()

    # Set up a single cosmosis module, which will be the interface
    # file in the same directory as this one
    module = Module('firecrown', COSMOSIS_INTERFACE)
    module.setup_functions(data)
    pipeline.modules = [module]

    return pipeline
Ejemplo n.º 3
0
def test_external():

    os.chdir(base_dir)
    mapping_proj = ['ell_0', 'ell_2', 'ell_4']
    make_data_covariance(data_fn=data_fn,
                         covariance_fn=covariance_fn,
                         mapping_proj=mapping_proj)
    ini = Inifile('test_cosmosis.ini')
    pipeline = LikelihoodPipeline(ini)
    data = pipeline.run_parameters([0.2])
    assert data['likelihoods', 'cosmopipe_like'] != 0.

    from cosmosis.samplers.emcee.emcee_sampler import EmceeSampler
    from cosmosis.output.in_memory_output import InMemoryOutput
    output = InMemoryOutput()
    sampler = EmceeSampler(ini, pipeline, output)
    sampler.config()

    while not sampler.is_converged():
        sampler.execute()
Ejemplo n.º 4
0
def gen_blindingfactors(refcosm,
                        shiftcosm,
                        inifor2pt='gen2pt_forblinding.ini',
                        nz_file='two_pt_cov.fits'):
    """
    Given input Cosmology objects refcosm and shiftcosm, plus cosmosis inifiles,
    runs cosmosis pipeline to generate and save C_ell; inifile values are 
    defaults, cosmology params in refcosm, shiftcosm supercede them. 
    """
    twoptdictlist = []
    for c in [refcosm, shiftcosm]:
        ini = Inifile(inifor2pt)
        pipeline = LikelihoodPipeline(ini)

        # set parameters as desired
        for parameter in pipeline.parameters:
            if parameter == ('cosmological_parameters', 'omega_m'):
                parameter.start = c.omega_m
            elif parameter == ('cosmological_parameters', 'h0'):
                parameter.start = c.h0
            elif parameter == ('cosmological_parameters', 'omega_b'):
                parameter.start = c.omega_b
            elif parameter == ('cosmological_parameters', 'sigma8_input'):
                parameter.start = c.sigma8
            elif parameter == ('cosmological_parameters', 'w'):
                parameter.start = c.w0
            elif parameter == ('cosmological_parameters', 'wa'):
                parameter.start = c.wa
            elif parameter == ('bias', 'b_g'):
                parameter.start = c.bias
            elif parameter == ('fits_nz', 'nz_file'):
                parameter = nz_file

        data = pipeline.run_parameters([])
        twoptdictlist.append(twoptdict_from_datablock(data))

    refdict = twoptdictlist[0]
    shiftdict = twoptdictlist[1]
    factordict = get_blindfactors(refdict, shiftdict)

    return factordict
Ejemplo n.º 5
0
    def setup(self):

        super(CosmosisSampler, self).setup()
        self.sampler_name = self.options['sampler']

        from cosmosis.runtime.config import Inifile
        from cosmosis.runtime.pipeline import LikelihoodPipeline
        from cosmosis.samplers.sampler import Sampler

        self.sampler_class = Sampler.registry[self.sampler_name]
        override = {}
        override['pipeline', 'modules'] = ''
        self.ini = Inifile(filename=None, override=override)
        self.pipeline = LikelihoodPipeline(self.ini)
Ejemplo n.º 6
0
  def __init__(self,infile='/home/troxel/destest/params.ini',fitsfile=None,values=None):
    from cosmosis.runtime.config import Inifile
    from cosmosis.runtime.pipeline import LikelihoodPipeline

    ini=Inifile(infile)
    if fitsfile is not None:
      ini.set('fits_nz', 'nz_file', fitsfile)
      ini.set('2pt_like', 'data_file', fitsfile)
    if values is not None:
      ini.set('pipeline', 'values', values)
    ini.set('pipeline','modules',ini.get('pipeline','modules').replace('2pt_like',''))
    ini.set('runtime','sampler','test')
    print ini.get('pipeline', 'values')
    self.pipeline=LikelihoodPipeline(ini)
    self.data=self.pipeline.run_parameters([])
Ejemplo n.º 7
0
def run(sampler, check_prior, check_extra=True, **options):

    sampler_class = Sampler.registry[sampler]

    values = tempfile.NamedTemporaryFile('w')
    values.write("[parameters]\n" "p1=-3.0  0.0  3.0\n" "p2=-3.0  0.0  3.0\n")
    values.flush()

    override = {
        ('runtime', 'root'): os.path.split(os.path.abspath(__file__))[0],
        ("pipeline", "debug"): "F",
        ("pipeline", "quiet"): "T",
        ("pipeline", "modules"): "test1",
        ("pipeline", "extra_output"): "parameters/p3",
        ("pipeline", "values"): values.name,
        ("test1", "file"): "test_module.py",
    }

    for k, v in options.items():
        override[(sampler, k)] = str(v)

    ini = Inifile(None, override=override)

    # Make the pipeline itself
    pipeline = LikelihoodPipeline(ini)

    output = InMemoryOutput()
    sampler = sampler_class(ini, pipeline, output)
    sampler.config()

    while not sampler.is_converged():
        sampler.execute()

    if check_prior:
        pr = np.array(output['prior'])
        # a few samples might be outside the bounds and have zero prior
        assert np.all((pr == -3.58351893845611) | (pr == -np.inf))
        # but not all of them
        assert not np.all(pr == -np.inf)

    if check_extra:
        p1 = output['parameters--p1']
        p2 = output['parameters--p2']
        p3 = output['PARAMETERS--P3']

        assert np.all((p1 + p2 == p3) | (np.isnan(p3)))

    return output
Ejemplo n.º 8
0
def run_cosmosis(args, pool=None):

    # In case we need to hand-hold a naive demo-10 user.
    demo_10_special(args)
    demo_20b_special(args)

    # Load configuration.
    ini = Inifile(args.inifile, override=args.params)

    pre_script = ini.get(RUNTIME_INI_SECTION, "pre_script", fallback="")
    post_script = ini.get(RUNTIME_INI_SECTION, "post_script", fallback="")

    if (pool is None) or pool.is_master():
        # This decodes the exist status
        status = os.WEXITSTATUS(os.system(pre_script))
        if status:
            raise RuntimeError(
                "The pre-run script {} retuned non-zero status {}".format(
                    pre_script, status))

    # Create pipeline.
    pool_stdout = ini.getboolean(RUNTIME_INI_SECTION,
                                 "pool_stdout",
                                 fallback=False)
    if (pool is None) or pool.is_master() or pool_stdout:
        pipeline = LikelihoodPipeline(ini,
                                      override=args.variables,
                                      only=args.only)
        if pipeline.do_fast_slow:
            pipeline.setup_fast_subspaces()

    else:
        # Suppress output on everything except the master process
        with stdout_redirected():
            pipeline = LikelihoodPipeline(ini,
                                          override=args.variables,
                                          only=args.only)
            if pipeline.do_fast_slow:
                pipeline.setup_fast_subspaces()

    # determine the type(s) of sampling we want.
    sample_methods = ini.get(RUNTIME_INI_SECTION, "sampler",
                             fallback="test").split()

    for sample_method in sample_methods:
        if sample_method not in Sampler.registry:
            raise ValueError("Unknown sampler method %s" % (sample_method, ))

    #Get that sampler from the system.
    sampler_classes = [
        Sampler.registry[sample_method] for sample_method in sample_methods
    ]

    if pool:
        if not any(
                issubclass(sampler_class, ParallelSampler)
                for sampler_class in sampler_classes):
            if len(sampler_classes) > 1:
                raise ValueError(
                    "None of the samplers you chose support parallel execution!"
                )
            else:
                raise ValueError(
                    "The sampler you chose does not support parallel execution!"
                )
        for sampler_class in sampler_classes:
            if isinstance(pool, process_pool.Pool) and issubclass(
                    sampler_class,
                    ParallelSampler) and not sampler_class.supports_smp:
                name = sampler_class.__name__[:-len("Sampler")].lower()
                raise ValueError(
                    "Sorry, the {} sampler does not support the --smp flag.".
                    format(name))

    number_samplers = len(sampler_classes)

    #To start with we do not have any estimates of
    #anything the samplers might give us like centers
    #or covariances.
    distribution_hints = Hints()

    #Now that we have a sampler we know whether we will need an
    #output file or not.  By default new samplers do need one.
    for sampler_number, (sampler_class, sample_method) in enumerate(
            zip(sampler_classes, sample_methods)):
        sampler_name = sampler_class.__name__[:-len("Sampler")].lower()

        # The resume feature lets us restart from an existing file.
        # It's not fully rolled out to all the suitable samplers yet though.
        resume = ini.getboolean(RUNTIME_INI_SECTION, "resume", fallback=False)

        # Not all samplers can be resumed.
        if resume and not sampler_class.supports_resume:
            print(
                "NOTE: You set resume=T in the [runtime] section but the sampler {} does not support resuming yet.  I will ignore this option."
                .format(sampler_name))
            resume = False

        if pool is None or pool.is_master():
            print("****************************")
            print("* Running sampler {}/{}: {}".format(sampler_number + 1,
                                                       number_samplers,
                                                       sampler_name))

        if sampler_class.needs_output and \
           (pool is None
            or pool.is_master()
            or sampler_class.parallel_output):

            #create the output files and methods.
            try:
                output_options = dict(ini.items('output'))
            except configparser.NoSectionError:
                sys.stderr.write(
                    "ERROR:\nFor the sampler (%s) you chose in the [runtime] section of the ini file I also need an [output] section describing how to save results\n\n"
                    % sample_method)
                return 1
            #Additionally we tell the output here if
            #we are parallel or not.
            if (pool is not None) and (sampler_class.parallel_output):
                output_options['rank'] = pool.rank
                output_options['parallel'] = pool.size

            #Give different output filenames to the different sampling steps
            #Only change if this is not the last sampling step - the final
            #one retains the name in the output file.
            # Change, e.g. demo17.txt to demo17.fisher.txt
            if ("filename" in output_options) and (sampler_number <
                                                   number_samplers - 1):
                filename = output_options['filename']
                filename, ext = os.path.splitext(filename)
                filename += '.' + sampler_name
                filename += ext
                output_options['filename'] = filename

            #Generate the output from a factory
            output = output_module.output_from_options(output_options, resume)
            output.metadata("sampler", sample_method)

            if ("filename" in output_options):
                print("* Saving output -> {}".format(
                    output_options['filename']))

        else:
            #some samplers, like the test one, do not need an output
            #file of the usual type.  In fact giving them one would be
            #a bad idea, because they might over-write something important.
            #so we just give them none.
            output = None

        # Finish the printout from above
        if pool is None or pool.is_master():
            print("****************************")

        #Initialize our sampler, with the class we got above.
        #It needs an extra pool argument if it is a ParallelSampler.
        #All the parallel samplers can also act serially too.
        if pool and sampler_class.is_parallel_sampler:
            sampler = sampler_class(ini, pipeline, output, pool)
        else:
            sampler = sampler_class(ini, pipeline, output)

        #Set up the sampler - for example loading
        #any resources it needs or checking the ini file
        #for additional parameters.
        sampler.distribution_hints.update(distribution_hints)
        sampler.config()

        # Potentially resume
        if resume and sampler_class.needs_output and \
            sampler_class.supports_resume and \
           (pool is None
            or pool.is_master()
            or sampler_class.parallel_output):
            sampler.resume()

        #If there is an output file, save the ini information to
        #it as well.  We do it here since it's nicer to have it
        #after the sampler options that get written in sampler.config.
        if output is not None:
            #Create a buffer to store the output:
            output.comment("START_OF_PARAMS_INI")
            comment_wrapper = output.comment_file_wrapper()
            ini.write(comment_wrapper)
            output.comment("END_OF_PARAMS_INI")
            #Do the same with the values file.
            #Unfortunately that means reading it in again;
            #if we ever refactor this bit we could eliminate that.
            values_ini = Inifile(pipeline.values_filename)
            output.comment("START_OF_VALUES_INI")
            values_ini.write(comment_wrapper)
            output.comment("END_OF_VALUES_INI")

            output.comment("START_OF_PRIORS_INI")
            for priors_file in pipeline.priors_files:
                prior_ini = Inifile(priors_file)
                prior_ini.write(comment_wrapper)
            output.comment("END_OF_PRIORS_INI")

        # Run the sampler until convergence
        # which really means "finished" here -
        # a sampler can "converge" just by reaching the
        # limit of the number of samples it is allowed.
        if (not pool) or pool.is_master():
            while not sampler.is_converged():
                sampler.execute()
                #Flush any output. This is to stop
                #a problem in some MPI cases where loads
                #of output is built up before being written.
                if output:
                    output.flush()
            # If we are in parallel tell the other processors to end the
            # loop and prepare for the next sampler
            if pool and sampler.is_parallel_sampler:
                pool.close()
        else:
            if sampler.is_parallel_sampler:
                sampler.worker()

        distribution_hints.update(sampler.distribution_hints)

        if output:
            output.close()

    pipeline.cleanup()

    # Extra-special actions we take to mollycoddle a brand-new user!
    demo_1_special(args)
    demo_20a_special(args)

    # User can specify in the runtime section a post-run script to launch.
    # In general this may be less useful than the pre-run script, because
    # often chains time-out instead of actually completing.
    # But we still offer it
    if post_script and ((pool is None) or pool.is_master()):
        # This decodes the exist status
        status = os.WEXITSTATUS(os.system(post_script))
        if status:
            sys.stdout.write(
                "WARNING: The post-run script {} failed with error {}".format(
                    post_script, error))

    return 0
from cosmosis.runtime.config import Inifile
from cosmosis.runtime.pipeline import LikelihoodPipeline
import numpy as np
import pylab

#The easiest way to start a pipeline it from a parameter file.
#You load in the file, and then build a LikelihoodPipeline from it.
#You could modify things in the ini file object after loading
#if you wanted.
ini = Inifile("demos/demo15.ini")
pipeline = LikelihoodPipeline(ini)

#You can modify which parameters you vary here.
#In the next cosmosis version there will be a
#nicer method for doing this.
for parameter in pipeline.parameters:
    if parameter == ("cosmological_parameters", "omega_m"):
        parameter.limits = (0.2, 0.4)

#You can also override these properties if useful
pipeline.quiet = True
pipeline.debug = False
pipeline.timing = False

#Let's look through different values of omega_m
#and get a Galaxy Galaxy-Lensing spectrum for each of them
for omega_m in [0.2, 0.25, 0.3, 0.35, 0.4]:

    #In this method of running the pipeline we
    #pass it a value for each of the parameters
    #we have told it to vary.
Ejemplo n.º 10
0
class _cosmosis(object):

  def __init__(self,infile='/home/troxel/destest/params.ini',fitsfile=None,values=None):
    from cosmosis.runtime.config import Inifile
    from cosmosis.runtime.pipeline import LikelihoodPipeline

    ini=Inifile(infile)
    if fitsfile is not None:
      ini.set('fits_nz', 'nz_file', fitsfile)
      ini.set('2pt_like', 'data_file', fitsfile)
    if values is not None:
      ini.set('pipeline', 'values', values)
    ini.set('pipeline','modules',ini.get('pipeline','modules').replace('2pt_like',''))
    ini.set('runtime','sampler','test')
    print ini.get('pipeline', 'values')
    self.pipeline=LikelihoodPipeline(ini)
    self.data=self.pipeline.run_parameters([])

  def cls(self,i,j,ell=None,interpout=False):

    ell0=self.data['shear_cl','ell']
    cl0=self.data['shear_cl','bin_'+str(i)+'_'+str(j)]

    if ell is None:
      self.ell=ell0
      self.cl=cl0
    else:
      f=scipy.interpolate.interp1d(ell0,cl0)
      self.ell=ell
      self.cl=f(ell)

    if interpout:
      return f
    else:
      return

  def xi(self,i,j,theta=None):

    theta0=self.data['shear_xi','theta']
    xip0=self.data['shear_xi','xiplus_'+str(i)+'_'+str(j)]
    xim0=self.data['shear_xi','ximinus_'+str(i)+'_'+str(j)]

    if theta is None:
      self.theta=theta0/np.pi*180.*60.
      self.xip=xip0
      self.xim=xim0
    else:
      theta=theta*np.pi/180./60.
      f=scipy.interpolate.interp1d(theta0,xip0)
      f2=scipy.interpolate.interp1d(theta0,xim0)
      self.theta=theta
      self.xip=f(theta)
      self.xim=f2(theta)

    return

  def xiobs(self,bandpowers):

    f=scipy.interpolate.interp1d(self.theta,self.xip)
    f2=scipy.interpolate.interp1d(self.theta,self.xim)
    def func(t,f,i):
      return bandpowers.window_theta_geometric(t,i)*f(t)

    self.xipobs=np.zeros(bandpowers.nt)
    self.ximobs=np.zeros(bandpowers.nt)
    for i in range(bandpowers.nt):
      self.xipobs[i]=scipy.integrate.quad(func,bandpowers.tmin[i],bandpowers.tmax[i],args=(f,i))[0]
      self.ximobs[i]=scipy.integrate.quad(func,bandpowers.tmin[i],bandpowers.tmax[i],args=(f2,i))[0]

    return
Ejemplo n.º 11
0
class _cosmosis(object):

  def __init__(self,infile='/home/troxel/destest/params.ini'):
    from cosmosis.runtime.config import Inifile
    from cosmosis.runtime.pipeline import LikelihoodPipeline

    ini=Inifile(infile)
    self.pipeline=LikelihoodPipeline(ini)
    self.data=self.pipeline.run_parameters([])

  def cls(self,i,j,ell=None,interpout=False):

    ell0=self.data['shear_cl','ell']
    cl0=self.data['shear_cl','bin_'+str(i)+'_'+str(j)]

    if ell is None:
      self.ell=ell0
      self.cl=cl0
    else:
      f=scipy.interpolate.interp1d(ell0,cl0)
      self.ell=ell
      self.cl=f(ell)

    if interpout:
      return f
    else:
      return

  def xi(self,i,j,theta=None):

    theta0=self.data['shear_xi','theta']
    xip0=self.data['shear_xi','xiplus_'+str(i)+'_'+str(j)]
    xim0=self.data['shear_xi','ximinus_'+str(i)+'_'+str(j)]

    if theta is None:
      self.theta=theta0
      self.xip=xip0
      self.xim=xim0
    else:
      f=scipy.interpolate.interp1d(theta0,xip0)
      f2=scipy.interpolate.interp1d(theta0,xim0)
      self.theta=theta
      self.xip=f(theta)
      self.xim=f2(theta)

    return

  def xiobs(self,bandpowers):

    f=scipy.interpolate.interp1d(self.theta,self.xip)
    f2=scipy.interpolate.interp1d(self.theta,self.xim)
    def func(t,f,i):
      return bandpowers.window_theta_geometric(t,i)*f(t)

    self.xipobs=np.zeros(bandpowers.nt)
    self.ximobs=np.zeros(bandpowers.nt)
    for i in range(bandpowers.nt):
      self.xipobs[i]=scipy.integrate.quad(func,bandpowers.tmin[i],bandpowers.tmax[i],args=(f,i))[0]
      self.ximobs[i]=scipy.integrate.quad(func,bandpowers.tmin[i],bandpowers.tmax[i],args=(f2,i))[0]

    return
Ejemplo n.º 12
0
if (args.best_fit_value):
    if (args.best_fit_priors):
        output_prior_file_name = args.best_fit_priors
    else:
        output_prior_file_name = "prior_" + args.best_fit_value

ini = Inifile(inifile)
extra_params_ini = ini.get("pipeline", "extra_output").split()

extra_params = []
for param in extra_params_ini:
    param = param.replace("/", "--")
    extra_params.append(param)

# run setup
pipeline = LikelihoodPipeline(ini)
# list of varied parameter names
param_names_varied = pipeline.varied_params

if args.multinest_file:
    # get the starting point from the best fit of the multinest chain if the file is given
    multinest_file = args.multinest_file
    print('using the multinest file:' + multinest_file +
          ' to set the starting point for maxlike')
    file = open(multinest_file)
    line = file.readline()
    parameter_list = (line.replace('#', '')).split()
    multi_chain = np.loadtxt(multinest_file, comments='#')
    cols, param_names = find_cols_for_params(parameter_list, input_names,
                                             parameter_names, ['post', 'like'])
    if (args.max_post):