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
143
144
145
146
147
148
149
150
151
152
153
154
155
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
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
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-11-03T19:50:50.150235Z",
"start_time": "2020-11-03T19:50:48.888079Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Notebook magic"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T19:16:29.118001Z",
"start_time": "2022-01-07T19:16:29.114692Z"
}
},
"outputs": [],
"source": [
"from IPython.core.magic import Magics, magics_class, line_cell_magic\n",
"from IPython.core.magic import cell_magic, register_cell_magic, register_line_magic\n",
"from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring\n",
"import subprocess\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T19:16:37.453883Z",
"start_time": "2022-01-07T19:16:37.422478Z"
}
},
"outputs": [],
"source": [
"@magics_class\n",
"class PyboardMagic(Magics):\n",
" @cell_magic\n",
" @magic_arguments()\n",
" @argument('-skip')\n",
" @argument('-unix')\n",
" @argument('-pyboard')\n",
" @argument('-file')\n",
" @argument('-data')\n",
" @argument('-time')\n",
" @argument('-memory')\n",
" def micropython(self, line='', cell=None):\n",
" args = parse_argstring(self.micropython, line)\n",
" if args.skip: # doesn't care about the cell's content\n",
" print('skipped execution')\n",
" return None # do not parse the rest\n",
" if args.unix: # tests the code on the unix port. Note that this works on unix only\n",
" with open('/dev/shm/micropython.py', 'w') as fout:\n",
" fout.write(cell)\n",
" proc = subprocess.Popen([\"../micropython/ports/unix/micropython-2\", \"/dev/shm/micropython.py\"], \n",
" stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
" print(proc.stdout.read().decode(\"utf-8\"))\n",
" print(proc.stderr.read().decode(\"utf-8\"))\n",
" return None\n",
" if args.file: # can be used to copy the cell content onto the pyboard's flash\n",
" spaces = \" \"\n",
" try:\n",
" with open(args.file, 'w') as fout:\n",
" fout.write(cell.replace('\\t', spaces))\n",
" printf('written cell to {}'.format(args.file))\n",
" except:\n",
" print('Failed to write to disc!')\n",
" return None # do not parse the rest\n",
" if args.data: # can be used to load data from the pyboard directly into kernel space\n",
" message = pyb.exec(cell)\n",
" if len(message) == 0:\n",
" print('pyboard >>>')\n",
" else:\n",
" print(message.decode('utf-8'))\n",
" # register new variable in user namespace\n",
" self.shell.user_ns[args.data] = string_to_matrix(message.decode(\"utf-8\"))\n",
" \n",
" if args.time: # measures the time of executions\n",
" pyb.exec('import utime')\n",
" message = pyb.exec('t = utime.ticks_us()\\n' + cell + '\\ndelta = utime.ticks_diff(utime.ticks_us(), t)' + \n",
" \"\\nprint('execution time: {:d} us'.format(delta))\")\n",
" print(message.decode('utf-8'))\n",
" \n",
" if args.memory: # prints out memory information \n",
" message = pyb.exec('from micropython import mem_info\\nprint(mem_info())\\n')\n",
" print(\"memory before execution:\\n========================\\n\", message.decode('utf-8'))\n",
" message = pyb.exec(cell)\n",
" print(\">>> \", message.decode('utf-8'))\n",
" message = pyb.exec('print(mem_info())')\n",
" print(\"memory after execution:\\n========================\\n\", message.decode('utf-8'))\n",
"\n",
" if args.pyboard:\n",
" message = pyb.exec(cell)\n",
" print(message.decode('utf-8'))\n",
"\n",
"ip = get_ipython()\n",
"ip.register_magics(PyboardMagic)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## pyboard"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-07T07:35:35.126401Z",
"start_time": "2020-05-07T07:35:35.105824Z"
}
},
"outputs": [],
"source": [
"import pyboard\n",
"pyb = pyboard.Pyboard('/dev/ttyACM0')\n",
"pyb.enter_raw_repl()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-19T19:11:18.145548Z",
"start_time": "2020-05-19T19:11:18.137468Z"
}
},
"outputs": [],
"source": [
"pyb.exit_raw_repl()\n",
"pyb.close()"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-07T07:35:38.725924Z",
"start_time": "2020-05-07T07:35:38.645488Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"%%micropython -pyboard 1\n",
"\n",
"import utime\n",
"import ulab as np\n",
"\n",
"def timeit(n=1000):\n",
" def wrapper(f, *args, **kwargs):\n",
" func_name = str(f).split(' ')[1]\n",
" def new_func(*args, **kwargs):\n",
" run_times = np.zeros(n, dtype=np.uint16)\n",
" for i in range(n):\n",
" t = utime.ticks_us()\n",
" result = f(*args, **kwargs)\n",
" run_times[i] = utime.ticks_diff(utime.ticks_us(), t)\n",
" print('{}() execution times based on {} cycles'.format(func_name, n, (delta2-delta1)/n))\n",
" print('\\tbest: %d us'%np.min(run_times))\n",
" print('\\tworst: %d us'%np.max(run_times))\n",
" print('\\taverage: %d us'%np.mean(run_times))\n",
" print('\\tdeviation: +/-%.3f us'%np.std(run_times)) \n",
" return result\n",
" return new_func\n",
" return wrapper\n",
"\n",
"def timeit(f, *args, **kwargs):\n",
" func_name = str(f).split(' ')[1]\n",
" def new_func(*args, **kwargs):\n",
" t = utime.ticks_us()\n",
" result = f(*args, **kwargs)\n",
" print('execution time: ', utime.ticks_diff(utime.ticks_us(), t), ' us')\n",
" return result\n",
" return new_func"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__END_OF_DEFS__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numerical\n",
"\n",
"Function in this section can be used for calculating statistical properties, or manipulating the arrangement of array elements."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## min, argmin, max, argmax\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.min.html\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.max.html\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html\n",
"\n",
"**WARNING:** Difference to `numpy`: the `out` keyword argument is not implemented.\n",
"\n",
"These functions follow the same pattern, and work with generic iterables, and `ndarray`s. `min`, and `max` return the minimum or maximum of a sequence. If the input array is two-dimensional, the `axis` keyword argument can be supplied, in which case the minimum/maximum along the given axis will be returned. If `axis=None` (this is also the default value), the minimum/maximum of the flattened array will be determined.\n",
"\n",
"`argmin/argmax` return the position (index) of the minimum/maximum in the sequence."
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {
"ExecuteTime": {
"end_time": "2020-10-17T21:26:22.507996Z",
"start_time": "2020-10-17T21:26:22.492543Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([1.0, 2.0, 3.0], dtype=float)\n",
"array([], dtype=float)\n",
"[] 0\n",
"array([1.0, 2.0, 3.0], dtype=float)\n",
"array([], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array([1, 2, 3])\n",
"print(a)\n",
"print(a[-1:-1:-3])\n",
"try:\n",
" sa = list(a[-1:-1:-3])\n",
" la = len(sa)\n",
"except IndexError as e:\n",
" sa = str(e)\n",
" la = -1\n",
" \n",
"print(sa, la)\n",
"\n",
"a[-1:-1:-3] = np.ones(0)\n",
"print(a)\n",
"\n",
"b = np.ones(0) + 1\n",
"print(b)\n",
"# print('b', b.shape())"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {
"ExecuteTime": {
"end_time": "2020-10-17T21:54:49.123748Z",
"start_time": "2020-10-17T21:54:49.093819Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0, 1, -3array([], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"a = np.array([1, 2, 3])\n",
"print(a[0:1:-3])"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {
"ExecuteTime": {
"end_time": "2020-10-17T21:57:01.482277Z",
"start_time": "2020-10-17T21:57:01.477362Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[0]"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = list(range(13))\n",
"\n",
"l[0:10:113]"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"ExecuteTime": {
"end_time": "2020-10-17T20:59:58.285134Z",
"start_time": "2020-10-17T20:59:58.263605Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(0,)"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([1, 2, 3])\n",
"np.ones(0, dtype=uint8) / np.zeros(0, dtype=uint16)\n",
"np.ones(0).shape"
]
},
{
"cell_type": "code",
"execution_count": 375,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-18T13:08:28.113525Z",
"start_time": "2019-10-18T13:08:28.093518Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a: array([1.0, 2.0, 0.0, 1.0, 10.0], dtype=float)\n",
"min of a: 0.0\n",
"argmin of a: 2\n",
"\n",
"b:\n",
" array([[1.0, 2.0, 0.0],\n",
"\t [1.0, 10.0, -1.0]], dtype=float)\n",
"min of b (flattened): -1.0\n",
"min of b (axis=0): array([1.0, 2.0, -1.0], dtype=float)\n",
"min of b (axis=1): array([0.0, -1.0], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([1, 2, 0, 1, 10])\n",
"print('a:', a)\n",
"print('min of a:', numerical.min(a))\n",
"print('argmin of a:', numerical.argmin(a))\n",
"\n",
"b = np.array([[1, 2, 0], [1, 10, -1]])\n",
"print('\\nb:\\n', b)\n",
"print('min of b (flattened):', numerical.min(b))\n",
"print('min of b (axis=0):', numerical.min(b, axis=0))\n",
"print('min of b (axis=1):', numerical.min(b, axis=1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sum, std, mean\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html\n",
"\n",
"These three functions follow the same pattern: if the axis keyword is not specified, it assumes the default value of `None`, and returns the result of the computation for the flattened array. Otherwise, the calculation is along the given axis."
]
},
{
"cell_type": "code",
"execution_count": 527,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-20T06:51:58.845076Z",
"start_time": "2019-10-20T06:51:58.798730Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a: \n",
" array([[1.0, 2.0, 3.0],\n",
"\t [4.0, 5.0, 6.0],\n",
"\t [7.0, 8.0, 9.0]], dtype=float)\n",
"sum, flat array: 45.0\n",
"mean, horizontal: array([2.0, 5.0, 8.0], dtype=float)\n",
"std, vertical: array([2.44949, 2.44949, 2.44949], dtype=float)\n",
"\n"
]
}
],
"source": [
"%%micropython -pyboard 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"print('a: \\n', a)\n",
"\n",
"print('sum, flat array: ', numerical.sum(a))\n",
"\n",
"print('mean, horizontal: ', numerical.mean(a, axis=1))\n",
"\n",
"print('std, vertical: ', numerical.std(a, axis=0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## roll\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.roll.html\n",
"\n",
"The roll function shifts the content of a vector by the positions given as the second argument. If the `axis` keyword is supplied, the shift is applied to the given axis."
]
},
{
"cell_type": "code",
"execution_count": 229,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-11T19:39:47.459395Z",
"start_time": "2019-10-11T19:39:47.443691Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:\t\t\t array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], dtype=float)\n",
"a rolled to the left:\t array([3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 1.0, 2.0], dtype=float)\n",
"a rolled to the right:\t array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"print(\"a:\\t\\t\\t\", a)\n",
"\n",
"numerical.roll(a, 2)\n",
"print(\"a rolled to the left:\\t\", a)\n",
"\n",
"# this should be the original vector\n",
"numerical.roll(a, -2)\n",
"print(\"a rolled to the right:\\t\", a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rolling works with matrices, too. If the `axis` keyword is 0, the matrix is rolled along its vertical axis, otherwise, horizontally. \n",
"\n",
"Horizontal rolls are faster, because they require fewer steps, and larger memory chunks are copied, however, they also require more RAM: basically the whole row must be stored internally. Most expensive are the `None` keyword values, because with `axis = None`, the array is flattened first, hence the row's length is the size of the whole matrix.\n",
"\n",
"Vertical rolls require two internal copies of single columns. "
]
},
{
"cell_type": "code",
"execution_count": 268,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-15T17:46:20.051069Z",
"start_time": "2019-10-15T17:46:20.033205Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:\n",
" array([[1.0, 2.0, 3.0, 4.0],\n",
"\t [5.0, 6.0, 7.0, 8.0]], dtype=float)\n",
"\n",
"a rolled to the left:\n",
" array([[3.0, 4.0, 5.0, 6.0],\n",
"\t [7.0, 8.0, 1.0, 2.0]], dtype=float)\n",
"\n",
"a rolled up:\n",
" array([[6.0, 3.0, 4.0, 5.0],\n",
"\t [2.0, 7.0, 8.0, 1.0]], dtype=float)\n",
"\n",
"a rolled with None:\n",
" array([[3.0, 4.0, 5.0, 2.0],\n",
"\t [7.0, 8.0, 1.0, 6.0]], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
"print(\"a:\\n\", a)\n",
"\n",
"numerical.roll(a, 2)\n",
"print(\"\\na rolled to the left:\\n\", a)\n",
"\n",
"numerical.roll(a, -1, axis=1)\n",
"print(\"\\na rolled up:\\n\", a)\n",
"\n",
"numerical.roll(a, 1, axis=None)\n",
"print(\"\\na rolled with None:\\n\", a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simple running weighted average\n",
"\n",
"As a demonstration of the conciseness of `ulab/numpy` operations, we will calculate an exponentially weighted running average of a measurement vector in just a couple of lines. I chose this particular example, because I think that this can indeed be used in real-life applications."
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-11T20:03:00.713235Z",
"start_time": "2019-10-11T20:03:00.696932Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([0.01165623031556606, 0.03168492019176483, 0.08612854033708572, 0.234121635556221, 0.6364086270332336], dtype=float)\n",
"0.2545634508132935\n",
"array([0.0, 0.0, 0.0, 0.0, 2.0], dtype=float)\n",
"0.3482121050357819\n",
"array([0.0, 0.0, 0.0, 2.0, 2.0], dtype=float)\n",
"0.3826635211706161\n",
"array([0.0, 0.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3953374892473221\n",
"array([0.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3999999813735485\n",
"array([2.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3999999813735485\n",
"array([2.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3999999813735485\n",
"array([2.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3999999813735485\n",
"array([2.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3999999813735485\n",
"array([2.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"0.3999999813735485\n",
"array([2.0, 2.0, 2.0, 2.0, 2.0], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"from ulab import vector\n",
"\n",
"def dummy_adc():\n",
" # dummy adc function, so that the results are reproducible\n",
" return 2\n",
" \n",
"n = 10\n",
"# These are the normalised weights; the last entry is the most dominant\n",
"weight = vector.exp([1, 2, 3, 4, 5])\n",
"weight = weight/numerical.sum(weight)\n",
"\n",
"print(weight)\n",
"# initial array of samples\n",
"samples = np.array([0]*n)\n",
"\n",
"for i in range(n):\n",
" # a new datum is inserted on the right hand side. This simply overwrites whatever was in the last slot\n",
" samples[-1] = dummy_adc()\n",
" print(numerical.mean(samples[-5:]*weight))\n",
" print(samples[-5:])\n",
" # the data are shifted by one position to the left\n",
" numerical.roll(samples, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## flip\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html\n",
"\n",
"The `flip` function takes one positional, an `ndarray`, and one keyword argument, `axis = None`, and reverses the order of elements along the given axis. If the keyword argument is `None`, the matrix' entries are flipped along all axes. `flip` returns a new copy of the array."
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-16T06:35:52.163725Z",
"start_time": "2019-10-16T06:35:52.149231Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a: \t array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)\n",
"a flipped:\t array([5.0, 4.0, 3.0, 2.0, 1.0], dtype=float)\n",
"\n",
"a flipped horizontally\n",
" array([[3, 2, 1],\n",
"\t [6, 5, 4],\n",
"\t [9, 8, 7]], dtype=uint8)\n",
"\n",
"a flipped vertically\n",
" array([[7, 8, 9],\n",
"\t [4, 5, 6],\n",
"\t [1, 2, 3]], dtype=uint8)\n",
"\n",
"a flipped horizontally+vertically\n",
" array([[9, 8, 7],\n",
"\t [6, 5, 4],\n",
"\t [3, 2, 1]], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([1, 2, 3, 4, 5])\n",
"print(\"a: \\t\", a)\n",
"print(\"a flipped:\\t\", np.flip(a))\n",
"\n",
"a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)\n",
"print(\"\\na flipped horizontally\\n\", numerical.flip(a, axis=1))\n",
"print(\"\\na flipped vertically\\n\", numerical.flip(a, axis=0))\n",
"print(\"\\na flipped horizontally+vertically\\n\", numerical.flip(a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## diff\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html\n",
"\n",
"The `diff` function returns the numerical derivative of the forward scheme, or more accurately, the differences of an `ndarray` along a given axis. The order of derivative can be stipulated with the `n` keyword argument, which should be between 0, and 9. Default is 1. If higher order derivatives are required, they can be gotten by repeated calls to the function. The `axis` keyword argument should be -1 (last axis, in `ulab` equivalent to the second axis, and this also happens to be the default value), 0, or 1. \n",
"\n",
"Beyond the output array, the function requires only a couple of bytes of extra RAM for the differentiation stencil. (The stencil is an `int8` array, one byte longer than `n`. This also explains, why the highest order is 9: the coefficients of a ninth-order stencil all fit in signed bytes, while 10 would require `int16`.) Note that as usual in numerical differentiation (and also in `numpy`), the length of the respective axis will be reduced by `n` after the operation. If `n` is larger than, or equal to the length of the axis, an empty array will be returned.\n",
"\n",
"**WARNING**: the `diff` function does not implement the `prepend` and `append` keywords that can be found in `numpy`. "
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T11:51:02.854338Z",
"start_time": "2019-10-31T11:51:02.838000Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:\n",
" array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=uint8)\n",
"\n",
"first derivative:\n",
" array([1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8)\n",
"\n",
"second derivative:\n",
" array([0, 0, 0, 0, 0, 0, 0], dtype=uint8)\n",
"\n",
"c:\n",
" array([[1.0, 2.0, 3.0, 4.0],\n",
"\t [4.0, 3.0, 2.0, 1.0],\n",
"\t [1.0, 4.0, 9.0, 16.0],\n",
"\t [0.0, 0.0, 0.0, 0.0]], dtype=float)\n",
"\n",
"first derivative, first axis:\n",
" array([[3.0, 1.0, -1.0, -3.0],\n",
"\t [-3.0, 1.0, 7.0, 15.0],\n",
"\t [-1.0, -4.0, -9.0, -16.0]], dtype=float)\n",
"\n",
"first derivative, second axis:\n",
" array([[1.0, 1.0, 1.0],\n",
"\t [-1.0, -1.0, -1.0],\n",
"\t [3.0, 5.0, 7.0],\n",
"\t [0.0, 0.0, 0.0]], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array(range(9), dtype=np.uint8)\n",
"print('a:\\n', a)\n",
"\n",
"print('\\nfirst derivative:\\n', numerical.diff(a, n=1))\n",
"print('\\nsecond derivative:\\n', numerical.diff(a, n=2))\n",
"\n",
"c = np.array([[1, 2, 3, 4], [4, 3, 2, 1], [1, 4, 9, 16], [0, 0, 0, 0]])\n",
"print('\\nc:\\n', c)\n",
"print('\\nfirst derivative, first axis:\\n', numerical.diff(c, axis=0))\n",
"print('\\nfirst derivative, second axis:\\n', numerical.diff(c, axis=1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## median\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.median.html\n",
"\n",
"The function computes the median along the specified axis, and returns the median of the array elements. If the `axis` keyword argument is `None`, the arrays is flattened first. The `dtype` of the results is always float."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2020-11-03T19:54:38.047790Z",
"start_time": "2020-11-03T19:54:38.029264Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:\n",
" array([[0, 1, 2, 3],\n",
" [4, 5, 6, 7],\n",
" [8, 9, 10, 11]], dtype=int8)\n",
"\n",
"median of the flattened array: 5.5\n",
"\n",
"median along the vertical axis: array([4.0, 5.0, 6.0, 7.0], dtype=float)\n",
"\n",
"median along the horizontal axis: array([1.5, 5.5, 9.5], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(12), dtype=np.int8).reshape((3, 4))\n",
"print('a:\\n', a)\n",
"print('\\nmedian of the flattened array: ', np.median(a))\n",
"print('\\nmedian along the vertical axis: ', np.median(a, axis=0))\n",
"print('\\nmedian along the horizontal axis: ', np.median(a, axis=1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sort\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html\n",
"\n",
"The sort function takes an ndarray, and sorts its elements in ascending order along the specified axis using a heap sort algorithm. As opposed to the `.sort()` method discussed earlier, this function creates a copy of its input before sorting, and at the end, returns this copy. Sorting takes place in place, without auxiliary storage. The `axis` keyword argument takes on the possible values of -1 (the last axis, in `ulab` equivalent to the second axis, and this also happens to be the default value), 0, 1, or `None`. The first three cases are identical to those in [diff](#diff), while the last one flattens the array before sorting. \n",
"\n",
"If descending order is required, the result can simply be `flip`ped, see [flip](#flip).\n",
"\n",
"**WARNING:** `numpy` defines the `kind`, and `order` keyword arguments that are not implemented here. The function in `ulab` always uses heap sort, and since `ulab` does not have the concept of data fields, the `order` keyword argument would have no meaning."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"ExecuteTime": {
"end_time": "2019-11-05T16:06:27.536193Z",
"start_time": "2019-11-05T16:06:27.521792Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"a:\n",
" array([[1.0, 12.0, 3.0, 0.0],\n",
"\t [5.0, 3.0, 4.0, 1.0],\n",
"\t [9.0, 11.0, 1.0, 8.0],\n",
"\t [7.0, 10.0, 0.0, 1.0]], dtype=float)\n",
"\n",
"a sorted along vertical axis:\n",
" array([[1.0, 3.0, 0.0, 0.0],\n",
"\t [5.0, 10.0, 1.0, 1.0],\n",
"\t [7.0, 11.0, 3.0, 1.0],\n",
"\t [9.0, 12.0, 4.0, 8.0]], dtype=float)\n",
"\n",
"a sorted along horizontal axis:\n",
" array([[0.0, 1.0, 3.0, 12.0],\n",
"\t [1.0, 3.0, 4.0, 5.0],\n",
"\t [1.0, 8.0, 9.0, 11.0],\n",
"\t [0.0, 1.0, 7.0, 10.0]], dtype=float)\n",
"\n",
"flattened a sorted:\n",
" array([0.0, 0.0, 1.0, ..., 10.0, 11.0, 12.0], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([[1, 12, 3, 0], [5, 3, 4, 1], [9, 11, 1, 8], [7, 10, 0, 1]], dtype=np.float)\n",
"print('\\na:\\n', a)\n",
"b = numerical.sort(a, axis=0)\n",
"print('\\na sorted along vertical axis:\\n', b)\n",
"\n",
"c = numerical.sort(a, axis=1)\n",
"print('\\na sorted along horizontal axis:\\n', c)\n",
"\n",
"c = numerical.sort(a, axis=None)\n",
"print('\\nflattened a sorted:\\n', c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Heap sort requires $\\sim N\\log N$ operations, and notably, the worst case costs only 20% more time than the average. In order to get an order-of-magnitude estimate, we will take the sine of 1000 uniformly spaced numbers between 0, and two pi, and sort them:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%micropython -pyboard 1\n",
"\n",
"import ulab as np\n",
"from ulab import vector\n",
"from ulab import numerical\n",
"\n",
"@timeit\n",
"def sort_time(array):\n",
" return numerical.sort(array)\n",
"\n",
"b = vector.sin(np.linspace(0, 6.28, num=1000))\n",
"print('b: ', b)\n",
"sort_time(b)\n",
"print('\\nb sorted:\\n', b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## argsort\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html\n",
"\n",
"Similarly to [sort](#sort), `argsort` takes a positional, and a keyword argument, and returns an unsigned short index array of type `ndarray` with the same dimensions as the input, or, if `axis=None`, as a row vector with length equal to the number of elements in the input (i.e., the flattened array). The indices in the output sort the input in ascending order. The routine in `argsort` is the same as in `sort`, therefore, the comments on computational expenses (time and RAM) also apply. In particular, since no copy of the original data is required, virtually no RAM beyond the output array is used. \n",
"\n",
"Since the underlying container of the output array is of type `uint16_t`, neither of the output dimensions should be larger than 65535. If that happens to be the case, the function will bail out with a `ValueError`."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"ExecuteTime": {
"end_time": "2019-11-06T06:28:45.719578Z",
"start_time": "2019-11-06T06:28:45.704072Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"a:\n",
" array([[1.0, 12.0, 3.0, 0.0],\n",
"\t [5.0, 3.0, 4.0, 1.0],\n",
"\t [9.0, 11.0, 1.0, 8.0],\n",
"\t [7.0, 10.0, 0.0, 1.0]], dtype=float)\n",
"\n",
"a sorted along vertical axis:\n",
" array([[0, 1, 3, 0],\n",
"\t [1, 3, 2, 1],\n",
"\t [3, 2, 0, 3],\n",
"\t [2, 0, 1, 2]], dtype=uint16)\n",
"\n",
"a sorted along horizontal axis:\n",
" array([[3, 0, 2, 1],\n",
"\t [3, 1, 2, 0],\n",
"\t [2, 3, 0, 1],\n",
"\t [2, 3, 0, 1]], dtype=uint16)\n",
"\n",
"flattened a sorted:\n",
" array([3, 14, 0, ..., 13, 9, 1], dtype=uint16)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([[1, 12, 3, 0], [5, 3, 4, 1], [9, 11, 1, 8], [7, 10, 0, 1]], dtype=np.float)\n",
"print('\\na:\\n', a)\n",
"b = numerical.argsort(a, axis=0)\n",
"print('\\na sorted along vertical axis:\\n', b)\n",
"\n",
"c = numerical.argsort(a, axis=1)\n",
"print('\\na sorted along horizontal axis:\\n', c)\n",
"\n",
"c = numerical.argsort(a, axis=None)\n",
"print('\\nflattened a sorted:\\n', c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since during the sorting, only the indices are shuffled, `argsort` does not modify the input array, as one can verify this by the following example:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"ExecuteTime": {
"end_time": "2019-11-06T16:04:31.653444Z",
"start_time": "2019-11-06T16:04:31.634995Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"a:\n",
" array([0, 5, 1, 3, 2, 4], dtype=uint8)\n",
"\n",
"sorting indices:\n",
" array([0, 2, 4, 3, 5, 1], dtype=uint16)\n",
"\n",
"the original array:\n",
" array([0, 5, 1, 3, 2, 4], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"from ulab import numerical\n",
"\n",
"a = np.array([0, 5, 1, 3, 2, 4], dtype=np.uint8)\n",
"print('\\na:\\n', a)\n",
"b = numerical.argsort(a, axis=1)\n",
"print('\\nsorting indices:\\n', b)\n",
"print('\\nthe original array:\\n', a)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "382.797px"
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|