3232import omero .util .script_utils as script_utils
3333
3434from collections import defaultdict
35+ from random import shuffle
3536
3637from numpy import amin , amax , iinfo
3738from 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,15 @@ def createData(self):
6768 def close (self ):
6869 pass
6970
71+ only_default = (choice == "default" )
7072 class Iteration (TileLoopIteration ):
7173
7274 def run (self , data , z , c , t , x , y ,
7375 tileWidth , tileHeight , tileCount ):
76+
77+ if only_default :
78+ if t != 0 or z != int (sizeZ / 2 ):
79+ return
7480 zctMap [c ].append (
7581 (z , c , t , (x , y , tileWidth , tileHeight )))
7682
@@ -79,7 +85,17 @@ def run(self, data, z, c, t, x, y,
7985 sizeZ , sizeC , sizeT ,
8086 tileW , tileH , Iteration ())
8187
88+ if choice == "random" :
89+ for c in zctMap :
90+ copy = list (zctMap [c ])
91+ if len (copy ) >= 100 :
92+ copy = copy [0 :99 ]
93+ shuffle (copy )
94+ zctMap [c ] = copy
95+
8296 def channelGen ():
97+ byte_count = 0
98+ tile_count = 0
8399 pixels = oldImage .getPrimaryPixels ()
84100 rv = dict ()
85101 dt = pixels .getTile (0 , 0 , 0 , (0 , 0 , 16 , 16 )).dtype
@@ -90,12 +106,17 @@ def channelGen():
90106 tile = pixels .getTile (* tileInfo )
91107 tile_min = min (tile_min , amin (tile ))
92108 tile_max = max (tile_max , amax (tile ))
109+ byte_count += len (tile )
110+ tile_count += 1
93111 rv [c ] = (tile_min , tile_max )
94- yield rv
112+ yield rv , byte_count , tile_count
113+
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" % combin )
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