Skip to content

Instantly share code, notes, and snippets.

@miura
Last active December 20, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miura/6166331 to your computer and use it in GitHub Desktop.
Save miura/6166331 to your computer and use it in GitHub Desktop.
a script for group1 2_EVA data. (MultipleFileTracking.py)
from ij import IJ, Prefs
from ij.gui import Roi
from ij.io import DirectoryChooser
from ij.plugin.frame import ThresholdAdjuster
from ij.process import ImageProcessor
from ij.plugin.filter import BackgroundSubtracter
from fiji.threshold import Auto_Threshold
from loci.plugins import BF
from pta import PTA
from pta.gui import ShowPdata
from pta.track import DetectParticle
from pta.data import PtaParam
from java.util import ArrayList
import os, csv
def CSVwrite(srcpath, msddata):
parentdirectory = os.path.dirname(srcpath)
filename = os.path.basename(srcpath)
basename = os.path.splitext(filename)[0]
for msd in msddata:
thisid = msd.getID()
coordsPath = os.path.join(parentdirectory, basename + "_" + str(int(thisid)) +"_coords.csv")
msdPath = os.path.join(parentdirectory, basename + "_" + str(int(thisid)) + "_msd.csv")
print coordsPath
print msdPath
f1 = open(coordsPath, 'wb')
writer = csv.writer(f1)
track = msd.getTrack()
for node in track:
writer.writerow([node.getCx(), node.getCy(), node.getParam()[1],node.getParam()[2]])
f1.close()
f2 = open(msdPath, 'wb')
writer = csv.writer(f2)
dtA = msd.getFullDFrames()
msdA = msd.getFullMSD()
for i in range(len(dtA)):
writer.writerow([dtA[i], msdA[i]])
f2.close()
def core(datafilepath):
# load data as an ImagePlus object
#imp = IJ.openImage(datafilepath)
imp = BF.openImagePlus(datafilepath)[0]
# default ROI = full frame
scanAreaRoi = Roi(0,0,imp.getWidth(),imp.getHeight())
IJ.run(imp,"Subtract Background...", "rolling=10 stack")
#bs = BackgroundSubtracter()
#for frame in range(imp.getStackSize()):
# ip = imp.getStack().getProcessor(frame + 1)
# bs.rollingBallBackground(ip, 10.0, False, False, False, False, False)
ip = imp.getProcessor()
#ht = ip.getAutoThreshold()
Prefs.blackBackground = True
iphist = imp.getProcessor().getHistogram()
#lt = Auto_Threshold.Intermodes(iphist)
#lt = Auto_Threshold.Triangle(iphist)
lt = Auto_Threshold.Moments(iphist)
ip.setThreshold(lt, ip.getMax(), ImageProcessor.RED_LUT)
#ip.setThreshold(0, 111, ImageProcessor.RED_LUT)
PTA.setDetectionState(True)
#PTA.setDebugMode()
# set no GUI mode
PTA.setNoGUI(True)
# set Detection Parameters
ptap = PtaParam.Builder(16,16, False).build()
### paramter customizations
#ptap.setDo2dGaussfit(False)
ptap.setDo2dGaussfit(True)
#ptap.setMinIntensity(100.0)
ptap.setSearchPointIncrement(2)
# instantiate a DetectParticle object
dp = DetectParticle(ptap,imp,scanAreaRoi, True)
# set range of frames to analyze
dp.setStackRange(1, imp.getStackSize())
# start threads.
dp.start()
# wait till the processing finishes
dp.join()
# stop the thread.
dp.interrupt()
PTA.setDetectionState(False)
# show detection results
imp.show()
# Particle lists
#ll = dp.getalldplist()
#print ll
# track lists
tracks = dp.getLinkedPointList()
for t in tracks:
print t
# MSD analysis
msds = dp.getMSDres(5.0, True)
print "msd data size", msds.size()
for msd in msds:
print msd.getID(), msd.getA(), msd.getB(), msd.getR()
CSVwrite(datafilepath, msds)
# discard the DetextParticle Object
dp = None
##Back to the GUI mode: without this line, PTA GUI will not showup from next time.
PTA.setNoGUI(False)
srcDir = DirectoryChooser("Choose!").getDirectory()
IJ.log("directory: "+srcDir)
for root, directories, filenames in os.walk(srcDir):
for filename in filenames:
if filename.endswith(".tif"):
path = os.path.join(root, filename)
IJ.log(path)
core(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment