Pad names


In [1]:
import os

In [2]:
import numpy as np

In [3]:
path='/home/sophie/Desktop/10007ss2/'

In [4]:
for fn in os.listdir(path):
    fn2=fn.split('-')
    fnnum=fn2[1].split('_')
    i=int(fnnum[0])
    fnnew=fn2[0]+'-'+ str(i).zfill(5) + '_'+fnnum[1]
    os.rename(path+"/"+fn, path+"/"+fnnew)


---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-4-65d60163e092> in <module>()
----> 1 for fn in os.listdir(path):
      2     fn2=fn.split('-')
      3     fnnum=fn2[1].split('_')
      4     i=int(fnnum[0])
      5     fnnew=fn2[0]+'-'+ str(i).zfill(5) + '_'+fnnum[1]

OSError: [Errno 2] No such file or directory: '/home/sophie/Desktop/10007ss2/'

Open the images


In [4]:
data = tsc.loadImages(path, inputFormat='tif')

In [5]:
data.dims


Out[5]:
Dimensions(values=[(0, 0, 0), (159, 209, 41)], n=3)

In [6]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('notebook')
from thunder import Colorize
image = Colorize.image

Crop

Open an example image in the middle of the time series


In [7]:
N = int(data.nrecords/2)

In [8]:
N


Out[8]:
6181

In [9]:
imgN = tsc.loadImages(path, inputFormat='tif', startIdx=N, stopIdx=N+1)

In [10]:
img=thunder.Images.collectValuesAsArray(imgN)

In [11]:
img.shape


Out[11]:
(1, 52, 105, 40)

In [13]:
image(img[0,:,:,20])


Click on the example image to extract cropping window


In [14]:
import Tkinter
import PIL
from PIL import Image, ImageTk, ImageOps
from sys import argv
import numpy as np

In [15]:
window = Tkinter.Tk(className="bla")

In [16]:
img2=np.mean(img,3)

In [17]:
img2=np.squeeze(img2*255/np.max(img2))

In [18]:
img2.shape


Out[18]:
(212, 210)

In [19]:
imgim=Image.fromarray(img2)

In [20]:
print imgim.format, imgim.size, imgim.mode


None (210, 212) F

In [21]:
imgim=imgim.convert('L')

In [22]:
imgim=ImageOps.autocontrast(imgim)

In [23]:
canvas = Tkinter.Canvas(window, width=img2.shape[1], height=img2.shape[0])

In [24]:
canvas.pack()

In [25]:
image_tk = ImageTk.PhotoImage(imgim)

In [26]:
canvas.create_image(img2.shape[1]/2, img2.shape[0]/2, image=image_tk)


Out[26]:
1

In [27]:
Pcrop=[0,0,0,0]

In [28]:
def callback(event):
    global Pcrop
    if (Pcrop[1]==0):
        Pcrop[0] = event.x
        Pcrop[1] = event.y
    else:
        Pcrop[2] = event.x
        Pcrop[3] = event.y
        
    print "clicked at: ", event.x, event.y

    
def quit(root):
    root.destroy()

root = Tkinter.Tk()

canvas.bind("<Button-1>", callback)
Tkinter.Button(root, text="Q", command=lambda root=root:quit(root)).pack()
    

root.mainloop()


clicked at:  16 51
clicked at:  161 153

In [207]:
Pcrop


Out[207]:
[16, 51, 161, 153]

Crop Images


In [238]:
S=img.shape

In [239]:
S


Out[239]:
(1, 212, 210, 40)

In [8]:
datac=data.crop((Pcrop[1],Pcrop[0],0),(Pcrop[3],Pcrop[2],S[3]))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-a84a34ae5cf0> in <module>()
----> 1 datac=data.crop((Pcrop[1],Pcrop[0],0),(Pcrop[3],Pcrop[2],S[3]))

NameError: name 'Pcrop' is not defined

In [241]:
datac.dims


Out[241]:
Dimensions(values=[(0, 0, 0), (101, 144, 39)], n=3)

In [242]:
mg = datac[N]

In [243]:
mg.shape


Out[243]:
(102, 145, 40)

In [244]:
from thunder import Colorize

image = Colorize.image

In [245]:
image(mg[:,:,20])


Keep only the frames for which the excitation is on


In [7]:
D=thunder.Images.collectValuesAsArray(data)

In [8]:
S=D.shape

In [9]:
S


Out[9]:
(836, 160, 210, 42)

In [10]:
M=np.mean(np.mean(np.mean(D,1),1),1)

In [11]:
plt.plot(np.squeeze(M))
plt.show()



In [12]:
Mav=M.mean()

In [13]:
liston=[i for i in range(len(M)) if M[i]>Mav*0.7]

In [14]:
liston2=[liston[i] for i in range(1,(len(liston)-1))]

In [15]:
len(liston)


Out[15]:
811

Save as .nii for analysis with AFNI


In [16]:
from nifti import NiftiImage

In [17]:
D2=D[liston2[:],:,:,:]

In [18]:
D2.shape


Out[18]:
(809, 160, 210, 42)

In [19]:
D3=np.transpose(D2,(0,3,1,2))

In [20]:
nim = NiftiImage(D3)

In [21]:
D3.shape


Out[21]:
(809, 42, 160, 210)

In [22]:
print nim.header['dim']


[4, 210, 160, 42, 809, 1, 1, 1]

In [23]:
nim.save('/home/sophie/Desktop/10007ss2.nii.gz')


/usr/lib/pymodules/python2.7/nifti/image.py:231: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  return (not self._data == None)

In [ ]: