Repository navigation
WIP: MRI/Head Coregistration / Scaling FsAverage - #379
christianbrodbeck wants to merge 151 commits into
Conversation
|
@dengemann mentioned Head-MRI coregistration (#366 (comment)). I also made a traits GUI for that and I can add it to this PR since it's very similar to scaling fsaverage. The main issue I encountered was that the fit requires the fiducials as an initial constraint, and fiducials do not exist for MRI files. Since I did not find a good way with a traits UI to switch back and forth between two tasks (setting fiducials and fitting) I made a separate GUI for creating a fiducials file first. Maybe there is a better way to do this? I can add the two GUIs here as a basis for discussion. |
|
@agramfort |
|
fine with me. for me to follow where you're heading can you describe a workflow / code to use what you wrote? A kind of draft of doc to make sure I agree with the philosophy? |
|
@agramfort Could you have a look at what I added to transforms.py (https://github.com/mne-tools/mne-python/pull/379/files#diff-4), whether you would prefer this to be somewhere else. For the coregistratrion, a first implementation is in place, although I still want to clean it up. The fsaverage folder has to contain all necessary MRI files as well as the fiducials file from the mne download. Then, there are these two options: This would be a draft of the doc: |
There was a problem hiding this comment.
I usually avoid any use of np.matrix. Could we make it an array?
There was a problem hiding this comment.
We could. I'm curious as to your reason, is it because np.matrix's behavior is inconsistent with arrays?
There was a problem hiding this comment.
It's not. The operators are overlpoaded differently. For matrix * means dot
product, for arrays element wise operations. There's another hand full of
features which make it different.
On Tue, Jan 15, 2013 at 2:54 PM, Christian Brodbeck <
[email protected]> wrote:
In mne/transforms/transforms.py:
pts = trans \* ptspts = np.array(pts[:3].T)- return pts
+def rotation(x=0, y=0, z=0):
- "Create a rotation matrix"
- r = np.matrix([[cos(y) * cos(z),
- cos(x) \* sin(z) + sin(x) \* sin(y) \* cos(z),sin(x) \* sin(z) + cos(x) \* sin(y) \* cos(z), 0],[cos(y) \* sin(z),cos(x) \* cos(z) + sin(x) \* sin(y) \* sin(z),- sin(x) \* cos(z) + cos(x) \* sin(y) \* sin(z), 0],[-sin(y), sin(x) \* cos(y), cos(x) \* cos(y), 0],[0, 0, 0, 1]], dtype=float)- return r
We could. I'm curious as to your reason, is it because np.matrix's
behavior is inconsistent with arrays?—
Reply to this email directly or view it on GitHubhttps://github.com//pull/379/files#r2649137.
There was a problem hiding this comment.
my reason is that with python you never know by reading the code if it's an array or a matrix. If everything are arrays then A * B is clear whatever the context. I never use np.matrix anymore.
fine there. Besides the use of np.matrix
looks pretty clean ! so this creates in subjects_dir a new subject called fsa_sc? with all |
Yes, should, although I am not sure if I am considering all the possibilities yet. |
|
Here's an update on the coregistration GUIs. Let me know if you think this should be prepared for potential inclusion. The fitting is done by functions that are already separate or that could be extracted if they would be useful independent of the GUI. Fitting an MRI to a head: Fitting a head to an existing MRI requires fiducials, which can be generated using this GUI: Then the head-mri coregistration can be done: |
There was a problem hiding this comment.
avoid linalg from numpy. Use scipy.linalg
There was a problem hiding this comment.
Ok, is it a better implementation?
There was a problem hiding this comment.
Ok, is it a better implementation?
numpy can avoid the dependency on fortran with blas/lapack and on some
systems use it's own C implementation of linalg which is much slower
and less accurate.
There was a problem hiding this comment.
Thanks for the info!
|
my review is not done but I just tried with the sample data and got: No such file or directory: '/Users/alex/work/data/subjects/fsaverage/bem/fsaverage-head.fif' should we ship the fsaverage-head.fif with the sample data? |
|
Yes the fsaverage in the sample data set has an empty bem directory. |
|
I just tried, it it seems to work quite well. Nice job :-). Two things:
|
|
One more thing.. in |
I'll do that
The nasion is kept constant (you can manually adjust it before running the fit). The fit uses scipy leastsq to minimize the distance from each digitizer head shape point to the closest mri point. Alternatively the head mri coregistration utility can use just the LAP/RAP fiducials.
The fiducials utility currently places the fiducials only to vertex locations (that's what Mayavi offers). In case that's not accurate enough I included the fields for manually adjusting the coordinates
Definitely
The thing is with points you can't really estimate how far they are from the surface either – they are either in or out of the mri. I was thinking about having something like lines connecting each point with the center, that might allow you do estimate how far each point is sticking out; I didn't find an easy way to implement it yet though. Transparency unfortunately isn't handled well in mayavi (at least on my system). I was thinking to add some display options as traits though. |
|
@christianmbrodbeck FYI the following point matching method is implemented in MNE (thanks @mshamalainen ): "Besl, P., & McKay, N. (1992). A method for registration of 3-D shapes. IEEE Trans PAMI" The method implemented here seems to be optimizing them same objective function, but I wonder if there are any guarantees that it converges to the same solution. |
|
Re quick how to: Here is a draft for a manual page. Currently, Strangely automatic alignment of the sample dataset head always ends up crooked, but that's not a problem I had with my own subjects... @mluessi thanks for the info, I don't know to what degree one could make the methods converge by customizing the |
|
nice ! quick feedback on usability:
Also I've seen some code relative to the forward modeling. I think |
I was planning to make arguments with templates as default, such as
That would be preferable but would require me to spend some more time with traits and co ... yes for the long term though
You mean pulling the data from your mne and freesurfer application folders and running the setup? I can look into that.
I'll go with
Yes.
It's only the preparatory |
+1
good we agree :)
yes do the necessary copies automatically.
+1
I don't think we should as not everybody uses the same parameters. I would follow the mne workflow and add an mne.setup_forward_model |
Yes you're right, there are too many parameters |
|
Btw. as we are at it how is it going here? I was already about to ask for it ;-) |
|
Hi @kambysese , thanks for testing this! The controls in your screenshot look like the latest version. Thanks also for your suggestions regarding the controls:
A Range Editor as for opacity might be problematic because when its range is small it limits the range of possible values, and when its range is large its steps will be too big for fine adjustments. What I could do is have a separate line for each value and add a [+] and a [-] button that increment and decrement the value in say 1 mm steps? Do you think that would help?
I am not sure what to do about this, maybe complementary text entry field?
It might simply be a question of the fiducials placement. If you think the fiducials are more accurate than head shape points you could use the fiducials for the fit. |
|
Hi @christianmbrodbeck thanks for making this! |
|
I added the +/- buttons. I can't find something like a grid sizer for traits, that's why the buttons are not vertically aligned... What do you think? About the drop-down menu, on Mac I can scroll through the list with the mouse wheel, as well as by hovering over the little arrows on either end. Nothing like that works for you? |
|
@christianmbrodbeck buttons look great! As long as they get the job done the alignment is an aesthetic issue. Maybe you can clear up a couple things for me. How do the scale parameters in the top panel (i.e., scale x1, & scale x3) translate into Rt, Ant, Superior? Once I am satisfied with the coreg/scaling I save the *trans.fif file. But I haven't been able to make anything happen in the subjects freesurfer directory. Obviously the sourcespace and leadfield cannot be created at this point for subjects without MRI, right? I am a bit more confused about whether or not something should happen in the subjects freesurfer directory, namely, the whereabouts of the scaled MRI? Or does the *trans.fif file contain the scaling/head-MRI transformation (which is applied to the BEM with mne_do_forward). Is this approach similar to what you do in cases without MRI? Sorry about all the confusion. Regarding the drop down menu, I will confirm on UBUNTU 13, but it may be a non-issue. Initially, I didn't realize that only directories with a BEM subdirectory are included. Nevertheless, the menu initially shows with arrows on either end, then scales and the arrows disappear, it may be some Unity quirk. BTW how do I update my local mne-copy to include the new features? Thanks in advance. |
When MRI scaling is set to "1 param", only the "Anterior" axis field is editable, but it's value is used for all axes (and the field for the other axes should also update whenever the "Anterior" value changes).
If "No scaling" is selected, all that is created is the trans file. The trans.fif file contains rotation and translation info but not scaling (because the transformation from the trans file is applied to the head shape, not the MRI, and we don't want to scale the original subject's head shape). Creating source spaces etc. should then be done using the command line utility. If on the other hand you enabled scaling, then after pressing "save" the GUI should ask you for a name for the new MRI and start processing the MRI in the background, creating a new MRI subject folder. Existing MRI folders should never be modified (unless you save the trans file there). Info about current processing is displayed under the save button: If these things don't happen maybe there's a bug, let me know!
navigate to your mne-python folder and run to update your local copy of my branch and then install that updated copy. |
|
Currently the GUI scales the bem file and then a new source space should be created from the scaled bem file. I wonder if it might be more efficient to create the source space first and then scale it. In experiments where all subjects use the common brain, that should remove the need for morphing. For ico source spaces the result should be the same anyways, because they are based on subdivision, right? For others (volume? @mluessi ) removing the need for morphing might speed things up. |
|
Hi @christianmbrodbeck. Currently I am using your Trans repo with MNE version 0.7 however I am not seeing the new additions i.e., +/- buttons. More troubling is that beyond generating the *trans.fif file, nothing else is happening. The subject folder is not created, hence there is no MRI/BEM file. I've tried this with or without the option(s) prepare bem and setup source space: In either case the outcome is no new subject directory, and no files. In the above case the terminal standard out reads: In the highlighted region, I am assuming the "u'" has to be in the mne_prepare_bem_model command? "FYI my permissions for $SUBJECTS_DIR is rwxrwxr-x. |
|
@christianmbrodbeck to update I managed to update to the latest version of your branch. I'd left out the |
|
@christianmbrodbeck same result with other data sets. I am at a loss here. |
|
@kambysese the output from the subprocess call (last line in your log) says you are missing (the "u" in front of strings only marks them as unicode and is not necessary unless they contain unicode characters) |
|
I was wondering what the status of this pull request is. I fear that if there are any added features, it may turn into a project itself ;). Any additional features can also be implemented if this ever gets merged. |
|
@t3on It will be worked on soon. Meanwhile please do report any problems or requests (even for future PRs). I added a list of open issues to the first post. |
|
@t3on good you ask and thanks for providing this valuable feedback, it's helpful for this PR's success to know it works for you and your colleagues. @christianmbrodbeck Thanks for ctaching up. There is certainly a generalized feature stop for this branch --- anything new, please after merge. I was believing we had persistent issues with regard to mayavi and tests coverage but this does not seem to be the case anymore. Two milestones for merge:
with this
All other improvements, if it's not COSMITS or DOC should be postponed. Generally I think what you could do is to stash commits in way they summarize essential additions, reducing @agramfort @mluessi @Eric89GXL |
|
@christianmbrodbeck I see a lot of changes in files outside of the gui modules. Some of then seem to be fixing issues of current master (eg. in the kit module). This is not good and I would have much preferred to quick review / quick merge for this. I know it's annoying to change branches, rebase to keep branches in sync with upstream but it's the only way to go when we work all together on different features. Also you added some functions that wrap mne C commands which can be a debate. For example @Eric89GXL now reimplements setup of source spaces that we plan to improve in the near future to support deep sources. Remember that when we add features we start a maintenance / deprecation cycle that takes time for all of us. @christianmbrodbeck how would you feel reopening PRs for your changes outside of the .gui code? That would allow quick review / quick merge. As @t3on this branch with 150 commits and 25 changed files turns into a project in itself ... ;) |
+100 |
The reason I did not add those features in separate PRs is that they evolved with the GUI.
I could just make any functions private that we don't want to maintain, like the C command wrappers.
As discussed above I can do it, but I feel it will take a lot of time and I am not sure it will make reviewing that much easier (since it is already possible here to view changes by file). |
why do changes in kit constants affect the GUI? it would take 5 minutes to open a PR for general functions like: read_fiducials, write_fiducials ?
that's certainly necessary.
basically you're asking us to spend more time reviewing (reviewing time |
|
I agree almost 5,000 lines is probably too much to review when scattered across topics. @christianmbrodbeck what do you think about breaking into consecutive PRs:
This would greatly facilitate review. Does that sound reasonable? |
@agramfort I don't remember, specifically. However, KIT conversion does sensor - head shape coregistration, and in order to improve this step kit2fiff is now using the same transform functionality as the MRI coregistration. I'm sorry to annoy you, but since I did not know what underlying functions had to do for the coregistration I could not implement them incrementally. This was too big to project with too little information at the outset to plan everything out from the bottom up. @Eric89GXL I am submitting the fiducials IO. After that I would continue with the transform functionality (since the KIT relies on them) |











Following up on #369
Open Issues: