In [104]:
import os
import numpy as np
import libtiff 
import matplotlib.pyplot as plt
import sys
sys.path.append('/home/sophie/Desktop/stanford_lfanalyze_v0.4')
import lfcalibrate

import Image
from libtiff import TIFFimage
from PIL import Image
import tifffile as tiff

In [20]:
import subprocess

In [8]:
%matplotlib inline

In [9]:
# Functions

In [100]:
def AvIm(path,ID):
    
    flist=os.listdir(path)
    im = Image.open(path+"/"+flist[0])
    tt= np.array(im)
    print(tt.shape)
    S=tt.shape
    M=np.zeros(len(flist))
    AVON=np.zeros(S)
    AVOFF=np.zeros(S)
    
    for i in range(len(os.listdir(path))):
        j=i+1
        im = Image.open(path+'/'+'Data'+ID+'_'+str(j)+'.tif')
        tt = np.array(im)
        M[i]=np.mean(np.mean(tt))
    Mav=np.mean(M)  
    liston=[i for i in range(len(M)) if M[i]>Mav*0.7]
    j=0
    k=0
    
    
    for i in range(len(os.listdir(path))):
        j=i+1
        im = Image.open(path+'/'+'Data'+ID+'_'+str(j)+'.tif')
        tt = np.array(im)
        if i in liston:
            AVOFF=AVOFF+tt
            j=j+1
        else:
            AVON=AVON+tt
            k=k+1       
    
    
    return M

In [109]:
def SaveAvs(path,M,ID):
    
    flist=os.listdir(path)
    im = Image.open(path+"/"+flist[0]) 
    tt= np.array(im)
    S=tt.shape
    M=np.zeros(len(flist))
    AVON=np.zeros(S)
    AVOFF=np.zeros(S)

    outpath="/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID   
    
    Mav=np.mean(M)
    liston=[i for i in range(len(M)) if M[i]>Mav*0.7]
    j=0
    k=0
    
    #for i in range(len(os.listdir(path))):
    for i in range(500):
        l=i+1
        im = Image.open(path+'/'+'Data'+ID+'_'+str(l)+'.tif')
        tt = np.array(im)
        if i in liston:
            AVOFF=AVOFF+tt
            j=j+1
        else:
            AVON=AVON+tt
            k=k+1       
            
    AVOFF=np.uint16(AVOFF/j)
    AVON=np.uint16(AVON/k)


    #data = numpy.zeros((1024,1024),numpy.uint16)
    #h,w = data.shape
    #for i in range(w):
    #    data[:,i] = numpy.arange(h)

    #tiff = TIFFimage(AVOFF, description='')
    #tiff.write_file(outpath+'/'+'Avoff.tif', compression='lzw')
    #del tiff
    
    tiff.imsave(outpath+'/'+'Avoff.tif', AVOFF)
    tiff.imsave(outpath+'/'+'Avon.tif', AVON)    
    #tiff = TIFFimage(AVON, description='')
    #tiff.write_file(outpath+'/'+'Avon.tif', compression='lzw')
    #del tiff

In [55]:
def getpath(ID):
    
    path1="/media/Seagate Backup Plus Drive/Data"+ID
    path2="/media/Seagate Expansion Drive/Data"+ID

    if os.path.exists(path1):
        path=path1
    else:
        path=path2
            
    return path

In [31]:
def makeoutfolder(ID):
    outpath="/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID     
    if os.path.exists(outpath):
        "nope"
    else:
        os.makedirs("/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID)

In [127]:
filename="/home/sophie/Desktop/ListeToAnalyze"
Listfile = open(filename, 'r')
ListToAnalyze = [line.split('\n')[0] for line in Listfile.readlines()]

In [124]:
for ID in ListToAnalyze:
    
    print(ID)
    path=getpath(ID)
    makeoutfolder(ID)
    M=AvIm(path,ID)
    SaveAvs(path,M,ID)
    outpath="/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID  
    bashCommand = "python /home/sophie/Desktop/stanford_lfanalyze_v0.4/lfcalibrate.py "+outpath+'/'+"Avon.tif --dark-frame "+outpath+'/'+"Avoff.tif --pixel-size 6.5 --pitch 125 --focal-length 1250 --magnification 20 --na 1.0 --tubelens-focal-length 180.0 --wavelength 510 --medium-index 1.33 --num-slices 40 --um-per-slice 6.0 --supersample 1 -o "+outpath+'/'+ID+"_ss1_f1250_6microns.lfc"
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    output = process.communicate()[0]


100125
(2048, 2048)
100121
(2048, 2048)
100111
(1096, 2048)
100112
(512, 2048)
100115
(1024, 2048)
100106
(2048, 2048)
100104
(1024, 2048)
100110
(1024, 2048)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-124-46c4f5408981> in <module>()
      9     bashCommand = "python /home/sophie/Desktop/stanford_lfanalyze_v0.4/lfcalibrate.py "+outpath+'/'+"Avon.tif --dark-frame "+outpath+'/'+"Avoff.tif --pixel-size 6.5 --pitch 125 --focal-length 1250 --magnification 20 --na 1.0 --tubelens-focal-length 180.0 --wavelength 510 --medium-index 1.33 --num-slices 40 --um-per-slice 6.0 --supersample 1 -o "+outpath+'/'+ID+"_ss1_f1250_6microns.lfc"
     10     process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
---> 11     output = process.communicate()[0]
     12 

/usr/lib/python2.7/subprocess.pyc in communicate(self, input)
    744                 self.stdin.close()
    745             elif self.stdout:
--> 746                 stdout = _eintr_retry_call(self.stdout.read)
    747                 self.stdout.close()
    748             elif self.stderr:

/usr/lib/python2.7/subprocess.pyc in _eintr_retry_call(func, *args)
    476     while True:
    477         try:
--> 478             return func(*args)
    479         except (OSError, IOError) as e:
    480             if e.errno == errno.EINTR:

KeyboardInterrupt: 

In [125]:
ID='100110'
outpath="/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID  
bashCommand = "python /home/sophie/Desktop/stanford_lfanalyze_v0.4/lfcalibrate.py "+outpath+'/'+"Avon.tif --dark-frame "+outpath+'/'+"Avoff.tif --pixel-size 6.5 --pitch 125 --focal-length 1250 --magnification 20 --na 1.0 --tubelens-focal-length 180.0 --wavelength 510 --medium-index 1.33 --num-slices 40 --um-per-slice 6.0 --supersample 1 -o "+outpath+'/'+ID+"_ss1_f1250_6microns.lfc"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]

In [165]:
for ID in ListToAnalyze:
    #path=getpath(ID)
    #pathfile=path+'/'+"Data"+ID+"_1500.tif" 
    #pathfile=str.replace(pathfile," ","\ ")
    #print(pathfile)
    outpath="/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID+'/'
    #os.system('mv "%s" "%s"' % (pathfile,outpath) )
    bashCommand = "/usr/bin/python2.7 /home/sophie/Desktop/stanford_lfanalyze_v0.4/lfdeconvolve.py "+outpath+"Data"+ID+"_1500.tif -c "+outpath+ID+"_ss1_f1250_6microns.lfc -o "+outpath+ID+"ss1.tif"
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    output = process.communicate()[0]
outpath="/home/sophie/Desktop/stanford_lfanalyze_v0.4/examples/"+ID

In [110]:
SaveAvs(path,M,ID)

In [ ]:
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]

In [ ]:


In [ ]:


In [ ]: