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