Skip to content

Instantly share code, notes, and snippets.

@chrisroat
Last active December 7, 2019 19:16
Show Gist options
  • Save chrisroat/2024a495710a871fc7484a5071356815 to your computer and use it in GitHub Desktop.
Save chrisroat/2024a495710a871fc7484a5071356815 to your computer and use it in GitHub Desktop.
Example of wrapping dask-array and cloud-volume objects so they will place nice
from cloudvolume import CloudVolume
import dask.array as da
import numpy as np
# Wraps a DaskArray to provide a `tostring` method, as well as providing
# pass-throughs for methods needed in interacting with `cloudvolume`.
class DaskWriteToCvWrap:
def __init__(self, dask_arr):
self.arr = dask_arr
def tostring(self, order='C'):
return self.arr.compute().tostring(order)
@property
def dtype(self):
return self.arr.dtype
@property
def ndim(self):
return self.arr.ndim
@property
def shape(self):
return self.arr.shape
def __getitem__(self, index):
return DaskWriteToCvWrap(self.arr.__getitem__(index))
arr = da.zeros((2,2,2))
arr_wrap = DaskWriteToCvWrap(arr)
info = CloudVolume.create_new_info(num_channels=1,
layer_type='image',
data_type=arr_wrap.dtype,
encoding='raw',
resolution=(1,1,1),
voxel_offset=(0,0,0),
volume_size=arr_wrap.shape)
cv = CloudVolume('file:///tmp/file/cv_direct', info=info)
cv.commit_info()
cv[()] = arr_wrap
cv = CloudVolume.from_numpy(arr_wrap, vol_path='file:///tmp/file/cv_from_numpy')
# Wraps a CloudVolume object to provide `shape` as a tuple, as well as
# pass-throughs for methods needed in interacting with dask.
class CvReadToDaskWrap:
def __init__(self, cloud_volume):
self.cv = cloud_volume
@property
def chunk_size(self):
return self.cv.chunk_size
@property
def shape(self):
return tuple(self.cv.shape)
@property
def dtype(self):
return self.cv.dtype
@property
def ndim(self):
return len(self.shape)
def __getitem__(self, index):
return self.cv.__getitem__(index)
cv = CloudVolume('file:///tmp/file/cv_direct')
cv_wrap = CvReadToDaskWrap(cv)
arr = da.from_array(cv_wrap, chunks=tuple(cv_wrap.chunk_size) + (1,))
arr.compute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment