Skip to content

Implement pop_eegplot menu session workflows - #105

Merged
suraj-ranganath merged 2 commits into
feature/eegbrowser-completionfrom
phase/eegbrowser-4-pop-eegplot
Jun 1, 2026
Merged

suraj-ranganath merged 2 commits into
feature/eegbrowser-completionfrom
phase/eegbrowser-4-pop-eegplot

Conversation

@suraj-ranganath

Copy link
Copy Markdown
Member

Wire pop_eegplot into the EEGBrowser menu workflows with EEGLAB-style variant arguments, command echo, history, and session synchronization. Add ICA gating and regression coverage for GUI, console, and menu state behavior.\n\nFixes #98

@claude

claude Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 7m 28s —— View job


PR Review

  • Gather PR context and diff
  • Read changed files
  • Review pop_eegplot wrapper (variants, history, ICA gating)
  • Review menu_actions/eeglab_menu wiring
  • Review console session updates
  • Review tests for coverage
  • Post review findings (inline + summary)

Summary

Overall the wiring is in good shape: pop_eegplot now returns (EEG, com), the dispatcher echoes/records history immediately on dispatch, the Accept callback re-stores the EEG with store_new keyed on whether data shrank, ICA gating is enforced both in the menu (ON_DATA_ICA / ON_EPOCH_ICA) and defensively inside pop_eegplot via _require_ica, and the no-mutation-on-error path is exercised by tests. Test coverage is solid (variant matrix, GUI/console dedup, ICA gating, Qt enable states).

Posted four inline comments. The first is the only one that looks like a behavior bug; the rest are robustness/maintainability notes.

High priority

  1. _pop_eegplot_variant_options defaults look off vs. EEGLAB — menu_actions.py:1480-1487.
    • Plot > "Channel data (scroll)" and "Component activations (scroll)" currently send reject=1, so the user gets a "Reject" button on what is supposed to be a view-only scroll. Clicking it truncates the dataset via eeg_eegrej.
    • "Reject by inspection" (data + ICA) sends superpose=0, so prior marks from other reject methods (rejthresh, rejjp, …) aren't shown in the browser. EEGLAB uses superpose=1 here.
    • The new test hard-codes the same tuples, so it locks in the deviation rather than verifying parity. Please cross-check src/eegprep/eeglab/functions/popfunc/pop_eegplot.m and the relevant callbacks in eeglab.m (the vendored EEGLAB checkout isn't present in this CI workspace, so I couldn't verify directly).

Medium priority

  1. History dedup via LASTCOM string compare is fragile — menu_actions.py:1022-1041. The dispatcher adds the command on dispatch (line 1040); the Accept callback later compares command == self.session.LASTCOM to decide whether to skip a second add. Any intervening GUI/console activity that changes LASTCOM will let a duplicate slip through. Prefer passing an explicit "already added" flag into the callback.
  2. Closure captures selection/reject at dispatch time — same block. If CURRENTSET changes before the user clicks Reject, _store_current_from_gui(eeg_out, new=False, ...) writes into the new current set. Unlikely in a strictly modal browser, but worth capturing the target index explicitly.

Low priority / nits

  1. _pop_eegplot_reject_argument hard-codes args[3] — console.py:1212-1217. Brittle to signature changes; consider inspect.signature(...).bind_partial(...), and add a positional-call test so future drift fails loudly.
  2. eegplot_accept_creates_dataset heuristic is narrow — pop_eegplot.py:102-110. Only reject=1 ever returns True, and "Reject with zero marks" silently overwrites in place. Probably fine; a short docstring example would make the intent obvious.
    · Branch: phase/eegbrowser-4-pop-eegplot

@suraj-ranganath

Copy link
Copy Markdown
Member Author

🤖 Phase 4 implementation evidence\n\nImplemented and verified pop_eegplot menu/session workflows for issue #98. Focused tests: 207 passed, 1 skipped, 19 subtests passed. Broad non-slow suite with MATLAB disabled: 1490 passed, 230 skipped, 11 deselected. Static checks passed: ruff check, ruff format --check, ty check, and ./pre-commit.py.\n\nVisual/menu evidence: macOS open-menu screenshot capture did not reliably open the EEGLAB native menu, so I used the visual parity skill's supported menu-inventory path for menu work. Targeted EEGLAB and EEGPrep enabled/separator states match for the Phase 4 pop_eegplot entries across startup, continuous, epoched, and multiple-dataset states.\n\nPhase 4 pop_eegplot menu evidence

Comment on lines +1480 to +1487
def _pop_eegplot_variant_options(variant: str) -> tuple[int, int, int]:
if variant == "components":
return 0, 1, 1
if variant == "reject_ica":
return 0, 0, 1
if variant == "channels":
return 1, 1, 1
return 1, 0, 1

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.

EEGLAB parity: please double-check the variant matrix against eeglab.m

These defaults look intentional (the test hard-codes the same tuples), but they don't line up with the EEGLAB menu callbacks I recall:

Menu EEGPrep (this PR) EEGLAB eeglab.m (typical)
Plot > Channel data (scroll) (1, 1, 1) (1, 1, 0) – view-only, Reject disabled, button reads "Update Marks"
Plot > Component activations (scroll) (0, 1, 1) (0, 1, 0) – view-only
Tools > Inspect/reject data by eye (1, 0, 1) (1, 1, 1) – superpose=1 so prior rejections (epoched) or thresh/jp marks are visible
Tools > Reject data epochs > Reject by inspection (1, 0, 1) (1, 1, 1)
Tools > Reject data using ICA > Reject by inspection (0, 0, 1) (0, 1, 1)

Two concrete consequences if I'm right:

  1. The Plot menu items currently produce a "Reject" button that, if clicked, will create a new dataset with samples deleted via eeg_eegrej — but EEGLAB's Plot scroll is supposed to be display-only. A user clicking through "just to look" can accidentally truncate the dataset.
  2. With superpose=0, _initial_epoch_winrej only paints EEG.reject.rejmanual; it skips rejthresh/rejjp/.... The "Reject by inspection" dialog therefore won't show the marks from other reject methods that EEGLAB does.

Worth re-checking against src/eegprep/eeglab/eeglab.m (not in this checkout). If the new behavior is deliberate, please add a brief comment here pointing to where the deviation is decided.

Comment on lines 1022 to +1041
def accept_eegplot(eeg_out: Any, command: str) -> None:
with self.session.gui_action("pop_eegplot"):
if command:
self._store_current_from_gui(eeg_out, command=command)
store_new = eegplot_accept_creates_dataset(selection, eeg_out, reject)
store_command = "" if command == self.session.LASTCOM else command
self._store_current_from_gui(eeg_out, new=store_new, command=store_command)
self._refresh()

pop_eegplot(
out = pop_eegplot(
selection,
icacomp=0 if variant in {"components", "reject_ica"} else 1,
icacomp=icacomp,
superpose=superpose,
reject=reject,
command_callback=accept_eegplot,
return_com=True,
)
command = out[1] if isinstance(out, tuple) and len(out) > 1 else ""
if command:
self._add_history_from_gui(command)
self._refresh()

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.

Dedup is fragile and the closure captures stale state

Two related concerns about the accept-callback wiring:

  1. LASTCOM dedup can break. History is added immediately on line 1040 (_add_history_from_gui(command)). The browser's Accept callback may run seconds or minutes later. The dedup at line 1026 ("" if command == self.session.LASTCOM else command) only works if nothing else has touched LASTCOM in between — e.g. the user opens another menu, the console echoes a command, or an autosave runs. When dedup fails the same pop_eegplot(EEG, ...) line ends up in ALLCOM twice. The dispatcher already knows it added the history; it would be more robust to pass that knowledge into the callback (e.g. via a flag closed over here) instead of inferring it from a stringly-typed comparison.

  2. selection and reject are snapshot at dispatch time. If the user changes CURRENTSET (selects a different dataset) between opening the browser and clicking Reject, eegplot_accept_creates_dataset(selection, eeg_out, reject) uses the original selection, but _store_current_from_gui(eeg_out, new=False, ...) then writes eeg_out into the currently selected slot. This is unlikely for a modal browser, but if multi-window inspection is supported it would silently overwrite the wrong dataset. Capturing the target CURRENTSET[0] here and routing through it explicitly would be safer.

Comment on lines +1212 to +1217
def _pop_eegplot_reject_argument(args: tuple[Any, ...], kwargs: Mapping[str, Any]) -> int:
if "reject" in kwargs:
return int(kwargs["reject"])
if len(args) >= 4:
return int(args[3])
return 1

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.

Hard-coded positional index 3 is brittle

This relies on reject being the 4th positional parameter of pop_eegplot. The signature today is pop_eegplot(EEG, icacomp, superpose, reject, topcommand, *args, ...), so this works — but the next refactor that reorders or inserts a parameter (or moves reject keyword-only) will silently start reading the wrong value, since the only test that exercises this path passes via kwargs (tests/test_console_workspace.py:_fake_pop_eegplot).

A safer pattern is to bind via inspect.signature(pop_eegplot).bind_partial(*args, **kwargs).arguments.get("reject", 1), or at minimum to name the index as a constant tied to the signature. As-is, please add a test that calls the wrapper positionally with reject explicitly set, so any future signature drift fails loudly.

Comment on lines +102 to +110
def eegplot_accept_creates_dataset(input_eeg: dict[str, Any], output_eeg: dict[str, Any], reject: int = 1) -> bool:
"""Return whether an accepted ``pop_eegplot`` result should be stored as a new dataset."""
if not int(bool(reject)):
return False
input_trials = int(input_eeg.get("trials", 1) or 1)
output_trials = int(output_eeg.get("trials", 1) or 1)
if input_trials > 1:
return output_trials < input_trials
return _dataset_pnts(output_eeg) < _dataset_pnts(input_eeg)

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.

Heuristic only catches sample/trial deletion

eegplot_accept_creates_dataset decides "new dataset vs. update in place" purely by comparing pnts / trials. This works for the rejection path in apply_eegplot_rejections, but it has two soft spots:

  • If the user marks at least one window then clicks Reject, but the marks are zero-width or otherwise produce no eeg_eegrej cut, output_pnts == input_pnts and store_new is False, so the dataset is overwritten in place. That's probably fine, but it means a user clicking "Reject" after marking nothing produces a silent re-store with no visible change — worth verifying that downstream listeners (notify_changed) don't trigger spurious refreshes.
  • reject=1 is the only branch that ever returns True. If you later add a path where reject=0 mutates the dataset (e.g. some new EEGLAB-style "save marks as new dataset" flow), this helper will quietly classify it as an in-place edit.

Optional: a docstring example mapping (input.shape, output.shape, reject) → bool would make the intended semantics easy to spot-check from the callers.

@suraj-ranganath
suraj-ranganath merged commit 6611674 into feature/eegbrowser-completion Jun 1, 2026
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