1
2 """
3 Created on Thu Mar 28 15:35:26 2013
4
5 Copyright (c) 2013-2014, CEA/DSV/I2BM/Neurospin. All rights reserved.
6
7 @author: Tommy Löfstedt
8 @email: tommy.loefstedt@cea.fr
9 @license: BSD 3-clause.
10 """
11 import abc
12 import numpy as np
13
14 from . import maths
15
16 __all__ = ['BaseStartVector', 'IdentityStartVector', 'RandomStartVector',
17 'OnesStartVector', 'ZerosStartVector']
21 """Base class for start vector generation.
22
23 Parameters
24 ----------
25 normalise : Bool. Whether or not to normalise the vector that is returned.
26
27 seed : Integer or None. The seed to the pseudo-random number generator. If
28 none, no seed is used. The seed is set at initialisation, so if the
29 RNG is used in between initialisation and utilisation, then the
30 random numbers will change. Default is None. The seed is not used
31 by all implementing classes.
32 """
33 __metaclass__ = abc.ABCMeta
34
35 - def __init__(self, normalise=True, seed=None):
42
43 @abc.abstractmethod
45
46 raise NotImplementedError('Abstract method "get_vector" must be '
47 'specialised!')
48
51 """A pre-determined start vector.
52
53 Parameters
54 ----------
55 vector : Numpy array. The predetermined start vector
56
57 Examples
58 --------
59 >>> from parsimony.utils.start_vectors import IdentityStartVector
60 >>> start_vector = IdentityStartVector(np.array([[0.5], [2.0], [0.3],
61 ... [1.0]]))
62 >>> start_vector.get_vector()
63 array([[ 0.5],
64 [ 2. ],
65 [ 0.3],
66 [ 1. ]])
67 """
73
75 """Return the predetermined start vector
76
77 Examples
78 --------
79 >>> from parsimony.utils.start_vectors import IdentityStartVector
80 >>> start_vector = IdentityStartVector(np.array([[0.5], [2.0], [0.3],
81 ... [1.0]]))
82 >>> start_vector.get_vector()
83 array([[ 0.5],
84 [ 2. ],
85 [ 0.3],
86 [ 1. ]])
87 """
88 return self.vector
89
92 """A start vector of uniformly distributed random values.
93
94 Parameters
95 ----------
96 normalise : Bool. If True, normalise the randomly created vectors.
97 Default is True.
98
99 seed : Integer or None. The seed to the pseudo-random number generator. If
100 none, no seed is used. The seed is set at initialisation, so if the
101 RNG is used in between initialisation and utilisation, then the
102 random numbers will change. Default is None.
103
104 limits : List or tuple. A list or tuple with two elements, the lower and
105 upper limits of the uniform distribution. If normalise=True, then
106 these limits may not be honoured. Default is (0.0, 1.0).
107
108 Examples
109 --------
110 >>> from parsimony.utils.start_vectors import RandomStartVector
111 >>>
112 >>> # Without normalization
113 >>> start_vector = RandomStartVector(normalise=False, seed=42)
114 >>> random = start_vector.get_vector(3)
115 >>> print random
116 [[ 0.37454012]
117 [ 0.95071431]
118 [ 0.73199394]]
119 >>> print maths.norm(random)
120 1.25696186254
121 >>>
122 >>> # With normalization
123 >>> start_vector_normalized = RandomStartVector(normalise=True, seed=2)
124 >>> random_normalized = start_vector_normalized.get_vector(3)
125 >>> print random_normalized
126 [[ 0.62101956]
127 [ 0.03692864]
128 [ 0.78292463]]
129 >>> print maths.norm(random_normalized)
130 1.0
131 >>>
132 >>> # With limits
133 >>> start_vector_normalized = RandomStartVector(normalise=True, seed=2,
134 ... limits=(-1, 1))
135 >>> random_limits = start_vector_normalized.get_vector(3)
136 >>> print random_limits
137 [[-0.1330817 ]
138 [-0.98571123]
139 [ 0.10326001]]
140 >>> print maths.norm(random_limits)
141 1.0
142
143 """
144 - def __init__(self, limits=(0.0, 1.0), **kwargs):
149
151 """Return randomly generated vector of given shape.
152
153 Parameters
154 ----------
155 size : Positive integer. Size of the vector to generate. The shape of
156 the output is (size, 1).
157
158 Examples
159 --------
160 >>> from parsimony.utils.start_vectors import RandomStartVector
161 >>> start_vector = RandomStartVector(normalise=False, seed=42)
162 >>> random = start_vector.get_vector(3)
163 >>> print random
164 [[ 0.37454012]
165 [ 0.95071431]
166 [ 0.73199394]]
167 >>>
168 >>> start_vector = RandomStartVector(normalise=False, seed=1,
169 ... limits=(-1, 2))
170 >>> random = start_vector.get_vector(3)
171 >>> print random
172 [[ 0.25106601]
173 [ 1.16097348]
174 [-0.99965688]]
175 """
176 l = float(self.limits[0])
177 u = float(self.limits[1])
178
179 size = int(size)
180 vector = np.random.rand(size, 1) * (u - l) + l
181
182 if self.normalise:
183 return vector * (1.0 / maths.norm(vector))
184 else:
185 return vector
186
189 """A start vector of ones.
190
191 Parameters
192 ----------
193 normalise : Bool. If True, normalise the randomly created vectors
194 Default is False
195
196 Examples
197 --------
198 >>> from parsimony.utils.start_vectors import OnesStartVector
199
200 # Without normalization
201 >>> start_vector = OnesStartVector(normalise=False)
202 >>> ones = start_vector.get_vector(3)
203 >>> print ones
204 [[ 1.]
205 [ 1.]
206 [ 1.]]
207 >>> print maths.norm(ones)
208 1.73205080757
209
210 # With normalization
211 >>> start_vector_normalized = OnesStartVector(normalise=True)
212 >>> ones_normalized = start_vector_normalized.get_vector(3)
213 >>> print ones_normalized
214 [[ 0.57735027]
215 [ 0.57735027]
216 [ 0.57735027]]
217 >>> print maths.norm(ones_normalized)
218 1.0
219 """
220 - def __init__(self, normalise=False, **kwargs):
223
225 """Return vector of ones of chosen shape
226
227 Parameters
228 ----------
229 size : Positive integer. Size of the vector to generate. The shape of
230 the output is (size, 1).
231
232 Examples
233 --------
234 >>> from parsimony.utils.start_vectors import OnesStartVector
235 >>> start_vector = OnesStartVector()
236 >>> ones = start_vector.get_vector(3)
237 >>> print ones
238 [[ 1.]
239 [ 1.]
240 [ 1.]]
241 """
242 size = int(size)
243 vector = np.ones((size, 1))
244
245 if self.normalise:
246 return vector * (1.0 / maths.norm(vector))
247 else:
248 return vector
249
252 """A start vector of zeros.
253
254 Use with care! Be aware that using this in algorithms that are not aware
255 may result in division by zero since the norm of this start vector is 0.
256
257 Examples
258 --------
259 >>> from parsimony.utils.start_vectors import ZerosStartVector
260 >>> start_vector = ZerosStartVector()
261 >>> zeros = start_vector.get_vector(3)
262 >>> print zeros
263 [[ 0.]
264 [ 0.]
265 [ 0.]]
266 """
272
274 """Return vector of zeros of chosen shape.
275
276 Parameters
277 ----------
278 size : Positive integer. Size of the vector to generate. The shape of
279 the output is (size, 1).
280
281 Examples
282 --------
283 >>> from parsimony.utils.start_vectors import ZerosStartVector
284 >>> start_vector = ZerosStartVector()
285 >>> zeros = start_vector.get_vector(3)
286 >>> print zeros
287 [[ 0.]
288 [ 0.]
289 [ 0.]]
290 """
291 size = int(size)
292 w = np.zeros((size, 1))
293
294 return w
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514 if __name__ == "__main__":
515 import doctest
516 doctest.testmod()
517