Skip to content

Commit e8b48c7

Browse files
committed
Fixed all the previous tests
1 parent d9b1a51 commit e8b48c7

12 files changed

+281
-268
lines changed

pyx/cosmology.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,4 @@ def scale_factor(redshift):
209209
array([1, 0.5, 0.3333333, 0.25])
210210
211211
"""
212-
return (1 + redshift)**-1.0
212+
return (1 + redshift)**-1.0

pyx/io.py

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
'write_row',
1313
]
1414

15-
1615
try:
1716
import h5py
1817
import_h5py = True
@@ -22,7 +21,6 @@
2221
import numpy as np
2322
import yaml
2423

25-
2624
def dict_to_hdf5_attributes(item, attrs_dict):
2725
"""
2826
Adds attributes from a dictionary to a HDF5 dataset or group.

pyx/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ def str2float(string, separator="p", prefix=None, suffix=None):
239239
# This line is a little funky but its a clever way to recombine
240240
# the two sides of the decimal.
241241
# e.g [25, 555] = 25 + 555/(10**3)
242-
flt = int(values[0]) + int(values[1])/(10**len(values[1]))
242+
if len(values) > 1:
243+
flt = int(values[0]) + int(values[1])/(10**len(values[1]))
244+
else:
245+
flt = int(values[0])
243246
return flt
244247

245248

setup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ def get_version():
2929
install_requires=requirements,
3030
classifiers=[
3131
"Programming Language :: Python :: 3",
32-
"Programming Language :: Python :: 3.6",
33-
"Programming Language :: Python :: 3.7",
32+
"Programming Language :: Python :: 3.8",
33+
"Programming Language :: Python :: 3.9",
34+
"Programming Language :: Python :: 3.10",
35+
"Programming Language :: Python :: 3.11",
3436
"License :: OSI Approved :: BSD License",
3537
"Operating System :: MacOS",
3638
"Operating System :: Unix",

tests/test_cosmology.py

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import numpy as np
2+
import pytest
3+
4+
import astropy.units as apu
5+
import astropy.cosmology as acosmo
6+
7+
from pyx import cosmology as pyxcosmo
8+
9+
10+
def test_get_cosmology_from_name():
11+
"""
12+
Test that all cosmologies are load correctly from name.
13+
"""
14+
W5 = acosmo.WMAP5
15+
W7 = acosmo.WMAP7
16+
W9 = acosmo.WMAP9
17+
P13 = acosmo.Planck13
18+
P15 = acosmo.Planck15
19+
P18 = acosmo.Planck18
20+
assert pyxcosmo.get_cosmology_from_name("WMAP5") == W5
21+
assert pyxcosmo.get_cosmology_from_name("WMAP7") == W7
22+
assert pyxcosmo.get_cosmology_from_name("WMAP9") == W9
23+
assert pyxcosmo.get_cosmology_from_name("Planck13") == P13
24+
assert pyxcosmo.get_cosmology_from_name("Planck15") == P15
25+
assert pyxcosmo.get_cosmology_from_name("Planck18") == P18
26+
27+
28+
def test_get_cosmology_from_name_invalid_entry():
29+
"""
30+
Test that passing an invalid string will return a ValueError
31+
"""
32+
with pytest.raises(ValueError):
33+
pyxcosmo.get_cosmology_from_name("NOTPLANCK2020")
34+
35+
36+
def test_get_cosmology_from_name_pass_cosmo():
37+
"""
38+
Test that passing an astropy.cosmology will return that same
39+
cosmology.
40+
41+
"""
42+
W5 = acosmo.WMAP5
43+
W7 = acosmo.WMAP7
44+
W9 = acosmo.WMAP9
45+
P13 = acosmo.Planck13
46+
P15 = acosmo.Planck15
47+
P18 = acosmo.Planck18
48+
assert pyxcosmo.get_cosmology_from_name(W5) == W5
49+
assert pyxcosmo.get_cosmology_from_name(W7) == W7
50+
assert pyxcosmo.get_cosmology_from_name(W9) == W9
51+
assert pyxcosmo.get_cosmology_from_name(P13) == P13
52+
assert pyxcosmo.get_cosmology_from_name(P15) == P15
53+
assert pyxcosmo.get_cosmology_from_name(P18) == P18
54+
55+
56+
def test_z_to_cMpc_check_float_and_int_works():
57+
"""
58+
Test comoving distance is calculated from redshift to match astropy
59+
60+
"""
61+
redshift_int = 1
62+
redshift_float = 1.0
63+
expected_distance = 3395.905416665515 # Calculated from astropy
64+
calculated_distance_float = pyxcosmo.z_to_cMpc(redshift_float, cosmology='Planck15').value
65+
calculated_distance_int = pyxcosmo.z_to_cMpc(redshift_int, cosmology='Planck15').value
66+
# Since expected distance is a float, no not test for exact equality
67+
assert np.isclose(expected_distance, calculated_distance_float)
68+
assert np.isclose(expected_distance, calculated_distance_int)
69+
70+
71+
def test_z_to_cMpc_redshift_zero():
72+
"""
73+
Test 0 Mpc is returned when given 0 redshift
74+
75+
"""
76+
redshift = 0.0
77+
expected_distance = 0.0
78+
calculated_distance = pyxcosmo.z_to_cMpc(redshift, cosmology='Planck15').value
79+
assert np.isclose(expected_distance, calculated_distance)
80+
81+
82+
def test_z_to_cMpc_redshift_array():
83+
"""
84+
Test using a redshift array returns an array of distances
85+
86+
"""
87+
redshift_array = np.array([0.0, 1.0])
88+
89+
# Must return an array
90+
expected_distance_array = np.array([0.0, 3395.905416665515])
91+
calculated_distance_array = pyxcosmo.z_to_cMpc(redshift_array, cosmology='Planck15').value
92+
assert np.isclose(expected_distance_array, calculated_distance_array).all
93+
94+
95+
def test_cMpc_to_z_check_float_and_int_works():
96+
"""
97+
Test redshift is calculated from cMpc with dtype = float
98+
99+
"""
100+
101+
comoving_distance_int = 3000
102+
comoving_distance_float = 3000.0
103+
expected_redshift = 0.8479314667609102 # Calculated from astropy
104+
calculated_redshift_int = pyxcosmo.cMpc_to_z(comoving_distance_int, cosmology='Planck15')
105+
calculated_redshift_float = pyxcosmo.cMpc_to_z(comoving_distance_float, cosmology='Planck15')
106+
assert np.isclose(expected_redshift, calculated_redshift_float)
107+
assert np.isclose(expected_redshift, calculated_redshift_int)
108+
109+
110+
def test_cMpc_to_z_zero():
111+
"""
112+
Test zero comoving distance should return zero redshift
113+
114+
"""
115+
# Zero distance should give zero redshift
116+
comoving_distance = 0.0
117+
expected_redshift = 0.0
118+
calculated_redshift = pyxcosmo.cMpc_to_z(comoving_distance, cosmology='Planck15')
119+
assert np.isclose(expected_redshift, calculated_redshift)
120+
121+
122+
def test_cMpc_to_z_array():
123+
"""
124+
Test passing a comoving distance array returns a redshift array
125+
126+
"""
127+
comoving_distance_array = np.array([0.0, 3000])
128+
# Must return an array
129+
expected_redshift_array = np.array([0.0, 0.8479314667609102])
130+
calculated_redshift_array = pyxcosmo.cMpc_to_z(comoving_distance_array, cosmology='Planck15')
131+
assert np.isclose(expected_redshift_array, calculated_redshift_array).all
132+
133+
134+
def test_cMpc_to_z_with_units():
135+
"""
136+
Test that passsing comoving distance with units calculated redshift
137+
correctly.
138+
139+
"""
140+
comoving_distance_units = 3000 * apu.Mpc
141+
expected_redshift = 0.8479314667609102
142+
calculated_redshift = pyxcosmo.cMpc_to_z(comoving_distance_units, cosmology='Planck15')
143+
assert np.isclose(expected_redshift, calculated_redshift)
144+
145+
def test_scale_factor_check_float_and_int_works():
146+
"""
147+
Test that the scale factor function works with floats and ints
148+
"""
149+
redshift_int = 1
150+
redshift_float = 1.0
151+
true_a = 0.5
152+
scale_factor_int = pyxcosmo.scale_factor(redshift_int)
153+
scale_factor_float = pyxcosmo.scale_factor(redshift_float)
154+
assert true_a == scale_factor_float
155+
assert true_a == scale_factor_int
156+
157+
158+
def test_scale_factor_with_arrays():
159+
"""
160+
Test that the scale factor function works with floats and ints
161+
"""
162+
redshift = np.array([0, 1, 2, 3])
163+
true_a_array = np.array([1, 1/2, 1/3, 1/4])
164+
scale_factors = pyxcosmo.scale_factor(redshift)
165+
166+
assert np.isclose(true_a_array, scale_factors).all()
167+
File renamed without changes.

0 commit comments

Comments
 (0)