Example #1
0
 def double_exp_xoffset(self, param, x, y=None):
     """
     You can try this for a positive current
     200.,100. is for the decay part. it decays to 0 
     -200.,30. is for the rising part. it rise to 0
     a=numpy.arange(500)
     f=ARG.double_exp_var(a,*[0,200.,100.,-200.,30.,0.])
     pyplot.plot(f)
     pyplot.show()
     
     Y0 is the Y_Offset
     A2,A2 are the values of the exponential at X=0
     tau1 and tau2 are the corresponding time constant
     if tau1<tau2 the resulting bi-exponential is negative
     if tau1>tau2 the resulting bi-exponential is positive
     if tau1=tau2 the resulting bi-exponential is an horizontal line
     X_Offset switch the peak to right if positive, or to the left if negative
     """
     p = []
     for i in list(param):
         p.append(param[i].value)
     p = p[:6]
     Y0, A1, tau1, A2, tau2, X_Offset = p
     if y is None:
         return (Y0 + A1 * numpy.exp(-(x - X_Offset) / tau1) +
                 A2 * numpy.exp(-(x - X_Offset) / tau2))
     return (Y0 + A1 * numpy.exp(-(x - X_Offset) / tau1) +
             A2 * numpy.exp(-(x - X_Offset) / tau2)) - y
Example #2
0
 def double_exp_xoffset(self,param,x,y=None):
     """
     You can try this for a positive current
     200.,100. is for the decay part. it decays to 0 
     -200.,30. is for the rising part. it rise to 0
     a=numpy.arange(500)
     f=ARG.double_exp_var(a,*[0,200.,100.,-200.,30.,0.])
     pyplot.plot(f)
     pyplot.show()
     
     Y0 is the Y_Offset
     A2,A2 are the values of the exponential at X=0
     tau1 and tau2 are the corresponding time constant
     if tau1<tau2 the resulting bi-exponential is negative
     if tau1>tau2 the resulting bi-exponential is positive
     if tau1=tau2 the resulting bi-exponential is an horizontal line
     X_Offset switch the peak to right if positive, or to the left if negative
     """
     p=[]
     for i in list(param):
         p.append(param[i].value)    
     p=p[:6]    
     Y0, A1, tau1, A2, tau2, X_Offset = p
     if y is None:
         return (Y0 + A1*numpy.exp(-(x-X_Offset)/tau1) + A2*numpy.exp(-(x-X_Offset)/tau2))
     return (Y0 + A1*numpy.exp(-(x-X_Offset)/tau1) + A2*numpy.exp(-(x-X_Offset)/tau2)) - y 
Example #3
0
 def lognormal(self,param,x,y=None):
     p=[]
     for i in list(param):
         p.append(param[i].value)    
     p=p[:4]    
     Y0, A, mu, width = p
     if y is None:
         return (Y0 + A*numpy.exp(-((numpy.log(x-mu)/width)**2)))
     return (Y0 + A*numpy.exp(-((numpy.log(x-mu)/width)**2))) - y
Example #4
0
 def sigmoid(self,param,x,y=None):
     p=[]
     for i in list(param):
         p.append(param[i].value)    
     p=p[:4]    
     base, Max, xhalf, rate = p
     if y is None:
         return (base+(Max/(1+numpy.exp((xhalf-x)/rate))))
     return (base+(Max/(1+numpy.exp((xhalf-x)/rate)))) - y
Example #5
0
 def double_exp(self,param,x,y=None):
     p=[]
     for i in list(param):
         p.append(param[i].value)    
     p=p[:5]    
     Y0, A1, tau1, A2, tau2 = p
     if y is None:
         return (Y0 + A1*numpy.exp(-x/tau1) + A2*numpy.exp(-x/tau2))
     return (Y0 + A1*numpy.exp(-x/tau1) + A2*numpy.exp(-x/tau2)) - y        
Example #6
0
 def lognormal(self, param, x, y=None):
     p = []
     for i in list(param):
         p.append(param[i].value)
     p = p[:4]
     Y0, A, mu, width = p
     if y is None:
         return (Y0 + A * numpy.exp(-((numpy.log(x - mu) / width)**2)))
     return (Y0 + A * numpy.exp(-((numpy.log(x - mu) / width)**2))) - y
Example #7
0
 def sigmoid(self, param, x, y=None):
     p = []
     for i in list(param):
         p.append(param[i].value)
     p = p[:4]
     base, Max, xhalf, rate = p
     if y is None:
         return (base + (Max / (1 + numpy.exp((xhalf - x) / rate))))
     return (base + (Max / (1 + numpy.exp((xhalf - x) / rate)))) - y
Example #8
0
 def double_exp(self, param, x, y=None):
     p = []
     for i in list(param):
         p.append(param[i].value)
     p = p[:5]
     Y0, A1, tau1, A2, tau2 = p
     if y is None:
         return (Y0 + A1 * numpy.exp(-x / tau1) + A2 * numpy.exp(-x / tau2))
     return (Y0 + A1 * numpy.exp(-x / tau1) + A2 * numpy.exp(-x / tau2)) - y
Example #9
0
 def gauss(self,param,x,y=None):
     """
     width (or Full Width at Half Maximum, or FWHM) is 2.sigma.sqrt(2.ln(2)), or about 2.35.sigma
     """
     p=[]
     for i in list(param):
         p.append(param[i].value)        
     p=p[:4]    
     Y0, A, mu, width = p
     if y is None:
         return (Y0 + A*numpy.exp(-((x-mu)/width)**2))
     return (Y0 + A*numpy.exp(-((x-mu)/width)**2)) - y        
Example #10
0
 def gauss(self, param, x, y=None):
     """
     width (or Full Width at Half Maximum, or FWHM) is 2.sigma.sqrt(2.ln(2)), or about 2.35.sigma
     """
     p = []
     for i in list(param):
         p.append(param[i].value)
     p = p[:4]
     Y0, A, mu, width = p
     if y is None:
         return (Y0 + A * numpy.exp(-((x - mu) / width)**2))
     return (Y0 + A * numpy.exp(-((x - mu) / width)**2)) - y
Example #11
0
 def exp(self,param,x,y=None):
     p=[]
     for i in list(param):
         p.append(param[i].value)        
     p=p[:3]    
     Y0, A, tau = p
     return Y0 + A*numpy.exp(-x/tau)
Example #12
0
 def exp(self, param, x, y=None):
     p = []
     for i in list(param):
         p.append(param[i].value)
     p = p[:3]
     Y0, A, tau = p
     return Y0 + A * numpy.exp(-x / tau)
Example #13
0
def idft(row):
    row = np.asarray(row, dtype=float)
    N = row.shape[0]
    n = np.arange(N)
    k = n.reshape((N, 1))
    M = np.exp(2j * np.pi * k * n / N)
    return np.dot(M, row) / N
Example #14
0
def ifft(row):
    N = row.shape[0]
    if N % 2 > 0:
        raise ValueError("size of x must be a power of 2")
    elif N <= 2:  # this cutoff should be optimized
        return dft(row)
    else:
        X_even = ifft(row[::2])
        X_odd = ifft(row[1::2])
        factor = np.exp(2j * np.pi * np.arange(N) / N)
        concatenated = np.concatenate([
            X_even + factor[:N // 2] * X_odd, X_even + factor[N // 2:] * X_odd
        ])
        return concatenated
Example #15
0
print(
    "0000 | 0\n0001 | 1\n0010 | 0\n0100 | 0\n1000 | 0\n0011 | 1\n0110 | 0\n1100 | 0\n1001 | 1\n1111 | 1\n\n0111 | ?\n1110 | ?"
)

x = np.array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0],
              [1, 0, 0, 0], [0, 0, 1, 1], [0, 1, 1, 0], [1, 1, 0, 0],
              [1, 0, 0, 1], [1, 1, 1, 1]])

y = np.array([[0, 1, 0, 0, 0, 1, 0, 0, 1, 1]]).T

w = np.random.random((4, 5))
w2 = np.random.random((5, 5))
w3 = np.random.random((5, 1))

for j in xrange(50000):
    a2 = 1 / (1 + np.exp(-((np.dot(x, w) + 1))))
    a3 = 1 / (1 + np.exp(-((np.dot(a2, w2) + 1))))
    a4 = 1 / (1 + np.exp(-(np.dot(a3, w3) + 1)))

    graphicalDataError.append(np.mean(np.abs(y - a4)))
    graphicalDataEpochs.append(j)

    print(y.shape, a4.shape)

    a4delta = (y - a4) * (a4 * (1 - a4))
    a3delta = a4delta.dot(w3.T) * (a3 * (1 - a3))
    a2delta = a3delta.dot(w2.T) * (a2 * (1 - a2))

    w3 += a3.T.dot(a4delta)
    w2 += a2.T.dot(a3delta)
    w += x.T.dot(a2delta)
Example #16
0
    def cannyEdgeDetectorFilter(self, img, sigma=1, t_low=0.01, t_high=0.2):

        im = img.convert('L')
        img = np.array(im, dtype=float)

        # 1) Convolve gaussian kernel with gradient
        # gaussian kernel
        halfSize = 3 * sigma
        maskSize = 2 * halfSize + 1
        mat = np.ones((maskSize, maskSize)) / (float)(2 * np.pi * (sigma**2))
        xyRange = np.arange(-halfSize, halfSize + 1)
        xx, yy = np.meshgrid(xyRange, xyRange)
        x2y2 = (xx**2 + yy**2)
        exp_part = np.exp(-(x2y2 / (2.0 * (sigma**2))))
        gSig = mat * exp_part

        gx, gy = self.drogEdgeDetectorFilter(gSig, ret_grad=True, pillow=False)

        # 2) Magnitude and Angles
        # apply kernels for Ix & Iy
        Ix = cv2.filter2D(img, -1, gx)
        Iy = cv2.filter2D(img, -1, gy)

        # compute magnitude
        mag = np.sqrt(Ix**2 + Iy**2)

        # normalize magnitude image
        normMag = my_Normalize(mag)

        # compute orientation of gradient
        orient = np.arctan2(Iy, Ix)

        # round elements of orient
        orientRows = orient.shape[0]
        orientCols = orient.shape[1]

        # 3) Non maximum suppression
        for i in range(0, orientRows):
            for j in range(0, orientCols):
                if normMag[i, j] > t_low:
                    # case 0
                    if (orient[i, j] > (-np.pi / 8) and orient[i, j] <=
                        (np.pi / 8)):
                        orient[i, j] = 0
                    elif (orient[i, j] > (7 * np.pi / 8)
                          and orient[i, j] <= np.pi):
                        orient[i, j] = 0
                    elif (orient[i, j] >= -np.pi and orient[i, j] <
                          (-7 * np.pi / 8)):
                        orient[i, j] = 0
                    # case 1
                    elif (orient[i, j] > (np.pi / 8) and orient[i, j] <=
                          (3 * np.pi / 8)):
                        orient[i, j] = 3
                    elif (orient[i, j] >= (-7 * np.pi / 8) and orient[i, j] <
                          (-5 * np.pi / 8)):
                        orient[i, j] = 3
                    # case 2
                    elif (orient[i, j] > (3 * np.pi / 8) and orient[i, j] <=
                          (5 * np.pi / 8)):
                        orient[i, j] = 2
                    elif (orient[i, j] >= (-5 * np.pi / 4) and orient[i, j] <
                          (-3 * np.pi / 8)):
                        orient[i, j] = 2
                    # case 3
                    elif (orient[i, j] > (5 * np.pi / 8) and orient[i, j] <=
                          (7 * np.pi / 8)):
                        orient[i, j] = 1
                    elif (orient[i, j] >= (-3 * np.pi / 8) and orient[i, j] <
                          (-np.pi / 8)):
                        orient[i, j] = 1

        mag = normMag
        mag_thin = np.zeros(mag.shape)
        for i in range(mag.shape[0] - 1):
            for j in range(mag.shape[1] - 1):
                if mag[i][j] < t_low:
                    continue
                if orient[i][j] == 0:
                    if mag[i][j] > mag[i][j - 1] and mag[i][j] >= mag[i][j +
                                                                         1]:
                        mag_thin[i][j] = mag[i][j]
                if orient[i][j] == 1:
                    if mag[i][j] > mag[i - 1][j + 1] and mag[i][j] >= mag[
                            i + 1][j - 1]:
                        mag_thin[i][j] = mag[i][j]
                if orient[i][j] == 2:
                    if mag[i][j] > mag[i - 1][j] and mag[i][j] >= mag[i +
                                                                      1][j]:
                        mag_thin[i][j] = mag[i][j]
                if orient[i][j] == 3:
                    if mag[i][j] > mag[i - 1][j - 1] and mag[i][j] >= mag[
                            i + 1][j + 1]:
                        mag_thin[i][j] = mag[i][j]

        # 4) Thresholding and edge linking

        result_binary = np.zeros(mag_thin.shape)

        tHigh = t_high
        tLow = t_low
        # forward scan
        for i in range(0, mag_thin.shape[0] - 1):  # rows
            for j in range(0, mag_thin.shape[1] - 1):  # columns
                if mag_thin[i][j] >= tHigh:
                    if mag_thin[i][j + 1] >= tLow:  # right
                        mag_thin[i][j + 1] = tHigh
                    if mag_thin[i + 1][j + 1] >= tLow:  # bottom right
                        mag_thin[i + 1][j + 1] = tHigh
                    if mag_thin[i + 1][j] >= tLow:  # bottom
                        mag_thin[i + 1][j] = tHigh
                    if mag_thin[i + 1][j - 1] >= tLow:  # bottom left
                        mag_thin[i + 1][j - 1] = tHigh

        # backwards scan
        for i in range(mag_thin.shape[0] - 2, 0, -1):  # rows
            for j in range(mag_thin.shape[1] - 2, 0, -1):  # columns
                if mag_thin[i][j] >= tHigh:
                    if mag_thin[i][j - 1] > tLow:  # left
                        mag_thin[i][j - 1] = tHigh
                    if mag_thin[i - 1][j - 1]:  # top left
                        mag_thin[i - 1][j - 1] = tHigh
                    if mag_thin[i - 1][j] > tLow:  # top
                        mag_thin[i - 1][j] = tHigh
                    if mag_thin[i - 1][j + 1] > tLow:  # top right
                        mag_thin[i - 1][j + 1] = tHigh

        # fill in result_binary
        for i in range(0, mag_thin.shape[0] - 1):  # rows
            for j in range(0, mag_thin.shape[1] - 1):  # columns
                if mag_thin[i][j] >= tHigh:
                    result_binary[i][j] = 255  # set to 255 for >= tHigh

        img = Image.fromarray(result_binary)
        return img
Example #17
0
from matplotlib import numpy as np
from matplotlib import pyplot as plt
import scipy.optimize as opt

def twoD_Gaussian((x, y), amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
    xo = float(xo)
    yo = float(yo)    
    a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
    b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
    c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
    g = offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) 
                            + c*((y-yo)**2)))
    return g.ravel()

# Create x and y indices
x = np.linspace(0, 200, 201)
y = np.linspace(0, 200, 201)
x, y = np.meshgrid(x, y)

#create data
data = twoD_Gaussian((x, y), 3, 100, 100, 20, 40, 0, 10)

# plot twoD_Gaussian data generated above
plt.figure()
plt.imshow(data.reshape(201, 201))
plt.colorbar()

# add some noise to the data and try to fit the data generated beforehand
initial_guess = (3,100,100,20,40,0,10)

data_noisy = data + 0.2*np.random.normal(size=data.shape)