示例#1
0
    def test_writefull(self):
        print("Testing KLB writing to %s" % self.testwrite_filepath)
        if os.path.exists(self.testwrite_filepath):
            print("Skipping writing tests because file %s exists.\n    Please move or delete this file and re-run the tests." % self.testwrite_filepath)
            return

        img = pyklb.readfull(self.testread_filepath)
        pyklb.writefull(img, self.testwrite_filepath, pixelspacing_tczyx=[0.5, 0.5, 5.0])
        self.assertTrue(os.path.exists(self.testwrite_filepath))
        img2 = pyklb.readfull(self.testwrite_filepath)
        self.assertEqual(img.dtype, img2.dtype)
        self.assertEqual(img.shape, img2.shape)
        self.assertEqual(np.mean(img), np.mean(img2))

        header = pyklb.readheader(self.testwrite_filepath)
        self.assertTrue(np.all( header["pixelspacing_tczyx"] == [1, 1, 0.5, 0.5, 5.0] ))
示例#2
0
文件: fileio.py 项目: yooerzf/fish
def _klb_reader(klb_path, roi=None):
    from pyklb import readfull

    if roi is not None:
        raise NotImplementedError

    # pyklb whines if it doesn't get a python string
    return readfull(str(klb_path))
示例#3
0
def read_klb(filename, SP_im = True):
    tmp = readfull(filename)
    if len(tmp.shape) == 2:
        tmp = tmp.reshape((1, ) + tmp.shape)
    if SP_im:
        im = SpatialImage(tmp.transpose(2, 1, 0), copy = False)
        im.voxelsize = readheader(filename).get('pixelspacing_tczyx', [1., ]*5)[:1:-1]
    else:
        im = tmp.transpose(2, 1, 0)
    return im
示例#4
0
    def test_writefull(self):
        print("Testing KLB writing to %s" % self.testwrite_filepath)
        if os.path.exists(self.testwrite_filepath):
            print(
                "Skipping writing tests because file %s exists.\n    Please move or delete this file and re-run the tests."
                % self.testwrite_filepath)
            return

        img = pyklb.readfull(self.testread_filepath)
        pyklb.writefull(img,
                        self.testwrite_filepath,
                        pixelspacing_tczyx=[0.5, 0.5, 5.0])
        self.assertTrue(os.path.exists(self.testwrite_filepath))
        img2 = pyklb.readfull(self.testwrite_filepath)
        self.assertEqual(img.dtype, img2.dtype)
        self.assertEqual(img.shape, img2.shape)
        self.assertEqual(np.mean(img), np.mean(img2))

        header = pyklb.readheader(self.testwrite_filepath)
        self.assertTrue(
            np.all(header["pixelspacing_tczyx"] == [1, 1, 0.5, 0.5, 5.0]))
示例#5
0
def load_volume(fullname_ext):
    '''load volume based on input name and extension'''

    import h5py
    import nibabel
    import numpy as np
    try:
        from skimage.external import tifffile
    except:
        import tifffile
    try:
        import PIL
        import pyklb
    except:
        pass

    try:
        ext = '.' + fullname_ext.split('.', 1)[1]

        if ('.tif' in ext) or ('.tiff' in ext):
            try:
                volume = tifffile.imread(fullname_ext)
            except:
                img = PIL.Image.open(fullname_ext)
                volume = []
                for i in range(img.n_frames):
                    img.seek(i)
                    volume.append(np.array(img).T)
                volume = np.array(volume)
        elif ('.h5' in ext) or ('.hdf5' in ext):
            with h5py.File(fullname_ext, 'r') as file_handle:
                volume = file_handle[list(file_handle.keys())[0]][()]
        elif ('.klb' in ext):
            volume = pyklb.readfull(fullname_ext)
            volume = volume.transpose(0, 2, 1)
        elif ('.nii' in ext) or ('.nii.gz' in ext):
            volume = nibabel.load(fullname_ext).get_data()
        else:
            raise Exception('unknown extension.')

        return volume

    except:
        return None
示例#6
0
def fetch_chunks(vol_paths, chunk_size):
    """ Grabs a chunk from the first octant

    Args:
      vol_paths: list of paths to klb files ordered by t idx - list(str)
        must be of length 3
      chunk_size: size of chunk to take - [z_dim, y_dim, x_dim]

    Returns:
      chunked_vol: array - [z_dim', y_dim', x_dim', t_dim]

    """
    import pyklb
    assert len(vol_paths) == 3
    assert len(chunk_size) == 3

    vols = [pyklb.readfull(vol_path)[:chunk_size[0], :chunk_size[1], :chunk_size[2]] for vol_path in vol_paths]
    vol_stack = np.stack(vols, axis=-1)
    ds_vols = preprocess_vol(vol_stack)
    return ds_vols
示例#7
0
 def _get_partition(self, i: int) -> np.ndarray:
     return self._reorder_axes(klb.readfull(self.uri).squeeze())
outPath = 'W:/SV3/RC_15-06-11/Dme_E2_His2AvRFP_spiderGFP_12-03_20150611_155054.corrected/Results/zarr/'
if not os.path.exists(outPath):
    os.makedirs(outPath)

inPath = 'W:/SV3/RC_15-06-11/Dme_E2_His2AvRFP_spiderGFP_12-03_20150611_155054.corrected/Results/TimeFused/'

t0 = time.time()
for ch in range(2):
    if ch == 0:
        fnames = glob(join(inPath, '*/*CHN00.fusedStack.klb'))
    elif ch == 1:
        fnames = glob(join(inPath, '*/*CHN01.fusedStack.klb'))
    fnames.sort()
    #fnames = fnames[0:20] #Change the number of timepoints in the empty array below accordingly

    sample = klb.readfull(fnames[0])  #Sample image

    #Generate lazy arrays
    lazy_arrays = [dask.delayed(klb.readfull)(fn) for fn in fnames]
    lazy_arrays = [
        da.from_delayed(x, shape=sample.shape, dtype=sample.dtype)
        for x in lazy_arrays
    ]

    #Generate empty object array to organize each chunk that loads the 3D volume
    a = np.empty((2, 2701, 1, 1, 1),
                 dtype=object)  #Dimension of (view,timepoint,Z,Y,X)
    #a = np.empty((2,10,1,1,1), dtype=object) #Dimension of (view,timepoint,Z,Y,X)

    for fn, x in zip(fnames, lazy_arrays):
        view = int(fn[fn.index("_CM") + 3:].split("_")[0])
示例#9
0
        def initial_processing(tuple_name_volume):
            name_volume = tuple_name_volume[1]
            fullname_original = os.path.join(dir_volume,
                                             name_volume + '_original.nii.gz')
            fullname_aligned = os.path.join(dir_volume,
                                            name_volume + '_aligned.nii.gz')
            fullname_aligned_hdf = fullname_aligned.replace('.nii.gz', '.hdf5')
            if os.path.isfile(fullname_original):
                try:
                    volume_original = nibabel.load(
                        fullname_original).get_data()
                    return
                except:
                    pass
            if os.path.isfile(fullname_aligned):
                try:
                    volume_aligned = nibabel.load(fullname_aligned).get_data()
                    return
                except:
                    pass
            if os.path.isfile(fullname_aligned_hdf):
                try:
                    with h5py.File(fullname_aligned_hdf) as file_handle:
                        volume_aligned = file_handle['V3D'][()].T
                        return
                except:
                    pass

            try:
                # load input images
                fullname_input = os.path.join(p.dir_input, name_volume + p.ext)
                if ('.tif' in p.ext) or ('.tiff' in p.ext):
                    try:
                        volume_input = tifffile.imread(fullname_input)
                    except:
                        img = PIL.Image.open(fullname_input)
                        volume_input = []
                        for i in range(img.n_frames):
                            img.seek(i)
                            volume_input.append(np.array(img).T)
                        volume_input = np.array(volume_input)
                elif ('.h5' in p.ext) or ('.hdf5' in p.ext):
                    with h5py.File(fullname_input, 'r') as file_handle:
                        volume_input = file_handle[list(
                            file_handle.keys())[0]][()]
                elif ('.klb' in p.ext):
                    volume_input = pyklb.readfull(fullname_input)
                    volume_input = volume_input.transpose(0, 2, 1)

                if volume_input.ndim == 2:
                    volume_input = volume_input[None, :, :]
                volume_input = volume_input.transpose(2, 1, 0)

                # get dimensions
                lx, ly, lz = volume_input.shape

                # split two-color images into two halves
                if p.n_colors == 2:
                    # ensure that two-frames have even number of y-dim voxels
                    assert (ly % 2 == 0)
                    ly /= 2
                    if color_i == 0:
                        volume_input = volume_input[:, :ly, :]
                    elif color_i == 1:
                        volume_input = volume_input[:, ly:, :]

                # downsample in the x-y if specified
                if p.ds > 1:
                    if (lx % p.ds) or (ly % p.ds):
                        lx -= (lx % p.ds)
                        ly -= (ly % p.ds)
                        volume_input = volume_input[:lx, :ly, :]

                    # make grid for computing downsampled values
                    sx_ds = np.arange(0.5, lx, p.ds)
                    sy_ds = np.arange(0.5, ly, p.ds)
                    xy_grid_ds = np.dstack(
                        np.meshgrid(sx_ds, sy_ds, indexing='ij'))

                    # get downsampled image
                    volume_input_ds = np.zeros((len(sx_ds), len(sy_ds), lz))
                    for zi in np.arange(lz):
                        interpolation_fx = interpolate.RegularGridInterpolator(
                            (np.arange(lx), np.arange(ly)),
                            volume_input[:, :, zi],
                            method='linear')
                        volume_input_ds[:, :,
                                        zi] = interpolation_fx(xy_grid_ds)

                    volume_input = volume_input_ds

                # pad planes as necessary
                if p.registration and p.planes_pad:
                    volume_input = np.lib.pad(
                        volume_input,
                        ((0, 0), (0, 0), (p.planes_pad, p.planes_pad)),
                        'constant',
                        constant_values=(np.percentile(volume_input, 1), ))

                # save image as a nifti file
                nibabel.save(
                    nii_image(volume_input.astype('float32'), p.affine_mat),
                    (fullname_original
                     if p.registration else fullname_aligned))

            except Exception as msg:
                raise Exception('image %s not processed: %s.' %
                                (name_volume, msg))
示例#10
0
 def test_readfull(self):
     img = pyklb.readfull(self.testread_filepath)
     self.assertEqual(np.prod(img.shape), 442279)
     self.assertEqual(round(np.mean(img)), 352)
示例#11
0
文件: fileTools.py 项目: d-v-b/zebra
 def klb_reader(klb_path):
     from pyklb import readfull
     return readfull(klb_path)
示例#12
0
 def test_readfull(self):
     img = pyklb.readfull(self.testread_filepath)
     self.assertEqual(np.prod(img.shape), 442279)
     self.assertEqual(round(np.mean(img)), 352)
示例#13
0
def init_image_process(image_name, image_proc=1):
    try:
        print((image_name + ': start'))

        # load original images
        if '.tif' in image_ext:
            try:
                image_data = tifffile.imread(input_dir + image_name +
                                             image_ext)
            except:
                img = Image.open(input_dir + image_name + image_ext)
                image_data = []
                for i in range(img.n_frames):
                    img.seek(i)
                    image_data.append(np.array(img).T)
                image_data = np.array(image_data)
        elif ('.stack.bz2' in image_ext) or ('.stack.gz' in image_ext):
            tempdir = tempfile.mkdtemp() + '/'
            if '.stack.bz2' in image_ext:
                unzip_cmd = 'bzcat '
            elif '.stack.gz' in image_ext:
                unzip_cmd = 'zcat '
            os.system(unzip_cmd + input_dir + image_name + image_ext + ' > ' +
                      tempdir + 'img.stack')
            image_data = \
                np.fromfile(
                    tempdir + 'img.stack', dtype='int16', count=-1, sep='')\
                .reshape((lz, ly * imageframe_nmbr, lx))
            os.system('rm ' + tempdir + 'img.stack; rmdir ' + tempdir)
        elif '.stack' in image_ext:
            image_data = \
                np.fromfile(
                    input_dir + image_name + image_ext, dtype='int16', count=-1, sep='')\
                .reshape((lz, ly * imageframe_nmbr, lx))
        elif ('.h5' in image_ext) or ('.hdf5' in image_ext):
            with h5py.File(input_dir + image_name + image_ext,
                           'r') as file_handle:
                image_data = file_handle[list(file_handle.keys())[0]][()]
        elif ('.klb' in image_ext):
            image_data = pyklb.readfull(input_dir + image_name + image_ext)
            image_data = image_data.transpose(0, 2, 1)

        if image_data.ndim == 2:
            image_data = image_data[None, :, :]
        image_data = image_data.transpose(2, 1, 0).astype(data_type)

        if not image_proc:
            print('returning dimensions.')
            return image_data.shape

        # split two-color images into two halves
        if imageframe_nmbr == 2:
            if frame_i == 0:
                image_data = image_data[:, :ly, :]
            elif frame_i == 1:
                image_data = image_data[:, ly:, :]

        # ensure original dimensions are even
        if ds > 1:
            if lx % 2: image_data = image_data[:-1, :, :]
            if ly % 2: image_data = image_data[:, :-1, :]

        # downsample in the x-y dimension and pad in the z dimension
        if ds > 1:
            image_data = downsample_xy(image_data)
        if lpad:
            image_data = pad_z('pad', image_data)

        # create image directory and save image as a nifti file
        os.system('mkdir -p ' + image_dir(image_name, frame_i))
        nibabel.save(
            nii_image(image_data.astype(data_type), niiaffmat),
            image_dir(image_name, frame_i) + 'image_original' + nii_ext)

        print((image_name + ': end'))

    except Exception as msg:
        raise Exception(' '.join(
            ['error processing', image_name + ':',
             str(msg)]))
示例#14
0
def extract_coords(vol_path, min_sigma=1, max_sigma=50,
                   chunk_size=(200, 200, 200), overlap=50):
    """ Extract the centroid coordinates of the volume
    Current implementation will almost certainly label the same chunk multiple times

    Args:
      vol_path: path to klb file - str
      min_sigma: size of smallest blob - float
      max_sigma: size of largest blob - float
      chunk_size: size of chunks to process, must be as large as biggest cells - [3] - (z_dim, y_dim, x_dim)

    Returns:
      blob_info: array with info on the N detected blobs - [N, 5] - (x, y, z, sigma, confidence)
    """
    import pyklb
    from skimage.feature import blob_log

    print("Loading vol")
    vol = pyklb.readfull(vol_path)
    print("Loading volume with shape: {}".format(vol.shape))


    def run_vol(vol):
        start_time = time()
        print("Running blob detection")
        # NOTE: as of the writing, skimage still does not support Nd blob detection
        # use my fork instead: https://github.com/rueberger/scikit-image/tree/blob_nd
        # be sure to use the blob_nd branch
        detected_blobs = blob_log(vol, min_sigma=min_sigma, max_sigma=max_sigma)
        print("Blob detection completed in {} seconds".format(time() - start_time))
        return detected_blobs

    chunks, chunk_coords = zip(*list(chunk_generator(vol, overlap=overlap, chunk_size=chunk_size)))

    print("Mapping blob detection over {} chunks".format(len(chunks)))
    # by default spawns n_cpus workers
    pool = Pool()
    chunk_blobs = pool.map(run_vol, chunks)

    detected_blobs = []
    for chunk_blob, chunk_coords in zip(chunk_blobs, chunk_coords):
        if len(chunk_blob) == 0:
            continue

        # do not offset the size of the blob
        chunk_offset = np.concatenate([chunk_coords, [0]])
        detected_blobs.append(chunk_blob + chunk_offset)

    # [n_blobs, 4]
    detected_blobs = np.concatenate(detected_blobs, axis=0)

    if len(detected_blobs) == 0:
        print("No blobs detected, aborting")
        return None

    print("Extracting ")
    confidences = vol[detected_blobs[:, 0].astype(int),
                      detected_blobs[:, 1].astype(int),
                      detected_blobs[:, 2].astype(int)]

    # [n_blobs, 5]
    blob_data = np.concatenate([detected_blobs, confidences[:, np.newaxis]], axis=1)
    return blob_data
示例#15
0
def load_data(filename):
    return pyklb.readfull(filename)