def test_downsample_segmentation_4x_z(): case1 = np.array([ [ 0, 1 ], [ 2, 3 ] ]).reshape((2,2,1,1)) # all different case2 = np.array([ [ 0, 0 ], [ 2, 3 ] ]).reshape((2,2,1,1)) # two are same case3 = np.array([ [ 1, 1 ], [ 2, 2 ] ]).reshape((2,2,1,1)) # two groups are same case4 = np.array([ [ 1, 2 ], [ 2, 2 ] ]).reshape((2,2,1,1)) # 3 are the same case5 = np.array([ [ 5, 5 ], [ 5, 5 ] ]).reshape((2,2,1,1)) # all are the same is_255_handled = np.array([ [ 255, 255 ], [ 1, 2 ] ], dtype=np.uint8).reshape((2,2,1,1)) test = lambda case: downsample.countless2d(case)[0][0][0][0] assert test(case1) == 3 # d assert test(case2) == 0 # a==b assert test(case3) == 1 # a==b assert test(case4) == 2 # b==c assert test(case5) == 5 # a==b assert test(is_255_handled) == 255 assert downsample.countless2d(case1).dtype == case1.dtype # 0 0 1 3 # 1 1 6 3 => 1 3 case_odd = np.array([ [ [ [1] ], [ [0] ] ], [ [ [1] ], [ [6] ], ], [ [ [3] ], [ [3] ], ], ]) # all are the same downsamplefn = downsample.method('segmentation') result = downsamplefn(case_odd, (2,2,1)) assert np.array_equal(result, np.array([ [ [ [1] ] ], [ [ [3] ] ] ])) data = np.ones(shape=(1024, 511, 62, 1), dtype=int) result = downsamplefn(data, (2,2,1)) assert result.shape == (512, 256, 62, 1)
def downsample_and_upload(image, bounds, vol, ds_shape, mip=0, axis='z', skip_first=False): ds_shape = min2(vol.volume_size, ds_shape[:3]) # sometimes we downsample a base layer of 512x512 # into underlying chunks of 64x64 which permits more scales underlying_mip = (mip + 1) if (mip + 1) in vol.available_mips else mip underlying_shape = vol.mip_underlying(underlying_mip).astype(np.float32) toidx = {'x': 0, 'y': 1, 'z': 2} preserved_idx = toidx[axis] underlying_shape[preserved_idx] = float('inf') # Need to use ds_shape here. Using image bounds means truncated # edges won't generate as many mip levels fullscales = downsample_scales.compute_plane_downsampling_scales( size=ds_shape, preserve_axis=axis, max_downsampled_size=int(min(*underlying_shape)), ) factors = downsample.scale_series_to_downsample_factors(fullscales) if len(factors) == 0: print( "No factors generated. Image Shape: {}, Downsample Shape: {}, Volume Shape: {}, Bounds: {}" .format(image.shape, ds_shape, vol.volume_size, bounds)) downsamplefn = downsample.method(vol.layer_type) vol.mip = mip if not skip_first: vol[bounds.to_slices()] = image new_bounds = bounds.clone() for factor3 in factors: vol.mip += 1 image = downsamplefn(image, factor3) new_bounds //= factor3 new_bounds.maxpt = new_bounds.minpt + Vec(*image.shape[:3]) vol[new_bounds.to_slices()] = image