So there is this software called “3DSlicer”. It is an open source tool to work with medical images (CT, MRI).
It actually has a python-API that is documented very extensively — but which is not easy to understand if you have no idea what you want to do and only get “this is the way I always set everything up”. What settings in the GUI are where in the API?
So what I did was not very nice: I used the API to gain access to the Qt objects of the tools and just clicked all buttons in the right order…
Usually I would have added empty lines, but then the file would not load in 3DSlicer.
importosimporttime#OUTPUT="output.nii"#defget_all_files(d):forfinos.listdir(d):ifos.path.exists(os.path.join(d,f,OUTPUT)):continueyieldos.path.join(d,f)#classWaiter(qt.QWidget):def__init__(self,parent=None):qt.QWidget.__init__(self,parent)self.timer=qt.QTimer()self.timer.setInterval(1000)self.timer.connect('timeout()',self.test)self.running=Falseself.gaf=get_all_files(r"F:\where_ever")self.timer.start()deftest(self):# If the apply-Button is enabled, we can click it!ifapply.enabledandnotself.running:# set newself.dir=next(self.gaf)mprage_name=os.listdir(os.path.join(self.dir,"MPRAge"))[0]flair_name=os.listdir(os.path.join(self.dir,"3DFLAIR"))[0]mprage=os.path.join(self.dir,"MPRAge",mprage_name)flair=os.path.join(self.dir,"3DFLAIR",flair_name)mprage_name=os.path.splitext(mprage_name)[0]flair_name=os.path.splitext(flair_name)[0]print("Starting new...",self.dir)loadVolume(mprage)loadVolume(flair)c=slicer.mrmlScene.GetNodes()# find the two loaded files in the file-selection listmprage_node=[xforxin[c.GetItemAsObject(i)foriinrange(c.GetNumberOfItems())]ifx.GetName()==mprage_name][0]flair_node=[xforxin[c.GetItemAsObject(i)foriinrange(c.GetNumberOfItems())]ifx.GetName()==flair_name][0]# and select themfixed.setCurrentNode(mprage_node)moving.setCurrentNode(flair_node)# then click "Apply"apply.click()self.running=Trueelifapply.enabledandself.running:# if the button is pressable and we are waiting for it to finish, it must have just finished!c=slicer.mrmlScene.GetNodes()# find the output-fileoutput=[xforxin[c.GetItemAsObject(i)foriinrange(c.GetNumberOfItems())]ifx.GetName()=="output"][0]# and save itsaveNode(output,os.path.join(self.dir,OUTPUT))print("Finished.")self.running=Falseelse:print("Waiting...")## Get a handle to a loaded modulem=getModuleGui('BRAINSFit')# find a handle of the input-file section of the GUIb=m.children()[2].children()[0].children()[0].children()[1]fixed=b.children()[4].children()[2]moving=b.children()[4].children()[4]## get a handle to the "Apply"-Buttonapply=m.children()[-1]# Enter the loopw=Waiter()
Note: This was hacked together for a very specific task. You will most likely need to change at least some bits to use it. Also note, that this might break with new version of 3DSlicer or the BRAINSFit-module. This is tested on 3DSlicer v4.5.0-1.