1import cmsisdsp as dsp
2import numpy as np
3from numpy.testing import assert_allclose
4from scipy.interpolate import CubicSpline
5from sklearn.naive_bayes import GaussianNB
6from sklearn import svm
7import math
8
9a=[1,4,2,6,7,0,-3,5]
10ref=sorted(a)
11print(ref)
12
13SORT_BITONIC=0
14SORT_BUBBLE=1
15SORT_HEAP=2
16SORT_INSERTION=3
17SORT_QUICK=4
18SORT_SELECTION=5
19
20
21SORT_DESCENDING = 0
22SORT_ASCENDING = 1
23
24sortinst=dsp.arm_sort_instance_f32()
25
26for mode in range(6):
27    dsp.arm_sort_init_f32(sortinst,mode,SORT_ASCENDING)
28    res=dsp.arm_sort_f32(sortinst,a)
29    print(res)
30    assert (res==ref).all()
31
32print("")
33ref.reverse()
34print(ref)
35
36for mode in range(6):
37    # Problem with bitonic probably in the C code
38    if mode > 0:
39       dsp.arm_sort_init_f32(sortinst,mode,SORT_DESCENDING)
40       res=dsp.arm_sort_f32(sortinst,a)
41       print(res)
42       assert (res==ref).all()
43
44print("Spline")
45
46x = np.arange(0, 2*np.pi+np.pi/4, np.pi/4)
47y = np.sin(x)
48xnew = np.arange(0, 2*np.pi+np.pi/16, np.pi/16)
49ynew = CubicSpline(x,y,bc_type="natural")
50yref=ynew(xnew)
51print(yref)
52
53splineInst = dsp.arm_spline_instance_f32()
54dsp.arm_spline_init_f32(splineInst,0,x,y)
55yres=dsp.arm_spline_f32(splineInst,xnew)
56print(yres)
57
58assert_allclose(yref,yres,1e-6,1e-6)
59
60print("Bayes")
61# Reusing example from https://developer.arm.com/documentation/102052/0000/Train-your-Bayesian-estimator-with-scikit-learn
62
63NBVECS = 100
64VECDIM = 2
65
66# 3 cluster of points are generated (3 classes)
67ballRadius = 1.0
68x1 = [1.5, 1] +  ballRadius * np.random.randn(NBVECS,VECDIM)
69x2 = [-1.5, 1] + ballRadius * np.random.randn(NBVECS,VECDIM)
70x3 = [0, -3] + ballRadius * np.random.randn(NBVECS,VECDIM)
71
72# All points are concatenated
73X_train=np.concatenate((x1,x2,x3))
74
75# The classes are 0,1 and 2.
76Y_train=np.concatenate((np.zeros(NBVECS),np.ones(NBVECS),2*np.ones(NBVECS)))
77
78gnb = GaussianNB()
79gnb.fit(X_train, Y_train)
80
81src1=[1.5,1.0]
82src2=[-1.5,1]
83src3=[0,-3]
84ref1 = gnb.predict([src1])
85print(ref1)
86
87ref2 = gnb.predict([src2])
88print(ref2)
89
90ref3 = gnb.predict([src3])
91print(ref3)
92
93#print(gnb.predict_log_proba([src]))
94
95theta=list(np.reshape(gnb.theta_,np.size(gnb.theta_)))
96
97# Gaussian variances
98sigma=list(np.reshape(gnb.var_,np.size(gnb.var_)))
99
100# Class priors
101prior=list(np.reshape(gnb.class_prior_,np.size(gnb.class_prior_)))
102
103epsilon=gnb.epsilon_
104
105bayesInst = dsp.arm_gaussian_naive_bayes_instance_f32(
106    vectorDimension=VECDIM,numberOfClasses=3,
107    theta=theta,sigma=sigma,classPriors=prior,epsilon=epsilon)
108
109_,res1=dsp.arm_gaussian_naive_bayes_predict_f32(bayesInst,src1)
110print(res1)
111
112_,res2=dsp.arm_gaussian_naive_bayes_predict_f32(bayesInst,src2)
113print(res2)
114
115_,res3=dsp.arm_gaussian_naive_bayes_predict_f32(bayesInst,src3)
116print(res3)
117
118assert res1 == ref1
119assert res2 == ref2
120assert res3 == ref3
121
122print("SVM")
123
124NBVECS = 100
125VECDIM = 2
126
127ballRadius = 0.5
128x = ballRadius * np.random.randn(NBVECS, 2)
129
130angle = 2.0 * math.pi * np.random.randn(1, NBVECS)
131radius = 3.0 + 0.1 * np.random.randn(1, NBVECS)
132
133xa = np.zeros((NBVECS,2))
134xa[:, 0] = radius * np.cos(angle)
135xa[:, 1] = radius * np.sin(angle)
136
137X_train = np.concatenate((x, xa))
138Y_train = np.concatenate((np.zeros(NBVECS), np.ones(NBVECS)))
139
140clf = svm.SVC(kernel='poly', gamma='auto', coef0=1.1)
141clf.fit(X_train, Y_train)
142
143test1 = np.array([0.4,0.1])
144test1 = test1.reshape(1,-1)
145
146refpredicted1 = clf.predict(test1)
147print(refpredicted1)
148
149test2 = np.array([3.1,0.1])
150test2 = test2.reshape(1,-1)
151
152refpredicted2 = clf.predict(test2)
153print(refpredicted2)
154
155supportShape = clf.support_vectors_.shape
156
157nbSupportVectors = supportShape[0]
158vectorDimensions = supportShape[1]
159
160degree=clf.degree
161coef0=clf.coef0
162gamma=clf._gamma
163
164intercept=clf.intercept_
165
166dualCoefs = clf.dual_coef_
167dualCoefs = dualCoefs.reshape(nbSupportVectors)
168supportVectors = clf.support_vectors_
169supportVectors = supportVectors.reshape(nbSupportVectors * VECDIM)
170
171svmInst=dsp.arm_svm_polynomial_instance_f32()
172
173
174dsp.arm_svm_polynomial_init_f32(svmInst,nbSupportVectors,vectorDimensions,
175    intercept[0],dualCoefs,supportVectors,
176    [0,1],degree,coef0,gamma)
177
178exit(0)
179
180test1 = np.array([0.4,0.1])
181predicted1 = dsp.arm_svm_polynomial_predict_f32(svmInst,test1)
182print(predicted1)
183
184test2 = np.array([3.1,0.1])
185predicted2 = dsp.arm_svm_polynomial_predict_f32(svmInst,test2)
186print(predicted2)
187
188assert predicted1==refpredicted1
189assert predicted2==refpredicted2