LAL 7.6.1.1-4859cae
test_iterutils.py
Go to the documentation of this file.
1import doctest
2import functools
3import math
4import sys
5from lal import iterutils
6
7
8#
9# check examples in documentation
10#
11
12
13failures = doctest.testmod(iterutils)[0]
14if failures:
15 sys.exit(bool(failures))
16
17
18#
19# check CDF of randindex. requires matplotlib for visual check of results.
20# without matplotlib, simply runs the code to test for failures
21#
22
23
25 randindex = functools.partial(next, iter(iterutils.randindex(10, 100, n)))
26 counts = [0.] * 100
27 p = [float("-inf")] * 100
28 for i in range(N):
29 x, lnP = randindex()
30 counts[x] += 1.
31 p[x] = lnP
32 for i in range(len(counts)):
33 counts[i] /= N
34 p[i] = math.exp(p[i])
35 return list(range(100)), counts, p
36
37try:
38 import matplotlib
39 matplotlib.use("Agg")
40 from matplotlib import figure
41 from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
42 fig = figure.Figure()
43 FigureCanvas(fig)
44 axes = fig.gca()
45 axes.loglog()
46except ImportError:
47 axes = None
48
49N = 1000000
50x, counts, p = gen_randindex_results(.5, N)
51if axes is not None:
52 axes.plot(x, counts, "k+", label = "Observed fractions (n=0.5)")
53 axes.plot(x, p, "k-", label = "Reported probabilities (n=0.5)")
54x, counts, p = gen_randindex_results(1., N)
55if axes is not None:
56 axes.plot(x, counts, "b+", label = "Observed fractions (n=1)")
57 axes.plot(x, p, "b-", label = "Reported probabilities (n=1)")
58x, counts, p = gen_randindex_results(2., N)
59if axes is not None:
60 axes.plot(x, counts, "g+", label = "Observed fractions (n=2)")
61 axes.plot(x, p, "g-", label = "Reported probabilities (n=2)")
62
63if axes is not None:
64 axes.set_xlim((9., 100.))
65 axes.set_title("randindex(10, 100, n), %d samples" % N)
66 axes.legend()
67 fig.savefig("iterutils_randindex_histogram.png")
def randindex(lo, hi, n=1.)
Yields integers in the range [lo, hi) where 0 <= lo < hi.
Definition: iterutils.py:373
def gen_randindex_results(n, N)