|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2026 The Google Research Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Tests for sampling module.""" |
| 16 | + |
| 17 | +from connectomics.brainstate import sampling |
| 18 | +import numpy as np |
| 19 | +from google3.testing.pybase import googletest |
| 20 | + |
| 21 | + |
| 22 | +class SamplingTest(googletest.TestCase): |
| 23 | + |
| 24 | + def test_split_indices_by_labels(self): |
| 25 | + labels = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] |
| 26 | + ratios = [0.8] |
| 27 | + rng = np.random.RandomState(22222) |
| 28 | + splits = sampling.split_indices_by_labels(labels, ratios, rng) |
| 29 | + np.testing.assert_array_equal(splits[0], [4, 0, 3, 2, 9, 6, 8, 5]) |
| 30 | + np.testing.assert_array_equal(splits[1], [1, 7]) |
| 31 | + |
| 32 | + def test_empty_split(self): |
| 33 | + labels = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] |
| 34 | + ratios = [0.8, 0.0] |
| 35 | + rng = np.random.RandomState(22222) |
| 36 | + splits = sampling.split_indices_by_labels(labels, ratios, rng) |
| 37 | + np.testing.assert_array_equal(splits[0], [4, 0, 3, 2, 9, 6, 8, 5]) |
| 38 | + np.testing.assert_array_equal(splits[1], []) |
| 39 | + np.testing.assert_array_equal(splits[2], [1, 7]) |
| 40 | + |
| 41 | + def test_split_dataset(self): |
| 42 | + sample_ids = range(10) |
| 43 | + seed = 22222 |
| 44 | + train_ratio = 0.7 |
| 45 | + valid_ratio = 0.1 # Test 0.2 implicit. |
| 46 | + split = sampling.split_dataset(sample_ids, seed, train_ratio, valid_ratio) |
| 47 | + np.testing.assert_array_equal(split.train_ids, [3, 5, 9, 4, 6, 7, 0]) |
| 48 | + np.testing.assert_array_equal(split.valid_ids, [8]) |
| 49 | + np.testing.assert_array_equal(split.test_ids, [2, 1]) |
| 50 | + np.testing.assert_array_equal(split.train_labels, [0, 0, 0, 0, 0, 0, 0]) |
| 51 | + np.testing.assert_array_equal(split.valid_labels, [0]) |
| 52 | + np.testing.assert_array_equal(split.test_labels, [0, 0]) |
| 53 | + |
| 54 | + # Results should be balanced by labels. |
| 55 | + labels = [1, 1, 1, 2, 2, 2, 2, 2, 2, 2] |
| 56 | + split = sampling.split_dataset( |
| 57 | + sample_ids, seed, train_ratio, valid_ratio, labels) |
| 58 | + np.testing.assert_array_equal(split.train_ids, [2, 0, 7, 4, 6, 8]) |
| 59 | + np.testing.assert_array_equal(split.valid_ids, []) |
| 60 | + np.testing.assert_array_equal(split.test_ids, [1, 9, 3, 5]) |
| 61 | + np.testing.assert_array_equal(split.train_labels, [1, 1, 2, 2, 2, 2]) |
| 62 | + np.testing.assert_array_equal(split.valid_labels, []) |
| 63 | + np.testing.assert_array_equal(split.test_labels, [1, 2, 2, 2]) |
| 64 | + |
| 65 | + def test_upsample(self): |
| 66 | + sample_ids = range(10) |
| 67 | + labels = [1, 1, 1, 2, 2, 2, 2, 2, 2, 2] |
| 68 | + seed = 22222 |
| 69 | + train_ratio = 0.7 |
| 70 | + valid_ratio = 0.1 # Test 0.2 implicit. |
| 71 | + split = sampling.split_dataset( |
| 72 | + sample_ids, seed, train_ratio, valid_ratio, labels |
| 73 | + ) |
| 74 | + upsampled = split.upsampled(upsample_factor=2, dataset_len=10) |
| 75 | + np.testing.assert_array_equal( |
| 76 | + upsampled.train_ids, [2, 0, 7, 4, 6, 8, 12, 10, 17, 14, 16, 18] |
| 77 | + ) |
| 78 | + np.testing.assert_array_equal(upsampled.valid_ids, []) |
| 79 | + np.testing.assert_array_equal( |
| 80 | + upsampled.test_ids, [1, 9, 3, 5, 11, 19, 13, 15] |
| 81 | + ) |
| 82 | + np.testing.assert_array_equal( |
| 83 | + upsampled.train_labels, [1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2] |
| 84 | + ) |
| 85 | + np.testing.assert_array_equal(upsampled.valid_labels, []) |
| 86 | + np.testing.assert_array_equal( |
| 87 | + upsampled.test_labels, [1, 2, 2, 2, 1, 2, 2, 2] |
| 88 | + ) |
| 89 | + |
| 90 | + def test_concatenate_splits(self): |
| 91 | + sample_ids = range(10) |
| 92 | + seed = 22222 |
| 93 | + train_ratio = 0.7 |
| 94 | + valid_ratio = 0.1 # Test 0.2 implicit. |
| 95 | + split = sampling.split_dataset(sample_ids, seed, train_ratio, valid_ratio) |
| 96 | + |
| 97 | + labels = [1, 1, 1, 2, 2, 2, 2, 2, 2, 2] |
| 98 | + split2 = sampling.split_dataset( |
| 99 | + sample_ids, seed, train_ratio, valid_ratio, labels |
| 100 | + ) |
| 101 | + |
| 102 | + dataset_lengths = [10, 10] |
| 103 | + concat = sampling.concatenate_splits([split, split2], dataset_lengths) |
| 104 | + np.testing.assert_array_equal( |
| 105 | + concat.train_ids, [3, 5, 9, 4, 6, 7, 0, 12, 10, 17, 14, 16, 18] |
| 106 | + ) |
| 107 | + np.testing.assert_array_equal(concat.valid_ids, [8]) |
| 108 | + np.testing.assert_array_equal(concat.test_ids, [2, 1, 11, 19, 13, 15]) |
| 109 | + np.testing.assert_array_equal( |
| 110 | + concat.train_labels, [0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2] |
| 111 | + ) |
| 112 | + np.testing.assert_array_equal(concat.valid_labels, [0]) |
| 113 | + np.testing.assert_array_equal(concat.test_labels, [0, 0, 1, 2, 2, 2]) |
| 114 | + |
| 115 | + def test_cross_validation_split_dataset(self): |
| 116 | + sample_ids = range(10) |
| 117 | + seed = 22222 |
| 118 | + num_splits = 5 |
| 119 | + splits = sampling.cross_validation_split_dataset( |
| 120 | + sample_ids, seed, num_splits).sample_id_splits |
| 121 | + np.testing.assert_array_equal(splits[0], [3, 5]) |
| 122 | + np.testing.assert_array_equal(splits[1], [9, 4]) |
| 123 | + np.testing.assert_array_equal(splits[2], [6, 7]) |
| 124 | + np.testing.assert_array_equal(splits[3], [0, 8]) |
| 125 | + np.testing.assert_array_equal(splits[4], [2, 1]) |
| 126 | + |
| 127 | + num_splits = 2 |
| 128 | + splits = sampling.cross_validation_split_dataset( |
| 129 | + sample_ids, seed, num_splits).sample_id_splits |
| 130 | + np.testing.assert_array_equal(splits[0], [3, 5, 9, 4, 6]) |
| 131 | + np.testing.assert_array_equal(splits[1], [7, 0, 8, 2, 1]) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + googletest.main() |
0 commit comments