-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMTK_Registration_GUI.py
219 lines (190 loc) · 6.3 KB
/
CMTK_Registration_GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from fiji.util.gui import GenericDialogPlus
from java.awt.event import TextListener
from java.awt.event import ItemListener
from java.awt import Font
from java.awt import Color
from java.lang import Runtime
import os,subprocess,sys
from java.lang.System import getProperty
sys.path.append(getProperty("fiji.dir") + "/plugins/CMTK_Registration")
from cmtkgui import relpath,makescript,findExecutable
import cmtkgui
# helper classes/functions for main generic dialog
class RegRootListener(TextListener):
def textValueChanged(self, tvc):
regroot = regrootf.getText()
if len(regroot)==0:
statusf.setText('')
return
if os.path.exists(regroot):
regrootf.setForeground(Color.black)
updateOuputFolders()
else:
regrootf.setForeground(Color.red)
statusf.setText('Please choose valid root directory')
statusf.setForeground(Color.red)
return
# check if we already have a sensible images dir - if not, then set
imgdir = imgdirf.getText()
if len(imgdir)==0 or not os.path.exists(imgdir):
imgdir = os.path.join(regroot,'images')
imgdirf.setText(imgdir)
else:
statusf.setText('')
class ImageDirListener(TextListener):
def textValueChanged(self, tvc):
imgdir = imgdirf.getText()
# no comment if unset
if len(imgdir)==0:
statusf.setText('')
return
# no comment if unset
if os.path.exists(imgdir):
statusf.setText('')
imgdirf.setForeground(Color.black)
regroot = regrootf.getText()
if len(regroot)==0 or not os.path.exists(regroot):
regrootf.setText(os.path.dirname(imgdir))
else:
imgdirf.setForeground(Color.red)
statusf.setText('Please choose valid images directory')
statusf.setForeground(Color.red)
class OuputSuffixListener(TextListener):
def textValueChanged(self, tvc):
updateOuputFolders()
class RegParamListener(ItemListener):
def itemStateChanged(self, isc):
regparamc=choicef.getSelectedItem()
regparams=''
if regparamc == 'Cachero, Ostrovsky 2010':
regparams = '-X 26 -C 8 -G 80 -R 4 -A "--accuracy 0.4" -W "--accuracy 0.4"'
regparamf.setText(regparams)
print "Chosen reg params: "+regparamc
def updateOuputFolders():
outsuffix=outsuffixf.getText()
if outsuffix:
reg="Registration."+outsuffix
ref="reformatted."+outsuffix
else:
reg="Registration"
ref="reformatted"
outputf.setText("Output Folders: "+reg+", "+ref)
return
## START!
try:
gd = GenericDialogPlus('CMTK Registration GUI')
# 0.1) Identify path to CMTK binaries
bindir=cmtkgui.install_dir()
print 'bindir is ' + bindir
# 0.1) Identify path to munger.pl script
#munger='/Users/jefferis/bin/munger.pl'
munger=cmtkgui.tool_path('munger')
print 'munger is ' + munger
gd.addHelp("http://flybrain.mrc-lmb.cam.ac.uk/dokuwiki/doku.php?id=warping_manual:registration_gui")
dirFieldWidth=50
gdMargin=130
gd.addDirectoryField("Registration Folder",None,dirFieldWidth)
regrootf = gd.getStringFields().get(0)
# reference brain
gd.addFileField("Reference Brain", "",dirFieldWidth)
# input directory/image
gd.addDirectoryOrFileField("Input Image or Image Directory",None,dirFieldWidth)
imgdirf = gd.getStringFields().get(2)
gd.setInsets(10,gdMargin,10)
gd.addMessage("Output folders:")
outputf=gd.getMessage()
# what to do: affine/warp/reformat
gd.setInsets(10,200,10)
gd.addCheckboxGroup(3,2,["affine","01","warp","02","reformat","03"],[True,True,True,True,True,True],["Registration Actions","Reformat Channels"])
#gd.addCheckboxGroup(1,3,["01","02","03"],[True,True,True],["Reformat Channels"])
# Registration options
# Jefferis,Potter 2007, Cachero,Ostrovsky 2010, Manual
gd.addChoice("Registration Params",["Jefferis, Potter 2007","Cachero, Ostrovsky 2010"],"Jefferis, Potter 2007")
choicef=gd.getChoices().get(0)
print choicef.getSelectedItem()
# final Action (Test, Run, Write Script)
gd.addChoice("Action",["Test","Write Script","Run (Experimental)"],"Write Script")
font=Font("SansSerif",Font.BOLD,12)
# Advanced options
gd.setInsets(25,100,10)
gd.addMessage("Advanced Options:",font)
advancedoptionsf=gd.getMessage()
ncores=Runtime.getRuntime().availableProcessors()
defaultCores=1
if ncores >=8:
defaultCores=4
elif ncores>=4:
defaultCores=2
gd.addSlider("Number of cpu cores to use",1,ncores,defaultCores)
gd.setInsets(0,230,10)
gd.addCheckbox("Verbose log messages",False)
gd.addStringField("Output folder suffix","",20)
outsuffixf = gd.getStringFields().get(3)
gd.addStringField("(Further) Registration Params","",50);
regparamf = gd.getStringFields().get(4)
gd.addStringField("Additional Arguments to munger.pl","",50);
regrootf.addTextListener(RegRootListener())
imgdirf.addTextListener(ImageDirListener())
outsuffixf.addTextListener(OuputSuffixListener())
choicef.addItemListener(RegParamListener())
# used for errors etc
gd.addMessage("Start by choosing a registration directory or images directory!")
statusf=gd.getMessage()
gd.showDialog()
if gd.wasCanceled():
sys.exit("User cancelled!")
# Process Dialog Choices
rootDir=gd.getNextString()
os.chdir(rootDir)
refBrain=gd.getNextString()
image=gd.getNextString()
image=relpath(image,rootDir)
print refBrain
refBrain=relpath(refBrain,rootDir)
print refBrain
affine=gd.getNextBoolean()
ch01=gd.getNextBoolean()
warp=gd.getNextBoolean()
ch02=gd.getNextBoolean()
reformat=gd.getNextBoolean()
ch03=gd.getNextBoolean()
munger_actions=""
if affine:
munger_actions+="-a "
if warp:
munger_actions+="-w "
if reformat:
channels=''
if ch01:
channels+='01'
if ch02:
channels+='02'
if ch03:
channels+='03'
if channels != '':
munger_actions+="-r "+channels+" "
verbose=gd.getNextBoolean()
corestouse=gd.getNextNumber()
outsuffix=gd.getNextString()
regparams=gd.getNextChoice()
print regparams
regparams=gd.getNextString()
mungeropts=gd.getNextString()
action=gd.getNextChoice()
if action == 'Test': mungeropts+=' -t'
if verbose: mungeropts+=' -v'
mungeropts+=' -T %d' % (int(corestouse))
if not outsuffix == '':
mungeropts += ' -d .'+outsuffix
cmd='"%s" -b "%s" %s %s %s -s "%s" %s' % (munger,bindir,munger_actions,regparams,mungeropts,refBrain,image)
print cmd
if action !='Test':
# make a script
script=makescript(cmd,rootDir,outdir=os.path.join(rootDir,'commands'))
print 'script is %s' % (script)
if action != 'Write Script':
# Actually run the script
subprocess.call(script,shell=False)
except SystemExit, e:
from ij import IJ
IJ.showStatus(str(e))