From 084422882d6dbf53cf4f2b9151df4b715f7f0e70 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 9 Jul 2024 09:18:33 +0200 Subject: [PATCH 1/6] Scale points and labels in montage plot --- mne/channels/montage.py | 2 +- mne/viz/montage.py | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mne/channels/montage.py b/mne/channels/montage.py index ea6d7ba92be..fa92536d630 100644 --- a/mne/channels/montage.py +++ b/mne/channels/montage.py @@ -369,7 +369,7 @@ def __repr__(self): @copy_function_doc_to_method_doc(plot_montage) def plot( self, - scale_factor=20, + scale_factor=1, show_names=True, kind="topomap", show=True, diff --git a/mne/viz/montage.py b/mne/viz/montage.py index 935a306e0d9..3381c76c1b5 100644 --- a/mne/viz/montage.py +++ b/mne/viz/montage.py @@ -16,7 +16,7 @@ @verbose def plot_montage( montage, - scale_factor=20, + scale_factor=1, show_names=True, kind="topomap", show=True, @@ -51,6 +51,8 @@ def plot_montage( fig : instance of matplotlib.figure.Figure The figure object. """ + import matplotlib.pyplot as plt + from ..channels import DigMontage, make_dig_montage _check_option("kind", kind, ["topomap", "3d"]) @@ -93,6 +95,20 @@ def plot_montage( sphere=sphere, axes=axes, ) + + # scale points collection = fig.axes[0].collections[0] - collection.set_sizes([scale_factor]) + collection.set_sizes([scale_factor * 10]) + + # scale labels + labels = fig.findobj(match=plt.Text) + x_label, y_label = fig.axes[0].xaxis.label, fig.axes[0].yaxis.label + z_label = fig.axes[0].zaxis.label if kind == "3d" else None + tick_labels = fig.axes[0].get_xticklabels() + fig.axes[0].get_yticklabels() + if kind == "3d": + tick_labels += fig.axes[0].get_zticklabels() + for label in labels: + if label not in [x_label, y_label, z_label] + tick_labels: + label.set_fontsize(label.get_fontsize() * scale_factor) + return fig From 41e480dacdf6aa46127d96d84bc260d57ee71e88 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 9 Jul 2024 11:00:07 +0200 Subject: [PATCH 2/6] Add changelog entry --- doc/changes/devel/12703.newfeature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changes/devel/12703.newfeature.rst diff --git a/doc/changes/devel/12703.newfeature.rst b/doc/changes/devel/12703.newfeature.rst new file mode 100644 index 00000000000..6761dfa36ec --- /dev/null +++ b/doc/changes/devel/12703.newfeature.rst @@ -0,0 +1 @@ +Montage plots created with :meth:`~mne.channels.DigMontage.plot` now scale both the channel dots *and* channel names (if desired); the default is `scaling_factor=1` (factors less than 1 will scale down, whereas factors greater than 1 will scale up), by `Clemens Brunner`_. \ No newline at end of file From c00ab277a2b7758f8eef8e56413c19ce5d33775d Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Thu, 11 Jul 2024 11:47:13 +0200 Subject: [PATCH 3/6] Monospace RST --- doc/changes/devel/12703.newfeature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/devel/12703.newfeature.rst b/doc/changes/devel/12703.newfeature.rst index 6761dfa36ec..4e4de523ec5 100644 --- a/doc/changes/devel/12703.newfeature.rst +++ b/doc/changes/devel/12703.newfeature.rst @@ -1 +1 @@ -Montage plots created with :meth:`~mne.channels.DigMontage.plot` now scale both the channel dots *and* channel names (if desired); the default is `scaling_factor=1` (factors less than 1 will scale down, whereas factors greater than 1 will scale up), by `Clemens Brunner`_. \ No newline at end of file +Montage plots created with :meth:`~mne.channels.DigMontage.plot` now scale both the channel dots *and* channel names (if desired); the default is ``scaling_factor=1`` (factors less than 1 will scale down, whereas factors greater than 1 will scale up), by `Clemens Brunner`_. \ No newline at end of file From 158414ee3f004e809eaa663f417179c77bc43457 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Fri, 12 Jul 2024 09:55:10 +0200 Subject: [PATCH 4/6] Add new scale parameter and deprecate scale_factor --- doc/changes/devel/12703.newfeature.rst | 2 +- mne/channels/montage.py | 7 +++- mne/viz/montage.py | 56 ++++++++++++++++++-------- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/doc/changes/devel/12703.newfeature.rst b/doc/changes/devel/12703.newfeature.rst index 4e4de523ec5..0c6afa4468b 100644 --- a/doc/changes/devel/12703.newfeature.rst +++ b/doc/changes/devel/12703.newfeature.rst @@ -1 +1 @@ -Montage plots created with :meth:`~mne.channels.DigMontage.plot` now scale both the channel dots *and* channel names (if desired); the default is ``scaling_factor=1`` (factors less than 1 will scale down, whereas factors greater than 1 will scale up), by `Clemens Brunner`_. \ No newline at end of file +Montage plots created with :meth:`~mne.channels.DigMontage.plot` now scale both the channel dots *and* channel names with the new ``scale`` parameter. The default is ``scale=1`` (factors less than 1 will scale down, whereas factors greater than 1 will scale up). The previous ``scale_factor`` parameter only affected marker size, and this parameter is now deprecated. By `Clemens Brunner`_. \ No newline at end of file diff --git a/mne/channels/montage.py b/mne/channels/montage.py index fa92536d630..540cf25d459 100644 --- a/mne/channels/montage.py +++ b/mne/channels/montage.py @@ -58,6 +58,7 @@ _validate_type, copy_function_doc_to_method_doc, fill_doc, + logger, verbose, warn, ) @@ -369,17 +370,19 @@ def __repr__(self): @copy_function_doc_to_method_doc(plot_montage) def plot( self, - scale_factor=1, + *, + scale=None, + scale_factor=None, show_names=True, kind="topomap", show=True, sphere=None, - *, axes=None, verbose=None, ): return plot_montage( self, + scale=scale, scale_factor=scale_factor, show_names=show_names, kind=kind, diff --git a/mne/viz/montage.py b/mne/viz/montage.py index 3381c76c1b5..4b36811a4b7 100644 --- a/mne/viz/montage.py +++ b/mne/viz/montage.py @@ -16,12 +16,13 @@ @verbose def plot_montage( montage, - scale_factor=1, + *, + scale=None, + scale_factor=None, show_names=True, kind="topomap", show=True, sphere=None, - *, axes=None, verbose=None, ): @@ -31,8 +32,11 @@ def plot_montage( ---------- montage : instance of DigMontage The montage to visualize. + scale : float + Determines the scale of the channel points and labels; values < 1 will scale + down, whereas values > 1 will scale up. Default to None, which implies 1. scale_factor : float - Determines the size of the points. + Determines the size of the points. Deprecated, use scale instead. show_names : bool | list Whether to display all channel names. If a list, only the channel names in the list are shown. Defaults to True. @@ -55,6 +59,18 @@ def plot_montage( from ..channels import DigMontage, make_dig_montage + if scale_factor is not None: + msg = ( + "scale_factor has been deprecated and will be removed. Use scale instead." + ) + if scale is not None: + raise ValueError( + " ".join(["scale and scale_factor cannot be used together.", msg]) + ) + logger.info(msg) + if scale is None: + scale = 1 + _check_option("kind", kind, ["topomap", "3d"]) _validate_type(montage, DigMontage, item_name="montage") ch_names = montage.ch_names @@ -96,19 +112,25 @@ def plot_montage( axes=axes, ) - # scale points - collection = fig.axes[0].collections[0] - collection.set_sizes([scale_factor * 10]) - - # scale labels - labels = fig.findobj(match=plt.Text) - x_label, y_label = fig.axes[0].xaxis.label, fig.axes[0].yaxis.label - z_label = fig.axes[0].zaxis.label if kind == "3d" else None - tick_labels = fig.axes[0].get_xticklabels() + fig.axes[0].get_yticklabels() - if kind == "3d": - tick_labels += fig.axes[0].get_zticklabels() - for label in labels: - if label not in [x_label, y_label, z_label] + tick_labels: - label.set_fontsize(label.get_fontsize() * scale_factor) + if scale_factor is not None: + # scale points + collection = fig.axes[0].collections[0] + collection.set_sizes([scale_factor]) + + if scale is not None: + # scale points + collection = fig.axes[0].collections[0] + collection.set_sizes([scale * 10]) + + # scale labels + labels = fig.findobj(match=plt.Text) + x_label, y_label = fig.axes[0].xaxis.label, fig.axes[0].yaxis.label + z_label = fig.axes[0].zaxis.label if kind == "3d" else None + tick_labels = fig.axes[0].get_xticklabels() + fig.axes[0].get_yticklabels() + if kind == "3d": + tick_labels += fig.axes[0].get_zticklabels() + for label in labels: + if label not in [x_label, y_label, z_label] + tick_labels: + label.set_fontsize(label.get_fontsize() * scale) return fig From 716493cc4c76cd55ed53f757dd8ff0d05080745f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 12 Jul 2024 07:57:00 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/channels/montage.py | 1 - mne/viz/montage.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/mne/channels/montage.py b/mne/channels/montage.py index 540cf25d459..8ebbce0cfc0 100644 --- a/mne/channels/montage.py +++ b/mne/channels/montage.py @@ -58,7 +58,6 @@ _validate_type, copy_function_doc_to_method_doc, fill_doc, - logger, verbose, warn, ) diff --git a/mne/viz/montage.py b/mne/viz/montage.py index 4b36811a4b7..707f0f07cd5 100644 --- a/mne/viz/montage.py +++ b/mne/viz/montage.py @@ -60,9 +60,7 @@ def plot_montage( from ..channels import DigMontage, make_dig_montage if scale_factor is not None: - msg = ( - "scale_factor has been deprecated and will be removed. Use scale instead." - ) + msg = "scale_factor has been deprecated and will be removed. Use scale instead." if scale is not None: raise ValueError( " ".join(["scale and scale_factor cannot be used together.", msg]) From ca848a6c722c3e53efcfa67f294d8d47e1152334 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Fri, 12 Jul 2024 09:59:10 +0200 Subject: [PATCH 6/6] Fix old behavior --- mne/viz/montage.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mne/viz/montage.py b/mne/viz/montage.py index 707f0f07cd5..4e45ac0a513 100644 --- a/mne/viz/montage.py +++ b/mne/viz/montage.py @@ -114,8 +114,7 @@ def plot_montage( # scale points collection = fig.axes[0].collections[0] collection.set_sizes([scale_factor]) - - if scale is not None: + elif scale is not None: # scale points collection = fig.axes[0].collections[0] collection.set_sizes([scale * 10])