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 resampleData(source, sink=None, orientation=None, dataSizeSink=None, resolutionSource=(4.0625, 4.0625, 3), resolutionSink=(25, 25, 25), processingDirectory=None, processes=1, cleanup=True, verbose=True, interpolation='linear', **args): """Resample data of source in resolution and orientation Arguments: source (str or array): image to be resampled sink (str or None): destination of resampled image orientation (tuple): orientation specified by permuation and change in sign of (1,2,3) dataSizeSink (tuple or None): target size of the resampled image 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 = fixOrientation(orientation) if isinstance(dataSizeSink, basestring): dataSizeSink = io.dataSize(dataSizeSink) #orient actual resolutions onto reference resolution dataSizeSource = io.dataSize(source) dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize( dataSizeSource=dataSizeSource, dataSizeSink=dataSizeSink, resolutionSource=resolutionSource, resolutionSink=resolutionSink, orientation=orientation) dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation) #print dataSizeSource, dataSizeSink, resolutionSource, resolutionSink, dataSizeSinkI #rescale in x y in parallel if processingDirectory == None: processingDirectory = tempfile.mkdtemp() interpolation = fixInterpolation(interpolation) nZ = dataSizeSource[2] pool = multiprocessing.Pool(processes=processes) argdata = [] for i in range(nZ): argdata.append( (source, os.path.join(processingDirectory, 'resample_%04d.tif' % i), dataSizeSinkI, interpolation, i, nZ, verbose)) #print argdata[i] pool.map(_resampleXYParallel, argdata) #rescale in z fn = os.path.join(processingDirectory, 'resample_%04d.tif' % 0) data = io.readData(fn) zImage = numpy.zeros((dataSizeSinkI[0], dataSizeSinkI[1], nZ), dtype=data.dtype) for i in range(nZ): if verbose and i % 10 == 0: print "resampleData; reading %d/%d" % (i, nZ) fn = os.path.join(processingDirectory, 'resample_%04d.tif' % i) zImage[:, :, i] = io.readData(fn) resampledData = numpy.zeros(dataSizeSinkI, dtype=zImage.dtype) for i in range(dataSizeSinkI[0]): if verbose and i % 25 == 0: print "resampleData: processing %d/%d" % (i, dataSizeSinkI[0]) #resampledImage[:, iImage ,:] = scipy.misc.imresize(zImage[:,iImage,:], [resizedZAxisSize, sagittalImageSize[1]] , interp = 'bilinear'); #cv2.resize takes reverse order of sizes ! resampledData[i, :, :] = cv2.resize( zImage[i, :, :], (dataSizeSinkI[2], dataSizeSinkI[1]), interpolation=interpolation) #resampledData[i ,:, :] = cv2.resize(zImage[i,:, :], (dataSize[1], resizedZSize)); #account for using (z,y,x) array representation -> (y,x,z) #resampledData = resampledData.transpose([1,2,0]); #resampledData = resampledData.transpose([2,1,0]); if cleanup: shutil.rmtree(processingDirectory) if not orientation is None: #reorient per = orientationToPermuation(orientation) resampledData = resampledData.transpose(per) #reverse orientation after permuting e.g. (-2,1) brings axis 2 to first axis and we can reorder there if orientation[0] < 0: resampledData = resampledData[::-1, :, :] if orientation[1] < 0: resampledData = resampledData[:, ::-1, :] if orientation[2] < 0: resampledData = resampledData[:, :, ::-1] #bring back from y,x,z to z,y,x #resampledImage = resampledImage.transpose([2,0,1]); if verbose: print "resampleData: resampled data size: " + str(resampledData.shape) if sink == []: if io.isFileExpression(source): sink = os.path.split(source) sink = os.path.join(sink[0], 'resample_\d{4}.tif') elif isinstance(source, basestring): sink = source + '_resample.tif' else: raise RuntimeError( 'resampleData: automatic sink naming not supported for non string source!' ) return io.writeData(sink, resampledData)
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 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, check = False): 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, check = False): # 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;
def resampleData(source, sink = None, orientation = None, dataSizeSink = None, resolutionSource = (4.0625, 4.0625, 3), resolutionSink = (25, 25, 25), processingDirectory = None, processes = 1, cleanup = True, verbose = True, interpolation = 'linear', **args): """Resample data of source in resolution and orientation Arguments: source (str or array): image to be resampled sink (str or None): destination of resampled image orientation (tuple): orientation specified by permuation and change in sign of (1,2,3) dataSizeSink (tuple or None): target size of the resampled image 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 = fixOrientation(orientation); if isinstance(dataSizeSink, basestring): dataSizeSink = io.dataSize(dataSizeSink); #orient actual resolutions onto reference resolution dataSizeSource = io.dataSize(source); dataSizeSource, dataSizeSink, resolutionSource, resolutionSink = resampleDataSize(dataSizeSource = dataSizeSource, dataSizeSink = dataSizeSink, resolutionSource = resolutionSource, resolutionSink = resolutionSink, orientation = orientation); dataSizeSinkI = orientDataSizeInverse(dataSizeSink, orientation); #print dataSizeSource, dataSizeSink, resolutionSource, resolutionSink, dataSizeSinkI #rescale in x y in parallel if processingDirectory == None: processingDirectory = tempfile.mkdtemp(); interpolation = fixInterpolation(interpolation); nZ = dataSizeSource[2]; pool = multiprocessing.Pool(processes=processes); argdata = []; for i in range(nZ): argdata.append( (source, os.path.join(processingDirectory, 'resample_%04d.tif' % i), dataSizeSinkI, interpolation, i, nZ, verbose) ); #print argdata[i] pool.map(_resampleXYParallel, argdata); #rescale in z fn = os.path.join(processingDirectory, 'resample_%04d.tif' % 0); data = io.readData(fn); zImage = numpy.zeros((dataSizeSinkI[0], dataSizeSinkI[1], nZ), dtype = data.dtype); for i in range(nZ): if verbose and i % 10 == 0: print "resampleData; reading %d/%d" % (i, nZ); fn = os.path.join(processingDirectory, 'resample_%04d.tif' % i); zImage[:,:, i] = io.readData(fn); resampledData = numpy.zeros(dataSizeSinkI, dtype = zImage.dtype); for i in range(dataSizeSinkI[0]): if verbose and i % 25 == 0: print "resampleData: processing %d/%d" % (i, dataSizeSinkI[0]) #resampledImage[:, iImage ,:] = scipy.misc.imresize(zImage[:,iImage,:], [resizedZAxisSize, sagittalImageSize[1]] , interp = 'bilinear'); #cv2.resize takes reverse order of sizes ! resampledData[i ,:, :] = cv2.resize(zImage[i,:,:], (dataSizeSinkI[2], dataSizeSinkI[1]), interpolation = interpolation); #resampledData[i ,:, :] = cv2.resize(zImage[i,:, :], (dataSize[1], resizedZSize)); #account for using (z,y,x) array representation -> (y,x,z) #resampledData = resampledData.transpose([1,2,0]); #resampledData = resampledData.transpose([2,1,0]); if cleanup: shutil.rmtree(processingDirectory); if not orientation is None: #reorient per = orientationToPermuation(orientation); resampledData = resampledData.transpose(per); #reverse orientation after permuting e.g. (-2,1) brings axis 2 to first axis and we can reorder there if orientation[0] < 0: resampledData = resampledData[::-1, :, :]; if orientation[1] < 0: resampledData = resampledData[:, ::-1, :]; if orientation[2] < 0: resampledData = resampledData[:, :, ::-1]; #bring back from y,x,z to z,y,x #resampledImage = resampledImage.transpose([2,0,1]); if verbose: print "resampleData: resampled data size: " + str(resampledData.shape) if sink == []: if io.isFileExpression(source): sink = os.path.split(source); sink = os.path.join(sink[0], 'resample_\d{4}.tif'); elif isinstance(source, basestring): sink = source + '_resample.tif'; else: raise RuntimeError('resampleData: automatic sink naming not supported for non string source!'); return io.writeData(sink, resampledData);