-
Notifications
You must be signed in to change notification settings - Fork 575
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
Allow not scaling background maps for surface plots #3173
Conversation
👋 @alexisthual Thanks for creating a PR! Until this PR is ready for review, you can include the [WIP] tag in its title, or leave it as a github draft. Please make sure it is compliant with our contributing guidelines. In particular, be sure it checks the boxes listed below.
We will review it as quick as possible, feel free to ping us with questions if needed. |
Codecov Report
@@ Coverage Diff @@
## main #3173 +/- ##
==========================================
+ Coverage 90.93% 91.02% +0.09%
==========================================
Files 133 133
Lines 15386 15414 +28
Branches 3212 3219 +7
==========================================
+ Hits 13991 14031 +40
+ Misses 832 819 -13
- Partials 563 564 +1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Here are a few snippets working with the current implementation: Base snippet to run before the others: import matplotlib.pyplot as plt
from nilearn import datasets, plotting, surface
import numpy as np
from importlib import reload
# %% Load fsaverage5
fs5 = datasets.fetch_surf_fsaverage()
fs7 = datasets.fetch_surf_fsaverage(mesh="fsaverage7")
# %% Load sulc surface from fsaverage
sulc_fs5 = surface.load_surf_data(fs5.sulc_left)
sulc_fs7 = surface.load_surf_data(fs7.sulc_left)
# Create binary map indicating sulci and gyri
sulc_sign_fs5 = (np.sign(sulc_fs5) + 1) / 8 + 0.25
sulc_sign_fs7 = (np.sign(sulc_fs7) + 1) / 8 + 0.25
sulc_norm_fs5 = sulc_fs5 - sulc_fs5.min()
sulc_norm_fs5 = sulc_fs5 / sulc_fs5.max()
sulc_norm_fs7 = sulc_fs7 - sulc_fs7.min()
sulc_norm_fs7 = sulc_fs7 / sulc_fs7.max()
# %% Load random contrast map
motor_images = datasets.fetch_neurovault_motor_task()
surf_fs5 = surface.vol_to_surf(motor_images.images[0], fs5.pial_left)
surf_fs7 = surface.vol_to_surf(motor_images.images[0], fs7.pial_left)
|
Thanks a lot @alexisthual I can't do a full review this week but I think the new plots are really cool. Actually I think we should make it easy to use the binary backgrounds that you show ( |
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.
Thx for opening this !
No worries, this doesn't have to be merged soon.
Overall, it feels to me that
I'll do my best 😊 |
Here is a new plot which make me think that it makes more sense to keep Indeed, after experimenting a bit, I think people use the curvature sign (and not the sulc sign), which yields a much nicer rendering: plotting.plot_surf(
fs7.infl_left,
surf_fs7,
cmap="coolwarm",
threshold=1.5,
bg_map=curv_sign_fs7 + sulc_norm_fs7 / 8,
bg_on_data=True,
scale_bg_map=False,
)
plt.show() On a different note, it also works well for flat maps (see #3171 for more info): flat_fs7 = "/home/alexis/singbrain/fsaverage_flat/surfaces/flat_lh.gii"
# %%
fig = plotting.plot_surf(
flat_fs7,
surf_fs7,
cmap="coolwarm",
threshold=1.5,
bg_map=curv_sign_fs7,
bg_on_data=True,
scale_bg_map=False,
view="dorsal",
engine="plotly"
)
_ = fig.show(renderer="nteract") |
nilearn/plotting/html_surface.py
Outdated
else: | ||
bg_data = bg_map | ||
|
||
bg_data *= darkness |
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.
Since the darkness parameter is applied to background data, should we revise it to bg_darkness
or something along the line?
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.
thanks @alexisthual it looks good! as usual with plotting testing is tricky...
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.
Thx. One general comment is that we should demo these parameters to help users figure out.
In the last commits, I tried to make sure that all surface plotting methods (including I also edited what I reckon is the main nilearn example for plotting surfaces, so that it shows how to use Other than that, I think this PR is ready for review 🙂 |
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.
Thanks @alexisthual ! 👍
I had a quick look. I made some small suggestions, but LGTM overall.
@alexisthual thanks for the updates and opening the follow-up issues! Seems ready for another round of reviews. Or is there still any TODO's left on your side? |
I don't think there is any TODO left 😊 |
Perhaps this can be the topic for next week's office hour |
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.
I only found a minor glitch. It can be merged otherwise I think.
Co-authored-by: bthirion <bertrand.thirion@inria.fr>
nilearn/plotting/html_surface.py
Outdated
@@ -164,6 +263,27 @@ def view_img_on_surf(stat_map_img, surf_mesh='fsaverage5', | |||
If True, image is plotted on a black background. Otherwise on a | |||
white background. Default=False. | |||
|
|||
bg_maps : "auto" or list of (str or numpy.ndarray), optional |
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.
IIRC we agreed orally during one of the office hours that there would be no such parameter for now, or that it could only be a string corresponding to one of the fsaverage maps?
nilearn/plotting/surf_plotting.py
Outdated
@@ -1154,6 +1213,23 @@ def plot_img_on_surf(stat_map, surf_mesh='fsaverage5', mask_img=None, | |||
during projection of the volume to the surface. | |||
If ``None``, don't apply any mask. | |||
|
|||
bg_maps : "auto" or list of (str or numpy.ndarray), optional |
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.
same here
I'd rather not have a warning show up for plots that use the default values, especially as the message is rather cryptic. in any case we want the behavior / plots to remain correct for someone who uses the fsaverage sulcal depth with default parameters, so I don't think we need to scare users with a warning |
I get your concern but I would advocate we need this warning: indeed, it's a deprecation warning (maybe the warning message is not clear) stating that the current default parameters will change in the future. |
Maybe we can discuss this today during the office hours!
good idea, see you then!
|
During today's office hours, we discussed:
I implemented all 3 in the last commits, so the PR should be good to go! |
I think it should be good to go! could you trigger a full build of the documentation to make sure all examples look good? and as we mentioned we may want to visually check a few example pots -- eg the plot with default parameters for each function, or re-running some of the scripts in this PR's discussion, but otherwise I think it should be ready! thanks for all this work :) |
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.
LGTM.
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.
we just went through a bunch of examples prepared by @alexisthual with @ymzayek and everything looks good! thanks a bunch @alexisthual this is a big one :) ! 💯
Congrats ! |
* Enable not scaling background map for matplotlib * Missing part in previous commit * Enable bg_on_data and scale_bg_map for plotly * Update documentation with scale_bg_map * Correct spelling mistake * Address comments on default values for low-level functions * Fix threshold = None for plotly * Add surf scale_bg_map to plot_surf_stat_map * Fix tests (pep8 + plot_surf_stat_map description) * Fix typo causing tests to fail * Add scale_bg_map to plot_surf_roi + reorder arguments properly * Add scale_bg_map and bg_on_data to view_surf and view_img_on_surf * Use scale_bg_map=False in main surface plotting example * Update nilearn/plotting/surf_plotting.py Co-authored-by: Gensollen <nicolas.gensollen@gmail.com> * Update nilearn/plotting/surf_plotting.py Co-authored-by: Gensollen <nicolas.gensollen@gmail.com> * Update nilearn/plotting/surf_plotting.py Co-authored-by: Gensollen <nicolas.gensollen@gmail.com> * Add underscore to one_mesh_info and full_brain_info methods * Test different values of scale_bg_map for _get_vertexcolor * Fix pep8 * Address Jerome's & Nicolas' comments * Fix pep8 * Fix incorrect calculus in curv sign * Rename scale_bg_map to bg_map_rescaled * Proper color mixin function to merge foreground and background maps * Better defaults for view_img_to_surf * Better default for darkness parameter throughout codebase * Bugfix color mixin when bg_on_data=False * Correct color mixin for matplotlib * Update nilearn/plotting/tests/test_html_surface.py Co-authored-by: bthirion <bertrand.thirion@inria.fr> * Specify default value for bg_map_rescale in view_img_on_surf * Specify default value for bg_map_rescale in view_surf * Raise error in _mix_colormaps if colormaps have different shapes * Test plotting.html_surface._mix_colormaps() * Docstring in numpy-style * Factorize division by alpha channel * Curv sign as default bg map when inflate for plot_img_on_surf * Fix spelling mistake * Add whatsnew entry * Update nilearn/plotting/html_surface.py Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com> * [full doc] clearer docstring for bg_map in new methods * [full doc] * Update nilearn/_utils/docs.py Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com> * Update nilearn/plotting/html_surface.py Co-authored-by: Jérôme Dockès <jerome@dockes.org> * Factorise logic in _mix_colormaps * Bugfix modifying input argument in full_brain_info * Clearer docstrings for the color mixin util function * Bugfix color mixing function * Bugfix missing ifelse case in full_brain_info * [full doc] * Change default value for bg_map_rescale to "auto" * Modify example to use simpler background maps * Fix dimming effect by copying background surface * Prevent modifying arguments values in _get_vertexcolor * Update comments in tests for _get_vertexcolor * Use similar logic for _get_vertexcolor and _compute_facecolros_matplotlib * Add tests for _compute_facecolors_matplotlib * Fix incorrect use of darkness in _compute_facecolors_matplotlib * Clean comments and formulation * Explicitly name kwargs when calling _get_vertexcolor() * Correct docstring for bg_map for view_img_on_surf * Clearer handling lf alpha for matplotlib facecolors * Use correct value for alpha when testing _compute_facecolors_matplotlib * Increase default darkness from .5 to .7 * Fix pep8 missing line * Take 2 background maps for view_img_on_surf * Rename argument in view_img_on_surf * Enable customizing bgmap for plot_img_on_surf * Add docstring for background related args for plot_img_on_surf * Bugfix wrong index for background maps * Add tests for plot_img_on_surf * Change versionadded for plotting functions * Remove useless print in plotting tests * Add deprecation warnings for scaled background maps * Remove mentions to Surface data object * Use correct warning category * Update examples/01_plotting/plot_3d_map_to_surface_projection.py Co-authored-by: bthirion <bertrand.thirion@inria.fr> * Remove background_map warning ; remove bg_map_rescale param ; edit bg_map docstring * Fix typo * Remove useless files generated by example 04 * Remove bg_maps argument * [full doc] * Move enhancement description to correct section --------- Co-authored-by: Gensollen <nicolas.gensollen@gmail.com> Co-authored-by: bthirion <bertrand.thirion@inria.fr> Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com> Co-authored-by: Jérôme Dockès <jerome@dockes.org>
Closes #3169 .
Currently, this PR:
bg_on_data
forplotly
enginescale_bg_map
formatplotlib
andplotly
enginesI still have to:
bg_on_data
andscale_bg_map
forview_surf()