| 182 | |
| 183 | |
| 184 | def test_motifs_two_motifs(): |
| 185 | # Fix seed, because in some case motifs can be off by an index resulting in test |
| 186 | # fails, which is caused since one of the motifs is not repeated perfectly in T. |
| 187 | np.random.seed(1234) |
| 188 | |
| 189 | # The time series is random noise with two motifs for m=10: |
| 190 | # * (almost) identical step functions at indices 10, 110 and 210 |
| 191 | # * identical linear slopes at indices 70 and 170 |
| 192 | T = np.random.normal(size=300) |
| 193 | m = 20 |
| 194 | |
| 195 | T[10:30] = 1 |
| 196 | T[12:28] = 2 |
| 197 | |
| 198 | T[110:130] = 3 |
| 199 | T[112:128] = 6 |
| 200 | T[120] = 6.6 |
| 201 | |
| 202 | T[210:230] = 1 |
| 203 | T[212:228] = 2 |
| 204 | T[220] = 1.9 |
| 205 | # naive.distance(naive.z_norm(T[10:30]), naive.z_norm(T[110:130])) = 0.47 |
| 206 | # naive.distance(naive.z_norm(T[10:30]), naive.z_norm(T[210:230])) = 0.24 |
| 207 | # naive.distance(naive.z_norm(T[110:130]), naive.z_norm(T[210:230])) = 0.72 |
| 208 | # Hence T[10:30] is the motif representative for this motif |
| 209 | |
| 210 | T[70:90] = np.arange(m) * 0.1 |
| 211 | T[170:190] = np.arange(m) * 0.1 |
| 212 | # naive.distance(naive.z_norm(T[70:90]), naive.z_norm(T[170:190])) = 0.0 |
| 213 | |
| 214 | max_motifs = 2 |
| 215 | |
| 216 | mp = naive.stump(T, m) |
| 217 | |
| 218 | # left_indices = [[70, 170, -1], [10, 210, 110]] |
| 219 | left_profile_values = [ |
| 220 | [0.0, 0.0, np.nan], |
| 221 | [ |
| 222 | 0.0, |
| 223 | naive.distance(core.z_norm(T[10:30]), core.z_norm(T[210:230])), |
| 224 | naive.distance(core.z_norm(T[10:30]), core.z_norm(T[110:130])), |
| 225 | ], |
| 226 | ] |
| 227 | |
| 228 | right_distance_values, right_indices = motifs( |
| 229 | T, |
| 230 | mp[:, 0], |
| 231 | max_motifs=max_motifs, |
| 232 | max_distance=0.5, |
| 233 | cutoff=np.inf, |
| 234 | ) |
| 235 | |
| 236 | # We ignore indices because of sorting ambiguities for equal distances. |
| 237 | # As long as the distances are correct, the indices will be too. |
| 238 | npt.assert_almost_equal(left_profile_values, right_distance_values) |
| 239 | |
| 240 | # Reset seed |
| 241 | np.random.seed(None) |