Skip to content

Add signal-quality checks and optional channel rejection for band powers - #875

Merged
Andrey1994 merged 5 commits into
masterfrom
feat/band-power-quality
Nov 7, 2026
Merged

Andrey1994 merged 5 commits into
masterfrom
feat/band-power-quality

Conversation

@Andrey1994

@Andrey1994 Andrey1994 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

A clipped or stuck EEG channel can dominate the channel-averaged spectrum and move every normalized band. This adds opt-in native quality checks and band-power methods that exclude rejected whole channel windows before filtering and aggregation. Existing APIs keep their behavior.

  • The C ABI exposes get_signal_quality and get_custom_band_powers_with_quality with a versioned policy and per-channel reports. The bindings can also call equivalent entry points that use primitive buffers.
  • C++, Python, C#, Java, TypeScript, Rust, Swift, Julia, and MATLAB expose standalone quality checks and custom or five-band power methods. Results identify accepted and rejected channels, measurements, and aggregate validity. R accesses the Python methods through reticulate.
  • Checks cover nonfinite values, caller-defined clipping limits and sustained flatlines. Too few acceptable channels or zero total power produces an explicit invalid result, with no usable powers.
  • Native DEBUG diagnostics and a runnable playback example provide per-window JSONL and rejection/recovery transition logging. The example avoids repeating warnings on overlapping windows.
  • Documentation defines units, threshold boundaries, disabled checks, minimum data requirements and classifier implications. New Python regressions run in Windows and Unix CI; the existing C++ regression now covers this API.

The API is stateless: recovery dwell belongs to the caller. It does not inspect timestamps, splice samples, alter raw recordings, or automatically classify every contact/movement artifact. Thresholds are explicit device/application settings.

Binding validation

  • C++, Python, C#, Java, TypeScript, Rust, Swift, and Julia regressions passed against a freshly built DataHandler after syncing with master. Cppcheck passed locally.
  • The MATLAB signal-quality regression passed in GitHub Actions.

Original Windows/MSVC Release validation

  • Fresh DataHandler build; 18 new quality tests, 18 existing band-power tests and 11 existing signal-metric tests passed.
  • C++ binding regression passed; exported declarations also compiled as C.
  • Clean inputs and accepted-channel subsets matched the existing band-power calculation exactly, including filtered results and channel variation.
  • A local 70-second worn Athena recording replayed through 322 six-second windows: TP10 was rejected for clipping in 103 windows, the other three channels remained usable, and the log contained two rejection/two recovery transitions. Every result matched manually selecting its reported accepted channels with zero numerical difference. Raw recordings are not included; this validates rejection/DSP behavior, not physiological accuracy.
  • git diff --check passed.

NeuroGames integration and classifier revalidation are separate from this library PR.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T00:59:11.542334Z 55ed29a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Expose signal quality and quality-aware band powers in C#, Java, TypeScript, Rust, Swift, Julia, and MATLAB using the existing native implementation. Exercise channel ordering, flags, power parity, and invalid windows through the language bindings.
Keep the signal-quality and reusable band-power regressions, and link the Swift API notes to the shared signal-quality documentation.
Validate logical and numeric switches before marshalling, and cover both forms in the MATLAB regression.
Move Unix tests and C++ artifacts to macos-15 and select macOS steps by runner OS. GitHub has retired the macos-14 image.
var report = DataFilter.get_signal_quality(data, channels, rate, options).quality[1];
if (report.channel != 1 || report.flags != (SignalQualityFlags.NONFINITE | SignalQualityFlags.CLIPPED | SignalQualityFlags.FLATLINE) ||
report.nonfinite_percentage != 100.0 / samples || report.clipped_percentage != 200.0 / samples ||
report.longest_flatline_seconds != (samples - 4.0) / rate)
throw new Exception("Quality-aware powers differ from the accepted channel subset");
var report = DataFilter.get_signal_quality(data, channels, rate, options).quality[1];
if (report.channel != 1 || report.flags != (SignalQualityFlags.NONFINITE | SignalQualityFlags.CLIPPED | SignalQualityFlags.FLATLINE) ||
report.nonfinite_percentage != 100.0 / samples || report.clipped_percentage != 200.0 / samples ||
throw new Exception("Quality-aware powers differ from the accepted channel subset");
var report = DataFilter.get_signal_quality(data, channels, rate, options).quality[1];
if (report.channel != 1 || report.flags != (SignalQualityFlags.NONFINITE | SignalQualityFlags.CLIPPED | SignalQualityFlags.FLATLINE) ||
report.nonfinite_percentage != 100.0 / samples || report.clipped_percentage != 200.0 / samples ||
Comment on lines +391 to +393
return DataHandlerLibrary64.get_signal_quality_flat (data, rows, cols, sampling_rate,
clipping, options.lower_bound, options.upper_bound, options.max_clipped_percentage,
options.flatline_tolerance, options.max_flatline_seconds, options.min_channels, flags, measurements);
Comment on lines +395 to +397
return DataHandlerLibrary32.get_signal_quality_flat (data, rows, cols, sampling_rate,
clipping, options.lower_bound, options.upper_bound, options.max_clipped_percentage,
options.flatline_tolerance, options.max_flatline_seconds, options.min_channels, flags, measurements);
Comment on lines +411 to +414
return DataHandlerLibrary64.get_custom_band_powers_with_quality_flat (data, rows, cols,
starts, stops, starts.Length, sampling_rate, filters, clipping, options.lower_bound,
options.upper_bound, options.max_clipped_percentage, options.flatline_tolerance,
options.max_flatline_seconds, options.min_channels, avgs, stddevs, flags, measurements, out valid);
Comment on lines +416 to +419
return DataHandlerLibrary32.get_custom_band_powers_with_quality_flat (data, rows, cols,
starts, stops, starts.Length, sampling_rate, filters, clipping, options.lower_bound,
options.upper_bound, options.max_clipped_percentage, options.flatline_tolerance,
options.max_flatline_seconds, options.min_channels, avgs, stddevs, flags, measurements, out valid);
class DataHandlerLibrary64
{
[DllImport ("DataHandler", CallingConvention = CallingConvention.Cdecl)]
internal static extern int get_signal_quality_flat (double[] data, int rows, int cols, int sampling_rate,
double flatline_tolerance, double max_flatline_seconds, int min_channels, [Out] int[] flags,
[Out] double[] measurements);
[DllImport ("DataHandler", CallingConvention = CallingConvention.Cdecl)]
internal static extern int get_custom_band_powers_with_quality_flat (double[] data, int rows, int cols,
class DataHandlerLibrary32
{
[DllImport ("DataHandler32", CallingConvention = CallingConvention.Cdecl)]
internal static extern int get_signal_quality_flat (double[] data, int rows, int cols, int sampling_rate,
from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError
from brainflow.utils import check_memory_layout_row_major, LogLevels
from numpy.ctypeslib import ndpointer
from brainflow.signal_quality import SignalQualityFlags as SignalQualityFlags
@Andrey1994
Andrey1994 merged commit c60ebca into master Nov 7, 2026
40 checks passed
@Andrey1994
Andrey1994 deleted the feat/band-power-quality branch November 7, 2026 21:27
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