Repository navigation
[topoplot] Draw emarker/emarker2 and validate option names - #697
Conversation
Implement EEGLAB-compatible emarker, emarker2, emarkersize/emsize, electcolor/ecolor, and emarkersize1chan/emarkersizemark rendering in topoplot, support EEGLAB electrodes aliases, and replace the open kwargs sink with an explicit case-insensitive allow-list that raises ValueError on unknown parameters. Simplify newtimef's single-channel inset to use emarkersize1chan directly and update tests and docs. Fixes sccn#683
SpecificationProblem Approach
Key code _COMPAT_IGNORED_OPTIONS = frozenset({"iclabel", "verbose", "chaninfo", "masksurf"})
_ALLOWED_OPTIONS = _IMPLEMENTED_OPTIONS | _COMPAT_IGNORED_OPTIONS
# Option validation at entry (_parse_topoplot_options):
for raw_key, value in kwargs.items():
key = str(raw_key).lower()
if key not in _ALLOWED_OPTIONS:
raise ValueError(f"topoplot: Unknown input parameter '{raw_key}'")
# emarker / emarker2 partition matching topoplot.m:913-914:
mark2_set = set(marker2_style["chans"].tolist())
mark2_mask = np.array([int(idx + 1) in mark2_set for idx in pltchans], dtype=bool)
mark1_mask = ~mark2_mask
# Zero-warning PathCollection rendering (_draw_markers):
if not MarkerStyle(marker).is_filled():
return ax.scatter(x_arr, y_arr, marker=marker, s=size_sq, color=color, linewidths=linewidth, zorder=zorder)
facecolors = color if (filled or marker in {".", "*"}) else "none"Tests & MATLAB reference evidence
|
arnodelorme
left a comment
There was a problem hiding this comment.
This review was written by an AI assistant during a nightly check, not by Arno.
Code review
- Overall assessment: needs changes
- Highest-risk area: the new allow-list in
topoplot, as seen from callers that forward mixed option dicts (pop_spectopo,pop_envtopo) - Merge recommendation: needs changes
Reviewed at b78f1c0. Tests run on that commit: tests/test_topoplot.py and tests/test_pop_topoplot.py with EEGPREP_SKIP_MATLAB=1 gave 37 passed, 2 skipped (MATLAB parity), 13 deselected. The EEGLAB reference suite was not run. The measurements quoted below come from short probe scripts run against develop and against this commit, plus MATLAB R2025b for marker sizes.
Blocking
1. pop_spectopo and pop_envtopo now raise on their own valid options. Both wrappers pass every option they do not consume straight to topoplot:
eegprep/src/eegprep/functions/popfunc/pop_spectopo.py
Lines 323 to 327 in b78f1c0
eegprep/src/eegprep/functions/popfunc/pop_envtopo.py
Lines 86 to 88 in b78f1c0
On develop the leftovers were dropped silently. With the allow-list they reach the new raise (topoplot.py line 356). Calls that run on develop and fail on this commit:
pop_spectopo(EEG, 1, [0, 1000], "EEG", "freq", [10], "freqrange", [2, 25], "limits", [...])givesValueError: topoplot: Unknown input parameter 'limits'. Same forreref,mapnorm,rmdc,plotmean.pop_envtopo(EEG, [0, 500], options="'electrodes','off','limits',[0 500 -5 5]")gives the same error. Same forvoffsets,dispmaps,pvaf,colorfile,fillcomp,actscale,xlabeltyped in the "Optional topoplot() and envtopo() arguments" field.
Why it matters: these are documented spectopo.m and envtopo.m options that appear in EEGLAB history lines, and the error names the wrong function. Issue #683 asks that the allow-list "keep working for the internal callers" before unknown keys start raising.
Suggested fix: make each wrapper split its options before the call. Keys that belong to spectopo or envtopo should be consumed there, or rejected there with an error that names that function and says the option is not implemented in EEGPrep. Only keys meant for topoplot should be forwarded. timtopo, eegmovie (topoplotopt) and _std_measureplot (topoplotopt) forward user dicts the same way and deserve the same check.
Important
2. The default electrode dot is now about 1.8 times the EEGLAB diameter. _resolve_emarker_size_sq squares the EEGLAB MarkerSize and _draw_markers hands it to scatter with marker '.':
eegprep/src/eegprep/functions/sigprocfunc/topoplot.py
Lines 399 to 424 in b78f1c0
MATLAB draws the '.' marker at one third of MarkerSize and ignores the line width. Matplotlib draws '.' at one half of the nominal size and adds the edge width. Rendered widths at 600 dpi:
| Channels | EEGLAB MarkerSize |
MATLAB R2025b | develop |
this PR |
|---|---|---|---|---|
| under 32 | 10 | 3.24 pt | 3.48 pt | 5.88 pt |
| 32 to 47 | 8 | about 2.6 pt | 3.48 pt | 4.92 pt |
| 64 to 79 | 5 | about 1.6 pt | 3.48 pt | 3.48 pt |
(The 8 and 5 MATLAB values are scaled from the measured 10 and 20 point cases, 3.24 and 6.60 pt.) This is the default for every map with electrodes on, so pop_topoplot, pop_prop, timtopo and envtopo all change look. The filled 'o' used by emarker2 matches (10.44 pt in MATLAB, 10.92 pt here with the 1 pt edge).
Suggested fix: convert the size per marker. For '.', something like s = (2 * size / 3) ** 2 with linewidths=0 renders at 3.24 pt and 6.60 pt for sizes 10 and 20, the same as MATLAB. The issue asks for visual parity against topoplot.m, and the evidence in the PR compares coordinates only, so a side-by-side render of default electrodes, emarker and emarker2 attached to the PR would close this.
3. Valid EEGLAB options are reported as "Unknown". conv, shading, whitebk, headrad, efontsize, nosedir, interplimits, headcolor, plotdisk, plotgrid, dipole and the other names in the topoplot.m switch (lines 389 to 616) now raise topoplot: Unknown input parameter '...':
eegprep/src/eegprep/functions/sigprocfunc/topoplot.py
Lines 353 to 357 in b78f1c0
The message is untrue for these names and gives the user no next step. The code this PR removes did distinguish the case ("is not supported yet in eegprep (EEGLAB topoplot.m supports it)"). headrad and plotgrid were also listed in the docstring until this PR.
Suggested fix: keep a second frozenset of EEGLAB topoplot.m names that EEGPrep does not implement, and raise a distinct message for them, for example "topoplot: 'conv' is an EEGLAB topoplot option that EEGPrep does not implement yet. Remove it from the call." Keep "Unknown input parameter" for names EEGLAB also rejects. List the unimplemented names in the migration table row, which today says only that unknown parameters raise.
Nits
4. Three marker symbols map to a different shape. In _MATLAB_MARKERS (topoplot.py lines 71 to 89), '*' goes to Matplotlib '*', 'p'/'pentagram' to 'p', and 'h'/'hexagram' to 'h'. In MATLAB '*' is an asterisk made of lines, 'p' is a five-pointed star and 'h' is a six-pointed star. In Matplotlib '*' is a filled five-pointed star, 'p' a pentagon and 'h' a hexagon. Suggested mapping: '*' to (8, 2, 0), 'p' and 'pentagram' to '*', 'h' and 'hexagram' to (6, 1, 0). The marker in {".", "*"} test in _draw_markers and the "solid for . and *" sentence in the migration row then need the matching update.
Test gaps
- No test calls a forwarding wrapper with one of its own options. Add to
tests/test_spectopo_parity.pyor the closestpop_spectopotest a call with"freq", [10], "limits", [...], and totests/test_envtopo.pyapop_envtopocall whoseoptionstext holds'limits'or'voffsets'. Both would have failed on this commit. - The marker tests assert
get_sizes()equalssize ** 2, which is the same rule the code applies, so they cannot see finding 2. A check of the'.'diameter against the MATLAB one-third rule would.
EEGLAB parity notes
emarker2indexes positions in the list left afterplotchansfiltering, astopoplot.mlines 913 and 914 do. Withplotchans=[2, 4, 5],emarker2=[[2], ...]marks channel 4, whileelectrodes='numpoint'labels that same channel "4". This copies an EEGLAB quirk. The project rule is to correct such quirks and document them, so a maintainer should decide which way this goes. The migration row currently describes it as parity.emarkersize1chanis applied on every blank map. In EEGLAB it is read only withplotdisk='on'(topoplot.mline 1636), and the default path draws fixed-radius disks (lines 1625 to 1633). The migration row documents this and a test locks it, so no change is requested.- AGENTS.md asks, for a pull request that changes core functions, to "run the suite locally once on the final branch". The evidence posted covers
tests/test_topoplot.pyandtests/test_pop_topoplot.pyonly (52 passed). The full reference run, or theeeglab-reference.ymlworkflow on this branch, is still needed before merge.
…h unimplemented topoplot options
|
Addressed all review items in commit
|
arnodelorme
left a comment
There was a problem hiding this comment.
This review was written by an AI assistant at Arno's request, not by Arno.
Second review, at 4fcaa7ac. The blocking problem of the first review is fixed: pop_spectopo and pop_envtopo run again with the options they implement. emarker and emarker2 draw the right markers on the right channels. What is left is small: one conflict in the changelog, and three cosmetic differences listed below. This is a comment, not an approval.
topoplot, EEGLAB beside EEGPrep with this pull request
Same data (component 3 of eeglab_data_epochs_ica.set, largest difference between the two programs 9.4e-15), same channel locations, same figure size (5 by 5 inches at 100 dpi), both rendered on Linux (jamming): MATLAB R2025b with EEGLAB 7ba2370cf on the left, EEGPrep at 4fcaa7ac on the right.
The colours of the map itself differ (EEGLAB scales around zero with jet, EEGPrep scales from minimum to maximum with turbo). That is the same on develop and does not come from this pull request.
1. The earlier blocking problem (pop_spectopo, pop_envtopo): fixed
I ran 55 calls of pop_spectopo (channels and components), pop_envtopo and pop_timtopo on eeglab_data_epochs_ica.set, each with one option taken from the option lists of spectopo.m, envtopo.m and topoplot.m, once on develop (2fe56069) and once on this commit.
| runs | stops | |
|---|---|---|
develop |
54 | 1 (emarker, not implemented there) |
| this pull request | 45 | 10 |
Every option named in the first review now runs: limits, reref, mapnorm, rmdc, plotmean for pop_spectopo, and limits, voffsets, dispmaps, pvaf, actscale for pop_envtopo, including the text typed in the "Optional topoplot() and envtopo() arguments" field. freqfac, winsize, overlap, nfft, wintype, plotchans, title, memory, verbose, percent, sortvar, fillcolor, sumenv, envmode, vert, subcomps, compnums, limcontribweight, vertweight and the topoplot options electrodes, emarker, maplimits run as well.
The 10 calls that stop, all with a message that names the right function:
pop_spectopo:boundaries,mapchans,mapframes("is an EEGLAB spectopo option that EEGPrep does not implement yet").pop_envtopo:fillcomp,colors,axisoff,plotproj(same message for envtopo).shading(atopoplot.moption not implemented): stopped bytopoplotwith the "not implemented yet" message.- An invented key (
bogus) in each wrapper: "topoplot: Unknown input parameter 'bogus'", which is also what EEGLAB does with a key no function knows.
One thing for Arno to decide, not a defect: the seven EEGLAB options in the first two lines were ignored silently on develop and now stop the call. A history line from EEGLAB that carries one of them will stop in EEGPrep with a clear message instead of drawing a figure without it. I find the clear stop better than a silent difference, but it is a change of behaviour.
2. Marker sizes and colours measured on the captures
Width of one marker in points, measured on the PNG files above (100 dpi, so 1 pixel is 0.72 point):
| marker | EEGLAB | EEGPrep |
|---|---|---|
| default dot | 2.2 | 2.9 |
'o', size 8, line 2 |
9.4 | 9.4 |
'd', size 10, line 1 (width by height) |
10.8 by 15.1 | 9.4 by 15.1 |
's', size 6, line 1 |
5.0 | 6.5 |
'p', default size |
10.8 | 9.4 |
'*', size 12, line 2 |
10.8 | not measured reliably, about 2 pixels larger by eye |
The default dot, which was 1.8 times too large in the first review, is now within one pixel. The circle matches exactly. The square is 2 pixels larger and the star 2 pixels smaller than in MATLAB. These are nits.
Colour letters (nit, new). A colour given as a letter goes to mcolors.to_rgba, and Matplotlib does not give m, y, c and g the MATLAB values. Measured on the captures: 'm' is (1, 0, 1) in EEGLAB and (0.75, 0, 0.75) in EEGPrep, and the yellow star is visibly darker. By Matplotlib's table 'y' is (0.75, 0.75, 0), 'c' is (0, 0.75, 0.75) and 'g' is (0, 0.5, 0), against (1, 1, 0), (0, 1, 1) and (0, 1, 0) in MATLAB. I rendered m and y only. A small table of the eight MATLAB letters before the call to to_rgba would fix it.
3. Conflict with develop, tests, and what this review did not cover
- Conflict. GitHub reports the branch as conflicting. A trial merge with
develop(2fe56069) conflicts in one file only,docs/source/changelog.rst. Mergingdevelopinto the branch and keeping both changelog entries is enough. - Tests.
tests/test_topoplot.py,tests/test_pop_topoplot.py,tests/test_spectopo_parity.py,tests/test_envtopo.pyandtests/test_phase4_plot_wrappers.pyon this commit withEEGPREP_SKIP_MATLAB=1: 77 passed, 7 skipped (the MATLAB parity tests), 17 deselected. No CI check is reported on the branch, so the full suite and the MATLAB parity tests have not run on this commit. - Not covered. I read the changes of the second commit in the three
pop_wrappers line by line. I did not read the new code insidespectopo.py,envtopo.py,timtopo.pyandtopoplot.pyline by line: for those I rely on the 55 calls, the tests and the captures. I did not check that the newly accepted options (voffsets,actscale,mapnorm,plotmeanand the others) give the same figure as EEGLAB, only that they run. The captures are from Linux only, the Mac was not captured. - Outside this pull request. The default colour scale and colormap of
topoplotdiffer from EEGLAB (absmaxwithjetthere, minimum to maximum withturbohere)._maplimits_kwargsis the same ondevelop.
Recommendation: ready to merge once the changelog conflict is resolved and the full test suite has run on the merged branch. The colour letters and the two marker sizes can follow in a small pull request.
— orchestrator
…-683 # Conflicts: # docs/source/changelog.rst


topoplot now draws emarker, emarker2, emarkersize, electcolor, and emarkersize1chan with MATLAB one-third dot scaling and matching star and asterisk shapes, distinguishes unimplemented EEGLAB topoplot options from truly unknown parameters, and wrapper callers split their own options before forwarding to topoplot. Verified locally against MATLAB R2024a on 249 plotting reference and parity tests (0 failed, 0 skipped, EEGLAB d0963be9, eeglab_tests fec88f9f).
Fixes #683