Skip to content

[STUDY] Implement long-tail selection helpers - #156

Merged
suraj-ranganath merged 1 commit into
feature/eeglab-core-parity-completionfrom
followup/149-study-long-tail-helpers
Jun 7, 2026
Merged

suraj-ranganath merged 1 commit into
feature/eeglab-core-parity-completionfrom
followup/149-study-long-tail-helpers

Conversation

@suraj-ranganath

Copy link
Copy Markdown
Member

Add standalone STUDY selection, trialinfo, consistency, subsetting, dataset removal, subject-filter, and ICA-grouping helpers with multi-dataset coverage. Classify the remaining non-PAC Phase 6 STUDY plotting/precompute rows as consolidated, MATLAB-runtime, or external-dependency entries while leaving PAC rows to #148. Part of epic #131 and stacked under PR #152.

Closes #149

@claude

claude Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 4m 8s —— View job


Code review

  • Overall assessment: Safe to merge. Clean, consistent, well-tested port of the STUDY long-tail helpers.
  • Highest-risk area: EEGLAB parity of std_findsameica subject-scoping (a deliberate deviation worth confirming).
  • Merge recommendation: Safe to merge.

I reviewed all 13 new helpers, _study_utils.py/__init__.py wiring, the test file, and the docs/parity-matrix changes. Imports all resolve (sets_array, trialinfo_from_eeg, build_python_call, as_alleeg_list, sync_datasetinfo, ensure_study, _empty_value, RESERVED_VARIABLE_FIELDS, STUDY_DATA_FIELDS all exist). 1-based indexing is handled consistently and exercised by tests. I could not run the suite (test execution needs approval in this environment) and the vendored src/eegprep/eeglab/ reference is absent on this checkout, so parity notes below are from EEGLAB knowledge rather than a side-by-side diff.

Blocking

None.

Important

None.

Nits

  • std_getindvar._append_trial_factors pairing heuristic (std_getindvar.py:87): paired.append("on" if all(isinstance(value, str) for value in values) else "off"). Pairing reflects whether the same subjects appear across factor levels (within-subject design), not whether the factor values happen to be strings. For dataset factors you correctly compute this via _same_subjects; the trial-factor branch substitutes a string-type check that doesn't track pairing. Low impact (trial-factor subjects is returned empty anyway), but the flag can be misleading.
  • Shadowed loop variable in std_selectdataset (std_selectdataset.py:46-59): the outer for index, info in enumerate(infos) and the inner generator (index for index, row in enumerate(rows, start=1) ...) both bind index. It's correct (the generator has its own scope, so selected_trials[index] still uses the dataset index), but reusing the name makes it read like a bug. Renaming the inner one to row_index would help.
  • Lazy-export ordering (__init__.py): std_findsameica is inserted just before std_findgroupvars, which breaks the otherwise-alphabetical ordering of _LAZY_EXPORTS. Purely cosmetic.

Test gaps

  • std_gettrialsind continuous/empty-string and type-mismatch paths are partly untested: the empty-string query (expected == "" → "field is numeric/non-empty") and the ValueError branches in _field_hits (string vs numeric mismatch) have no coverage. A small case asserting std_gettrialsind(rows, "rt", "") returns the numeric trials and that querying a string field with a numeric expectation raises would lock in that behavior. Everything else in the PR is well covered by test_study_long_tail_helpers.py.

EEGLAB parity notes

  • std_findsameica adds subject-scoping not present in EEGLAB (std_findsameica.py:21-30): when more than one non-empty subject is present, datasets are grouped within each subject before comparing icaweights @ icasphere. EEGLAB's std_findsameica compares all datasets directly regardless of subject label (its purpose is to discover which sets share a decomposition). With the new scoping, two datasets with identical ICA but different subject strings will land in separate groups — which is exactly what test_same_ica_groups_are_subject_scoped asserts ([[1,2],[3]] rather than [[1,2,3]]). If this is an intentional improvement, it's worth a one-line comment in the function and a note in study_workflows.rst; if strict parity is the goal, the pre-grouping should be dropped. Please confirm against the EEGLAB source which isn't vendored on this branch.
  • std_rmdat subjectind silently overrides keepvarvalues (std_rmdat.py:57-59): passing both replaces the caller's keepvarvalues with ("subject", ...). This matches EEGLAB's intent (the two aren't meant to be combined), so it's fine — just flagging that there's no warning if a caller passes both.
    • followup/149-study-long-tail-helpers

@suraj-ranganath

Copy link
Copy Markdown
Member Author

🤖 Specification for follow-up #149

Problem

Phase 6 left STUDY long-tail rows outside the implemented design, cache-reader, plotting, precompute, and selection surfaces. The missing standalone pieces were mostly metadata helpers that EEGLAB users expect around dataset/trial selection, trialinfo construction, subject filtering, same-ICA grouping, and cache invalidation after membership changes. The remaining non-PAC rows also needed explicit row-level disposition instead of generic port placeholders.

Approach

Added STUDY helper modules under src/eegprep/functions/studyfunc and lazy exports in src/eegprep/init.py. The helpers preserve EEGLAB-facing 1-based dataset/trial indices at public boundaries, sync STUDY.datasetinfo from ALLEEG when practical, and avoid any runtime dependency on src/eegprep/eeglab. Expanded STUDY_DATA_FIELDS so std_substudy/std_rmdat/std_rmalldatafields invalidate stale cached plotting and measure fields after dataset membership changes. Updated Sphinx docs, implementation notes, and docs/parity/eeglab_core_parity_matrix.json so non-PAC #149 rows are implemented, consolidated, matlab_runtime_skip, or external_dependency_skip; PAC rows remain open for #148.

Key code

def std_selectdataset(
    STUDY: dict[str, Any],
    ALLEEG: list[dict[str, Any]] | None,
    indvar: str | None = "",
    indvarvals: Any = None,
    verboseFlag: str = "verbose",
) -> tuple[list[int], list[list[int]]]:
    _ = verboseFlag
    datasets = as_alleeg_list(ALLEEG)
    study = sync_datasetinfo(ensure_study(STUDY), datasets)
    infos = [info for info in study.get("datasetinfo") or [] if isinstance(info, dict)]
    trialselect = _all_trials(infos, datasets)
    if _empty_value(indvar):
        return list(range(1, len(infos) + 1)), trialselect
output = deepcopy(study)
if remove_data:
    output["datasetinfo"] = [deepcopy(infos[index - 1]) for index in keep]
    for index, info in enumerate(output["datasetinfo"], start=1):
        info["index"] = index
    output_alleeg = [deepcopy(datasets[index - 1]) for index in keep if index <= len(datasets)]
else:
    output["datasetinfo"] = deepcopy(infos)
    output_alleeg = deepcopy(datasets)
_remap_dataset_references(output, mapping)
output = std_rmalldatafields(output, "both")

Tests

The new tests/test_study_long_tail_helpers.py uses three loaded datasets with datasetinfo and per-trial event metadata. It covers dataset and trial independent-variable discovery, 1-based selection, missing trialinfo-key position preservation, numeric continuous trial filters, direct ALLEEG sync, subject filtering, same-ICA grouping with subject boundaries, STUDY subsetting, dataset removal, event-count filtering, and cache-field clearing. Existing STUDY metadata, measures, and end-to-end tests were rerun to cover integration with current design/precompute/read/plot paths.

@suraj-ranganath
suraj-ranganath merged commit b5be08d into feature/eeglab-core-parity-completion Jun 7, 2026
1 check 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