Skip to content

Commit ccabe32

Browse files
committed
Add 'default' and 'random' strategies
Add options under the 'Choice' argument to reduce the processing overhead. Note: this does not yet search for random tiles across *all* images but rather per image
1 parent 9cb1874 commit ccabe32

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

omero/util_scripts/Min_Max.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
import omero.util.script_utils as script_utils
3333

3434
from collections import defaultdict
35+
from random import shuffle
3536

3637
from numpy import amin, amax, iinfo
3738
from numpy import average as avg
3839

3940

40-
def calcStatsInfo(conn, imageId):
41+
def calcStatsInfo(conn, imageId, choice, debug=False):
4142
"""
4243
Process a single image here: creating a new StatsInfo object
4344
if necessary.
@@ -67,10 +68,16 @@ def createData(self):
6768
def close(self):
6869
pass
6970

71+
only_default = (choice == "default")
72+
7073
class Iteration(TileLoopIteration):
7174

7275
def run(self, data, z, c, t, x, y,
7376
tileWidth, tileHeight, tileCount):
77+
78+
if only_default:
79+
if t != 0 or z != int(sizeZ/2):
80+
return
7481
zctMap[c].append(
7582
(z, c, t, (x, y, tileWidth, tileHeight)))
7683

@@ -79,7 +86,17 @@ def run(self, data, z, c, t, x, y,
7986
sizeZ, sizeC, sizeT,
8087
tileW, tileH, Iteration())
8188

89+
if choice == "random":
90+
for c in zctMap:
91+
copy = list(zctMap[c])
92+
if len(copy) >= 100:
93+
copy = copy[0:100]
94+
shuffle(copy)
95+
zctMap[c] = copy
96+
8297
def channelGen():
98+
byte_count = 0
99+
tile_count = 0
83100
pixels = oldImage.getPrimaryPixels()
84101
rv = dict()
85102
dt = pixels.getTile(0, 0, 0, (0, 0, 16, 16)).dtype
@@ -90,12 +107,16 @@ def channelGen():
90107
tile = pixels.getTile(*tileInfo)
91108
tile_min = min(tile_min, amin(tile))
92109
tile_max = max(tile_max, amax(tile))
110+
byte_count += len(tile)
111+
tile_count += 1
93112
rv[c] = (tile_min, tile_max)
94-
yield rv
113+
yield rv, byte_count, tile_count
95114

96115
statsInfos = dict()
97-
for x in channelGen():
116+
for x, byte_count, tile_count in channelGen():
98117
statsInfos.update(x)
118+
119+
print "Loaded %s tile(s) (%s bytes)" % (tile_count, byte_count)
99120
return statsInfos
100121

101122

@@ -113,13 +134,15 @@ def processImages(conn, scriptParams):
113134
raise Exception("No images found")
114135
imageIds = sorted(set([i.getId() for i in images]))
115136

137+
choice = scriptParams["Choice"]
138+
debug = bool(scriptParams.get("Debug", False))
116139
globalmin = defaultdict(list)
117140
globalmax = defaultdict(list)
118141

119142
tb = TableBuilder("Context", "Channel", "Min", "Max")
120143
statsInfos = dict()
121144
for iId in imageIds:
122-
statsInfo = calcStatsInfo(conn, iId)
145+
statsInfo = calcStatsInfo(conn, iId, choice, debug)
123146
statsInfos[iId] = statsInfo
124147
for c, si in sorted(statsInfo.items()):
125148
c_min, c_max = si
@@ -138,7 +161,7 @@ def processImages(conn, scriptParams):
138161
if scriptParams["DryRun"]:
139162
print str(tb.build())
140163
else:
141-
method = scriptParams["Method"]
164+
combine = scriptParams["Combine"]
142165
for iId in imageIds:
143166
img = conn.getObject("Image", iId)
144167
for c, ch in enumerate(img.getChannels(noRE=True)):
@@ -150,18 +173,20 @@ def processImages(conn, scriptParams):
150173
si = si._obj
151174
action = "updating"
152175

153-
if method == "no":
176+
if combine == "no":
154177
si.globalMin = rdouble(statsInfos[iId][c][0])
155178
si.globalMax = rdouble(statsInfos[iId][c][1])
156-
elif method == "outer":
179+
elif combine == "outer":
157180
si.globalMin = rdouble(min(globalmin[c]))
158181
si.globalMax = rdouble(max(globalmax[c]))
159-
elif method == "inner":
182+
elif combine == "inner":
160183
si.globalMin = rdouble(max(globalmin[c]))
161184
si.globalMax = rdouble(min(globalmax[c]))
162-
elif method == "average":
185+
elif combine == "average":
163186
si.globalMin = rdouble(avg(globalmin[c]))
164187
si.globalMax = rdouble(avg(globalmax[c]))
188+
else:
189+
raise Exception("unknown combine: %s" % combine)
165190

166191
print "Image:%s(c=%s) - %s StatsInfo(%s, %s)" % (
167192
iId, c, action, si.globalMin.val, si.globalMax.val)
@@ -197,11 +222,22 @@ def runAsScript():
197222
default=True),
198223

199224
scripts.String(
200-
"Method", optional=True, grouping="4",
225+
"Choice", optional=True, grouping="4",
226+
description="How to choose which planes will be chosen",
227+
default="default",
228+
values=("default", "random", "all")),
229+
230+
scripts.String(
231+
"Combine", optional=True, grouping="5",
201232
description="Whether and if so how to combine values",
202233
default="no",
203234
values=("no", "outer", "inner", "average")),
204235

236+
scripts.Bool(
237+
"Debug", optional=True, grouping="6",
238+
description="Whether to print debug statements",
239+
default=False),
240+
205241
version="5.1.3",
206242
authors=["Josh Moore", "OME Team"],
207243
institutions=["University of Dundee"],

0 commit comments

Comments
 (0)