Skip to content

pop_chanplot: Match channel rows and measure selection to the study - #1036

Merged
arnodelorme merged 7 commits into
developfrom
agent/issue-969-chanplot
Dec 1, 2026
Merged

arnodelorme merged 7 commits into
developfrom
agent/issue-969-chanplot

Conversation

@arnodelorme

@arnodelorme arnodelorme commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 Part of #969 and #875. Match channel rows and measure selection to the study. Stacked on #1027.

Live MATLAB, EEGPrep before and after

Function output distances

Vertical control offsets

Native measurements and validation

The N400 dialog has a maximum vertical difference of 0.762 px. On the recorded EEGLAB sample epochs, two native ERP plot submissions agree with MATLAB std_erp over each entire channel waveform to 3.54e-7 relative error. Twenty-six tests passed on jamming. The fallback raw ERP remains available on OK.

Full inventory report for the project team. pop_chanplot (p. 53). PDF · Editable Word.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

@claude

claude Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @arnodelorme's task in 1m 12s —— View job


Code review

  • Overall assessment: Needs one fix. The Sel. all wiring, enabling buttons only when a measure is cached, and mapping rows to changrp indices all work when the clicked measure is cached for channels. The raw-ERP fallback on OK still picks the wrong channel when STUDY.changrp holds only some of the channels.
  • Highest-risk area: the row-to-channel mapping in _run_gui (pop_chanplot.py).
  • Merge recommendation: Needs changes.

Blocking

None.

Important

  • pop_chanplot._run_gui (L171): rows are changrp positions, but the raw-ERP fallback reads them as dataset channel numbers.

    • What changed: whenever STUDY.changrp exists, the rows now list its group names (channel_options = [f"All {label}" for label in group_names or labels]), and channels = selected passes the row numbers on unchanged.
    • Where it breaks: _plot_channel_measure only treats those numbers as group indices when _has_cached_channels(study, measure) is true. Otherwise it calls _plot_loaded_erp, which reads them as 1-based channels of ALLEEG[0].
    • Realistic case: std_precomp can build changrp for only some channels, because _precompute_channels loops over _channel_indices(chanorcomp, labels). Run std_precomp(..., channels=['Cz'], spec='on'), then press OK with the default erp action. Row 1 ("All Cz") plots ALLEEG[0] channel 1, which is not Cz, and the history records channels=[1]. The PR body says this OK fallback stays available, so this is a common path.
    • Fix: when changrp exists but the measure is not cached for channels, convert each row to its group's channel with _group_channel_index(groups[row - 1], row). Or pass group names, since _cached_channel_groups_by_name already accepts labels.

    Fix this →

Nits

  • In qt.py, the study_channel_subjects callback hard-codes the widget tags "chan_list" and "chan_onechan" inside the shared renderer. By contrast, select_all_rows takes its tags from params. Pass source/target through the CallbackSpec params so this generic renderer does not depend on one dialog's tag names.

Test gaps

  • The new test test_channel_dialog_second_row_plots_second_channel uses _study_pair(), which has no changrp, so it only covers the case where row number = dataset channel number. Add a test for the case above: a changrp that covers only some channels, then OK, checking that last_chanplot["channels"] matches the group's inds.

EEGLAB parity notes

None. The vendored pop_chanplot.m is not in this checkout, so I compared against the declared eeglab_source and the PR screenshots only.


  • Pre-review gate (open, not draft, no prior review, HEAD bfc5566 matches)
  • Read pop_chanplot.py, qt.py callbacks, std_precomp changrp construction
  • Review passes (AGENTS.md, conventions, correctness, parity)
  • Posted review and one inline comment

Note: I couldn't post the separate gh pr comment summary because writing to /tmp needs approval in this run. This comment holds the full review.

measure = _measure_name(action)
selected = numeric_vector(result.get("chan_list", []), dtype=int).tolist()
channels = [] if not selected or 1 in selected else [index - 1 for index in selected if index > 1]
channels = selected

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important: rows are changrp positions, but the raw-ERP fallback reads them as dataset channel numbers.

Whenever STUDY.changrp exists, the rows now list changrp group names (L81-82). _plot_channel_measure only treats channels as group indices when the clicked measure is cached for channels. Otherwise _plot_loaded_erp reads the same numbers as 1-based channels of ALLEEG[0].

std_precomp can build changrp for only some channels (_precompute_channels loops over _channel_indices(chanorcomp, labels)). Example: std_precomp(..., channels=['Cz'], spec='on'), then OK with the default erp action. Row 1 ("All Cz") plots ALLEEG[0] channel 1 and records channels=[1] in history.

Suggested fix: when changrp exists and not _has_cached_channels(STUDY, measure), map each selected row to its group's channel with _group_channel_index(groups[row - 1], row). Or pass group names instead. Then add a test with a changrp that covers only some channels.

@arnodelorme arnodelorme left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This review was written by an AI assistant during a nightly check, not by Arno.

Code review

  • Overall assessment: Needs changes
  • Highest-risk area: STUDY.changrp channel mapping on uncached EEGLAB studies and untested Qt dialog callbacks
  • Merge recommendation: Needs changes

Blocking

None.

Important

  • Severity: Important (src/eegprep/functions/studyfunc/pop_chanplot.py, _run_gui and _group_channel_index,
    selected = numeric_vector(result.get("chan_list", []), dtype=int).tolist()
    groups = [group for group in STUDY.get("changrp") or [] if isinstance(group, dict) and group.get("name")]
    if groups and not _has_cached_channels(STUDY, measure):
    channels = [_group_channel_index(groups[row - 1], row) for row in selected]
    else:
    channels = selected
    mode = (
    and
    def _group_channel_index(group: dict[str, Any], fallback: int) -> int:
    inds = group.get("inds") or [fallback]
    return int(inds[0])
    ): When _has_cached_channels(STUDY, measure) is False, _run_gui maps selected dialog rows through _group_channel_index(groups[row - 1], row), which reads group.get("inds") or [fallback]. Only EEGPrep's std_precomp writes "inds". MATLAB EEGLAB's std_changroup.m and pop_chanplot.m store name and channels without "inds", and .study files loaded from MATLAB have no in-memory erpdata. If an EEGLAB .study file has a subset STUDY.changrp, had groups deleted or reordered in MATLAB, or merged non-identical dataset montages via eeg_mergelocs, group.get("inds") is missing and _group_channel_index falls back to row instead of matching the group's channel label against datasets[0], plotting the wrong channel or raising ValueError when len(groups) > datasets[0]["nbchan"].
    Suggested fix: When group.get("inds") is empty in the raw ERP fallback path, resolve group.get("name") (or the first label in group.get("channels")) case-insensitively against channel_labels(datasets[0]) (1-based) before falling back to row.

Nits

None.

Test gaps

  • Severity: Important (tests/test_study_measures.py,
    @pytest.mark.gui
    def test_channel_dialog_subset_changrp_raw_erp_fallback_uses_group_channel(qt_dialogs):
    study, datasets = _study_pair()
    study, datasets = std_precomp(study, datasets, ["Ch3"], spec="on")
    class Inspect:
    def run(self, spec, initial_values=None):
    widgets = qt_dialogs.build(spec)
    assert [widgets["chan_list"].item(i).text() for i in range(widgets["chan_list"].count())] == ["All Ch3"]
    return {"chan_list": [1]}
    study, command, figure = pop_chanplot(study, datasets, gui=True, renderer=Inspect(), plot="off", return_com=True)
    assert study["etc"]["last_chanplot"]["channels"] == [3]
    assert "channels=[3]" in command
    plt.close(figure)
    and src/eegprep/functions/guifunc/qt.py,
    elif callback.name == "select_all_rows":
    widgets[params["source"]].clicked.connect(widgets[params["target"]].selectAll)
    elif callback.name == "study_channel_subjects":
    channel_list = widgets[params["source"]]
    subject_list = widgets[params["target"]]
    def refresh_subjects():
    selected = channel_list.selectedIndexes()
    subjects = params["subjects"]
    label = params["labels"][selected[0].row()] if len(selected) == 1 else ""
    subject_list.clear()
    subject_list.addItems([subjects[0], *[f"{name} {label}".strip() for name in subjects[1:]]])
    subject_list.setCurrentRow(0)
    channel_list.itemSelectionChanged.connect(refresh_subjects)
    ): test_channel_dialog_subset_changrp_raw_erp_fallback_uses_group_channel builds the Qt widgets, asserts only the chan_list item text, and returns a hardcoded {"chan_list": [1]} dictionary. None of the tests exercise the new select_all_rows callback, the study_channel_subjects selection callback updating chan_onechan, the available[measure] button enablement states, or clicking an enabled measure button (such as plot_chan_spectra) and reading widget values back via _read_widget to verify measure_action survives layout="supergui" (which regressed in 0302d8b5 before bfc5566a changed measure_action from "spacer" to a hidden "text" control).
    Suggested fix: Extend the pytest.mark.gui test in tests/test_study_measures.py on a multi-channel study with spec="on" to assert plot_chan_erp is disabled and plot_chan_spectra is enabled, click select_all_channels to verify all rows select and chan_onechan updates its labels, and click plot_chan_spectra and read widget values through _read_widget so pop_chanplot executes measure="spec".

EEGLAB parity notes

  • Verified dialog layout, geomvert, row labels, "All <chan>" entries, subject list suffix updates, and measure button enablement against functions/studyfunc/pop_chanplot.m (lines 178 to 255 and 365 to 410). Aside from the uncached STUDY.changrp label resolution noted under Important and the documented known_differences OK-submission workflow, behavior matches EEGLAB.

Verification run

Ran in a detached worktree at 48efc636adf2bd46f1045f94a5990bc8a7d9c2f5:

  • PYTHONPATH=<worktree>/src .venv/bin/python -m pytest tests/test_study_measures.py tests/test_study_end_to_end.py tests/test_gui_geometry.py: 154 passed, 67 skipped (Linux-only reference positions).
  • PYTHONPATH=<worktree>/src .venv/bin/python -m pytest tests/test_gui_pop_study.py tests/test_guifunc_primitives.py: 35 passed, 1 deselected.
  • .venv/bin/ruff check and .venv/bin/ruff format --check on changed Python files: passed.

@arnodelorme arnodelorme left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This review was written by an AI assistant during a nightly check, not by Arno.

Code review

  • Overall assessment: Looks good.
  • Highest-risk area: STUDY.changrp channel row mapping and Qt dialog callbacks in pop_chanplot.
  • Merge recommendation: Safe to merge.

Blocking

None.

Important

None.

Nits

None.

Test gaps

None.

EEGLAB parity notes

  • Verified channel row labels (All <chan>), supergui geometry and geomvert, subject list suffix updates on single and multiple row selection, Sel. all selection behavior, measure button enablement against cached channel and component measures, and uncached STUDY.changrp label resolution against functions/studyfunc/pop_chanplot.m and functions/studyfunc/std_changroup.m.

Checked for correctness bugs, EEGLAB parity, data structure compatibility, tests for changed behavior (pop_chanplot.py, qt.py, study_workflows.rst, test_study_measures.py), and CLAUDE.md/AGENTS.md compliance at commit c8f48a3043fbd8d5256a7256afc273efdbc93b1a.

Tests run in a detached worktree at c8f48a3043fbd8d5256a7256afc273efdbc93b1a:

  • PYTHONPATH=<worktree>/src .venv/bin/python -m pytest <worktree>/tests/test_study_measures.py <worktree>/tests/test_study_end_to_end.py <worktree>/tests/test_gui_pop_study.py <worktree>/tests/test_guifunc_primitives.py <worktree>/tests/test_gui_geometry.py: 198 passed, 70 skipped (Linux reference geometry), 1 deselected in 12.00s.
  • .venv/bin/ruff check and .venv/bin/ruff format --check on qt.py, pop_chanplot.py, and test_study_measures.py: passed.

@arnodelorme
arnodelorme changed the base branch from agent/issue-968-studydesign to develop December 1, 2026 08:19
@arnodelorme
arnodelorme merged commit f65d501 into develop Dec 1, 2026
3 checks passed
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.

1 participant