Exemple #1
0
def read_config(configfile, nplanets, narrow):
    """
    Initialises a sampler object using parameters in config.

    :param string config: string indicating path to configuration file.
    :param int nplanets: integer with the number of planets to be used in the model
    :param float narrow: Float indicating how wide to take the narrow priors.
                         If it is 0, broad priors will be used instead.
    """

    # Import configuration file as module
    c = imp.load_source('c', configfile)

    # Make copy of all relavant dictionaries and lists
    datadict, fpdict, input_dict = map(dict.copy, c.configdicts)
    planet_periods = copy.copy(c.planet_periods)
    periods_std = copy.copy(c.periods_std)
    # Create input_dict in acordance to number of planets in the model
    #input_dict = {'harps': harpsdict, 'drift1': driftdict}
    for i in range(1, nplanets+1):
        input_dict.update({'planet{}'.format(i): copy.deepcopy(fpdict)})
        if narrow:
            # Change the prior range for the planet periods
            if narrow <= 1:
                # Lower limit
                input_dict['planet{}'.format(i)]['period'][2][1] = (1-narrow) * \
                    planet_periods[i-1]
                # Upper limit
                input_dict['planet{}'.format(i)]['period'][2][2] = (1+narrow) * \
                    planet_periods[i-1]
            elif narrow > 1:
                # Lower limit
                input_dict['planet{}'.format(i)]['period'][2][1] = planet_periods[i-1] - \
                    narrow*periods_std[i-1]
                # Upper limit
                input_dict['planet{}'.format(i)]['period'][2][2] = planet_periods[i-1] + \
                    narrow*periods_std[i-1]

    # Create prior instances
    priordict = priors.prior_constructor(input_dict, {})

    # Build list of parameter names
    parnames, _ = get_parnames(input_dict)

    # Read data from file(s)
    read_data(c.datadict)

    # Fixed parameters
    fixedpardict = get_fixedparvalues(input_dict)

    return parnames, datadict, priordict, fixedpardict
Exemple #2
0
def read_config(configfile, data_path, dfile):
    """
    Initialises a sampler object using parameters in config.

    :param string config: string indicating path to configuration file.
    """

    model = int(configfile[-4])

    # Import configuration file as module
    c = imp.load_source('c', configfile)

    # Make copy of all relavant dictionaries
    input_dict, datadict = map(dict.copy, c.configdicts)

    # Build list of parameter names
    parnames, _ = get_parnames(input_dict)

    # Read data from file(s)
    # Extract all names of the data files
    data_files = subprocess.check_output(
        'ls {}'.format(data_path), shell=True).decode('utf-8').split('\n')
    data_files.remove('')  # Remove last empty item
    datadict['harps']['datafile'] = data_path + data_files[dfile-1]
    read_data(datadict)

    # Change prior ranges dependening on dataset
    k = 5
    Tobs = datadict['harps']['data']['Time'].max(
    ) - datadict['harps']['data']['Time'].min()
    freq1 = [1./60 + k/Tobs, 1./60 - k/Tobs]
    input_dict['planet1']['period'][2][1] = 1/freq1[0]
    input_dict['planet1']['period'][2][2] = 1/freq1[1]
    input_dict['planet1']['epoch'][0] = datadict['harps']['data']['Time'].mean()
    if model == 2 or model == 4:
        alpha = 0.05
        freq2 = [(1+alpha)/30, (1-alpha)/30]
        input_dict['planet2']['period'][2][1] = 1/freq2[0]
        input_dict['planet2']['period'][2][2] = 1/freq2[1]
        input_dict['planet2']['epoch'][0] = datadict['harps']['data']['Time'].mean()

    pprint(input_dict)

    # Create prior instances
    priordict = priors.prior_constructor(input_dict, {})

    # Fixed parameters
    fixedpardict = get_fixedparvalues(input_dict)

    return parnames, datadict, priordict, fixedpardict
def read_config(configfile, nplanets, dfile, narrow):
    """
    Initialises a sampler object using parameters in config.

    :param string config: string indicating path to configuration file.
    :param int nplanets: integer with the number of planets to be used in the model
    :param int dfile: number data set to use
    :param float narrow: Float indicating how wide to take the narrow priors.
                         If it is 0, broad priors will be used instead.
    """

    # Import configuration file as module
    c = imp.load_source('c', configfile)

    # Make copy of all relavant dictionaries and lists
    datadict, fpdict, driftdict, eprvdict = map(dict.copy, c.configdicts)
    narrow_priors = dict.copy(c.narrow_priors)

    # Write correct data file path
    datapath = os.path.join(os.getenv('HOME'), 'tesis/codigo/data')
    datadict['eprv']['datafile'] = datapath + '/rvs_000{}.txt'.format(dfile)

    # Create input_dict in acordance to number of planets in the model
    input_dict = {'eprv': eprvdict, 'drift1': driftdict}
    for i in range(1, nplanets+1):
        input_dict.update({'planet{}'.format(i): copy.deepcopy(fpdict)})
        # If narrow is True change period priors to the narrow values
        if narrow:
            input_dict['planet{}'.format(
                i)]['period'][2][1] = narrow_priors[dfile][i-1][0]
            input_dict['planet{}'.format(
                i)]['period'][2][2] = narrow_priors[dfile][i-1][1]

    pprint(input_dict)
    # Create prior instances
    priordict = priors.prior_constructor(input_dict, {})

    # Build list of parameter names
    parnames, _ = get_parnames(input_dict)

    # Read data from file(s)
    read_data(c.datadict)

    # Fixed parameters
    fixedpardict = get_fixedparvalues(input_dict)

    return parnames, datadict, priordict, fixedpardict
def read_config(configfile):
    """
    Initialises a sampler object using parameters in config.

    :param string config: string indicating path to configuration file.
    """

    model = int(configfile[-4])

    # Import configuration file as module
    c = imp.load_source('c', configfile)

    # Make copy of all relavant dictionaries
    input_dict, datadict = map(dict.copy, c.configdicts)

    # Build list of parameter names
    parnames, _ = get_parnames(input_dict)

    # Read the data
    read_data(datadict)

    # Change prior ranges dependening on dataset
    factor = float(5)
    Tobs = datadict['harps']['data']['Time'].max(
    ) - datadict['harps']['data']['Time'].min()
    freq1 = [1. / 95.27 + factor / Tobs, 1. / 95.27 - factor / Tobs]
    input_dict['planet1']['period'][2][1] = 1 / freq1[0]
    input_dict['planet1']['period'][2][2] = 1 / freq1[1]
    if model == 2 or model == 4:
        alpha = 0.05
        freq2 = [(1 + alpha) / 47.635, (1. - alpha) / 47.635]
        input_dict['planet2']['period'][2][1] = 1 / freq2[0]
        input_dict['planet2']['period'][2][2] = 1 / freq2[1]

    pprint(input_dict)

    # Create prior instances
    priordict = priors.prior_constructor(input_dict, {})

    # Fixed parameters
    fixedpardict = get_fixedparvalues(input_dict)

    return parnames, datadict, priordict, fixedpardict
datasets = ['0001', '0002', '0003', '0004', '0005', '0006']
narrow_priors = {
    1: [(39.8107, 44.6684), (11.4815, 12.8825), (10.0, 10.7452)],
    2: [(15.4882, 16.2181), (14.7911, 17.0608), (158.489, 251.189)],
    3: [(81.2831, 107.152), (38.0189, 42.658), (16.5959, 17.5792)],
    4: [(138.038, 204.174), (15.1356, 16.5959), (398.107, 1000.0)],
    5: [(29.5121, 32.3594), (10.7152, 11.4815), (18.197, 19.9526)],
    6: [(79.4328, 141.254), (31.6228, 50.1187), (316.228, 398.107)]
}

narrowZ_notnorm = pd.read_csv('eprv3_narrowZ.txt')
narrowZ_norm = pd.read_csv('eprv3_narrowZ.txt')

input_dict = {'harps': {'period': [0, 1, ['Jeffreys', 1.25, 1e4], 0.13]}}

prior_dict = priors.prior_constructor(input_dict, {})
prior = prior_dict['harps_period']

# x = np.linspace(1.25, 1e4, 1000)
# pdf = prior.pdf(x)

for i, dset in enumerate(datasets):
    integrales = np.ones(4)
    for n in range(3):
        x = np.linspace(narrow_priors[i + 1][n][0], narrow_priors[i + 1][n][1],
                        1000)
        y = prior.pdf(x)
        integrales[n] = np.trapz(y, x)
        narrowZ_norm[dset][n+1] = narrowZ_notnorm[dset][n+1] - \
            np.log10(np.prod(integrales))
Exemple #6
0
def DEMCMC(input_dict,
           datadict,
           customprior_dict,
           N,
           Nchains,
           randomstart=True,
           **kwargs):
    """
    Implementation of DE-MC code
    (Caj. J. F. Ter Braak, Stat Comput (2006) 16:239-249)
    """

    autocorrect = kwargs.pop('autocorrect', False)
    gamfactor = kwargs.pop('gamma', 0.4)
    beta = kwargs.pop('beta', 1.0)
    randfactor = kwargs.pop('randomfactor', 0.0)
    comment = kwargs.pop('comment', '')
    if comment != '':
        comment = comment + '_'

    resume = kwargs.pop('resume', None)
    save = kwargs.pop('save', True)

    ### INITIALISATION

    # Construct initial state from info in input_dict
    X, labeldict = state_constructor(input_dict)

    # Prepare dictionary with prior functions
    priordict = prior_constructor(input_dict, customprior_dict)

    jumpind = []
    for i, xx in enumerate(labeldict.keys()):
        if labeldict[xx].jump:
            jumpind.append(i)

    ## DEFINE GRAND STATE XX
    XX = n.zeros((N, Nchains, len(jumpind) + 3))

    priorx = n.zeros(Nchains)
    logLx = n.zeros(Nchains)

    accepted = n.zeros(Nchains)

    ti = time.time()
    for j in range(Nchains):

        # If requested, start chains at random point in prior
        if randomstart:
            pick_random_point(labeldict, priordict)

        for kk, ii in enumerate(jumpind):

            if resume == None:
                XX[0, j, kk] = labeldict[labeldict.keys()[ii]].get_value()

            else:
                XX[0, j, kk] = resume[-1, j, kk]
                labeldict[labeldict.keys()[ii]].set_value(XX[0, j, kk])

        priorx[j], priorprobx = compute_priors(priordict, labeldict)

        # Compute likelihood for initial state
        haspassed = False

        while not haspassed:
            try:
                Lx, logLx[j], likedictx = get_likelihood(
                    X, input_dict, datadict, labeldict, False, autocorrect)

            except RVgaussianFitError as rvfite:
                print(rvfite)
                print('Input parameters: %f, %f, %f' %
                      (rvfite.contrast, rvfite.rv0, rvfite.sigma))

            except (EvolTrackError, OutofIsochroneError):
                print(
                    'Something went wrong. Initial objects cannot be constructed due to limitations in the stellar evolution tracks.'
                )
                pick_random_point(labeldict, priordict)

            except RuntimeError:
                print(
                    'Something went wrong. Initial objects cannot be constructed due to a Runtime error (possibly Qhull!).'
                )
                pick_random_point(labeldict, priordict)

            except EBOPparamError as eboperr:
                print('Parameter outside limits for JKTEBOP; error message: %s'\
                          %eboperr.message)
                pick_random_point(labeldict, priordict)

            else:
                haspassed = True

    XX[0, :, -3] = priorx
    XX[0, :, -2] = logLx
    XX[0, :, -1] = logLx * log10(e) + n.log10(priorx)

    N = int(N)
    for i in xrange(1, N):

        # Every 500 steps, print
        if (i + 1) % 500 == 0.0:
            print('Step %d out of %d' % (i + 1, N))

        ####
        # Make proposal for each chain
        ####
        for j in range(Nchains):

            # Every 500 steps, print
            if (i + 1) % 500 == 0.0 or ii == 1:
                print('Chain %d: posterior = %f' % (j, XX[i - 1, j, -1]))

            reject = False
            """
            ### Sort the chains according to their merit
            ind = n.argsort(XX[i - 1, :, -1])[::-1]
            # Choose two chains from the best half at random
            indbest = ind[: Nchains/2.]
            random.shuffle(indbest)
            c1, c2 = indbest[:2]
            """

            ### Pick the two chains that will construct the jump vector
            chains = n.concatenate((n.arange(j), n.arange(j + 1, Nchains)))
            random.shuffle(chains)
            c1, c2 = chains[:2]

            ## Construct proposal vector
            xp = XX[i - 1, j, :-3] + gamfactor * (
                XX[i - 1, c1, :-3] - XX[i - 1, c2, :-3]) + n.random.randn(
                    len(XX[i - 1, j, :-3])) * XX[i - 1, j, :-3] * randfactor

            for kk, ii in enumerate(jumpind):
                labeldict[labeldict.keys()[ii]].set_value(xp[kk])

            # Compute prior on proposal state
            priory, priorproby = compute_priors(priordict, labeldict)

            # If proposal state is not possible, reject and continue
            if priory == 0.0:
                XX[i, j, :] = XX[i - 1, j, :]
                continue

            ### Compute likelihood on proposal step
            try:
                Ly, logLy, likedicty = get_likelihood(X, input_dict, datadict,
                                                      labeldict, False,
                                                      autocorrect)

            except RVgaussianFitError as rvfite:
                print(rvfite)
                print('Input parameters: %f, %f, %f' %
                      (self.contrast, self.rv0, self.sigma))
                reject = True

            except (EvolTrackError, OutofIsochroneError):
                #print('One of the requested stars cannot be constructed because it is outside the limits of the evolution tracks.')
                ## Instead of printing, add number of step to TrackError list
                reject = True

            except RuntimeError:
                print(
                    'Runtime Error. Probably and error in Qhull. Rejecting step.'
                )
                reject = True

            except EBOPparamError as eboperr:

                #print('Parameter outside limits for JKTEBOP; error message: %s'\
                #          %eboperr.message)
                reject = True

            else:
                reject = False

            if reject:
                XX[i, j, :] = XX[i - 1, j, :]
                continue

            try:
                r = priory / XX[i - 1, j, -3] * exp(beta *
                                                    (logLy - XX[i - 1, j, -2]))

            except OverflowError:
                try:
                    # If Metropolis ratio overflows, compute rather log(r)
                    # and check that the ratio is greater than 1
                    logr = log(priory) - log(XX[i - 1, j, -3]) \
                        + beta*(logLy - XX[i - 1, j, -2])

                    if logr >= 0.0:
                        accept = True

                except ValueError:
                    # If there's a problem stop execution !!
                    print(priory, XX[i - 1, j, -3])
                    print('Chain: %d; Step: %d' % (j, i))
                    return XX, priory, logLy
                    raise ValueError(
                        'Fatal Error: can not compute Metropolis ratio.\n Priors < 0.0??'
                    )

            else:
                # If r is > 1.0, accept step right away.
                if r >= 1.0:
                    accept = True

                # If not decide whether to accept step or not
                elif scipy.random.random() <= r:
                    accept = True

                else:
                    accept = False

            if accept:
                XX[i, j, :-3] = xp
                XX[i, j, -3] = priory
                XX[i, j, -2] = logLy
                XX[i, j, -1] = logLy * log10(e) + n.log10(priory)
                accepted[j] = accepted[j] + 1
            else:
                XX[i, j, :] = XX[i - 1, j, :]

    print('Running time: %.4f hours' % ((time.time() - ti) / 3600.0))
    """
    ## Create dictionary
    vd = {}

    for i, kk in enumerate(jumpind):
        vd[n.array(labeldict.keys())[jumpind]] = XX[:, :, i]
    vd['prior'] = XX[:, :, -3]
    vd['likelihood'] = XX[:, :, -2]
    """

    if save:
        import datetime
        dt = datetime.datetime.isoformat(datetime.datetime.now())
        f = open(
            '/data/PASTIS/resultfiles/testDEMC/%stestDEMC_nchains%d_gamma%.4f_%s.dat'
            % (comment, Nchains, gamfactor, dt), 'w')
        import pickle
        pickle.dump(
            [XX[::10, :, :], accepted,
             n.array(labeldict.keys())[jumpind]], f)
        f.close()

    return XX, accepted, n.array(labeldict.keys())[jumpind]