From 7534ee2638c923b8e4dec69a39118a1f83f8273a Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 15 Sep 2024 13:45:23 +0700 Subject: [PATCH] Create neurointerface_biometric_authentication.py --- ...neurointerface_biometric_authentication.py | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 advanced_feature/neurointerface_biometric_authentication/neurointerface_biometric_authentication.py diff --git a/advanced_feature/neurointerface_biometric_authentication/neurointerface_biometric_authentication.py b/advanced_feature/neurointerface_biometric_authentication/neurointerface_biometric_authentication.py new file mode 100644 index 000000000..e13dc0cc4 --- /dev/null +++ b/advanced_feature/neurointerface_biometric_authentication/neurointerface_biometric_authentication.py @@ -0,0 +1,99 @@ +import numpy as np +import pandas as pd +from sklearn.preprocessing import StandardScaler +from sklearn.model_selection import train_test_split +from sklearn.metrics import accuracy_score, classification_report, confusion_matrix +import torch +import torch.nn as nn +import torch.optim as optim +from torch.utils.data import Dataset, DataLoader +from brainflow import BoardShim, BoardIds, BrainFlowInputParams, DataFilter +from brainflow.data_filter import FilterTypes, DetrendOperations + +class NeuroInterfaceDataset(Dataset): + def __init__(self, eeg_data, labels): + self.eeg_data = eeg_data + self.labels = labels + + def __len__(self): + return len(self.eeg_data) + + def __getitem__(self, idx): + eeg_data = self.eeg_data.iloc[idx, :] + label = self.labels.iloc[idx] + return { + 'eeg_data': torch.tensor(eeg_data.values, dtype=torch.float), + 'label': torch.tensor(label, dtype=torch.long) + } + +class NeuroInterfaceModel(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim): + super(NeuroInterfaceModel, self).__init__() + self.fc1 = nn.Linear(input_dim, hidden_dim) + self.fc2 = nn.Linear(hidden_dim, hidden_dim) + self.fc3 = nn.Linear(hidden_dim, output_dim) + + def forward(self, x): + x = torch.relu(self.fc1(x)) + x = torch.relu(self.fc2(x)) + x = self.fc3(x) + return x + +def generate_eeg_data(board_id, sampling_rate, duration): + # Initialize BrainFlow board + board = BoardShim(board_id, BrainFlowInputParams()) + board.prepare_session() + # Start streaming EEG data + board.start_stream() + # Read EEG data for specified duration + eeg_data = board.get_board_data(sampling_rate * duration) + # Stop streaming and release board + board.stop_stream() + board.release_session() + return pd.DataFrame(eeg_data) + +def train_neurointerface_model(eeg_data, labels, epochs=100, batch_size=32): + # Create dataset and data loader + dataset = NeuroInterfaceDataset(eeg_data, labels) + data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True) + + # Initialize NeuroInterface model + model = NeuroInterfaceModel(input_dim=128, hidden_dim=64, output_dim=2) + + # Define loss function and optimizer + criterion = nn.CrossEntropyLoss() + optimizer = optim.Adam(model.parameters(), lr=0.001) + + # Train NeuroInterface model + for epoch in range(epochs): + for batch in data_loader: + eeg_data = batch['eeg_data'].to(device) + label = batch['label'].to(device) + optimizer.zero_grad() + output = model(eeg_data) + loss = criterion(output, label) + loss.backward() + optimizer.step() + print(f'Epoch {epoch+1}, Loss: {loss.item()}') + + return model + +def authenticate_user(eeg_data, model): + # Authenticate user using NeuroInterface model + eeg_data = torch.tensor(eeg_data.values, dtype=torch.float) + output = model(eeg_data) + _, predicted = torch.max(output, 1) + return predicted.item() + +# Generate EEG data for user authentication +eeg_data = generate_eeg_data(BoardIds.SYNTHETIC_BOARD, 128, 10) + +# Generate labels for user authentication +labels = pd.DataFrame([{'label': 1}]) + +# Train NeuroInterface model +model = train_neurointerface_model(eeg_data, labels) + +# Authenticate user +authentication_result = authenticate_user(eeg_data, model) +print(f'Authentication result: {authentication_result}')