-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
ENH: Multiple raw instance support to head pos average #12445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add support for multiple raw instances in :func:`mne.preprocessing.compute_average_dev_head_t` by `Eric Larson`_. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |||||
| _validate_type, | ||||||
| logger, | ||||||
| verbose, | ||||||
| warn, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -293,27 +294,68 @@ def annotate_movement( | |||||
| return annot, disp | ||||||
|
|
||||||
|
|
||||||
| def compute_average_dev_head_t(raw, pos): | ||||||
| @verbose | ||||||
| def compute_average_dev_head_t(raw, pos, *, verbose=None): | ||||||
| """Get new device to head transform based on good segments. | ||||||
|
|
||||||
| Segments starting with "BAD" annotations are not included for calculating | ||||||
| the mean head position. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| raw : instance of Raw | ||||||
| Data to compute head position. | ||||||
| pos : array, shape (N, 10) | ||||||
| The position and quaternion parameters from cHPI fitting. | ||||||
| raw : instance of Raw | list of Raw | ||||||
| Data to compute head position. Can be a list containing multiple raw | ||||||
| instances. | ||||||
| pos : array, shape (N, 10) | list of ndarray | ||||||
| The position and quaternion parameters from cHPI fitting. Can be | ||||||
| a list containing multiple position arrays, one per raw instance passed. | ||||||
| %(verbose)s | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| dev_head_t : instance of Transform | ||||||
| New ``dev_head_t`` transformation using the averaged good head positions. | ||||||
|
|
||||||
| Notes | ||||||
| ----- | ||||||
| .. versionchanged:: 1.7 | ||||||
| Support for multiple raw instances and position arrays was added. | ||||||
| """ | ||||||
| # Get weighted head pos trans and rot | ||||||
| if not isinstance(raw, (list, tuple)): | ||||||
| raw = [raw] | ||||||
| if not isinstance(pos, (list, tuple)): | ||||||
| pos = [pos] | ||||||
| if len(pos) != len(raw): | ||||||
| raise ValueError( | ||||||
| f"Number of head positions ({len(pos)}) must match the number of raw " | ||||||
| f"instances ({len(raw)})" | ||||||
| ) | ||||||
| hp = list() | ||||||
| dt = list() | ||||||
| for ri, (r, p) in enumerate(zip(raw, pos)): | ||||||
| _validate_type(r, BaseRaw, f"raw[{ri}]") | ||||||
| _validate_type(p, np.ndarray, f"pos[{ri}]") | ||||||
| hp_, dt_ = _raw_hp_weights(r, p) | ||||||
| hp.append(hp_) | ||||||
| dt.append(dt_) | ||||||
| hp = np.concatenate(hp, axis=0) | ||||||
| dt = np.concatenate(dt, axis=0) | ||||||
| dt /= dt.sum() | ||||||
| best_q = _average_quats(hp[:, 1:4], weights=dt) | ||||||
| trans = np.eye(4) | ||||||
| trans[:3, :3] = quat_to_rot(best_q) | ||||||
| trans[:3, 3] = dt @ hp[:, 4:7] | ||||||
| dist = np.linalg.norm(trans[:3, 3]) | ||||||
| if dist > 1: # less than 1 meter is sane | ||||||
| warn(f"Implausible head position detected: {dist} meters from device origin") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tempted to do this but it bumps the line over the length limit
Suggested change
|
||||||
| dev_head_t = Transform("meg", "head", trans) | ||||||
| return dev_head_t | ||||||
|
|
||||||
|
|
||||||
| def _raw_hp_weights(raw, pos): | ||||||
| sfreq = raw.info["sfreq"] | ||||||
| seg_good = np.ones(len(raw.times)) | ||||||
| trans_pos = np.zeros(3) | ||||||
| hp = pos.copy() | ||||||
| hp_ts = hp[:, 0] - raw._first_time | ||||||
|
|
||||||
|
|
@@ -353,19 +395,7 @@ def compute_average_dev_head_t(raw, pos): | |||||
| assert (dt >= 0).all() | ||||||
| dt = dt / sfreq | ||||||
| del seg_good, idx | ||||||
|
|
||||||
| # Get weighted head pos trans and rot | ||||||
| trans_pos += np.dot(dt, hp[:, 4:7]) | ||||||
|
|
||||||
| rot_qs = hp[:, 1:4] | ||||||
| best_q = _average_quats(rot_qs, weights=dt) | ||||||
|
|
||||||
| trans = np.eye(4) | ||||||
| trans[:3, :3] = quat_to_rot(best_q) | ||||||
| trans[:3, 3] = trans_pos / dt.sum() | ||||||
| assert np.linalg.norm(trans[:3, 3]) < 1 # less than 1 meter is sane | ||||||
| dev_head_t = Transform("meg", "head", trans) | ||||||
| return dev_head_t | ||||||
| return hp, dt | ||||||
|
|
||||||
|
|
||||||
| def _annotations_from_mask(times, mask, annot_name, orig_time=None): | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the error messages here (which will reference
raw[0]orpos[0]) might be slightly confusing for the case where a user passed only 1 raw / pos (not a list). But getting it "right" is pretty convoluted:if you can think of a cleaner/simpler way please go for it, otherwise probably fine as-is.