frompymindwave2importMindWaveMobile2,Session,SessionConfig# Initialize and connect to the headsetheadset=MindWaveMobile2()success=headset.start(n_tries=5,timeout=30)ifsuccess:# if the headset is connected successfully# Create a session configurationsess_config=SessionConfig(user_name="Ahmed",classes=["left","right"]# Define your classification tasks)# Initialize and start the recording sessionsession=Session(headset,config=sess_config)whileheadset.signal_quality!=100:time.sleep(1)# wait for the user to wear the headset properlysession.start()# Start recording datawhilesession.is_active:time.sleep(1)# wait for the session to finishsession.save()# save the recorded data to disk
sess_config=SessionConfig(user_name="Ahmed",# Name of the useruser_age=0,# Age of the userclasses=["left","right","foot"],# Classes used for motor imagery tasksn_trials=10,# Number of trials for each classcapture_blinks=True,# Whether to capture blinks or notmotor_duration=5,# Duration of each motor imagery task in seconds)
for the full list of attributes, check SessionConfig
Event Listeners
An example to subscribe to headset data emitted events
frompymindwave2importMindWaveMobile2,HeadsetDataEventheadset=MindWaveMobile2()success=headset.start(n_tries=5,timeout=30)ifsucess:defmeditation_handler(event:HeadsetDataEvent):print(event.data.meditation)subscription=headset.on_data(meditation_handler)whilesome_condition:time.sleep(1)subscription.detach()# Unsubscribe from the event
An example to reconnect to the headset if the connection is lost
...# headset connectiondefsignal_quality_handler(event:SignalQualityEvent):ifevent.signal_quality<100:print("Please wear the headset properly")subscription=headset.on_signal_quality_change(signal_quality_handler)
Subscribe to session signals, such as start and end of each trial and phases of the trial. This can be used to build a GUI on top of it to create data collection environment.
...# session and headset connectiondefsession_handler(event:SessionEvent):signal=event.signalifsignal==SessionSignal.SESSION_START:print("Session started")elifsignal==SessionSignal.TRIAL_START:print("Trial started for class:",event.class_name)elifsignal==SessionSignal.REST:print("Resting phase")elifsignal==SessionSignal.READY:print("Ready for the motor imagery task")elifsignal==SessionSignal.CUE:print("Cue for the motor imagery task")elifsignal==SessionSignal.MOTOR:print("Motor imagery task")elifsignal==SessionSignal.TRIAL_END:print("Trial ended")elifsignal==SessionSignal.SESSION_END:print("Session ended")subscription=session.on_signal(session_handler)
for the full list of Session Signals, check SessionSignal.
for the data collection session workflow, check Session Workflow.
Logging
The module has different levels of logging, you can change the logging level to see more or less information, as well as logging to a file or console options.
Note: if you want to configure the logger, do it before initializing any object from the module