From 3eff3363e0f54c134ea86b6e3c91b587f8633a15 Mon Sep 17 00:00:00 2001 From: Michal Januszewski Date: Thu, 3 Dec 2026 11:18:47 -0800 Subject: [PATCH] `EstimateMissingFlow.process` previously loaded the full image stack before checking whether the subvolume had any unmasked `NaN` flow entries. When most sections already have valid flows or fall inside exterior masks, these unused image reads dominate I/O and memory. Return early before loading image data when all flow entries are valid or masked, and trim `load_box` along Z to active candidate and lookback slices. PiperOrigin-RevId: 1035580336 --- processor/flow.py | 397 +++++++++++++++++++++++++++-------------- processor/flow_test.py | 130 ++++++++++++++ 2 files changed, 395 insertions(+), 132 deletions(-) diff --git a/processor/flow.py b/processor/flow.py index 35f772a..d5ddd29 100644 --- a/processor/flow.py +++ b/processor/flow.py @@ -709,17 +709,24 @@ def _estimate_batch_flows( [jax.device_put(f, sharding) for f in pair_flows], axis=1 ) - def process(self, subvol: Subvolume) -> Subvolume: + _COUNTER_NAMESPACE = 'estimate-missing-flow' + _MAX_DEV_BYTES = 2 * 1024**3 + + def _clip_to_valid_context( + self, + subvol: Subvolume, + image_volume: Any, + ) -> tuple[ + bounding_box.BoundingBox, + np.ndarray, + bounding_box.BoundingBox, + ] | None: + """Clips flow subvolume to entries with full XY image context.""" box = subvol.bbox input_ndarray = subvol.data - namespace = 'estimate-missing-flow' - beam_utils.counter(namespace, 'subvolumes-started').inc() - - image_volume = self._open_volume(self._config.image_volinfo) - + stride = self._config.stride # Bounding box identifying the region of the image for which the input # flow was computed. - stride = self._config.stride full_image_box = bounding_box.BoundingBox( start=( box.start[0] * stride - self._search_patch_size // 2, @@ -738,7 +745,7 @@ def process(self, subvol: Subvolume) -> Subvolume: # Nothing to do if we don't have sufficient image context for any # flow field entries. if np.any(prev_image_box.size[:2] <= self._search_patch_size): - return subvol + return None # Do not process flow field entries for which we do not have sufficient # image context. @@ -750,24 +757,17 @@ def process(self, subvol: Subvolume) -> Subvolume: offset = -((prev_image_box.end - full_image_box.end) // stride) out_box = out_box.adjusted_by(end=-offset) input_ndarray = input_ndarray[:, :, : out_box.size[1], : out_box.size[0]] + return out_box, input_ndarray, prev_image_box - # The input flow forms the initial state of the output. We will try - # to fill-in any invalid (NaN) pixels by computing flow against - # earlier sections. - ret = np.zeros([3] + list(out_box.size[::-1])) - ret[:2, ...] = input_ndarray - ret[2, ...] = self._config.delta_z - - sel_mask = None - if self._config.selection_mask_configs: - sel_mask = self._build_mask(self._config.selection_mask_configs, out_box) - - mfc = flow_field.JAXMaskedXCorrWithStatsCalculator( - max_shift=self._config.max_shift, - use_multi_device=self._config.use_multi_device, - ) - invalid = np.isnan(input_ndarray[0, ...]) - + def _compute_load_geometry( + self, + image_volume: Any, + out_box: bounding_box.BoundingBox, + prev_image_box: bounding_box.BoundingBox, + has_invalid: np.ndarray, + ) -> tuple[range, bounding_box.BoundingBox, tuple[slice, slice]]: + """Computes search deltas, Z-pretrimmed load box, and curr XY slice.""" + stride = self._config.stride patch_size = self._config.patch_size curr_image_box = bounding_box.BoundingBox( start=( @@ -778,25 +778,28 @@ def process(self, subvol: Subvolume) -> Subvolume: size=( (out_box.size[0] - 1) * stride + patch_size, (out_box.size[1] - 1) * stride + patch_size, - invalid.shape[0], + has_invalid.shape[0], ), ) curr_image_box = image_volume.clip_box_to_volume(curr_image_box) assert curr_image_box is not None + invalid_z = np.flatnonzero(has_invalid) + min_inv_z = out_box.start[2] + int(invalid_z[0]) + max_inv_end_z = out_box.start[2] + int(invalid_z[-1]) + 1 if self._config.delta_z > 0: search_deltas = range( self._config.delta_z + 1, self._config.max_delta_z + 1 ) - load_start_z = out_box.start[2] - self._config.max_delta_z - load_end_z = out_box.end[2] + load_start_z = min_inv_z - self._config.max_delta_z + load_end_z = max_inv_end_z else: search_deltas = range( self._config.delta_z - 1, self._config.max_delta_z - 1, -1 ) - load_start_z = out_box.start[2] + load_start_z = min_inv_z # max_delta_z is negative. - load_end_z = out_box.end[2] - self._config.max_delta_z + load_end_z = max_inv_end_z - self._config.max_delta_z load_box = bounding_box.BoundingBox( start=( @@ -811,13 +814,7 @@ def process(self, subvol: Subvolume) -> Subvolume: ), ) load_box = image_volume.clip_box_to_volume(load_box) - - logging.info('Loading image data: %r', load_box) - full_image_stack = image_volume.asarray[load_box.to_slice4d()][0, ...] - full_mask = None - if self._config.mask_configs: - full_mask = self._build_mask(self._config.mask_configs, load_box) - logging.info('Loaded mask: %r', full_mask.shape) + assert load_box is not None # The 'curr' image is a subset of the loaded stack, centered within the # 'prev' image (which includes the search radius). @@ -826,131 +823,267 @@ def process(self, subvol: Subvolume) -> Subvolume: slice(curr_rel_start[1], curr_rel_start[1] + curr_image_box.size[1]), slice(curr_rel_start[0], curr_rel_start[0] + curr_image_box.size[0]), ) + return search_deltas, load_box, curr_slice - max_dev_bytes = 2 * 1024**3 - if full_image_stack.nbytes <= max_dev_bytes: - img_src = jnp.asarray(full_image_stack) - else: - img_src = full_image_stack - - mask_src = None + def _select_active_sections( + self, + ret: np.ndarray, + out_box: bounding_box.BoundingBox, + load_box: bounding_box.BoundingBox, + curr_slice: tuple[slice, slice], + has_invalid: np.ndarray, + sel_mask: np.ndarray | None, + search_deltas: range, + ) -> tuple[ + list[tuple[int, int, np.ndarray, np.ndarray]], + bounding_box.BoundingBox, + np.ndarray | None, + np.ndarray | None, + ]: + """Loads masks, selects active sections, and trims load_box along Z.""" + full_mask = None curr_mask_all = None prev_mask_all = None - if full_mask is not None: + if self._config.mask_configs: + full_mask = self._build_mask(self._config.mask_configs, load_box) + logging.info('Loaded mask: %r', full_mask.shape) curr_mask_all = np.all( full_mask[:, curr_slice[0], curr_slice[1]], axis=(1, 2) ) prev_mask_all = np.all(full_mask, axis=(1, 2)) - if full_mask.nbytes <= max_dev_bytes: - mask_src = jnp.asarray(full_mask) - else: - mask_src = full_mask - - same_xy = self._config.search_radius == 0 and curr_slice == ( - slice(0, load_box.size[1]), - slice(0, load_box.size[0]), - ) # Per-section mutable state: (z, curr_z_idx, attempts, mask). active_sections: list[tuple[int, int, np.ndarray, np.ndarray]] = [] - for z in range(invalid.shape[0]): - if np.all(~invalid[z, ...]): - beam_utils.counter(namespace, 'sections-already-valid').inc() + needed_mask_z: list[int] = [] + for z in range(has_invalid.shape[0]): + if not has_invalid[z]: + beam_utils.counter( + self._COUNTER_NAMESPACE, 'sections-already-valid' + ).inc() continue curr_z_idx = (out_box.start[2] + z) - load_box.start[2] - assert 0 <= curr_z_idx < full_image_stack.shape[0] + assert 0 <= curr_z_idx < load_box.size[2] if curr_mask_all is not None and curr_mask_all[curr_z_idx]: - beam_utils.counter(namespace, 'sections-masked').inc() + beam_utils.counter(self._COUNTER_NAMESPACE, 'sections-masked').inc() continue mask_z = ~np.isfinite(ret[0, z, ...]) if sel_mask is not None: mask_z &= sel_mask[z, ...] + if not np.any(mask_z): + continue + z_needed = [ + curr_z_idx - dz + for dz in search_deltas + if 0 <= curr_z_idx - dz < load_box.size[2] + and (prev_mask_all is None or not prev_mask_all[curr_z_idx - dz]) + ] + if not z_needed: + continue active_sections.append( (z, curr_z_idx, np.zeros(ret.shape[2:], dtype=int), mask_z) ) + needed_mask_z.append(curr_z_idx) + needed_mask_z.extend(z_needed) + + if not active_sections: + return [], load_box, None, None + + z_trim_start = min(needed_mask_z) + z_trim_end = max(needed_mask_z) + 1 + if z_trim_start > 0 or z_trim_end < load_box.size[2]: + load_box = load_box.adjusted_by( + start=(0, 0, z_trim_start), + end=(0, 0, z_trim_end - load_box.size[2]), + ) + if full_mask is not None: + full_mask = full_mask[z_trim_start:z_trim_end] + if prev_mask_all is not None: + prev_mask_all = prev_mask_all[z_trim_start:z_trim_end] + if z_trim_start > 0: + active_sections = [ + (z, curr_z_idx - z_trim_start, attempts_z, mask_z) + for z, curr_z_idx, attempts_z, mask_z in active_sections + ] + return active_sections, load_box, full_mask, prev_mask_all - for delta_z in search_deltas: - next_active = [] - batch_sections = [] - z_pairs: list[tuple[int, int]] = [] - pair_sel_masks: list[np.ndarray] = [] - - for sec in active_sections: - z, curr_z_idx, attempts_z, mask_z = sec - prev_z_idx = curr_z_idx - delta_z - if prev_z_idx < 0 or prev_z_idx >= full_image_stack.shape[0]: - continue - if prev_mask_all is not None and prev_mask_all[prev_z_idx]: - next_active.append(sec) - continue - # Limit estimation attempts per voxel (counted when both sides valid). - mask_z &= attempts_z <= self._config.max_attempts - if not np.any(mask_z): - continue + def _fill_delta_z( + self, + mfc: flow_field.JAXMaskedXCorrWithStatsCalculator, + img_src: np.ndarray | jax.Array, + mask_src: np.ndarray | jax.Array | None, + prev_mask_all: np.ndarray | None, + active_sections: list[tuple[int, int, np.ndarray, np.ndarray]], + delta_z: int, + curr_slice: tuple[slice, slice], + same_xy: bool, + ret: np.ndarray, + ) -> list[tuple[int, int, np.ndarray, np.ndarray]]: + """Estimates and applies missing flows for one lookback delta_z.""" + num_z = img_src.shape[0] + next_active = [] + batch_sections = [] + z_pairs: list[tuple[int, int]] = [] + pair_sel_masks: list[np.ndarray] = [] + + for sec in active_sections: + z, curr_z_idx, attempts_z, mask_z = sec + prev_z_idx = curr_z_idx - delta_z + if prev_z_idx < 0 or prev_z_idx >= num_z: + continue + if prev_mask_all is not None and prev_mask_all[prev_z_idx]: next_active.append(sec) - batch_sections.append(sec) - z_pairs.append((prev_z_idx, curr_z_idx)) - pair_sel_masks.append(mask_z) - active_sections = next_active - - if not z_pairs: continue - - t_start = time.time() - logging.info( - 'Trying delta_z=%d across %d active sections (%d points)', - delta_z, - len(z_pairs), - sum(int(np.sum(m)) for m in pair_sel_masks), + # Limit estimation attempts per voxel (counted when both sides valid). + mask_z &= attempts_z <= self._config.max_attempts + if not np.any(mask_z): + continue + next_active.append(sec) + batch_sections.append(sec) + z_pairs.append((prev_z_idx, curr_z_idx)) + pair_sel_masks.append(mask_z) + + if not z_pairs: + return next_active + + t_start = time.time() + logging.info( + 'Trying delta_z=%d across %d active sections (%d points)', + delta_z, + len(z_pairs), + sum(int(np.sum(m)) for m in pair_sel_masks), + ) + flows_dev = self._estimate_batch_flows( + mfc, + img_src, + mask_src, + z_pairs, + pair_sel_masks, + curr_slice, + same_xy, + ) + t3 = time.time() + valid_dev = jnp.isfinite(flows_dev[0, ...]) + cleaned_dev = flow_utils.clean_flow_jax( + flows_dev, + self._config.min_peak_ratio, + self._config.min_peak_sharpness, + float(self._config.max_magnitude), + max_deviation=0.0, + ) + valid_batch, cleaned_batch = jax.device_get((valid_dev, cleaned_dev)) + t4 = time.time() + + sy, sx = cleaned_batch.shape[2:] + total_updated = 0 + for pair_i, (z, _, attempts_z, mask_z) in enumerate(batch_sections): + valid_i = valid_batch[pair_i] + attempts_z[: valid_i.shape[0], : valid_i.shape[1]][valid_i] += 1 + to_update = mask_z[:sy, :sx] & np.isfinite( + cleaned_batch[0, pair_i, ...] ) - flows_dev = self._estimate_batch_flows( + mask_z[:sy, :sx][to_update] = False + total_updated += int(np.sum(to_update)) + ret[:2, z, :sy, :sx][:, to_update] = cleaned_batch[:2, pair_i, :][ + :, to_update + ] + ret[2, z, :sy, :sx][to_update] = delta_z + beam_utils.counter( + self._COUNTER_NAMESPACE, f'sections-filled-delta{delta_z}' + ).inc(total_updated) + t5 = time.time() + logging.info( + 'delta_z=%d timings: flow:%.2f clean:%.2f update:%.2f' + ' (updated=%d)', + delta_z, + t3 - t_start, + t4 - t3, + t5 - t4, + total_updated, + ) + return next_active + + def _maybe_to_device(self, arr: np.ndarray) -> np.ndarray | jax.Array: + if arr.nbytes <= self._MAX_DEV_BYTES: + return jnp.asarray(arr) + return arr + + def process(self, subvol: Subvolume) -> Subvolume: + beam_utils.counter(self._COUNTER_NAMESPACE, 'subvolumes-started').inc() + image_volume = self._open_volume(self._config.image_volinfo) + + clipped = self._clip_to_valid_context(subvol, image_volume) + if clipped is None: + return subvol + out_box, input_ndarray, prev_image_box = clipped + + # The input flow forms the initial state of the output. We will try + # to fill-in any invalid (NaN) pixels by computing flow against + # earlier sections. + ret = np.zeros([3] + list(out_box.size[::-1])) + ret[:2, ...] = input_ndarray + ret[2, ...] = self._config.delta_z + + invalid = np.isnan(input_ndarray[0, ...]) + has_invalid = np.any(invalid, axis=(1, 2)) + if not np.any(has_invalid): + beam_utils.counter( + self._COUNTER_NAMESPACE, 'sections-already-valid' + ).inc(invalid.shape[0]) + return Subvolume(ret, out_box) + + sel_mask = None + if self._config.selection_mask_configs: + sel_mask = self._build_mask(self._config.selection_mask_configs, out_box) + if not np.any(sel_mask & invalid): + beam_utils.counter( + self._COUNTER_NAMESPACE, 'sections-already-valid' + ).inc(int(np.sum(~has_invalid))) + return Subvolume(ret, out_box) + + search_deltas, load_box, curr_slice = self._compute_load_geometry( + image_volume, out_box, prev_image_box, has_invalid + ) + active_sections, load_box, full_mask, prev_mask_all = ( + self._select_active_sections( + ret, + out_box, + load_box, + curr_slice, + has_invalid, + sel_mask, + search_deltas, + ) + ) + if not active_sections: + return Subvolume(ret, out_box) + + mfc = flow_field.JAXMaskedXCorrWithStatsCalculator( + max_shift=self._config.max_shift, + use_multi_device=self._config.use_multi_device, + ) + logging.info('Loading image data: %r', load_box) + full_image_stack = image_volume.asarray[load_box.to_slice4d()][0, ...] + img_src = self._maybe_to_device(full_image_stack) + mask_src = None if full_mask is None else self._maybe_to_device(full_mask) + same_xy = self._config.search_radius == 0 and curr_slice == ( + slice(0, load_box.size[1]), + slice(0, load_box.size[0]), + ) + + for delta_z in search_deltas: + active_sections = self._fill_delta_z( mfc, img_src, mask_src, - z_pairs, - pair_sel_masks, + prev_mask_all, + active_sections, + delta_z, curr_slice, same_xy, + ret, ) - t3 = time.time() - valid_dev = jnp.isfinite(flows_dev[0, ...]) - cleaned_dev = flow_utils.clean_flow_jax( - flows_dev, - self._config.min_peak_ratio, - self._config.min_peak_sharpness, - float(self._config.max_magnitude), - max_deviation=0.0, - ) - valid_batch, cleaned_batch = jax.device_get((valid_dev, cleaned_dev)) - t4 = time.time() - - sy, sx = cleaned_batch.shape[2:] - total_updated = 0 - for pair_i, (z, _, attempts_z, mask_z) in enumerate(batch_sections): - valid_i = valid_batch[pair_i] - attempts_z[: valid_i.shape[0], : valid_i.shape[1]][valid_i] += 1 - to_update = mask_z[:sy, :sx] & np.isfinite( - cleaned_batch[0, pair_i, ...] - ) - mask_z[:sy, :sx][to_update] = False - total_updated += int(np.sum(to_update)) - ret[:2, z, :sy, :sx][:, to_update] = cleaned_batch[:2, pair_i, :][ - :, to_update - ] - ret[2, z, :sy, :sx][to_update] = delta_z - beam_utils.counter(namespace, f'sections-filled-delta{delta_z}').inc( - total_updated - ) - t5 = time.time() - logging.info( - 'delta_z=%d timings: flow:%.2f clean:%.2f update:%.2f' - ' (updated=%d)', - delta_z, - t3 - t_start, - t4 - t3, - t5 - t4, - total_updated, - ) + if not active_sections: + break del img_src del mask_src diff --git a/processor/flow_test.py b/processor/flow_test.py index 9fd7766..9a23594 100644 --- a/processor/flow_test.py +++ b/processor/flow_test.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import dataclasses from unittest import mock from absl.testing import absltest @@ -817,6 +818,135 @@ def test_selection_mask_restricts_filled_locations(self): self.assertTrue(np.isnan(out.data[0, 0, 1, 0])) self.assertTrue(np.isnan(out.data[0, 0, 1, 1])) + def test_skip_image_load_when_valid_or_masked_and_trim_z(self): + class CountingVolume(MockVolume): + + def __init__(self, data): + super().__init__(data) + self.read_slices = [] + + @property + def asarray(self): + return self + + def __getitem__(self, key): + self.read_slices.append(key) + return super().__getitem__(key) + + class TrackingEstimateMissingFlow(TestEstimateMissingFlow): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.mask_boxes = [] + + def _build_mask(self, mask_configs, box): + if mask_configs == self._config.mask_configs: + self.mask_boxes.append(box) + return super()._build_mask(mask_configs, box) + + rng = np.random.default_rng(123) + vol_data = rng.uniform(0.1, 0.9, size=(1, 16, 128, 128)).astype(np.float32) + dx, dy = 2, -3 + base = rng.uniform(0.1, 0.9, size=(160, 160)).astype(np.float32) + vol_data[0, 7] = base[16:144, 16:144] + vol_data[0, 8] = base[16:144, 16:144] + vol_data[0, 10] = base[16 - dy : 144 - dy, 16 - dx : 144 - dx] + + cfg = flow.EstimateMissingFlow.Config( + patch_size=16, + stride=16, + delta_z=1, + max_delta_z=3, + max_attempts=2, + mask_configs="dummy_mask", + mask_only_for_patch_selection=False, + selection_mask_configs=None, + min_peak_sharpness=0.0, + min_peak_ratio=0.0, + max_magnitude=0, + batch_size=16, + image_volinfo="dummy_path", + image_cache_bytes=0, + mask_cache_bytes=0, + search_radius=0, + ) + box = bounding_box.BoundingBox((2, 2, 4), (2, 2, 10)) + mask_none = np.zeros(vol_data.shape[1:], dtype=bool) + + # 1. All-valid input: zero image and mask reads. + cvol = CountingVolume(vol_data) + proc = TrackingEstimateMissingFlow(cfg, cvol, mask_data=mask_none) + valid_in = np.ones((2, 10, 2, 2), dtype=np.float32) + out_valid = proc.process(subvolume.Subvolume(valid_in, box)) + self.assertEmpty(cvol.read_slices) + self.assertEmpty(proc.mask_boxes) + np.testing.assert_array_equal(out_valid.data[:2], valid_in) + + # 2. Exterior masked subvolume (all NaN, mask=True): zero image reads. + cvol = CountingVolume(vol_data) + mask_all = np.ones(vol_data.shape[1:], dtype=bool) + proc_ext = TrackingEstimateMissingFlow(cfg, cvol, mask_data=mask_all) + nan_in = np.full((2, 10, 2, 2), np.nan, dtype=np.float32) + out_ext = proc_ext.process(subvolume.Subvolume(nan_in, box)) + self.assertEmpty(cvol.read_slices) + self.assertTrue(np.all(np.isnan(out_ext.data[:2]))) + + # 3. Single unmasked invalid section at abs z=10 (rel z=6), with abs z=4 + # (rel z=0) masked: mask load is pre-trimmed to [1, 11) (excluding valid + # sections 11..13) and image load is trimmed to [7, 11) (z=10 and + # delta_z=2,3 -> z=8,7) instead of [1, 14). + cvol = CountingVolume(vol_data) + mixed_in = np.zeros((2, 10, 2, 2), dtype=np.float32) + mixed_in[:, 0, :, :] = np.nan # abs z=4, masked out below + mixed_in[:, 6, :, :] = np.nan # abs z=10, unmasked + mask_partial = np.zeros(vol_data.shape[1:], dtype=bool) + mask_partial[4] = True # mask abs z=4 completely + proc_trim = TrackingEstimateMissingFlow(cfg, cvol, mask_data=mask_partial) + out_trim = proc_trim.process(subvolume.Subvolume(mixed_in, box)) + self.assertLen(proc_trim.mask_boxes, 1) + self.assertEqual( + (proc_trim.mask_boxes[0].start[2], proc_trim.mask_boxes[0].end[2]), + (1, 11), + ) + self.assertLen(cvol.read_slices, 1) + z_slice = cvol.read_slices[0][1] + self.assertEqual((z_slice.start, z_slice.stop), (7, 11)) + self.assertTrue(np.all(np.isnan(out_trim.data[:2, 0]))) + np.testing.assert_allclose(out_trim.data[0, 6], -dx, atol=0.5) + np.testing.assert_allclose(out_trim.data[1, 6], -dy, atol=0.5) + np.testing.assert_allclose(out_trim.data[2, 6], 2.0) + + # 4. selection_mask_configs excluding all NaNs: zero image and mask reads. + cvol = CountingVolume(vol_data) + sel_cfg = dataclasses.replace(cfg, selection_mask_configs="dummy_sel") + sel_mask_data = np.zeros((16, 4, 4), dtype=bool) + sel_mask_data[5, :, :] = True # only valid abs z=5 is selected + proc_sel = TrackingEstimateMissingFlow( + sel_cfg, cvol, mask_data=mask_none, selection_mask_data=sel_mask_data + ) + out_sel = proc_sel.process(subvolume.Subvolume(mixed_in, box)) + self.assertEmpty(cvol.read_slices) + self.assertEmpty(proc_sel.mask_boxes) + np.testing.assert_array_equal(out_sel.data[:2], mixed_in) + + # 5. Overlap-Save (max_shift=8) with z_trim_start > 0 and masked delta_z=2 + # lookback (abs z=8 masked): prev_mask_all slicing skips delta_z=2 and + # falls through to delta_z=3 (abs z=7). + cvol = CountingVolume(vol_data) + mask_skip_d2 = mask_partial.copy() + mask_skip_d2[8] = True # mask abs z=8 so delta_z=2 is skipped + os_cfg = dataclasses.replace(cfg, max_shift=8) + proc_os = TrackingEstimateMissingFlow( + os_cfg, cvol, mask_data=mask_skip_d2 + ) + out_os = proc_os.process(subvolume.Subvolume(mixed_in, box)) + self.assertLen(cvol.read_slices, 1) + z_slice_os = cvol.read_slices[0][1] + self.assertEqual((z_slice_os.start, z_slice_os.stop), (7, 11)) + np.testing.assert_allclose(out_os.data[0, 6], -dx, atol=0.5) + np.testing.assert_allclose(out_os.data[1, 6], -dy, atol=0.5) + np.testing.assert_allclose(out_os.data[2, 6], 3.0) + class ReconcileAndFilterFlowsTest(absltest.TestCase):