Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SethYC committed Jul 31, 2022
2 parents 1df30f5 + ec271d8 commit 0a512e4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 81 deletions.
57 changes: 33 additions & 24 deletions UI/temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,25 @@ def setupUi(self, MainWindow):
self.label_3.setGeometry(QtCore.QRect(210, 30, 81, 21))
self.label_3.setObjectName("label_3")
self.verticalLayoutWidget = QtWidgets.QWidget(self.groupBox)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(40, 70, 160, 88))
self.verticalLayoutWidget.setGeometry(QtCore.QRect(40, 70, 141, 88))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.checkBox = QtWidgets.QCheckBox(self.verticalLayoutWidget)
self.checkBox.setObjectName("checkBox")
self.verticalLayout.addWidget(self.checkBox)
self.checkBox_2 = QtWidgets.QCheckBox(self.verticalLayoutWidget)
self.checkBox_2.setTristate(False)
self.checkBox_2.setObjectName("checkBox_2")
self.verticalLayout.addWidget(self.checkBox_2)
self.checkBox_3 = QtWidgets.QCheckBox(self.verticalLayoutWidget)
self.checkBox_3.setObjectName("checkBox_3")
self.verticalLayout.addWidget(self.checkBox_3)
self.checkBox_4 = QtWidgets.QCheckBox(self.verticalLayoutWidget)
self.checkBox_4.setObjectName("checkBox_4")
self.verticalLayout.addWidget(self.checkBox_4)
self.radioButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton.setObjectName("radioButton")
self.verticalLayout.addWidget(self.radioButton)
self.radioButton_2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton_2.setObjectName("radioButton_2")
self.verticalLayout.addWidget(self.radioButton_2)
self.radioButton_3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton_3.setObjectName("radioButton_3")
self.verticalLayout.addWidget(self.radioButton_3)
self.radioButton_4 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton_4.setObjectName("radioButton_4")
self.verticalLayout.addWidget(self.radioButton_4)
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.groupBox)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(400, 130, 239, 25))
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(400, 130, 239, 32))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.verticalLayoutWidget_2)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
Expand All @@ -74,13 +73,22 @@ def setupUi(self, MainWindow):
self.pushButton_3 = QtWidgets.QPushButton(self.verticalLayoutWidget_2)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(460, 30, 75, 23))
self.horizontalLayoutWidget = QtWidgets.QWidget(self.groupBox)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(400, 30, 220, 41))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.pushButton_4 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_2.addWidget(self.pushButton_4)
self.gridLayout.addWidget(self.groupBox, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 748, 21))
self.menubar.setGeometry(QtCore.QRect(0, 0, 748, 24))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
Expand All @@ -97,13 +105,14 @@ def retranslateUi(self, MainWindow):
self.groupBox.setTitle(_translate("MainWindow", "Parameters"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:12pt;\">Features</span></p></body></html>"))
self.label_3.setText(_translate("MainWindow", "Feature Value"))
self.checkBox.setText(_translate("MainWindow", "Channel 1"))
self.checkBox_2.setText(_translate("MainWindow", "Channel 2"))
self.checkBox_3.setText(_translate("MainWindow", "Channel 3"))
self.checkBox_4.setText(_translate("MainWindow", "Channel 4"))
self.pushButton.setText(_translate("MainWindow", "Play"))
self.radioButton.setText(_translate("MainWindow", "Channel 1"))
self.radioButton_2.setText(_translate("MainWindow", "Channel 2"))
self.radioButton_3.setText(_translate("MainWindow", "Channel 3"))
self.radioButton_4.setText(_translate("MainWindow", "Channel 4"))
self.pushButton.setText(_translate("MainWindow", "Play Music"))
self.pushButton_3.setText(_translate("MainWindow", "Stop"))
self.pushButton_2.setText(_translate("MainWindow", "Plot "))
self.pushButton_2.setText(_translate("MainWindow", "Apply EEG Signal"))
self.pushButton_4.setText(_translate("MainWindow", "Stop"))


if __name__ == "__main__":
Expand Down
46 changes: 28 additions & 18 deletions UI/temp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,34 @@
<rect>
<x>40</x>
<y>70</y>
<width>160</width>
<width>141</width>
<height>88</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCheckBox" name="checkBox">
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>Channel 1</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_2">
<widget class="QRadioButton" name="radioButton_2">
<property name="text">
<string>Channel 2</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_3">
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>Channel 3</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_4">
<widget class="QRadioButton" name="radioButton_4">
<property name="text">
<string>Channel 4</string>
</property>
Expand All @@ -133,14 +130,14 @@
<x>400</x>
<y>130</y>
<width>239</width>
<height>25</height>
<height>32</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Play</string>
<string>Play Music</string>
</property>
</widget>
</item>
Expand All @@ -153,18 +150,31 @@
</item>
</layout>
</widget>
<widget class="QPushButton" name="pushButton_2">
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>460</x>
<x>400</x>
<y>30</y>
<width>75</width>
<height>23</height>
<width>220</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>Plot </string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>Apply EEG Signal</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
Expand All @@ -177,7 +187,7 @@
<x>0</x>
<y>0</y>
<width>748</width>
<height>21</height>
<height>24</height>
</rect>
</property>
</widget>
Expand Down
64 changes: 35 additions & 29 deletions brain-music-player.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import UI.live_matplot_funcs as live_matplot_funcs
from os import path
import btm
from btm import BTM

matplotlib.use('Qt5Agg')

Expand All @@ -41,17 +41,20 @@ def __init__(self):
self.ui = uic.loadUi(ui_path,self)
self.resize(888, 600)

# LSL stream
#get and print list of detected lsl streams
streams = resolve_byprop('type', 'EEG', timeout=2)
print(streams)
#choose the stream we want, since there is only one it'll be the first
stream = streams[0]
#number of samples per second
sample_rate = 250
#the object that lets us pull data from the stream
self.inlet = StreamInlet(stream, max_chunklen = sample_rate)
# # LSL stream
# #get and print list of detected lsl streams
# streams = resolve_byprop('type', 'EEG', timeout=2)
# print(streams)
# #choose the stream we want, since there is only one it'll be the first
# stream = streams[0]
# #number of samples per second
# sample_rate = 250
# #the object that lets us pull data from the stream
# self.inlet = StreamInlet(stream, max_chunklen = sample_rate)

self.btm = BTM()
self.btm.connect(5)

#Flags
self.music_on = False
self.plot_on = False
Expand Down Expand Up @@ -106,19 +109,20 @@ def __init__(self):
self.comboBox.currentIndexChanged['QString'].connect(self.update_feature)

# Checkbox Events
self.checkBox.stateChanged.connect(self.update_channel)
self.checkBox_2.stateChanged.connect(self.update_channel)
self.checkBox_3.stateChanged.connect(self.update_channel)
self.checkBox_4.stateChanged.connect(self.update_channel)
# self.radioButton.stateChanged.connect(self.update_channel)
# self.radioButton_2.stateChanged.connect(self.update_channel)
# self.radioButton_3.stateChanged.connect(self.update_channel)
# self.radioButton_4.stateChanged.connect(self.update_channel)

def getData(self):
QtWidgets.QApplication.processEvents()
CHUNK = self.CHUNK

while(self.plot_on):
QtWidgets.QApplication.processEvents()
samples,time = self.inlet.pull_chunk(timeout=.5, max_samples=CHUNK)

# samples,time = self.inlet.pull_chunk(timeout=.5, max_samples=CHUNK)
samples = self.btm.stream_update([0])
print(samples)
self.pq.put_nowait(samples)

if self.plot_on is False:
Expand Down Expand Up @@ -209,7 +213,8 @@ def getAudio(self):
self.pushButton.setEnabled(True)

def start_plot(self):

self.btm.init_buffer(1)

self.pushButton_2.setEnabled(False)
self.canvas.axes.clear()
self.plot_on = True
Expand All @@ -218,6 +223,7 @@ def start_plot(self):
self.preference_plot = None
self.timer.setInterval(30) #msec


def start_music(self):

self.pushButton.setEnabled(False)
Expand Down Expand Up @@ -265,11 +271,11 @@ def update_plot(self):

chunk_data = np.vstack(self.pdata).T
new_data = chunk_data[0] #get a shape (250,)
# self.plotdata = live_matplot_funcs.update_data_array(self.plotdata, new_data)

self.plotdata = live_matplot_funcs.update_data_array(self.plotdata, new_data)

self.plotdata[ -len(new_data) : ] = new_data

# self.plotdata[ -len(new_data) : ] = new_data

self.plotdata = new_data
if self.preference_plot is None:
plot_refs = self.canvas.axes.plot( self.plotdata, color=(0,1,0.29))
self.preference_plot = plot_refs[0]
Expand All @@ -278,9 +284,9 @@ def update_plot(self):

self.canvas.axes.yaxis.grid(True,linestyle='--')
start, end = self.canvas.axes.get_ylim()
self.canvas.axes.yaxis.set_ticks(np.arange(start, end, 0.5))
self.canvas.axes.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
self.canvas.axes.set_ylim( ymin=-1, ymax=1)
# self.canvas.axes.yaxis.set_ticks(np.arange(start, end, 0.5))
# self.canvas.axes.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
self.canvas.axes.set_ylim( ymin=-10, ymax=10)

self.canvas.draw()
except Exception as e:
Expand Down Expand Up @@ -341,11 +347,11 @@ def run(self):

if __name__ == '__main__':

stream_process = Process(target=live_matplot_funcs.sendingData)
stream_process.start()
# stream_process = Process(target=live_matplot_funcs.sendingData)
# stream_process.start()

if stream_process.is_alive():
print("streaming data...")
# if stream_process.is_alive():
# print("streaming data...")

app = QtWidgets.QApplication(sys.argv)
mainWindow = BRAIN_MUSIC_PLAYER()
Expand Down
23 changes: 13 additions & 10 deletions btm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class BTM:
def __init__(self):
self.buffer_len = 20 ### length of the buffer (sec)
self.buffer_len = 5 ### length of the buffer (sec)
self.freqs = 0 ### variable for input frequency
self.data_chunk = 0.25 ### data chunk each time buffer is updated

Expand Down Expand Up @@ -59,16 +59,10 @@ def run(self, channels):
while True:
self.stream_update(channels)
# print(feature_extract(eeg_buffer))
print(self.eeg_buffer)
# print(self.eeg_buffer)

# Enable for generating sample data
# if eeg_buffer[0,0] != 0:
# with open('eegs.csv', 'w', newline='') as csvfile:
# for row in eeg_buffer:
# print
# spamwriter = csv.writer(csvfile, delimiter=' ')
# spamwriter.writerow(row)
# break
# self.buffer_to_file(eeg_buffer)


except Exception as e:
Expand All @@ -81,8 +75,10 @@ def stream_update(self, channels):

ch_data = np.array(eeg_data)[:, channels]
self.eeg_buffer = self.update_buffer(self.eeg_buffer, ch_data)
print(self.eeg_buffer)
return self.eeg_buffer


#### Helper functions
def get_buffer_len(self):
"""
Expand Down Expand Up @@ -120,10 +116,17 @@ def update_buffer(self, data_buffer, new_data):

return new_buffer

def buffer_to_file(self, eeg_buffer):
if eeg_buffer[0,0] != 0:
with open('eegs.csv', 'w', newline='') as csvfile:
for row in eeg_buffer:
print
spamwriter = csv.writer(csvfile, delimiter=' ')
spamwriter.writerow(row)

if __name__ == "__main__":

btm = BTM()
stream_in, __ = btm.connect(5)
btm.run([0, 1, 2, 3])
btm.run([0])

0 comments on commit 0a512e4

Please sign in to comment.