32
32
import omero .util .script_utils as script_utils
33
33
34
34
from collections import defaultdict
35
+ from random import shuffle
35
36
36
37
from numpy import amin , amax , iinfo
37
38
from numpy import average as avg
38
39
39
40
40
- def calcStatsInfo (conn , imageId ):
41
+ def calcStatsInfo (conn , imageId , choice , debug = False ):
41
42
"""
42
43
Process a single image here: creating a new StatsInfo object
43
44
if necessary.
@@ -67,10 +68,16 @@ def createData(self):
67
68
def close (self ):
68
69
pass
69
70
71
+ only_default = (choice == "default" )
72
+
70
73
class Iteration (TileLoopIteration ):
71
74
72
75
def run (self , data , z , c , t , x , y ,
73
76
tileWidth , tileHeight , tileCount ):
77
+
78
+ if only_default :
79
+ if t != 0 or z != int (sizeZ / 2 ):
80
+ return
74
81
zctMap [c ].append (
75
82
(z , c , t , (x , y , tileWidth , tileHeight )))
76
83
@@ -79,7 +86,17 @@ def run(self, data, z, c, t, x, y,
79
86
sizeZ , sizeC , sizeT ,
80
87
tileW , tileH , Iteration ())
81
88
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
+
82
97
def channelGen ():
98
+ byte_count = 0
99
+ tile_count = 0
83
100
pixels = oldImage .getPrimaryPixels ()
84
101
rv = dict ()
85
102
dt = pixels .getTile (0 , 0 , 0 , (0 , 0 , 16 , 16 )).dtype
@@ -90,12 +107,16 @@ def channelGen():
90
107
tile = pixels .getTile (* tileInfo )
91
108
tile_min = min (tile_min , amin (tile ))
92
109
tile_max = max (tile_max , amax (tile ))
110
+ byte_count += len (tile )
111
+ tile_count += 1
93
112
rv [c ] = (tile_min , tile_max )
94
- yield rv
113
+ yield rv , byte_count , tile_count
95
114
96
115
statsInfos = dict ()
97
- for x in channelGen ():
116
+ for x , byte_count , tile_count in channelGen ():
98
117
statsInfos .update (x )
118
+
119
+ print "Loaded %s tile(s) (%s bytes)" % (tile_count , byte_count )
99
120
return statsInfos
100
121
101
122
@@ -113,13 +134,15 @@ def processImages(conn, scriptParams):
113
134
raise Exception ("No images found" )
114
135
imageIds = sorted (set ([i .getId () for i in images ]))
115
136
137
+ choice = scriptParams ["Choice" ]
138
+ debug = bool (scriptParams .get ("Debug" , False ))
116
139
globalmin = defaultdict (list )
117
140
globalmax = defaultdict (list )
118
141
119
142
tb = TableBuilder ("Context" , "Channel" , "Min" , "Max" )
120
143
statsInfos = dict ()
121
144
for iId in imageIds :
122
- statsInfo = calcStatsInfo (conn , iId )
145
+ statsInfo = calcStatsInfo (conn , iId , choice , debug )
123
146
statsInfos [iId ] = statsInfo
124
147
for c , si in sorted (statsInfo .items ()):
125
148
c_min , c_max = si
@@ -138,7 +161,7 @@ def processImages(conn, scriptParams):
138
161
if scriptParams ["DryRun" ]:
139
162
print str (tb .build ())
140
163
else :
141
- method = scriptParams ["Method " ]
164
+ combine = scriptParams ["Combine " ]
142
165
for iId in imageIds :
143
166
img = conn .getObject ("Image" , iId )
144
167
for c , ch in enumerate (img .getChannels (noRE = True )):
@@ -150,18 +173,20 @@ def processImages(conn, scriptParams):
150
173
si = si ._obj
151
174
action = "updating"
152
175
153
- if method == "no" :
176
+ if combine == "no" :
154
177
si .globalMin = rdouble (statsInfos [iId ][c ][0 ])
155
178
si .globalMax = rdouble (statsInfos [iId ][c ][1 ])
156
- elif method == "outer" :
179
+ elif combine == "outer" :
157
180
si .globalMin = rdouble (min (globalmin [c ]))
158
181
si .globalMax = rdouble (max (globalmax [c ]))
159
- elif method == "inner" :
182
+ elif combine == "inner" :
160
183
si .globalMin = rdouble (max (globalmin [c ]))
161
184
si .globalMax = rdouble (min (globalmax [c ]))
162
- elif method == "average" :
185
+ elif combine == "average" :
163
186
si .globalMin = rdouble (avg (globalmin [c ]))
164
187
si .globalMax = rdouble (avg (globalmax [c ]))
188
+ else :
189
+ raise Exception ("unknown combine: %s" % combine )
165
190
166
191
print "Image:%s(c=%s) - %s StatsInfo(%s, %s)" % (
167
192
iId , c , action , si .globalMin .val , si .globalMax .val )
@@ -197,11 +222,22 @@ def runAsScript():
197
222
default = True ),
198
223
199
224
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" ,
201
232
description = "Whether and if so how to combine values" ,
202
233
default = "no" ,
203
234
values = ("no" , "outer" , "inner" , "average" )),
204
235
236
+ scripts .Bool (
237
+ "Debug" , optional = True , grouping = "6" ,
238
+ description = "Whether to print debug statements" ,
239
+ default = False ),
240
+
205
241
version = "5.1.3" ,
206
242
authors = ["Josh Moore" , "OME Team" ],
207
243
institutions = ["University of Dundee" ],
0 commit comments