Completed
Push — master ( 40c094...b6a1c4 )
by Wojtek
02:30
created

prepare_rand_mock()   B

Complexity

Conditions 2

Size

Total Lines 658

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 658
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"""File which contains data for testing regression with matlab script."""
2
from unittest import TestCase
3
from unittest.mock import patch
4
5
import networkx as nx
0 ignored issues
show
Configuration introduced by
The import networkx could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
8
from grortir.main.model.processes.factories.calls_process_factory import \
9
    CallsProcessFactory
10
from grortir.main.optimizers.grouped_optimizer import GroupedOptimizer
11
from grortir.main.optimizers.grouping_strategy import GroupingStrategy
12
from grortir.main.pso.calls_optimization_strategy import \
13
    CallsOptimizationStrategy
14
from grortir.main.pso.pso_algorithm import PsoAlgorithm
15
16
17
class TestLinearRVT(TestCase):
18
    """Test class for regression."""
19
20
    def _regression_mocked(self, rand_mock, max_calls):
21
        """Test use some mocked values of random function from one known example
22
         from matlab. Because we set the same values in rand function, we should
23
        have equal values as in matlab. This is working example for 3 particles,
24
         5 stages, 2 dimensions when all stages are in one group.
25
         """
26
        prepare_rand_mock(rand_mock)
27
        how_many_particles = 3
28
        max_calls = max_calls
29
        calls_factory = CallsProcessFactory("linear", 5,
30
                                            max_calls,
31
                                            [0, 0])
32
        process = calls_factory.construct_process()
33
        ordered_stages = nx.topological_sort(process)
34
35
        grouping_strategy = GroupingStrategy(ordered_stages)
36
        grouping_strategy.define_group(ordered_stages)
37
        optimization_startegy = CallsOptimizationStrategy()
38
        pso_algortihm = PsoAlgorithm(process, grouping_strategy,
39
                                     optimization_startegy,
40
                                     how_many_particles)
41
        optimizer = GroupedOptimizer(process, grouping_strategy, pso_algortihm)
42
43
        optimizer.optimize_process()
44
45
        for index, stage in enumerate(ordered_stages):
46
            self.assertAlmostEqual(stage.final_quality,
47
                                   EXPECTED_RESULTS_X2_MATLAB[max_calls][index],
48
                                   7)
49
50
    @patch('grortir.main.pso.velocity_calculator.np.random.rand')
51
    def test_matlab_regression_x2_300(self, rand_mock):
52
        """Test case for max_calls = 300."""
53
        self._regression_mocked(rand_mock, 300)
54
55
    @patch('grortir.main.pso.velocity_calculator.np.random.rand')
56
    def test_matlab_regression_x2_102(self, rand_mock):
57
        """Test case for max_calls = 102."""
58
        self._regression_mocked(rand_mock, 102)
59
60
61
EXPECTED_RESULTS_X2_MATLAB = {102: [
62
    0.196381073803784,
63
    0.182060227702233,
64
    0.077236432031289,
65
    0.044953250296990,
66
    0.121949294446318
67
], 300: [
68
    0.175906072353589,
69
    0.174734347239612,
70
    0.042669220710921,
71
    0.077825241269615,
72
    0.175914451614580
73
]}
74
75
76
def prepare_rand_mock(rand_mock):
77
    """Rand mock for:
78
        3 particles
79
        5 stages
80
        2 dimensions
81
    """
82
    returned_values = [  # rand_vec - positions particle 1
83
        [0.076308289, 0.909593528],
84
        [0.723465178, 0.93120602],
85
        [0.501120464, 0.9501295],
86
        [0.499882501, 0.909128375],
87
        [0.380941133, 0.750409859],
88
89
        # rand_vec - initial_velocities particle 1
90
        [0.20484909, 0.719324123],
91
        [0.477401155, 0.180451619],
92
        [0.768647507, 0.426453573],
93
        [0.276049048, 0.414885978],
94
        [0.657399463, 0.709394394],
95
96
        # rand_vec - positions particle 2
97
        [0.779918792, 0.213385354],
98
        [0.977989512, 0.024899228],
99
        [0.072051133, 0.230302879],
100
        [0.679229996, 0.133169446],
101
        [0.065936347, 0.669013241],
102
103
        # rand_vec - initial_velocities particle 2
104
        [0.490765889, 0.412991829],
105
        [0.365890386, 0.741118873],
106
        [0.313994677, 0.634379869],
107
        [0.452842933, 0.001426881],
108
        [0.370351083, 0.524345597],
109
110
        # rand_vec - positions particle 3
111
        [0.438409231, 0.452123962],
112
        [0.53849587, 0.600548917],
113
        [0.26843898, 0.548489919],
114
        [0.803739036, 0.523412581],
115
        [0.288145599, 0.46775286],
116
117
        # rand_vec - initial_velocities particle 3
118
        [0.372384689, 0.906423269],
119
        [0.837917994, 0.422374044],
120
        [0.572625333, 0.522906201],
121
        [0.352978366, 0.092262346],
122
        [0.459092978, 0.696160464],
123
124
        # rand_1
125
        0.955468323002926,
126
        # rand_2
127
        0.682913854375418,
128
        # etc.
129
        0.053128690672958,
130
        0.308852684863797,
131
        0.592594687323373,
132
        0.235120407257464,
133
        0.964970999536127,
134
        0.945048223792794,
135
        0.848400880837689,
136
        0.472323996288402,
137
        0.841476714898397,
138
        0.131110642347914,
139
        0.308733657297835,
140
        0.462996394154417,
141
        0.741847200683467,
142
        0.485825228708897,
143
        0.136876118797451,
144
        0.343536529704358,
145
        0.324426169672443,
146
        0.300418904318040,
147
        0.165501400465787,
148
        0.414901772573252,
149
        0.448120657502723,
150
        0.774900375814017,
151
        0.796390700782548,
152
        0.522390128001612,
153
        0.460630296163277,
154
        0.778213601543897,
155
        0.887288951852735,
156
        0.674918769866788,
157
        0.800479048998464,
158
        0.939111353732185,
159
        0.040655809436686,
160
        0.875671724870083,
161
        0.276563072451134,
162
        0.475764499424244,
163
        0.796760956931902,
164
        0.717242232275230,
165
        0.147147571941693,
166
        0.658748258954145,
167
        0.069252070009835,
168
        0.357070628352879,
169
        0.812829550163706,
170
        0.427704833027551,
171
        0.599854437543446,
172
        0.728161283147134,
173
        0.821227608306559,
174
        0.760515119980283,
175
        0.007143275278998,
176
        0.420256833672535,
177
        0.463136218038147,
178
        0.055499503112723,
179
        0.541442132095220,
180
        0.607770751443867,
181
        0.828453194996125,
182
        0.941809268191449,
183
        0.128147854002733,
184
        0.230430674410219,
185
        0.659158397070561,
186
        0.132473988816749,
187
        0.224078636946319,
188
        0.574862587589149,
189
        0.169523723566209,
190
        0.782230151323778,
191
        0.856975626111760,
192
        0.033674204502848,
193
        0.532644804943674,
194
        0.796951357443564,
195
        0.975139681157116,
196
        0.274258593194421,
197
        0.169101063365383,
198
        0.876700930181635,
199
        0.909182463760849,
200
        0.197532889467157,
201
        0.441529742702640,
202
        0.719232141692337,
203
        0.845345158763973,
204
        0.168275305367244,
205
        0.664968962904761,
206
        0.807835459296069,
207
        0.549714120800727,
208
        0.164716658779013,
209
        0.035528804830363,
210
        0.281533817848494,
211
        0.807870846453552,
212
        0.044766260845139,
213
        0.008216505039312,
214
        0.361616647712057,
215
        0.063622285698552,
216
        0.149486303324230,
217
        0.023190368099362,
218
        0.524719836768632,
219
        0.696695902189413,
220
        0.427053493013728,
221
        0.134570458644688,
222
        0.331357210962406,
223
        0.590345854306335,
224
        0.940661393396388,
225
        0.992557719172640,
226
        0.241602916653850,
227
        0.010579906938346,
228
        0.830640325993805,
229
        0.926612937770057,
230
        0.458603479767969,
231
        0.771442344563682,
232
        0.866199034084391,
233
        0.609614798334559,
234
        0.872627183144187,
235
        0.023903026142331,
236
        0.271595220259564,
237
        0.277219582694295,
238
        0.120632428306158,
239
        0.910713454288238,
240
        0.030439266665623,
241
        0.672561029522113,
242
        0.071339687257207,
243
        0.360780493785221,
244
        0.418099542677566,
245
        0.181404289057367,
246
        0.521014103282176,
247
        0.534991760927916,
248
        0.317043881936196,
249
        0.737088059552291,
250
        0.160202410619170,
251
        0.192508218959855,
252
        0.354511018045606,
253
        0.378376133431419,
254
        0.206285856500654,
255
        0.918709205142268,
256
        0.828093569546948,
257
        0.106876744256641,
258
        0.369485915660384,
259
        0.232671095631974,
260
        0.451078602950542,
261
        0.276317186210793,
262
        0.501806891118423,
263
        0.922603153077957,
264
        0.382511138929834,
265
        0.650128328628633,
266
        0.595621113020374,
267
        0.751953561142208,
268
        0.061665330169869,
269
        0.744823673681810,
270
        0.946275927172428,
271
        0.603559551695486,
272
        0.287579942119900,
273
        0.672369218429286,
274
        0.712048786781395,
275
        0.656450286538602,
276
        0.146930315693544,
277
        0.973475568269701,
278
        0.955383451820879,
279
        0.424625532795641,
280
        0.593637329408872,
281
        0.039628573866894,
282
        0.988634334561939,
283
        0.818744992623299,
284
        0.636502342429667,
285
        0.761084744221686,
286
        0.188029928582586,
287
        0.307654590092852,
288
        0.246393644285721,
289
        0.596049120125150,
290
        0.091904852163772,
291
        0.895609985214942,
292
        0.462275942130728,
293
        0.444812364954379,
294
        0.104711303573244,
295
        0.684904037060114,
296
        0.816893994342448,
297
        0.629550171462332,
298
        0.242022607464133,
299
        0.785420721585510,
300
        0.145679433383434,
301
        0.827276328979597,
302
        0.580705370611886,
303
        0.289372811578436,
304
        0.513243399843739,
305
        0.628851403064647,
306
        0.258589498592646,
307
        0.846906560459974,
308
        0.421253708599273,
309
        0.892337457255232,
310
        0.835468561147535,
311
        0.099347514159631,
312
        0.646254109448792,
313
        0.310494566495742,
314
        0.754053196983175,
315
        0.542631657447731,
316
        0.457598874651717,
317
        0.895366784537062,
318
        0.057253559800168,
319
        0.557317625867979,
320
        0.327911099296676,
321
        0.035273336313463,
322
        0.753518518702918,
323
        0.561740656068876,
324
        0.894145336956216,
325
        0.598261227571831,
326
        0.337034279430268,
327
        0.985219655379547,
328
        0.115714050609305,
329
        0.052601742578789,
330
        0.732621909452081,
331
        0.370874899289484,
332
        0.361465078628537,
333
        0.876532156489572,
334
        0.327293646515387,
335
        0.888988596884109,
336
        0.643987979231622,
337
        0.329049543886762,
338
        0.059532654412467,
339
        0.245109352640558,
340
        0.968413201216849,
341
        0.405267640233955,
342
        0.160033461445328,
343
        0.298013249200853,
344
        0.899575778861325,
345
        0.164976715508502,
346
        0.777860066567281,
347
        0.134864631396897,
348
        0.961393713437924,
349
        0.530205386091562,
350
        0.043169935597521,
351
        0.930954677740200,
352
        0.358225544459122,
353
        0.731421278900107,
354
        0.523708014799748,
355
        0.092581803144957,
356
        0.106059218020559,
357
        0.149224950691087,
358
        0.161200009216847,
359
        0.052755793124503,
360
        0.047013165032900,
361
        0.948428296058719,
362
        0.091291789989614,
363
        0.508351675888390,
364
        0.118629820373576,
365
        0.214787851208711,
366
        0.763072072781084,
367
        0.938134821028349,
368
        0.469712694723217,
369
        0.005641953918998,
370
        0.999208301397325,
371
        0.047773212685936,
372
        0.389447904030537,
373
        0.539056700681636,
374
        0.891733437554219,
375
        0.823112111660542,
376
        0.609386641044624,
377
        0.398602290975866,
378
        0.834478719162267,
379
        0.869058386305670,
380
        0.871904907345985,
381
        0.717636435413349,
382
        0.097787343977917,
383
        0.298579428134399,
384
        0.485922055987473,
385
        0.505053367201341,
386
        0.832873412737514,
387
        0.237836773702923,
388
        0.664590755548858,
389
        0.374199897461594,
390
        0.775482553226357,
391
        0.195774329322454,
392
        0.476627293393027,
393
        0.103558580363407,
394
        0.211990174132978,
395
        0.935174686344270,
396
        0.316047110507828,
397
        0.892977004089998,
398
        0.524303087485621,
399
        0.039033151475225,
400
        0.792981682333857,
401
        0.051956895132490,
402
        0.826943528363990,
403
        0.008799110700279,
404
        0.674111327346674,
405
        0.165713924746117,
406
        0.342875238126080,
407
        0.952751394795348,
408
        0.486233473130853,
409
        0.657426385161858,
410
        0.740735096261484,
411
        0.109927976537212,
412
        0.838795758110978,
413
        0.913919938722552,
414
        0.155420652349409,
415
        0.546238428894773,
416
        0.283505123187259,
417
        0.740646730859789,
418
        0.028270392857499,
419
        0.512547003441387,
420
        0.792814855356639,
421
        0.734736907792527,
422
        0.108183831433641,
423
        0.832940108733776,
424
        0.237840730117073,
425
        0.807787631216831,
426
        0.497128831862446,
427
        0.160825765137294,
428
        0.734040677567008,
429
        0.807912952139197,
430
        0.698901149985949,
431
        0.964846339026962,
432
        0.256144451205951,
433
        0.235762326997982,
434
        0.147230091811600,
435
        0.539406209950657,
436
        0.399448822385716,
437
        0.356662048714573,
438
        0.460304600931861,
439
        0.274542227929074,
440
        0.004260377665060,
441
        0.471750688851412,
442
        0.275884516508146,
443
        0.449783608929888,
444
        0.928875114011594,
445
        0.179005406834722,
446
        0.637483578006810,
447
        0.644414896262441,
448
        0.281293183556632,
449
        0.470887996729068,
450
        0.969611641631324,
451
        0.342460125401140,
452
        0.712345182452183,
453
        0.840219155681310,
454
        0.360691107716966,
455
        0.989914134053230,
456
        0.625869240477631,
457
        0.500985270593922,
458
        0.714907517506250,
459
        0.423829933866559,
460
        0.712326722843593,
461
        0.274802383334603,
462
        0.927208314345688,
463
        0.541962837933759,
464
        0.241649493587256,
465
        0.437196668431502,
466
        0.895096540620895,
467
        0.807055319072113,
468
        0.484468952038663,
469
        0.351701676740330,
470
        0.376247414102291,
471
        0.981195357595743,
472
        0.112212785131700,
473
        0.889023895569209,
474
        0.857608873426699,
475
        0.224388405937294,
476
        0.349358734167979,
477
        0.569559886725433,
478
        0.802392703740247,
479
        0.950445405338136,
480
        0.991808703583101,
481
        0.281231706882225,
482
        0.192363574340851,
483
        0.487725152667488,
484
        0.557563900934068,
485
        0.656704680393275,
486
        0.107591132314336,
487
        0.646356019355153,
488
        0.921379080678338,
489
        0.915201067829541,
490
        0.943773999300075,
491
        0.922339597531884,
492
        0.960777827482642,
493
        0.048692537116349,
494
        0.916018190702266,
495
        0.271212807892103,
496
        0.320665735466156,
497
        0.458614240947963,
498
        0.955280395007635,
499
        0.605088902655730,
500
        0.352693216652037,
501
        0.374157647552767,
502
        0.904928488823167,
503
        0.822347568235971,
504
        0.137537720235976,
505
        0.248681866157169,
506
        0.371945201069240,
507
        0.216741990290173,
508
        0.563481752713618,
509
        0.223638759945981,
510
        0.574924496446997,
511
        0.936024621406188,
512
        0.848675975533586,
513
        0.099263463082568,
514
        0.206904798370428,
515
        0.412749242263999,
516
        0.646127576659247,
517
        0.386566362834885,
518
        0.251612368340209,
519
        0.344659037128738,
520
        0.204013586529791,
521
        0.861866719522683,
522
        0.697251712343329,
523
        0.411029102405943,
524
        0.700431428209971,
525
        0.519729317478726,
526
        0.526262211158570,
527
        0.367355608463953,
528
        0.488218358572307,
529
        0.985516241695369,
530
        0.784464867312748,
531
        0.827700292651744,
532
        0.124371851424182,
533
        0.504273983084654,
534
        0.366900940402063,
535
        0.088906970727544,
536
        0.395277026236348,
537
        0.074187674732258,
538
        0.234019714465247,
539
        0.798762711251609,
540
        0.686217476243966,
541
        0.698546638346593,
542
        0.079357876930814,
543
        0.163982277587226,
544
        0.021584144070867,
545
        0.097514526910029,
546
        0.101218802428116,
547
        0.960144691621100,
548
        0.893951263471334,
549
        0.204253978553430,
550
        0.253812954707619,
551
        0.960877849135793,
552
        0.569997647125525,
553
        0.368523292689715,
554
        0.466544291475107,
555
        0.272209948677861,
556
        0.583577279989395,
557
        0.725271822389783,
558
        0.515448255738200,
559
        0.820666334538531,
560
        0.113210535060978,
561
        0.603652973857912,
562
        0.989602297305464,
563
        0.149079049867556,
564
        0.495036066395777,
565
        0.630145284441744,
566
        0.337529420211799,
567
        0.964617319252480,
568
        0.485410407692122,
569
        0.478698686317731,
570
        0.902663704426373,
571
        0.015910411244440,
572
        0.868885158976661,
573
        0.129073400787753,
574
        0.379168418226396,
575
        0.488456170430982,
576
        0.476558696915659,
577
        0.039408558168118,
578
        0.317107127394286,
579
        0.356794690367346,
580
        0.956179326863944,
581
        0.916985374978591,
582
        0.584348634196598,
583
        0.933367704937314,
584
        0.863712403344464,
585
        0.375722701116526,
586
        0.116966851055671,
587
        0.607137546900165,
588
        0.323009439198225,
589
        0.009742502911630,
590
        0.981179352124537,
591
        0.969276095448999,
592
        0.536806445779076,
593
        0.618185702219458,
594
        0.668866801761036,
595
        0.926538709044051,
596
        0.467508678029278,
597
        0.582878176454622,
598
        0.226539656503770,
599
        0.242398242505033,
600
        0.522112194722591,
601
        0.229204951410235,
602
        0.030252785430061,
603
        0.368407959481347,
604
        0.749945144136553,
605
        0.277070182795515,
606
        0.862703938488081,
607
        0.702502384733204,
608
        0.653319109148049,
609
        0.892483140903137,
610
        0.073811030749940,
611
        0.178765399504983,
612
        0.909270222459388,
613
        0.907037376819791,
614
        0.078267255898490,
615
        0.774231552555221,
616
        0.015432415436891,
617
        0.135235553368546,
618
        0.109760573693710,
619
        0.293995577818383,
620
        0.388268420643610,
621
        0.910873864270862,
622
        0.436397812772955,
623
        0.123849077393590,
624
        0.272348747150734,
625
        0.006376828101283,
626
        0.028049402896190,
627
        0.431068321456428,
628
        0.512188268191776,
629
        0.249961583279100,
630
        0.379892416692459,
631
        0.103368972691539,
632
        0.658821561774462,
633
        0.392605570467657,
634
        0.161525599474659,
635
        0.497393631286220,
636
        0.208374839752940,
637
        0.292129277966286,
638
        0.124170432861418,
639
        0.780730491164767,
640
        0.279315228275671,
641
        0.955676655905381,
642
        0.405431910896856,
643
        0.155194491682113,
644
        0.705212968993427,
645
        0.733558148258664,
646
        0.881319646023381,
647
        0.131512475929842,
648
        0.789885959077657,
649
        0.859759087461651,
650
        0.304518295195883,
651
        0.454482181611214,
652
        0.619641959041911,
653
        0.245513143317838,
654
        0.871355919631592,
655
        0.699194254911087,
656
        0.856683045869332,
657
        0.449915049918563,
658
        0.518998362932756,
659
        0.194254445030116,
660
        0.314024381595863,
661
        0.729512159519921,
662
        0.959007778167720,
663
        0.152102868420930,
664
        0.050643527733202,
665
        0.067452674119625,
666
        0.435471768821968,
667
        0.792671277316913,
668
        0.224877270039349,
669
        0.477132071395185,
670
        0.515402183663472,
671
        0.837702398077169,
672
        0.164033206983383,
673
        0.014718606080760,
674
        0.506655032325643,
675
        0.355245916780601,
676
        0.292165375856786,
677
        0.772237097456584,
678
        0.683467071380522,
679
        0.083937147680062,
680
        0.548103362726367,
681
        0.797844619110595,
682
        0.909684337475206,
683
        1.849190299395564e-04,
684
        0.807550098685804,
685
        0.043247927660589,
686
        0.682120580410576,
687
        0.598007103650569,
688
        0.952016292239996,
689
        0.631879092240196,
690
        0.069939064084080,
691
        0.670397983148165,
692
        0.254920416648672,
693
        0.831943993534743,
694
        0.031462089910604,
695
        0.324135823771549,
696
        0.676435886980024,
697
        0.155634800139984,
698
        0.646272009255702,
699
        0.697224805278189,
700
        0.733777664963868,
701
        0.875335192151078,
702
        0.413166116051979,
703
        0.142482973491241,
704
        0.260310930391146,
705
        0.389685401442653,
706
        0.054577570176633,
707
        0.708548829749348,
708
        0.063010647774122,
709
        0.057708757228460,
710
        0.932499537221799,
711
        0.806245878340841,
712
        0.253681046813631,
713
        0.341708091806300,
714
        0.825919699801219,
715
        0.619235464658352,
716
        0.264917616209675,
717
        0.449019195939970,
718
        0.855559587316900,
719
        6.913526320485586e-04,
720
        0.992393866454483,
721
        0.565933533985480,
722
        0.182020076790290,
723
        0.800192805760516,
724
        0.076195188027430,
725
        0.410000079369867,
726
        0.573132263268180
727
    ]
728
729
    returned_values_arrays = []
730
    for row in returned_values:
731
        returned_values_arrays.append(np.array(row))
732
733
    rand_mock.side_effect = returned_values_arrays
734