-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXT_MJG_Surface_Surface_Overlap.py
More file actions
236 lines (207 loc) · 9.63 KB
/
Copy pathXT_MJG_Surface_Surface_Overlap.py
File metadata and controls
236 lines (207 loc) · 9.63 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#Surface-Surface Overlap
#
#Written by Matthew J. Gastinger
#June 2020
#
# <CustomTools>
# <Menu>
# <Submenu name="Surfaces Functions">
# <Item name="Surface-Surface Overlap" icon="Python3">
# <Command>Python3XT::XT_MJG_Surface_Surface_Overlap(%i)</Command>
# </Item>
# </Submenu>
# </Menu>
# <SurpassTab>
# <SurpassComponent name="bpSurfaces">
# <Item name="Surface-Surface Overlap" icon="Python3">
# <Command>Python3XT::XT_MJG_Surface_Surface_Overlap(%i)</Command>
# </Item>
# </SurpassComponent>
# </SurpassTab>
# </CustomTools>
#Description
#
#This XTension will mask each of 2 surface scenes. It will find the voxels
#inside each surface that overlap with each other. A new channel will be
#made, and a new surface generated from the overlapping regions.
#Python libraries - no special libraries are required for this XTension
import ImarisLib
import numpy
# GUI imports
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
# aImarisId=0
def XT_MJG_Surface_Surface_Overlap(aImarisId):
# Create an ImarisLib object
vImarisLib = ImarisLib.ImarisLib()
# Get an imaris object with id aImarisId
vImarisApplication = vImarisLib.GetApplication(aImarisId)
# Get the factory
vFactory = vImarisApplication.GetFactory()
# Get the currently loaded dataset
vImage = vImarisApplication.GetDataSet()
# Get the Surpass scene
vScene = vImarisApplication.GetSurpassScene()
############################################################################
############################################################################
#Get image properties #timepoints #channels
#Get the extents of the image
vExtendMin = (vImage.GetExtendMinX(),vImage.GetExtendMinY(),vImage.GetExtendMinZ())
vExtendMax = (vImage.GetExtendMaxX(),vImage.GetExtendMaxY(),vImage.GetExtendMaxZ())
vImageSize = (vImage.GetSizeX(),vImage.GetSizeY(),vImage.GetSizeZ())
vSizeT = vImage.GetSizeT()
vSizeC = vImage.GetSizeC()
Xvoxelspacing= (vExtendMax[1]-vExtendMin[1])/vImageSize[1];
vDefaultSmoothingFactor=round(Xvoxelspacing*2,2);
############################################################################
window = tk.Tk()
window.title('Surface Overlap')
window.geometry('260x55')
window.attributes("-topmost", True)
w = window.winfo_reqwidth()
h = window.winfo_reqheight()
ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
window.geometry('+%d+%d' % (x, y))
def CreateSurface():
global vSmoothingFactor
if (var1.get() == 0):
vSmoothingFactor=0
window.destroy()
else:
vSmoothingFactor=float(Entry1.get())
window.destroy()
var1 = tk.IntVar(value=0)
tk.Checkbutton(window, text='Smoothing',
variable=var1, onvalue=1, offvalue=0).grid(row=0, column=0, padx=40,sticky=W)
tk.Label(window, text='um').grid(row=0,column=1,padx=45)
Entry1=Entry(window,justify='center',width=4)
Entry1.grid(row=0, column=1, sticky=W)
Entry1.insert(0, str(vDefaultSmoothingFactor))
btn = Button(window, text="Create Overlap Surface",highlightbackground='blue',command=CreateSurface)
btn.grid(column=0, row=2, sticky=E)
window.mainloop()
#########################################################
#Count and find Surpass objects in Scene
vNumberSurpassItems=vImarisApplication.GetSurpassScene().GetNumberOfChildren()
NamesSurfaces=[]
NamesSpots=[]
NamesFilaments=[]
NamesFilamentIndex=[]
NamesSurfaceIndex=[]
NamesSpotsIndex=[]
vSurpassSurfaces = 0;
vSurpassSpots = 0;
vSurpassFilaments = 0;
for vChildIndex in range(0,vNumberSurpassItems):
vDataItem=vScene.GetChild(vChildIndex)
IsSurface=vImarisApplication.GetFactory().IsSurfaces(vDataItem)
IsSpot=vImarisApplication.GetFactory().IsSpots(vDataItem)
IsFilament=vImarisApplication.GetFactory().IsFilaments(vDataItem)
if IsSurface:
vSurpassSurfaces = vSurpassSurfaces+1
NamesSurfaces.append(vDataItem.GetName())
NamesSurfaceIndex.append(vChildIndex)
elif IsSpot:
vSurpassSpots = vSurpassSpots+1
NamesSpots.append(vDataItem.GetName())
NamesSpotsIndex.append(vChildIndex)
elif IsFilament:
vSurpassFilaments = vSurpassFilaments+1
NamesFilaments.append(vDataItem.GetName(),)
NamesFilamentIndex.append(vChildIndex)
#####################################################
#Making the Listbox for the Surpass menu
main = Tk()
main.title("Surpass menu")
main.geometry("+50+150")
main.attributes("-topmost", True)
#################################################################
#Set input in center on screen
# Gets the requested values of the height and widht.
windowWidth = main.winfo_reqwidth()
windowHeight = main.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(main.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(main.winfo_screenheight()/2 - windowHeight/2)
# Positions the window in the center of the page.
main.geometry("+{}+{}".format(positionRight, positionDown))
##################################################################
names = StringVar()
names.set(NamesSurfaces)
lstbox = Listbox(main, listvariable=names, selectmode=MULTIPLE, width=20, height=10)
lstbox.grid(column=0, row=0, columnspan=2)
def select():
global ObjectSelection
ObjectSelection = list()
selection = lstbox.curselection()
for i in selection:
entrada = lstbox.get(i)
ObjectSelection.append(entrada)
#Test for the correct number selected
if len(ObjectSelection)!=2:
messagebox.showerror(title='Surface menu',
message='Please Select 2 surfaces')
main.mainloop()
else:
main.destroy()
btn = Button(main, text="Choose 2 surfaces!", command=select)
btn.grid(column=1, row=1)
#Selects the top 2 items in the list
lstbox.selection_set(0,1)
main.mainloop()
####################################################################
# get the Selected surfaces Indices in Surpass for specific
vDataItem=vScene.GetChild(NamesSurfaceIndex[(NamesSurfaces.index( ''.join(map(str, ObjectSelection[0]))))])
vSurfaces1=vImarisApplication.GetFactory().ToSurfaces(vDataItem)
vDataItem=vScene.GetChild(NamesSurfaceIndex[(NamesSurfaces.index( ''.join(map(str, ObjectSelection[1]))))])
vSurfaces2=vImarisApplication.GetFactory().ToSurfaces(vDataItem)
#clone Dataset
vDataSet = vImage.Clone()
#Test retrieval
# vNumberOfSurfaces1 = vSurfaces1.GetNumberOfSurfaces()
# vNumberOfSurfaces2 = vSurfaces2.GetNumberOfSurfaces()
#add additional channel
vDataSet.SetSizeC(vSizeC+1)
vLastChannel=vSizeC#newest channel added (starting from "0")
#Generate surface mask for each surface over time
for vTimeIndex in range (0,vSizeT):
vSurfaces1Mask = vSurfaces1.GetMask(vExtendMin[0],vExtendMin[1],vExtendMin[2],
vExtendMax[0],vExtendMax[1],vExtendMax[2],
vImageSize[0], vImageSize[1],vImageSize[2],
vTimeIndex)
vSurfaces2Mask = vSurfaces2.GetMask(vExtendMin[0],vExtendMin[1],vExtendMin[2],
vExtendMax[0],vExtendMax[1],vExtendMax[2],
vImageSize[0], vImageSize[1],vImageSize[2],
vTimeIndex)
for vIndexZ in range (0,vImageSize[2]):
ch1=vSurfaces1Mask.GetDataSubVolumeAs1DArrayFloats(0,0,vIndexZ,0,0,
vImageSize[0], vImageSize[1],1)
ch2=vSurfaces2Mask.GetDataSubVolumeAs1DArrayFloats(0,0,vIndexZ,0,0,
vImageSize[0], vImageSize[1],1)
#Determine the Voxels that are colocalized
Coloc = [ch1[i] + ch2[i] for i in range(len(ch1))]
Coloc=[0 if x!=2 else x for x in Coloc]#replace >2 with "0"
Coloc=[1 if x==2 else x for x in Coloc]#replace 2 with 1
vDataSet.SetDataSubVolumeAs1DArrayFloats(Coloc,0,0,vIndexZ,
vLastChannel,vTimeIndex,
vImageSize[0],vImageSize[1],1)
vDataSet.SetChannelName(vLastChannel,'ColocChannel')
vDataSet.SetChannelRange(vLastChannel,0,1)
vImarisApplication.SetDataSet(vDataSet)
#Run the Surface Creation Wizard on the new channel
ip = vImarisApplication.GetImageProcessing()
Coloc_surfaces1 = ip.DetectSurfaces(vDataSet, [],
vLastChannel,
vSmoothingFactor, 0, True, 55, '')
Coloc_surfaces1.SetName('ColocSurface')
#Add new surface to Surpass Scene
vSurfaces1.SetVisible(0)
vSurfaces2.SetVisible(0)
vScene.AddChild(Coloc_surfaces1, -1)