-
Notifications
You must be signed in to change notification settings - Fork 3
/
optimize.py
189 lines (156 loc) · 5.46 KB
/
optimize.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
# -*- coding: iso-8859-1 -*-
"""
Unit: bahama
Project: BioImageXD
Description:
BAHAMA - BioimAgexd Highly Advanced Memory Architecture
Copyright (C) 2006 BioImageXD Project
See CREDITS.txt for details
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
__author__ = "BioImageXD Project <http://www.bioimagexd.org/>"
__version__ = "$Revision: 1.21 $"
__date__ = "$Date: 2005/01/13 13:42:03 $"
import scripting
import Configuration
import Logging
import vtk
import vtkbxd
currentPipeline = []
memLimit = None
numberOfDivisions = None
alwaysSplit = 0
noLimits = 0
conf = None
targetSize = None
executing = 0
def set_target_size(xDimension, yDimension, zDimension = 0):
"""
Resizes the target sample
"""
assert xDimension > 0 and yDimension > 0 and zDimension > 0,"Target dimensions need to be > 0"
global targetSize, conf
dataUnit = scripting.visualizer.getDataUnit()
if zDimension == 0:
zDimension = dataUnit.getDimensions()[2]
targetSize = xDimension, yDimension, zDimension
if not conf:
conf = Configuration.getConfiguration()
if dataUnit.isProcessed():
sources = dataUnit.getSourceDataUnits()
else:
sources = [dataUnit]
for dataUnit in sources:
dataUnit.dataSource.setResampleDimensions((xDimension, yDimension, zDimension))
def optimize(image = None, vtkFilter = None, updateExtent = None, releaseData = 0):
"""
Execute a pipeline and optimize it
"""
if image:
filterInUse = image.GetProducerPort().GetProducer()
else:
filterInUse = vtkFilter
#val, numFilters = optimizePipeline(filterInUse, releaseData = releaseData)
if updateExtent and not scripting.wantWholeDataset:
Logging.info("Using update extent %s"%str(updateExtent),kw="pipeline")
filterInUse.GetOutput().SetUpdateExtent(updateExtent)
img = execute_limited(filterInUse, updateExtent = updateExtent)
#if numFilters != 0:
# img = execute_limited(val, updateExtent = updateExtent)
#else:
# img = val.GetOutput()
return img
def execute_limited(pipeline, updateExtent = None):
"""
"""
global memLimit, noLimits, alwaysSplit
global numberOfDivisions
global executing
#retval = pipeline.GetOutput()
#if updateExtent and not scripting.wantWholeDataset:
# retval.SetUpdateExtent(updateExtent)
#pipeline.Update()
#return pipeline.GetOutput()
if not memLimit:
get_memory_limit()
if noLimits or (not memLimit and not alwaysSplit):
streamer = vtk.vtkImageDataStreamer()
streamer.SetNumberOfStreamDivisions(1)
if alwaysSplit:
Logging.info("Using vtkImageDataStreamer with %d divisions"%numberOfDivisions, kw = "pipeline")
streamer = vtk.vtkImageDataStreamer()
streamer.SetNumberOfStreamDivisions(numberOfDivisions)
elif (memLimit and not noLimits):
Logging.info("Using vtkMemoryLimitImageDataStreamer with with limit=%dMB"%memLimit, kw = "pipeline")
streamer = vtk.vtkMemoryLimitImageDataStreamer()
streamer.SetMemoryLimit(1024*memLimit)
streamer.GetExtentTranslator().SetSplitModeToZSlab()
executing = 1
streamer.SetInputConnection(pipeline.GetOutputPort())
retval = streamer.GetOutput()
if updateExtent and not scripting.wantWholeDataset:
Logging.info("Setting update extent to ", updateExtent, kw = "pipeline")
retval.SetUpdateExtent(updateExtent)
print "Update extent now=",retval.GetUpdateExtent()
streamer.Update()
print "Done"
executing = 0
return retval
def get_memory_limit():
"""
Get memory limits from configuration
"""
global conf, memLimit, alwaysSplit, numberOfDivisions, noLimits
if memLimit:
return memLimit
if not conf:
conf = Configuration.getConfiguration()
memLimit = conf.getConfigItem("LimitTo", "Performance")
alwaysSplit = conf.getConfigItem("AlwaysSplit", "Performance")
if alwaysSplit != None:
alwaysSplit = eval(alwaysSplit)
noLimits = conf.getConfigItem("NoLimits", "Performance")
if noLimits != None:
noLimits = eval(noLimits)
numberOfDivisions = conf.getConfigItem("NumberOfDivisions", "Performance")
if numberOfDivisions:
numberOfDivisions = eval(numberOfDivisions)
if memLimit:
memLimit = eval(memLimit)
return memLimit
def optimizePipeline(ifilter, numberNotUsed = 0, releaseData = 0):
"""
Optimize the pipeline
"""
stack = []
filterStack = []
if isinstance(ifilter, vtkbxd.vtkImageSimpleMIP):
isMip = 1
cfilter = ifilter
while 1:
hasParents = 0
for i in range(0, cfilter.GetNumberOfInputPorts()):
for j in range(0, cfilter.GetNumberOfInputConnections(i)):
inp = cfilter.GetInputConnection(i, j).GetProducer()
if inp:
stack.insert(0, inp)
filterStack.append(inp)
hasParents = 1
try:
cfilter = filterStack.pop()
except:
break
if hasParents and releaseData:
cfilter.GetOutput().ReleaseDataFlagOn()
nfilters = len(stack)
return ifilter, nfilters