test_geometry()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 15
rs 8.5454
1
2
import os
3
4
import numpy as np
5
import pytest
6
from click.testing import CliRunner
7
from numpy.random import rand
8
from numpy.random import randint
9
from numpy.random import seed
10
from scipy import integrate
11
12
from ne2001 import density
13
from ne2001 import ne_io
14
from ne2001 import utils
15
from ne2001.cli import main
16
17
PARAMS = ne_io.Params()
18
density.set_xyz_sun(np.array([0, 8.5, 0]))
19
20
21
def test_main():
22
    runner = CliRunner()
23
    result = runner.invoke(main, [])
24
25
    assert result.output == '()\n'
26
    assert result.exit_code == 0
27
28
29
def test_density():
30
    xyz = (1-2*rand(3, 100)) * 20
31
32
    ne_disk1 = density.NEobject(density.thick_disk,
33
                                **PARAMS['thick_disk']).ne(xyz)
34
    assert len(ne_disk1) == 100
35
    assert all(ne_disk1 >= 0)
36
37
    ne_disk2 = density.NEobject(density.thin_disk, **PARAMS['thin_disk']).ne(xyz)
38
    assert ne_disk2.size == 100
39
    assert all(ne_disk2 >= 0)
40
41
    ne_gc = density.NEobject(density.gc, **PARAMS['galactic_center']).ne(xyz)
42
    assert len(ne_gc) == 100
43
    assert all(ne_gc >= 0)
44
45
46
def test_geometry():
47
    ellipsoid = rand(3)*20
48
    xyz = (1-2*rand(3, 100)) * 20
49
    theta = rand()*2*np.pi
50
    center = rand(3)*20
51
    assert density.in_ellipsoid(center, ellipsoid, theta)(center)
52
    assert len(density.in_ellipsoid(center, ellipsoid, theta)(xyz)) == 100
53
54
    cylinder = rand(3)*20
55
    xyz = (1-2*rand(3, 100)) * 20
56
    theta = rand()*2*np.pi
57
    center = rand(3)*20
58
    assert len(density.in_cylinder(xyz, center, cylinder, theta)) == 100
59
60
    assert all(density.in_half_sphere(xyz, center, 10)[xyz[-1] < 0] == 0)
61
62
63
def test_clumps():
64
    clumps_file = os.path.join(os.path.split(density.__file__)[0], "data",
65
                               "neclumpN.NE2001.dat")
66
67
    clumps = density.Clumps(clumps_file)
68
69
    clumps = density.Clumps()
70
    xyz = (clumps.xyz.T[randint(0, clumps.gl.size, 100)].T +
71
           (1-2*rand(3, 100))*0.01)
72
73
    ne_clumps = clumps.ne(xyz)
74
    assert len(ne_clumps) == 100
75
    ix = ne_clumps.argmax()
76
    assert ne_clumps[ix] == clumps.ne(xyz[:, ix])
77
78
79
def test_void():
80
    tol = 1e-3
81
    voids_file = os.path.join(os.path.split(density.__file__)[0], "data",
82
                              "nevoidN.NE2001.dat")
83
84
    voids = density.Voids(voids_file)
85
86
    voids = density.Voids()
87
88
    xyz = (voids.xyz.T[randint(0, voids.gl.size, 100)].T +
89
           (1-2*rand(3, 100))*0.01)
90
91
    ne_voids = voids.ne(xyz)
92
    assert len(ne_voids) == 100
93
94
    ix = ne_voids.argmax()
95
    assert ne_voids[ix] == voids.ne(xyz[:, ix])
96
    l, b, d = -2, 12, 1
97
    DM = 0.9308054
98
    assert abs(voids.DM(l, b, d).value - DM)/DM < tol
99
100
101
def test_local_ism():
102
    tol = 1e-3
103
    xyz = (1-2*rand(3, 100)) * 20
104
    local_ism = density.LocalISM(**PARAMS)
105
106
    assert all(local_ism.ne(xyz) >= 0)
107
    assert len(local_ism.ne(xyz)) == 100
108
    l, b, d = -2, 12, 1
109
    DM = 2.453550
110
    assert (abs(local_ism.DM(l, b, d).value -
111
                DM)/DM < tol)
112
113
def test_DM():
114
    tol = 1e-3
115
    d1 = density.NEobject(density.thick_disk, **PARAMS['thick_disk'])
116
    xyz = np.array([0.1503843, 7.647129, 0.5000018])
117
    l, b, d = 10, 30, 1
118
    DM = 32.36372
119
    assert abs(d1.DM(l, b, d).value - DM) / DM  < tol
120
121
122
    d2 = density.NEobject(density.thin_disk,
123
                          **PARAMS['thin_disk'])
124
125
126
    l, b, d = 10, 30, 1
127
    DM = 0.046937
128
    assert abs(d2.DM(l, b, d).value - DM) / DM  < tol
129
130
131
def test_electron_density_quad():
132
    tol = 1e-3
133
    ne = density.ElectronDensity()
134
    l, b, d = -2, 12, 1
135
    DM = 23.98557
136
    assert abs(ne.DM(l, b, d).value - DM)/DM < tol
137
138
139
def test_electron_density_trapz():
140
    tol = 1e-3
141
    ne = density.ElectronDensity()
142
    l, b, d = -2, 12, 1
143
    DM = 23.98557
144
    assert abs(ne.DM(l, b, d, integrator=integrate.trapz).value - DM)/DM < tol
145
146
147
def test_dist():
148
    seed(123)
149
    for i in range(1):
150
        tol = 0.1
151
        ne = density.ElectronDensity()
152
        l = rand()*360
153
        b = np.arccos(1 - 2*rand())/np.pi*180
154
        d = rand()*5
155
        DM = ne.DM(l, b, d)
156
        d_DM = ne.dist(l, b, DM)
157
        err = abs(d_DM.value - d)/d
158
        print(err, l, b, d, d_DM)
159
        assert err < tol, (l, b, d)
160