annotate data_generation/transformations/testmod.py @ 253:a491d3600a77

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