def fileNameToIlastikOuput(filename): """Converts *ClearMap* file name to an argument string for use with Ilastik headless mode Arguments: filename (str): image file name or expression Returns: str: Ilastik headless ouput specifications Note: The output is formated accroding to the Ilastik pixel calssification output specifications """ if not isValidOutputFileName(filename): raise RuntimeError('Ilastik: file format not compatibel with Ilastik output'); if io.isFileExpression(filename): o = '--output_format="' + io.fileExtension(filename) + ' sequence" '+ \ '--output_filename_format="' + filelist.fileExpressionToFileName(filename, '{slice_index}') + '"'; return o; else: # single file extensionToOuput = {'bmp' : 'bmp', 'gif' : 'gif', 'hdr' : 'hrd', 'jpg' : 'jpg', 'jpeg': 'jpeg','pbm' : 'pbm', 'pgm' : 'pgm', 'png' : 'png', 'pnm' : 'pnm', 'ppm' : 'ppm', 'ras' : 'ras', 'tif' : 'tif', 'tiff': 'tiff','xv' : 'xv', 'h5' : 'hdf5' , 'npy' : 'numpy'}; ext = extensionToOuput[io.fileExtension(filename)]; o = '--output_format="' + ext +'" ' + \ '--output_filename_format="' + filename + '"'; return o;
def fileNameToIlastikOuput(filename): """Converts *ClearMap* file name to an argument string for use with Ilastik headless mode Arguments: filename (str): image file name or expression Returns: str: Ilastik headless ouput specifications Note: The output is formated accroding to the Ilastik pixel calssification output specifications """ if not isValidOutputFileName(filename): raise RuntimeError("Ilastik: file format not compatibel with Ilastik output") if io.isFileExpression(filename): o = ( '--output_format="' + io.fileExtension(filename) + ' sequence" ' + '--output_filename_format="' + filelist.fileExpressionToFileName(filename, "{slice_index}") + '"' ) return o else: # single file extensionToOuput = { "bmp": "bmp", "gif": "gif", "hdr": "hrd", "jpg": "jpg", "jpeg": "jpeg", "pbm": "pbm", "pgm": "pgm", "png": "png", "pnm": "pnm", "ppm": "ppm", "ras": "ras", "tif": "tif", "tiff": "tiff", "xv": "xv", "h5": "hdf5", "npy": "numpy", } ext = extensionToOuput[io.fileExtension(filename)] o = '--output_format="' + ext + '" ' + '--output_filename_format="' + filename + '"' return o
def isValidOutputFileName(filename): """Checks if the file is a valid format for use with Ilastik ouput Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be written by Ilastik """ if io.isFileExpression(filename): validExtensions = [ "bmp", "gif", "hdr", "jpg", "jpeg", "pbm", "pgm", "png", "pnm", "ppm", "ras", "tif", "tiff", "xv", ] return io.fileExtension(filename) in validExtensions else: validExtensions = [ "bmp", "gif", "hdr", "jpg", "jpeg", "pbm", "pgm", "png", "pnm", "ppm", "ras", "tif", "tiff", "xv", "h5", "npy", ] return io.fileExtension(filename) in validExtensions
def isValidOutputFileName(filename): """Checks if the file is a valid format for use with Ilastik ouput Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be written by Ilastik """ if io.isFileExpression(filename): validExtensions = ['bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv']; return io.fileExtension(filename) in validExtensions; else: validExtensions = ['bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv', 'h5', 'npy']; return io.fileExtension(filename) in validExtensions;
def fileNameToIlastikOuput(filename): """Converts *ClearMap* file name to an argument string for use with Ilastik headless mode Arguments: filename (str): image file name or expression Returns: str: Ilastik headless ouput specifications Note: The output is formated accroding to the Ilastik pixel calssification output specifications """ if not isValidOutputFileName(filename): raise RuntimeError( 'Ilastik: file format not compatibel with Ilastik output') if io.isFileExpression(filename): o = '--output_format="' + io.fileExtension(filename) + ' sequence" '+ \ '--output_filename_format="' + filelist.fileExpressionToFileName(filename, '{slice_index}') + '"' return o else: # single file extensionToOuput = { 'bmp': 'bmp', 'gif': 'gif', 'hdr': 'hrd', 'jpg': 'jpg', 'jpeg': 'jpeg', 'pbm': 'pbm', 'pgm': 'pgm', 'png': 'png', 'pnm': 'pnm', 'ppm': 'ppm', 'ras': 'ras', 'tif': 'tif', 'tiff': 'tiff', 'xv': 'xv', 'h5': 'hdf5', 'npy': 'numpy' } ext = extensionToOuput[io.fileExtension(filename)] o = '--output_format="' + ext +'" ' + \ '--output_filename_format="' + filename + '"' return o
def fileNameToIlastikInput(filename): """Converts *ClearMap* file name to a string for use with Ilastik input Arguments: filename (str): image file name or expression Returns: str: Ilastik conform file name Note: file expressions in *ClearMap* are regular expressions but shell expressions in Ilastik. """ if not isValidInputFileName(filename): raise RuntimeError('Ilastik: file format not compatibel with Ilastik'); if io.isFileExpression(filename): return '"' + filelist.fileExpressionToFileName(filename, '*') + '"'; else: return '"' + filename + '"';
def fileNameToIlastikInput(filename): """Converts *ClearMap* file name to a string for use with Ilastik input Arguments: filename (str): image file name or expression Returns: str: Ilastik conform file name Note: file expressions in *ClearMap* are regular expressions but shell expressions in Ilastik. """ if not isValidInputFileName(filename): raise RuntimeError('Ilastik: file format not compatibel with Ilastik') if io.isFileExpression(filename): return '"' + filelist.fileExpressionToFileName(filename, '*') + '"' else: return '"' + filename + '"'
def isValidOutputFileName(filename): """Checks if the file is a valid format for use with Ilastik ouput Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be written by Ilastik """ if io.isFileExpression(filename): validExtensions = [ 'bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv' ] return io.fileExtension(filename) in validExtensions else: validExtensions = [ 'bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv', 'h5', 'npy' ] return io.fileExtension(filename) in validExtensions
def cropData(source, sink=None, x=all, y=all, z=all, adjustOverlap=False, verbose=True, processes=all): """Crop source from start to stop point Arguments: source (str or array): filename or data array of source sink (str or None): filename or sink x,y,z (tuple or all): the range to crop the data to adjustOverlap (bool): correct overlap meta data if exists Return: str or array: array or filename with cropped data """ if sink is None: return readDataFiles(source, x=x, y=y, z=z) else: # sink assumed to be file expression if not io.isFileExpression(sink): raise RuntimeError("cropping data to different format not supported!") fileheader, fileext, digitfrmt = splitFileExpression(sink) # read first image to get data size and type fp, fl = readFileList(source) nz = len(fl) rz = io.toDataRange(nz, r=z) if adjustOverlap: # change overlap in first file try: fn = os.path.join(fp, fl[0]) info = io.readMetaData(fn, info=["description", "overlap", "resolution"]) description = str(info["description"]) overlap = numpy.array(info["overlap"], dtype=float) resolution = numpy.array(info["resolution"], dtype=float) except: raise RuntimeWarning("could not modify overlap!") fullsize = io.dataSize(fn) data = io.readData(fn, x=x, y=y) # overlap in pixels poverlap = overlap[:2] / resolution[:2] print poverlap # cropped pixel xr = io.toDataRange(fullsize[0], r=x) yr = io.toDataRange(fullsize[1], r=y) print xr print yr print fullsize poverlap[0] = poverlap[0] - xr[0] - (fullsize[0] - xr[1]) poverlap[1] = poverlap[1] - yr[0] - (fullsize[1] - yr[1]) print poverlap # new overlap in microns overlap = poverlap * resolution[:2] # check for consistency if numpy.abs(fullsize[0] - xr[1] - xr[0]) > 1 or numpy.abs(fullsize[1] - yr[1] - yr[0]) > 1: raise RuntimeWarning("cropping is inconsistent with overlap )modification!") # change image description import ClearMap.IO.TIF as CMTIF description = CMTIF.changeOMEMetaDataString(description, {"overlap": overlap}) print len(description) # write first file fnout = fileheader + (digitfrmt % 0) + fileext io.writeData(fnout, data, info=description) zr = range(rz[0] + 1, rz[1]) else: zr = range(rz[0], rz[1]) print zr nZ = len(zr) if processes is None: processes = 1 if processes is all: processes = multiprocessing.cpu_count() if processes > 1: # parallel processing pool = multiprocessing.Pool(processes=processes) argdata = [] for i, z in enumerate(zr): if verbose: argdata.append( (os.path.join(fp, fl[z]), fileheader + (digitfrmt % (i + 1)) + fileext, x, y, (i + 1), (nZ + 1)) ) else: argdata.append( (os.path.join(fp, fl[z]), fileheader + (digitfrmt % (i + 1)) + fileext, x, y, None, None) ) pool.map(_cropParallel, argdata) else: # sequential processing for i, z in enumerate(zr): if verbose: print "cropData: corpping image %d / %d" % (i + 1, nZ + 1) fileSource = os.path.join(fp, fl[z]) data = io.readData(fileSource, x=x, y=y) fileSink = fileheader + (digitfrmt % (i + 1)) + fileext io.writeData(fileSink, data) return sink
def stitchData( xmlPlacementFile, resultPath, algorithm=None, resolutions=None, form=None, channel=None, subRegion=None, bitDepth=None, blockSize=None, cleanup=True, compress=False, ): """Runs the final stiching step of TeraSticher Arguments: xmlPlacementFile (str or None): the xml placement descriptor resultPath (str): result path, file name or file expression for the stiched data algorithm (str or None): optional algorithm to use for placement: 'NOBLEND' for no blending 'SINBLEND' for sinusoidal blending resolutions (tuple or None): the different resolutions to produce form (str or None): the output form, if None determined automatically channel (str or None): the channels to use, 'R', 'G', 'B' or 'all' subRegion (tuple or None): optional sub region in the form ((xmin,xmax),(ymin,ymax), (zmin, zmax)) bitDepth (int or None): the pits per pixel to use, default is 8 blockSize (tuple): the sizes of various blocks to save stiched image into cleanup (bool): if True delete the TeraSticher file structure compress (bool): if True compress final tif images Returns: str : the result path or file name of the stiched data See also: `TeraStitcher project step <https://github.com/abria/TeraStitcher/wiki/Step-6:-Merge>`_. """ checkSticherInitialized() global TeraStitcherBinary cmd = TeraStitcherBinary + ' --merge --imout_format="tif" ' cmd = cmd + '--projin="' + xmlPlacementFile + '" ' if len(resultPath) > 3 and resultPath[-4:] == ".tif": if io.isFileExpression(resultPath): form = "TiledXY|2Dseries" resultPath, filename = os.path.split(resultPath) else: form = "TiledXY|3Dseries" resultPath, filename = os.path.split(resultPath) else: filename = None cmd = cmd + '--volout="' + resultPath + '" ' if algorithm is not None: cmd = cmd + ' --algorithm="' + algorithm + '" ' if resolutions is not None: cmd = cmd + '--resolutions="' for r in sorted(resolutions): cmd = cmd + str(r) cmd = cmd + " " if form is not None: cmd = cmd + '--volout_plugin="' + form + '" ' if channel is not None: cmd = cmd + '--imin_channel="' + channel + '" ' if subRegion is not None: sns = (("--R0=", "--R1="), ("--C0=", "--C1="), ("--D0=", "--D1-")) for d in range(3): for m in range(2): if subRegion[d][m] is not None: cmd = cmd + sns[d][m] + str(subRegion[d][m]) + " " if blockSize is not None: bs = ("--slicewidth=", "--sliceheight=", "--slicedepth=") for d in range(3): if blockSize[d] is not None: cmd = cmd + bs[d] + str(blockSize[d]) + " " if bitDepth is not None: cmd = cmd + "--imout_depth=" + str(bitDepth) + " " if not compress: cmd = cmd + "--libtiff_uncompress " # print resultPath io.createDirectory(resultPath, split=False) print "running: " + cmd res = os.system(cmd) if res != 0: raise RuntimeError("stitchData: failed executing: " + cmd) if filename is not None: if io.isFileExpression(filename): # convert list of files in TeraSticher from # TODO: multiple resolutions basedir = max(glob.glob(os.path.join(resultPath, "*")), key=os.path.getmtime) if cleanup: moveTeraStitcherStackToFileList(basedir, os.path.join(resultPath, filename), deleteDirectory=True) # shutil.rmtree(basedir); else: copyTeraStitcherStackToFileList(basedir, os.path.join(resultPath, filename)) else: # single file in TeraSticher folder # get most recent created file # TODO: test if this works imgfile = max(glob.glob(os.path.join(resultPath, "*/*/*/*")), key=os.path.getmtime) filename = os.path.join(resultPath, filename) os.rename(imgfile, filename) if cleanup: imgpath = os.path.sep.join(imgfile.split(os.path.sep)[:-3]) shutil.rmtree(imgpath) return filename else: return resultPath