-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvolvLibTestFile.py
More file actions
152 lines (106 loc) · 4.59 KB
/
convolvLibTestFile.py
File metadata and controls
152 lines (106 loc) · 4.59 KB
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
import numpy as np
import convolvLib as cl
import dataPull
import DepConvLib.primaryConvolve as pc
import matplotlib.pyplot as plt
import random
import stackOverflowTestFile as stackTest
axis = dataPull.neutronEnergyList
dataSet = dataPull.theoryValuesList
currentCon = pc.convolution_2d_changing_kernel(dataSet, dataPull.matrixGaussianFunc(axis), axis)
libraryConv = cl.convolve2D(cl.kernel, dataSet, axis)
if (currentCon==libraryConv).all():
print("Same convolutions")
else:
print("Diffrence found")
#analitical test case
#numpy convolution function
#numpyConvolv = np.convolve(dataSet, dataPull.matrixGaussianFunc(axis), mode="same")
#if (currentCon==numpyConvolv).all() and (libraryConv==numpyConvolv).all():
# print("Same convolutions to numpy's convolution")
#else:
# print("Diffrence found")
numpyConvolution = np.convolve(dataSet, dataPull.matrixGaussianFunc(axis)[0], mode="full")
singleDimConv = pc.convolution_1d_same(dataSet, dataPull.matrixGaussianFunc(axis)[0], axis)
"""
if (singleDimConv==numpyConvolution).all():
print("Same convolutions to numpy's convolution")
else:
print("Diffrence found")
#
"""
def convolutionTest1(convolutionFunc):
axis = np.linspace(-10,10,200)
arbitarySigma = 2
trueAnalitical = 1/(np.sqrt(2*np.pi* (arbitarySigma**2 + arbitarySigma**2))) * np.exp(-(axis**2)/(2*(arbitarySigma**2 + arbitarySigma**2)))
fFunction = (1/np.sqrt(2*np.pi*arbitarySigma**2)) * np.exp((-1 * axis**2)/(2*arbitarySigma**2))
gFunction = fFunction
return trueAnalitical, convolutionFunc(fFunction, gFunction, axis), fFunction, gFunction, axis
def convolutionTest2(convolutionFunc):
axis = np.linspace(-10,10, 8001)
print(axis)
def TriangleFunc(arr):
return np.where((arr >= 0) & (arr <= 2), arr, 0)
def squareFunc(arr):
return np.where((arr >= 0) & (arr <= 2), 5, 0)
#introduce analitical solution from notes
def analyilticlSolution(axis):
output = np.zeros(len(axis))
for i, x in enumerate(axis):
if x < 0:
output[i] = 0
elif x >= 0 and x <= 2:
output[i] = (5/2)* (x**2)
elif x > 2 and x < 4:
output[i] = (10 * x -10) - (5*x*(x-2)-(5/2)*(x-2)**2)
elif x >= 4:
output[i] = 0
else:
print("Something broke")
return output
return analyilticlSolution(axis), convolutionFunc(TriangleFunc(axis), squareFunc(axis), axis), TriangleFunc(axis), squareFunc(axis), axis
def convolutionTest3(convolutionFunc):
#make the axis non uniform stepping size
def changeingStep():
xAxis = np.linspace(-10,10,100)
finalX = []
for i, x in enumerate(xAxis):
finalX.append(x * i)
return finalX
axis = changeingStep()
axis = np.array(axis)
print(np.diff(axis))
arbitarySigma = 2
trueAnalitical = 1/(np.sqrt(2*np.pi* (arbitarySigma**2 + arbitarySigma**2))) * np.exp(-(axis**2)/(2*(arbitarySigma**2 + arbitarySigma**2)))
fFunction = (1/np.sqrt(2*np.pi*arbitarySigma**2)) * np.exp((-1 * axis**2)/(2*arbitarySigma**2))
gFunction = fFunction
return trueAnalitical, convolutionFunc(fFunction, gFunction, axis), fFunction, gFunction, axis
def plotConvolutionTest(testFunc, convolutionFunc):
trueCon, testCon, kernal, signal, axis = testFunc(convolutionFunc)
plt.plot(axis, trueCon, label= "True Con", color = "blue", )
plt.plot(axis, testCon, label= "Test Con", color = "red")
plt.plot(axis, kernal, label= "Kernel Func", color = "yellow")
plt.plot(axis, signal, label= "Signal Func", color = "purple")
plt.plot(axis, np.convolve(kernal, signal, mode= "same") * np.diff(axis)[0], label="Numpy")
#plt.plot(axis, stackTest.convolve_1d_same(signal, kernal) * np.diff(axis)[0], label="Stack Conv")
plt.legend(loc="upper left", fontsize = 24)
plt.show()
plotConvolutionTest(convolutionTest2, pc.convolution_1and2D)
"""
def peiceWiseConvolve(kernelArray, dataSet, axis):
kernelArrayLength = len(kernelArray)
dataSetLength = len(dataSet)
#create if statments for cases of unequal length arrays
#needs to be able to check for aprox 5 total cases exluding edge cases such as trivial products
if kernelArrayLength <= dataSetLength:
leftSideIntergral()
rightSideIntergral()
if kernelArrayLength == dataSetLength:
equalArrayIntergral()
else:
smallerKernalIntergral()
elif kernelArrayLength >= dataSetLength:
leftSideIntergral()
rightSideIntergral()
kernalOverlapIntergral()
"""