-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
208 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
# import gevent.monkey | ||
#from gevent import monkey | ||
#monkey.patch_all(thread=True) | ||
|
||
from .__version__ import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
|
||
__version__ = "1.0.9" | ||
version = "1.0.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import Union | ||
|
||
import numpy as np | ||
|
||
from cloudvolume import CloudVolume | ||
from .lib.bounding_boxes import BoundingBox, Cartesian | ||
from .chunk import Chunk | ||
|
||
|
||
class Volume: | ||
"""The major difference with CloudVolume is that we use C order here. | ||
ZYX indexing. | ||
Args: | ||
CloudVolume (class): the cloud-volume class | ||
""" | ||
def __init__(self, vol: CloudVolume) -> None: | ||
self.vol = vol | ||
|
||
@property | ||
def dtype(self): | ||
return self.vol.dtype | ||
|
||
@classmethod | ||
def from_numpy(cls, arr: np.ndarray, vol_path: str): | ||
vol = CloudVolume.from_numpy(np.transpose(arr), vol_path=vol_path) | ||
return cls(vol) | ||
|
||
def cutout(self, key: Union[BoundingBox, list]): | ||
if isinstance(key, BoundingBox): | ||
chunk = self.vol[ key.to_slices()[::-1] ] | ||
voxel_offset = key.start | ||
elif isinstance(key, list): | ||
chunk = self.vol[key[::-1]] | ||
voxel_offset = Cartesian(key[0].start, key[1].start, key[2].start) | ||
else: | ||
raise ValueError('we only support BoundingBox or a list of slices') | ||
|
||
# transpose | ||
chunk = np.transpose(chunk) | ||
chunk = Chunk(np.asarray(chunk), voxel_offset=voxel_offset) | ||
return chunk | ||
|
||
def _auto_convert_dtype(self, chunk: Chunk): | ||
"""convert the data type to fit volume datatype""" | ||
if np.issubdtype(self.dtype, np.floating) and \ | ||
np.issubdtype(chunk.dtype, np.uint8): | ||
chunk = chunk.astype(self.dtype) | ||
chunk /= 255. | ||
# chunk = chunk / chunk.array.max() * np.iinfo(volume.dtype).max | ||
elif np.issubdtype(self.dtype, np.uint8) and \ | ||
np.issubdtype(chunk.dtype, np.floating): | ||
chunk.max() <= 1. | ||
chunk *= 255 | ||
|
||
if self.dtype != chunk.dtype: | ||
print(f'converting chunk data type {chunk.dtype} ' + | ||
f'to volume data type: {self.dtype}') | ||
return chunk.astype(self.dtype) | ||
else: | ||
return chunk | ||
|
||
def save(self, chunk: Chunk): | ||
chunk = self._auto_convert_dtype(chunk) | ||
|
||
# transpose czyx to xyzc order | ||
arr = np.transpose(chunk.array) | ||
self[chunk.slices[::-1]] = arr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import shutil | ||
|
||
import numpy as np | ||
|
||
from cloudvolume.lib import generate_random_string | ||
from chunkflow.lib.bounding_boxes import BoundingBox, Cartesian | ||
from chunkflow.volume import Volume | ||
|
||
|
||
def test_volume(): | ||
print('test volume cutout...') | ||
# compute parameters | ||
mip = 0 | ||
size = (36, 448, 448) | ||
|
||
# create image dataset using cloud-volume | ||
img = np.random.randint(0, 256, size=size) | ||
img = img.astype(np.uint8) | ||
# save the input to disk | ||
volume_path = 'file:///tmp/test/volume/' + \ | ||
generate_random_string() | ||
|
||
vol = Volume.from_numpy( | ||
img, | ||
volume_path | ||
) | ||
|
||
offset = Cartesian(4, 64, 64) | ||
shape = (28, 320, 320) | ||
bbox = BoundingBox.from_delta(offset, shape) | ||
chunk = vol.cutout(bbox) | ||
chunk = chunk.squeeze_channel() | ||
|
||
assert offset == chunk.voxel_offset | ||
np.testing.assert_array_equal(chunk, img[4:-4, 64:-64, 64:-64]) | ||
|
||
shutil.rmtree('/tmp/test') |