|
| 1 | +from dataclasses import astuple |
1 | 2 | import os |
2 | 3 | import sys |
3 | 4 | import traceback |
4 | 5 |
|
5 | | -from cloudvolume import CloudVolume, VolumeCutout |
| 6 | +from cloudvolume import CloudVolume, VolumeCutout, Bbox |
6 | 7 | import numpy as np |
7 | 8 |
|
8 | 9 | from .bounding_boxes import BoundingBox, boxes_dim_range |
@@ -158,16 +159,26 @@ def remove_volume(self, volume_index: int, destroy_shared: bool = False): |
158 | 159 | def download_volume( |
159 | 160 | self, volume_index: int, bounding_box: BoundingBox, parallel=False |
160 | 161 | ) -> VolumeCutout: |
161 | | - bbox = bounding_box.to_cloudvolume_bbox() |
| 162 | + bbox = bounding_box.to_cloudvolume_bbox().astype(int) |
| 163 | + vol_shape = NGOrder(*bbox.size3(), self.cv.cv.num_channels) |
| 164 | + |
| 165 | + # Limit size of area we are grabbing, in case we go out of bounds. |
| 166 | + dl_box = Bbox.intersection(self.cv.cv.bounds, bbox) |
| 167 | + local_min = [int(start) for start in np.subtract(dl_box.minpt, bbox.minpt)] |
| 168 | + |
| 169 | + local_bounds = np.s_[*[slice(start, stop) for start, stop in |
| 170 | + zip(local_min, np.sum([local_min, dl_box.size3()], axis=0))], |
| 171 | + :] |
162 | 172 |
|
163 | 173 | # Download the bounding box volume |
164 | 174 | if self.use_shared: |
165 | | - vol_shape = NGOrder(*bbox.astype(int).size3(), self.cv.cv.num_channels) |
166 | 175 | volume = self.shm_host.SharedNPArray(vol_shape, np.float32) |
167 | 176 | with volume as volume_data: |
168 | | - volume_data[:] = self.cv.cv.download(bbox, mip=self.mip, parallel=parallel) |
| 177 | + volume_data[:] = 0 # Prob not most efficient but makes math much easier |
| 178 | + volume_data[local_bounds] = self.cv.cv.download(dl_box, mip=self.mip, parallel=parallel) |
169 | 179 | else: |
170 | | - volume = self.cv.cv.download(bbox, mip=self.mip, parallel=parallel) |
| 180 | + volume = np.zeros(astuple(vol_shape)) |
| 181 | + volume[local_bounds] = self.cv.cv.download(dl_box, mip=self.mip, parallel=parallel) |
171 | 182 |
|
172 | 183 | # Store the volume in the cache |
173 | 184 | self.volumes[volume_index] = volume |
|
0 commit comments