Assignment 12

1) Finalize figures(Bijan


In [7]:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
%matplotlib inline
import urllib2
import numpy as np

np.set_printoptions(precision=3, suppress=True)
url = ('https://raw.githubusercontent.com/Upward-Spiral-Science'
       '/data/master/syn-density/output.csv')
data = urllib2.urlopen(url)
csv = np.genfromtxt(data, delimiter=",")[1:]

def check_condition(row):
    if row[3] == 0:
        return False
    return True

a = np.apply_along_axis(check_condition, 1, csv)
a = np.where(a == True)[0]
nonZeroMask = csv[a, :]

synDividedMask = np.divide(nonZeroMask[:,4],nonZeroMask[:,3])
synDividedMask = synDividedMask * (64**3)
accurateDataT = np.vstack((nonZeroMask[:,0],nonZeroMask[:,1],nonZeroMask[:,2],synDividedMask))
accurateData = accurateDataT.T

cleaned = accurateData[accurateData[:,0] >= 409]
cleaned = cleaned[cleaned[:,0] <= 3529]
cleaned = cleaned[cleaned[:,1] >= 1564]
cleaned = cleaned[cleaned[:,1] <= 3124]

length, width = cleaned.shape
print length
print width


36036
4

Make a higher definition graph


In [15]:
import math

divisionsx = np.unique(cleaned[:,0])
meanx = np.zeros((2,len(divisionsx)))

divisionsy = np.unique(cleaned[:,1])
meany = np.zeros((2,len(divisionsy)))

divisionsz = np.unique(cleaned[:,2])
meanz = np.zeros((2,len(divisionsz)))

maxlen = np.amax([len(divisionsx),len(divisionsy),len(divisionsz)])
xstep = np.divide(maxlen,len(divisionsx))
ystep = 2
zstep = 8

counter = 0
for d in divisionsx:
    slicex = cleaned[cleaned[:,0] == d]
    meanx[0,counter] = (counter)*xstep
    meanx[1,counter] = np.mean(slicex[:,3])
    counter += 1
    
counter = 0
for d in divisionsy:
    slicey = cleaned[cleaned[:,1] == d]
    meany[0,counter] = (counter)*ystep
    meany[1,counter] = np.mean(slicey[:,3])
    counter += 1
    
counter = 0
for d in divisionsz:
    slicez = cleaned[cleaned[:,2] == d]
    meanz[0,counter] = (counter)*zstep
    meanz[1,counter] = np.mean(slicez[:,3])
    counter += 1
    
#plot it
allmean = [301] * maxlen
fig,ax = plt.subplots()
ax.set_ylim([150,375])
ax.set_title('Average Synaptic Density Values along Each Axis')
ax.set_yticks([150,225,300,375])
ax.set_xticks([0,20,40,60,80])
ax.set_xlabel('x,y,z coordinate standardized to 0')
ax.set_ylabel('synaptic density')
ax.plot(allmean,label='whole data set',color='black')
ax.plot(meanx[0,:],meanx[1,:],label='x',color='blue')
ax.plot(meany[0,:],meany[1,:],label='y',color='red')
ax.plot(meanz[0,:],meanz[1,:],label='z',color='magenta')
legend = ax.legend(loc='lower left')


It seems clear that the data changes across y. Let's look at the magnitude of these changes.


In [3]:
mean = meany[1,:]
mean1 = mean[:-1]
mean2 = mean[1:]
meandiff = mean2 - mean1
print meandiff
fig,ax = plt.subplots()
ax.plot(meandiff)
ax.plot([0] * len(meandiff))
ax.set_xlabel('y slice')
ax.set_ylabel('change in syn/unmasked')
ax.set_xticks(np.arange(0,40,10))
ax.set_yticks(np.arange(-30,15,10))


[ -1.151  11.256   5.299  -0.731  -4.266 -12.13   -8.529   6.754   3.87
  -6.869  -6.101 -11.096   0.626   4.467   2.237   7.492  -8.962 -12.884
  -7.842   4.103   7.88   -6.088  -4.872  -4.008 -10.934   3.753  -3.197
  -8.365  -1.651   4.076  -8.661  -7.858  -7.266  -7.974  -6.467  -9.197
 -25.165  -3.464 -20.893 -14.473]
Out[3]:
[<matplotlib.axis.YTick at 0x7b3bba8>,
 <matplotlib.axis.YTick at 0x7964eb8>,
 <matplotlib.axis.YTick at 0x75b9dd8>,
 <matplotlib.axis.YTick at 0x75c5320>,
 <matplotlib.axis.YTick at 0x75c58d0>]

Next week I want to see how I can clean this, maybe isolate cortical layer boundaries from it.

Next is generating a .gif of the data points at increasing levels of syn/unmasked


In [4]:
cleanedSyn = cleaned[cleaned[:,3].argsort()]
print cleanedSyn
uniqueSyn = np.unique(cleaned[:,3])
print len(uniqueSyn)


[[ 2593.     3085.       55.        0.   ]
 [ 1813.     3046.      166.        0.   ]
 [ 2125.     3046.      388.        0.   ]
 ..., 
 [ 2632.     2188.      721.      818.721]
 [ 1774.     2227.      721.      880.442]
 [ 2749.     1876.     1054.      886.1  ]]
28421

In [11]:
import Figtodat
from images2gif import writeGif
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

figure = plt.figure()
plot = figure.add_subplot(111, projection='3d')
plot.hold(False)
plot.view_init(elev=50, azim=20)
plot.set_xlabel('x axis')
plot.set_xticks(np.arange(0,4000,1000))
plot.set_ylabel('y axis')
plot.set_yticks(np.arange(1000,3500,1000))
plot.set_zlabel('z axis')
plot.set_zticks(np.arange(0,1500,500))
plot.set_title('Time points = increasing values of syn/unmasked')
images=[]
imageSteps = np.arange(0,49)
stepSize = len(cleaned)/len(imageSteps)
for i in imageSteps[:-1]:
    currImage = cleanedSyn[(stepSize*i):(stepSize*(i+1)),:]
    plot.scatter(currImage[:,0],currImage[:,1],currImage[:,2])  
    plot.hold(True)
    plot.scatter(4000,np.mean(currImage[:,1]),s = 10,c = 'r')
    plot.hold(False)
    im = Figtodat.fig2img(figure)
    images.append(im)
 
writeGif("Bijan_gif_dirty.gif",images,duration=0.1,dither=0)


Next week I plan to clean up this gif, as well as try to find clear cutoffs (# of near neighbors, changing step size for y)

I also plan to try to reduce noise of my ydiffs graph.


In [ ]: