Exemplo n.º 1
0
    def to_synth(self, params_in, **kwargs):
        """Convert to synth basis

        Convert Parameters object with parameters of a given basis into the
        synth basis

        Args:
            params_in (radvel.Parameters or pandas.DataFrame):  radvel.Parameters object or pandas.Dataframe containing 
                orbital parameters expressed in current basis
            noVary (Optional[bool]): if True, set the 'vary' attribute of the returned Parameter objects 
                to '' (used for displaying best fit parameters)

        Returns: 
            Parameters or DataFrame: parameters expressed in the synth basis

        """
        basis_name = kwargs.setdefault('basis_name', self.name)

        if isinstance(params_in, pd.core.frame.DataFrame):
            # Output by emcee
            params_out = params_in.copy()
        else:
            params_out = _copy_params(params_in)

        for num_planet in range(1, 1 + self.num_planets):

            def _getpar(key):
                if isinstance(params_in, pd.core.frame.DataFrame):
                    return params_in['{}{}'.format(key, num_planet)]
                else:
                    return params_in['{}{}'.format(key, num_planet)].value

            def _setpar(key, new_value):
                key_name = '{}{}'.format(key, num_planet)

                if isinstance(params_in, pd.core.frame.DataFrame):
                    params_out[key_name] = new_value
                else:
                    if key_name in params_in:
                        local_vary = params_in[key_name].vary
                        local_mcmcscale = params_in[key_name].mcmcscale
                    elif kwargs.get('noVary', True):
                        local_vary = ''
                        local_mcmcscale = None
                    else:
                        local_vary = True
                        local_mcmcscale = None

                    params_out[key_name] = radvel.model.Parameter(
                        value=new_value,
                        vary=local_vary,
                        mcmcscale=local_mcmcscale)

            # transform into synth basis
            if basis_name == 'per tp e w k':
                # already in the synth basis
                per = _getpar('per')
                tp = _getpar('tp')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')

            if basis_name == 'per tc e w k':
                per = _getpar('per')
                tc = _getpar('tc')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')
                tp = timetrans_to_timeperi(tc, per, e, w)

            if basis_name == 'per tc secosw sesinw logk':
                # pull out parameters
                per = _getpar('per')
                tc = _getpar('tc')
                secosw = _getpar('secosw')
                sesinw = _getpar('sesinw')
                logk = _getpar('logk')

                k = np.exp(logk)
                e = secosw**2 + sesinw**2
                w = np.arctan2(sesinw, secosw)
                tp = timetrans_to_timeperi(tc, per, e, w)

            if basis_name == 'per tc secosw sesinw k':
                # pull out parameters
                per = _getpar('per')
                tc = _getpar('tc')
                secosw = _getpar('secosw')
                sesinw = _getpar('sesinw')
                k = _getpar('k')

                # transform into synth basis
                e = secosw**2 + sesinw**2
                w = np.arctan2(sesinw, secosw)
                tp = timetrans_to_timeperi(tc, per, e, w)

            if basis_name == 'logper tc secosw sesinw k':
                # pull out parameters
                logper = _getpar('logper')
                tc = _getpar('tc')
                secosw = _getpar('secosw')
                sesinw = _getpar('sesinw')
                k = _getpar('k')

                # transform into synth basis
                per = np.exp(logper)
                e = secosw**2 + sesinw**2
                w = np.arctan2(sesinw, secosw)
                tp = timetrans_to_timeperi(tc, per, e, w)

            if basis_name == 'logper tc secosw sesinw logk':
                # pull out parameters
                logper = _getpar('logper')
                tc = _getpar('tc')
                secosw = _getpar('secosw')
                sesinw = _getpar('sesinw')
                k = _getpar('logk')

                # transform into synth basis
                per = np.exp(logper)
                e = secosw**2 + sesinw**2
                k = np.exp(k)
                w = np.arctan2(sesinw, secosw)
                tp = timetrans_to_timeperi(tc, per, e, w)

            if basis_name == 'per tc ecosw esinw k':
                # pull out parameters
                per = _getpar('per')
                tc = _getpar('tc')
                ecosw = _getpar('ecosw')
                esinw = _getpar('esinw')
                k = _getpar('k')

                # transform into synth basis
                e = np.sqrt(ecosw**2 + esinw**2)
                w = np.arctan2(esinw, ecosw)
                tp = timetrans_to_timeperi(tc, per, e, w)

            # shoves synth parameters from namespace into param_out
            _setpar('per', per)
            _setpar('tp', tp)
            _setpar('e', e)
            _setpar('w', w)
            _setpar('k', k)

        if isinstance(params_out, radvel.model.Parameters):
            params_out.basis = Basis('per tp e w k', self.num_planets)
        return params_out
def process_acr_posterior_dataframe(acr_df,
                                    acrlike,
                                    Mstar=1,
                                    meters_per_second=True):
    """
    Process a Pandas dataframe containing the posterior samples from a radvel MCMC fit with
    an ACR model. Columns for orbital parameters and planet masses are are added to the dataframe. 

    Arguments
    ---------
    acr_df : pandas.Dataframe
        Dataframe containing posterior samples
    acrlike : radvel likelihood
        The likelihood object used to compute the ACR model posterior sample.
    Mstar : float (optional)
        Host star mass in solar masses.
        Used for converting semi-amplitudes and periods to planet masses. 
        Default value is 1
    meters_per_second : bool (optional)
        Whether semi-amplitude values are reported in meters per second or km/s.
        True (default) if values are given in m/s.
    """
    acr_df['timebase'] = acrlike.model.time_base
    acr_df['t'] = acr_df.stcosw**2 + acr_df.stsinw**2
    acr_df['Mstar'] = Mstar
    acrfn = acrlike.model._acr_curves_fn
    # Planet 1
    acr_df['pomega1'] = np.arctan2(acr_df.stsinw, acr_df.stcosw)
    acr_df['e1'] = acrfn.e1_rbs(acr_df.m2_by_m1, acr_df.t, grid=False)
    acr_df['tp1'] = timetrans_to_timeperi(acr_df.tc1, acr_df.per1, acr_df.e1,
                                          acr_df.pomega1)
    acr_df['M1'] = 2 * np.pi * (acr_df.timebase - acr_df.tp1) / acr_df.per1
    acr_df['l1'] = np.mod(acr_df.M1 + acr_df.pomega1, 2 * np.pi)
    k1 = acr_df['k1'].values

    if not meters_per_second:
        acr_df['m1'] = np.vectorize(get_planet_mass_from_mstar_K_P_e)(
            acr_df['Mstar'].values, 1e3 * k1, acr_df['per1'].values,
            acr_df['e1'].values)
    else:
        acr_df['m1'] = np.vectorize(get_planet_mass_from_mstar_K_P_e)(
            acr_df['Mstar'].values, k1, acr_df['per1'].values,
            acr_df['e1'].values)

    # Resonance-related variables
    acr_df['jres'] = acrlike.params['jres'].value
    acr_df['kres'] = acrlike.params['kres'].value
    acr_df['angle_n'] = acrlike.params['angle_n'].value
    # Planet 2
    acr_df['m2'] = acr_df.m1 * acr_df.m2_by_m1
    acr_df['e2'] = acrfn.e2_rbs(acr_df.m2_by_m1, acr_df.t, grid=False)
    acr_df['pomega2'] = np.mod(acr_df['pomega1'] + np.pi, 2 * np.pi)
    j, k = acr_df['jres'].values, acr_df['kres'].values

    acr_df['per2'] = j * acr_df.per1 / (j - k)
    M1 = acr_df['M1']
    angle_n = acr_df['angle_n']

    acr_df['M2'] = (1 - k / j) * acr_df.M1 + (
        (1 - j + k + 2 * acr_df.angle_n) / j) * np.pi
    acr_df['tp2'] = acr_df.timebase - acr_df.per2 * acr_df.M2 / 2 / np.pi

    for i in range(1, 3):
        rt_e = acr_df['e{}'.format(i)].apply(np.sqrt)
        pmg = acr_df['pomega{}'.format(i)]
        acr_df['secosw{}'.format(i)] = rt_e * np.cos(pmg)
        acr_df['sesinw{}'.format(i)] = rt_e * np.sin(pmg)
Exemplo n.º 3
0
    def from_synth(self, params_in, newbasis, **kwargs):
        """Convert from synth basis into another basis

        Convert instance of Parameters with parameters of a given basis into the synth basis

        Args:
            params_in (radvel.Parameters or pandas.DataFrame):  radvel.Parameters object or pandas.Dataframe containing 
                orbital parameters expressed in current basis
            newbasis (string): string corresponding to basis to switch into
            keep (Optional[bool]): keep the parameters expressed in
                the old basis, else remove them from the output
                dictionary/DataFrame

        Returns:
            dict or dataframe with the parameters converted into the new basis
        """

        if newbasis not in BASIS_NAMES:
            print("{} not valid basis".format(newbasis))
            _print_valid_basis()
            return None

        if isinstance(params_in, pd.core.frame.DataFrame):
            # Output by emcee
            params_out = params_in.copy()
        else:
            params_out = _copy_params(params_in)

        for num_planet in range(1, 1 + self.num_planets):

            def _getpar(key):
                if isinstance(params_in, pd.core.frame.DataFrame):
                    return params_in['{}{}'.format(key, num_planet)]
                else:
                    return params_in['{}{}'.format(key, num_planet)].value

            def _setpar(key, new_value):
                key_name = '{}{}'.format(key, num_planet)

                if isinstance(params_in, pd.core.frame.DataFrame):
                    params_out[key_name] = new_value
                else:
                    if key_name in params_in:
                        local_vary = params_in[key_name].vary
                        local_mcmcscale = params_in[key_name].mcmcscale
                    else:
                        local_vary = True
                        local_mcmcscale = None

                    params_out[key_name] = radvel.model.Parameter(
                        value=new_value,
                        vary=local_vary,
                        mcmcscale=local_mcmcscale)

            def _delpar(key):
                if isinstance(params_in, OrderedDict):
                    del params_out['{}{}'.format(key, num_planet)]
                elif isinstance(params_in, pd.core.frame.DataFrame):
                    params_out.drop('{}{}'.format(key, num_planet))

            if newbasis == 'per tc e w k':
                per = _getpar('per')
                e = _getpar('e')
                w = _getpar('w')
                tp = _getpar('tp')

                _setpar('tc', timeperi_to_timetrans(tp, per, e, w))
                _setpar('w', w)

                if not kwargs.get('keep', True):
                    _delpar('tp')

            if newbasis == 'per tc secosw sesinw logk':
                per = _getpar('per')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')
                try:
                    tp = _getpar('tp')
                except KeyError:
                    tc = _getpar('tc')
                    tp = timetrans_to_timeperi(tc, per, e, w)
                    _setpar('tp', tp)

                _setpar('secosw', np.sqrt(e) * np.cos(w))
                _setpar('sesinw', np.sqrt(e) * np.sin(w))
                _setpar('logk', np.log(k))
                _setpar('tc', timeperi_to_timetrans(tp, per, e, w))

                if not kwargs.get('keep', True):
                    _delpar('tp')
                    _delpar('e')
                    _delpar('w')
                    _delpar('k')

                # basis_name = newbasis
                self.params = newbasis.split()

            if newbasis == 'per tc secosw sesinw k':
                per = _getpar('per')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')
                try:
                    tp = _getpar('tp')
                except KeyError:
                    tp = timetrans_to_timeperi(_getpar('tc'), per, e, w)
                    _setpar('tp', tp)
                _setpar('secosw', np.sqrt(e) * np.cos(w))
                _setpar('sesinw', np.sqrt(e) * np.sin(w))
                _setpar('k', k)
                _setpar('tc', timeperi_to_timetrans(tp, per, e, w))

                if not kwargs.get('keep', True):
                    _delpar('tp')
                    _delpar('e')
                    _delpar('w')

                self.name = newbasis
                self.params = newbasis.split()

            if newbasis == 'logper tc secosw sesinw k':
                per = _getpar('per')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')
                try:
                    tp = _getpar('tp')
                except KeyError:
                    tp = timetrans_to_timeperi(_getpar('tc'), per, e, w)
                    _setpar('tp', tp)
                _setpar('logper', np.log(per))
                _setpar('secosw', np.sqrt(e) * np.cos(w))
                _setpar('sesinw', np.sqrt(e) * np.sin(w))
                _setpar('k', k)
                _setpar('tc', timeperi_to_timetrans(tp, per, e, w))

                if not kwargs.get('keep', True):
                    _delpar('per')
                    _delpar('tp')
                    _delpar('e')
                    _delpar('w')

                self.name = newbasis
                self.params = newbasis.split()

            if newbasis == 'logper tc secosw sesinw logk':
                per = _getpar('per')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')
                try:
                    tp = _getpar('tp')
                except KeyError:
                    tp = timetrans_to_timeperi(_getpar('tc'), per, e, w)
                    _setpar('tp', tp)
                _setpar('logper', np.log(per))
                _setpar('secosw', np.sqrt(e) * np.cos(w))
                _setpar('sesinw', np.sqrt(e) * np.sin(w))
                _setpar('logk', np.log(k))
                _setpar('tc', timeperi_to_timetrans(tp, per, e, w))

                if not kwargs.get('keep', True):
                    _delpar('per')
                    _delpar('tp')
                    _delpar('e')
                    _delpar('w')
                    _delpar('k')

                self.name = newbasis
                self.params = newbasis.split()

            if newbasis == 'per tc ecosw esinw k':
                per = _getpar('per')
                e = _getpar('e')
                w = _getpar('w')
                k = _getpar('k')
                try:
                    tp = _getpar('tp')
                except KeyError:
                    tp = timetrans_to_timeperi(_getpar('tc'), per, e, w)
                    _setpar('tp', tp)
                _setpar('ecosw', e * np.cos(w))
                _setpar('esinw', e * np.sin(w))
                _setpar('k', k)
                _setpar('tc', timeperi_to_timetrans(tp, per, e, w))

                if not kwargs.get('keep', True):
                    _delpar('tp')
                    _delpar('e')
                    _delpar('w')

                self.name = newbasis
                self.params = newbasis.split()

        params_out.basis = Basis(newbasis, self.num_planets)

        return params_out
Exemplo n.º 4
0
    def v_to_synth(self, params_in, **kwargs):
        basis_name = kwargs.setdefault('basis_name', self.name)
        if isinstance(params_in, radvel.Vector):
            vector = params_in.vector
        else:
            vector = params_in

        vector2 = vector.copy()

        def setvary(indices):
            if kwargs.get('noVary', True):
                for i in indices:
                    vector2[i][1] = False
                    vector2[i][2] = None
            else:
                for i in indices:
                    vector2[i][1] = True
                    vector2[i][2] = None

        for num_planet in range(self.num_planets):

            #per: (5*num_planet)
            #tp: 1 + (5*num_planet)
            #e: 2 + (5*num_planet)
            #w: 3 + (5*num_planet)
            #k: 4 + (5*num_planet)

            if basis_name == 'per tp e w k':
                return vector2

            if basis_name == 'per tc e w k':
                vector2[1+(5*num_planet)][0] = timetrans_to_timeperi(vector[1+(5*num_planet)][0],
                                                                     vector[(5*num_planet)][0],
                                                                     vector[2+(5*num_planet)][0],
                                                                     vector[3+(5*num_planet)][0])
                setvary([1+(5*num_planet)])

            if basis_name == 'per tc se w k':
                vector2[2+(5 * num_planet)][0] = vector[2+(5 * num_planet)][0] ** 2
                vector2[1+(5*num_planet)][0] = timetrans_to_timeperi(vector[1+(5*num_planet)][0],
                                                                     vector[(5*num_planet)][0],
                                                                     vector2[2+(5*num_planet)][0],
                                                                     vector[3+(5*num_planet)][0])
                setvary([2+(5*num_planet), 1+(5*num_planet)])

            if basis_name == 'per tc secosw sesinw logk':
                vector2[4+(5*num_planet)][0] = np.exp(vector[4+(5*num_planet)][0])
                vector2[2+(5*num_planet)][0] = vector[2+(5*num_planet)][0]**2 + vector[3+(5*num_planet)][0]**2
                vector2[3+(5 * num_planet)][0] = np.arctan2(vector[3+(5 * num_planet)][0], vector[2+(5 * num_planet)][0])
                vector2[1+(5*num_planet)][0] = timetrans_to_timeperi(vector[1+(5*num_planet)][0],
                                                                     vector[(5*num_planet)][0],
                                                                     vector2[2+(5*num_planet)][0],
                                                                     vector2[3+(5*num_planet)][0])
                setvary([4+(5*num_planet),2+(5 * num_planet),3+(5 * num_planet),1+(5 * num_planet)])

            if basis_name == 'per tc secosw sesinw k':
                vector2[2 + (5 * num_planet)][0] = vector[2+(5 * num_planet)][0] ** 2 + vector[3+(5 * num_planet)][0] ** 2
                vector2[3 + (5 * num_planet)][0] = np.arctan2(vector[3+(5 * num_planet)][0], vector[2+(5 * num_planet)][0])
                vector2[1 + (5 * num_planet)][0] = timetrans_to_timeperi(vector[1+(5 * num_planet)][0],
                                                                         vector[(5 * num_planet)][0],
                                                                         vector2[2+(5 * num_planet)][0],
                                                                         vector2[3+(5 * num_planet)][0])
                setvary([2+(5 * num_planet),3+(5 * num_planet),1+(5 * num_planet)])

            if basis_name == 'logper tc secosw sesinw k':
                vector2[2+(5 * num_planet)][0] = vector[2+(5 * num_planet)][0] ** 2 + vector[3+(5 * num_planet)][0] ** 2
                vector2[3+(5 * num_planet)][0] = np.arctan2(vector[3+(5 * num_planet)][0], vector[2+(5 * num_planet)][0])
                vector2[(5 * num_planet)][0] = np.exp(vector[(5 * num_planet)][0])
                vector2[1 + (5 * num_planet)][0] = timetrans_to_timeperi(vector[1 + (5 * num_planet)][0],
                                                                          vector2[(5 * num_planet)][0],
                                                                          vector2[2+(5 * num_planet)][0],
                                                                          vector2[3 + (5 * num_planet)][0])
                setvary([2+(5 * num_planet),3+(5 * num_planet),(5 * num_planet),1 + (5 * num_planet)])

            if basis_name == 'logper tc secosw sesinw logk':
                vector2[2+(5 * num_planet)][0] = vector[2+(5 * num_planet)][0] ** 2 + vector[3+(5 * num_planet)][0] ** 2
                vector2[3+(5 * num_planet)][0] = np.arctan2(vector[3+(5 * num_planet)][0], vector[2+(5 * num_planet)][0])
                vector2[(5 * num_planet)][0] = np.exp(vector[(5 * num_planet)][0])
                vector2[4 + (5 * num_planet)][0] = np.exp(vector[4 + (5 * num_planet)][0])
                vector2[1 + (5 * num_planet)][0] = timetrans_to_timeperi(vector[1 + (5 * num_planet)][0],
                                                                        vector2[(5 * num_planet)][0],
                                                                        vector2[2+(5 * num_planet)][0],
                                                                        vector2[3+(5 * num_planet)][0])
                setvary([2+(5 * num_planet),3 + (5 * num_planet),(5 * num_planet),4 + (5 * num_planet),1 + (5 * num_planet)])

            if basis_name == 'per tc ecosw esinw k':
                vector2[2 + (5 * num_planet)][0] = np.sqrt(vector[2 + (5 * num_planet)][0] ** 2 + vector[3 + (5 * num_planet)][0] ** 2)
                vector2[3+(5 * num_planet)][0] = np.arctan2(vector[3+(5 * num_planet)][0], vector[2+(5 * num_planet)][0])
                vector2[1 + (5 * num_planet)][0] = timetrans_to_timeperi(vector[1 + (5 * num_planet)][0],
                                                                            vector[(5 * num_planet)][0],
                                                                            vector2[2+(5 * num_planet)][0],
                                                                            vector2[3+(5 * num_planet)][0])
                setvary([3+(5 * num_planet),2+(5 * num_planet),1 + (5 * num_planet)])

            if basis_name == 'logper tp e w logk':
                vector2[4 + (5 * num_planet)][0] = np.exp(vector[4 + (5 * num_planet)][0])
                vector2[(5 * num_planet)][0] = np.exp(vector[(5 * num_planet)][0])
                setvary([4+(5 * num_planet),(5 * num_planet)])

        return vector2