Skip to content

Commit

Permalink
Updated the Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
zindy committed Mar 23, 2018
1 parent 46495bd commit 4b59bea
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 52 deletions.
5 changes: 4 additions & 1 deletion BridgeLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ def GetVoxelSize(vDataSet):
def GetChannelDescription(vDataSet,aIndexC):
"""Get the Description string for a channel"""

s = vDataSet.GetChannelDescription(aIndexC)
s = ""
if aIndexC >= 0 and aIndexC < vDataSet.GetSizeC():
s = vDataSet.GetChannelDescription(aIndexC)

return s

def SetChannelDescription(vDataSet,aIndexC,s):
Expand Down
23 changes: 14 additions & 9 deletions CalculatorDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def __init__(self):
self.arraychannel = None

self.wm_geometry("500x470")
self.title("Channel Calculator XTension - Copyright (c) 2015 Egor Zindy")
self.title("Channel Calculator - Copyright (c) 2015 Egor Zindy")

self.add_menu("File",["Open configuration", "Save configuration","|","Exit"])
self.add_menu("Help",["About"])

widget = [ttk.Combobox(self.mainframe, textvariable=self.arrayvar("chana_input"), values=[], exportselection=0, state="readonly"),
ttk.Entry(self.mainframe,textvariable=self.arrayvar("factor_a"))]
Expand Down Expand Up @@ -83,6 +86,10 @@ def __init__(self):
#we have all the ingredients, now bake the dialog box!
self.bake(has_live=True, has_cancel=False) #, has_preview=True) #"Calculate")

def About(self):
'''About XTCalculator'''
tkMessageBox.showinfo("About XTCalculator", "Source code available from https://github.com/zindy/libatrous/\n\nAuthor: Egor Zindy <[email protected]>\nWellcome Centre for Cell-Matrix Research\nUniversity of Manchester (UK)")

def SetDefaults(self):
#Here you set default values
self.arrayvar["lothresh"] = 0
Expand All @@ -93,15 +100,17 @@ def SetDefaults(self):
self.ctrl_progress["value"]=0
#self.arrayvar["check_liveview"] = "on"

def SetChannels(self, channel_list, chana=0, chanb=0):
def SetChannels(self, channel_list, chana=None, chanb=None):
self.SetDefaults()

n_channels = len(channel_list)

self.chana_input['values'] = channel_list
self.chana_input.current(chana)
if chana is not None:
self.chana_input.current(chana)

self.chanb_input['values'] = channel_list
self.chanb_input.current(chanb)
if chanb is not None:
self.chanb_input.current(chanb)

#This is specific to the dialog validation and update.
#Additionally, the Validate and Update methods are also called.
Expand All @@ -117,10 +126,6 @@ def _Validate(self, arrayvar, elementname):
if hithresh < lothresh:
arrayvar["lothresh"] = hithresh

def _Update(self, arrayvar, elementname):
if arrayvar[elementname] == 'None':
return

def OnCalculate(self,*args):
'''Calculate button action'''
self.Calculate()
Expand Down
98 changes: 56 additions & 42 deletions XTCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
class MyModule:
def __init__(self,vImaris):
self.vImaris = vImaris
self.vDataSet = vImaris.GetDataSet()

#Use a clone
self.vDataSet = vImaris.GetDataSet().Clone()


#Keep all these in memory
self.vdataset_nt = self.vDataSet.GetSizeT()
Expand All @@ -65,11 +68,7 @@ def __init__(self,vImaris):
def InitDialog(self):
#Build the dialog
self.Dialog=CalculatorDialog.Dialog()

#Get the right icon...
fn_icon = './icons/Imaris_128.ico'
if '8.' in self.vImaris.GetVersion(): fn_icon = './icons/Imaris8_128.ico'
self.Dialog.wm_iconbitmap(fn_icon)
self.Dialog.set_icon(BridgeLib.GetIcon())

self.Dialog.ExitOK = self.ExitOK
self.Dialog.ExitCancel = self.ExitCancel
Expand All @@ -94,22 +93,28 @@ def InitDialog(self):
self.indexes.append(i)
self.indexdic[cname] = i

self.current_chana = 0
self.current_chanb = 0
self.current_tp = self.vImaris.GetVisibleIndexT()
self.Dialog.SetChannels(self.names)# ,current_chan_a, current_chan_b)

#Check if we already have an output channel and if json info is contained in the description
output_channel = self.GetOutputChannel()
self.SetThresholdScales(output_channel)

json = BridgeLib.GetChannelDescription(self.vDataSet, output_channel)
if json != "":
self.Dialog.arrayvar.set_json(json)

self.Dialog.SetChannels(self.names,self.current_chana, self.current_chanb)
self.SetThresholdScales()
self.Dialog.mainloop()


def SetThresholdScales(self,channel=None):
#The threshold values
if type(channel) == tuple or type(channel) == list:
michan = channel[0]
machan = channel[1]
else:
if channel is None:
channel = self.current_chana
if channel is None or channel < 0:
channel = self.Dialog.arrayvar["chana_input"]

michan = self.vDataSet.GetChannelRangeMin(channel)
machan = self.vDataSet.GetChannelRangeMax(channel)
Expand All @@ -127,23 +132,17 @@ def GetUpdated(self,old,new):
def Update(self, arrayvar, elementname):
'''Show the new value in a label'''

if elementname == "chana_input":
self.current_chana = self.indexdic[arrayvar[elementname]]
#print self.current_chana

if elementname == "chanb_input":
self.current_chanb = self.indexdic[arrayvar[elementname]]
#print self.current_chana

#Do we need to preview?
if (arrayvar["check_liveview"] == "on"):
self.Preview()

def GetOutputChannel(self,match="(calc)"):
def GetOutputChannel(self,match="(calc)",create=False):
"""Finds the output channel for this plugin.
If none found, this method will create a new channel and return its new index
In any case, channel name reflects channels and operation"""

arrayvar = self.Dialog.arrayvar.get()

ret = -1
nc = self.vDataSet.GetSizeC()
for i in range(nc):
Expand All @@ -152,29 +151,36 @@ def GetOutputChannel(self,match="(calc)"):
ret = i
break

if ret == -1:
if ret == -1 and create == True:
self.vDataSet.SetSizeC(nc+1)
ret = nc
rgba = self.vDataSet.GetChannelColorRGBA(self.current_chana)
#print rgba
#rgba = rgba ^ 0x00ffffff
self.vDataSet.SetChannelColorRGBA(ret,rgba)

arrayvar = self.Dialog.arrayvar.get()
check_inverta = (arrayvar["check_inverta"] == "on")
check_invertb = (arrayvar["check_invertb"] == "on")
current_chan_a = self.Dialog.arrayvar["chana_input"]
current_chan_b = self.Dialog.arrayvar["chanb_input"]

cname_a = self.vDataSet.GetChannelName(self.current_chana)
cname_b = self.vDataSet.GetChannelName(self.current_chanb)
try:
current_chan_a = self.indexdic[current_chan_a]
current_chan_b = self.indexdic[current_chan_b]
except:
print "channels A or B not available?"
else:
rgba = self.vDataSet.GetChannelColorRGBA(current_chan_a)
self.vDataSet.SetChannelColorRGBA(ret,rgba)

if check_inverta:
cname_a = "!"+cname_a
check_inverta = (arrayvar["check_inverta"] == "on")
check_invertb = (arrayvar["check_invertb"] == "on")

if check_invertb:
cname_b = "!"+cname_b
cname_a = self.vDataSet.GetChannelName(current_chan_a)
cname_b = self.vDataSet.GetChannelName(current_chan_b)

operation_name = arrayvar["operation_type"]
self.vDataSet.SetChannelName(ret,"%s(%s,%s) %s" % (operation_name,cname_a,cname_b,match))
if check_inverta:
cname_a = "!"+cname_a

if check_invertb:
cname_b = "!"+cname_b

operation_name = arrayvar["operation_type"]
self.vDataSet.SetChannelName(ret,"%s(%s,%s) %s" % (operation_name,cname_a,cname_b,match))

return ret

Expand All @@ -196,6 +202,9 @@ def Calculate(self,preview=False):
check_threshold = (arrayvar["check_threshold"] == "on")
check_normalise = (arrayvar["check_normalise"] == "on")

current_chan_a = self.indexdic[arrayvar["chana_input"]]
current_chan_b = self.indexdic[arrayvar["chanb_input"]]

try:
factor_a = float(arrayvar["factor_a"])
except:
Expand Down Expand Up @@ -246,7 +255,11 @@ def Calculate(self,preview=False):
break

#The output channel is created if needed. In any case, the name will be updated (if needed)
channel_out = self.GetOutputChannel()
channel_out = self.GetOutputChannel(create=True)

#Update the channel description...
print "Updating?"
BridgeLib.SetChannelDescription(self.vDataSet,channel_out,self.Dialog.arrayvar.get_json())

if preview == False:
channel_visibility = []
Expand All @@ -270,11 +283,11 @@ def Calculate(self,preview=False):
matp = None
for tp in tps:
if nz == 1:
array_a = BridgeLib.GetDataSlice(self.vDataSet,0,self.current_chana,tp).astype(float)
array_b = BridgeLib.GetDataSlice(self.vDataSet,0,self.current_chanb,tp).astype(float)
array_a = BridgeLib.GetDataSlice(self.vDataSet,0,current_chan_a,tp).astype(float)
array_b = BridgeLib.GetDataSlice(self.vDataSet,0,current_chan_b,tp).astype(float)
else:
array_a = BridgeLib.GetDataVolume(self.vDataSet,self.current_chana,tp).astype(float)
array_b = BridgeLib.GetDataVolume(self.vDataSet,self.current_chanb,tp).astype(float)
array_a = BridgeLib.GetDataVolume(self.vDataSet,current_chan_a,tp).astype(float)
array_b = BridgeLib.GetDataVolume(self.vDataSet,current_chan_b,tp).astype(float)

if check_inverta:
array_a = machan - array_a
Expand Down Expand Up @@ -380,6 +393,7 @@ def Calculate(self,preview=False):
#Keeping the arrayvar values
self.arrayvar_last = arrayvar
self.Dialog.ctrl_progress["value"]=0.
self.vImaris.SetDataSet(self.vDataSet)

def Preview(self):
self.Calculate(preview=True)
Expand Down

0 comments on commit 4b59bea

Please sign in to comment.