In [5]:
import sys
sys.path.append('../code/functions/')
import tiffIO as io
import neuroGraphLib as ngl
from neuroGraphLib import neuroGraph
from scipy.stats import zscore
from skimage.exposure import equalize_adapthist
from scipy.ndimage import convolve
from scipy.signal import argrelmax
import synapseLib as sl
from skimage.measure import label

import matplotlib.pyplot as plt

import cv2
import time
import math
import numpy as np

In [6]:
data_0 = np.array(io.loadTiff('../data/rr46b_s0_ch1.tif'))
data_1 = np.array(io.loadTiff('../data/rr46b_s1_ch1.tif'))

In [3]:
start = time.time()
neuroGraphStack_0 = ngl.generateNeuroGraphStack(data_0)
end = time.time()
print 'Neuro Graph Generation Took: ', end - start


Growing Dendrites
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:110: UserWarning: Possible precision loss when converting from float64 to uint16
  "%s to %s" % (dtypeobj_in, dtypeobj))
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:106: UserWarning: Possible sign loss when converting negative image of type int64 to positive image of type uint16.
  "%s to positive image of type %s." % (dtypeobj_in, dtypeobj))
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 50 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 42 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 44 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 33 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 40 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 47 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 48 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 49 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 41 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 55 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 57 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 46 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 38 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 35 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 60 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 45 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 36 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 39 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 34 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 31 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 29 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 32 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 30 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 61 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 62 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 59 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 56 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 54 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 58 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 52 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 53 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 51 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 63 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 64 fits in uint16
  warn(msg)
Estimating Graph Connections. Completion:
	0.0
	0.0153846153846
	0.0307692307692
	0.0461538461538
	0.0615384615385
	0.0769230769231
	0.0923076923077
	0.107692307692
	0.123076923077
	0.138461538462
	0.153846153846
	0.169230769231
	0.184615384615
	0.2
	0.215384615385
	0.230769230769
	0.246153846154
	0.261538461538
	0.276923076923
	0.292307692308
	0.307692307692
	0.323076923077
	0.338461538462
	0.353846153846
	0.369230769231
	0.384615384615
	0.4
	0.415384615385
	0.430769230769
	0.446153846154
	0.461538461538
	0.476923076923
	0.492307692308
	0.507692307692
	0.523076923077
	0.538461538462
	0.553846153846
	0.569230769231
	0.584615384615
	0.6
	0.615384615385
	0.630769230769
	0.646153846154
	0.661538461538
	0.676923076923
	0.692307692308
	0.707692307692
	0.723076923077
	0.738461538462
	0.753846153846
	0.769230769231
	0.784615384615
	0.8
	0.815384615385
	0.830769230769
	0.846153846154
	0.861538461538
	0.876923076923
	0.892307692308
	0.907692307692
	0.923076923077
	0.938461538462
	0.953846153846
	0.969230769231
	0.984615384615
Neuro Graph Generation Took:  233.273061991

In [7]:
start = time.time()
neuroGraphStack_1 = ngl.generateNeuroGraphStack(data_1)
end = time.time()
print 'Neuro Graph Generation Took: ', end - start


Growing Dendrites
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:110: UserWarning: Possible precision loss when converting from float64 to uint16
  "%s to %s" % (dtypeobj_in, dtypeobj))
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:106: UserWarning: Possible sign loss when converting negative image of type int64 to positive image of type uint16.
  "%s to positive image of type %s." % (dtypeobj_in, dtypeobj))
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 41 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 46 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 49 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 58 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 50 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 43 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 39 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 33 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 36 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 40 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 54 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 56 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 35 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 44 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 32 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 29 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 30 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 25 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 37 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 34 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 31 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 38 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 26 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 23 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 22 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 27 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 28 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 60 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 59 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 57 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 55 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 51 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 52 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 53 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 48 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 62 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 61 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 63 fits in uint16
  warn(msg)
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.py:136: UserWarning: Downcasting int64 to uint16 without scaling because max value 64 fits in uint16
  warn(msg)
Estimating Graph Connections. Completion:
	0.0
	0.0166666666667
	0.0333333333333
	0.05
	0.0666666666667
	0.0833333333333
	0.1
	0.116666666667
	0.133333333333
	0.15
	0.166666666667
	0.183333333333
	0.2
	0.216666666667
	0.233333333333
	0.25
	0.266666666667
	0.283333333333
	0.3
	0.316666666667
	0.333333333333
	0.35
	0.366666666667
	0.383333333333
	0.4
	0.416666666667
	0.433333333333
	0.45
	0.466666666667
	0.483333333333
	0.5
	0.516666666667
	0.533333333333
	0.55
	0.566666666667
	0.583333333333
	0.6
	0.616666666667
	0.633333333333
	0.65
	0.666666666667
	0.683333333333
	0.7
	0.716666666667
	0.733333333333
	0.75
	0.766666666667
	0.783333333333
	0.8
	0.816666666667
	0.833333333333
	0.85
	0.866666666667
	0.883333333333
	0.9
	0.916666666667
	0.933333333333
	0.95
	0.966666666667
	0.983333333333
Neuro Graph Generation Took:  213.885783911

In [5]:
for i in range(10, 20):
    plt.subplot(121)
    plt.imshow(neuroGraphStack_0[i]._vis, cmap='gray')
    
    plt.subplot(122)
    plt.imshow(data_0[i]*100, cmap='gray')
    
    plt.show()



In [6]:
for i in range(10, 20):
    plt.subplot(121)
    plt.imshow(neuroGraphStack_1[i]._vis, cmap='gray')
    
    plt.subplot(122)
    plt.imshow(data_1[i]*100, cmap='gray')
    
    plt.show()



In [55]:
def generateTube(mat, x0, x1, y0, y1, thickness):
    x = np.linspace(x0, x1, int(np.sqrt(np.abs(x1**2 - x0**2))))
    y = np.linspace(y0, y1, int(np.sqrt(np.abs(y1**2 - y0**2))))
    coords = zip(x, y)
    perpSlope = -1.0*(x1 - x0)/(y1 - y0)
    numSamplePoints = thickness
    netCoords = []
    for coord in coords:
        xtop = int(math.floor(coord[0])) + numSamplePoints*perpSlope
        xbot = int(math.floor(coord[0])) - numSamplePoints*perpSlope
        xperp = np.linspace(xtop, xbot, int(np.sqrt(np.abs(xtop**2 - xbot**2))))
        ytop = int(math.floor(coord[1])) + numSamplePoints
        
        ybot = int(math.floor(coord[1])) - numSamplePoints
        yperp = np.linspace(ytop, ybot, int(np.sqrt(np.abs(ytop**2 - ybot**2))))
        coordsPerp = zip(xperp, yperp)
        
        finalCoordsPerp = []
        for coordPerp in coordsPerp:
            if coordPerp[0] > 0 and coordPerp[1] > 0 and coordPerp[0] < mat.shape[0] - 1 and coordPerp[1] < mat.shape[1] - 1:
                finalCoordsPerp.append(((int(math.floor(coordPerp[1]))), (int(math.floor(coordPerp[0])))))
        netCoords.append(finalCoordsPerp)
    return netCoords

mat = np.zeros((301,301))
netCoords = generateTube(mat, 0, 300, 0, 300, 50)
#finalCoordsPerp is what Brandon wants
for viewCoord in netCoords:
    for subCoord in viewCoord:
        mat[subCoord[0]][subCoord[1]] = 2
plt.imshow(mat)
plt.show()



In [8]:
testNeuroGraph = neuroGraphStack_1[15]

In [57]:
len(testNeuroGraph._edges)


Out[57]:
293

In [58]:
vis = np.zeros_like(testNeuroGraph._vis)
for i in range(0, len(testNeuroGraph._edges), 10):
    y, x = testNeuroGraph._edges[i]
    
    curROI = generateTube(vis, x[0], x[-1], y[0], y[-1], 10)

    ROIDist = []
    for orthagonal in curROI:
        orthagonalDist = []
        for elem in orthagonal:
            orthagonalDist.append(data_1[15][elem[0]][elem[1]])
        ROIDist.append(np.mean(orthagonalDist))
        
    for k in range(len(y)):
        vis[max(y[k]-3,0):min(y[k]+3, vis.shape[0]), max(x[k]-3, 0):min(x[k]+3, vis.shape[1])] = 255
    
    plt.figure()
    plt.scatter(range(len(ROIDist)), ROIDist)
    plt.show()


---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-58-52ffe78b59bf> in <module>()
      3     y, x = testNeuroGraph._edges[i]
      4 
----> 5     curROI = generateTube(vis, x[0], x[-1], y[0], y[-1], 10)
      6 
      7     ROIDist = []

<ipython-input-55-52be7aabd17d> in generateTube(mat, x0, x1, y0, y1, thickness)
      3     y = np.linspace(y0, y1, int(np.sqrt(np.abs(y1**2 - y0**2))))
      4     coords = zip(x, y)
----> 5     perpSlope = -1.0*(x1 - x0)/(y1 - y0)
      6     numSamplePoints = thickness
      7     netCoords = []

ZeroDivisionError: float division by zero

In [ ]:
plt.figure()
plt.imshow(vis, cmap='gray')
plt.show()

As expected, the distribution spikes when walking across the edge. Time to mark the elements that are sufficiently 'spiked' as synapse


In [24]:
def getSynapseROIs(neuroGraph):
    synapseROIs = []
    for i in range(0, len(testNeuroGraph._edges)):
        print i/float(len(testNeuroGraph._edges))
        y, x = testNeuroGraph._edges[i]
    
        start = time.time()
        curROI = generateTube(vis, x[0], y[0], x[-1], y[-1], 10)
        end = time.time()
        print 'Generating tube took: ', end - start
        
        start = time.time()
        ROIDist = []
        for orthagonal in curROI:
            orthagonalDist = []
            for elem in orthagonal:
                orthagonalDist.append(data_1[20][elem[0]][elem[1]])
            ROIDist.append(np.mean(orthagonalDist))
        
        end = time.time()
        print 'Generating dist took: ', end-start
        
        start = time.time()
        mu = np.average(ROIDist)
        sig = np.std(ROIDist)
        end = time.time()
        print'Numpy stats took: ', end - start
        
        start = time.time()
        z = [(elem - mu)/sig for elem in ROIDist]
        
        for i, elem in enumerate(z):
            if z >= 2:
                synapseROIs = synapseROIs + curROI[i]
        end = time.time()
        print 'List stats took: ', end - start
        
    return synapseROIs

In [25]:
synapseROIs = getSynapseROIs(testNeuroGraph)


0.0
Generating tube took:  0.0141429901123
Generating dist took:  0.004723072052
Numpy stats took:  8.89301300049e-05
List stats took:  0.00229096412659
0.00446428571429
Generating tube took:  0.0672678947449
Generating dist took:  0.0228440761566
Numpy stats took:  0.000108003616333
List stats took:  0.0721831321716
0.00892857142857
Generating tube took:  0.0792679786682
Generating dist took:  0.0299029350281
Numpy stats took:  0.000123977661133
List stats took:  0.273699045181
0.0133928571429
Generating tube took:  0.0886921882629
Generating dist took:  0.0259139537811
Numpy stats took:  0.000118017196655
List stats took:  0.632496833801
0.0178571428571
Generating tube took:  0.0908691883087
Generating dist took:  0.0256531238556
Numpy stats took:  0.000115871429443
List stats took:  0.985887050629
0.0223214285714
Generating tube took:  0.221467971802
Generating dist took:  0.0772049427032
Numpy stats took:  0.00018310546875
List stats took:  3.57307291031
0.0267857142857
Generating tube took:  0.146853923798
Generating dist took:  0.0452871322632
Numpy stats took:  0.000150918960571
List stats took:  3.72477602959
0.03125
Generating tube took:  0.179939031601
Generating dist took:  0.0377998352051
Numpy stats took:  0.000153779983521
List stats took:  4.23021316528
0.0357142857143
Generating tube took:  0.130398035049
Generating dist took:  0.0438578128815
Numpy stats took:  0.000145196914673
List stats took:  4.49622297287
0.0401785714286
Generating tube took:  0.106725931168
Generating dist took:  0.037976026535
Numpy stats took:  0.000132083892822
List stats took:  4.5429391861
0.0446428571429
Generating tube took:  0.10689496994
Generating dist took:  0.0310139656067
Numpy stats took:  0.000128984451294
List stats took:  4.68736696243
0.0491071428571
Generating tube took:  0.112553834915
Generating dist took:  0.0318269729614
Numpy stats took:  0.000141143798828
List stats took:  5.37558698654
0.0535714285714
Generating tube took:  0.112096071243
Generating dist took:  0.0307021141052
Numpy stats took:  0.000128030776978
List stats took:  6.04226303101
0.0580357142857
Generating tube took:  0.0642819404602
Generating dist took:  0.0149111747742
Numpy stats took:  0.000148057937622
List stats took:  4.04233908653
0.0625
Generating tube took:  0.0659370422363
Generating dist took:  0.015035867691
Numpy stats took:  9.58442687988e-05
List stats took:  3.47289609909
0.0669642857143
Generating tube took:  0.063866853714
Generating dist took:  0.0164711475372
Numpy stats took:  9.91821289062e-05
List stats took:  3.64347195625
0.0714285714286
Generating tube took:  0.0899128913879
Generating dist took:  0.0262701511383
Numpy stats took:  0.000119209289551
List stats took:  5.61796593666
0.0758928571429
Generating tube took:  0.184833049774
Generating dist took:  0.0627880096436
Numpy stats took:  0.000179052352905
List stats took:  12.5294418335
0.0803571428571
Generating tube took:  0.199116945267
Generating dist took:  0.0801301002502
Numpy stats took:  0.000327825546265
List stats took:  12.7536270618
0.0848214285714
Generating tube took:  0.0284390449524
Generating dist took:  0.00389885902405
Numpy stats took:  8.60691070557e-05
List stats took:  1.43954586983
0.0892857142857
Generating tube took:  0.0215888023376
Generating dist took:  0.00346612930298
Numpy stats took:  8.20159912109e-05
List stats took:  1.44247508049
0.09375
Generating tube took:  0.0344128608704
Generating dist took:  0.0035560131073
Numpy stats took:  0.000119924545288
List stats took:  1.52794885635
0.0982142857143
Generating tube took:  0.200921058655
Generating dist took:  0.0795071125031
Numpy stats took:  0.000221967697144
List stats took:  13.7846281528
0.102678571429
Generating tube took:  0.198568105698
Generating dist took:  0.0678069591522
Numpy stats took:  0.000194072723389
List stats took:  15.0269610882
0.107142857143
Generating tube took:  0.182887792587
Generating dist took:  0.0556261539459
Numpy stats took:  0.000198841094971
List stats took:  14.5174260139
0.111607142857
Generating tube took:  0.186153173447
Generating dist took:  0.0562689304352
Numpy stats took:  0.000200986862183
List stats took:  17.0972459316
0.116071428571
Generating tube took:  0.202940940857
Generating dist took:  0.0665860176086
Numpy stats took:  0.000209093093872
List stats took:  19.2683849335
0.120535714286
Generating tube took:  0.170881032944
Generating dist took:  0.0525140762329
Numpy stats took:  0.0001540184021
List stats took:  18.7072019577
0.125
Generating tube took:  0.268071889877
Generating dist took:  0.0857138633728
Numpy stats took:  0.000221014022827
List stats took:  27.5352339745
0.129464285714
Generating tube took:  0.16769194603
Generating dist took:  0.0579240322113
Numpy stats took:  0.000146865844727
List stats took:  20.3943989277
0.133928571429
Generating tube took:  0.111814975739
Generating dist took:  0.0265100002289
Numpy stats took:  0.000113964080811
List stats took:  12.1231219769
0.138392857143
Generating tube took:  0.0806701183319
Generating dist took:  0.0202701091766
Numpy stats took:  0.000113010406494
List stats took:  9.81721115112
0.142857142857
Generating tube took:  0.112473011017
Generating dist took:  0.0259890556335
Numpy stats took:  0.000118017196655
List stats took:  11.3592100143
0.147321428571
Generating tube took:  0.069837808609
Generating dist took:  0.00757598876953
Numpy stats took:  8.48770141602e-05
List stats took:  4.83581399918
0.151785714286
Generating tube took:  0.0596849918365
Generating dist took:  0.00543904304504
Numpy stats took:  7.70092010498e-05
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
KeyboardInterrupt: 

In [59]:
def getSynapseROIs(neuroGraph):
    synapseROIs = []
    #for i in range(0, 1):
    for i in range(0, len(testNeuroGraph._edges)):
        print i/float(len(testNeuroGraph._edges))
        try:
            y, x = testNeuroGraph._edges[i]
    
            curROI = np.array(generateTube(vis, x[0], x[-1], y[0], y[-1], 20))
        
            ROIDist = []
            for orthagonal in curROI:
                orthagonalDist = []
                for elem in orthagonal:
                    #since will returns xy
                    orthagonalDist.append(data_1[20][elem[0]][elem[1]])
                ROIDist.append(np.mean(orthagonalDist))
    
            valid = np.array(np.where(zscore(ROIDist) > 1.5)[0])
            if len(valid) > 0:
                toAdd = np.concatenate([elem for elem in curROI[valid]])
                synapseROIs.append(toAdd)

        except:
            continue
    return synapseROIs

In [60]:
synapseROIs = getSynapseROIs(testNeuroGraph)


0.0
0.00341296928328
0.00682593856655
0.0102389078498
0.0136518771331
0.0170648464164
0.0204778156997
0.0238907849829
0.0273037542662
0.0307167235495
0.0341296928328
0.037542662116
0.0409556313993
0.0443686006826
0.0477815699659
0.0511945392491
0.0546075085324
0.0580204778157
0.061433447099
0.0648464163823
0.0682593856655
0.0716723549488
0.0750853242321
0.0784982935154
0.0819112627986
0.0853242320819
0.0887372013652
0.0921501706485
0.0955631399317
0.098976109215
0.102389078498
0.105802047782
0.109215017065
/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:19: RuntimeWarning: invalid value encountered in greater
0.112627986348
0.116040955631
0.119453924915
0.122866894198
0.126279863481
0.129692832765
0.133105802048
0.136518771331
0.139931740614
0.143344709898
0.146757679181
0.150170648464
0.153583617747
0.156996587031
0.160409556314
0.163822525597
0.167235494881
0.170648464164
0.174061433447
0.17747440273
0.180887372014
0.184300341297
0.18771331058
0.191126279863
0.194539249147
0.19795221843
0.201365187713
0.204778156997
0.20819112628
0.211604095563
0.215017064846
0.21843003413
0.221843003413
0.225255972696
0.22866894198
0.232081911263
0.235494880546
0.238907849829
0.242320819113
0.245733788396
0.249146757679
0.252559726962
0.255972696246
0.259385665529
0.262798634812
0.266211604096
0.269624573379
0.273037542662
0.276450511945
0.279863481229
0.283276450512
0.286689419795
0.290102389078
0.293515358362
0.296928327645
0.300341296928
0.303754266212
0.307167235495
0.310580204778
0.313993174061
0.317406143345
0.320819112628
0.324232081911
0.327645051195
0.331058020478
0.334470989761
0.337883959044
0.341296928328
0.344709897611
0.348122866894
0.351535836177
0.354948805461
0.358361774744
0.361774744027
0.365187713311
0.368600682594
0.372013651877
0.37542662116
0.378839590444
0.382252559727
0.38566552901
0.389078498294
0.392491467577
0.39590443686
0.399317406143
0.402730375427
0.40614334471
0.409556313993
0.412969283276
0.41638225256
0.419795221843
0.423208191126
0.42662116041
0.430034129693
0.433447098976
0.436860068259
0.440273037543
0.443686006826
0.447098976109
0.450511945392
0.453924914676
0.457337883959
0.460750853242
0.464163822526
0.467576791809
0.470989761092
0.474402730375
0.477815699659
0.481228668942
0.484641638225
0.488054607509
0.491467576792
0.494880546075
0.498293515358
0.501706484642
0.505119453925
0.508532423208
0.511945392491
0.515358361775
0.518771331058
0.522184300341
0.525597269625
0.529010238908
0.532423208191
0.535836177474
0.539249146758
0.542662116041
0.546075085324
0.549488054608
0.552901023891
0.556313993174
0.559726962457
0.563139931741
0.566552901024
0.569965870307
0.57337883959
0.576791808874
0.580204778157
0.58361774744
0.587030716724
0.590443686007
0.59385665529
0.597269624573
0.600682593857
0.60409556314
0.607508532423
0.610921501706
0.61433447099
0.617747440273
0.621160409556
0.62457337884
0.627986348123
0.631399317406
0.634812286689
0.638225255973
0.641638225256
0.645051194539
0.648464163823
0.651877133106
0.655290102389
0.658703071672
0.662116040956
0.665529010239
0.668941979522
0.672354948805
0.675767918089
0.679180887372
0.682593856655
0.686006825939
0.689419795222
0.692832764505
0.696245733788
0.699658703072
0.703071672355
0.706484641638
0.709897610922
0.713310580205
0.716723549488
0.720136518771
0.723549488055
0.726962457338
0.730375426621
0.733788395904
0.737201365188
0.740614334471
0.744027303754
0.747440273038
0.750853242321
0.754266211604
0.757679180887
0.761092150171
0.764505119454
0.767918088737
0.77133105802
0.774744027304
0.778156996587
0.78156996587
0.784982935154
0.788395904437
0.79180887372
0.795221843003
0.798634812287
0.80204778157
0.805460750853
0.808873720137
0.81228668942
0.815699658703
0.819112627986
0.82252559727
0.825938566553
0.829351535836
0.832764505119
0.836177474403
0.839590443686
0.843003412969
0.846416382253
0.849829351536
0.853242320819
0.856655290102
0.860068259386
0.863481228669
0.866894197952
0.870307167235
0.873720136519
0.877133105802
0.880546075085
0.883959044369
0.887372013652
0.890784982935
0.894197952218
0.897610921502
0.901023890785
0.904436860068
0.907849829352
0.911262798635
0.914675767918
0.918088737201
0.921501706485
0.924914675768
0.928327645051
0.931740614334
0.935153583618
0.938566552901
0.941979522184
0.945392491468
0.948805460751
0.952218430034
0.955631399317
0.959044368601
0.962457337884
0.965870307167
0.969283276451
0.972696245734
0.976109215017
0.9795221843
0.982935153584
0.986348122867
0.98976109215
0.993174061433
0.996587030717

In [ ]:
roi = testNeuroGraph._vis.copy()
for orth in synapseROIs:
    for y, x in orth:
        roi[y][x] = 255
plt.imshow(roi, cmap='gray')
plt.show()

In [9]:
def generateTube(x0, x1, y0, y1, thickness, mat):
    x = np.linspace(x0, x1, int(np.sqrt(np.abs((x1 - x0)**2 + (y1 - y0)**2))))
    y = np.linspace(y0, y1, int(np.sqrt(np.abs((x1 - x0)**2 + (y1 - y0)**2))))
    coords = zip(x, y)
    
    perpSlope = None
    if y1-y0 == 0:
        perpSlope = 1000
    elif x1-x0 == 0:
        perpSlope == .0001
    else:
        perpSlope = -1.0*(x1 - x0)/(y1 - y0)

    numSamplePoints = thickness/2
    netCoords = []
    for coord in coords:
        xtop = coord[0] + numSamplePoints/np.sqrt(1 + perpSlope*perpSlope)
        xbot = coord[0] - numSamplePoints/np.sqrt(1 + perpSlope*perpSlope)
        ytop = coord[1] + perpSlope*numSamplePoints/np.sqrt(1 + perpSlope*perpSlope)
        ybot = coord[1] - perpSlope*numSamplePoints/np.sqrt(1 + perpSlope*perpSlope)
        xperp = np.linspace(xtop, xbot, int(np.sqrt(np.abs((xtop - xbot)**2 + (ytop - ybot)**2))))
        yperp = np.linspace(ytop, ybot, int(np.sqrt(np.abs((xtop - xbot)**2 + (ytop - ybot)**2))))
        coordsPerp = zip(xperp, yperp)
        finalCoordsPerp = []
        for coordPerp in coordsPerp:
            if coordPerp[0] > 0 and coordPerp[1] > 0 and coordPerp[0] < mat.shape[0] - 1 and coordPerp[1] < mat.shape[1] - 1:
                finalCoordsPerp.append(((int(math.floor(coordPerp[1]))), (int(math.floor(coordPerp[0])))))
        netCoords.append(finalCoordsPerp)
    return netCoords

In [10]:
def waveletSynapseROIs(neuroGraph):
    boostedImg, _ = ngl.boostDendrites(data_1[13:17], percentile=50)
    adaptiveImg = ngl.adaptiveThreshold(boostedImg, 32, 32)[2]
    plt.imshow(adaptiveImg, cmap='gray')
    plt.show()
    synapseROIs = []
    #for i in range(0, 1):
    for i in range(0, len(testNeuroGraph._edges)):
        print i/float(len(testNeuroGraph._edges))
        try:
            
            y, x = testNeuroGraph._edges[i]

            curROI = np.array(generateTube(x[0], x[-1], y[0], y[-1], 30, adaptiveImg))

            ROIDist = []
            ROIDistLedger = []
            for orthagonal in curROI:
                orthagonalDist = []
                orthagonalMaxLedger = []
                for elem in orthagonal:
                    #since will returns xy
                    orthagonalDist.append(adaptiveImg[elem[0]][elem[1]])
                    orthagonalMaxLedger.append((elem[0], elem[1]))
                ROIDist.append(np.max(orthagonalDist))
                ROIDistLedger.append(orthagonalMaxLedger[np.argmax(orthagonalDist)])

            valid = np.array(np.where(zscore(ROIDist) > 1)[0])


            '''
            valid = argrelmax(np.array(ROIDist))[0]
            if len(valid) > 0:
                toAdd = np.concatenate([elem for elem in curROI[valid]])
                synapseROIs.append(toAdd)


            if len(valid) > 0:
                toAdd = np.concatenate([elem for elem in curROI[valid]])
                synapseROIs.append(toAdd) 
            '''

            if len(valid) > 0:
                for idx in valid:
                    synapseROIs.append(ROIDistLedger[idx])
            else:
                print 'no maxima found!'

        except:
            print sys.exc_info()[0]

    return synapseROIs

In [11]:
synapseROIs = waveletSynapseROIs(testNeuroGraph)


0.0
0.00296735905045
0.00593471810089
0.00890207715134
0.0118694362018
0.0148367952522
0.0178041543027
0.0207715133531
0.0237388724036
0.026706231454
0.0296735905045
0.0326409495549
0.0356083086053
0.0385756676558
0.0415430267062
0.0445103857567
0.0474777448071
0.0504451038576
0.053412462908
0.0563798219585
0.0593471810089
0.0623145400593
0.0652818991098
0.0682492581602
0.0712166172107
0.0741839762611
0.0771513353116
0.080118694362
0.0830860534125
0.0860534124629
0.0890207715134
0.0919881305638
no maxima found!
0.0949554896142
0.0979228486647
0.100890207715
0.103857566766
0.106824925816
<type 'exceptions.TypeError'>
0.109792284866
0.112759643917
0.115727002967
0.118694362018
0.121661721068
0.124629080119
0.127596439169
0.13056379822
0.13353115727
0.13649851632
0.139465875371
0.142433234421
0.145400593472
0.148367952522
0.151335311573
0.154302670623
0.157270029674
0.160237388724
no maxima found!
0.163204747774
0.166172106825
0.169139465875
0.172106824926
<type 'exceptions.TypeError'>
0.175074183976
0.178041543027
0.181008902077
0.183976261128
0.186943620178
0.189910979228
0.192878338279
0.195845697329
0.19881305638
0.20178041543
0.204747774481
0.207715133531
0.210682492582
0.213649851632
0.216617210682
0.219584569733
0.222551928783
0.225519287834
0.228486646884
0.231454005935
0.234421364985
0.237388724036
0.240356083086
0.243323442136
0.246290801187
0.249258160237
0.252225519288
0.255192878338
0.258160237389
0.261127596439
0.26409495549
0.26706231454
<type 'exceptions.TypeError'>
0.270029673591
0.272997032641
0.275964391691
0.278931750742
0.281899109792
0.284866468843
0.287833827893
0.290801186944
0.293768545994
0.296735905045
0.299703264095
0.302670623145
0.305637982196
0.308605341246
0.311572700297
0.314540059347
0.317507418398
0.320474777448
0.323442136499
0.326409495549
0.329376854599
0.33234421365
0.3353115727
0.338278931751
0.341246290801
0.344213649852
0.347181008902
0.350148367953
0.353115727003
0.356083086053
0.359050445104
0.362017804154
0.364985163205
0.367952522255
0.370919881306
0.373887240356
0.376854599407
0.379821958457
0.382789317507
0.385756676558
0.388724035608
0.391691394659
0.394658753709
0.39762611276
0.40059347181
0.403560830861
0.406528189911
0.409495548961
0.412462908012
0.415430267062
0.418397626113
0.421364985163
0.424332344214
0.427299703264
0.430267062315
0.433234421365
0.436201780415
0.439169139466
0.442136498516
0.445103857567
0.448071216617
0.451038575668
0.454005934718
0.456973293769
0.459940652819
0.462908011869
0.46587537092
0.46884272997
0.471810089021
0.474777448071
0.477744807122
0.480712166172
0.483679525223
0.486646884273
0.489614243323
0.492581602374
0.495548961424
0.498516320475
0.501483679525
no maxima found!
0.504451038576
0.507418397626
0.510385756677
0.513353115727
0.516320474777
<type 'exceptions.TypeError'>
0.519287833828
0.522255192878
0.525222551929
0.528189910979
0.53115727003
0.53412462908
0.537091988131
0.540059347181
0.543026706231
0.545994065282
0.548961424332
0.551928783383
0.554896142433
0.557863501484
0.560830860534
no maxima found!
0.563798219585
0.566765578635
0.569732937685
0.572700296736
0.575667655786
no maxima found!
0.578635014837
0.581602373887
0.584569732938
0.587537091988
0.590504451039
0.593471810089
0.596439169139
0.59940652819
0.60237388724
0.605341246291
0.608308605341
0.611275964392
0.614243323442
0.617210682493
0.620178041543
0.623145400593
0.626112759644
0.629080118694
0.632047477745
0.635014836795
0.637982195846
0.640949554896
0.643916913947
/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:70: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:82: RuntimeWarning: Degrees of freedom <= 0 for slice
  warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning)
/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:94: RuntimeWarning: invalid value encountered in true_divide
  arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:116: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
0.646884272997
no maxima found!
0.649851632047
0.652818991098
0.655786350148
0.658753709199
0.661721068249
0.6646884273
0.66765578635
0.670623145401
0.673590504451
0.676557863501
0.679525222552
0.682492581602
0.685459940653
0.688427299703
0.691394658754
0.694362017804
0.697329376855
0.700296735905
0.703264094955
0.706231454006
0.709198813056
0.712166172107
0.715133531157
0.718100890208
0.721068249258
0.724035608309
0.727002967359
0.729970326409
0.73293768546
0.73590504451
0.738872403561
0.741839762611
0.744807121662
0.747774480712
0.750741839763
0.753709198813
0.756676557864
0.759643916914
0.762611275964
0.765578635015
0.768545994065
0.771513353116
0.774480712166
0.777448071217
0.780415430267
0.783382789318
0.786350148368
0.789317507418
no maxima found!
0.792284866469
0.795252225519
0.79821958457
0.80118694362
0.804154302671
0.807121661721
0.810089020772
0.813056379822
0.816023738872
0.818991097923
0.821958456973
0.824925816024
0.827893175074
0.830860534125
0.833827893175
0.836795252226
0.839762611276
0.842729970326
0.845697329377
0.848664688427
0.851632047478
0.854599406528
0.857566765579
0.860534124629
0.86350148368
0.86646884273
0.86943620178
0.872403560831
0.875370919881
0.878338278932
0.881305637982
0.884272997033
0.887240356083
0.890207715134
0.893175074184
0.896142433234
0.899109792285
0.902077151335
0.905044510386
0.908011869436
0.910979228487
0.913946587537
0.916913946588
0.919881305638
0.922848664688
0.925816023739
0.928783382789
0.93175074184
0.93471810089
0.937685459941
0.940652818991
0.943620178042
0.946587537092
0.949554896142
0.952522255193
0.955489614243
0.958456973294
0.961424332344
0.964391691395
0.967359050445
0.970326409496
0.973293768546
0.976261127596
0.979228486647
0.982195845697
0.985163204748
0.988130563798
0.991097922849
0.994065281899
0.99703264095

In [12]:
roi = testNeuroGraph._vis.copy()
#for orth in synapseROIs:
for i in range(1):
    for y, x in synapseROIs: # was orth
        roi[y][x] = 9001
plt.imshow(roi,  cmap='gray')
plt.show()



In [17]:
data_1_b, _ = ngl.boostDendrites(data_1)
print data_1_b.shape
zoom = data_1_b[15, 600:1000, 300:700]
plt.imshow(zoom, cmap='gray')
plt.show()


roi = np.zeros_like(data_1[15].copy())
#for orth in synapseROIs:
for i in range(1):
    for y, x in synapseROIs: # was orth
        roi[y][x] = 300
        
kernel_a = np.ones((8, 8))
kernel_b = np.ones((3, 3))
roi_c = np.add(convolve(roi, kernel_a), convolve(roi, kernel_b)) 
plt.imshow(roi_c[100:400, 500:900], cmap='gray')
#plt.imshow(roi, cmap='gray')
plt.show()


(60, 1024, 1024)

In [69]:
dat = np.extract(roi_c[600:1000, 300:700] > 0, roi_c)
plt.hist(dat, bins=50)
plt.show()



In [ ]: