Setup


In [2]:
%matplotlib inline

In [3]:
from numpy import array
import matplotlib.pyplot as plt
import seaborn as sns

In [4]:
from showit import image, tile
import matplotlib.animation as animation

In [5]:
from os.path import join, exists
from os import mkdir, makedirs

In [6]:
#from mesoscope.utils import normalize

In [7]:
import thunder

In [8]:
from registration import CrossCorr

Set directory and session information


In [9]:
directory = '/tier2/freeman/Nick/sessions'

In [10]:
key = '000222'
prefix = 'trial'
path = directory + '/' + key
print path
print exists(path)


/tier2/freeman/Nick/sessions/000222
True

Load data


In [11]:
data = thunder.images.fromtif(path + '/images', engine=None)

In [12]:
data


Out[12]:
Images
mode: local
dtype: uint8
shape: (202, 2100, 2100)

Register data


In [1]:
from mesoscope import reference

In [13]:
ref = reference(data)

In [14]:
fig = plt.figure(figsize=[10,10])
ax = plt.axes()
im = image(ref, clim=(0, 3.5*ref.mean()), ax=ax)



In [15]:
ref.shape


Out[15]:
(2100, 2100)

In [ ]:


In [16]:
algorithm = CrossCorr(axis=0)

In [17]:
model = algorithm.fit(data, reference)

In [18]:
registered = model.transform(data)

In [19]:
registered


Out[19]:
Images
mode: spark
dtype: int16
shape: (8585, 4, 512, 512)

Inspect shifts


In [20]:
shifts = model.toarray()

In [21]:
shifts.shape


Out[21]:
(8585, 4, 2)

In [22]:
[plt.plot(model.toarray()[:,i]) for i in range(shifts.shape[1])];
plt.xlim([0, 1000])


Out[22]:
(0, 1000)

Inspect data


In [23]:
mean = registered.mean().toarray()

In [24]:
mean.shape


Out[24]:
(4, 512, 512)

In [25]:
img = mean[0]
fig = plt.figure(figsize=[10,10])
ax = plt.axes()
im = image(img, clim=(0, 3.5*img.mean()), ax=ax)


Save registered binary data


In [26]:
if not exists(path + '/registered'):
    makedirs(path + '/registered')
registered.tobinary(path + '/registered', overwrite=True)

Save summary data


In [27]:
if not exists(path + '/summary'):
    makedirs(path + '/summary')

Raw tiffs


In [28]:
from skimage.io import imsave
imsave(path + '/summary/registered.tif', mean.astype('float32'), plugin='tifffile', photometric='minisblack')
imsave(path + '/summary/reference.tif', reference.astype('float32'), plugin='tifffile', photometric='minisblack')

Normalized tiffs


In [29]:
imsave(path + '/summary/registered-norm.tif', normalize(mean).astype('float32'), plugin='tifffile', photometric='minisblack')
imsave(path + '/summary/reference-norm.tif', normalize(reference).astype('float32'), plugin='tifffile', photometric='minisblack')