Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for doppler factor functions #2379

Merged
merged 14 commits into from
Aug 11, 2023
Merged
46 changes: 0 additions & 46 deletions tardis/montecarlo/montecarlo_numba/tests/test_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,52 +154,6 @@ def test_calculate_tau_electron(electron_density, distance):
assert_almost_equal(actual, expected)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(0.3, 7.5e14, 1 / 5.2e7, 0.9998556693818854),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_doppler_factor(mu, r, inv_t_exp, expected):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = frame_transformations.get_doppler_factor(r, mu, time_explosion)

# Perform required assertions
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(0.3, 7.5e14, 1 / 5.2e7, 1 / 0.9998556693818854),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_inverse_doppler_factor(mu, r, inv_t_exp, expected):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = frame_transformations.get_inverse_doppler_factor(
r, mu, time_explosion
)

# Perform required assertions
assert_almost_equal(obtained, expected)


def test_get_random_mu(set_seed_fixture):
"""
Ensure that different calls results
Expand Down
114 changes: 0 additions & 114 deletions tardis/montecarlo/tests/test_montecarlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,120 +238,6 @@ def test_binary_search(x, x_insert, imin, imax, expected_params):
assert obtained_result == expected_params["result"]


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(0.3, 7.5e14, 1 / 5.2e7, 0.9998556693818854),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_doppler_factor(mu, r, inv_t_exp, expected):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = get_doppler_factor(r, mu, time_explosion)

# Perform required assertions
assert_almost_equal(obtained, expected)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more doppler factor tests below. I would also move them to the new file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chvogl the tests below fail because of this line. Seems like the SuperluminousError is not defined anywhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It looks like it doesn't exist anymore. I also just noticed that all tests in test_montecarlo.py are skipped because of pytestmark = pytest.mark.skip(reason="Port from C to numba"). This explains why the tests did not fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make an issue that we should check if there are any tests in test_montecarlo.py that we actually need but are currently skipped?

@pytest.mark.parametrize(["mu", "r", "inv_t_exp"], [(-0.3, 5, 1e10)])
def test_unphysical_doppler_factor(mu, r, inv_t_exp):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables
with pytest.raises(r_packet.SuperluminalError):
obtained = get_doppler_factor(r, mu, time_explosion)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(0.3, 7.5e14, 1 / 5.2e7, 1 / 0.9998556693818854),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_inverse_doppler_factor(mu, r, inv_t_exp, expected):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = r_packet.get_inverse_doppler_factor(r, mu, time_explosion)

# Perform required assertions
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(["mu", "r", "inv_t_exp"], [(-0.3, 5, 1e10)])
def test_unphysical_inverse_doppler_factor(mu, r, inv_t_exp):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables
with pytest.raises(r_packet.SuperluminalError):
obtained = r_packet.get_inverse_doppler_factor(r, mu, time_explosion)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(-0.3, 10000000, 0.001, 1.0000001000692842),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_doppler_factor_full_relativity(mu, r, inv_t_exp, expected):
# Set the params from test cases here
# TODO: add relativity tests
mc.full_relativity = True
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = get_doppler_factor(r, mu, time_explosion)
mc.full_relativity = False
# Perform required assertions
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(-0.3, 10000000, 0.001, 0.999999899930827),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_inverse_doppler_factor_full_relativity(mu, r, inv_t_exp, expected):
# Set the params from test cases here
# TODO: add relativity tests
mc.full_relativity = True
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = r_packet.get_inverse_doppler_factor(r, mu, time_explosion)
mc.full_relativity = False
# Perform required assertions
assert_almost_equal(obtained, expected)


def test_get_random_mu_different_output():
"""
Ensure that different calls results
Expand Down
141 changes: 141 additions & 0 deletions tardis/transport/tests/test_doppler_factor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import pytest
import numpy as np

import tardis.transport.frame_transformations as frame_transformations
import tardis.montecarlo.montecarlo_numba.r_packet as r_packet

from numpy.testing import (
assert_almost_equal,
)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(0.3, 7.5e14, 1 / 5.2e7, 0.9998556693818854),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_doppler_factor(mu, r, inv_t_exp, expected):
# Set the params from test cases here
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = frame_transformations.get_doppler_factor(r, mu, time_explosion)

# Perform required assertions
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "beta", "expected"],
[
(0.3, 0.2, 0.94),
(-0.3, 0, 1.0),
(0, 0.8, 1.0),
],
)
def test_get_doppler_factor_partial_relativity(mu, beta, expected):
obtained = frame_transformations.get_doppler_factor_partial_relativity(
mu, beta
)
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "beta", "expected"],
[
(0.3, 0.2, 0.95938348),
(-0.3, 0, 1.0),
(0, 0.8, 1.6666667),
],
)
def test_get_doppler_factor_full_relativity(mu, beta, expected):
obtained = frame_transformations.get_doppler_factor_full_relativity(
mu, beta
)
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "r", "inv_t_exp", "expected"],
[
(0.3, 7.5e14, 1 / 5.2e7, 1 / 0.9998556693818854),
(-0.3, 0, 1 / 2.6e7, 1.0),
(0, 1, 1 / 2.6e7, 1.0),
],
)
def test_get_inverse_doppler_factor(mu, r, inv_t_exp, expected):
sonachitchyan marked this conversation as resolved.
Show resolved Hide resolved
# Set the params from test cases here
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables

obtained = frame_transformations.get_inverse_doppler_factor(
r, mu, time_explosion
)

# Perform required assertions
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "beta", "expected"],
[
(0.3, 0.2, 1 / 0.94),
(-0.3, 0, 1.0),
(0, 0.8, 1.0),
],
)
def test_get_inverse_doppler_factor_partial_relativity(mu, beta, expected):
obtained = (
frame_transformations.get_inverse_doppler_factor_partial_relativity(
mu, beta
)
)
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(
["mu", "beta", "expected"],
[
(0.3, 0.2, 1.0818579),
(-0.3, 0, 1.0),
(0, 0.8, 1.6666667),
],
)
def test_get_inverse_doppler_factor_full_relativity(mu, beta, expected):
obtained = frame_transformations.get_inverse_doppler_factor_full_relativity(
mu, beta
)
assert_almost_equal(obtained, expected)


@pytest.mark.parametrize(["mu", "r", "inv_t_exp"], [(-0.3, 5, 1e10)])
def test_unphysical_doppler_factor(mu, r, inv_t_exp):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables
with pytest.raises(r_packet.SuperluminalError):
sonachitchyan marked this conversation as resolved.
Show resolved Hide resolved
obtained = frame_transformations.get_doppler_factor(
r, mu, time_explosion
)


@pytest.mark.parametrize(["mu", "r", "inv_t_exp"], [(-0.3, 5, 1e10)])
def test_unphysical_inverse_doppler_factor(mu, r, inv_t_exp):
# Set the params from test cases here
# TODO: add relativity tests
time_explosion = 1 / inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables
with pytest.raises(r_packet.SuperluminalError):
obtained = r_packet.get_inverse_doppler_factor(r, mu, time_explosion)