1
1
import logging
2
2
import json
3
3
import os
4
+ import re
4
5
import subprocess
5
6
import tempfile
6
7
@@ -135,6 +136,11 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
135
136
excludeSameSectionNeighbors = None ,
136
137
excludePairsInMatchCollection = None ,
137
138
minx = None , maxx = None , miny = None , maxy = None ,
139
+ useRowColPositions = None , existingMatchOwner = None ,
140
+ minExistingMatchCount = None , onlyIncludeTilesFromStack = None ,
141
+ onlyIncludeTilesNearTileIdsJson = None , maxPairsPerFile = None ,
142
+ return_jsondata = True ,
143
+ return_jsonfiles = False ,
138
144
subprocess_mode = None ,
139
145
host = None , port = None , owner = None , project = None ,
140
146
client_script = None , memGB = None ,
@@ -191,11 +197,29 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
191
197
minimum y bound from which tile 'p' is selected
192
198
maxy : float
193
199
maximum y bound from wich tile 'p' is selected
200
+ useRowColPositions : bool
201
+ whether to use raster positions for neighbor analysis rather
202
+ than radial cutoff
203
+ existingMatchOwner : str
204
+ owner for the excludePairsInMatchCollection collection
205
+ minExistingMatchCount : int
206
+ minimum match threshold to exclude pairs
207
+ present in excludePairsInMatchCollection collection
208
+ onlyIncludeTilesFromStack : str
209
+ only include tiles which exist in this stack
210
+ onlyIncludeTilesNearTileIdsJson : str
211
+ path to json listing tileIds which should be paired
212
+ maxPairsPerFile : int
213
+ maximum neighborPairs per file. Generating more pairs than
214
+ this number (default 100000) will generate additional json
215
+ files with a p[0-9]+ suffix on the root.
194
216
195
217
Returns
196
218
-------
197
- :obj:`list` of :obj:`dict`
219
+ :obj:`list` of :obj:`dict`, optional
198
220
list of tilepairs
221
+ :obj:`list` of string, optional
222
+ list of json files containing tilepairs
199
223
"""
200
224
if outjson is None :
201
225
with tempfile .NamedTemporaryFile (
@@ -219,6 +243,18 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
219
243
'--excludeSameSectionNeighbors' ) +
220
244
get_param (excludePairsInMatchCollection ,
221
245
'--excludePairsInMatchCollection' ) +
246
+ get_param (useRowColPositions ,
247
+ '--useRowColPositions' ) +
248
+ get_param (existingMatchOwner ,
249
+ '--existingMatchOwner' ) +
250
+ get_param (minExistingMatchCount ,
251
+ '--minExistingMatchCount' ) +
252
+ get_param (onlyIncludeTilesFromStack ,
253
+ "--onlyIncludeTilesFromStack" ) +
254
+ get_param (onlyIncludeTilesNearTileIdsJson ,
255
+ '--onlyIncludeTilesNearTileIdsJson' ) +
256
+ get_param (maxPairsPerFile ,
257
+ '--maxPairsPerFile' ) +
222
258
['--toJson' , outjson ] +
223
259
get_param (minx , '--minX' ) + get_param (maxx , '--maxX' ) +
224
260
get_param (miny , '--minY' ) + get_param (maxy , '--maxY' ))
@@ -228,12 +264,45 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
228
264
subprocess_mode = subprocess_mode ,
229
265
add_args = argvs , ** kwargs )
230
266
231
- with open (outjson , 'r' ) as f :
232
- jsondata = json .load (f )
267
+ # We create the jsonfile, so if it is empty it could be multiple
268
+ if not os .path .isfile (outjson ) or os .stat (outjson ).st_size == 0 :
269
+ if os .path .isfile (outjson ):
270
+ # outjson we created is empty, so remove it
271
+ os .remove (outjson )
272
+
273
+ outbn_root , outbn_ext = os .path .splitext (os .path .basename (outjson ))
274
+ outbn_pattern = "{root}_p[0-9]+{ext}" .format (
275
+ root = outbn_root , ext = outbn_ext )
276
+ jsonfiles = [
277
+ os .path .join (os .path .dirname (outjson ), bn )
278
+ for bn in os .listdir (os .path .dirname (outjson ))
279
+ if re .match (outbn_pattern , bn )]
280
+ else :
281
+ jsonfiles = [outjson ]
282
+
283
+ if return_jsondata :
284
+ jsondata_list = []
285
+ for jsonfile in jsonfiles :
286
+ with open (jsonfile , 'r' ) as f :
287
+ jsondata_list .append (json .load (f ))
288
+ if len ({d ["renderParametersUrlTemplate" ] for d in jsondata_list }) != 1 : # pragma: no cover
289
+ raise ValueError (
290
+ "Found tilepair files with disparate "
291
+ "renderParametersUrlTemplate values. "
292
+ "Maybe there are additional files "
293
+ "matching the outjson pattern?" )
294
+ pairdata = [i for l in (d ["neighborPairs" ] for d in jsondata_list )
295
+ for i in l ]
296
+ jsondata = dict (jsondata_list [0 ], ** {"neighborPairs" : pairdata })
233
297
234
298
if delete_json :
235
- os .remove (outjson )
236
- return jsondata
299
+ for jsonfile in jsonfiles :
300
+ os .remove (jsonfile )
301
+
302
+ return ((jsondata , jsonfiles ) if return_jsonfiles and return_jsondata
303
+ else jsondata if return_jsondata
304
+ else jsonfiles if return_jsonfiles
305
+ else None )
237
306
238
307
239
308
@renderclientaccess
@@ -346,10 +415,11 @@ def renderSectionClient(stack, rootDirectory, zs, scale=None,
346
415
maxIntensity = None , minIntensity = None , bounds = None ,
347
416
format = None , channel = None , customOutputFolder = None ,
348
417
customSubFolder = None , padFileNamesWithZeros = None ,
349
- doFilter = None , fillWithNoise = None , imageType = None ,
350
- subprocess_mode = None , host = None , port = None , owner = None ,
351
- project = None , client_script = None , memGB = None ,
352
- render = None , ** kwargs ):
418
+ resolutionUnit = None , doFilter = None , fillWithNoise = None ,
419
+ imageType = None , subprocess_mode = None , host = None ,
420
+ port = None , owner = None , project = None ,
421
+ client_script = None , memGB = None , render = None ,
422
+ ** kwargs ):
353
423
"""run RenderSectionClient.java
354
424
355
425
Parameters
@@ -379,6 +449,9 @@ def renderSectionClient(stack, rootDirectory, zs, scale=None,
379
449
folder to save all images in under outputFolder (overrides default of none)
380
450
padFileNamesWithZeros: bool
381
451
whether to pad file names with zeros to make sortable
452
+ resolutionUnit: str
453
+ if format is tiff and unit is specified (e.g. as 'nm'), include resolution data
454
+ in rendered tiff headers.
382
455
imageType: int
383
456
8,16,24 to specify what kind of image type to save
384
457
doFilter : str
@@ -418,9 +491,11 @@ def renderSectionClient(stack, rootDirectory, zs, scale=None,
418
491
get_param (customOutputFolder , '--customOutputFolder' ) +
419
492
get_param (imageType , '--imageType' ) +
420
493
get_param (channel , '--channels' ) +
421
- get_param (customSubFolder , '--customSubFolder' ) +
494
+ get_param (customSubFolder , '--customSubFolder' ) +
422
495
get_param (padFileNamesWithZeros , '--padFileNamesWithZeros' ) +
496
+ get_param (resolutionUnit , '--resolutionUnit' ) +
423
497
bound_param + zs )
498
+
424
499
call_run_ws_client ('org.janelia.render.client.RenderSectionClient' ,
425
500
memGB = memGB , client_script = client_script ,
426
501
subprocess_mode = subprocess_mode , add_args = argvs ,
0 commit comments