Skip to content

Commit

Permalink
Merge pull request #154 from cosimoNigro/mv_tests
Browse files Browse the repository at this point in the history
Moved the tests in the respective submodule subdirectories
  • Loading branch information
cosimoNigro committed Jan 9, 2024
2 parents c3be0a3 + a115321 commit c268fa8
Show file tree
Hide file tree
Showing 26 changed files with 72 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -24,7 +24,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pytest-cov
python -m pip install .
python -m pip install -e . --config-settings editable_mode=compat
python -m pip install gammapy
python -m pip install sherpa
- name: Lint with flake8
Expand All @@ -37,7 +37,7 @@ jobs:
run: |
pytest -v --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
Expand Down
File renamed without changes.
Expand Up @@ -11,7 +11,7 @@
from agnpy.targets import PointSourceBehindJet, SSDisk, SphericalShellBLR, RingDustTorus
from agnpy.absorption import Absorption, EBL
from agnpy.utils.math import axes_reshaper
from .utils import (
from agnpy.utils.validation_utils import (
make_comparison_plot,
extract_columns_sample_file,
check_deviation,
Expand All @@ -20,7 +20,7 @@

mec2 = m_e.to("erg", equivalencies=u.mass_energy())

agnpy_dir = Path(__file__).parent.parent
agnpy_dir = Path(__file__).parent.parent.parent
# where to read sampled files
data_dir = agnpy_dir / "data"
# where to save figures
Expand Down
Empty file added agnpy/compton/tests/__init__.py
Empty file.
Expand Up @@ -16,7 +16,7 @@
)
from agnpy.compton import SynchrotronSelfCompton, ExternalCompton
from agnpy.utils.math import trapz_loglog
from .utils import (
from agnpy.utils.validation_utils import (
make_comparison_plot,
extract_columns_sample_file,
check_deviation,
Expand All @@ -25,7 +25,7 @@

agnpy_dir = Path(__file__).parent.parent.parent # go to the agnpy root
# where to read sampled files
data_dir = agnpy_dir / "agnpy/data"
data_dir = agnpy_dir / "data"
# where to save figures, clean-up before making the new
figures_dir_ssc = clean_and_make_dir(agnpy_dir, "crosschecks/figures/compton/ssc")
figures_dir_ec_disk = clean_and_make_dir(
Expand All @@ -39,7 +39,7 @@
# parameters of the Blob and EED used in Figure 7.4 of Dermer 2009
# and for the EC scenario in Finke 2016
R_b = 1e16 * u.cm
V_b = 4 / 3 * np.pi * R_b ** 3
V_b = 4 / 3 * np.pi * R_b**3
W_e_ssc = 1e48 * u.Unit("erg")
W_e_ec = 6e42 * u.Unit("erg")
z_ssc = Distance(1e27, unit=u.cm).z
Expand Down
Empty file.
1 change: 1 addition & 0 deletions agnpy/constraints/tests/test_constraints.py
@@ -0,0 +1 @@
# TODO: add some tests on the contraints class
Empty file.
Expand Up @@ -42,7 +42,7 @@ def test_blob_properties(self):
assert u.isclose(blob.d_L, 5.21497473e28 * u.cm, atol=0 * u.cm, rtol=1e-3)
# - change cosmology
blob = Blob(z=0.1, cosmology=Planck18)
d_L = 1.4682341e+27 * u.cm
d_L = 1.4682341e27 * u.cm
assert u.isclose(blob.d_L, d_L, atol=0 * u.cm, rtol=1e-5)

def test_particles_spectra(self):
Expand Down
Empty file added agnpy/fit/tests/__init__.py
Empty file.
11 changes: 8 additions & 3 deletions agnpy/tests/test_fit.py → agnpy/fit/tests/test_fit.py
Expand Up @@ -236,7 +236,12 @@ def test_pks_1510_fit(self):
wrappers return the same results."""
# electron energy distribution
n_e = LogParabola(
k=1 * u.Unit("cm-3"), p=2.0, q=0.2, gamma_0=1e2, gamma_min=1, gamma_max=3e4,
k=1 * u.Unit("cm-3"),
p=2.0,
q=0.2,
gamma_0=1e2,
gamma_min=1,
gamma_max=3e4,
)

# initialise the wrappers
Expand All @@ -258,8 +263,8 @@ def test_pks_1510_fit(self):
L_disk = 6.7e45 * u.Unit("erg s-1") # disk luminosity
M_BH = 5.71 * 1e7 * M_sun
eta = 1 / 12
m_dot = (L_disk / (eta * c ** 2)).to("g s-1")
R_g = ((G * M_BH) / c ** 2).to("cm")
m_dot = (L_disk / (eta * c**2)).to("g s-1")
R_g = ((G * M_BH) / c**2).to("cm")
R_in = 6 * R_g
R_out = 3e4 * R_g
xi_dt = 0.6
Expand Down
Expand Up @@ -19,10 +19,14 @@
from agnpy.synchrotron import Synchrotron
from agnpy.compton import SynchrotronSelfCompton, ExternalCompton
from agnpy.fit import SynchrotronSelfComptonModel, ExternalComptonModel
from .utils import make_comparison_plot, check_deviation, clean_and_make_dir
from agnpy.utils.validation_utils import (
make_comparison_plot,
check_deviation,
clean_and_make_dir,
)


agnpy_dir = Path(__file__).parent.parent
agnpy_dir = Path(__file__).parent.parent.parent
# where to read sampled files
data_dir = agnpy_dir / "data"
# where to save figures, clean-up before making the new
Expand Down
Empty file.
Empty file.
Empty file.
File renamed without changes.
Empty file added agnpy/spectra/tests/__init__.py
Empty file.
Expand Up @@ -18,7 +18,7 @@

agnpy_dir = Path(__file__).parent.parent.parent # go to the agnpy root
# where to read sampled files
data_dir = agnpy_dir / "agnpy/data"
data_dir = agnpy_dir / "data"

gamma_init_interp = np.logspace(2, 5)
n_e_interp = 1e-3 * u.Unit("cm-3") * gamma_init_interp ** (-2.1)
Expand Down
Empty file.
Expand Up @@ -10,7 +10,7 @@
from agnpy.synchrotron import Synchrotron, nu_synch_peak
from agnpy.synchrotron import ProtonSynchrotron
from agnpy.utils.math import trapz_loglog
from .utils import (
from agnpy.utils.validation_utils import (
make_comparison_plot,
extract_columns_sample_file,
check_deviation,
Expand All @@ -19,18 +19,18 @@

agnpy_dir = Path(__file__).parent.parent.parent # go to the agnpy root
# where to read sampled files
data_dir = agnpy_dir / "agnpy/data"
data_dir = agnpy_dir / "data"
# where to save figures, clean-up before making the new
figures_dir = clean_and_make_dir(agnpy_dir, "crosschecks/figures/proton_synchrotron")


# Define source parameters
B = 10 * u.G
redshift = 0.32
distPKS = Distance(z=redshift) # Already inside blob definition
distPKS = Distance(z=redshift) # Already inside blob definition
doppler_s = 30
Gamma_bulk = 16
R = 1e16 * u.cm #radius of the blob
R = 1e16 * u.cm # radius of the blob


class TestProtonSynchrotron:
Expand All @@ -39,28 +39,31 @@ class TestProtonSynchrotron:
def test_synch_reference_sed(self):
"""Test agnpy proton synchrotron SED against the SED produced by the same particle distribution from Matteo Cerruti's code."""
# reference SED
lognu, lognuFnu = np.genfromtxt(f"{data_dir}/reference_seds/cerruti_psynch/test_pss.dat", dtype = 'float', comments = '#', usecols = (0,4), unpack = True)
nu_ref = 10**lognu * u.Unit('Hz')
sed_ref = 10**lognuFnu *u.Unit("erg cm-2 s-1")
lognu, lognuFnu = np.genfromtxt(
f"{data_dir}/reference_seds/cerruti_psynch/test_pss.dat",
dtype="float",
comments="#",
usecols=(0, 4),
unpack=True,
)
nu_ref = 10**lognu * u.Unit("Hz")
sed_ref = 10**lognuFnu * u.Unit("erg cm-2 s-1")

# agnpy
n_p = ExpCutoffPowerLaw(k=12e4 / u.Unit('cm3'), #12e3 / u.Unit('cm3'),
p = 2.2 ,
gamma_c= 2.5e9,
gamma_min= 1,
n_p = ExpCutoffPowerLaw(
k=12e4 / u.Unit("cm3"), # 12e3 / u.Unit('cm3'),
p=2.2,
gamma_c=2.5e9,
gamma_min=1,
gamma_max=1e20,
mass=m_p
mass=m_p,
)

blob = Blob(R_b=R,
z=redshift,
delta_D=doppler_s,
Gamma=Gamma_bulk,
B=B,
n_p=n_p
blob = Blob(
R_b=R, z=redshift, delta_D=doppler_s, Gamma=Gamma_bulk, B=B, n_p=n_p
)

psynch = ProtonSynchrotron(blob)#, ssa = True)
psynch = ProtonSynchrotron(blob) # , ssa = True)
sed_agnpy = psynch.sed_flux(nu_ref)

# sed comparison plot
Expand All @@ -80,26 +83,22 @@ def test_synch_reference_sed(self):
# requires that the SED points deviate less than 25% from the figure
assert check_deviation(nu_ref, sed_agnpy, sed_ref, 0.25, nu_range)


def test_sed_integration_methods(self):
"""
Test different integration methods against each other:
simple trapezoidal rule vs trapezoidal rule in log-log space.
"""
n_p = ExpCutoffPowerLaw(k=12e4 / u.Unit('cm3'), #12e3 / u.Unit('cm3'),
p = 2.2 ,
gamma_c= 2.5e9,
gamma_min= 1,
n_p = ExpCutoffPowerLaw(
k=12e4 / u.Unit("cm3"), # 12e3 / u.Unit('cm3'),
p=2.2,
gamma_c=2.5e9,
gamma_min=1,
gamma_max=1e20,
mass=m_p
mass=m_p,
)

blob = Blob(R_b=R,
z=redshift,
delta_D=doppler_s,
Gamma=Gamma_bulk,
B=B,
n_p=n_p
blob = Blob(
R_b=R, z=redshift, delta_D=doppler_s, Gamma=Gamma_bulk, B=B, n_p=n_p
)

psynch = ProtonSynchrotron(blob)
Expand Down Expand Up @@ -136,5 +135,5 @@ def test_sed_integration_methods(self):
def test_nu_synch_peak(self):
"""Test peak synchrotron frequency for a given magnetic field and Lorentz factor."""
gamma = 100
nu_synch = nu_synch_peak(1 * u.G, gamma, mass = m_p).to_value("Hz")
nu_synch = nu_synch_peak(1 * u.G, gamma, mass=m_p).to_value("Hz")
assert np.isclose(nu_synch, 15245186.451944835, atol=0)
Expand Up @@ -10,7 +10,7 @@
from agnpy.synchrotron import Synchrotron, nu_synch_peak
from agnpy.utils.math import trapz_loglog

from .utils import (
from agnpy.utils.validation_utils import (
make_comparison_plot,
extract_columns_sample_file,
check_deviation,
Expand All @@ -19,15 +19,15 @@

agnpy_dir = Path(__file__).parent.parent.parent # go to the agnpy root
# where to read sampled files
data_dir = agnpy_dir / "agnpy/data"
data_dir = agnpy_dir / "data"
# where to save figures, clean-up before making the new
figures_dir = clean_and_make_dir(agnpy_dir, "crosschecks/figures/synchrotron")


# parameters of the Blob and EED in Figure 7.4 Dermer and Menon 2009
W_e = 1e48 * u.Unit("erg")
R_b = 1e16 * u.cm
V_b = 4 / 3 * np.pi * R_b ** 3
V_b = 4 / 3 * np.pi * R_b**3
z = Distance(1e27, unit=u.cm).z


Expand Down Expand Up @@ -116,7 +116,11 @@ def test_synch_reference_sed(self, gamma_max, nu_range_max):
],
)
def test_ssa_reference_sed(
self, file_ref, n_e, figure_title, figure_path,
self,
file_ref,
n_e,
figure_title,
figure_path,
):
"""Test SSA SED generated by a given electron distribution against the
ones generated with jetset version 1.1.2, via jetset_ssa_sed.py script."""
Expand Down
Empty file added agnpy/targets/tests/__init__.py
Empty file.
Expand Up @@ -112,7 +112,10 @@ def test_m_dot(self):

@pytest.mark.parametrize(
"R_in, R_out, R_g_units",
[(R_in_tilde_test, R_out_tilde_test, False), (R_in_test, R_out_test, True),],
[
(R_in_tilde_test, R_out_tilde_test, False),
(R_in_test, R_out_test, True),
],
)
def test_R_in_R_out_units(self, R_in, R_out, R_g_units):
"""check if a TypeError is raised when passing R_in and R_out with
Expand Down
Empty file added agnpy/utils/tests/__init__.py
Empty file.
11 changes: 5 additions & 6 deletions agnpy/tests/test_utils.py → agnpy/utils/tests/test_utils.py
Expand Up @@ -6,14 +6,13 @@
import agnpy.utils.math as math
import agnpy.utils.geometry as geom
from agnpy.utils.plot import load_mpl_rc, plot_sed
from .utils import clean_and_make_dir

twopi = 2 * np.pi


def line_loglog(x, m, n):
"""a straight line in loglog-space"""
return x ** m * np.exp(n)
return x**m * np.exp(n)


def integral_line_loglog(x_min, x_max, m, n):
Expand Down Expand Up @@ -62,7 +61,7 @@ def test_x_re_ring_mu_s_perp(self, R_re, r, uu, phi_re):
mu_s = 0.01
x1 = geom.x_re_ring_mu_s(R_re, r, phi_re, uu, mu_s)
x2 = np.sqrt(
r ** 2 + (uu - R_re * np.cos(phi_re)) ** 2 + (R_re * np.sin(phi_re)) ** 2
r**2 + (uu - R_re * np.cos(phi_re)) ** 2 + (R_re * np.sin(phi_re)) ** 2
)
assert np.isclose(x1, x2, atol=0, rtol=0.01)

Expand Down Expand Up @@ -146,7 +145,7 @@ def test_x_re_shell_mu_s(self, phi_re):
uu = 5e17 * u.cm
mu_re = 0.999
mu_s = 0.001
x_true = np.sqrt((r - R_re) ** 2 + uu ** 2)
x_true = np.sqrt((r - R_re) ** 2 + uu**2)
x_shell = geom.x_re_shell_mu_s(R_re, r, phi_re, mu_re, uu, mu_s)
assert np.isclose(x_shell, x_true, atol=0, rtol=0.01)

Expand Down Expand Up @@ -177,9 +176,9 @@ def test_phi_mu_re_shell(self, R_re, r, phi_re, uu, mu_s):
mu_re = 0.99999
phi_shell, mu_shell = geom.phi_mu_re_shell(R_re, r, phi_re, mu_re, uu, mu_s)
phi_true = 0 # unless mu_s = 1 in which case phi_true can be whatever
dx = uu * np.sqrt(1 - mu_s ** 2)
dx = uu * np.sqrt(1 - mu_s**2)
dz = r - R_re + uu * mu_s
mu_true = dz / np.sqrt(dx ** 2 + dz ** 2)
mu_true = dz / np.sqrt(dx**2 + dz**2)
assert np.isclose(mu_shell, mu_true, atol=0.01, rtol=0)
print(phi_true, phi_shell, R_re, r, phi_re, uu, mu_s)

Expand Down
File renamed without changes.

0 comments on commit c268fa8

Please sign in to comment.