1 '''
2 Created on Feb 13, 2014
3
4 @author: maxzwiessele
5 '''
6 import unittest
7 import numpy as np
8
9 from ..core.observable_array import ObsAr
10 from ..core.index_operations import ParameterIndexOperations
11 from ..core.nameable import adjust_name_for_printing
12 from ..core import HierarchyError
13 from .. import transformations
14 from ..parameterized import Parameterized
15 from ..param import Param, ParamConcatenation
16 from ..model import Model
17 from unittest.case import SkipTest
18 from paramz.caching import Cache_this
22 self.X = np.random.normal(1,1, size=(100,10))
23 self.obsX = ObsAr(self.X)
24
26 X = ObsAr(self.X)
27 X2 = ObsAr(X)
28 self.assertIs(X, X2, "no new Observable array, when Observable is given")
29
31 t1 = self.X[2:78]
32 t2 = self.obsX[2:78]
33 self.assertListEqual(t1.tolist(), t2.tolist(), "Slicing should be the exact same, as in ndarray")
34
36 class Test(Parameterized):
37 def __init__(self, name=None, parameters=[], *a, **kw):
38 super(Test, self).__init__(name=name)
39 self.x = Param('x', np.random.uniform(0,1,(3,4)), transformations.__fixed__)
40 self.x[0].constrain_bounded(0,1)
41 self.link_parameter(self.x)
42 self.x.unfix()
43 self.x[1].fix()
44 t = Test()
45 c = {transformations.Logistic(0,1): np.array([0, 1, 2, 3]), 'fixed': np.array([4, 5, 6, 7])}
46 np.testing.assert_equal(t.x.constraints[transformations.Logistic(0,1)], c[transformations.Logistic(0,1)])
47 np.testing.assert_equal(t.x.constraints['fixed'], c['fixed'])
48
72
73
74 m = TestLikelihood()
75 print(m)
76 val = m.p1.values.copy()
77 assert(m.p1.is_fixed)
78 assert(m.constraints[transformations.Logexp()].tolist() == [1])
79 m.randomize()
80 assert(m.p1 == val)
81
82 -class P(Parameterized):
92
95
103 def objective_function(self):
104 return self._obj
105 def log_likelihood(self):
106 return -self.objective_function()
107 def parameters_changed(self):
108 self._obj = (self.param_array**2).sum()
109 for p in self.parameters:
110 if hasattr(p, 'heres_johnny'):
111 p.heres_johnny()
112 self.gradient[:] = 2*self.param_array
113
114 self.testmodel = M('testmodel')
115 self.testmodel.kern = P('rbf')
116 self.testmodel.likelihood = P('Gaussian_noise', variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()))
117 self.testmodel.link_parameter(self.testmodel.kern)
118 self.testmodel.link_parameter(self.testmodel.likelihood)
119 variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp())
120 lengthscale=Param('lengthscale', np.random.uniform(.1, 1, 1), transformations.Logexp())
121 self.testmodel.kern.variance = variance
122 self.testmodel.kern.lengthscale = lengthscale
123 self.testmodel.kern.link_parameter(lengthscale)
124 self.testmodel.kern.link_parameter(variance)
125 self.testmodel.trigger_update()
126
127
128
129
130
131
133 try:
134 import pydot
135 G = self.testmodel.build_pydot()
136 testmodel_node_labels = set(['testmodel',
137 'lengthscale',
138 'variance',
139 'Cacher(heres_johnny)\n limit=5\n \\#cached=1',
140 'rbf',
141 'Cacher(heres_johnny)\n limit=5\n \\#cached=1',
142 'Gaussian_noise',
143 'variance'])
144 testmodel_edges = set([tuple(e) for e in [['variance', 'Gaussian_noise'],
145 ['Gaussian_noise', 'Cacher(heres_johnny)\n limit=5\n \\#cached=1'],
146 ['rbf', 'rbf'],
147 ['Gaussian_noise', 'variance'],
148 ['testmodel', 'Gaussian_noise'],
149 ['lengthscale', 'rbf'],
150 ['rbf', 'lengthscale'],
151 ['rbf', 'testmodel'],
152 ['variance', 'rbf'],
153 ['testmodel', 'rbf'],
154 ['testmodel', 'testmodel'],
155 ['Gaussian_noise', 'testmodel'],
156 ['Gaussian_noise', 'Gaussian_noise'],
157 ['rbf', 'variance'],
158 ['rbf', 'Cacher(heres_johnny)\n limit=5\n \\#cached=1']]])
159
160 self.assertSetEqual(set([n.get_label() for n in G.get_nodes()]), testmodel_node_labels)
161
162 edges = set()
163 for e in G.get_edges():
164 points = e.obj_dict['points']
165 edges.add(tuple(G.get_node(p)[0].get_label() for p in points))
166
167 self.assertSetEqual(edges, testmodel_edges)
168 except ImportError:
169 raise SkipTest("pydot not available")
170
172 self.testmodel.update_toggle()
173 self.testmodel.optimize(messages=True, xtol=0, ftol=0, gtol=1e-6, bfgs_factor=1)
174 self.testmodel.optimize(messages=False)
175 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
177 import warnings
178 with warnings.catch_warnings():
179 warnings.simplefilter("ignore")
180 self.testmodel.optimize('scg', messages=1, max_f_eval=10, max_iters=100)
181 self.testmodel.optimize('scg', messages=0, xtol=0, ftol=0, gtol=1e-6, max_iters=2)
182 self.testmodel.optimize('scg', messages=0, xtol=0, ftol=20, gtol=0, max_iters=2)
183 self.testmodel.optimize('scg', messages=0, xtol=20, ftol=0, gtol=0, max_iters=2)
184 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-1)
186 from ..optimization.optimization import opt_tnc
187 import warnings
188 with warnings.catch_warnings():
189 warnings.simplefilter("ignore")
190 self.testmodel.optimize_restarts(1, messages=1, optimizer=opt_tnc(), verbose=False)
191 self.testmodel.optimize('tnc', messages=1, xtol=0, ftol=0, gtol=1e-6)
192 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
193
195 try:
196 import climin
197 except ImportError:
198 raise SkipTest("climin not installed, skipping test")
199 import warnings
200 with warnings.catch_warnings():
201 warnings.simplefilter("ignore")
202 self.testmodel.optimize('rprop', messages=1)
203 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
205 try:
206 import climin
207 except ImportError:
208 raise SkipTest("climin not installed, skipping test")
209 import warnings
210 with warnings.catch_warnings():
211 warnings.simplefilter("ignore")
212 self.testmodel.trigger_update()
213 self.testmodel.optimize('adadelta', messages=1, step_rate=1, momentum=1)
214 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
216 try:
217 import climin
218 except ImportError:
219 raise SkipTest("climin not installed, skipping test")
220 import warnings
221 with warnings.catch_warnings():
222 warnings.simplefilter("ignore")
223 self.testmodel.trigger_update()
224 self.testmodel.optimize('adam', messages=1, step_rate=1., momentum=1.)
225 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
227 import warnings
228 with warnings.catch_warnings():
229 warnings.simplefilter("ignore")
230 with np.errstate(divide='ignore'):
231 self.testmodel.optimize_restarts(1, messages=0, optimizer='org-bfgs', xtol=0, ftol=0, gtol=1e-6)
232 self.testmodel.optimize(messages=1, optimizer='org-bfgs')
233 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
235 self.testmodel.fix()
236 self.assertTrue(self.testmodel.checkgrad())
237 self.assertTrue(self.testmodel.checkgrad(1))
238 self.testmodel.optimize(messages=1)
240 self.assertRaises(KeyError, self.testmodel.optimize, 'cgd', messages=1)
242 import warnings
243 with warnings.catch_warnings():
244 warnings.simplefilter("ignore")
245 self.testmodel.optimize('simplex', messages=1, xtol=0, ftol=0, gtol=1e-6)
246 self.testmodel.optimize('simplex', start=self.testmodel.optimizer_array, messages=0)
247 np.testing.assert_array_less(self.testmodel.gradient, np.ones(self.testmodel.size)*1e-2)
256 def objective_function(self):
257 raise ValueError('Some error occured')
258 def log_likelihood(self):
259 raise ValueError('Some error occured')
260 def parameters_changed(self):
261
262 self.gradient[:] = 2*self.param_array
263 testmodel = M("test", var=Param('test', np.random.normal(0,1,(20))))
264 testmodel.optimize_restarts(2, messages=0, optimizer='org-bfgs', xtol=0, ftol=0, gtol=1e-6, robust=True)
265 self.assertRaises(ValueError, testmodel.optimize_restarts, 1, messages=0, optimizer='org-bfgs', xtol=0, ftol=0, gtol=1e-6, robust=False)
267 m = self.testmodel.copy()
268 m.optimize_restarts(2, messages=0, xtol=0, ftol=0, gtol=1e-6, robust=False)
269 np.testing.assert_array_less(m.gradient, np.ones(self.testmodel.size)*1e-2)
270
272 self.assertListEqual(self.testmodel._raveled_index_for(self.testmodel['.*variance']).tolist(), [1, 2])
273 self.assertListEqual(self.testmodel.kern.lengthscale._raveled_index_for(None).tolist(), [0])
274
276 self.testmodel['.*rbf'].constrain_negative()
277 self.assertListEqual(self.testmodel.constraints[transformations.NegativeLogexp()].tolist(), [0,1])
278
279 self.testmodel['.*lengthscale'].constrain_bounded(0,1)
280 self.assertListEqual(self.testmodel.constraints[transformations.NegativeLogexp()].tolist(), [1])
281 self.assertListEqual(self.testmodel.constraints[transformations.Logistic(0, 1)].tolist(), [0])
282
283 self.testmodel[''].unconstrain_negative()
284 self.assertListEqual(self.testmodel.constraints[transformations.NegativeLogexp()].tolist(), [])
285 self.assertListEqual(self.testmodel.constraints[transformations.Logistic(0, 1)].tolist(), [0])
286
287 self.testmodel['.*lengthscale'].unconstrain_bounded(0,1)
288 self.assertListEqual(self.testmodel.constraints[transformations.Logistic(0, 1)].tolist(), [])
289
291 self.testmodel['.*rbf'].constrain_negative()
292 self.testmodel['.*lengthscale'].constrain_bounded(0,1)
293 self.testmodel['.*variance'].fix()
294
295 self.assertListEqual(self.testmodel.constraints[transformations.__fixed__].tolist(), [1,2])
296 self.assertListEqual(self.testmodel.constraints[transformations.Logistic(0,1)].tolist(), [0])
297 self.assertListEqual(self.testmodel.constraints[transformations.NegativeLogexp()].tolist(), [1])
298
299 cache_constraints = self.testmodel.constraints.copy()
300
301 self.testmodel.unconstrain()
302 self.testmodel.likelihood.fix()
303
304 self.assertListEqual(self.testmodel._fixes_.tolist(), [transformations.UNFIXED, transformations.UNFIXED, transformations.FIXED])
305
306 self.assertListEqual(self.testmodel.constraints[transformations.__fixed__].tolist(), [2])
307 self.assertListEqual(self.testmodel.constraints[transformations.Logistic(0,1)].tolist(), [])
308 self.assertListEqual(self.testmodel.constraints[transformations.NegativeLogexp()].tolist(), [])
309
310 self.testmodel.constraints = cache_constraints
311 self.assertListEqual(self.testmodel.constraints[transformations.__fixed__].tolist(), [1,2])
312 self.assertListEqual(self.testmodel.constraints[transformations.Logistic(0,1)].tolist(), [0])
313 self.assertListEqual(self.testmodel.constraints[transformations.NegativeLogexp()].tolist(), [1])
314
315 self.assertListEqual(self.testmodel._fixes_.tolist(), [transformations.UNFIXED, transformations.FIXED, transformations.FIXED])
316
317 self.assertIs(self.testmodel.constraints, self.testmodel.likelihood.constraints._param_index_ops)
318 self.assertIs(self.testmodel.constraints, self.testmodel.kern.constraints._param_index_ops)
319
320
321
330
332 self.testmodel.gradient = 10.
333 np.testing.assert_array_equal(self.testmodel.gradient, 10.)
334 self.testmodel.kern.lengthscale.gradient = 15
335 np.testing.assert_array_equal(self.testmodel.gradient, [15., 10., 10.])
336
338 self.testmodel.kern.lengthscale.fix()
339 val = float(self.testmodel.kern.lengthscale)
340 self.testmodel.randomize()
341 self.assertEqual(val, self.testmodel.kern.lengthscale)
342 self.testmodel.optimize(max_iters=2)
343
345 self.assertTrue(self.testmodel[''].checkgrad())
346
347 self.testmodel['.*rbf'][:] = 10
348 self.testmodel[''][2] = 11
349
350 np.testing.assert_array_equal(self.testmodel.param_array, [10,10,11])
351 np.testing.assert_((self.testmodel[''][:2] == [10,10]).all())
352
353 self.testmodel.kern.lengthscale.fix()
354 val = float(self.testmodel.kern.lengthscale)
355 self.testmodel.randomize()
356 self.assertEqual(val, self.testmodel.kern.lengthscale)
357
358 variances = self.testmodel['.*var'].values()
359 self.testmodel['.*var'].fix()
360 self.testmodel.randomize()
361 np.testing.assert_equal(variances, self.testmodel['.*var'].values())
362
363 self.testmodel[''] = 1.0
364 self.maxDiff = None
365
366 self.testmodel[''].unconstrain()
367 self.assertSequenceEqual(self.testmodel[''].__str__(VT100=False), " index | testmodel.rbf.lengthscale | constraints\n [0] | 1.00000000 | \n ----- | testmodel.rbf.variance | -----------\n [0] | 1.00000000 | \n ----- | testmodel.Gaussian_noise.variance | -----------\n [0] | 1.00000000 | ")
368
370 default_constraints = dict(self.testmodel.constraints.items())
371 self.testmodel['.*lengthscale'].fix()
372 fixed = self.testmodel.constraints[transformations.__fixed__]
373 self.assertListEqual(fixed.tolist(), [0])
374 unfixed = self.testmodel.kern.lengthscale.unfix()
375 self.testmodel['.*lengthscale'].constrain_positive()
376 self.assertListEqual(unfixed.tolist(), [0])
377
378 fixed = self.testmodel['.*rbf'].fix()
379 fixed = self.testmodel.constraints[transformations.__fixed__]
380 self.assertListEqual(fixed.tolist(), [0,1])
381
382 unfixed = self.testmodel.kern.unfix()
383 self.assertListEqual(unfixed.tolist(), [0,1])
384
385 fixed = self.testmodel.constraints[transformations.__fixed__]
386 self.testmodel['.*rbf'].unfix()
387 np.testing.assert_array_equal(fixed, self.testmodel.constraints[transformations.__fixed__])
388
389
390 test_constraints = dict(self.testmodel.constraints.items())
391 for k in default_constraints:
392 np.testing.assert_array_equal(default_constraints[k], test_constraints[k])
393
418
420
421 before_constraints = dict(self.testmodel.constraints.items())
422
423 self.testmodel.fix()
424
425 test_constraints = dict(self.testmodel.constraints.items())
426
427 np.testing.assert_array_equal(test_constraints[transformations.__fixed__], [0,1,2])
428
429 for k in before_constraints:
430 np.testing.assert_array_equal(before_constraints[k], test_constraints[k])
431
432
433 self.testmodel.likelihood.constrain_bounded(0,1)
434
435 np.testing.assert_array_equal(self.testmodel.constraints[transformations.__fixed__], [0,1])
436
437 np.testing.assert_array_equal(self.testmodel.constraints[transformations.Logexp()], [0,1])
438
439 np.testing.assert_array_equal(self.testmodel.constraints[transformations.Logistic(0,1)], [2])
440
442 self.assertEqual(len(self.testmodel.kern.cache), 1)
443 [self.assertEqual(len(c.cached_outputs), 1) for c in self.testmodel.kern.cache.values()]
444
445 self.testmodel.disable_caching()
446 self.testmodel.trigger_update()
447
448 [self.assertFalse(c.cacher_enabled) for c in self.testmodel.kern.cache.values()]
449 self.assertFalse(self.testmodel.kern.cache.caching_enabled)
450 self.assertFalse(self.testmodel.likelihood.cache.caching_enabled)
451
452 self.assertTrue(self.testmodel.checkgrad())
453
454 self.assertEqual(len(self.testmodel.kern.cache), 1)
455
456 [self.assertEqual(len(c.cached_outputs), 0) for c in self.testmodel.kern.cache.values()]
457
458 self.testmodel.enable_caching()
459 self.testmodel.trigger_update()
460
461 self.assertEqual(len(self.testmodel.kern.cache), 1)
462 [self.assertEqual(len(c.cached_outputs), 1) for c in self.testmodel.kern.cache.values()]
463
464
466 self.assertTrue(self.testmodel.checkgrad(1))
467 self.assertTrue(self.testmodel.checkgrad())
468 self.assertTrue(self.testmodel.rbf.variance.checkgrad(1))
469 self.assertTrue(self.testmodel.rbf.variance.checkgrad())
470 self.assertTrue(self.testmodel._checkgrad(verbose=1))
471 self.assertTrue(self.testmodel._checkgrad(verbose=0))
472
477
479 self.assertRaises(HierarchyError, self.testmodel.link_parameter, self.testmodel.parameters[0])
480 p2 = P('Gaussian_noise', variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()))
481 self.testmodel.link_parameter(p2.variance)
482 self.assertTrue(self.testmodel.checkgrad())
483 self.assertRaises(HierarchyError, self.testmodel.unlink_parameter, p2)
484 self.assertRaises(HierarchyError, self.testmodel.unlink_parameter, 'not a parameter')
485
487 self.testmodel.likelihood.variance = 10
488 self.assertIsInstance(self.testmodel.likelihood.variance, Param)
489 np.testing.assert_array_equal(self.testmodel.likelihood[:], [10])
490
492 self.testmodel.likelihood.variance = 10
493 self.assertIsInstance(self.testmodel.likelihood.variance, Param)
494 np.testing.assert_array_equal(self.testmodel.likelihood[:], [10])
495
497 m = self.testmodel
498 m2 = self.testmodel.copy(memo={})
499
500 np.testing.assert_array_equal(self.testmodel[:], m2[:])
501
502 np.testing.assert_equal(m.log_likelihood(), m2.log_likelihood())
503 m.randomize()
504 m2[:] = m[''].values()
505 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
506 m.randomize()
507 m2[''] = m[:]
508 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
509 m.randomize()
510 m2[:] = m[:]
511 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
512 m.randomize()
513 m2[''] = m['']
514 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
515
516 m.kern.lengthscale.randomize()
517 m2[:] = m[:]
518 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
519
520 m.Gaussian_noise.randomize()
521 m2[:] = m[:]
522 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
523
524 m['.*var'] = 2
525 m2['.*var'] = m['.*var']
526 np.testing.assert_almost_equal(m.log_likelihood(), m2.log_likelihood())
527
528 np.testing.assert_array_equal(self.testmodel[''].values(), m2[''].values())
529 np.testing.assert_array_equal(self.testmodel[:], m2[''].values())
530 np.testing.assert_array_equal(self.testmodel[''].values(), m2[:])
531 np.testing.assert_array_equal(self.testmodel['.*variance'].values(), m2['.*variance'].values())
532 np.testing.assert_array_equal(self.testmodel['.*len'].values, m2['.*len'].values)
533 np.testing.assert_array_equal(self.testmodel['.*rbf'].values(), m2['.*rbf'].values())
534
536 pars = self.testmodel[:].copy()
537 self.testmodel.rbf[:] = None
538 np.testing.assert_array_equal(self.testmodel[:], pars)
539
541 self.assertRaises(ValueError, self.testmodel.__setitem__, slice(None), 'test')
542
544
545 self.testmodel.name = 'anothername'
546 self.testmodel.link_parameter(Parameterized('empty'))
547 hmm = Parameterized('test')
548 self.testmodel.kern.test = hmm
549 self.testmodel.kern.link_parameter(hmm)
550 self.testmodel.kern.test.link_parameter(Param('test1',1))
551 self.assertIsInstance(self.testmodel['.*test1$'], Param)
552 self.assertIsInstance(self.testmodel['.*test$'], Parameterized)
553 self.assertIsInstance(self.testmodel['.*empty'], Parameterized)
554 self.assertIsInstance(self.testmodel['.*test'], ParamConcatenation)
555 self.assertIsInstance(self.testmodel['.*rbf$'], Parameterized)
556 self.assertIs(self.testmodel['rbf.variance'], self.testmodel.rbf.variance)
557 self.assertIs(self.testmodel['rbf$'], self.testmodel.rbf)
558
560 m = self.testmodel
561 m2 = self.testmodel.copy()
562 np.testing.assert_equal(m.log_likelihood(), m2.log_likelihood())
563
564 m.kern.lengthscale.randomize()
565 m2.kern.lengthscale = m.kern.lengthscale
566 np.testing.assert_equal(m.log_likelihood(), m2.log_likelihood())
567
568 m.kern.lengthscale.randomize()
569 m2['.*lengthscale'] = m.kern.lengthscale
570 np.testing.assert_equal(m.log_likelihood(), m2.log_likelihood())
571
572 m.kern.lengthscale.randomize()
573 m2['.*lengthscale'] = m.kern['.*lengthscale']
574 np.testing.assert_equal(m.log_likelihood(), m2.log_likelihood())
575
576 m.kern.lengthscale.randomize()
577 m2.kern.lengthscale = m.kern['.*lengthscale']
578 np.testing.assert_equal(m.log_likelihood(), m2.log_likelihood())
579
580 np.testing.assert_array_equal(self.testmodel[''].values(), m2[''].values())
581 np.testing.assert_array_equal(self.testmodel[:], m2[''].values())
582 np.testing.assert_array_equal(self.testmodel[''].values(), m2[:])
583 np.testing.assert_array_equal(self.testmodel['.*variance'].values(), m2['.*variance'].values())
584 np.testing.assert_array_equal(self.testmodel['.*len'], m2['.*len'])
585 np.testing.assert_array_equal(self.testmodel['.*rbf'][0], m2['.*rbf'][0])
586 np.testing.assert_array_equal(self.testmodel['.*rbf'][1], m2['.*rbf'][1])
587
597
599 self.rbf = Parameterized('rbf')
600 self.rbf.lengthscale = Param('lengthscale', np.random.uniform(.1, 1), transformations.Logexp())
601 self.rbf.variance = Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp())
602 self.rbf.link_parameters(self.rbf.variance, self.rbf.lengthscale)
603
604 self.white = P('white', variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()))
605 self.param = Param('param', np.random.uniform(0,1,(10,5)), transformations.Logistic(0, 1))
606
607 self.test1 = Parameterized('test_parameterized')
608
609 self.test1.param = self.param
610 self.test1.kern = Parameterized('add')
611 self.test1.kern.link_parameters(self.rbf, self.white)
612
613 self.test1.link_parameter(self.test1.kern)
614 self.test1.link_parameter(self.param, 0)
615
616
617
618
619
620
621
622
623
624
627
635
637 self.assertRaises(AttributeError, setattr, self.test1, 'param_array', 0)
638
656
658 self.assertSequenceEqual(self.test1.kern.rbf.parameter_names_flat().tolist(), ['test_parameterized.add.rbf.variance', 'test_parameterized.add.rbf.lengthscale'])
659
660 self.test1.param.fix()
661 self.test1.kern.rbf.lengthscale.fix()
662 self.assertSequenceEqual(self.test1.parameter_names_flat().tolist(), ['test_parameterized.add.rbf.variance', 'test_parameterized.add.white.variance'])
663 self.assertEqual(self.test1.parameter_names_flat(include_fixed=True).size, self.test1.size)
664
670
674
685
687 c = []
688 self.test1.kern.rbf.traverse_parents(lambda x: c.append(x.name))
689 self.assertSequenceEqual(c, ['test_parameterized', 'param', 'add', 'white', 'variance'])
690 c = []
691 self.test1.kern.white.variance.traverse_parents(lambda x: c.append(x.name))
692 self.assertSequenceEqual(c, ['test_parameterized', 'param', 'add', 'rbf', 'variance', 'lengthscale', 'white'])
693
695 self.test1.kern.name = 'newname'
696 self.test1.p = Param('newname', 1.22345)
697 self.test1.link_parameter(self.test1.p)
698 self.assertSequenceEqual(self.test1.kern.name, 'newname')
699 self.assertSequenceEqual(self.test1.p.name, 'newname_1')
700 self.test1.p2 = Param('newname', 1.22345)
701 self.test1.link_parameter(self.test1.p2)
702 self.assertSequenceEqual(self.test1.p2.name, 'newname_2')
703 self.test1.kern.rbf.lengthscale.name = 'variance'
704 self.assertSequenceEqual(self.test1.kern.rbf.lengthscale.name, 'variance_1')
705 self.test1.kern.rbf.variance.name = 'variance_1'
706 self.assertSequenceEqual(self.test1.kern.rbf.lengthscale.name, 'variance_2')
707 self.test1.kern.rbf.variance.name = 'variance'
708 self.assertSequenceEqual(self.test1.kern.rbf.lengthscale.name, 'variance_2')
709 self.assertSequenceEqual(self.test1.kern.rbf.variance.name, 'variance')
710
711
717
730
732 self.white.fix()
733 self.test1.kern.unlink_parameter(self.white)
734 self.assertIs(self.test1._fixes_,None)
735
736 self.assertIsInstance(self.white.constraints, ParameterIndexOperations)
737 self.assertListEqual(self.white._fixes_.tolist(), [transformations.FIXED])
738 self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
739 self.assertIs(self.test1.constraints, self.param.constraints._param_index_ops)
740
741 self.test1.link_parameter(self.white, 0)
742 self.assertIs(self.test1.constraints, self.white.constraints._param_index_ops)
743 self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
744 self.assertIs(self.test1.constraints, self.param.constraints._param_index_ops)
745 self.assertListEqual(self.test1.constraints[transformations.__fixed__].tolist(), [0])
746 self.assertIs(self.white._fixes_,None)
747 self.assertListEqual(self.test1._fixes_.tolist(),[transformations.FIXED] + [transformations.UNFIXED] * 52)
748
749 self.test1.unlink_parameter(self.white)
750 self.assertIs(self.test1._fixes_,None)
751 self.assertListEqual(self.white._fixes_.tolist(), [transformations.FIXED])
752 self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
753 self.assertIs(self.test1.constraints, self.param.constraints._param_index_ops)
754 self.assertListEqual(self.test1.constraints[transformations.Logexp()].tolist(), list(range(self.param.size, self.param.size+self.rbf.size)))
755
760
763
765 self.assertIs(self.rbf.variance.constraints._param_index_ops, self.rbf.constraints._param_index_ops)
766 self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
767 self.assertListEqual(self.rbf.constraints.indices()[0].tolist(), list(range(2)))
768 kern = self.test1.kern
769 self.test1.unlink_parameter(kern)
770 self.assertListEqual(kern.constraints[transformations.Logexp()].tolist(), list(range(3)))
771
782
789
794
796 self.white.fix(warning=True)
797 val = float(self.white.variance)
798 self.test1.randomize()
799 self.assertEqual(val, self.white.variance)
800
802 ps = self.test1.param.view(np.ndarray).copy()
803 self.test1.param[2:5].fix()
804 self.test1.param.randomize()
805 self.assertFalse(np.all(ps==self.test1.param),str(ps)+str(self.test1.param))
806
808 self.rbf.fix(0.1, warning=True)
809 val = float(self.rbf.variance)
810 self.test1.kern.randomize()
811 self.assertEqual(val, self.rbf.variance)
812
814 self.test1.kern.rbf.link_parameter(Param("NEW", np.random.rand(2), transformations.NegativeLogexp()), 1)
815 self.assertListEqual(self.test1.constraints[transformations.NegativeLogexp()].tolist(), list(range(self.param.size+1, self.param.size+1 + 2)))
816 self.assertListEqual(self.test1.constraints[transformations.Logistic(0,1)].tolist(), list(range(self.param.size)))
817 self.assertListEqual(self.test1.constraints[transformations.Logexp(0,1)].tolist(), np.r_[50, 53:55].tolist())
818
822
824 print(self.test1)
825 print(self.param)
826 print(self.test1[''])
827
836 def objective_function(self):
837 return self._obj
838 def log_likelihood(self):
839 return -self.objective_function()
840 def parameters_changed(self):
841 self._obj = (self.param_array**2).sum()
842 self.gradient[:] = 2*self.param_array
843 import warnings
844 with warnings.catch_warnings():
845 warnings.simplefilter("ignore")
846 self.testmodel = M('testmodel', initialize=False)
847 self.testmodel.kern = Parameterized('rbf', initialize=False)
848 self.testmodel.likelihood = P('Gaussian_noise', variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()), initialize=False)
849 self.testmodel.link_parameter(self.testmodel.kern)
850 self.testmodel.link_parameter(self.testmodel.likelihood)
851 variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp())
852 lengthscale=Param('lengthscale', np.random.uniform(.1, 1, 1), transformations.Logexp())
853 self.testmodel.kern.variance = variance
854 self.testmodel.kern.lengthscale = lengthscale
855 self.testmodel.kern.link_parameter(lengthscale)
856 self.testmodel.kern.link_parameter(variance)
857
859 self.assertFalse(self.testmodel.likelihood._model_initialized_)
860 self.assertFalse(self.testmodel.kern._model_initialized_)
861 self.assertRaises(AttributeError, self.testmodel.__str__)
862 import warnings
863 with warnings.catch_warnings():
864 warnings.simplefilter("error")
865
866 self.assertRaises(RuntimeWarning, self.testmodel.checkgrad)
867 with warnings.catch_warnings():
868 warnings.simplefilter("ignore")
869
870 self.assertFalse(self.testmodel.checkgrad())
871 self.assertFalse(self.testmodel.kern.checkgrad())
872 self.testmodel.initialize_parameter()
873 self.assertTrue(self.testmodel.likelihood._model_initialized_)
874 self.assertTrue(self.testmodel.kern._model_initialized_)
875 self.assertTrue(self.testmodel.checkgrad())
876
878 self.assertFalse(self.testmodel.likelihood._model_initialized_)
879 self.assertFalse(self.testmodel.kern._model_initialized_)
880 self.assertRaises(AttributeError, self.testmodel.__str__)
881
882
883 def err():
884 self.testmodel[:] = 2.
885 self.assertRaises(AttributeError, err)
886
887 self.assertFalse(self.testmodel.likelihood._model_initialized_)
888 self.assertFalse(self.testmodel.kern._model_initialized_)
889 self.assertFalse(self.testmodel._model_initialized_)
890
891 import warnings
892
893
894
895
896
897 with warnings.catch_warnings():
898 warnings.simplefilter("ignore")
899
900 self.assertFalse(self.testmodel.checkgrad())
901 self.assertFalse(self.testmodel.kern.checkgrad())
902
903
904 self.testmodel.update_model(False)
905
906
907 self.assertRaises(AttributeError, err)
908
909
910 self.testmodel.initialize_parameter()
911
912 self.assertIsNone(err())
913
914
915
916 self.assertRaises(AttributeError, self.testmodel.checkgrad)
917 self.assertRaises(AttributeError, self.testmodel.kern.checkgrad)
918
919
920 self.assertTrue(self.testmodel.likelihood._model_initialized_)
921 self.assertTrue(self.testmodel.kern._model_initialized_)
922
923
924
925 self.testmodel.update_model(True)
926
927 np.testing.assert_allclose(self.testmodel.param_array, 2., 1e-4)
928 self.assertTrue(self.testmodel.checkgrad())
929
930
931 if __name__ == "__main__":
932
933 unittest.main()
934