Пример #1
0
def test_create_companion():
    host = RV(semi_amp=1, m1=10, m2=5)
    companion = host.create_companion()

    assert companion.semi_amp == -host.semi_amp * host._params["m1"] / host._params["m2"]
    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp
Пример #2
0
def test_set_ignore_mean():
    rv = RV()
    assert rv.ignore_mean is False
    rv.ignore_mean = True
    assert rv.ignore_mean is True
    rv.ignore_mean = False
    assert rv.ignore_mean is False
Пример #3
0
def test_create_companion_with_k2(k2):
    host = RV(semi_amp=1, m1=10, m2=5, k2=k2)
    companion = host.create_companion()

    assert companion.semi_amp == k2  # does not depend on m1 and m2 if k2 given

    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp
Пример #4
0
def test_full_phase(center, npoints):
    params = {"semi_amp": 1, "period": 15, "tau": 2400000, "omega": 0, "ecc": 0.1}
    rv1 = RV(**params).rv_full_phase(center, npoints)
    rv2 = RV(**params).rv_full_phase(center + 1, npoints)

    assert len(rv1) == npoints
    assert np.allclose(rv1, rv2)
    assert np.allclose(rv1[0], rv1[-1])
Пример #5
0
def test_double_create_companion_returns_host():
    host = RV(semi_amp=1, m1=10, m2=5)
    companion = host.create_companion()

    host_2 = companion.create_companion()

    assert host_2.semi_amp == host.semi_amp
    assert host == host_2
    assert host != companion
Пример #6
0
def test_double_create_companion_with_ratio_returns_host(mass_ratio):
    host = RV(semi_amp=1, m1=10, m2=5)
    print("host parsm", host._params)
    companion = host.create_companion(mass_ratio=mass_ratio)
    print("comp params", companion._params)
    host_2 = companion.create_companion(1.0 / mass_ratio)
    print("host_2 params", host_2._params)
    assert host_2.semi_amp == host.semi_amp
    assert host == host_2
    assert host != companion
Пример #7
0
def test_param_from_dict_and_to_dict_give_the_same_dict():
    params = {"k1": 10, "eccentricity": 0.5, "period": 5, "mean_val": 4, "tau": 1, "omega": 1,
              "m1": 4, "m2": 6, "k2": 100, "name": "test", "ignore_mean": True}

    rv1 = RV.from_dict(params)
    rv1_params = rv1.to_dict()
    assert params == rv1_params
    assert params == rv1._params
    assert rv1_params == rv1._params

    rv2 = RV.from_dict(rv1_params)
    assert rv2 == rv1
Пример #8
0
def test_create_companion_with_mass_ratio(semi_amp, mass_ratio):
    host = RV(semi_amp=1, k1=1, m1=10, m2=5)
    print("host params", host._params)

    companion = host.create_companion(mass_ratio=mass_ratio)
    print("companion params", companion._params)
    assert companion.semi_amp == (- host.semi_amp * mass_ratio)
    assert host.period == companion.period
    assert host.gamma == companion.gamma
    assert host.ecc == companion.ecc
    assert host.tau == companion.tau

    assert companion._params["k2"] == host._params["k1"]
    assert companion._params["k1"] == companion.semi_amp
Пример #9
0
def test_from_dict_works_properly():
    params = {"k1": 10, "eccentricity": 0.5, "period": 5, "mean_val": 4, "tau": 1, "omega": 1, "m1": 4, "m2": 6}

    rv = RV.from_dict(params)
    assert rv._params.get("other_params") is None
    assert rv._params.get("m1") == 4
    assert rv._params.get("m2") == 6
Пример #10
0
def test_mean_anomaly_shape(t, t0, p):
    """Mean anomaly is an angle, doesn't have a constraint value."""
    t = np.array(t)
    ma = RV.mean_anomaly(t, t0, p)

    assert len(t) == len(ma)
    assert isinstance(t, np.ndarray)
Пример #11
0
def test_true_anomaly(ma, ecc):
    ma = np.asarray(ma)
    assume(np.all(np.abs(ma) > 0.0001))
    ta = RV.true_anomaly(ma, ecc)
    E = np.arccos((ecc + np.cos(ta)) / (1 + ecc * np.cos(ta)))

    assert np.allclose(ma, E - ecc * np.sin(E), rtol=0.05)
    assert len(ta) == len(ma)
Пример #12
0
def test_initalize_rv_class_from_dict():
    params = {"k1": 1, "period": 2, "tau": 5000, "omega": 1, "eccentricity": 0.5, "mean_val": 5}
    rv = RV.from_dict(params)
    assert rv.semi_amp == params["k1"]
    assert rv.period == params["period"]
    assert rv.ecc == params["eccentricity"]
    assert rv.tau == params["tau"]
    assert rv.gamma == params["mean_val"]
    assert rv.omega == params["omega"]
Пример #13
0
def test_initalize_rv_class_from_file():
    paramfile = "tests/test_params.txt"
    rv = RV.from_file(paramfile)
    params = parse_paramfile(paramfile)

    assert rv.semi_amp == params["k1"]
    assert rv.period == params["period"]
    assert rv.ecc == params["eccentricity"]
    assert rv.tau == params["tau"]
    assert rv.gamma == params["mean_val"]
    assert rv.omega == params["omega"]
Пример #14
0
def test_RV_to_dict_updates_parameters_in_params():
    rv = RV(semi_amp=1.0, period=3, tau=4, omega=5, ecc=0.5, mean_val=8, ignore_mean=True)
    assert rv.ignore_mean == True
    assert rv._params["ignore_mean"] == True
    assert rv.semi_amp == 1.0

    # _params not updated (yet)
    rv.semi_amp = 2
    rv.ignore_mean = False
    assert rv._params["k1"] == 1
    assert rv._params["ignore_mean"] == True
    assert rv.ignore_mean == False

    # RV.to_dict() updates _params
    param_dict = rv.to_dict()
    assert param_dict["ignore_mean"] == False
    assert rv._params["ignore_mean"] == False
    assert rv.semi_amp == 2
    assert param_dict["k1"] == 2
    assert rv._params["k1"] == 2
Пример #15
0
def test_companion_without_mass_gives_errors():
    rv = RV()
    with pytest.raises(ValueError):
        # Needs mass parameters
        rv.create_companion()
Пример #16
0
def test_rv_object_instance_of_rv_class():
    rv = RV()
    assert isinstance(rv, RV)
Пример #17
0
def test_true_anomaly_errors(ecc):
    with pytest.raises(TypeError):
        RV.true_anomaly([], ecc)

    with pytest.raises(ValueError):
        RV.true_anomaly(np.array([]), ecc)
Пример #18
0
def test_true_anomaly_with_scalar(ma, ecc):
    assume(abs(ma) > 0.001)
    ta = RV.true_anomaly(ma, ecc)
    E = np.arccos((ecc + np.cos(ta)) / (1 + ecc * np.cos(ta)))
    assert np.allclose(ma, E - ecc * np.sin(E))
    assert len(ta) == 1
Пример #19
0
def test_rv_class_max_amp_on_elipse(semi_amp, period, ecc, tau, gamma, omega, expected_amp):
    rv = RV(semi_amp, period, ecc, tau, gamma, omega)
    assert rv.max_amp() <= abs(semi_amp * (1 + ecc))  # omega = 0, 2pi etc
    assert rv.max_amp() == expected_amp
Пример #20
0
def test_rv_class_max_amp_on_circle(semi_amp, period, tau, gamma, omega):
    ecc = 0
    rv = RV(semi_amp, period, ecc, tau, gamma, omega)
    assert rv.max_amp() == semi_amp
Пример #21
0
def test_RV_can_handle_ignore_mean_as_input(ignore):
    rv = RV(ignore_mean=ignore)
    assert rv.ignore_mean == ignore
    assert rv._params["ignore_mean"] == ignore
    assert "ignore_mean" in rv.to_dict().keys()
Пример #22
0
def main(params, mode="phase", obs_times=None, obs_list=None, date=None,
         cycle_fraction=1, phase_center=0, save_only=False):  # obs_times=None, mode='phase', rv_diff=None
    # type: (Dict[str, Union[str, float]], str, List[str], str, str, bool) -> None
    r"""Radial velocity displays.

    Parameters
    ----------
    params: str
        Filename for text file containing the rv parameters. Format of 'param = value\n'.
    mode: str
        Mode for script to use. Phase, time, future.
    obs_times: list of str
        Dates of observations added manually at command line of format YYYY-MM-DD.
    obs_list: str
        Filename for list which contains obs_times (YYY-MM-DD HH:MM:SS).
    date: str
        Reference date for some modes. Defaults=None)
    """

    # Load in params and store as a dictionary
    parameters = parse_paramfile(params)

    parameters = prepare_mass_params(parameters, only_msini=True)

    parameters = check_core_parameters(parameters)

    # combine obs_times and obs_list and turn into jd.
    if obs_times:
        if (".txt" in obs_times) or (".dat" in obs_times):
            raise ValueError("Filename given instead of dates for obs_times.")

    obs_times = join_times(obs_times, obs_list)
    obs_jd = strtimes2jd(obs_times)

    test_jd = obs_jd[0] if isinstance(obs_jd, (list, tuple)) else obs_jd

    if ((str(parameters["tau"]).startswith("24") and not str(test_jd).startswith("24")) or
        (not(str(parameters["tau"]).startswith("24")) and str(test_jd).startswith("24"))):
        raise ValueError("Mismatch between Tau parameter '{0}' and times used '{1}'.".format(parameters["tau"], obs_jd))

    date_split = JulianDate.now().jd if date is None else JulianDate.from_str(date).jd
    # Slit past and future obs
    future_obs = [obs for obs in obs_jd if obs > date_split]
    past_obs = [obs for obs in obs_jd if obs <= date_split]

    host_orbit = RV.from_dict(parameters)
    companion_orbit = host_orbit.create_companion()

    if mode == "phase":
        fig = binary_phase_curve(host_orbit, companion_orbit, t_past=past_obs,
                                 t_future=future_obs,
                                 cycle_fraction=cycle_fraction,
                                 phase_center=phase_center)
    elif mode == "time":
        fig = binary_time_curve(host_orbit, companion_orbit, t_past=past_obs,
                                start_day=date_split, t_future=future_obs,
                                cycle_fraction=cycle_fraction)
    else:
        raise NotImplementedError("Other modes are not Implemented yet.")
    if not save_only:
        fig.show()

    return fig
Пример #23
0
def test_anomaly_gives_runtime_error():
    with pytest.raises(RuntimeError):
        RV.true_anomaly(0.0000001, 0.9999999, niterationmax=5)
Пример #24
0
def test_companion_label_with_no_flags():
    assert generate_companion_label(RV()) == "M2 Companion"
Пример #25
0
def test__repr__():
    rv = RV(semi_amp=1.0, period=1, omega=35, k2=7, m1=0.81, tau=3561.51)
    assert isinstance(rv.__repr__(), str)
    assert rv.__repr__() == "RV(semi_amp=1.0, period=1, ecc=0.0, tau=3561.51, omega=35, gamma=0.0, k2=7, m1=0.81)"

    assert RV().__repr__() == "RV(semi_amp=0.0, period=0.0, ecc=0.0, tau=0.0, omega=0.0, gamma=0.0)"
Пример #26
0
def main(params,
         mode="phase",
         obs_times=None,
         obs_list=None,
         date=None,
         cycle_fraction=1,
         phase_center=0,
         save_only=False):  # obs_times=None, mode='phase', rv_diff=None
    # type: (Dict[str, Union[str, float]], str, List[str], str, str, bool) -> None
    r"""Radial velocity displays.

    Parameters
    ----------
    params: str
        Filename for text file containing the rv parameters. Format of 'param = value\n'.
    mode: str
        Mode for script to use. Phase, time, future.
    obs_times: list of str
        Dates of observations added manually at command line of format YYYY-MM-DD.
    obs_list: str
        Filename for list which contains obs_times (YYY-MM-DD HH:MM:SS).
    date: str
        Reference date for some modes. Defaults=None)
    """

    # Load in params and store as a dictionary
    parameters = parse_paramfile(params)

    parameters = prepare_mass_params(parameters, only_msini=True)

    parameters = check_core_parameters(parameters)

    # combine obs_times and obs_list and turn into jd.
    if obs_times:
        if (".txt" in obs_times) or (".dat" in obs_times):
            raise ValueError("Filename given instead of dates for obs_times.")

    obs_times = join_times(obs_times, obs_list)
    obs_jd = strtimes2jd(obs_times)

    test_jd = obs_jd[0] if isinstance(obs_jd, (list, tuple)) else obs_jd

    if ((str(parameters["tau"]).startswith("24")
         and not str(test_jd).startswith("24"))
            or (not (str(parameters["tau"]).startswith("24"))
                and str(test_jd).startswith("24"))):
        raise ValueError(
            "Mismatch between Tau parameter '{0}' and times used '{1}'.".
            format(parameters["tau"], obs_jd))

    date_split = JulianDate.now().jd if date is None else JulianDate.from_str(
        date).jd
    # Slit past and future obs
    future_obs = [obs for obs in obs_jd if obs > date_split]
    past_obs = [obs for obs in obs_jd if obs <= date_split]

    host_orbit = RV.from_dict(parameters)
    companion_orbit = host_orbit.create_companion()

    if mode == "phase":
        fig = binary_phase_curve(host_orbit,
                                 companion_orbit,
                                 t_past=past_obs,
                                 t_future=future_obs,
                                 cycle_fraction=cycle_fraction,
                                 phase_center=phase_center)
    elif mode == "time":
        fig = binary_time_curve(host_orbit,
                                companion_orbit,
                                t_past=past_obs,
                                start_day=date_split,
                                t_future=future_obs,
                                cycle_fraction=cycle_fraction)
    else:
        raise NotImplementedError("Other modes are not Implemented yet.")
    if not save_only:
        fig.show()

    return fig
Пример #27
0
def test_generate_companion_label(msini_flag, k2_flag, ratio_flag, expected):
    orbit = RV(msini_flag=msini_flag, k2_flag=k2_flag, ratio_flag=ratio_flag)
    assert generate_companion_label(orbit) == expected