-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXT_MJG_SpotsCopyToAllTimepoints3.py
159 lines (146 loc) · 7 KB
/
XT_MJG_SpotsCopyToAllTimepoints3.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
#Spots Copy to all time points
#
# Written by Matthew J. Gastinger
# June 2020 - Imaris 9.5.1
#
# Installation:
#
# - Copy this file into the XTensions folder in the Imaris installation directory
# - You will find this function in the Image Processing menu
#
# <CustomTools>
# <Menu>
# <Submenu name="Spots Functions">
# <Item name="Spots Copy to All Time Points" icon="Python3">
# <Command>Python3XT::XT_MJG_SpotsCopyToAllTimepoints3(%i)</Command>
# </Item>
# </Submenu>
# </Menu>
# <SurpassTab>
# <SurpassComponent name="bpSpots">
# <Item name="Spots Copy to All Time Points" icon="Python3">
# <Command>Python3XT::XT_MJG_SpotsCopyToAllTimepoints3(%i)</Command>
# </Item>
# </SurpassComponent>
# </SurpassTab>
# </CustomTools>
#
# Description:
#
# This XTension will copy all or selected spots/surfaces from a single time frame to
# all of the time points. And it will generate Track edges to connect the spots over time.
from operator import itemgetter
import numpy as np
import ImarisLib
# aImarisId=0
def XT_MJG_SpotsCopyToAllTimepoints3(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
vSurpassScene = vImarisApplication.GetSurpassScene()
vSurfaces = vFactory.ToSurfaces(vImarisApplication.GetSurpassSelection())
vSpots = vFactory.ToSpots(vImarisApplication.GetSurpassSelection())
############################################################################
############################################################################
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()
############################################################################
#Get Image properties
vDataMin = (vImage.GetExtendMinX(),vImage.GetExtendMinY(),vImage.GetExtendMinZ())
vDataMax = (vImage.GetExtendMaxX(),vImage.GetExtendMaxY(),vImage.GetExtendMaxZ())
vDataSize = (vImage.GetSizeX(),vImage.GetSizeY(),vImage.GetSizeZ())
vSizeT = vImage.GetSizeT()
vSizeC = vImage.GetSizeC()
aXvoxelSpacing= (vDataMax[0]-vDataMin[0])/vDataSize[0]
aYvoxelSpacing= (vDataMax[1]-vDataMin[1])/vDataSize[1]
aZvoxelSpacing = round((vDataMax[2]-vDataMin[2])/vDataSize[2],3)
############################################################################
vOption1=1
#Process Spots
#get all spots
vSpots = vFactory.ToSpots(vImarisApplication.GetSurpassSelection())
vNewSpots = vImarisApplication.GetFactory().CreateSpots()
vSpotsRadius = vSpots.GetRadii()
vSpotsPositionXYZ = vSpots.GetPositionsXYZ()
vSpotsTime = vSpots.GetIndicesT()
vSpotsIds = vSpots.GetIds()
vNumberOfSpots = len(vSpotsTime)
vCurrentTimeIndex = vImarisApplication.GetVisibleIndexT()
vSpotsName = vSpots.GetName()
vSpots.SetVisible(0)
vSelectedSpotsIds = vSpots.GetSelectedIds()
vSpotsTimeFinal= []
vSpotsPositionXYZFinal = []
vSpotsRadiusFinal= []
qSpotsSelected=False
#Remove TrackIDs ids >10000000
vSelectedSpotsIds=[x for x in vSelectedSpotsIds if x < 1000000000]
vNumberOfSelectedSpots = len(vSelectedSpotsIds)
if vSelectedSpotsIds != []:
qSpotsSelected=True
#grab Selected Spot position and radius
vSpotsIds=np.array(vSpotsIds)
vSelectedSpotsIds=np.array(vSelectedSpotsIds)
sorter = np.argsort(vSpotsIds)
vSelectedSpotIndices=sorter[np.searchsorted(vSpotsIds, vSelectedSpotsIds, sorter=sorter)]
if vNumberOfSelectedSpots>1:
vSelectedPositionsXYZ=list(itemgetter(*vSelectedSpotIndices)(vSpotsPositionXYZ))
vSelectedRadius=list(itemgetter(*vSelectedSpotIndices)(vSpotsRadius))
vSelectedTimeIndex=list(itemgetter(*vSelectedSpotIndices)(vSpotsTime))
else:
vSelectedPositionsXYZ=[x[1] for x in enumerate(vSpotsPositionXYZ)
if x[0] in vSelectedSpotIndices]
vSelectedRadius=[x[1] for x in enumerate(vSpotsRadius)
if x[0] in vSelectedSpotIndices]
vSelectedTimeIndex=[x[1] for x in enumerate(vSpotsTime)
if x[0] in vSelectedSpotIndices]
############################################################################
#Copy selected or all spots to all timepoints
if vOption1==1:
if qSpotsSelected==True:
for t in range (vSizeT):
vTimeIndex= [t]*len(vSelectedRadius)
vSpotsTimeFinal.extend(vTimeIndex)
vSpotsPositionXYZFinal.extend(vSelectedPositionsXYZ)
vSpotsRadiusFinal.extend(vSelectedRadius)
vNewSpots.Set(vSpotsPositionXYZFinal, vSpotsTimeFinal, vSpotsRadiusFinal)
else:
for t in range (vSizeT):
vTimeIndex= [t]*vNumberOfSpots
vSpotsTimeFinal.extend(vTimeIndex)
vSpotsPositionXYZFinal.extend(vSpotsPositionXYZ)
vSpotsRadiusFinal.extend(vSpotsRadius)
vNewSpots.Set(vSpotsPositionXYZFinal, vSpotsTimeFinal, vSpotsRadiusFinal)
############################################################################
if vOption1==1:
if qSpotsSelected==True:
vSpotsIndicesFinal1=list(range(int((vSizeT-1)*vNumberOfSelectedSpots)))
vSpotsIndicesFinal2=[x + vNumberOfSelectedSpots for x in vSpotsIndicesFinal1]
vTrackEdges=[]
#Populate track edges [0,1],[1,2],[2,3], etc
for i in range (len(vSpotsIndicesFinal1)):
vTrackEdges.append([vSpotsIndicesFinal1[i],vSpotsIndicesFinal2[i]])
else:
#Generate artificial Track edges for all spots in timelapse
vSpotsIndicesFinal1=list(range(int((vSizeT-1)*vNumberOfSpots)))
vSpotsIndicesFinal2=[x + vNumberOfSpots for x in vSpotsIndicesFinal1]
vTrackEdges=[]
#Populate track edges [0,1],[1,2],[2,3], etc
for i in range (len(vSpotsIndicesFinal1)):
vTrackEdges.append([vSpotsIndicesFinal1[i],vSpotsIndicesFinal2[i]])
###################################################################
###################################################################
vNewSpots.SetTrackEdges(vTrackEdges)
vNewSpots.SetName(str(vSpots.GetName())+" copied to all timepoints")
vRGBA = vSpots.GetColorRGBA()
vNewSpots.SetColorRGBA(vRGBA)
vSurpassScene.AddChild(vNewSpots, -1)