def _process_training( coords: np.ndarray, targets: np.ndarray, feature_source: H5Features, image_spec: ImageSpec, halfwidth: int, ) -> DataArrays: coords_x, coords_y = coords.T indices_x = world_to_image(coords_x, image_spec.x_coordinates) indices_y = world_to_image(coords_y, image_spec.y_coordinates) patch_reads, mask_reads = patch.patches(indices_x, indices_y, halfwidth, image_spec.width, image_spec.height) npatches = indices_x.shape[0] patchwidth = 2 * halfwidth + 1 con_marray, cat_marray = None, None if feature_source.continuous: con_marray = _direct_read(feature_source.continuous, patch_reads, mask_reads, npatches, patchwidth) if feature_source.categorical: cat_marray = _direct_read(feature_source.categorical, patch_reads, mask_reads, npatches, patchwidth) indices = np.vstack((indices_x, indices_y)).T output = DataArrays(con_marray, cat_marray, targets, coords, indices) return output
def _process_query(indices: np.ndarray, feature_source: H5Features, image_spec: ImageSpec, halfwidth: int) -> DataArrays: indices_x, indices_y = indices.T coords_x = image_to_world(indices_x, image_spec.x_coordinates) coords_y = image_to_world(indices_y, image_spec.y_coordinates) patch_reads, mask_reads = patch.patches(indices_x, indices_y, halfwidth, image_spec.width, image_spec.height) patch_data_slices = _slices_from_patches(patch_reads) npatches = indices_x.shape[0] patchwidth = 2 * halfwidth + 1 con_marray, cat_marray = None, None if feature_source.continuous: con_data_cache = _get_rows(patch_data_slices, feature_source.continuous) con_marray = _cached_read(con_data_cache, feature_source.continuous, patch_reads, mask_reads, npatches, patchwidth) if feature_source.categorical: cat_data_cache = _get_rows(patch_data_slices, feature_source.categorical) cat_marray = _cached_read(cat_data_cache, feature_source.categorical, patch_reads, mask_reads, npatches, patchwidth) coords = np.vstack((coords_x, coords_y)).T output = DataArrays(con_marray, cat_marray, None, coords, indices) return output
def test_patch_20(): """Check patch code in y edge.""" halfwidth = 1 im_width = 5 im_height = 5 image = np.arange((im_height * im_width)).reshape((im_height, im_width)) n = 2 * halfwidth + 1 # 2,0 edge p_data = np.zeros((n, n), dtype=int) - 1 x = np.array([2]) y = np.array([0]) patch_rws, mask_ws = patch.patches(x, y, halfwidth, im_width, im_height) for r in patch_rws: p_data[r.yp, r.xp] = image[r.y, r.x] true_answer = np.array([[-1, -1, -1], [1, 2, 3], [6, 7, 8]], dtype=int) assert np.all(true_answer == p_data)
def test_patch_44(): """Check patch code in outer corner.""" halfwidth = 1 im_width = 5 im_height = 5 image = np.arange((im_height * im_width)).reshape((im_height, im_width)) n = 2 * halfwidth + 1 # 4,4 corner p_data = np.zeros((n, n), dtype=int) - 1 x = np.array([4]) y = np.array([4]) patch_rws, mask_ws = patch.patches(x, y, halfwidth, im_width, im_height) for r in patch_rws: p_data[r.yp, r.xp] = image[r.y, r.x] true_answer = np.array([[18, 19, -1], [23, 24, -1], [-1, -1, -1]], dtype=int) assert np.all(true_answer == p_data)
def test_patch_44_mask(): """Check that patches are correctly created from points in 00 corner.""" halfwidth = 1 im_width = 5 im_height = 5 n = 2 * halfwidth + 1 # 0,0 corner p_mask = np.zeros((n, n), dtype=bool) x = np.array([4]) y = np.array([4]) patch_rws, mask_ws = patch.patches(x, y, halfwidth, im_width, im_height) for r in mask_ws: p_mask[r.yp, r.xp] = True true_answer = np.array( [[False, False, True], [False, False, True], [True, True, True]], dtype=bool) assert np.all(true_answer == p_mask)
def test_patch_00(): """Check that patches are correctly created from points in 00 corner.""" halfwidth = 1 im_width = 5 im_height = 5 image = np.arange((im_height * im_width)).reshape((im_height, im_width)) n = 2 * halfwidth + 1 # 0,0 corner p_data = np.zeros((n, n), dtype=int) - 1 x = np.array([0]) y = np.array([0]) patch_rws, mask_ws = patch.patches(x, y, halfwidth, im_width, im_height) for r in patch_rws: p_data[r.yp, r.xp] = image[r.y, r.x] true_answer = np.array([[-1, -1, -1], [-1, 0, 1], [-1, 5, 6]], dtype=int) assert np.all(true_answer == p_data)