def readTiff(fileName): """ Read a tiff file into a numpy array Usage: img = readTiff(fileName) """ tiff = TIFFfile(fileName) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)) out = np.concatenate(outList, axis=-1) tiff.close() return out
def read(fileName): """ Script to import tif file from imageJ, usage: zstack = tiff.read(inFileName) PTW 2015/01/29 """ tiff = TIFFfile(fileName) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)[...,np.newaxis]) out = np.concatenate(outList,axis=-1) out = np.rollaxis(out,0,3) out = np.flipud(out) tiff.close() return out
def read(fileName): """ Script to import tif file from imageJ, usage: zstack = tiff.read(inFileName) PTW 2015/01/29 """ tiff = TIFFfile(fileName) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)[..., np.newaxis]) out = np.concatenate(outList, axis=-1) out = np.rollaxis(out, 0, 3) out = np.flipud(out) tiff.close() return out
def read_tif(filename,channel=0): """Read a tif image :Parameters: - `filename` (str) - name of the file to read """ # TIF reader tif = libtiff.TIFF.open(filename) if tif.GetField('ImageDescription'): tif = TIFFfile(filename) arr = tif.get_tiff_array() _data = arr[:].T info_str = tif.get_info() else: i = 1 while not tif.LastDirectory(): i+=1 tif.ReadDirectory() tif.SetDirectory(0) _data = np.zeros((i,)+tif.read_image().shape,dtype=tif.read_image().dtype) for ii,i in enumerate(tif.iter_images()): _data[ii] = i _data = _data.transpose(2, 1, 0) info_str = tif.info() nx, ny, nz = _data.shape # -- prepare metadata dictionnary -- info_dict = dict( filter( lambda x: len(x)==2, (inf.split(':') for inf in info_str.split("\n")) ) ) for k,v in info_dict.iteritems(): info_dict[k] = v.strip() # -- getting the voxelsizes from the tiff image: sometimes # there is a BoundingBox attribute, sometimes there are # XResolution, YResolution, ZResolution or spacing. # the object returned by get_tiff_array has a "get_voxel_sizes()" # method but it fails, so here we go. -- if "BoundingBox" in info_dict: bbox = info_dict["BoundingBox"] xm, xM, ym, yM, zm, zM = map(float,bbox.split()) _vx = (xM-xm)/nx _vy = (yM-ym)/ny _vz = (zM-zm)/nz else: # -- When we have [XYZ]Resolution fields, it describes the # number of voxels per real unit. In SpatialImage we want the # voxelsizes, which is the number of real units per voxels. # So we must invert the result. -- if "XResolution" in info_dict: # --resolution is stored in a [(values, precision)] list-of-one-tuple, or # sometimes as a single number -- xres_str = eval(info_dict["XResolution"]) if isinstance(xres_str, list) and isinstance(xres_str[0], tuple): xres_str = xres_str[0] _vx = float(xres_str[0])/xres_str[1] elif isinstance(xres_str, (int, float)): _vx = float(xres_str) else: _vx = 1. _vx = 1./_vx if _vx != 0 else 1. else: _vx = 1.0 # dumb fallback, maybe we will find something smarter later on if "YResolution" in info_dict: # --resolution is stored in a [(values, precision)] list-of-one-tuple, or # sometimes as a single number -- yres_str = eval(info_dict["YResolution"]) if isinstance(yres_str, list) and isinstance(yres_str[0], tuple): yres_str = yres_str[0] _vy = float(yres_str[0])/yres_str[1] elif isinstance(yres_str, (int, float)): _vy = float(yres_str) else: _vy = 1. _vy = 1./_vy if _vy != 0 else 1. else: _vy = 1.0 # dumb fallback, maybe we will find something smarter later on if "ZResolution" in info_dict: # --resolution is stored in a [(values, precision)] list-of-one-tuple, or # sometimes as a single number -- zres_str = eval(info_dict["ZResolution"]) if isinstance(zres_str, list) and isinstance(zres_str[0], tuple): zres_str = zres_str[0] _vz = float(zres_str[0])/zres_str[1] elif isinstance(zres_str, (int, float)): _vz = float(zres_str) else: _vz = 1. _vz = 1./_vz if _vz != 0 else 1. else: if "spacing" in info_dict: _vz = eval(info_dict["spacing"]) else: _vz = 1.0 # dumb fallback, maybe we will find something smarter later on tif.close() # -- dtypes are not really stored in a compatible way (">u2" instead of uint16) # but we can convert those -- dt = np.dtype(_data.dtype.name) # -- Return a SpatialImage please! -- im = SpatialImage(_data, dtype=dt) im.resolution = _vx,_vy,_vz return im
def loadData(outputFilename, frames=None, ycrop=None, xcrop=None, transpose=False, datatype=np.float64): """ Reads data from a tiff, hdf5, or npy file and returns a numpy array. Parameters ---------- outputFilename : string The absolute or relative location of the particular hdf5 or tiff file to be read in. Filename must end in one of the following extensions ['tif', 'tiff', 'hdf5', 'h5', 'npy', 'lsm']. **If the file is hdf5, loadData assumes the data is in a dataset called \"/raw\"**. frames : array_like A tuple, list, or array containing the beginning and ending indices for the frames to be extracted. i.e. frames=[23,77] will return a numpy array containing the frames 23-76 within the file, 'filename'. xcrop : array_like A tuple, list, or array containing the beginning and ending indices for the columns to be extracted. This allows you to specify a subset or roi of the data to be returned. i.e. xcrop=[0,100] will return a numpy array containing first 100 columns of each frame within the file 'filename'. ycrop : array_like A tuple, list, or array containing the beginning and ending indices for the rows to be extracted. This allows you to specify a subset or roi of the data to be returned. i.e. ycrop=[0,100] will return a numpy array containing first 100 rows of each frame within the file 'filename'. transpose : boolean This specifies whether or not to transpose the last two dimensions of each frame. This might happen when reading and writing data between matlab and python, for example. dataset : numpy.dtype Specify the datatype of the returned numpy array. If dataset is of lower precision than the original data then truncation will take place. Returns ------- filename_data : array The data read from within the file specified in filename Notes ----- - If the file type is hdf5, loadData assumes the data to be read in is stored in a dataset called \"/raw\". - After the data has been read in, a .npy file is created and saved with a filename that specifies the parameters of the modified data. If in the future you wish to read in the same data, the .npy file will be read instead, saving time. Example ------- >>> fname = "data/testHDF5.hdf5" >>> yroi = [175,250] >>> xroi = [100,150] >>> hdfData = loadData(fname, frames=[0,32], ycrop=yroi, xcrop=xroi) >>> hdfData.shape (32, 75, 50) >>> hdfData.dtype dtype('float32') >>> hdfData = loadData(fname, ycrop=yroi, xcrop=xroi, datatype=np.int16) >>> hdfData.shape (544, 75, 50) >>> hdfData.dtype dtype('int16') """ print "-- Loading Data..." print "outputFilename = ", outputFilename filename = outputFilename.rstrip('/') print "filename = ", filename basePath, fName = os.path.split(filename) name, ext = os.path.splitext(fName) if basePath and not os.path.exists(basePath): raise IOError, "Directory does not exist: %s" % (basePath) elif not os.path.exists(filename): raise IOError, "File does not exist: %s" % (fName) npFilenameBase = os.path.join(basePath, name) if not frames is None: f0 = frames[0]; f1 = frames[1] npFilenameBase += "_frames" + str(f0) + "-" + str(f1-1) if not ycrop is None: y0 = ycrop[0]; y1 = ycrop[1] npFilenameBase += "_ycrop" + str(y0) + "-" + str(y1-1) if not xcrop is None: x0 = xcrop[0]; x1 = xcrop[1] npFilenameBase += "_xcrop" + str(x0) + "-" + str(x1-1) if transpose: npFilenameBase += "_T" npFilenameBase += "_" + str(np.dtype(datatype)) # File name for the numpy file np_filename = npFilenameBase + '.npy' # Check if a numpy file already exists for this data if os.path.exists(np_filename): print "\t- Numpy file already exists. Loading %s..." % (np_filename) # If so, load it and be done try: volumeData = np.load(np_filename) except IOError: raise IOError, "Error reading filename: \"%s\"" % filename return volumeData elif ext.lower() in ['.npy']: # If it is a numpy file try: volumeData = np.load(filename) except IOError: raise IOError, "Error reading filename: \"%s\"" % filename # Otherwise check if the data is in hdf5 format elif ext.lower() in ['.h5', '.hdf5']: from h5py import File print "\t- Reading from hdf5 file %s..." % (filename) try: h5File = File(filename, 'r') except IOError: raise IOError, "Error reading filename: \"%s\"" % (filename) volumeData = np.array(h5File["/raw"]) # Then check to see if it is an lsm file elif ext.lower() in ['.lsm']: print "\n\nIN LSM SECTION\n\n" from libtiff import TIFFfile try: tiff = TIFFfile(filename) except IOError: raise IOError, "Error opening lsm file: \"%s\"" % (filename) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)[...,np.newaxis]) out = np.concatenate(outList,axis=-1) out = np.rollaxis(out,0,3) data = np.swapaxes(out[:,:,:,0],0,2) volumeData = np.swapaxes(data,1,2) tiff.close() # Then finally it must a tif file (hopefully)... elif ext.lower() in ['.tif', '.tiff']: print "\t- Reading from tiff file %s..." % (filename) from libtiff import TIFF try: tiff = TIFF.open(filename, 'r') except IOError: raise IOError, "Error opening tiff file: \"%s\"" % (filename) for count,image in enumerate(tiff.iter_images()): image_shape = image.shape volumeData = np.ndarray((count+1, image_shape[0], image_shape[1]), order='c', dtype=np.float64) # DO NOT HARDCODE for count,image in enumerate(tiff.iter_images()): volumeData[count,:,:] = image else: assert False, "The filename must have one of the following extensions [\"h5\", \"hdf5\", \"tif\", \"tiff\", \"npy\"]" if transpose: # For some reason the hdf5 data is transposed when written from matlab... volumeData = np.swapaxes(volumeData,1,2) dims = volumeData.shape if frames is None: f0 = 0; f1 = dims[0] if ycrop is None: y0 = 0; y1 = dims[1] if xcrop is None: x0 = 0; x1 = dims[2] print "made it to here!" finalData = np.array(volumeData[f0:f1,y0:y1,x0:x1], dtype=datatype) print "\t- Saving to numpy data file %s" % (np_filename) # Save it so we don't have to parse the tiff/hdf5 file every time np.save(npFilenameBase, finalData) return finalData
def read_tif(filename,channel=0): """Read a tif image :Parameters: - `filename` (str) - name of the file to read """ # TIF reader tif = libtiff.TIFF.open(filename) if tif.GetField('ImageDescription'): tif = TIFFfile(filename) arr = tif.get_tiff_array() _data = arr[:].T info_str = tif.get_info() else: i = 1 while not tif.LastDirectory(): i+=1 tif.ReadDirectory() tif.SetDirectory(0) _data = np.zeros((i,)+tif.read_image().shape,dtype=tif.read_image().dtype) for ii,i in enumerate(tif.iter_images()): _data[ii] = i _data = _data.transpose(2, 1, 0) info_str = tif.info() nx, ny, nz = _data.shape # -- prepare metadata dictionnary -- info_dict = dict( filter( lambda x: len(x)==2, (inf.split(':') for inf in info_str.split("\n")) ) ) info_dict.update(dict( filter( lambda x: len(x)==2,(inf.split('=') for inf in info_str.split("\n"))) )) for k,v in info_dict.iteritems(): info_dict[k] = v.strip() info_dict.update({'Filename':filename.split('/')[-1]}) print info_dict # -- getting the voxelsizes from the tiff image: sometimes # there is a BoundingBox attribute, sometimes there are # XResolution, YResolution, ZResolution or spacing. # the object returned by get_tiff_array has a "get_voxel_sizes()" # method but it fails, so here we go. -- if "BoundingBox" in info_dict: bbox = info_dict["BoundingBox"] xm, xM, ym, yM, zm, zM = map(float,bbox.split()) _vx = (xM-xm)/nx _vy = (yM-ym)/ny _vz = (zM-zm)/nz else: # -- When we have [XYZ]Resolution fields, it describes the # number of voxels per real unit. In SpatialImage we want the # voxelsizes, which is the number of real units per voxels. # So we must invert the result. -- if "XResolution" in info_dict: # --resolution is stored in a [(values, precision)] list-of-one-tuple, or # sometimes as a single number -- xres_str = eval(info_dict["XResolution"]) if isinstance(xres_str, list) and isinstance(xres_str[0], tuple): xres_str = xres_str[0] _vx = float(xres_str[0])/xres_str[1] elif isinstance(xres_str, (int, float)): _vx = float(xres_str) else: _vx = 1. _vx = 1./_vx if _vx != 0 else 1. else: _vx = 1.0 # dumb fallback, maybe we will find something smarter later on if "YResolution" in info_dict: # --resolution is stored in a [(values, precision)] list-of-one-tuple, or # sometimes as a single number -- yres_str = eval(info_dict["YResolution"]) if isinstance(yres_str, list) and isinstance(yres_str[0], tuple): yres_str = yres_str[0] _vy = float(yres_str[0])/yres_str[1] elif isinstance(yres_str, (int, float)): _vy = float(yres_str) else: _vy = 1. _vy = 1./_vy if _vy != 0 else 1. else: _vy = 1.0 # dumb fallback, maybe we will find something smarter later on if "ZResolution" in info_dict: # --resolution is stored in a [(values, precision)] list-of-one-tuple, or # sometimes as a single number -- zres_str = eval(info_dict["ZResolution"]) if isinstance(zres_str, list) and isinstance(zres_str[0], tuple): zres_str = zres_str[0] _vz = float(zres_str[0])/zres_str[1] elif isinstance(zres_str, (int, float)): _vz = float(zres_str) else: _vz = 1. _vz = 1./_vz if _vz != 0 else 1. else: if "spacing" in info_dict: _vz = eval(info_dict["spacing"]) else: _vz = 1.0 # dumb fallback, maybe we will find something smarter later on tif.close() # -- dtypes are not really stored in a compatible way (">u2" instead of uint16) # but we can convert those -- dt = np.dtype(_data.dtype.name) # -- Return a SpatialImage please! -- im = SpatialImage(_data, dtype=dt) im.resolution = _vx,_vy,_vz im.info = info_dict return im