|
| 1 | +"""Tests for the TimeCNNNetwork Model.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from aeon.networks import TimeCNNNetwork |
| 6 | +from aeon.utils.validation._dependencies import _check_soft_dependencies |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.skipif( |
| 10 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 11 | + reason="Tensorflow soft dependency unavailable.", |
| 12 | +) |
| 13 | +def test_time_cnn_input_shape_padding(): |
| 14 | + """Test of CNN network with input_shape < 60.""" |
| 15 | + input_shape = (40, 2) |
| 16 | + network = TimeCNNNetwork() |
| 17 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 18 | + |
| 19 | + assert hasattr(input_layer, "shape") |
| 20 | + assert hasattr(output_layer, "shape") |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.skipif( |
| 24 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 25 | + reason="Tensorflow soft dependency unavailable.", |
| 26 | +) |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "activation, n_layers, should_raise", |
| 29 | + [ |
| 30 | + ("relu", 2, False), |
| 31 | + ("sigmoid", 2, False), |
| 32 | + ("tanh", 2, False), |
| 33 | + (["relu", "sigmoid", "tanh"], 2, True), |
| 34 | + (["relu"], 2, True), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_time_cnn_activation(activation, n_layers, should_raise): |
| 38 | + """Test activation configuration handling.""" |
| 39 | + input_shape = (100, 5) |
| 40 | + if should_raise: |
| 41 | + with pytest.raises(ValueError): |
| 42 | + network = TimeCNNNetwork(activation=activation, n_layers=n_layers) |
| 43 | + network.build_network(input_shape=input_shape) |
| 44 | + else: |
| 45 | + network = TimeCNNNetwork(activation=activation, n_layers=n_layers) |
| 46 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 47 | + |
| 48 | + assert hasattr(input_layer, "shape") |
| 49 | + assert hasattr(output_layer, "shape") |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.skipif( |
| 53 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 54 | + reason="Tensorflow soft dependency unavailable.", |
| 55 | +) |
| 56 | +@pytest.mark.parametrize( |
| 57 | + "kernel_size, n_layers, should_raise", |
| 58 | + [ |
| 59 | + (7, 2, False), |
| 60 | + ([5, 3], 2, False), |
| 61 | + ([5, 3, 2], 2, True), |
| 62 | + ([5], 2, True), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_time_cnn_kernel_size(kernel_size, n_layers, should_raise): |
| 66 | + """Test kernel size configuration with different layer counts.""" |
| 67 | + input_shape = (100, 5) |
| 68 | + if should_raise: |
| 69 | + with pytest.raises(ValueError): |
| 70 | + network = TimeCNNNetwork(n_layers=n_layers, kernel_size=kernel_size) |
| 71 | + network.build_network(input_shape=input_shape) |
| 72 | + else: |
| 73 | + network = TimeCNNNetwork(n_layers=n_layers, kernel_size=kernel_size) |
| 74 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 75 | + |
| 76 | + assert hasattr(input_layer, "shape") |
| 77 | + assert hasattr(output_layer, "shape") |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.skipif( |
| 81 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 82 | + reason="Tensorflow soft dependency unavailable.", |
| 83 | +) |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "n_layers,n_filters,should_raise", |
| 86 | + [ |
| 87 | + (2, [8, 16], False), |
| 88 | + (1, [12, 10, 4], True), |
| 89 | + (2, 8, False), |
| 90 | + (3, [8], True), |
| 91 | + ], |
| 92 | +) |
| 93 | +def test_time_cnn_n_filters(n_layers, n_filters, should_raise): |
| 94 | + """Test filter configuration handling.""" |
| 95 | + input_shape = (100, 5) |
| 96 | + if should_raise: |
| 97 | + with pytest.raises(ValueError): |
| 98 | + network = TimeCNNNetwork(n_layers=n_layers, n_filters=n_filters) |
| 99 | + network.build_network(input_shape=input_shape) |
| 100 | + else: |
| 101 | + network = TimeCNNNetwork(n_layers=n_layers, n_filters=n_filters) |
| 102 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 103 | + |
| 104 | + assert hasattr(input_layer, "shape") |
| 105 | + assert hasattr(output_layer, "shape") |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.skipif( |
| 109 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 110 | + reason="Tensorflow soft dependency unavailable.", |
| 111 | +) |
| 112 | +@pytest.mark.parametrize( |
| 113 | + "avg_pool_size, n_layers, should_raise", |
| 114 | + [ |
| 115 | + (3, 2, False), |
| 116 | + ([2, 3], 2, False), |
| 117 | + ([2, 3, 4], 2, True), |
| 118 | + ([2], 2, True), |
| 119 | + ], |
| 120 | +) |
| 121 | +def test_time_cnn_avg_pool_size(avg_pool_size, n_layers, should_raise): |
| 122 | + """Test average pool size configuration.""" |
| 123 | + input_shape = (100, 5) |
| 124 | + if should_raise: |
| 125 | + with pytest.raises(ValueError): |
| 126 | + network = TimeCNNNetwork(avg_pool_size=avg_pool_size, n_layers=n_layers) |
| 127 | + network.build_network(input_shape=input_shape) |
| 128 | + else: |
| 129 | + network = TimeCNNNetwork(avg_pool_size=avg_pool_size, n_layers=n_layers) |
| 130 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 131 | + |
| 132 | + assert hasattr(input_layer, "shape") |
| 133 | + assert hasattr(output_layer, "shape") |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.skipif( |
| 137 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 138 | + reason="Tensorflow soft dependency unavailable.", |
| 139 | +) |
| 140 | +@pytest.mark.parametrize( |
| 141 | + "strides_pooling, n_layers, should_raise", |
| 142 | + [ |
| 143 | + (None, 2, False), |
| 144 | + (2, 2, False), |
| 145 | + ([2, 3], 2, False), |
| 146 | + ([2, 3, 4], 2, True), |
| 147 | + ([2], 2, True), |
| 148 | + ], |
| 149 | +) |
| 150 | +def test_time_cnn_strides_pooling(strides_pooling, n_layers, should_raise): |
| 151 | + """Test strides pooling configuration.""" |
| 152 | + input_shape = (100, 5) |
| 153 | + if should_raise: |
| 154 | + with pytest.raises(ValueError): |
| 155 | + network = TimeCNNNetwork(strides_pooling=strides_pooling, n_layers=n_layers) |
| 156 | + network.build_network(input_shape=input_shape) |
| 157 | + else: |
| 158 | + network = TimeCNNNetwork(strides_pooling=strides_pooling, n_layers=n_layers) |
| 159 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 160 | + |
| 161 | + assert hasattr(input_layer, "shape") |
| 162 | + assert hasattr(output_layer, "shape") |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.skipif( |
| 166 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 167 | + reason="Tensorflow soft dependency unavailable.", |
| 168 | +) |
| 169 | +@pytest.mark.parametrize( |
| 170 | + "padding, n_layers, should_raise", |
| 171 | + [ |
| 172 | + ("valid", 2, False), |
| 173 | + ("same", 2, False), |
| 174 | + (["same", "valid"], 2, False), |
| 175 | + (["same", "valid", "same"], 2, True), |
| 176 | + (["same"], 2, True), |
| 177 | + ], |
| 178 | +) |
| 179 | +def test_time_cnn_padding(padding, n_layers, should_raise): |
| 180 | + """Test padding override behavior for different inputs.""" |
| 181 | + input_shape = (100, 5) |
| 182 | + if should_raise: |
| 183 | + with pytest.raises(ValueError): |
| 184 | + network = TimeCNNNetwork(padding=padding, n_layers=n_layers) |
| 185 | + network.build_network(input_shape=input_shape) |
| 186 | + else: |
| 187 | + network = TimeCNNNetwork(padding=padding, n_layers=n_layers) |
| 188 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 189 | + assert hasattr(input_layer, "shape") |
| 190 | + assert hasattr(output_layer, "shape") |
| 191 | + |
| 192 | + |
| 193 | +@pytest.mark.skipif( |
| 194 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 195 | + reason="Tensorflow soft dependency unavailable.", |
| 196 | +) |
| 197 | +@pytest.mark.parametrize( |
| 198 | + "dilation, n_layers, should_raise", |
| 199 | + [ |
| 200 | + (2, 2, False), |
| 201 | + ([1, 2], 2, False), |
| 202 | + ([1, 2, 3], 2, True), |
| 203 | + ([1], 2, True), |
| 204 | + ], |
| 205 | +) |
| 206 | +def test_time_cnn_dilation_rate(dilation, n_layers, should_raise): |
| 207 | + """Test dilation rate configuration.""" |
| 208 | + input_shape = (100, 5) |
| 209 | + if should_raise: |
| 210 | + with pytest.raises(ValueError): |
| 211 | + network = TimeCNNNetwork(dilation_rate=dilation, n_layers=n_layers) |
| 212 | + network.build_network(input_shape=input_shape) |
| 213 | + else: |
| 214 | + network = TimeCNNNetwork(dilation_rate=dilation, n_layers=n_layers) |
| 215 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 216 | + |
| 217 | + assert hasattr(input_layer, "shape") |
| 218 | + assert hasattr(output_layer, "shape") |
| 219 | + |
| 220 | + |
| 221 | +@pytest.mark.skipif( |
| 222 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 223 | + reason="Tensorflow soft dependency unavailable.", |
| 224 | +) |
| 225 | +@pytest.mark.parametrize( |
| 226 | + "strides, n_layers, should_raise", |
| 227 | + [ |
| 228 | + (1, 2, False), |
| 229 | + ([1, 2], 2, False), |
| 230 | + ([1, 2, 3], 2, True), |
| 231 | + ([1], 2, True), |
| 232 | + ], |
| 233 | +) |
| 234 | +def test_time_cnn_strides(strides, n_layers, should_raise): |
| 235 | + """Test strides configuration.""" |
| 236 | + input_shape = (100, 5) |
| 237 | + if should_raise: |
| 238 | + with pytest.raises(ValueError): |
| 239 | + network = TimeCNNNetwork(strides=strides, n_layers=n_layers) |
| 240 | + network.build_network(input_shape=input_shape) |
| 241 | + else: |
| 242 | + network = TimeCNNNetwork(strides=strides, n_layers=n_layers) |
| 243 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 244 | + |
| 245 | + assert hasattr(input_layer, "shape") |
| 246 | + assert hasattr(output_layer, "shape") |
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.skipif( |
| 250 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 251 | + reason="Tensorflow soft dependency unavailable.", |
| 252 | +) |
| 253 | +@pytest.mark.parametrize( |
| 254 | + "use_bias, n_layers, should_raise", |
| 255 | + [ |
| 256 | + (True, 2, False), |
| 257 | + ([True, False], 2, False), |
| 258 | + ([True, False, True], 2, True), |
| 259 | + ([True], 2, True), |
| 260 | + ], |
| 261 | +) |
| 262 | +def test_time_cnn_use_bias(use_bias, n_layers, should_raise): |
| 263 | + """Test bias usage configuration.""" |
| 264 | + input_shape = (100, 5) |
| 265 | + if should_raise: |
| 266 | + with pytest.raises(ValueError): |
| 267 | + network = TimeCNNNetwork(use_bias=use_bias, n_layers=n_layers) |
| 268 | + network.build_network(input_shape=input_shape) |
| 269 | + else: |
| 270 | + network = TimeCNNNetwork(use_bias=use_bias, n_layers=n_layers) |
| 271 | + input_layer, output_layer = network.build_network(input_shape=input_shape) |
| 272 | + |
| 273 | + assert hasattr(input_layer, "shape") |
| 274 | + assert hasattr(output_layer, "shape") |
0 commit comments