|
| 1 | +# Copyright 2026 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +import functools as ft |
| 17 | +import os |
| 18 | + |
| 19 | + |
| 20 | +from absl.testing import absltest |
| 21 | +from connectomics.common import bounding_box |
| 22 | +from connectomics.common import tuples |
| 23 | +from connectomics.volume import metadata |
| 24 | +from ffn.training import inputs |
| 25 | +import numpy as np |
| 26 | +import tensorflow.compat.v1 as tf |
| 27 | + |
| 28 | +tf.disable_eager_execution() |
| 29 | + |
| 30 | + |
| 31 | +class _FilterOobTestBase: |
| 32 | + |
| 33 | + _volinfo_map_string: str |
| 34 | + |
| 35 | + def test_filter_oob_in_bounds(self): |
| 36 | + coord = tf.constant([[50, 50, 50]], dtype=tf.int64) |
| 37 | + volname = tf.constant(['testvol'], dtype=tf.string) |
| 38 | + item = {'coord': coord, 'volname': volname} |
| 39 | + result = inputs.filter_oob( |
| 40 | + item, self._volinfo_map_string, patch_size=[10, 10, 10] |
| 41 | + ) |
| 42 | + with tf.Session() as sess: |
| 43 | + self.assertTrue(sess.run(result)) |
| 44 | + |
| 45 | + def test_filter_oob_out_of_bounds(self): |
| 46 | + coord = tf.constant([[0, 0, 0]], dtype=tf.int64) |
| 47 | + volname = tf.constant(['testvol'], dtype=tf.string) |
| 48 | + item = {'coord': coord, 'volname': volname} |
| 49 | + result = inputs.filter_oob( |
| 50 | + item, self._volinfo_map_string, patch_size=[10, 10, 10] |
| 51 | + ) |
| 52 | + with tf.Session() as sess: |
| 53 | + self.assertFalse(sess.run(result)) |
| 54 | + |
| 55 | + def test_filter_oob_in_dataset_filter(self): |
| 56 | + ds = tf.data.Dataset.from_tensors({ |
| 57 | + 'coord': tf.constant([[50, 50, 50]], dtype=tf.int64), |
| 58 | + 'volname': tf.constant(['testvol'], dtype=tf.string), |
| 59 | + }) |
| 60 | + ds = ds.filter( |
| 61 | + ft.partial( |
| 62 | + inputs.filter_oob, |
| 63 | + volinfo_map_string=self._volinfo_map_string, |
| 64 | + patch_size=[10, 10, 10], |
| 65 | + ) |
| 66 | + ) |
| 67 | + iterator = tf.data.make_one_shot_iterator(ds) |
| 68 | + item = iterator.get_next() |
| 69 | + with tf.Session() as sess: |
| 70 | + result = sess.run(item) |
| 71 | + np.testing.assert_array_equal(result['coord'], [[50, 50, 50]]) |
| 72 | + |
| 73 | + |
| 74 | +class FilterOobMetadataJsonTest(_FilterOobTestBase, absltest.TestCase): |
| 75 | + |
| 76 | + def setUp(self): |
| 77 | + super().setUp() |
| 78 | + tf.reset_default_graph() |
| 79 | + |
| 80 | + self._tmpdir = self.create_tempdir().full_path |
| 81 | + meta = metadata.VolumeMetadata( |
| 82 | + path='none', |
| 83 | + volume_size=tuples.XYZ(100, 100, 100), |
| 84 | + pixel_size=tuples.XYZ(8, 8, 30), |
| 85 | + bounding_boxes=[ |
| 86 | + bounding_box.BoundingBox(start=(0, 0, 0), size=(100, 100, 100)) |
| 87 | + ], |
| 88 | + ) |
| 89 | + self._metadata_path = os.path.join(self._tmpdir, 'metadata.json') |
| 90 | + with open(self._metadata_path, 'w') as f: |
| 91 | + f.write(meta.to_json()) |
| 92 | + |
| 93 | + self._volinfo_map_string = f'testvol:{self._metadata_path}' |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + absltest.main() |
0 commit comments