Source code for brainaccess.sdk.p300_classifier

from ctypes import *
from brainaccess.sdk import ba_sdk
from brainaccess.utilities import * 
from brainaccess.sdk.ba_sdk_ctypes import *   

sdk = ba_sdk.get_sdk()

# this P300 Classifier wrapper needs to remember the number of classes set
# in order to parse prediction score list
num_classes_set = 0

# API
sdk.baSDK_initializeP300Classifier.restype = c_int
[docs]def initialize(channel_indices): """ Initializes P300 Classifier's internal structures, intializes BrainAccess Core library and attempts to connect to BrainAccess EEG hardware. Args: ``channel_indices`` (**list[int]**): indices of channels that should be used by the algorithm. The first two channels need to be placed in Fp1 and Fp2 positions and their indices must be smaller than other provided channels. We simply recommend using channels 0 and 1 for Fp1 and Fp2 electrodes. Other channels can be in central, parietal or occipital regions. Returns: ``bool``: True if successful, False on error. """ ch_ptr = python_array_to_ctype(channel_indices, c_int) if sdk.baSDK_initializeP300Classifier(ch_ptr, len(channel_indices)) != 0: print("P300 classifier could not be initialized") return False return True
sdk.baSDK_startP300Classifier.restype = c_int
[docs]def start(): """ Starts EEG data collection Should be called before :meth:`brainaccess.sdk.p300_classifier.collect_class_data` and :meth:`brainaccess.sdk.p300_classifier.predict`. Returns: ``bool``: True if successful, False on error. """ return sdk.baSDK_startP300Classifier() == 0
sdk.baSDK_stopP300Classifier.restype = c_int
[docs]def stop(): """ Stops EEG data collection. Should be called when P300 classifier is no longer needed. Returns: ``bool``: True if successful, False on error. """ return sdk.baSDK_stopP300Classifier() == 0
sdk.baSDK_setP300NumberClasses.restype = c_int
[docs]def set_num_classes(num_classes): """ Sets the number of classes for which the data will be collected. This usually refers to a number of choices or a number of columns/lines in a grid of choices provided for the user. Args: ``num_classes`` (**int**): number of classes. Returns: ``bool``: True if successful, False on error. """ global num_classes_set num_classes_set = num_classes return sdk.baSDK_setP300NumberClasses(c_int(num_classes)) == 0
sdk.baSDK_collectP300Data.restype = c_int
[docs]def collect_class_data(class_idx): """ Records and adds data to existing measurements of a certain class Equal number of additions for each class should be made before calling :meth:`brainaccess.sdk.p300_classifier.predict`. The number of additions per class should be 2-3 for an 8 channel system. This function should be called straight after visual stimulus. Args: ``class_idx`` (**int**): index of the class for which the data should be collected. Returns: ``int``: 0 if successful, -1 on error, -2 if the recorded data did not contain enough channels giving good quality signals. """ return sdk.baSDK_collectP300Data(c_int(class_idx))
sdk.baSDK_estimateP300SignalQuality.restype = P300QualityEstimationResponseCType
[docs]def estimate_signal_quality(): """ Estimates signal quality of different channels, and only channels with good quality are then used in acquisition. It is recommended to use this function after attaching the electrodes and before running the rest of the algorithm. User should stay still for 5s when this method is called. Returns: ``int``: The number of channels giving good quality signal, the more good channels the less repeated measurements are needed. -1 on failure. """ result = sdk.baSDK_estimateP300SignalQuality() return result.error == 1, result.numGoodChannels, result.enoughGoodChannels == 1
sdk.baSDK_predictP300.restype = POINTER(c_double)
[docs]def predict(): """ Predicts class chosen by user from data collected during :meth:`brainaccess.sdk.p300_classifier.collect_class_data` calls. Before calling this function, all of the classes must have received an equal number of :meth:`brainaccess.sdk.p300_classifier.collect_class_data` calls. Returns: ``int``: Predicted class index or -1 if an error occured. """ return sdk.baSDK_predictP300()[:num_classes_set]