Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def map2(fn, a, b):
assert len(a) == len(b), "Vector lengths do not match: {} (len {}), {} (len {})".format(
a[:3], len(a), b[:3], len(b))
result = np.empty(len(a))
for i in range(len(result)):
result[i] = fn(a[i], b[i])
if isinstance(a, Vec) or isinstance(b, Vec):
return Vec(*result)
return result
def zeros(cls, shape):
if isinstance(shape, int):
shape = range(shape)
return Vec(*[0 for i in shape])
def size3(self):
return Vec(*(self.maxpt[:3] - self.minpt[:3]), dtype=self.dtype)
def __init__(self, a, b, dtype=None):
if dtype is None:
if floating(a) or floating(b):
dtype = np.float32
else:
dtype = np.int32
self.minpt = Vec(*[min(ai, bi) for ai, bi in zip(a, b)], dtype=dtype)
self.maxpt = Vec(*[max(ai, bi) for ai, bi in zip(a, b)], dtype=dtype)
self._dtype = np.dtype(dtype)
def round_to_chunk_size(self, chunk_size, offset=Vec(0, 0, 0, dtype=int)):
"""
Align a potentially non-axis aligned bbox to the grid by rounding it
to the nearest grid lines.
Required:
chunk_size: arraylike (x,y,z), the size of chunks in the
dataset e.g. (64,64,64)
Optional:
offset: arraylike (x,y,z), the starting coordinate of the dataset
"""
chunk_size = np.array(chunk_size, dtype=np.float32)
result = self.clone()
result = result - offset
result.minpt = np.round(result.minpt / chunk_size) * chunk_size
result.maxpt = np.round(result.maxpt / chunk_size) * chunk_size
return (result + offset).astype(self.dtype)
def generate_chunks(img, offset, chunk_size):
shape = Vec(*img.shape)
offset = Vec(*offset)
bounds = Bbox(offset, shape + offset)
alignment_check = bounds.round_to_chunk_size(
chunk_size, Vec.zeros(*chunk_size))
if not np.all(alignment_check.minpt == bounds.minpt):
raise AlignmentError("""
Only chunk aligned writes are supported by this function.
Got: {}
Volume Offset: {}
Nearest Aligned: {}
""".format(
bounds, Vec.zeros(*chunk_size), alignment_check)
)
def generate_chunks(img, offset, chunk_size):
shape = Vec(*img.shape)
offset = Vec(*offset)
bounds = Bbox(offset, shape + offset)
alignment_check = bounds.round_to_chunk_size(
chunk_size, Vec.zeros(*chunk_size))
if not np.all(alignment_check.minpt == bounds.minpt):
raise AlignmentError("""
Only chunk aligned writes are supported by this function.
Got: {}
Volume Offset: {}
Nearest Aligned: {}
""".format(
bounds, Vec.zeros(*chunk_size), alignment_check)
def map2(fn, a, b):
assert len(a) == len(b), "Vector lengths do not match: {} (len {}), {} (len {})".format(
a[:3], len(a), b[:3], len(b))
result = np.empty(len(a))
for i in range(len(result)):
result[i] = fn(a[i], b[i])
if isinstance(a, Vec) or isinstance(b, Vec):
return Vec(*result)
return result
def expand_to_chunk_size(self, chunk_size, offset=Vec(0, 0, 0, dtype=int)):
"""
Align a potentially non-axis aligned bbox to the grid by growing it
to the nearest grid lines.
Required:
chunk_size: arraylike (x,y,z), the size of chunks in the
dataset e.g. (64,64,64)
Optional:
offset: arraylike (x,y,z), the starting coordinate of the dataset
"""
chunk_size = np.array(chunk_size, dtype=np.float32)
result = self.clone()
result = result - offset
result.minpt = np.floor(result.minpt / chunk_size) * chunk_size
result.maxpt = np.ceil(result.maxpt / chunk_size) * chunk_size
return (result + offset).astype(self.dtype)
def __truediv__(self, operand):
tmp = self.clone()
if isinstance(operand, int):
operand = float(operand)
tmp.minpt = Vec(*(tmp.minpt.astype(float) / operand), dtype=float)
tmp.maxpt = Vec(*(tmp.maxpt.astype(float) / operand), dtype=float)
return tmp.astype(tmp.minpt.dtype)