Mercurial > ift6266
comparison transformations/testmod.py @ 16:368f1907ad5a
Script to test modules for conformance to the interface.
author | Arnaud Bergeron <abergeron@gmail.com> |
---|---|
date | Thu, 28 Jan 2010 13:32:15 -0500 |
parents | |
children | 22823acc1712 089f236759d9 |
comparison
equal
deleted
inserted
replaced
15:f6b6c74bb82f | 16:368f1907ad5a |
---|---|
1 # This script is to test your modules to see if they conform to the module API | |
2 # defined on the wiki. | |
3 import random, numpy, gc, time, math | |
4 | |
5 # this is an example module that does stupid image value shifting | |
6 | |
7 class DummyModule(object): | |
8 def get_settings_names(self): | |
9 return ['value'] | |
10 | |
11 def regenerate_parameters(self, complexity): | |
12 self._value = random.gauss(0, 0.5*complexity) | |
13 return [self._value] | |
14 | |
15 def transform_image(self, image): | |
16 return numpy.clip(image+self._value, 0, 1) | |
17 | |
18 #import <your module> | |
19 | |
20 # instanciate your class here (rather than DummyModule) | |
21 mod = DummyModule() | |
22 | |
23 def error(msg): | |
24 print "ERROR:", msg | |
25 sys.exit(1) | |
26 | |
27 def warn(msg): | |
28 print "WARNING:", msg | |
29 | |
30 def timeit(f, lbl): | |
31 | |
32 gc.disable() | |
33 t = time.time() | |
34 f() | |
35 est = time.time() - t | |
36 gc.enable() | |
37 | |
38 loops = max(1, int(10**math.floor(math.log(10/est, 10)))) | |
39 | |
40 gc.disable() | |
41 t = time.time() | |
42 for _ in xrange(loops): | |
43 f() | |
44 | |
45 print lbl, "(", loops, "loops ):", (time.time() - t)/loops, "s" | |
46 gc.enable() | |
47 | |
48 ######################## | |
49 # get_settings_names() # | |
50 ######################## | |
51 | |
52 print "Testing get_settings_names()" | |
53 | |
54 names = mod.get_settings_names() | |
55 | |
56 if type(names) is not list: | |
57 error("Must return a list") | |
58 | |
59 if len(names) == 0: | |
60 error("Must return at least one element") | |
61 | |
62 if not all(type(e) is str for e in names): | |
63 warn("The elements of the list should be strings") | |
64 | |
65 ########################### | |
66 # regenerate_parameters() # | |
67 ########################### | |
68 | |
69 print "Testing regenerate_parameters()" | |
70 | |
71 params = mod.regenerate_parameters(0.2) | |
72 | |
73 if type(params) is not list: | |
74 error("Must return a list") | |
75 | |
76 if len(params) != len(names): | |
77 error("the returned parameter list must have the same length as the number of parameters") | |
78 | |
79 params2 = mod.regenerate_parameters(0.2) | |
80 if params == params2: | |
81 error("the complexity parameter determines the distribution of the parameters, not their value") | |
82 | |
83 mod.regenerate_parameters(0.0) | |
84 mod.regenerate_parameters(1.0) | |
85 | |
86 mod.regenerate_parameters(0.5) | |
87 | |
88 ##################### | |
89 # transform_image() # | |
90 ##################### | |
91 | |
92 print "Testing transform_image()" | |
93 | |
94 imgr = numpy.random.random_sample((32, 32)).astype(numpy.float32) | |
95 img1 = numpy.ones((32, 32), dtype=numpy.float32) | |
96 img0 = numpy.zeros((32, 32), dtype=numpy.float32) | |
97 | |
98 resr = mod.transform_image(imgr) | |
99 | |
100 if type(resr) is not numpy.ndarray: | |
101 error("Must return an ndarray") | |
102 | |
103 if resr.shape != (32, 32): | |
104 error("Must return 32x32 array") | |
105 | |
106 if resr.dtype != numpy.float32: | |
107 error("Must return float32 array") | |
108 | |
109 res1 = mod.transform_image(img1) | |
110 res0 = mod.transform_image(img0) | |
111 | |
112 if res1.max() > 1.0 or res0.max() > 1.0: | |
113 error("Must keep array values between 0 and 1") | |
114 | |
115 if res1.min() < 0.0 or res0.min() < 0.0: | |
116 error("Must keep array values between 0 and 1") | |
117 | |
118 mod.regenerate_parameters(0.0) | |
119 mod.transform_image(imgr) | |
120 mod.regenerate_parameters(1.0) | |
121 mod.transform_image(imgr) | |
122 | |
123 print "Bonus Stage: timings" | |
124 | |
125 timeit(lambda: None, "empty") | |
126 timeit(lambda: mod.regenerate_parameters(0.5), "regenerate_parameters()") | |
127 timeit(lambda: mod.transform_image(imgr), "tranform_image()") | |
128 | |
129 def f(): | |
130 mod.regenerate_parameters(0.2) | |
131 mod.transform_image(imgr) | |
132 | |
133 timeit(f, "regen and transform") |