Skip to content

Conversation

@larsoner
Copy link
Member

@larsoner larsoner commented Nov 11, 2025

Sometimes the Neuromag system screws up the initial HPI fitting, which then messes up dev_head_t and also any subsequent continuous head position estimation. This PR will fix that. Lots of TODOs in the code but I wanted to get it up to have a place to track progress.

  • Get it working with problematic dataset
  • Add test to show necessity of if proj_op is None addition
  • Show that it's essentially a no-op on correct data
  • Add whats_new entry
  • Add elements from problematic dataset to tests
  • Show it fails without refitting
  • Show it works with refitting
  • Ensure all TODOs inline are addressed

@larsoner larsoner added this to the 1.11 milestone Nov 13, 2025
@larsoner larsoner marked this pull request as ready for review November 13, 2025 21:59
@larsoner
Copy link
Member Author

@drammock I think this one is good to go!

@larsoner
Copy link
Member Author

I don't see this one as a blocker for 1.11 -- can be an early 1.12 so bumping milestone

@larsoner larsoner removed this from the 1.11 milestone Nov 20, 2025
@larsoner larsoner added this to the 1.12 milestone Nov 20, 2025
Copy link
Member

@drammock drammock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few comments/suggestions/questions. Didn't look too closely at the tests.

Comment on lines 368 to 383
assert isinstance(desired, Transform), "Both must be Transform or ndarray"
assert actual["from"] == desired["from"]
assert actual["to"] == desired["to"]
actual = actual["trans"]
desired = desired["trans"]
assert isinstance(actual, np.ndarray)
assert isinstance(desired, np.ndarray)
assert actual.shape == (4, 4)
assert desired.shape == (4, 4)
angle, dist = angle_distance_between_rigid(
actual, desired, angle_units="deg", distance_units="m"
)
assert dist <= dist_tol, (
f"{1000 * dist:0.3f} > {1000 * dist_tol:0.3f} mm translation"
)
assert angle <= angle_tol, f"{angle:0.3f} > {angle_tol:0.3f}° rotation"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good for more of these asserts to have explanatory text for failures

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the pytest default assert output should ideally be good enough. But it turned out:

assert actual.shape == (4, 4)

Didn't say anything. BUT! I discovered:

https://docs.pytest.org/en/stable/how-to/assert.html#assertion-introspection-details

can make it say something like this (if I change it to (3, 4) just to get it to fail, which seems clear enough to me given the traceback

mne/tests/test_chpi.py:976: in test_refit_hpi_locs_problematic
    assert_trans_allclose(
E   assert (4, 4) == (3, 4)
E     
E     At index 0 diff: 4 != 3
E     Use -v to get more diff

so knowing that it overwrites some of it, I can at least add a little less text. Will push a commit shortly

mne/chpi.py Outdated
for the best goodness of fit between digitized coil locations and
(rigid-transformed) fitted coil locations.
4. Subselect coils to use for fitting ``dev_head_t`` based on ``gof_limit``,
``dist_limit``, and ``max_use``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``dist_limit``, and ``max_use``.
``dist_limit``, and ``use``.

or, change param name to max_use, that might be better actually?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it can be a list of coils to use, too, which is why I changed it from max_use (when initially I only allowed setting it as a max count) to use (when I also allowed it to be a list of int)

@larsoner
Copy link
Member Author

Thanks @drammock I think I addressed the comments! I'll mark for merge-when-green to sneak it in for 1.11. I think it should be good enough for people to try

@larsoner larsoner enabled auto-merge (squash) November 20, 2025 19:22
@larsoner larsoner merged commit 0a06818 into mne-tools:main Nov 20, 2025
32 checks passed
@larsoner larsoner deleted the refit branch November 20, 2025 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants