本文整理汇总了Python中ClearMap.IO.writeData方法的典型用法代码示例。如果您正苦于以下问题:Python IO.writeData方法的具体用法?Python IO.writeData怎么用?Python IO.writeData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ClearMap.IO
的用法示例。
在下文中一共展示了IO.writeData方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: writeData
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def writeData(filename, data, startIndex=0):
"""Write image stack to single or multiple image files
Arguments:
filename (str): file name as regular expression
data (array): image data
startIndex (int): index of first z-slice
Returns:
str: file name as regular expression
"""
# create directory if not exsits
io.createDirectory(filename)
# check for the \d{xx} part of the regular expression -> if not assume file header
(fileheader, fileext, digitfrmt) = splitFileExpression(filename)
d = len(data.shape)
if d == 2:
fname = fileheader + (digitfrmt % startIndex) + fileext
io.writeData(fname, data)
return fname
else:
nz = data.shape[2]
for i in range(nz):
fname = fileheader + (digitfrmt % (i + startIndex)) + fileext
io.writeData(fname, data[:, :, i])
return filename
示例2: openData3D
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def openData3D(dataSource, x = all, y = all, z = all, cleanUp = True):
"""Open image in ImageJ
Arguments:
dataSouce (str or array): volumetric image data
x, y, z (all or tuple): sub-range specification
inverse (bool):invert image
Returns:
(object): figure handle
"""
checkImageJInitialized();
if isinstance(dataSource, numpy.ndarray):
filename = tempfile.mktemp(suffix = '.mhd', prefix = 'CM_ImageJ');
io.writeData(filename, dataSource, x = x, y = y, z = z);
filepath, imagename = os.path.split(filename);
imagename = imagename[:-4] + '.raw';
temp = True;
dSize = dataSource.shape;
else:
filename = dataSource;
filepath, imagename = os.path.split(filename);
temp = False;
if len(dSize) == 4:
colorImage = True;
else:
colorImage = False;
if colorImage:
macro = ('open("%s"); ' % filename) + \
'run("Stack to Hyperstack...", "order=xyzct channels=%d slices=%d frames=1 display=Color"); ' % (dSize[3], dSize[2]) + \
'Stack.setDisplayMode("composite"); ' + \
'run("3D Viewer"); call("ij3d.ImageJ3DViewer.setCoordinateSystem", "false"); ' + \
'call("ij3d.ImageJ3DViewer.add", "%s", "None", "%s", "0", "true", "true", "true", "1", "0");' % (imagename, imagename);
else:
macro = ('open("%s");' % filename) + ' run("RGB Color"); run("3D Viewer"); call("ij3d.ImageJ3DViewer.setCoordinateSystem", "false"); ' + \
'call("ij3d.ImageJ3DViewer.add", "%s", "None", "%s", "0", "true", "true", "true", "1", "0");' % (imagename, imagename);
cmd = ImageJBinary + " -eval '%s'" % macro;
print 'running: %s' % cmd
res = os.system(cmd);
if res != 0:
raise RuntimeError('openData3D: failed executing: ' + cmd);
if cleanUp and temp:
os.remove(filename);
os.remove(os.path.join(filepath, imagename));
return macro;
示例3: writeSubStack
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def writeSubStack(filename, img, subStack = None):
"""Write the non-redundant part of a sub-stack to disk
The routine is used to write out images when porcessed in parallel.
It assumes that the filename is a patterned file name.
Arguments:
filename (str or None): file name pattern as described in
:mod:`~ClearMap.Io.FileList`, if None return as array
img (array): image data of sub-stack
subStack (dict or None): sub-stack information, if None write entire image
see :ref:`SubStack`
Returns:
str or array: the file name pattern or image
"""
if not subStack is None:
ii = subStack["zSubStackCenterIndices"][0];
ee = subStack["zSubStackCenterIndices"][1];
si = subStack["zCenterIndices"][0];
else:
si = 0;
ii = 0;
ee = -1;
return io.writeData(filename, img[:,:,ii:ee], startIndex = si );
示例4: test
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def test():
import os;
import ClearMap.IO as io
import ClearMap.Settings as settings
import ClearMap.ImageProcessing.Ilastik as il;
reload(il);
ilp = os.path.join(settings.ClearMapPath, 'Test/Ilastik/Test.ilp')
src = os.path.join(settings.ClearMapPath, 'Test/Data/ImageAnalysis/cfos-substack.tif');
#out = os.path.join(settings.ClearMapPath, 'Test/Data/Ilastik/image.npy');
out = None;
#out = os.path.join(settings.ClearMapPath, 'Test/Data/Ilastik/result\d*.tif');
cls = il.classifyPixel(ilp, src, out);
print io.dataSize(src)
print cls.shape
io.writeData('/home/ckirst/result.raw', cls);
示例5: _cropParallel
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def _cropParallel(arg):
"""Cropping helper function to use for parallel cropping of image slices"""
fileSource = arg[0]
fileSink = arg[1]
x = arg[2]
y = arg[3]
ii = arg[4]
nn = arg[5]
if ii is not None:
pw = ProcessWriter(ii)
pw.write("cropData: corpping image %d / %d" % (ii, nn))
# pw.write('%s -> %s' % (fileSource, fileSink));
data = io.readData(fileSource, x=x, y=y)
io.writeData(fileSink, data)
示例6: test
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def test():
"""Test Elastix module"""
import ClearMap.Alignment.Elastix as self
reload(self)
from ClearMap.Settings import ClearMapPath;
import os, numpy
p = ClearMapPath;
resultdir = os.path.join(p, 'Test/Elastix/Output');
print 'Searching for transformation parameter file in ' + resultdir;
pf = self.getTransformParameterFile(resultdir)
print 'Found: ' + pf;
#replace path in trasform parameter files:
self.setPathTransformParameterFiles(resultdir)
#initialize
self.initializeElastix('/home/ckirst/programs/elastix')
self.printSettings()
#transform points
pts = numpy.random.rand(5,3);
print 'Transforming points: '
tpts = self.transformPoints(pts, transformParameterFile = pf, indices = False);
print pts
print 'Transformed points: '
print tpts
#deformation and distance fields
df = self.deformationField(transformParameterFile = pf, resultDirectory = None);
#df = '/tmp/elastix_output/deformationField.mhd';
import ClearMap.IO as io
data = io.readData('/tmp/elastix_output/deformationField.mhd');
ds = self.deformationDistance(data);
io.writeData(os.path.join(p, 'Test/Elastix/Output/distances.raw'), ds);
示例7: overlayLabel
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def overlayLabel(dataSource, labelSource, sink = None, alpha = False, labelColorMap = 'jet', x = all, y = all, z = all):
"""Overlay a gray scale image with colored labeled image
Arguments:
dataSouce (str or array): volumetric image data
labelSource (str or array): labeled image to be overlayed on the image data
sink (str or None): destination for the overlayed image
alpha (float or False): transparency
labelColorMap (str or object): color map for the labels
x, y, z (all or tuple): sub-range specification
Returns:
(array or str): figure handle
See Also:
:func:`overlayPoints`
"""
label = io.readData(labelSource, x= x, y = y, z = z);
image = io.readData(dataSource, x= x, y = y, z = z);
lmax = label.max();
if lmax <= 1:
carray = numpy.array([[1,0,0,1]]);
else:
cm = mpl.cm.get_cmap(labelColorMap);
cNorm = mpl.colors.Normalize(vmin=1, vmax = int(lmax));
carray = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm);
carray = carray.to_rgba(numpy.arange(1, int(lmax + 1)));
if alpha == False:
carray = numpy.concatenate(([[0,0,0,1]], carray), axis = 0);
else:
carray = numpy.concatenate(([[1,1,1,1]], carray), axis = 0);
cm = mpl.colors.ListedColormap(carray);
carray = cm(label);
carray = carray.take([0,1,2], axis = -1);
if alpha == False:
cimage = (label == 0) * image;
cimage = numpy.repeat(cimage, 3);
cimage = cimage.reshape(image.shape + (3,));
cimage = cimage.astype(carray.dtype);
cimage += carray;
else:
cimage = numpy.repeat(image, 3);
cimage = cimage.reshape(image.shape + (3,));
cimage = cimage.astype(carray.dtype);
cimage *= carray;
return io.writeData(sink, cimage);
示例8: makeColorAnnotations
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def makeColorAnnotations(filename, labeledImage=None):
if labeledImage is None:
labeledImage = DefaultLabeledImageFile
li = io.readData(labeledImage)
dsize = li.shape
lr = numpy.zeros(dsize, dtype=numpy.uint8)
lg = lr.copy()
lb = lr.copy()
global Label
maxlabel = max(Label.ids)
colarray = numpy.zeros((maxlabel, 3))
for i in Label.ids:
colarray[i - 1, :] = Label.color(i)
for i in Label.ids:
ll = li == i
lr[ll] = colarray[i - 1, 0]
lg[ll] = colarray[i - 1, 1]
lb[ll] = colarray[i - 1, 2]
io.writeData(filename + "_r.tif", lr)
io.writeData(filename + "_g.tif", lg)
io.writeData(filename + "_b.tif", lb)
return (lr, lg, lb)
示例9: voxelize
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def voxelize(points, dataSize = None, sink = None, voxelizeParameter = None, method = 'Spherical', size = (5,5,5), weights = None):
"""Converts a list of points into an volumetric image array
Arguments:
points (array): point data array
dataSize (tuple): size of final image
sink (str, array or None): the location to write or return the resulting voxelization image, if None return array
voxelizeParameter (dict):
========== ==================== ===========================================================
Name Type Descritption
========== ==================== ===========================================================
*method* (str or None) method for voxelization: 'Spherical', 'Rectangular' or 'Pixel'
*size* (tuple) size parameter for the voxelization
*weights* (array or None) weights for each point, None is uniform weights
========== ==================== ===========================================================
Returns:
(array): volumetric data of smeared out points
"""
if dataSize is None:
dataSize = tuple(int(math.ceil(points[:,i].max())) for i in range(points.shape[1]));
elif isinstance(dataSize, basestring):
dataSize = io.dataSize(dataSize);
points = io.readPoints(points);
if method.lower() == 'spherical':
if weights is None:
data = vox.voxelizeSphere(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2]);
else:
data = vox.voxelizeSphereWithWeights(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2], weights);
elif method.lower() == 'rectangular':
if weights is None:
data = vox.voxelizeRectangle(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2]);
else:
data = vox.voxelizeRectangleWithWeights(points.astype('float'), dataSize[0], dataSize[1], dataSize[2], size[0], size[1], size[2], weights);
elif method.lower() == 'pixel':
data = voxelizePixel(points, dataSize, weights);
else:
raise RuntimeError('voxelize: mode: %s not supported!' % method);
return io.writeData(sink, data);
示例10: overlayPoints
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def overlayPoints(dataSource, pointSource, sink = None, pointColor = [1,0,0], x = all, y = all, z = all):
"""Overlay points on 3D data and return as color image
Arguments:
dataSouce (str or array): volumetric image data
pointSource (str or array): point data to be overlayed on the image data
pointColor (array): RGB color for the overlayed points
x, y, z (all or tuple): sub-range specification
Returns:
(str or array): image overlayed with points
See Also:
:func:`overlayLabel`
"""
data = io.readData(dataSource, x = x, y = y, z = z);
points = io.readPoints(pointSource, x = x, y = y, z = z, shift = True);
#print data.shape
if not pointColor is None:
dmax = data.max(); dmin = data.min();
if dmin == dmax:
dmax = dmin + 1;
cimage = numpy.repeat( (data - dmin) / (dmax - dmin), 3);
cimage = cimage.reshape(data.shape + (3,));
if data.ndim == 2:
for p in points: # faster version using voxelize ?
cimage[p[0], p[1], :] = pointColor;
elif data.ndim == 3:
for p in points: # faster version using voxelize ?
cimage[p[0], p[1], p[2], :] = pointColor;
else:
raise RuntimeError('overlayPoints: data dimension %d not suported' % data.ndim);
else:
cimage = vox.voxelize(points, data.shape, method = 'Pixel');
cimage = cimage.astype(data.dtype) * data.max();
data.shape = data.shape + (1,);
cimage.shape = cimage.shape + (1,);
cimage = numpy.concatenate((data, cimage), axis = 3);
#print cimage.shape
return io.writeData(sink, cimage);
示例11: deformationDistance
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def deformationDistance(deformationField, sink = None, scale = None):
"""Compute the distance field from a deformation vector field
Arguments:
deformationField (str or array): source of the deformation field determined by :func:`deformationField`
sink (str or None): image sink to save the deformation field to
scale (tuple or None): scale factor for each dimension, if None = (1,1,1)
Returns:
array or str: array or file name of the transformed data
"""
deformationField = io.readData(deformationField);
df = numpy.square(deformationField);
if not scale is None:
for i in range(3):
df[:,:,:,i] = df[:,:,:,i] * (scale[i] * scale[i]);
return io.writeData(sink, numpy.sqrt(numpy.sum(df, axis = 3)));
示例12: transformData
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
def transformData(source, sink = [], transformParameterFile = None, transformDirectory = None, resultDirectory = None):
"""Transform a raw data set to reference using the elastix alignment results
If the map determined by elastix is
:math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`,
transformix on data works as :math:`T^{-1}(\\mathrm{data})`.
Arguments:
source (str or array): image source to be transformed
sink (str, [] or None): image sink to save transformed image to. if [] return the default name of the data file generated by transformix.
transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory.
transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given.
resultDirectory (str or None): the directorty for the transformix results
Returns:
array or str: array or file name of the transformed data
"""
global TransformixBinary;
if isinstance(source, numpy.ndarray):
imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif');
io.writeData(source, imgname);
elif isinstance(source, basestring):
if io.dataFileNameToType(source) == "TIF":
imgname = source;
else:
imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif');
io.transformData(source, imgname);
else:
raise RuntimeError('transformData: source not a string or array');
if resultDirectory == None:
resultdirname = os.path.join(tempfile.tempdir, 'elastix_output');
else:
resultdirname = resultDirectory;
if not os.path.exists(resultdirname):
os.makedirs(resultdirname);
if transformParameterFile == None:
if transformDirectory == None:
raise RuntimeError('neither alignment directory and transformation parameter file specified!');
transformparameterdir = transformDirectory
transformParameterFile = getTransformParameterFile(transformparameterdir);
else:
transformparameterdir = os.path.split(transformParameterFile);
transformparameterdir = transformparameterdir[0];
#transform
#make path in parameterfiles absolute
setPathTransformParameterFiles(transformparameterdir);
#transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt
cmd = TransformixBinary + ' -in ' + imgname + ' -out ' + resultdirname + ' -tp ' + transformParameterFile;
res = os.system(cmd);
if res != 0:
raise RuntimeError('transformData: failed executing: ' + cmd);
if not isinstance(source, basestring):
os.remove(imgname);
if sink == []:
return getResultDataFile(resultdirname);
elif sink is None:
resultfile = getResultDataFile(resultdirname);
return io.readData(resultfile);
elif isinstance(sink, basestring):
resultfile = getResultDataFile(resultdirname);
return io.convertData(resultfile, sink);
else:
raise RuntimeError('transformData: sink not valid!');
示例13: cropData
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
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)
)
#.........这里部分代码省略.........
示例14:
# 需要导入模块: from ClearMap import IO [as 别名]
# 或者: from ClearMap.IO import writeData [as 别名]
plt.subplot(2,3,5);
rd.plot(ridges, normals, points, image = gray_s);
#%%
res = wgn.detect_contour(gray, level = 140)
for r in res:
plt.plot(*r.T, c = 'm');
#%%
io.writeData('/home/ckirst/test4.tif', gray);
#%% Gaussian kernels for derivatives
sigma = 1.5;
import imageprocessing.ridge_detection as rd;
reload(rd);
n,p,evals,evecs = rd.ridge_points(gray_s, sigma);
cplt.plot([gray, evals[:,:,0], evals[:,:,1], evals[:,:,0]> 0], fig = 3)
cplt.plot([gray, n[:,:,0], n[:,:,1]], fig = 4)