Browse Source

增加buffer_length控制参数

dk 1 year ago
parent
commit
40e04d849f

+ 1 - 1
backend/device/data_client.py

@@ -9,7 +9,7 @@ class NeuracleDataClient:
     UPDATE_INTERVAL = 0.04
     BYTES_PER_NUM = 4
 
-    def __init__(self, n_channel=9, samplerate=1000, host='localhost', port=8712, buffer_len=1):
+    def __init__(self, n_channel=9, samplerate=1000, host='localhost', port=8712, buffer_len=1.):
         self.n_channel = n_channel
         self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
         self.chunk_size = int(self.UPDATE_INTERVAL * samplerate * self.BYTES_PER_NUM * n_channel)

+ 1 - 1
backend/free_grasp.psyexp

@@ -269,7 +269,7 @@
         <Param val="parameter_inputs" valType="code" updates="None" name="name"/>
       </CodeComponent>
       <CodeComponent name="device" plugin="None">
-        <Param val="# connect neo&amp;#10;receiver = NeuracleDataClient(n_channel=len(config_info['channel_labels']), &amp;#10;                               samplerate=config_info['sample_rate'],&amp;#10;                               host=config_info['host'],&amp;#10;                               port=config_info['port'])&amp;#10;&amp;#10;# connect to trigger box&amp;#10;trigger = TriggerNeuracle()&amp;#10;&amp;#10;# connect to mechanical hand&amp;#10;hand_device = FuboPneumaticFingerClient({'port': args.com})&amp;#10;" valType="extendedCode" updates="constant" name="Before Experiment"/>
+        <Param val="# connect neo&amp;#10;receiver = NeuracleDataClient(n_channel=len(config_info['channel_labels']), &amp;#10;                               samplerate=config_info['sample_rate'],&amp;#10;                               host=config_info['host'],&amp;#10;                               port=config_info['port'].&amp;#10;                               buffer_len=config_info['buffer_length'])&amp;#10;&amp;#10;# connect to trigger box&amp;#10;trigger = TriggerNeuracle()&amp;#10;&amp;#10;# connect to mechanical hand&amp;#10;hand_device = FuboPneumaticFingerClient({'port': args.com})&amp;#10;" valType="extendedCode" updates="constant" name="Before Experiment"/>
         <Param val="receiver = new NeuracleDataClient({&quot;n_channel&quot;: config_info[&quot;channel_labels&quot;].length, &quot;samplerate&quot;: config_info[&quot;sample_rate&quot;], &quot;host&quot;: config_info[&quot;host&quot;], &quot;port&quot;: config_info[&quot;port&quot;]});&amp;#10;trigger = new TriggerNeuracle();&amp;#10;hand_device = new FuboPneumaticFingerClient({&quot;port&quot;: args.com});&amp;#10;controller = new Controller(0.0, args.model_path, {&quot;state_change_threshold&quot;: 0.8});&amp;#10;" valType="extendedCode" updates="constant" name="Before JS Experiment"/>
         <Param val="" valType="extendedCode" updates="constant" name="Begin Experiment"/>
         <Param val="" valType="extendedCode" updates="constant" name="Begin JS Experiment"/>

+ 4 - 3
backend/free_grasp.py

@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 """
 This experiment was created using PsychoPy3 Experiment Builder (v2023.2.3),
-    on 十一月 21, 2023, at 14:15
+    on Tue Nov 21 18:13:33 2023
 If you publish work using this script the most relevant publication is:
 
     Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019) 
@@ -93,7 +93,8 @@ controller = Controller(0., args.model_path,
 receiver = NeuracleDataClient(n_channel=len(config_info['channel_labels']), 
                                samplerate=config_info['sample_rate'],
                                host=config_info['host'],
-                               port=config_info['port'])
+                               port=config_info['port'].
+                               buffer_len=config_info['buffer_length'])
 
 # connect to trigger box
 trigger = TriggerNeuracle()
@@ -175,7 +176,7 @@ def setupData(expInfo, dataDir=None):
     thisExp = data.ExperimentHandler(
         name=expName, version='',
         extraInfo=expInfo, runtimeInfo=None,
-        originPath='C:\\Users\\asena\\Desktop\\kraken\\backend\\free_grasp.py',
+        originPath='/Users/dingkunliu/Projects/MI-BCI-Proj/kraken/backend/free_grasp.py',
         savePickle=True, saveWideText=True,
         dataFileName=dataDir + os.sep + filename, sortColumns='time'
     )

File diff suppressed because it is too large
+ 0 - 0
backend/general_grasp_training.psyexp


+ 4 - 3
backend/general_grasp_training.py

@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 """
 This experiment was created using PsychoPy3 Experiment Builder (v2023.2.3),
-    on 十一月 21, 2023, at 13:15
+    on Tue Nov 21 18:12:16 2023
 If you publish work using this script the most relevant publication is:
 
     Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019) 
@@ -109,7 +109,8 @@ args = parse_args()
 receiver = NeuracleDataClient(n_channel=len(config_info['channel_labels']), 
                                samplerate=config_info['sample_rate'],
                                host=config_info['host'],
-                               port=config_info['port'])
+                               port=config_info['port']
+                               buffer_len=cofig_info['buffer_length'])
 
 # connect to trigger box
 trigger = TriggerNeuracle()
@@ -198,7 +199,7 @@ def setupData(expInfo, dataDir=None):
     thisExp = data.ExperimentHandler(
         name=expName, version='',
         extraInfo=expInfo, runtimeInfo=None,
-        originPath='C:\\Users\\asena\\Desktop\\kraken\\backend\\general_grasp_training.py',
+        originPath='/Users/dingkunliu/Projects/MI-BCI-Proj/kraken/backend/general_grasp_training.py',
         savePickle=True, saveWideText=True,
         dataFileName=dataDir + os.sep + filename, sortColumns='time'
     )

+ 1 - 0
backend/settings/config.py

@@ -14,6 +14,7 @@ class Settings:
         'port': 8712,
         'channel_count': 9,
         'sample_rate': 1000,
+        'buffer_length': 0.5,
         'channel_labels': [
             'CH001',
             'CH002',

+ 3 - 1
backend/training.py

@@ -13,9 +13,11 @@ import bci_core.feature_extractors as feature_extractors
 import bci_core.utils as bci_utils
 import bci_core.model as bci_model
 from dataloaders import neo
+from settings.config import settings
 
 
 logging.basicConfig(level=logging.INFO)
+config_info = settings.CONFIG_INFO
 
 
 def train_model(raw, event_id, trial_duration=1., model_type='baseline'):
@@ -120,7 +122,7 @@ if __name__ == '__main__':
     for f in sessions.keys():
         event_id[f] = neo.FINGERMODEL_IDS[f]
     
-    trial_duration = 0.5
+    trial_duration = config_info['buffer_length']
     # preprocess raw
     raw = neo.raw_preprocessing(data_dir, sessions, unify_label=True, upsampled_epoch_length=trial_duration, ori_epoch_length=5, mov_trial_ind=[2], rest_trial_ind=[1])
 

+ 5 - 2
backend/validation.py

@@ -13,9 +13,11 @@ from dataloaders import neo
 import bci_core.online as online
 import bci_core.utils as bci_utils
 import bci_core.viz as bci_viz
+from settings.config import settings
 
 
-logging.basicConfig(level=logging.DEBUG)
+logging.basicConfig(level=logging.INFO)
+config_info = settings.CONFIG_INFO
 
 
 class DataGenerator:
@@ -123,7 +125,8 @@ if __name__ == '__main__':
     metrics, fig_erds, fig_pred = validation(raw, 
                                              event_id, 
                                              model=model_path, 
-                                             state_change_threshold=0.75)
+                                             state_change_threshold=0.75,
+                                             step_length=config_info['buffer_length'])
     fig_erds.savefig(os.path.join(data_dir, 'erds.pdf'))
     fig_pred.savefig(os.path.join(data_dir, 'pred.pdf'))   
     logging.info(f'precision: {metrics[0]:.4f}, recall: {metrics[1]:.4f}, f_beta_score: {metrics[2]:.4f}, corr: {metrics[3]:.4f}')

Some files were not shown because too many files changed in this diff