Skip to content

Vec, Bbox, & More

William Silversmith edited this page Oct 16, 2019 · 7 revisions

Check out cloudvolume.lib there's useful goodies in there.

save_images

While CloudVolume provides a save_images method on images returned from its download process, sometimes you want to investigate arbitrary 3D numpy arrays, e.g. for derived data.

import numpy as np
from cloudvolume.lib import save_images

arr = np.random.randint(0, 60000, size=(256,256,256), dtype=np.uint32)
save_images(arr)

The images will by default be saved to ./saved_images/default/default/$boundingbox/.

Vec Class

Vec is just a 1d numpy ndarray, but you can initialize it like so: Vec(1,2,3, dtype=np.uint8), Vec(1,2,3,4), Vec(*array).

It is convenient because it includes x, y, z, w and r, g, b, a as aliases for indices 0, 1, 2, 3. It also supports a few vector operations like dot product vec2.dot(vec2) and euclidean vec.length().

Bbox Class

Represents a 3D bounding box via a bottom-left-outside-the-page minimum point (bbx.minpt) and a top-right-inside-the-page maximum point (bbx.maxpt). minpt and maxpt are both Vec types.

from cloudvolume import Bbox
bbx = Bbox( (0,0,0), (1,1,1) )

A major advantage to using Bbox is that it serves as an seamless intermediary between Precomputed filenames, python slices, and bounding boxes.

bbx = Bbox.from_slices(np.s_[...])
bbx = Bbox.from_filename('0-5_6-17_20-30')
bbx = Bbox.from_list( [ 0, 1, 2, 3, 4, 5, 6 ] )
bbx = Bbox.from_points( [[ 0, 0, 0], ... ] ) # point cloud
img = vol[ bbx ]

with open( bbx.to_filename(), 'w') as f:
   pass

You can also...

  • get the volume: bbx.size3(), bbx.volume()
  • get the center: bbx.center()
  • get dtype bbx.dtype
  • set dtype bbx.astype(np.float32)
  • test for intersections:
    • bbx.contains(pt) => bool
    • bbx.contains_bbox(bbx2) => bool
    • Bbox.intersects(bbx1, bbx2) => bool
    • Bbox.intersection(bbx1, bbx2) => Bbox
  • Expand, shrink, or round to the nearest chunk aligned size
    • bbx2 = bbx.expand_to_chunk_size( (64,64,64), offset=(102,0,1) )
    • bbx2 = bbx.shrink_to_chunk_size( ... )
    • bbx2 = bbx.round_to_chunk_size( ... )

Of course, you can also multiply, divide, add, subtract, check for equality etc.

Misc

  • mkdir make a directory path, no error if it already exists (thread safe)
    • path = mkdir('~/wow/too/cool/')
  • touch create an empty file
    • path = touch('~/omg/a/file.txt')
  • green color text green in terminal
    • print(green("success!"))
  • red color text red in terminal
    • print(red("horrible error message"))
  • yellow color text yellow in terminal
    • print(yellow("this function is deprecated"))