def moveTeraStitcherStackToFileList(source, sink, deleteDirectory = True, verbose = True): """Moves image files from TeraSticher file structure to a list of files Arguments: source (str): base directory of the TeraStitcher files sink (str): regular expression of the files to copy to verbose (bool): show progress Returns: str: sink regular expression """ fns = glob.glob(os.path.join(source, '*/*/*')); fns = natsort.natsorted(fns); io.createDirectory(sink); for i,f in enumerate(fns): fn = filelist.fileExpressionToFileName(sink, i); if verbose: print '%s -> %s' % (f,fn) shutil.move(f, fn); if deleteDirectory: p,_ = os.path.split(fns[0]); p = p.split(os.path.sep); p = p[:-2]; p = os.path.sep.join(p); shutil.rmtree(p); return sink;
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 moveTeraStitcherStackToFileList(source, sink, deleteDirectory=True, verbose=True): """Moves image files from TeraSticher file structure to a list of files Arguments: source (str): base directory of the TeraStitcher files sink (str): regular expression of the files to copy to verbose (bool): show progress Returns: str: sink regular expression """ fns = glob.glob(os.path.join(source, "*/*/*")) fns = natsort.natsorted(fns) io.createDirectory(sink) for i, f in enumerate(fns): fn = filelist.fileExpressionToFileName(sink, i) if verbose: print "%s -> %s" % (f, fn) shutil.move(f, fn) if deleteDirectory: p, _ = os.path.split(fns[0]) p = p.split(os.path.sep) p = p[:-2] p = os.path.sep.join(p) shutil.rmtree(p) return sink
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 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 copyTeraStitcherStackToFileList(source, sink, verbose = True): """Copies image files from TeraSticher file structure to a list of files Arguments: source (str): base directory of the TeraStitcher files sink (str): regular expression of the files to copy to verbose (bool): show progress Returns: str: sink regular expression """ #TODO: multiple tiles ! fns = glob.glob(os.path.join(source, '*/*/*')); fns = natsort.natsorted(fns); #print fns io.createDirectory(sink); for i,f in enumerate(fns): fn = filelist.fileExpressionToFileName(sink, i); if verbose: print '%s -> %s' % (f,fn) shutil.copyfile(f, fn); return sink;
def copyTeraStitcherStackToFileList(source, sink, verbose=True): """Copies image files from TeraSticher file structure to a list of files Arguments: source (str): base directory of the TeraStitcher files sink (str): regular expression of the files to copy to verbose (bool): show progress Returns: str: sink regular expression """ # TODO: multiple tiles ! fns = glob.glob(os.path.join(source, "*/*/*")) fns = natsort.natsorted(fns) # print fns io.createDirectory(sink) for i, f in enumerate(fns): fn = filelist.fileExpressionToFileName(sink, i) if verbose: print "%s -> %s" % (f, fn) shutil.copyfile(f, fn) return sink
def resampleDataInverse(sink, source=None, dataSizeSource=None, orientation=None, resolutionSource=(4.0625, 4.0625, 3), resolutionSink=(25, 25, 25), processingDirectory=None, processes=1, cleanup=True, verbose=True, interpolation='linear', **args): """Resample data inversely to :func:`resampleData` routine Arguments: sink (str or None): image to be inversly resampled (=sink in :func:`resampleData`) source (str or array): destination for inversly resmapled image (=source in :func:`resampleData`) dataSizeSource (tuple or None): target size of the resampled image orientation (tuple): orientation specified by permuation and change in sign of (1,2,3) resolutionSource (tuple): resolution of the source image (in length per pixel) resolutionSink (tuple): resolution of the resampled image (in length per pixel) processingDirectory (str or None): directory in which to perform resmapling in parallel, None a temporary directry will be created processes (int): number of processes to use for parallel resampling cleanup (bool): remove temporary files verbose (bool): display progress information interpolation (str): method to use for interpolating to the resmapled image Returns: (array or str): data or file name of resampled image Notes: * resolutions are assumed to be given for the axes of the intrinsic orientation of the data and reference as when viewed by matplotlib or ImageJ * orientation: permuation of 1,2,3 with potential sign, indicating which axes map onto the reference axes, a negative sign indicates reversal of that particular axes * only a minimal set of information to detremine the resampling parameter has to be given, e.g. dataSizeSource and dataSizeSink """ #orientation orientation = fixOrientation(orientation) #assume we can read data fully into memory resampledData = io.readData(sink) dataSizeSink = resampledData.shape if isinstance(dataSizeSource, basestring): dataSizeSource = io.dataSize(dataSizeSource) dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize( dataSizeSource=dataSizeSource, dataSizeSink=dataSizeSink, resolutionSource=resolutionSource, resolutionSink=resolutionSink, orientation=orientation) #print (dataSizeSource, dataSizeSink, resolutionSource, resolutionSink ) dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation) #flip axes back and permute inversely if not orientation is None: if orientation[0] < 0: resampledData = resampledData[::-1, :, :] if orientation[1] < 0: resampledData = resampledData[:, ::-1, :] if orientation[2] < 0: resampledData = resampledData[:, :, ::-1] #reorient peri = inverseOrientation(orientation) peri = orientationToPermuation(peri) resampledData = resampledData.transpose(peri) # upscale in z interpolation = fixInterpolation(interpolation) resampledDataXY = numpy.zeros( (dataSizeSinkI[0], dataSizeSinkI[1], dataSizeSource[2]), dtype=resampledData.dtype) for i in range(dataSizeSinkI[0]): if verbose and i % 25 == 0: print "resampleDataInverse: processing %d/%d" % (i, dataSizeSinkI[0]) #cv2.resize takes reverse order of sizes ! resampledDataXY[i, :, :] = cv2.resize( resampledData[i, :, :], (dataSizeSource[2], dataSizeSinkI[1]), interpolation=interpolation) # upscale x, y in parallel if io.isFileExpression(source): files = source else: if processingDirectory == None: processingDirectory = tempfile.mkdtemp() files = os.path.join(sink[0], 'resample_\d{4}.tif') io.writeData(files, resampledDataXY) nZ = dataSizeSource[2] pool = multiprocessing.Pool(processes=processes) argdata = [] for i in range(nZ): argdata.append((source, fl.fileExpressionToFileName(files, i), dataSizeSource, interpolation, i, nZ)) pool.map(_resampleXYParallel, argdata) if io.isFileExpression(source): return source else: data = io.convertData(files, source) if cleanup: shutil.rmtree(processingDirectory) return data
def resampleDataInverse(sink, source = None, dataSizeSource = None, orientation = None, resolutionSource = (4.0625, 4.0625, 3), resolutionSink = (25, 25, 25), processingDirectory = None, processes = 1, cleanup = True, verbose = True, interpolation = 'linear', **args): """Resample data inversely to :func:`resampleData` routine Arguments: sink (str or None): image to be inversly resampled (=sink in :func:`resampleData`) source (str or array): destination for inversly resmapled image (=source in :func:`resampleData`) dataSizeSource (tuple or None): target size of the resampled image orientation (tuple): orientation specified by permuation and change in sign of (1,2,3) resolutionSource (tuple): resolution of the source image (in length per pixel) resolutionSink (tuple): resolution of the resampled image (in length per pixel) processingDirectory (str or None): directory in which to perform resmapling in parallel, None a temporary directry will be created processes (int): number of processes to use for parallel resampling cleanup (bool): remove temporary files verbose (bool): display progress information interpolation (str): method to use for interpolating to the resmapled image Returns: (array or str): data or file name of resampled image Notes: * resolutions are assumed to be given for the axes of the intrinsic orientation of the data and reference as when viewed by matplotlib or ImageJ * orientation: permuation of 1,2,3 with potential sign, indicating which axes map onto the reference axes, a negative sign indicates reversal of that particular axes * only a minimal set of information to detremine the resampling parameter has to be given, e.g. dataSizeSource and dataSizeSink """ #orientation orientation = fixOrientation(orientation); #assume we can read data fully into memory resampledData = io.readData(sink); dataSizeSink = resampledData.shape; if isinstance(dataSizeSource, basestring): dataSizeSource = io.dataSize(dataSizeSource); dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize(dataSizeSource = dataSizeSource, dataSizeSink = dataSizeSink, resolutionSource = resolutionSource, resolutionSink = resolutionSink, orientation = orientation); #print (dataSizeSource, dataSizeSink, resolutionSource, resolutionSink ) dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation); #flip axes back and permute inversely if not orientation is None: if orientation[0] < 0: resampledData = resampledData[::-1, :, :]; if orientation[1] < 0: resampledData = resampledData[:, ::-1, :]; if orientation[2] < 0: resampledData = resampledData[:, :, ::-1]; #reorient peri = inverseOrientation(orientation); peri = orientationToPermuation(peri); resampledData = resampledData.transpose(peri); # upscale in z interpolation = fixInterpolation(interpolation); resampledDataXY = numpy.zeros((dataSizeSinkI[0], dataSizeSinkI[1], dataSizeSource[2]), dtype = resampledData.dtype); for i in range(dataSizeSinkI[0]): if verbose and i % 25 == 0: print "resampleDataInverse: processing %d/%d" % (i, dataSizeSinkI[0]) #cv2.resize takes reverse order of sizes ! resampledDataXY[i ,:, :] = cv2.resize(resampledData[i,:,:], (dataSizeSource[2], dataSizeSinkI[1]), interpolation = interpolation); # upscale x, y in parallel if io.isFileExpression(source): files = source; else: if processingDirectory == None: processingDirectory = tempfile.mkdtemp(); files = os.path.join(sink[0], 'resample_\d{4}.tif'); io.writeData(files, resampledDataXY); nZ = dataSizeSource[2]; pool = multiprocessing.Pool(processes=processes); argdata = []; for i in range(nZ): argdata.append( (source, fl.fileExpressionToFileName(files, i), dataSizeSource, interpolation, i, nZ) ); pool.map(_resampleXYParallel, argdata); if io.isFileExpression(source): return source; else: data = io.convertData(files, source); if cleanup: shutil.rmtree(processingDirectory); return data;