Completed
Push — master ( cc9cbc...fb6854 )
by Ben
01:27
created

test_local_ism()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
c 3
b 0
f 0
dl 0
loc 9
rs 9.2
1
2
import os
3
4
import numpy as np
5
from click.testing import CliRunner
6
from numpy.random import rand
7
from numpy.random import randint
8
9
from ne2001 import density
10
from ne2001.cli import main
11
12
PARAMS = density.PARAMS
13
r_sun = 8.5
14
15
def test_main():
16
    runner = CliRunner()
17
    result = runner.invoke(main, [])
18
19
    assert result.output == '()\n'
20
    assert result.exit_code == 0
21
22
23
def test_density():
24
    xyz = (1-2*rand(3, 100)) * 20
25
26
    ne_disk1 = density.NEobject(density.thick_disk, r_sun=r_sun,
27
                                **PARAMS['thick_disk']).ne(xyz)
28
    assert len(ne_disk1) == 100
29
    assert all(ne_disk1 >= 0)
30
31
    ne_disk2 = density.NEobject(density.thin_disk, **PARAMS['thin_disk']).ne(xyz)
32
    assert len(ne_disk2) == 100
33
    assert all(ne_disk2 >= 0)
34
35
    ne_gc = density.NEobject(density.gc, **PARAMS['galactic_center']).ne(xyz)
36
    assert len(ne_gc) == 100
37
    assert all(ne_gc >= 0)
38
39
40
def test_geometry():
41
    ellipsoid = rand(3)*20
42
    xyz = (1-2*rand(3, 100)) * 20
43
    theta = rand()*2*np.pi
44
    center = rand(3)*20
45
    assert density.in_ellipsoid(center, center, ellipsoid, theta)
46
    assert len(density.in_ellipsoid(xyz, center, ellipsoid, theta)) == 100
47
48
    cylinder = rand(3)*20
49
    xyz = (1-2*rand(3, 100)) * 20
50
    theta = rand()*2*np.pi
51
    center = rand(3)*20
52
    assert len(density.in_cylinder(xyz, center, cylinder, theta)) == 100
53
54
    assert all(density.in_half_sphere(xyz, center, 10)[xyz[-1] < 0] == 0)
55 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
57
def test_clumps():
58
    clumps_file = os.path.join(os.path.split(density.__file__)[0], "data",
59
                               "neclumpN.NE2001.dat")
60
61
    clumps = density.Clumps(clumps_file)
62
63
    clumps = density.Clumps()
64
    xyz = (clumps.xyz.T[randint(0, clumps.gl.size, 100)].T +
65
           (1-2*rand(3, 100))*0.01)
66
67
    ne_clumps = clumps.ne_clumps(xyz)
68
    assert len(ne_clumps) == 100
69
    ix = ne_clumps.argmax()
70
    assert ne_clumps[ix] == clumps.ne_clumps(xyz[:, ix])
71
72 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
def test_void():
74
    voids_file = os.path.join(os.path.split(density.__file__)[0], "data",
75
                              "nevoidN.NE2001.dat")
76
77
    voids = density.Voids(voids_file)
78
79
    voids = density.Voids()
80
81
    xyz = (voids.xyz.T[randint(0, voids.gl.size, 100)].T +
82
           (1-2*rand(3, 100))*0.01)
83
84
    ne_voids = voids.ne_voids(xyz)
85
    assert len(ne_voids) == 100
86
87
    ix = ne_voids.argmax()
88
    assert ne_voids[ix] == voids.ne_voids(xyz[:, ix])
89
90
91
def test_local_ism():
92
    tol = 1e-3
93
    xyz = (1-2*rand(3, 100)) * 20
94
    local_ism = density.LocalISM(**PARAMS)
95
96
    assert all(local_ism.ne(xyz) >= 0)
97
    assert len(local_ism.ne(xyz)) == 100
98
    assert (abs(local_ism.DM(np.array([-3.4136981E-02, 7.522445, 0.2079124])) -
99
                2.453550)/2.453550 < tol)
100
101
def test_DM():
102
    tol = 1e-3
103
    d1 = density.NEobject(density.thick_disk, r_sun=8.5,
104
                          **PARAMS['thick_disk'])
105
    assert (abs(d1.DM(np.array([0.1503843, 7.647129, 0.5000018])) -
106
                32.36372) / 32.36372 < tol)
107
108
109
    d2 = density.NEobject(density.thin_disk,
110
                          **PARAMS['thin_disk'])
111
112
113
    assert abs(d2.DM(np.array([0.1503843, 7.647129, 0.5000018])) -
114
               0.046937)/0.046937 < tol
115