def importJsonClient(stack, tileFiles=None, transformFile=None,
                     subprocess_mode=None,
                     host=None, port=None, owner=None, project=None,
                     client_script=None, memGB=None,
                     render=None, **kwargs):
    """run ImportJsonClient.java
    see render documentation (add link here)

    Parameters
    ----------
    stack : str
        stack to which tilespecs in tileFiles will be imported
    tileFiles : :obj:`list` of :obj:`str`
        json files containing tilespecs to import
    transformFile : str, optional
        json file containing transform specs which are
        referenced by tilespecs in tileFiles
    render : :class:`renderapi.render.Render`
        render connection object
    """
    argvs = (make_stack_params(host, port, owner, project, stack) +
             (['--transformFile', transformFile] if transformFile else []) +
             (tileFiles if isinstance(tileFiles, list)
              else [tileFiles]))
    call_run_ws_client('org.janelia.render.client.ImportJsonClient',
                       add_args=argvs, subprocess_mode=subprocess_mode,
                       client_script=client_script, memGB=memGB, **kwargs)
def coordinateClient(stack,
                     z,
                     fromJson=None,
                     toJson=None,
                     localToWorld=None,
                     numberOfThreads=None,
                     subprocess_mode=None,
                     host=None,
                     port=None,
                     owner=None,
                     project=None,
                     client_script=None,
                     memGB=None,
                     render=None,
                     **kwargs):
    """run CoordinateClient.java

    map coordinates between local and world systems

    Parameters
    ----------
    stack : str
        stack representing the world coordinates
    z : str
        z value of the section containing the tiles to map
    fromJson : str
        input json file in format defined by
        list of coordinate dictionaries (for world to local)
        or list of list of coordinate dictionaries (local to world)
    toJson : str
        json to save results of mapping coordinates
    localToWorld : bool
        whether to transform form local to world coordinates (False if None)
    numberOfThreads : int
        number of threads for java process (1 if None)

    Returns
    -------
    :obj:`list` of :obj:`dict` for local to world or :obj:`list` of :obj:`list` of :obj:`dict` for world to local
        list representing mapped coordinates
    """  # noqa: E501
    argvs = (make_stack_params(host, port, owner, project, stack) +
             ['--z', z, '--fromJson', fromJson, '--toJson', toJson] +
             (['--localToWorld'] if localToWorld else []) +
             get_param(numberOfThreads, '--numberOfThreads'))
    call_run_ws_client('org.janelia.render.client.CoordinateClient',
                       memGB=memGB,
                       client_script=client_script,
                       subprocess_mode=subprocess_mode,
                       add_args=argvs,
                       **kwargs)

    with open(toJson, 'r') as f:
        jsondata = json.load(f)

    return jsondata
def importTransformChangesClient(stack, targetStack, transformFile,
                                 targetOwner=None, targetProject=None,
                                 changeMode=None, close_stack=True,
                                 subprocess_mode=None,
                                 host=None, port=None, owner=None,
                                 project=None, client_script=None, memGB=None,
                                 render=None, **kwargs):
    """run ImportTransformChangesClient.java

    Parameters
    ----------
    stack : str
        stack from which tiles will be transformed
    targetStack : str
        stack that will hold results of transforms
    transformFile : str
        locaiton of json file in format defined below
        ::
            [{{"tileId": <tileId>,
               "transform": <transformDict>}},
              {{"tileId": ...}},
              ...
            ]
    targetOwner : str
        owner of target stack
    targetProject : str
        project of target stack
    changeMode : str
        method to apply transform to tiles.  Options are:
        'APPEND' -- add transform to tilespec's list of transforms
        'REPLACE_LAST' -- change last transform in tilespec's
            list of transforms to match transform
        'REPLACE_ALL' -- overwrite tilespec's transforms field to match
            transform

    Raises
    ------
    ClientScriptError
        if changeMode is not valid
    """
    if changeMode not in ['APPEND', 'REPLACE_LAST', 'REPLACE_ALL']:
        raise ClientScriptError(
            'changeMode {} is not valid!'.format(changeMode))
    argvs = (make_stack_params(host, port, owner, project, stack) +
             ['--targetStack', targetStack] +
             ['--transformFile', transformFile] +
             get_param(targetOwner, '--targetOwner') +
             get_param(targetProject, '--targetProject') +
             get_param(changeMode, '--changeMode'))
    call_run_ws_client(
        'org.janelia.render.client.ImportTransformChangesClient', memGB=memGB,
        client_script=client_script, subprocess_mode=subprocess_mode,
        add_args=argvs, **kwargs)
    if close_stack:
        set_stack_state(stack, 'COMPLETE', host, port, owner, project)
def transformSectionClient(stack,
                           transformId,
                           transformClass,
                           transformData,
                           zValues,
                           targetProject=None,
                           targetStack=None,
                           replaceLast=None,
                           subprocess_mode=None,
                           host=None,
                           port=None,
                           owner=None,
                           project=None,
                           client_script=None,
                           memGB=None,
                           render=None,
                           **kwargs):
    """run TranformSectionClient.java

    Parameters
    ----------
    stack : str
        stack containing section to transform
    transformId : str
        unique transform identifier
    transformClass : str
        transform className defined by the java mpicbg library
    transformData : str
        mpicbg datastring delimited by "," instead of " "
    zValues : list
        z values to which transform should be applied
    targetProject : str, optional
        project to which transformed sections should be added
    targetStack : str, optional
        stack to which transformed sections should be added
    replaceLast : bool, optional
        whether to replace the last transform in the section
        with this transform

    """
    argvs = (make_stack_params(host, port, owner, project, stack) +
             (['--replaceLast'] if replaceLast else []) +
             get_param(targetProject, '--targetProject') +
             get_param(targetStack, '--targetStack') + [
                 '--transformId', transformId, '--transformClass',
                 transformClass, '--transformData', transformData
             ] + zValues)
    call_run_ws_client('org.janelia.render.client.TransformSectionClient',
                       memGB=memGB,
                       client_script=client_script,
                       subprocess_mode=subprocess_mode,
                       add_args=argvs,
                       **kwargs)
Exemple #5
0
def import_jsonfiles_validate_client(stack,
                                     jsonfiles,
                                     transformFile=None,
                                     client_script=None,
                                     host=None,
                                     port=None,
                                     owner=None,
                                     project=None,
                                     close_stack=True,
                                     mem=6,
                                     validator=None,
                                     subprocess_mode=None,
                                     memGB=None,
                                     render=None,
                                     **kwargs):
    """Uses java client for parallelization and validation

    Parameters
    ----------
    stack: str
        stack to which jsonfiles should be uploaded
    jsonfiles: :obj:`list` of :obj:`str`
        tilespecs in json files
    transformFile: str, optional
        json file listing transformspecs with ids which are referenced
        in tilespecs contained in jsonfiles

    """
    transform_params = (['--transformFile', transformFile]
                        if transformFile is not None else [])
    if validator is None:
        validator_params = [
            '--validatorClass',
            'org.janelia.alignment.spec.validator.TemTileSpecValidator',
            '--validatorData', 'minCoordinate:-500,maxCoordinate:100000,'
            'minSize:500,maxSize:10000'
        ]
    else:
        raise NotImplementedError('No custom validation handling!')

    stack_params = make_stack_params(host, port, owner, project, stack)
    set_stack_state(stack, 'LOADING', host, port, owner, project)

    call_run_ws_client('org.janelia.render.client.ImportJsonClient',
                       stack_params + validator_params + transform_params +
                       jsonfiles,
                       client_script=client_script,
                       memGB=memGB,
                       subprocess_mode=subprocess_mode,
                       **kwargs)

    if close_stack:
        set_stack_state(stack, 'COMPLETE', host, port, owner, project)
def renderSectionClient(stack,
                        rootDirectory,
                        zs,
                        scale=None,
                        maxIntensity=None,
                        minIntensity=None,
                        bounds=None,
                        format=None,
                        channel=None,
                        customOutputFolder=None,
                        customSubFolder=None,
                        padFileNamesWithZeros=None,
                        doFilter=None,
                        fillWithNoise=None,
                        imageType=None,
                        subprocess_mode=None,
                        host=None,
                        port=None,
                        owner=None,
                        project=None,
                        client_script=None,
                        memGB=None,
                        render=None,
                        **kwargs):
    """run RenderSectionClient.java

    Parameters
    ----------
    stack : str
        stack to which zs to render belong
    rootDirectory : str
        directory to which rendered sections should be generated
    zs : :obj:`list` of :obj:`str`
        z indices of sections to render
    scale : float
        factor by which section image should be scaled
        (this materialization is 32-bit limited)
    maxIntensity : int
        value todisplay as white on a linear colormap
    minIntensity : int
        value to display as black on a linear colormap
    bounds: dict
        dictionary with keys of minX maxX minY maxY
    format : str
        output image format in 'PNG', 'TIFF', 'JPEG'
    channel : str
        channel to render out (use on multichannel stack)
    customOutputFolder : str
        folder to save all images in (overrides default of sections_at_%scale)
    customSubFolder : str
        folder to save all images in under outputFolder (overrides default of none)
    padFileNamesWithZeros: bool
        whether to pad file names with zeros to make sortable
    imageType: int
        8,16,24 to specify what kind of image type to save
    doFilter : str
        string representing java boolean for whether to render image
        with default filter (varies with render version)
    fillWithNoise : str
        string representing java boolean for whether to replace saturated
        image values with uniform noise

    """  # noqa: E501
    if bounds is not None:
        try:
            if bounds['maxX'] < bounds['minX']:
                raise ClientScriptError('maxX:{} is less than minX:{}'.format(
                    bounds['maxX'], bounds['minX']))
            if bounds['maxY'] < bounds['minY']:
                raise ClientScriptError('maxY:{} is less than minY:{}'.format(
                    bounds['maxY'], bounds['minY']))
            bound_list = ','.join(
                map(lambda x: str(int(x)), [
                    bounds['minX'], bounds['maxX'], bounds['minY'],
                    bounds['maxY']
                ]))
            bound_param = ['--bounds', bound_list]
        except KeyError as e:
            raise ClientScriptError(
                'bounds does not contain correct keys {}. Missing {}'.format(
                    bounds, e))
    else:
        bound_param = []

    argvs = (make_stack_params(host, port, owner, project, stack) +
             ['--rootDirectory', rootDirectory] + get_param(scale, '--scale') +
             get_param(format, '--format') +
             get_param(doFilter, '--doFilter') +
             get_param(minIntensity, '--minIntensity') +
             get_param(maxIntensity, '--maxIntensity') +
             get_param(fillWithNoise, '--fillWithNoise') +
             get_param(customOutputFolder, '--customOutputFolder') +
             get_param(imageType, '--imageType') +
             get_param(channel, '--channels') +
             get_param(customSubFolder, '--customSubFolder') +
             get_param(padFileNamesWithZeros, '--padFileNamesWithZeros') +
             bound_param + zs)
    call_run_ws_client('org.janelia.render.client.RenderSectionClient',
                       memGB=memGB,
                       client_script=client_script,
                       subprocess_mode=subprocess_mode,
                       add_args=argvs,
                       **kwargs)
def tilePairClient(stack,
                   minz,
                   maxz,
                   outjson=None,
                   delete_json=False,
                   baseowner=None,
                   baseproject=None,
                   basestack=None,
                   xyNeighborFactor=None,
                   zNeighborDistance=None,
                   excludeCornerNeighbors=None,
                   excludeCompletelyObscuredTiles=None,
                   excludeSameLayerNeighbors=None,
                   excludeSameSectionNeighbors=None,
                   excludePairsInMatchCollection=None,
                   minx=None,
                   maxx=None,
                   miny=None,
                   maxy=None,
                   useRowColPositions=None,
                   existingMatchOwner=None,
                   minExistingMatchCount=None,
                   onlyIncludeTilesFromStack=None,
                   onlyIncludeTilesNearTileIdsJson=None,
                   maxPairsPerFile=None,
                   return_jsondata=True,
                   return_jsonfiles=False,
                   subprocess_mode=None,
                   host=None,
                   port=None,
                   owner=None,
                   project=None,
                   client_script=None,
                   memGB=None,
                   render=None,
                   maxPairsPerFile=None,
                   **kwargs):
    """run TilePairClient.java
    see render documentation (#add link here)

    This client selects a set of tiles 'p' based on its position in
    a stack and then searches for nearby 'q' tiles using geometric parameters

    Parameters
    ----------
    stack : str
        stack from which tilepairs should be considered
    minz : str
        minimum z bound from which tile 'p' is selected
    maxz : str
        maximum z bound from which tile 'p' is selected
    outjson : str or None
        json to which tile pair file should be written
        (defaults to using temporary file and deleting after completion)
    delete_json : bool
        whether to delete outjson on function exit (True if outjson is None)
    baseowner : str
        owner of stack from which stack was derived
    baseproject : str
        project of stack from which stack was derived
    basestack : str
        stack from which stack was derived
    xyNeighborFactor : float
        factor to multiply by max(width, height) of tile 'p' in order
        to generate search radius in z (0.9 if None)
    zNeighborDistance : int
        number of z sections defining the half-height of search cylinder
        for tile 'p' (2 if None)
    excludeCornerNeighbors : bool
        whether to exclude potential 'q' tiles based on center points
        falling outside search (True if None)
    excludeCompletelyObscuredTiles : bool
        whether to exclude potential 'q' tiles that are obscured by other tiles
        based on Render's sorting (True if None)
    excludeSameLayerNeighbors : bool
        whether to exclude potential 'q' tiles in the same z layer as 'p'
    excludeSameSectionNeighbors : bool
        whether to exclude potential 'q' tiles with the same sectionId as 'p'
    excludePairsInMatchCollection : str
        a matchCollection whose 'p' and 'q' pairs will be ignored
        if generated using this client
    minx : float
        minimum x bound from which tile 'p' is selected
    maxx : float
        maximum x bound from wich tile 'p' is selected
    miny : float
        minimum y bound from which tile 'p' is selected
    maxy : float
        maximum y bound from wich tile 'p' is selected
    useRowColPositions : bool
        whether to use raster positions for neighbor analysis rather
        than radial cutoff
    existingMatchOwner : str
        owner for the excludePairsInMatchCollection collection
    minExistingMatchCount : int
        minimum match threshold to exclude pairs
        present in excludePairsInMatchCollection collection
    onlyIncludeTilesFromStack : str
        only include tiles which exist in this stack
    onlyIncludeTilesNearTileIdsJson : str
        path to json listing tileIds which should be paired
    maxPairsPerFile : int
        maximum neighborPairs per file.  Generating more pairs than
        this number (default 100000) will generate additional json
        files with a p[0-9]+ suffix on the root.


    Returns
    -------
    :obj:`list` of :obj:`dict`, optional
        list of tilepairs
    :obj:`list` of string, optional
        list of json files containing tilepairs
    """
    if outjson is None:
        with tempfile.NamedTemporaryFile(suffix=".json",
                                         mode='r',
                                         delete=False) as f:
            outjson = f.name
        delete_json = True

    argvs = (
        make_stack_params(host, port, owner, project, stack) +
        get_param(baseowner, '--baseOwner') +
        get_param(baseproject, '--baseProject') +
        get_param(basestack, '--baseStack') +
        ['--minZ', minz, '--maxZ', maxz] +
        get_param(xyNeighborFactor, '--xyNeighborFactor') +
        get_param(zNeighborDistance, '--zNeighborDistance') +
        get_param(excludeCornerNeighbors, '--excludeCornerNeighbors') +
        get_param(excludeCompletelyObscuredTiles,
                  '--excludeCompletelyObscuredTiles') +
        get_param(excludeSameLayerNeighbors, '--excludeSameLayerNeighbors') +
        get_param(excludeSameSectionNeighbors, '--excludeSameSectionNeighbors')
        + get_param(excludePairsInMatchCollection,
                    '--excludePairsInMatchCollection') +
        get_param(useRowColPositions, '--useRowColPositions') +
        get_param(existingMatchOwner, '--existingMatchOwner') +
        get_param(minExistingMatchCount, '--minExistingMatchCount') +
        get_param(onlyIncludeTilesFromStack, "--onlyIncludeTilesFromStack") +
        get_param(onlyIncludeTilesNearTileIdsJson,
                  '--onlyIncludeTilesNearTileIdsJson') +
        get_param(maxPairsPerFile, '--maxPairsPerFile') +
        ['--toJson', outjson] + get_param(minx, '--minX') +
        get_param(maxx, '--maxX') + get_param(miny, '--minY') + get_param(
            maxy, '--maxY') + get_param(maxPairsPerFile, '--maxPairsPerFile'))

    call_run_ws_client('org.janelia.render.client.TilePairClient',
                       memGB=memGB,
                       client_script=client_script,
                       subprocess_mode=subprocess_mode,
                       add_args=argvs,
                       **kwargs)

    # We create the jsonfile, so if it is empty it could be multiple
    if not os.path.isfile(outjson) or os.stat(outjson).st_size == 0:
        if os.path.isfile(outjson):
            # outjson we created is empty, so remove it
            os.remove(outjson)

        outbn_root, outbn_ext = os.path.splitext(os.path.basename(outjson))
        outbn_pattern = "{root}_p[0-9]+{ext}".format(root=outbn_root,
                                                     ext=outbn_ext)
        jsonfiles = [
            os.path.join(os.path.dirname(outjson), bn)
            for bn in os.listdir(os.path.dirname(outjson))
            if re.match(outbn_pattern, bn)
        ]
    else:
        jsonfiles = [outjson]

    if return_jsondata:
        jsondata_list = []
        for jsonfile in jsonfiles:
            with open(jsonfile, 'r') as f:
                jsondata_list.append(json.load(f))
        if len({d["renderParametersUrlTemplate"]
                for d in jsondata_list}) != 1:  # pragma: no cover
            raise ValueError("Found tilepair files with disparate "
                             "renderParametersUrlTemplate values. "
                             "Maybe there are additional files "
                             "matching the outjson pattern?")
        pairdata = [
            i for l in (d["neighborPairs"] for d in jsondata_list) for i in l
        ]
        jsondata = dict(jsondata_list[0], **{"neighborPairs": pairdata})

    if delete_json:
        for jsonfile in jsonfiles:
            os.remove(jsonfile)

    return ((jsondata,
             jsonfiles) if return_jsonfiles and return_jsondata else jsondata
            if return_jsondata else jsonfiles if return_jsonfiles else None)
def tilePairClient(stack,
                   minz,
                   maxz,
                   outjson=None,
                   delete_json=False,
                   baseowner=None,
                   baseproject=None,
                   basestack=None,
                   xyNeighborFactor=None,
                   zNeighborDistance=None,
                   excludeCornerNeighbors=None,
                   excludeCompletelyObscuredTiles=None,
                   excludeSameLayerNeighbors=None,
                   excludeSameSectionNeighbors=None,
                   excludePairsInMatchCollection=None,
                   minx=None,
                   maxx=None,
                   miny=None,
                   maxy=None,
                   subprocess_mode=None,
                   host=None,
                   port=None,
                   owner=None,
                   project=None,
                   client_script=None,
                   memGB=None,
                   render=None,
                   **kwargs):
    """run TilePairClient.java
    see render documentation (#add link here)

    This client selects a set of tiles 'p' based on its position in
    a stack and then searches for nearby 'q' tiles using geometric parameters

    Parameters
    ----------
    stack : str
        stack from which tilepairs should be considered
    minz : str
        minimum z bound from which tile 'p' is selected
    maxz : str
        maximum z bound from which tile 'p' is selected
    outjson : str or None
        json to which tile pair file should be written
        (defaults to using temporary file and deleting after completion)
    delete_json : bool
        whether to delete outjson on function exit (True if outjson is None)
    baseowner : str
        owner of stack from which stack was derived
    baseproject : str
        project of stack from which stack was derived
    basestack : str
        stack from which stack was derived
    xyNeighborFactor : float
        factor to multiply by max(width, height) of tile 'p' in order
        to generate search radius in z (0.9 if None)
    zNeighborDistance : int
        number of z sections defining the half-height of search cylinder
        for tile 'p' (2 if None)
    excludeCornerNeighbors : bool
        whether to exclude potential 'q' tiles based on center points
        falling outside search (True if None)
    excludeCompletelyObscuredTiles : bool
        whether to exclude potential 'q' tiles that are obscured by other tiles
        based on Render's sorting (True if None)
    excludeSameLayerNeighbors : bool
        whether to exclude potential 'q' tiles in the same z layer as 'p'
    excludeSameSectionNeighbors : bool
        whether to exclude potential 'q' tiles with the same sectionId as 'p'
    excludePairsInMatchCollection : str
        a matchCollection whose 'p' and 'q' pairs will be ignored
        if generated using this client
    minx : float
        minimum x bound from which tile 'p' is selected
    maxx : float
        maximum x bound from wich tile 'p' is selected
    miny : float
        minimum y bound from which tile 'p' is selected
    maxy : float
        maximum y bound from wich tile 'p' is selected

    Returns
    -------
    :obj:`list` of :obj:`dict`
        list of tilepairs
    """
    if outjson is None:
        with tempfile.NamedTemporaryFile(suffix=".json",
                                         mode='r',
                                         delete=False) as f:
            outjson = f.name
        delete_json = True

    argvs = (
        make_stack_params(host, port, owner, project, stack) +
        get_param(baseowner, '--baseOwner') +
        get_param(baseproject, '--baseProject') +
        get_param(basestack, '--baseStack') +
        ['--minZ', minz, '--maxZ', maxz] +
        get_param(xyNeighborFactor, '--xyNeighborFactor') +
        get_param(zNeighborDistance, '--zNeighborDistance') +
        get_param(excludeCornerNeighbors, '--excludeCornerNeighbors') +
        get_param(excludeCompletelyObscuredTiles,
                  '--excludeCompletelyObscuredTiles') +
        get_param(excludeSameLayerNeighbors, '--excludeSameLayerNeighbors') +
        get_param(excludeSameSectionNeighbors,
                  '--excludeSameSectionNeighbors') +
        get_param(excludePairsInMatchCollection,
                  '--excludePairsInMatchCollection') + ['--toJson', outjson] +
        get_param(minx, '--minX') + get_param(maxx, '--maxX') +
        get_param(miny, '--minY') + get_param(maxy, '--maxY'))

    call_run_ws_client('org.janelia.render.client.TilePairClient',
                       memGB=memGB,
                       client_script=client_script,
                       subprocess_mode=subprocess_mode,
                       add_args=argvs,
                       **kwargs)

    with open(outjson, 'r') as f:
        jsondata = json.load(f)

    if delete_json:
        os.remove(outjson)
    return jsondata