forked from gwastro/pycbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_transforms.py
99 lines (82 loc) · 3.38 KB
/
test_transforms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (C) 2017 Christopher M. Biwer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import numpy
import unittest
from pycbc import transforms
from utils import parse_args_cpu_only
from utils import simple_exit
# list of transforms without an inverse function and to ignore
IGNORE = [t.name for t in transforms.common_cbc_transforms
if t.inverse is None]
# ranges to draw random numbers for each parameter
RANGES = {
"mass1" : (1.0, 100.0),
"mass2" : (1.0, 100.0),
"mchirp" : (1.0, 20.0),
"q" : (1.0, 10.0),
"spin1_a" : (0.0, 1.0),
"spin1_polar" : (0, numpy.pi),
"spin1_azimuthal" : (0.0, 2 * numpy.pi),
"spin2_a" : (0.0, 1.0),
"spin2_polar" : (0, numpy.pi),
"spin2_azimuthal" : (0.0, 2 * numpy.pi),
"chi_eff" : (-1.0, 1.0),
"chi_a" : (0.0, 1.0),
"chi_p" : (0.0, 1.0),
"phi_s" : (0.0, 2 * numpy.pi),
"phi_a" : (0.0, 2 * numpy.pi),
"xi1" : (0.0, 1.0),
"xi2" : (0.0, 1.0),
"chirp_distance" : (2.0, 10.0),
}
# tests only need to happen on the CPU
parse_args_cpu_only("Transforms")
class TestTransforms(unittest.TestCase):
def setUp(self):
# set random seed
numpy.random.seed(1024)
def test_inverse(self):
# set threshold how similar values must be
threshold = 0.001
# loop over forward CBC transforms
for trans in transforms.common_cbc_forward_transforms:
# check if inverse exists
if trans.name in IGNORE:
continue
if trans.name == 'spherical_to_cartesian':
# spherical to cartesian requires the cartesian and spherical
# parameter names to be specified, which we can get from
# the inputs and outputs
inv = trans.inverse(*trans._outputs+trans._inputs)
else:
inv = trans.inverse()
# generate some random points
in_map = {p : numpy.random.uniform(*RANGES[p])
for p in trans.inputs}
# transforms to and back from inverse transform
intermediate_map = trans.transform(in_map)
out_map = inv.transform(intermediate_map)
# check that input equals outputs to some threshold
in_arr = numpy.array([in_map[p] for p in trans.inputs])
out_arr = numpy.array([out_map[p] for p in trans.inputs])
if not numpy.all(1.0 - in_arr / out_arr < threshold):
raise ValueError(
"Transform {} does not map back to itself.".format(trans.name))
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestTransforms))
if __name__ == "__main__":
results = unittest.TextTestRunner(verbosity=2).run(suite)
simple_exit(results)