Example #1
0
def copyData(source, sink, x=None, y=None, z=None, returnMemmap=False):
    """Copy a data file from source to sink
    
    Arguments:
        source (str): file name pattern of source
        sink (str): file name pattern of sink
        returnMemmap (bool): returns the result as an array
    Returns:
        str: file name of the copy
    """
    out_type = io.dataFileNameToType(sink)
    if out_type == 'TIF':
        if isinstance(source, np.memmap) and x == y == y == z == None:
            shutil.copyfile(source.filename, sink)
        else:
            Xsize, Ysize, Zsize = io.dataSize(source)
            # cropped size
            Xsize = io.toDataSize(Xsize, r=x)
            Ysize = io.toDataSize(Ysize, r=y)
            Zsize = io.toDataSize(Zsize, r=z)
            im = io.readData(source, x=x, y=y, z=z)
            out = io.writeData(sink, im, returnMemmap=returnMemmap)

        if returnMemmap:
            return io.readData(sink)
        else:
            return sink
    else:
        raise RuntimeError(
            'copying from TIF to {} not yet supported.'.format(out_type))
Example #2
0
def readDataFiles(filename, x=None, y=None, z=None, **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
Example #3
0
def dataZSize(filename, z=None):
    """Returns z size of data in tif file
    
    Arguments:
        filename (str): file name as regular expression
        z (tuple): z data range specification
    
    Returns:
        int: z data size
    """

    t = tif.TiffFile(filename)

    d2 = t.pages[0].shape
    if len(d2) == 3:
        return io.toDataSize(d2[0], r=z)

    d3 = len(t.pages)
    if d3 > 1:
        return io.toDataSize(d3, r=z)
    else:
        return None
Example #4
0
def dataZSize(filename, z=None, **args):
    """Returns size of data stored as a file list
    
    Arguments:
        filename (str): file name as regular expression
        z (tuple): z data range specification
    
    Returns:
        int: z data size
    """

    fp, fl = readFileList(filename)
    nz = len(fl)
    return io.toDataSize(nz, r=z)
Example #5
0
def dataZSize(filename, z = None, **args):
    """Read data z size from nrrd image
        
    Arguments:
        filename (str): file name as regular expression
        z (tuple): z data range specification
    
    Returns:
        int: z data size
    """
    
    header = readHeader(filename)
    dims = header['sizes']

    if len(dims) > 2:
        return io.toDataSize(dims[2], r = z)
    else:
        return None
Example #6
0
def dataZSize(filename, z = None, **args):
    """Read z data size from raw/mhd image
        
    Arguments:
        filename (str):  imaris file name
        z (tuple or None): range specification
    
    Returns:
        int: raw image z data size
    """  
    
    imr = vtk.vtkMetaImageReader()
    imr.SetFileName(filename)
    imr.Update()

    im = imr.GetOutput()
    dims = im.GetDimensions()
    
    if len(dims) > 2:
        return io.toDataSize(dims[2], r = z)
    else:
        return None
Example #7
0
def copyData(source, sink, processes=1, x=None, y=None, z=None, **kwargs):
    """Copy a data file from source to sink when for entire list of files
    
    Arguments:
        source (str): file name pattern of source
        sink (str): file name pattern of sink
        processes (int): number of processes to be used when writing files in parallel

    Returns:
        str: file name pattern of the copy
    Notes:
        TODO: replace cropData with this. currently still using because cropData is more flexible
        TODO: could simplify by not splitting up the regex files
    """

    if isinstance(source, Path):
        source = source.as_posix()
    if isinstance(sink, Path):
        sink = sink.as_posix()

    fp, fl = readFileList(source,
                          z=z)  # crops is z by only reading files in range
    out_type = io.dataFileNameToType(sink)

    if out_type == 'FileList':
        # setup inputs for pool
        files = []
        z_idx = []
        for i, fn in enumerate(fl):
            files.append(os.path.join(fp, fn))
            z_idx.append(i)

        f_chunks = [files[i::processes] for i in range(processes)]
        z_chunks = [z_idx[i::processes] for i in range(processes)]

        # setup pool
        args = [(sources, z_chunks[i], sink, x, y)
                for i, sources in enumerate(f_chunks)]

        if processes == 1:
            _parallelCopyToFileList(*args)
        else:
            pool = multiprocessing.Pool(processes)
            pool.map(_parallelCopyToFileList, args)
            pool.close()

    elif out_type == 'TIF':
        # get datasize
        Zsize, Ysize, Xsize = io.dataSize(source)
        # cropped size
        Xsize = io.toDataSize(Xsize, r=x)
        Ysize = io.toDataSize(Ysize, r=y)
        Zsize = io.toDataSize(Zsize, r=z)
        # setup inputs for pool
        data_type = getDataType(os.path.join(fp, fl[0]))
        files = [os.path.join(fp, i) for i in fl]
        idxs = list(range(len(files)))
        z_f_chunks = [files[i::processes] for i in range(processes)]
        z_i_chunks = [idxs[i::processes] for i in range(processes)]
        im = io.empty(sink, dtype=data_type, shape=(Zsize, Ysize, Xsize))
        args = [(z_f_chunks[i], idxs, sink, x, y)
                for i, idxs in enumerate(z_i_chunks)]

        # setup pool
        if processes == 1:
            _parallelCopyToTif(*args)
        else:
            pool = multiprocessing.Pool(processes)
            pool.map(_parallelCopyToTif, args)
            pool.close()

    return sink