Exemplo n.º 1
0
def read_dv(path, sanity_check=False):
    """Read a video file with ``dv`` extension.

    Parameters
    ----------
    path : str
        Path of the file to read.
    sanity_check : bool
        Check if the array returned fits with bigfish pipeline.

    Returns
    -------
    video : ndarray
        Video read.

    """
    # check path
    check_parameter(path=str, sanity_check=bool)

    # read video file
    video = mrc.imread(path)

    # check the output video
    # metadata can be read running 'tensor.mrc.info()'
    if sanity_check:
        check_array(video,
                    dtype=[np.uint16, np.int16, np.int32, np.float32],
                    allow_nan=False)

    return video
Exemplo n.º 2
0
def open_dv(route):
    """Opens a DeltaVision .dv image stack, and generates a tuple of 
    settings using mrc

    Args:
        route (string): route for the .dv file being processed

    Returns:
        tuple: complying the order in the Image class, returns the tuple
        of dimensions (TZWXY), as well as pixel size and other features
    """
    # Load the .dv file into memory with mrc
    image = mrc.imread(route)

    # Parse the header
    header = image.Mrc.hdr

    # Parse the number of timepoints and wavelengths
    nt, nw = header.NumTimes, header.NumWaves

    # Parse the number of XY pixels (image resolution)
    nx, ny, nsecs = header.Num

    # Parse the number of z planes
    nz = int(nsecs / nt / nw)

    # Parse the pixel and stack sizes in microns
    pxx, pxy, pxz = header.d[0:3]
    return header.wave[0:4], [pxx, pxy, pxz], [nt, nz, nw, nx, ny], image
Exemplo n.º 3
0
def file_splitter(fname):
    """Context manager that takes care of splitting the file and making sure that
    duplicated data gets cleaned up when done.
    """
    im = mrc.imread(fname)
    header = im.Mrc.header
    namesplit = os.path.splitext(fname)
    try:
        splitfiles = []
        if header.NumWaves == 1:
            # if it's a single channel file, we don't need to split
            wave = header.wave[0]
            otf_config = get_otf_and_config(wave)
            if otf_config is not None:
                splitfiles = [(fname, wave, *otf_config)]
        else:
            # otherwise break out individual wavelenghts
            for c in range(header.NumWaves):
                wave = header.wave[c]
                out = f"{namesplit[0]}-{wave}{namesplit[1]}"
                # assumes channel is the 3rd to last dimension
                data = np.take(im, c, -3)
                otf_config = get_otf_and_config(wave)
                if otf_config is not None:
                    write_single_channel(data, out, header, wave)
                    splitfiles.append((out, wave, *otf_config))
        yield splitfiles
    finally:
        # clean up the split files
        if header.NumWaves > 1:
            print("\nCleaning up extracted channels...")
            for file, b, c, d in splitfiles:
                os.remove(file)
    def load_particle_images(base_dir: str):

        # Load image
        mrc_file = osp.join(base_dir, 'imgdata.mrc')
        imgs = mrc.imread(mrc_file)
        imgs = np.array(imgs)

        # Load rotation angle
        pkl = pickle.load(open(osp.join(base_dir, 'pardata.pkl'), "rb"))
        rot_rad = pkl['i']
        rot_deg = 180 * np.array(rot_rad) / np.pi
        return imgs, rot_deg
Exemplo n.º 5
0
def merge_files(file_list, outfile, delete=False):
    data = [mrc.imread(fname) for fname in file_list]
    waves = [0, 0, 0, 0, 0]
    for i, item in enumerate(data):
        waves[i] = item.Mrc.hdr.wave[0]
    hdr = data[0].Mrc.hdr
    m = mrc.Mrc2(outfile, mode="w")
    array = np.stack(data, -3)
    m.initHdrForArr(array)
    mrc.copyHdrInfo(m.hdr, hdr)
    m.hdr.NumWaves = len(file_list)
    m.hdr.wave = waves
    m.writeHeader()
    m.writeStack(array)
    m.close()
    if delete:
        try:
            [os.remove(f) for f in file_list]
        except Exception:
            pass
    return outfile
def get_mrc(filename):
    assert os.path.isfile(filename)
    v = mrc.imread(filename)
    v = N.swapaxes(v, 0, 2)
    v = N.array(v, dtype=N.float32)
    return v
Exemplo n.º 7
0
def test_reader():
    array = imread(str(dv_file))
    assert array.shape == (2, 17, 128, 128)
    assert array.Mrc.header.dvid == -16224