Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/deploy_cpp_libs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
max-parallel: 16
matrix:
build_type: [Release, Debug]
os: [ubuntu-latest, macos-14]
os: [ubuntu-latest, macos-15]

steps:
- name: Clone Repository
Expand All @@ -69,15 +69,15 @@ jobs:
with:
cmakeVersion: '3.21.4'
- name: Install Ninja
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
if ! command -v ninja; then
brew install ninja
fi
ninja --version
# build SimpleCBLE outside from brainflow because of different deployment targets
- name: Compile SimpleCBLE MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
mkdir $GITHUB_WORKSPACE/third_party/SimpleBLE/simplecble/build
cd $GITHUB_WORKSPACE/third_party/SimpleBLE/simplecble/build
Expand All @@ -88,7 +88,7 @@ jobs:
env:
BUILD: ${{ matrix.build_type }}
- name: Compile BrainFlow for MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
mkdir $GITHUB_WORKSPACE/build
cd $GITHUB_WORKSPACE/build
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/run_matlab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ jobs:
uses: matlab-actions/run-command@v2
with:
command: addpath('matlab_package/brainflow'),addpath('matlab_package/brainflow/examples'),addpath('matlab_package/brainflow/inc'),addpath('matlab_package/brainflow/lib'),ICA
- name: Run Signal Quality Regression
uses: matlab-actions/run-command@v2
with:
command: addpath('matlab_package/brainflow'),addpath('matlab_package/brainflow/examples'),addpath('matlab_package/brainflow/inc'),addpath('matlab_package/brainflow/lib'),signal_quality_regression
38 changes: 20 additions & 18 deletions .github/workflows/run_unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
os: [ubuntu-latest, macos-14]
os: [ubuntu-latest, macos-15]

steps:
# compile and prepare env
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
run: |
npm install -g ts-node
- name: Install Ninja
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
if ! command -v ninja; then
brew install ninja
Expand All @@ -67,15 +67,15 @@ jobs:
java-version: 11.0.3
# build SimpleCBLE outside from brainflow because of different deployment targets
- name: Compile SimpleCBLE MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
mkdir $GITHUB_WORKSPACE/third_party/SimpleBLE/simplecble/build
cd $GITHUB_WORKSPACE/third_party/SimpleBLE/simplecble/build
cmake -G Ninja -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DCMAKE_OSX_DEPLOYMENT_TARGET=10.15 -DCMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/installed -DCMAKE_BUILD_TYPE=Release ..
ninja
ninja install
- name: Compile BrainFlow MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
mkdir $GITHUB_WORKSPACE/build
cd $GITHUB_WORKSPACE/build
Expand All @@ -85,23 +85,23 @@ jobs:
env:
BRAINFLOW_VERSION: ${{ steps.version.outputs.version }}
- name: Build Swift Package MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/swift_package
swift --version
BRAINFLOW_LIB_DIR=$GITHUB_WORKSPACE/installed/lib swift build
- name: Test Swift Package MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/swift_package
BRAINFLOW_LIB_DIR=$GITHUB_WORKSPACE/installed/lib swift test
- name: Swift CLI Synthetic Board MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/swift_package
BRAINFLOW_LIB_DIR=$GITHUB_WORKSPACE/installed/lib swift run brainflow-swift-cli
- name: Swift Examples MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/swift_package
for example in \
Expand All @@ -119,27 +119,27 @@ jobs:
BRAINFLOW_LIB_DIR=$GITHUB_WORKSPACE/installed/lib swift run "$example"
done
- name: Build Apple XCFramework Artifacts
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
$GITHUB_WORKSPACE/tools/apple/build_xcframeworks.sh \
--output $GITHUB_WORKSPACE/build/apple_xcframeworks
env:
BRAINFLOW_VERSION: ${{ steps.version.outputs.version }}
- name: Verify Apple XCFramework Artifacts
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
$GITHUB_WORKSPACE/tools/apple/verify_xcframeworks.sh $GITHUB_WORKSPACE/build/apple_xcframeworks
- name: Build Generated Swift Binary Package MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/build/apple_xcframeworks/BrainFlowSwiftBinaryPackage
swift build
- name: Test Generated Swift Binary Package MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
$GITHUB_WORKSPACE/tools/apple/test_swift_binary_package.sh $GITHUB_WORKSPACE/build/apple_xcframeworks
- name: Build iOS Demo With Generated XCFrameworks
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
BRAINFLOW_APPLE_XCFRAMEWORKS_DIR=$GITHUB_WORKSPACE/build/apple_xcframeworks/XCFrameworks \
xcodebuild -quiet \
Expand All @@ -151,12 +151,12 @@ jobs:
CODE_SIGNING_ALLOWED=NO \
build
- name: Package macOS Demo With Generated XCFrameworks
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
BRAINFLOW_APPLE_XCFRAMEWORKS_DIR=$GITHUB_WORKSPACE/build/apple_xcframeworks/XCFrameworks \
$GITHUB_WORKSPACE/tools/apple/package_macos_demo_app.sh $GITHUB_WORKSPACE/build/apple_xcframeworks/BrainFlowMacDemo.app
- name: Upload Apple XCFramework Artifacts
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
uses: actions/upload-artifact@v4
with:
name: brainflow-apple-xcframeworks
Expand Down Expand Up @@ -243,7 +243,7 @@ jobs:
- name: Run unit tests
run: $GITHUB_WORKSPACE/build/tests/brainflow_tests
- name: Synthetic C# Test MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/csharp_package/brainflow/examples/brainflow_get_data/bin/Release/net7.0
cp $GITHUB_WORKSPACE/installed/lib/libBoardController.dylib $GITHUB_WORKSPACE/csharp_package/brainflow/examples/brainflow_get_data/bin/Release/net7.0/
Expand All @@ -253,7 +253,7 @@ jobs:
LD_LIBRARY_PATH: ${{ github.workspace }}/installed/lib
DYLD_LIBRARY_PATH: ${{ github.workspace }}/installed/lib
- name: EEG Metrics C# Test MacOS
if: (matrix.os == 'macos-14')
if: runner.os == 'macOS'
run: |
cd $GITHUB_WORKSPACE/csharp_package/brainflow/examples/eeg_metrics/bin/Release/net7.0
cp $GITHUB_WORKSPACE/installed/lib/libBoardController.dylib $GITHUB_WORKSPACE/csharp_package/brainflow/examples/eeg_metrics/bin/Release/net7.0/
Expand Down Expand Up @@ -384,6 +384,8 @@ jobs:
run: sudo -H python3 $GITHUB_WORKSPACE/python_package/examples/tests/band_power_all.py
- name: Band Power Regression Python
run: sudo -H python3 $GITHUB_WORKSPACE/python_package/examples/tests/band_power_regression.py
- name: Signal Quality Regression Python
run: sudo -H python3 $GITHUB_WORKSPACE/python_package/examples/tests/signal_quality_regression.py
- name: Signal Processing Regression Python
run: |
sudo -H python3 $GITHUB_WORKSPACE/python_package/examples/tests/signal_processing_regression.py
Expand Down Expand Up @@ -557,7 +559,7 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Push Libraries MacOS Dev
if: ${{ github.event_name == 'push' && matrix.os == 'macos-14' && github.repository == 'brainflow-dev/brainflow' }}
if: ${{ github.event_name == 'push' && runner.os == 'macOS' && github.repository == 'brainflow-dev/brainflow' }}
run: |
aws s3 cp $GITHUB_WORKSPACE/installed/lib/ s3://brainflow/$GITHUB_SHA/macos_release --recursive
env:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/run_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ jobs:
python python_package\examples\tests\signal_metrics_regression.py
python python_package\examples\tests\binding_validation_regression.py
shell: cmd
- name: Signal Quality Regression Python
run: python python_package\examples\tests\signal_quality_regression.py
shell: cmd
- name: Signal Processing Regression Csharp
run: dotnet run --project csharp_package\brainflow\examples\signal_processing_regression
- name: Signal Processing Regression Rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <functional>
#include <iostream>
#include <limits>
#include <memory>
#include <stdexcept>

#include "data_filter.h"
Expand All @@ -19,9 +20,49 @@ static void reject (const std::function<void ()> &action)
throw std::runtime_error ("Expected invalid input to be rejected");
}

static void quality_tests ()
{
BrainFlowArray<double, 2> data (3, 1024);
for (int row = 0; row < 3; ++row)
for (int i = 0; i < 1024; ++i)
data.at (row, i) = (row + 1) * std::sin (2 * 3.141592653589793 * 10 * i / 128 + .1);
SignalQualityOptions options;
options.enable_clipping = true;
options.lower_bound = -10;
options.upper_bound = 10;
options.min_channels = 2;
data.at (1, 200) = 1000;
auto result =
DataFilter::get_avg_band_powers_with_quality (data, {2, 1, 0}, 128, false, options);
if (!result.valid || result.used_channels != std::vector<int> ({2, 0}) ||
result.rejected_channels != std::vector<int> ({1}) ||
result.quality[1].flags != BRAINFLOW_QUALITY_CLIPPED || data.at (1, 200) != 1000)
throw std::runtime_error ("Quality exclusion, channel identity or raw preservation failed");
auto expected = DataFilter::get_avg_band_powers (data, {2, 0}, 128, false);
std::unique_ptr<double[]> means (expected.first), variation (expected.second);
for (int i = 0; i < 5; ++i)
if (result.avg_band_powers[i] != means[i] || result.stddev_band_powers[i] != variation[i])
throw std::runtime_error ("Quality-aware calculation changed the accepted-channel DSP");
data.at (0, 100) = std::numeric_limits<double>::quiet_NaN ();
result = DataFilter::get_avg_band_powers_with_quality (data, {0, 1, 2}, 128, false, options);
if (result.valid || !result.avg_band_powers.empty () || !result.stddev_band_powers.empty ())
throw std::runtime_error ("Insufficient channels exposed usable powers");
auto quality = DataFilter::get_signal_quality (data, {0, 1, 2}, 128, options);
if (quality.quality[0].flags != BRAINFLOW_QUALITY_NONFINITE)
throw std::runtime_error ("Nonfinite diagnosis missing");
reject ([&] { DataFilter::get_signal_quality (data, {0, 0}, 128); });
reject ([&] { DataFilter::get_signal_quality (data, {-1}, 128); });
options.min_channels = 0;
reject (
[&] {
DataFilter::get_avg_band_powers_with_quality (data, {0, 1}, 128, false, options);
});
}

int main ()
try
{
quality_tests ();
BrainFlowArray<double, 2> data (2, 256);
reject ([&] { data.at (-1, 0); });
reject ([&] { data.at (0, 256); });
Expand Down
110 changes: 110 additions & 0 deletions cpp_package/src/data_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdarg>
#include <limits>
#include <memory>
#include <set>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -428,6 +429,115 @@ std::pair<double *, double *> DataFilter::get_custom_band_powers (
return std::make_pair (avg_bands.release (), stddev_bands.release ());
}

namespace
{
BrainFlowSignalQualityParams quality_params (const SignalQualityOptions &options)
{
BrainFlowSignalQualityParams p = {};
p.struct_size = sizeof (p);
p.version = 1;
p.enable_clipping = options.enable_clipping ? 1 : 0;
p.lower_bound = options.lower_bound;
p.upper_bound = options.upper_bound;
p.max_clipped_percentage = options.max_clipped_percentage;
p.flatline_tolerance = options.flatline_tolerance;
p.max_flatline_seconds = options.max_flatline_seconds;
p.min_channels = options.min_channels;
return p;
}

std::vector<double> quality_input (
const BrainFlowArray<double, 2> &data, const std::vector<int> &channels)
{
validate_channels (data, channels);
if (std::set<int> (channels.begin (), channels.end ()).size () != channels.size ())
throw BrainFlowException (
"duplicate channels", (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR);
const int cols = data.get_size (1);
std::vector<double> input ((size_t)cols * channels.size ());
for (size_t i = 0; i < channels.size (); ++i)
std::copy (data.get_address (channels[i]), data.get_address (channels[i]) + cols,
input.begin () + i * cols);
return input;
}

void quality_channels (SignalQualityResult &result)
{
for (size_t i = 0; i < result.channels.size (); ++i)
(result.quality[i].flags ? result.rejected_channels : result.used_channels)
.push_back (result.channels[i]);
}
}

SignalQualityResult DataFilter::get_signal_quality (const BrainFlowArray<double, 2> &data,
std::vector<int> channels, int sampling_rate, const SignalQualityOptions &options)
{
auto input = quality_input (data, channels);
auto params = quality_params (options);
SignalQualityResult result;
result.channels = channels;
result.quality.resize (channels.size ());
int ec = ::get_signal_quality (input.data (), (int)channels.size (), data.get_size (1),
sampling_rate, &params, result.quality.data ());
if (ec != (int)BrainFlowExitCodes::STATUS_OK)
throw BrainFlowException ("failed to calculate signal quality", ec);
quality_channels (result);
return result;
}

BandPowerQualityResult DataFilter::get_custom_band_powers_with_quality (
const BrainFlowArray<double, 2> &data, std::vector<std::pair<double, double>> bands,
std::vector<int> channels, int sampling_rate, bool apply_filters,
const SignalQualityOptions &options)
{
auto input = quality_input (data, channels);
if (bands.empty () || bands.size () > (size_t)(std::numeric_limits<int>::max) ())
throw BrainFlowException (
"invalid bands", (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR);
auto params = quality_params (options);
BandPowerQualityResult result;
result.channels = channels;
result.quality.resize (channels.size ());
result.avg_band_powers.resize (bands.size ());
result.stddev_band_powers.resize (bands.size ());
std::vector<double> starts, stops;
for (const auto &band : bands)
{
starts.push_back (band.first);
stops.push_back (band.second);
}
int valid = 0;
int ec = ::get_custom_band_powers_with_quality (input.data (), (int)channels.size (),
data.get_size (1), starts.data (), stops.data (), (int)bands.size (), sampling_rate,
apply_filters ? 1 : 0, &params, result.avg_band_powers.data (),
result.stddev_band_powers.data (), result.quality.data (), &valid);
if (ec != (int)BrainFlowExitCodes::STATUS_OK)
throw BrainFlowException ("failed to calculate quality-aware band powers", ec);
result.valid = valid != 0;
if (!result.valid)
{
result.avg_band_powers.clear ();
result.stddev_band_powers.clear ();
}
quality_channels (result);
if (result.valid)
result.reason = "ok";
else if (result.used_channels.size () < (size_t)options.min_channels)
result.reason = "insufficient_channels";
else
result.reason = "no_band_power";
return result;
}

BandPowerQualityResult DataFilter::get_avg_band_powers_with_quality (
const BrainFlowArray<double, 2> &data, std::vector<int> channels, int sampling_rate,
bool apply_filters, const SignalQualityOptions &options)
{
return get_custom_band_powers_with_quality (data,
{{2.0, 4.0}, {4.0, 8.0}, {8.0, 13.0}, {13.0, 30.0}, {30.0, 45.0}}, channels, sampling_rate,
apply_filters, options);
}

double DataFilter::get_band_power (
std::pair<double *, double *> psd, int data_len, double freq_start, double freq_end)
{
Expand Down
Loading
Loading