Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# imports
# standard
import random
import math
from statistics import mean
# local, internal
from src.topupopt.data.misc import utils
class TestDataUtils:
def test_profile_synching2(self):
integration_result = 10446
ratio_min_avg = 0.2
min_max_ratio = ratio_min_avg / (2 - ratio_min_avg)
states = [
2.66,
2.34,
3.54,
7.42,
11.72,
16.94,
17.94,
17.98,
14.1,
10.48,
6.74,
3.16,
]
time_interval_durations = [
31, # jan
28, # fev
31, # mar
30, # apr
31, # may
30, # june
31, # july
31, # august
30, # september
31, # october
30, # november
31, # december
]
# *********************************************************************
# *********************************************************************
# state correlates with output
new_profile = utils.create_profile_using_time_weighted_state(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
min_max_ratio=min_max_ratio,
states_correlate_profile=False,
)
expected_result = [
1500.0513102636057,
1436.2189684321309,
1500.0513102636044,
1206.6051909345115,
896.2493366213225,
525.7218723351705,
283.3475134825171,
185.83058429361876,
270.8127439165165,
538.2566419011698,
861.4985340132666,
1241.355993542566,
]
abs_tol = 1e-3
assert math.isclose(sum(new_profile), integration_result, abs_tol=abs_tol)
for sample, expected_sample in zip(new_profile, expected_result):
assert math.isclose(sample, expected_sample, abs_tol=abs_tol)
# *********************************************************************
# *********************************************************************
# state does not correlate with output
# state correlates with output
new_profile = utils.create_profile_using_time_weighted_state(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
min_max_ratio=min_max_ratio,
states_correlate_profile=True,
)
expected_result = [
274.3377308322865,
166.45500417060902,
274.33773083228533,
510.54549399699533,
878.1397044745678,
1191.4288125963367,
1491.041527613374,
1588.5584568022714,
1446.3379410149894,
1236.132399194721,
855.6521509182398,
533.0330475533233,
]
abs_tol = 1e-3
assert math.isclose(sum(new_profile), integration_result, abs_tol=abs_tol)
for sample, expected_sample in zip(new_profile, expected_result):
assert math.isclose(sample, expected_sample, abs_tol=abs_tol)
# *********************************************************************
# *********************************************************************
# find out the peaks of the sinusoidal profile
pmax, pmin = utils.max_min_sinusoidal_profile(
integration_result=integration_result,
period=sum(time_interval_durations),
time_interval_duration=mean(time_interval_durations),
min_max_ratio=min_max_ratio,
)
expected_pmax, expected_pmin = 1558.972133279683, 182.02786672031687
assert math.isclose(pmax, expected_pmax, abs_tol=1e-3)
assert math.isclose(pmin, expected_pmin, abs_tol=1e-3)
# *********************************************************************
# *********************************************************************
# raise exception
Pedro L. Magalhães
committed
error_raised = False
time_interval_durations.pop(0)
try:
new_profile = utils.create_profile_using_time_weighted_state(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
min_max_ratio=min_max_ratio,
states_correlate_profile=True,
)
except ValueError:
Pedro L. Magalhães
committed
error_raised = True
assert error_raised
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
# *********************************************************************
# *********************************************************************
# *************************************************************************
# *************************************************************************
def test_profile_synching(self):
# synch, normal, ex1
profile = [1, 2, 3, 4]
reference_profile = [2, 3, 4, 1]
synched_profile = utils.synch_profile(profile, reference_profile)
true_synched_profile = [2, 3, 4, 1]
assert repr(synched_profile) == repr(true_synched_profile)
# synch, normal, ex2
profile = [-2, -1, 1, 2, 0]
reference_profile = [2, 3, 4, 1, 5]
synched_profile = utils.synch_profile(profile, reference_profile)
true_synched_profile = [-1, 0, 1, -2, 2]
assert repr(synched_profile) == repr(true_synched_profile)
# synch, alternative, ex1
profile = [1, 2, 3, 4]
reference_profile = [2, 3, 4, 1]
synched_profile = utils.synch_profile(profile, reference_profile, synch=False)
true_synched_profile = [3, 2, 1, 4]
assert repr(synched_profile) == repr(true_synched_profile)
# *************************************************************************
# *************************************************************************
def test_profile_generation(self):
# *********************************************************************
# fixed time interval durations
number_tests = 10
for test_index in range(number_tests):
integration_period = 365 * 24 * 3600
number_intervals = random.randint(1, 8760)
phase_shift_radians = 2 * math.pi * random.random()
time_interval_durations = [
round(integration_period / number_intervals)
for i in range(number_intervals)
]
integration_result = 100
min_max_ratio = 0.2
profile = utils.discrete_sinusoid_matching_integral(
integration_result,
time_interval_durations,
min_max_ratio,
phase_shift_radians=phase_shift_radians,
)
assert math.isclose(sum(profile), integration_result, abs_tol=0.01)
# *********************************************************************
# import matplotlib.pyplot as plt
# # Data for plotting
# x = [i for i in range(number_intervals)]
# y = profile
# fig, ax = plt.subplots()
# ax.plot(x, y)
# ax.set(xlabel='time (s)', ylabel='voltage (mV)',
# title='About as simple as it gets, folks')
# ax.grid()
# #fig.savefig("test.png")
# plt.show()
# *********************************************************************
# variable time step durations
number_tests = 10
for test_index in range(number_tests):
number_intervals = random.randint(10, 8760)
time_interval_durations = [
random.random() * 3.6e3 for i in range(number_intervals)
]
integration_period = sum(time_interval_durations)
phase_shift_radians = 2 * math.pi * random.random()
integration_result = 100
min_max_ratio = 0.2
profile = utils.discrete_sinusoid_matching_integral(
integration_result,
time_interval_durations,
min_max_ratio,
phase_shift_radians=phase_shift_radians,
)
assert math.isclose(sum(profile), integration_result, abs_tol=0.01)
# *********************************************************************
# # import matplotlib.pyplot as plt
# t = [sum(time_interval_durations[0:i])
# for i in range(len(time_interval_durations)+1)]
# # Data for plotting
# x = [(t[i+1]+t[i])*0.5
# for i in range(number_intervals)] # time interval's center point
# y = profile
# fig, ax = plt.subplots()
# ax.plot(x, y)
# ax.set(xlabel='time (s)', ylabel='voltage (mV)',
# title='About as simple as it gets, folks')
# ax.grid()
# #fig.savefig("test.png")
# plt.show()
# *********************************************************************
# use the default phase shift
integration_period = 365 * 24 * 3600
number_intervals = random.randint(1, 8760)
time_interval_durations = [
round(integration_period / number_intervals)
for i in range(number_intervals)
]
integration_result = 100
min_max_ratio = 0.2
profile = utils.discrete_sinusoid_matching_integral(
integration_result, time_interval_durations, min_max_ratio
)
assert math.isclose(sum(profile), integration_result, abs_tol=0.01)
# *************************************************************************
# *************************************************************************
def test_key_generation(self):
# generate_pseudo_unique_key
key_list = (str(random.random()) for i in range(10))
new_key = utils.generate_pseudo_unique_key(key_list=key_list)
assert new_key not in key_list
# use an empty key list
new_key = utils.generate_pseudo_unique_key(key_list=[])
assert new_key not in key_list
# use zero iterations to force an error
Pedro L. Magalhães
committed
error_raised = False
try:
new_key = utils.generate_pseudo_unique_key(
key_list=key_list, max_iterations=0
)
except Exception:
Pedro L. Magalhães
committed
error_raised = True
assert error_raised
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# use a seed number to trigger more iterations
import uuid
rand = random.Random()
rand.seed(360)
uuid.uuid4 = lambda: uuid.UUID(int=rand.getrandbits(128), version=4)
key_list = [
"3e225573-4e78-48c8-bb08-efbeeb795c22",
"f6d30428-15d1-41e9-a952-0742eaaa5a31",
"8c29b906-2518-41c5-ada8-07b83508b5b8",
"f9a72a39-1422-4a02-af97-906ce79c32a3",
"b6941a48-10cc-465d-bf53-178bd2939bd1",
]
new_key = utils.generate_pseudo_unique_key(key_list=key_list)
assert new_key not in key_list
# *************************************************************************
# *************************************************************************
def test_state_correlated_profile(self):
# correlation: direct, inverse
# states: positive, negative
# time intervals: regular irregular
#
# profile with positive correlation, positive states, regular intervals
number_time_intervals = 10
states = [i+1 for i in range(number_time_intervals)]
integration_result = 100
time_interval_durations = [10 for i in range(number_time_intervals)]
states_correlate_profile = True
min_max_ratio = 0.2
profile, a, b = utils.generate_state_correlated_profile(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
states_correlate_profile=states_correlate_profile,
min_max_ratio=min_max_ratio,
solver='glpk'
)
# test profile
assert a > 0 and b > 0
assert len(profile) == number_time_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
assert math.isclose(min(profile), max(profile)*min_max_ratio, abs_tol=1e-3)
assert max(profile) == profile[number_time_intervals-1]
# profile with inverse correlation, positive states, regular intervals
number_time_intervals = 10
states = [i+1 for i in range(number_time_intervals)]
integration_result = 100
time_interval_durations = [10 for i in range(number_time_intervals)]
states_correlate_profile = False
min_max_ratio = 0.2
profile, a, b = utils.generate_state_correlated_profile(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
states_correlate_profile=states_correlate_profile,
min_max_ratio=min_max_ratio,
solver='glpk'
)
# test profile
assert a < 0 and b > 0
assert len(profile) == number_time_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
assert math.isclose(min(profile), max(profile)*min_max_ratio, abs_tol=1e-3)
assert min(profile) == profile[number_time_intervals-1]
# *************************************************************************
# *************************************************************************
def test_trigger_state_correlated_profile_error(self):
# trigger an error
number_time_intervals = 10
states = [i+1 for i in range(number_time_intervals)]
integration_result = 100
time_interval_durations = [10 for i in range(number_time_intervals+1)]
states_correlate_profile = True
min_max_ratio = 0.2
error_raised = False
try:
utils.generate_state_correlated_profile(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
states_correlate_profile=states_correlate_profile,
min_max_ratio=min_max_ratio,
solver='glpk'
)
except ValueError:
error_raised = True
assert error_raised
# *************************************************************************
# *************************************************************************
def test_manual_state_correlated_profile(self):
# correlation: direct, inverse
# states: positive, negative
# time intervals: regular irregular
# profile with positive correlation, positive states, regular intervals
number_time_intervals = 10
states = [i+1 for i in range(number_time_intervals)]
integration_result = 100
time_interval_durations = [10 for i in range(number_time_intervals)]
deviation_gain = 1
profile = utils.generate_manual_state_correlated_profile(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
deviation_gain=deviation_gain
)
# test profile
assert len(profile) == number_time_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
assert max(profile) == profile[number_time_intervals-1]
# profile with inverse correlation, positive states, regular intervals
number_time_intervals = 10
states = [i+1 for i in range(number_time_intervals)]
integration_result = 100
time_interval_durations = [10 for i in range(number_time_intervals)]
deviation_gain = -1
profile = utils.generate_manual_state_correlated_profile(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
deviation_gain=deviation_gain
)
# test profile
assert len(profile) == number_time_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
assert min(profile) == profile[number_time_intervals-1]
# *************************************************************************
# *************************************************************************
def test_trigger_manual_state_correlated_profile_error(self):
# trigger an error
number_time_intervals = 10
states = [i+1 for i in range(number_time_intervals)]
integration_result = 100
time_interval_durations = [10 for i in range(number_time_intervals+1)]
deviation_gain = -1
error_raised = False
try:
utils.generate_manual_state_correlated_profile(
integration_result=integration_result,
states=states,
time_interval_durations=time_interval_durations,
deviation_gain=deviation_gain
)
except ValueError:
error_raised = True
assert error_raised
# *************************************************************************
# *************************************************************************
def test_create_profile_sinusoidal(self):
number_intervals = 10
integration_result = 100
min_max_ratio = 0.25
# sinusoidal profile
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
# sinusoidal profile with phase shift
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
phase_shift_radians=math.pi/2
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
# use incorrect parameter
error_raised = False
try:
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
deviation_gain=-1,
)
except TypeError:
error_raised = True
assert error_raised
# *************************************************************************
# *************************************************************************
def test_create_profile_predefined_gain(self):
number_intervals = 10
integration_result = 100
deviation_gain = 5
states = [number_intervals-i*0.5 for i in range(number_intervals)]
# predefined gain
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
states=states,
deviation_gain=deviation_gain
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
# predefined gain, opposite sign
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
states=states,
deviation_gain=-deviation_gain
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
# use incorrect parameter
error_raised = False
try:
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
states=states,
deviation_gain=-deviation_gain,
phase_shift_radians=math.pi
)
except TypeError:
error_raised = True
assert error_raised
# *************************************************************************
# *************************************************************************
def test_create_profile_via_sorting_sinusoid(self):
number_intervals = 10
integration_result = 100
states_correlate_profile = True
min_max_ratio = 0.25
states = [number_intervals-i*0.5 for i in range(number_intervals)]
# sorting and sinusoidal function
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
states=states,
states_correlate_profile=states_correlate_profile,
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
# *************************************************************************
# *************************************************************************
def test_create_profile_via_optimisation(self):
number_intervals = 10
integration_result = 100
states_correlate_profile = True
min_max_ratio = 0.25
solver = 'glpk'
states = [number_intervals-i*0.5 for i in range(number_intervals)]
# optimisation
# states_correlate_profile is necessary
# min_max_ratio is necessary
# solver is necessary
# states matter but the gain must be determined
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
states=states,
states_correlate_profile=states_correlate_profile,
solver=solver
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
assert math.isclose(min(profile),max(profile)*min_max_ratio, abs_tol=1e-3)
# optimisation but with states that do no warrant it
states = [5 for i in range(number_intervals)]
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
states=states,
states_correlate_profile=states_correlate_profile,
solver=solver
)
assert len(profile) == number_intervals
assert math.isclose(sum(profile), integration_result, abs_tol=1e-3)
# the min to max ratio cannot be observed if the states do not change
assert math.isclose(min(profile), max(profile), abs_tol=1e-3)
# use incorrect parameter
error_raised = False
try:
profile = utils.generate_profile(
integration_result=integration_result,
time_interval_durations=[1 for i in range(number_intervals)],
min_max_ratio=min_max_ratio,
states=states,
states_correlate_profile=states_correlate_profile,
solver=solver,
phase_shift_radians=math.pi
)
except TypeError:
error_raised = True
assert error_raised
# *************************************************************************
# *************************************************************************
# *****************************************************************************
# *****************************************************************************