MCPcopy
hub / github.com/mne-tools/mne-python / __add__

Method __add__

mne/label.py:325–393  ·  view source on GitHub ↗

Add Labels.

(self, other)

Source from the content-addressed store, hash-verified

323 return len(self.vertices)
324
325 def __add__(self, other):
326 """Add Labels."""
327 _validate_type(other, (Label, BiHemiLabel), "other")
328 if isinstance(other, BiHemiLabel):
329 return other + self
330 else: # isinstance(other, Label)
331 if self.subject != other.subject:
332 raise ValueError(
333 "Label subject parameters must match, got "
334 f'"{self.subject}" and "{other.subject}". Consider setting the '
335 "subject parameter on initialization, or "
336 "setting label.subject manually before "
337 "combining labels."
338 )
339 if self.hemi != other.hemi:
340 name = f"{self.name} + {other.name}"
341 if self.hemi == "lh":
342 lh, rh = self.copy(), other.copy()
343 else:
344 lh, rh = other.copy(), self.copy()
345 color = _blend_colors(self.color, other.color)
346 return BiHemiLabel(lh, rh, name, color)
347
348 # check for overlap
349 duplicates = np.intersect1d(self.vertices, other.vertices)
350 n_dup = len(duplicates)
351 if n_dup:
352 self_dup = [np.where(self.vertices == d)[0][0] for d in duplicates]
353 other_dup = [np.where(other.vertices == d)[0][0] for d in duplicates]
354 if not np.all(self.pos[self_dup] == other.pos[other_dup]):
355 err = (
356 f"Labels {repr(self.name)} and {repr(other.name)}: vertices "
357 "overlap but differ in position values"
358 )
359 raise ValueError(err)
360
361 isnew = np.array([v not in duplicates for v in other.vertices])
362
363 vertices = np.hstack((self.vertices, other.vertices[isnew]))
364 pos = np.vstack((self.pos, other.pos[isnew]))
365
366 # find position of other's vertices in new array
367 tgt_idx = [np.where(vertices == v)[0][0] for v in other.vertices]
368 n_self = len(self.values)
369 n_other = len(other.values)
370 new_len = n_self + n_other - n_dup
371 values = np.zeros(new_len, dtype=self.values.dtype)
372 values[:n_self] += self.values
373 values[tgt_idx] += other.values
374 else:
375 vertices = np.hstack((self.vertices, other.vertices))
376 pos = np.vstack((self.pos, other.pos))
377 values = np.hstack((self.values, other.values))
378
379 indcs = np.argsort(vertices)
380 vertices, pos, values = vertices[indcs], pos[indcs, :], values[indcs]
381
382 comment = f"{self.comment} + {other.comment}"

Callers

nothing calls this directly

Calls 5

copyMethod · 0.95
_validate_typeFunction · 0.85
_blend_colorsFunction · 0.85
BiHemiLabelClass · 0.85
LabelClass · 0.85

Tested by

no test coverage detected