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;
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
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
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
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;
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
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