Test label addition.
()
| 206 | |
| 207 | |
| 208 | def test_label_addition(): |
| 209 | """Test label addition.""" |
| 210 | pos = np.random.RandomState(0).rand(10, 3) |
| 211 | values = np.arange(10.0) / 10 |
| 212 | idx0 = list(range(7)) |
| 213 | idx1 = list(range(7, 10)) # non-overlapping |
| 214 | idx2 = list(range(5, 10)) # overlapping |
| 215 | l0 = Label(idx0, pos[idx0], values[idx0], "lh", color="red") |
| 216 | l1 = Label(idx1, pos[idx1], values[idx1], "lh") |
| 217 | l2 = Label(idx2, pos[idx2], values[idx2], "lh", color=(0, 1, 0, 0.5)) |
| 218 | |
| 219 | assert_equal(len(l0), len(idx0)) |
| 220 | |
| 221 | l_good = l0.copy() |
| 222 | l_good.subject = "sample" |
| 223 | l_bad = l1.copy() |
| 224 | l_bad.subject = "foo" |
| 225 | pytest.raises(ValueError, l_good.__add__, l_bad) |
| 226 | pytest.raises(TypeError, l_good.__add__, "foo") |
| 227 | pytest.raises(ValueError, l_good.__sub__, l_bad) |
| 228 | pytest.raises(TypeError, l_good.__sub__, "foo") |
| 229 | |
| 230 | # adding non-overlapping labels |
| 231 | l01 = l0 + l1 |
| 232 | assert_equal(len(l01), len(l0) + len(l1)) |
| 233 | assert_array_equal(l01.values[: len(l0)], l0.values) |
| 234 | assert_equal(l01.color, l0.color) |
| 235 | # subtraction |
| 236 | assert_labels_equal(l01 - l0, l1, comment=False, color=False) |
| 237 | assert_labels_equal(l01 - l1, l0, comment=False, color=False) |
| 238 | |
| 239 | # adding overlapping labels |
| 240 | l02 = l0 + l2 |
| 241 | i0 = np.where(l0.vertices == 6)[0][0] |
| 242 | i2 = np.where(l2.vertices == 6)[0][0] |
| 243 | i = np.where(l02.vertices == 6)[0][0] |
| 244 | assert_equal(l02.values[i], l0.values[i0] + l2.values[i2]) |
| 245 | assert_equal(l02.values[0], l0.values[0]) |
| 246 | assert_array_equal(np.unique(l02.vertices), np.unique(idx0 + idx2)) |
| 247 | assert_equal(l02.color, _blend_colors(l0.color, l2.color)) |
| 248 | |
| 249 | # adding lh and rh |
| 250 | l2.hemi = "rh" |
| 251 | bhl = l0 + l2 |
| 252 | assert_equal(bhl.hemi, "both") |
| 253 | assert_equal(len(bhl), len(l0) + len(l2)) |
| 254 | assert_equal(bhl.color, l02.color) |
| 255 | assert "BiHemiLabel" in repr(bhl) |
| 256 | # subtraction |
| 257 | assert_labels_equal(bhl - l0, l2) |
| 258 | assert_labels_equal(bhl - l2, l0) |
| 259 | |
| 260 | bhl2 = l1 + bhl |
| 261 | assert_labels_equal(bhl2.lh, l01) |
| 262 | assert_equal(bhl2.color, _blend_colors(l1.color, bhl.color)) |
| 263 | assert_array_equal((l2 + bhl).rh.vertices, bhl.rh.vertices) # rh label |
| 264 | assert_array_equal((bhl + bhl).lh.vertices, bhl.lh.vertices) |
| 265 | pytest.raises(TypeError, bhl.__add__, 5) |
nothing calls this directly
no test coverage detected