|
| 1 | +"""Test for the AEFCNNetwork class.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from aeon.networks import AEFCNNetwork |
| 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_aefcn_default(): |
| 14 | + """Default testing for aefcn.""" |
| 15 | + model = AEFCNNetwork() |
| 16 | + assert model.latent_space_dim == 128 |
| 17 | + assert model.temporal_latent_space is False |
| 18 | + assert model.n_layers == 3 |
| 19 | + assert model.n_filters is None |
| 20 | + assert model.kernel_size is None |
| 21 | + assert model.activation == "relu" |
| 22 | + assert model.padding == "same" |
| 23 | + assert model.strides == 1 |
| 24 | + assert model.dilation_rate == 1 |
| 25 | + assert model.use_bias is True |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.skipif( |
| 29 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 30 | + reason="Tensorflow soft dependency unavailable.", |
| 31 | +) |
| 32 | +@pytest.mark.parametrize("latent_space_dim", [64, 128, 256]) |
| 33 | +def test_aefcn_latent_space(latent_space_dim): |
| 34 | + """Test AEFCNNetwork with different latent space dimensions.""" |
| 35 | + import tensorflow as tf |
| 36 | + |
| 37 | + aefcn = AEFCNNetwork(latent_space_dim=latent_space_dim) |
| 38 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 39 | + assert isinstance(encoder, tf.keras.models.Model) |
| 40 | + assert isinstance(decoder, tf.keras.models.Model) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.skipif( |
| 44 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 45 | + reason="Tensorflow soft dependency unavailable.", |
| 46 | +) |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "kernel_size, should_raise", |
| 49 | + [ |
| 50 | + ([8, 5, 3], False), |
| 51 | + (3, False), |
| 52 | + ([5, 5], True), |
| 53 | + ([3, 3, 3, 3], True), |
| 54 | + ], |
| 55 | +) |
| 56 | +def test_aefcnnetwork_kernel_size(kernel_size, should_raise): |
| 57 | + """Test AEFCNNetwork with different kernel sizes.""" |
| 58 | + import tensorflow as tf |
| 59 | + |
| 60 | + if should_raise: |
| 61 | + with pytest.raises( |
| 62 | + ValueError, |
| 63 | + match="Number of kernels .* should be the same as number of layers", |
| 64 | + ): |
| 65 | + AEFCNNetwork(kernel_size=kernel_size, n_layers=3).build_network((1000, 5)) |
| 66 | + else: |
| 67 | + aefcn = AEFCNNetwork(kernel_size=kernel_size, n_layers=3) |
| 68 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 69 | + assert isinstance(encoder, tf.keras.models.Model) |
| 70 | + assert isinstance(decoder, tf.keras.models.Model) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.skipif( |
| 74 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 75 | + reason="Tensorflow soft dependency unavailable.", |
| 76 | +) |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "n_filters, should_raise", |
| 79 | + [ |
| 80 | + ([128, 256, 128], False), |
| 81 | + (32, False), |
| 82 | + ([32, 64], True), |
| 83 | + ([16, 32, 64, 128], True), |
| 84 | + ], |
| 85 | +) |
| 86 | +def test_aefcnnetwork_n_filters(n_filters, should_raise): |
| 87 | + """Test AEFCNNetwork with different number of filters.""" |
| 88 | + import tensorflow as tf |
| 89 | + |
| 90 | + if should_raise: |
| 91 | + with pytest.raises( |
| 92 | + ValueError, |
| 93 | + match="Number of filters .* should be the same as number of layers", |
| 94 | + ): |
| 95 | + AEFCNNetwork(n_filters=n_filters, n_layers=3).build_network((1000, 5)) |
| 96 | + else: |
| 97 | + aefcn = AEFCNNetwork(n_filters=n_filters, n_layers=3) |
| 98 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 99 | + assert isinstance(encoder, tf.keras.models.Model) |
| 100 | + assert isinstance(decoder, tf.keras.models.Model) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.skipif( |
| 104 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 105 | + reason="Tensorflow soft dependency unavailable.", |
| 106 | +) |
| 107 | +@pytest.mark.parametrize( |
| 108 | + "dilation_rate, should_raise", |
| 109 | + [ |
| 110 | + ([1, 2, 1], False), |
| 111 | + (2, False), |
| 112 | + ([1, 2], True), |
| 113 | + ([1, 2, 2, 1], True), |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_aefcnnetwork_dilation_rate(dilation_rate, should_raise): |
| 117 | + """Test AEFCNNetwork with different dilation rates.""" |
| 118 | + import tensorflow as tf |
| 119 | + |
| 120 | + if should_raise: |
| 121 | + with pytest.raises( |
| 122 | + ValueError, |
| 123 | + match="Number of dilations .* should be the same as number of layers", |
| 124 | + ): |
| 125 | + AEFCNNetwork(dilation_rate=dilation_rate, n_layers=3).build_network( |
| 126 | + (1000, 5) |
| 127 | + ) |
| 128 | + else: |
| 129 | + aefcn = AEFCNNetwork(dilation_rate=dilation_rate, n_layers=3) |
| 130 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 131 | + assert isinstance(encoder, tf.keras.models.Model) |
| 132 | + assert isinstance(decoder, tf.keras.models.Model) |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.skipif( |
| 136 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 137 | + reason="Tensorflow soft dependency unavailable.", |
| 138 | +) |
| 139 | +@pytest.mark.parametrize( |
| 140 | + "strides, should_raise", |
| 141 | + [ |
| 142 | + ([1, 2, 1], False), |
| 143 | + (2, False), |
| 144 | + ([1, 2], True), |
| 145 | + ([1, 2, 2, 1], True), |
| 146 | + ], |
| 147 | +) |
| 148 | +def test_aefcnnetwork_strides(strides, should_raise): |
| 149 | + """Test AEFCNNetwork with different strides.""" |
| 150 | + import tensorflow as tf |
| 151 | + |
| 152 | + if should_raise: |
| 153 | + with pytest.raises( |
| 154 | + ValueError, |
| 155 | + match="Number of strides .* should be the same as number of layers", |
| 156 | + ): |
| 157 | + AEFCNNetwork(strides=strides, n_layers=3).build_network((1000, 5)) |
| 158 | + else: |
| 159 | + aefcn = AEFCNNetwork(strides=strides, n_layers=3) |
| 160 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 161 | + assert isinstance(encoder, tf.keras.models.Model) |
| 162 | + assert isinstance(decoder, tf.keras.models.Model) |
| 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, should_raise", |
| 171 | + [ |
| 172 | + (["same", "valid", "same"], False), |
| 173 | + ("same", False), |
| 174 | + (["same", "valid"], True), |
| 175 | + ( |
| 176 | + ["same", "valid", "same", "valid"], |
| 177 | + True, |
| 178 | + ), |
| 179 | + ], |
| 180 | +) |
| 181 | +def test_aefcnnetwork_padding(padding, should_raise): |
| 182 | + """Test AEFCNNetwork with different paddings.""" |
| 183 | + import tensorflow as tf |
| 184 | + |
| 185 | + if should_raise: |
| 186 | + with pytest.raises( |
| 187 | + ValueError, |
| 188 | + match="Number of paddings .* should be the same as number of layers", |
| 189 | + ): |
| 190 | + AEFCNNetwork(padding=padding, n_layers=3).build_network((1000, 5)) |
| 191 | + else: |
| 192 | + aefcn = AEFCNNetwork(padding=padding, n_layers=3) |
| 193 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 194 | + assert isinstance(encoder, tf.keras.models.Model) |
| 195 | + assert isinstance(decoder, tf.keras.models.Model) |
| 196 | + |
| 197 | + |
| 198 | +@pytest.mark.skipif( |
| 199 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 200 | + reason="Tensorflow soft dependency unavailable.", |
| 201 | +) |
| 202 | +@pytest.mark.parametrize( |
| 203 | + "activation, should_raise", |
| 204 | + [ |
| 205 | + (["relu", "sigmoid", "tanh"], False), |
| 206 | + ("sigmoid", False), |
| 207 | + (["relu", "sigmoid"], True), |
| 208 | + ( |
| 209 | + ["relu", "sigmoid", "tanh", "softmax"], |
| 210 | + True, |
| 211 | + ), |
| 212 | + ], |
| 213 | +) |
| 214 | +def test_aefcnnetwork_activation(activation, should_raise): |
| 215 | + """Test AEFCNNetwork with different activations.""" |
| 216 | + import tensorflow as tf |
| 217 | + |
| 218 | + if should_raise: |
| 219 | + with pytest.raises( |
| 220 | + ValueError, |
| 221 | + match="Number of activations .* should be the same as number of layers", |
| 222 | + ): |
| 223 | + AEFCNNetwork(activation=activation, n_layers=3).build_network((1000, 5)) |
| 224 | + else: |
| 225 | + aefcn = AEFCNNetwork(activation=activation, n_layers=3) |
| 226 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 227 | + assert isinstance(encoder, tf.keras.models.Model) |
| 228 | + assert isinstance(decoder, tf.keras.models.Model) |
| 229 | + |
| 230 | + |
| 231 | +@pytest.mark.skipif( |
| 232 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 233 | + reason="Tensorflow soft dependency unavailable.", |
| 234 | +) |
| 235 | +@pytest.mark.parametrize( |
| 236 | + "use_bias, should_raise", |
| 237 | + [ |
| 238 | + ([True, False, True], False), |
| 239 | + (True, False), |
| 240 | + ([True, False], True), |
| 241 | + ([True, False, True, False], True), |
| 242 | + ], |
| 243 | +) |
| 244 | +def test_aefcnnetwork_use_bias(use_bias, should_raise): |
| 245 | + """Test AEFCNNetwork with different use_bias values.""" |
| 246 | + import tensorflow as tf |
| 247 | + |
| 248 | + if should_raise: |
| 249 | + with pytest.raises( |
| 250 | + ValueError, |
| 251 | + match="Number of biases .* should be the same as number of layers", |
| 252 | + ): |
| 253 | + AEFCNNetwork(use_bias=use_bias, n_layers=3).build_network((1000, 5)) |
| 254 | + else: |
| 255 | + aefcn = AEFCNNetwork(use_bias=use_bias, n_layers=3) |
| 256 | + encoder, decoder = aefcn.build_network((1000, 5)) |
| 257 | + assert isinstance(encoder, tf.keras.models.Model) |
| 258 | + assert isinstance(decoder, tf.keras.models.Model) |
| 259 | + |
| 260 | + |
| 261 | +@pytest.mark.skipif( |
| 262 | + not _check_soft_dependencies(["tensorflow"], severity="none"), |
| 263 | + reason="Tensorflow soft dependency unavailable.", |
| 264 | +) |
| 265 | +@pytest.mark.parametrize("temporal_latent_space", [True, False]) |
| 266 | +def test_aefcnnetwork_temporal_latent_space(temporal_latent_space): |
| 267 | + """Test for temporal latent space.""" |
| 268 | + import tensorflow as tf |
| 269 | + |
| 270 | + input_shape = (1000, 5) |
| 271 | + |
| 272 | + aefcn = AEFCNNetwork( |
| 273 | + latent_space_dim=128, temporal_latent_space=temporal_latent_space |
| 274 | + ) |
| 275 | + |
| 276 | + encoder, decoder = aefcn.build_network(input_shape) |
| 277 | + |
| 278 | + assert isinstance(encoder, tf.keras.models.Model) |
| 279 | + assert isinstance(decoder, tf.keras.models.Model) |
| 280 | + |
| 281 | + if temporal_latent_space: |
| 282 | + assert any( |
| 283 | + isinstance(layer, tf.keras.layers.Conv1D) for layer in encoder.layers |
| 284 | + ), "Expected Conv1D layer in encoder but not found." |
| 285 | + else: |
| 286 | + assert any( |
| 287 | + isinstance(layer, tf.keras.layers.Dense) for layer in decoder.layers |
| 288 | + ), "Expected Dense layer in decoder but not found." |
0 commit comments