11import logging
22import json
33import os
4+ import re
45import subprocess
56import tempfile
67
@@ -135,6 +136,11 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
135136 excludeSameSectionNeighbors = None ,
136137 excludePairsInMatchCollection = None ,
137138 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 ,
138144 subprocess_mode = None ,
139145 host = None , port = None , owner = None , project = None ,
140146 client_script = None , memGB = None ,
@@ -191,11 +197,29 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
191197 minimum y bound from which tile 'p' is selected
192198 maxy : float
193199 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.
194216
195217 Returns
196218 -------
197- :obj:`list` of :obj:`dict`
219+ :obj:`list` of :obj:`dict`, optional
198220 list of tilepairs
221+ :obj:`list` of string, optional
222+ list of json files containing tilepairs
199223 """
200224 if outjson is None :
201225 with tempfile .NamedTemporaryFile (
@@ -219,6 +243,18 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
219243 '--excludeSameSectionNeighbors' ) +
220244 get_param (excludePairsInMatchCollection ,
221245 '--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' ) +
222258 ['--toJson' , outjson ] +
223259 get_param (minx , '--minX' ) + get_param (maxx , '--maxX' ) +
224260 get_param (miny , '--minY' ) + get_param (maxy , '--maxY' ))
@@ -228,12 +264,45 @@ def tilePairClient(stack, minz, maxz, outjson=None, delete_json=False,
228264 subprocess_mode = subprocess_mode ,
229265 add_args = argvs , ** kwargs )
230266
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 })
233297
234298 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 )
237306
238307
239308@renderclientaccess
@@ -346,10 +415,11 @@ def renderSectionClient(stack, rootDirectory, zs, scale=None,
346415 maxIntensity = None , minIntensity = None , bounds = None ,
347416 format = None , channel = None , customOutputFolder = None ,
348417 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 ):
353423 """run RenderSectionClient.java
354424
355425 Parameters
@@ -379,6 +449,9 @@ def renderSectionClient(stack, rootDirectory, zs, scale=None,
379449 folder to save all images in under outputFolder (overrides default of none)
380450 padFileNamesWithZeros: bool
381451 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.
382455 imageType: int
383456 8,16,24 to specify what kind of image type to save
384457 doFilter : str
@@ -418,9 +491,11 @@ def renderSectionClient(stack, rootDirectory, zs, scale=None,
418491 get_param (customOutputFolder , '--customOutputFolder' ) +
419492 get_param (imageType , '--imageType' ) +
420493 get_param (channel , '--channels' ) +
421- get_param (customSubFolder , '--customSubFolder' ) +
494+ get_param (customSubFolder , '--customSubFolder' ) +
422495 get_param (padFileNamesWithZeros , '--padFileNamesWithZeros' ) +
496+ get_param (resolutionUnit , '--resolutionUnit' ) +
423497 bound_param + zs )
498+
424499 call_run_ws_client ('org.janelia.render.client.RenderSectionClient' ,
425500 memGB = memGB , client_script = client_script ,
426501 subprocess_mode = subprocess_mode , add_args = argvs ,
0 commit comments