Get mri_head_t (from=mri, to=head) from mri filename.
(trans, fro="mri", to="head", allow_none=True, *, extra="")
| 484 | |
| 485 | |
| 486 | def _get_trans(trans, fro="mri", to="head", allow_none=True, *, extra=""): |
| 487 | """Get mri_head_t (from=mri, to=head) from mri filename.""" |
| 488 | types = (Transform, "path-like") |
| 489 | if allow_none: |
| 490 | types += (None,) |
| 491 | _validate_type(trans, types, "trans", extra=extra) |
| 492 | if _path_like(trans): |
| 493 | if trans == "fsaverage": |
| 494 | trans = Path(__file__).parent / "data" / "fsaverage" / "fsaverage-trans.fif" |
| 495 | trans = Path(trans) |
| 496 | if not trans.is_file(): |
| 497 | raise OSError(f'trans file "{trans}" not found') |
| 498 | if trans.suffix in [".fif", ".gz"]: |
| 499 | fro_to_t = read_trans(trans) |
| 500 | else: |
| 501 | # convert "-trans.txt" to "-trans.fif" mri-type equivalent |
| 502 | # these are usually actually in to_fro form |
| 503 | t = np.genfromtxt(trans) |
| 504 | if t.ndim != 2 or t.shape != (4, 4): |
| 505 | raise RuntimeError(f'File "{trans}" did not have 4x4 entries') |
| 506 | fro_to_t = Transform(to, fro, t) |
| 507 | elif isinstance(trans, Transform): |
| 508 | fro_to_t = trans |
| 509 | trans = "instance of Transform" |
| 510 | else: |
| 511 | assert trans is None |
| 512 | fro_to_t = Transform(fro, to) |
| 513 | trans = "identity" |
| 514 | # it's usually a head->MRI transform, so we probably need to invert it |
| 515 | fro_to_t = _ensure_trans(fro_to_t, fro, to, extra=extra) |
| 516 | return fro_to_t, trans |
| 517 | |
| 518 | |
| 519 | def combine_transforms(t_first, t_second, fro, to): |