1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) |
---|
4 | # Copyright (c) 2012-2016 California Institute of Technology. |
---|
5 | # License: 3-clause BSD. The full license text is available at: |
---|
6 | # - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE |
---|
7 | |
---|
8 | from wrapper import * |
---|
9 | from mystic.math import almostEqual |
---|
10 | |
---|
11 | def test_monitored(): |
---|
12 | |
---|
13 | from mystic.monitors import Monitor |
---|
14 | mon = Monitor() |
---|
15 | @monitored(monitor=mon) |
---|
16 | def squared(x): |
---|
17 | return x**2 |
---|
18 | |
---|
19 | x = range(10) |
---|
20 | y = [squared(i) for i in x] |
---|
21 | assert y == mon.y |
---|
22 | assert x == mon.x |
---|
23 | |
---|
24 | |
---|
25 | def test_counted(): |
---|
26 | |
---|
27 | @counted |
---|
28 | def squared(x, n=0.0): |
---|
29 | return x**2 + n |
---|
30 | |
---|
31 | _ = squared(-1) |
---|
32 | _ = [squared(i) for i in range(10)] |
---|
33 | n,y = squared(10, 100) |
---|
34 | assert n[0] == 12 |
---|
35 | assert y == 10**2 + 100 |
---|
36 | |
---|
37 | from numpy import array |
---|
38 | x = array([1,2,3,4,5]) |
---|
39 | n,y = squared(x, 1) |
---|
40 | assert n[0] == 13 |
---|
41 | assert all(y == x**2 + 1) |
---|
42 | |
---|
43 | |
---|
44 | def test_monitored_counted(): |
---|
45 | |
---|
46 | from mystic.monitors import Monitor |
---|
47 | mon = Monitor() |
---|
48 | @monitoredcounted(monitor=mon) |
---|
49 | def squared(x): |
---|
50 | return x**2 |
---|
51 | |
---|
52 | _ = squared(-1) |
---|
53 | _ = [squared(i) for i in range(10)] |
---|
54 | n,y = squared(10) |
---|
55 | assert n[0] == 12 |
---|
56 | assert y == 10**2 |
---|
57 | |
---|
58 | x = [-1] + range(10) + [10] |
---|
59 | assert mon.x == x |
---|
60 | assert mon.y == [i**2 for i in x] |
---|
61 | |
---|
62 | |
---|
63 | def test_bounded(): |
---|
64 | |
---|
65 | @bounded(min=1,max=9.9999) |
---|
66 | def squared(x): |
---|
67 | return x**2 |
---|
68 | |
---|
69 | from numpy import array, inf |
---|
70 | x = range(11) |
---|
71 | y = [squared(i) for i in x] |
---|
72 | assert y == [i**2 if i in range(1,10) else inf for i in x] |
---|
73 | |
---|
74 | x = array(x) |
---|
75 | y = squared(x) |
---|
76 | assert y == inf #XXX: better, [i**2 if i in range(1,10) else inf for i in x] ? |
---|
77 | |
---|
78 | |
---|
79 | def test_clip_bounded(): |
---|
80 | |
---|
81 | @clip_bounded(min=1.0,max=9.9999) |
---|
82 | def squared(x): |
---|
83 | return x**2 |
---|
84 | |
---|
85 | from numpy import array, arange |
---|
86 | x = range(11) |
---|
87 | y = [squared(i) for i in x] |
---|
88 | assert y == [min(max(i,1.0),9.9999)**2 for i in x] |
---|
89 | |
---|
90 | y = squared(x) |
---|
91 | assert all(y == array([min(max(i,1.0),9.9999)**2 for i in x])) |
---|
92 | |
---|
93 | |
---|
94 | def test_target_bounded(): |
---|
95 | |
---|
96 | @target_bounded(min=1.0,max=9.9999,target=5.0) |
---|
97 | def squared(x): |
---|
98 | return x**2 |
---|
99 | |
---|
100 | from numpy import array, arange |
---|
101 | x = range(11) |
---|
102 | y = [squared(i) for i in x] |
---|
103 | assert y == [i**2 if 1.0 <= i <= 9.999 else 5.0**2 for i in x] |
---|
104 | |
---|
105 | y = squared(x) |
---|
106 | assert all(y == [i**2 if 1.0 <= i <= 9.9999 else 5.0**2 for i in x]) |
---|
107 | |
---|
108 | |
---|
109 | def test_bounce_bounded(): |
---|
110 | |
---|
111 | from mystic.math.measures import impose_mean, mean |
---|
112 | |
---|
113 | @bounce_bounded(min=1.0,max=9.9999) |
---|
114 | def set_mean(x, m): |
---|
115 | return impose_mean(m, x) |
---|
116 | |
---|
117 | from numpy import array, arange |
---|
118 | x = range(11) |
---|
119 | assert almostEqual(mean(set_mean(x, 5.0)), 5.0) |
---|
120 | |
---|
121 | |
---|
122 | def test_mixedin(): |
---|
123 | |
---|
124 | def squared(x, *args): #XXX: needs to have same number of arguments |
---|
125 | return x**2 |
---|
126 | |
---|
127 | @mixedin(mixin=squared, shift=1, normalized=True) |
---|
128 | def plus_and_squared(x, n=0): |
---|
129 | return x + n |
---|
130 | |
---|
131 | assert plus_and_squared(2) == ((2**2) + (2+0))/2.0 |
---|
132 | assert plus_and_squared(3,1) == ((3**2) + (3+1))/2.0 |
---|
133 | |
---|
134 | |
---|
135 | if __name__ == '__main__': |
---|
136 | test_monitored() |
---|
137 | test_counted() |
---|
138 | test_monitored_counted() |
---|
139 | test_bounded() |
---|
140 | test_mixedin() |
---|
141 | test_clip_bounded() |
---|
142 | test_target_bounded() |
---|
143 | test_bounce_bounded() |
---|
144 | |
---|
145 | |
---|
146 | # EOF |
---|