本文整理汇总了Python中ClearMap.IO类的典型用法代码示例。如果您正苦于以下问题:Python IO类的具体用法?Python IO怎么用?Python IO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: moveTeraStitcherStackToFileList
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
示例2: fileNameToIlastikOuput
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;
示例3: readData
def readData(filename, x = all, y = all, z = all, resolution = 0, channel = 0, timepoint = 0, **args):
"""Read data from imaris file
Arguments:
filename (str): file name as regular expression
x,y,z (tuple): data range specifications
resolution (int): resolution level
channel (int): color channel
timepoint (int): time point
Returns:
array: image data
"""
f = h5py.File(filename, "r");
dataset = readDataSet(f, resolution = resolution, channel = channel, timepoint = timepoint);
dsize = dataset.shape;
rz = io.toDataRange(dsize[0], r = z);
ry = io.toDataRange(dsize[1], r = y);
rx = io.toDataRange(dsize[2], r = x);
data = dataset[rz[0]:rz[1],ry[0]:ry[1],rx[0]:rx[1]];
data = data.transpose((2,1,0)); # imaris stores files in reverse x,y,z ordering
#data = dataset[x[0]:x[1],y[0]:y[1],z[0]:z[1]];
f.close();
return data;
示例4: readDataFiles
def readDataFiles(filename, x=all, y=all, z=all, **args):
"""Read data from individual images assuming they are the z slices
Arguments:
filename (str): file name as regular expression
x,y,z (tuple): data range specifications
Returns:
array: image data
"""
fpath, fl = readFileList(filename)
nz = len(fl)
# read first image to get data size and type
rz = io.toDataRange(nz, r=z)
sz = io.toDataSize(nz, r=z)
fn = os.path.join(fpath, fl[rz[0]])
img = io.readData(fn, x=x, y=y)
nxy = img.shape
data = numpy.zeros(nxy + (sz,), dtype=img.dtype)
data[:, :, 0] = img
for i in range(rz[0] + 1, rz[1]):
fn = os.path.join(fpath, fl[i])
data[:, :, i - rz[0]] = io.readData(fn, x=x, y=y)
return data
示例5: readData
def readData(filename, x = all, y = all, z = all, **args):
"""Read data from a single tif image or stack
Arguments:
filename (str): file name as regular expression
x,y,z (tuple): data range specifications
Returns:
array: image data
"""
dsize = dataSize(filename);
#print "dsize %s" % str(dsize);
if len(dsize) == 2:
data = tiff.imread(filename, key = 0);
#print "data.shape %s" % str(data.shape);
return io.dataToRange(data.transpose([1,0]), x = x, y = y);
#return io.dataToRange(data, x = x, y = y);
else:
if z is all:
data = tiff.imread(filename);
if data.ndim == 2:
# data = data
data = data.transpose([1,0]);
elif data.ndim == 3:
#data = data.transpose([1,2,0]);
data = data.transpose([2,1,0]);
elif data.ndim == 4: # multi channel image
#data = data.transpose([1,2,0,3]);
data = data.transpose([2,1,0,3]);
else:
raise RuntimeError('readData: dimension %d not supproted!' % data.ndim)
return io.dataToRange(data, x = x, y = y, z = all);
else: #optimize for z ranges
ds = io.dataSizeFromDataRange(dsize, x = x, y = y, z = z);
t = tiff.TiffFile(filename);
p = t.pages[0];
data = numpy.zeros(ds, dtype = p.dtype);
rz = io.toDataRange(dsize[2], r = z);
#print "test"
#print rz;
#print dsize
for i in range(rz[0], rz[1]):
xydata = t.pages[i].asarray();
#data[:,:,i-rz[0]] = io.dataToRange(xydata, x = x, y = y);
data[:,:,i-rz[0]] = io.dataToRange(xydata.transpose([1,0]), x = x, y = y);
return data
示例6: openData3D
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;
示例7: calculateSubStacks
def calculateSubStacks(source, z = all, x = all, y = all, **args):
"""Calculates the chunksize and other info for parallel processing and returns a list of sub-stack objects
The sub-stack information is described in :ref:`SubStack`
Arguments:
source (str): image source
x,y,z (tuple or all): range specifications
processes (int): number of parallel processes
chunkSizeMax (int): maximal size of a sub-stack
chunkSizeMin (int): minial size of a sub-stack
chunkOverlap (int): minimal sub-stack overlap
chunkOptimization (bool): optimize chunck sizes to best fit number of processes
chunkOptimizationSize (bool or all): if True only decrease the chunk size when optimizing
verbose (bool): print information on sub-stack generation
Returns:
list: list of sub-stack objects
"""
#determine z ranges
fs = io.dataSize(source);
zs = fs[2];
zr = io.toDataRange(zs, r = z);
nz = zr[1] - zr[0];
#calculate optimal chunk sizes
nchunks, zranges, zcenters = calculateChunkSize(nz, **args);
#adjust for the zrange
zcenters = [c + zr[0] for c in zcenters];
zranges = [(zc[0] + zr[0], zc[1] + zr[0]) for zc in zranges];
#create substacks
subStacks = [];
indexlo = zr[0];
for i in range(nchunks):
indexhi = int(round(zcenters[i+1]));
if indexhi > zr[1] or i == nchunks - 1:
indexhi = zr[1];
zs = zranges[i][1] - zranges[i][0];
subStacks.append({"stackId" : i, "nStacks" : nchunks,
"source" : source, "x" : x, "y" : y, "z" : zranges[i],
"zCenters" : (zcenters[i], zcenters[i+1]),
"zCenterIndices" : (indexlo, indexhi),
"zSubStackCenterIndices" : (indexlo - zranges[i][0], zs - (zranges[i][1] - indexhi))});
indexlo = indexhi; # + 1;
return subStacks;
示例8: copyData
def copyData(source, sink):
"""Copy a imaris file from source to sink
Arguments:
source (str): file name pattern of source
sink (str): file name pattern of sink
Returns:
str: file name patttern of the copy
"""
io.copyFile(source, sink);
示例9: overlayLabel
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);
示例10: copyData
def copyData(source, sink):
"""Copy an nrrd file from source to sink
Arguments:
source (str): file name pattern of source
sink (str): file name pattern of sink
Returns:
str: file name of the copy
Notes:
Todo: dealt with nrdh header files!
"""
io.copyFile(source, sink);
示例11: joinPoints
def joinPoints(results, subStacks = None, shiftPoints = True, **args):
"""Joins a list of points obtained from processing a stack in chunks
Arguments:
results (list): list of point results from the individual sub-processes
subStacks (list or None): list of all sub-stack information, see :ref:`SubStack`
shiftPoints (bool): if True shift points to refer to origin of the image stack considered
when range specification is given. If False, absolute
position in entire image stack.
Returns:
tuple: joined points, joined intensities
"""
nchunks = len(results);
pointlist = [results[i][0] for i in range(nchunks)];
intensities = [results[i][1] for i in range(nchunks)];
results = [];
resultsi = [];
for i in range(nchunks):
cts = pointlist[i];
cti = intensities[i];
if cts.size > 0:
cts[:,2] += subStacks[i]["z"][0];
iid = numpy.logical_and(subStacks[i]["zCenters"][0] <= cts[:,2] , cts[:,2] < subStacks[i]["zCenters"][1]);
cts = cts[iid,:];
results.append(cts);
if not cti is None:
cti = cti[iid];
resultsi.append(cti);
if results == []:
if not intensities is None:
return (numpy.zeros((0,3)), numpy.zeros((0)));
else:
return numpy.zeros((0,3))
else:
points = numpy.concatenate(results);
if shiftPoints:
points = points + io.pointShiftFromRange(io.dataSize(subStacks[0]["source"]), x = subStacks[0]["x"], y = subStacks[0]["y"], z = 0);
else:
points = points - io.pointShiftFromRange(io.dataSize(subStacks[0]["source"]), x = 0, y = 0, z = subStacks[0]["z"]); #absolute offset is added initially via zranges !
if intensities is None:
return points;
else:
return (points, numpy.concatenate(resultsi));
示例12: fileNameToIlastikOuput
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
示例13: isValidOutputFileName
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
示例14: writePoints
def writePoints(filename, points, indices = True):
"""Write points as elastix/transformix point file
Arguments:
filename (str): file name of the elastix point file.
points (array or str): source of the points.
indices (bool): write as pixel indices or physical coordiantes
Returns:
str : file name of the elastix point file
"""
points = io.readPoints(points);
#points = points[:,[1,0,2]]; # points in ClearMap (y,x,z) -> permute to (x,y,z)
with open(filename, 'w') as pointfile:
if indices:
pointfile.write('index\n')
else:
pointfile.write('point\n')
pointfile.write(str(points.shape[0]) + '\n');
numpy.savetxt(pointfile, points, delimiter = ' ', newline = '\n', fmt = '%.5e')
pointfile.close();
return filename;
示例15: _processSubStack
def _processSubStack(dsr):
"""Helper to process stack in parallel"""
sf = dsr[0];
pp = dsr[1];
sub = dsr[2];
verbose = dsr[3];
timer = Timer();
pw = ProcessWriter(sub["stackId"]);
if verbose:
pw.write("processing substack " + str(sub["stackId"]) + "/" + str(sub["nStacks"]));
pw.write("file = " + sub["source"]);
pw.write("segmentation = " + str(sf));
pw.write("ranges: x,y,z = " + str(sub["x"]) + "," + str(sub["y"]) + "," + str(sub["z"]));
img = io.readData(sub["source"], x = sub["x"], y = sub["y"], z = sub["z"]);
if verbose:
pw.write(timer.elapsedTime(head = 'Reading data of size ' + str(img.shape)));
timer.reset();
seg = sf(img, subStack = sub, out = pw, **pp);
if verbose:
pw.write(timer.elapsedTime(head = 'Processing substack of size ' + str(img.shape)));
return seg;