In [1]:
%matplotlib inline
In [2]:
from numpy import array
import matplotlib.pyplot as plt
import seaborn as sns
In [3]:
from showit import image, tile
import matplotlib.animation as animation
In [4]:
import thunder
In [5]:
import json
In [6]:
from os.path import join, exists
from os import mkdir, makedirs
In [7]:
directory = '/tier2/freeman/Nick/mVR/sessions'
In [8]:
key = '000133'
prefix = 'trial'
path = directory + '/' + key
print path
print exists(path)
In [9]:
savepath = path + '/movies'
if not exists(savepath):
makedirs(savepath)
In [52]:
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=40000)
In [11]:
data = thunder.images.frombinary(path + '/images', engine=sc)
In [12]:
summarypath = path + '/summary'
with open(summarypath + '/meta.json') as infile:
meta = json.load(infile)
In [13]:
meta
Out[13]:
In [14]:
pixelsPerUmOrig = meta['rois'][0]['npixels'][0]/meta['rois'][0]['size'][0]/1000
aspect = meta['rois'][0]['size'][0]/meta['rois'][0]['size'][1]
volumeRate = meta['volumeRate']
scaleBar = 100*round(meta['rois'][0]['size'][0]*1.8)
In [53]:
fSpace = 1
dsTime = 1
dsSpace = 1
frameRate = volumeRate/dsTime
pixelsPerUm = pixelsPerUmOrig/dsSpace
In [54]:
from skimage.restoration import denoise_bilateral
from skimage import img_as_uint
In [55]:
def bilateral_filter_2D(img):
return map(lambda x: img_as_uint(denoise_bilateral(x, win_size=fSpace, sigma_range=0.1, sigma_spatial=50)), img)
In [56]:
if fSpace > 1:
#filtered = data.median_filter((1, fSpace, fSpace))
filtered = data.clip(min=0).map(bilateral_filter_2D)
else:
filtered = data
In [57]:
if dsSpace > 1:
filtered = filtered.subsample((1, dsSpace, dsSpace))
In [58]:
from numpy import convolve, ones
In [59]:
def smooth(s, dsTime):
r = convolve(s, ones(dsTime)/dsTime, 'same').astype('int16')
return r[::dsTime]
In [60]:
if dsTime > 1:
smoothed = filtered.map_as_series(lambda x: smooth(x, dsTime))
else:
smoothed = filtered
In [61]:
result = smoothed.toarray()
In [319]:
from numpy import percentile
In [320]:
def dff(F):
#F0 = F.mean()
F0 = percentile(F,20)
offset = 20
return (F - F0)/(F0+offset)
In [321]:
normed = smoothed.map_as_series(dff)
#normedF = normed.clip(min=0).map(lambda x: denoise_bilateral(x, win_size=fSpace, sigma_range=0.1, sigma_spatial=20))
In [322]:
result = normed.toarray()
In [62]:
plane = 0
if len(result.shape) == 3:
plane = 0
movie = result
else:
movie = result[:,plane,:,:]
means = movie.mean()
label = 'movie'
clim = means #3.0/4 #means
In [63]:
frame = movie.shape[0]/2
img = movie[frame,:,:]
fig = plt.figure(figsize=[12, 12.0*img.shape[0]/img.shape[1]])
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
ax = plt.gca()
im = image(img, clim=(0, clim*8), ax=ax)
time = ax.text(.97*img.shape[1], .04*img.shape[0], '%.1f s' % (frame/frameRate), color='white', fontsize=22, ha='right', fontdict={'family': 'monospace'});
ax.plot([.04*img.shape[1], .04*img.shape[1]+scaleBar*pixelsPerUm], [.94*img.shape[0], .94*img.shape[0]], 'w');
sclae = ax.text(.04*img.shape[1]+scaleBar*pixelsPerUm/2, .97*img.shape[0], '%d um' % scaleBar, color='white', fontsize=22, ha='center', fontdict={'family': 'monospace'});
plt.xlim([0, img.shape[1]]);
plt.ylim([img.shape[0], 0]);