Skip to content

Commit

Permalink
cleaned up unused imports
Browse files Browse the repository at this point in the history
used ruff linter to clean code
  • Loading branch information
LMBooth committed Oct 22, 2023
1 parent f8fcdba commit 56865fc
Show file tree
Hide file tree
Showing 32 changed files with 104 additions and 123 deletions.
2 changes: 0 additions & 2 deletions Tests/test_PseudoDevice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import time
import pytest
from pybci import PyBCI
from pybci.Utils.PseudoDevice import PseudoDeviceController
# Test case using the fixture
#@pytest.mark.timeout(300) # Extended timeout to 5 minutes
def test_run_pseudo():
Expand Down
2 changes: 0 additions & 2 deletions Tests/test_Pytorch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from pybci import PyBCI
import time
import pytest

import torch
from torch.utils.data import DataLoader, TensorDataset
from torch import nn
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_Simple.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from pybci import PyBCI
import time, os
import pytest

import time
# Test case using the fixture
#@pytest.mark.timeout(300) # Extended timeout to 5 minutes
def test_run_bci():
Expand Down
1 change: 0 additions & 1 deletion Tests/test_Sklearn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pybci import PyBCI
import time
import pytest
from sklearn.neural_network import MLPClassifier
# Test case using the fixture
#@pytest.mark.timeout(300) # Extended timeout to 5 minutes
Expand Down
2 changes: 0 additions & 2 deletions Tests/test_Tensorflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from pybci import PyBCI
import time
import pytest

import tensorflow as tf# bring in tf for custom model creation

num_chs = 8 # 8 channels are created in the PseudoLSLGenerator
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.{build}
version: 1.2.{build}
image: Visual Studio 2019
environment:
matrix:
Expand Down
7 changes: 3 additions & 4 deletions pybci/CliTests/testPyTorch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import time, argparse
import time
import argparse
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings

import torch
from torch.utils.data import DataLoader, TensorDataset
from torch import nn


def main(create_pseudo_device=True, min_epochs_train=4, min_epochs_test=10, num_chs = 8, num_feats = 2, num_classes = 4):
if create_pseudo_device == True:
if create_pseudo_device:
num_chs = 8 # 8 channels are created in the PseudoLSLGenerator
num_feats = 2 # default is mean freq and rms to keep it simple
num_classes = 4 # number of different triggers (can include baseline) sent, defines if we use softmax of binary
Expand Down
4 changes: 2 additions & 2 deletions pybci/CliTests/testSklearn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings
import time, argparse
import time
import argparse
from sklearn.neural_network import MLPClassifier

def main(create_pseudo_device=True, min_epochs_train=4, min_epochs_test=10):
Expand Down
7 changes: 3 additions & 4 deletions pybci/CliTests/testTensorflow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import time, argparse
import time
import argparse
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings
import tensorflow as tf# bring in tf for custom model creation


def main(create_pseudo_device=True, min_epochs_train=4, min_epochs_test=10, num_chs = 8, num_feats = 2, num_classes = 4):
if create_pseudo_device == True:
if create_pseudo_device:
num_chs = 8 # 8 channels are created in the PseudoLSLGenerator
num_feats = 2 # default is mean freq and rms to keep it simple
num_classes = 4 # number of different triggers (can include baseline) sent, defines if we use softmax of binary
Expand Down
1 change: 0 additions & 1 deletion pybci/Examples/ArduinoHandGrasp/testArduinoHand.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import time
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings
from pybci.Utils.FeatureExtractor import GenericFeatureExtractor, GeneralFeatureChoices
from pybci.Utils.Logger import Logger
import numpy as np
from scipy.fft import fft
Expand Down
5 changes: 2 additions & 3 deletions pybci/Examples/ArduinoHandGrasp/testArduinoPytorch.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import time, torch
import time
import torch
from torch.utils.data import DataLoader, TensorDataset
from torch import nn
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings
from pybci.Utils.FeatureExtractor import GenericFeatureExtractor, GeneralFeatureChoices
from pybci.Utils.Logger import Logger
import numpy as np
from scipy.fft import fft
# We control the arduino via lsl python script which is responsible for COM connection
from pylsl import StreamInfo, StreamOutlet
# Set up the LSL stream info
Expand Down
4 changes: 2 additions & 2 deletions pybci/Examples/MultimodalPupilLabsEEG/testMultimodal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from pybci import PyBCI
import numpy as np
from pybci.Utils.FeatureExtractor import GenericFeatureExtractor
from scipy.fft import fft

class PupilGazeDecode():
# pupil-labs channels:
Expand All @@ -24,7 +24,7 @@ def ProcessFeatures(self, epochData, sr, epochNum): # This is the required funct
bothmean = np.mean([(epochData[1][i] + epochData[2][i]) / 2 for i in range(len(epochData[1]))]) # mean of both eyes in 3d
return np.nan_to_num([confidence, rightmean,leftmean,bothmean]) # expects 2d

from scipy.fft import fft

class EOGClassifier():
# used Fp1 and Fp2 from io:bio EEG device
def ProcessFeatures(self, epochData, sr, epochNum): # Every custom class requires a function with this name and structure to extract the featur data and epochData is always [Samples, Channels]
Expand Down
2 changes: 1 addition & 1 deletion pybci/Examples/testEpochTimingsConfig.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings, IndividualEpochSetting
from pybci.Configuration.EpochSettings import GlobalEpochSettings

gs = GlobalEpochSettings()
gs.tmax = 1 # grab 1 second after marker
Expand Down
2 changes: 0 additions & 2 deletions pybci/Examples/testPyTorch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import time
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings

import torch
from torch.utils.data import DataLoader, TensorDataset
from torch import nn
Expand Down
5 changes: 3 additions & 2 deletions pybci/Examples/testRaw.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import torch, time
import torch
import time
from torch.utils.data import DataLoader, TensorDataset
from torch import nn
from pybci import PyBCI
import numpy as np
from pybci.Utils.Logger import Logger

num_chs = 3 # 8 channels are created in the PseudoLSLGenerator, but we drop 5 to save compute (real-time CNN can be computationally heavy!)
sum_samps = 125 # sample rate is 250 in the PseudoLSLGwnerator
num_classes = 4 # number of different triggers (can include baseline) sent, defines if we use softmax of binary
Expand Down
1 change: 0 additions & 1 deletion pybci/Examples/testSklearn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings
import time
from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(max_iter = 1000, solver ="lbfgs")#solver=clf, alpha=alpha,hidden_layer_sizes=hid)
Expand Down
1 change: 0 additions & 1 deletion pybci/Examples/testTensorflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time
from pybci import PyBCI
from pybci.Configuration.EpochSettings import GlobalEpochSettings
import tensorflow as tf# bring in tf for custom model creation

num_chs = 8 # 8 channels are created in the PseudoLSLGenerator
Expand Down
7 changes: 3 additions & 4 deletions pybci/ThreadClasses/AsyncDataReceiverThread.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import threading, time
import threading
from collections import deque
import itertools
from bisect import bisect_left
def slice_fifo_by_time(fifo, start_time, end_time):
"""Find the slice of fifo between start_time and end_time using binary search."""
Expand Down Expand Up @@ -48,7 +47,7 @@ def run(self):
dataFIFOs = [deque(maxlen=fifoLength) for ch in range(chCount - len(self.streamChsDropDict))]
while not self.closeEvent.is_set():
sample, timestamp = self.dataStreamInlet.pull_sample(timeout = 1)
if sample != None:
if sample is not None:
for index in sorted(self.streamChsDropDict, reverse=True):
del sample[index] # remove the desired channels from the sample
for i,fifo in enumerate(dataFIFOs):
Expand Down Expand Up @@ -114,7 +113,7 @@ def run(self):
# add levels of debug?

def ReceiveMarker(self, marker, timestamp): # timestamp will be used for non sample rate specific devices (pupil-labs gazedata)
if self.startCounting == False: # only one marker at a time allow, other in windowed timeframe ignored
if self.startCounting is False: # only one marker at a time allow, other in windowed timeframe ignored
self.currentMarker = marker
self.markerTimestamp = timestamp
if len(self.customEpochSettings.keys())>0: # custom marker received
Expand Down
4 changes: 3 additions & 1 deletion pybci/ThreadClasses/ClassifierThread.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from ..Utils.Classifier import Classifier
from ..Utils.Logger import Logger
import queue,threading, time
import queue
import threading
import time
import numpy as np

class ClassifierThread(threading.Thread):
Expand Down
4 changes: 2 additions & 2 deletions pybci/ThreadClasses/DataReceiverThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def run(self):
dataFIFOs = [deque(maxlen=fifoLength) for ch in range(chCount - len(self.streamChsDropDict))]
while not self.closeEvent.is_set():
sample, timestamp = self.dataStreamInlet.pull_sample(timeout = 1)
if sample != None:
if sample is not None:
for index in sorted(self.streamChsDropDict, reverse=True):
del sample[index] # remove the desired channels from the sample
for i,fifo in enumerate(dataFIFOs):
Expand Down Expand Up @@ -88,7 +88,7 @@ def run(self):
# add levels of debug

def ReceiveMarker(self, marker, timestamp): # timestamp will be used for non sample rate specific devices (pupil-labs gazedata)
if self.startCounting == False: # only one marker at a time allow, other in windowed timeframe ignored
if self.startCounting is False: # only one marker at a time allow, other in windowed timeframe ignored
self.currentMarker = marker
if len(self.customEpochSettings.keys())>0: # custom marker received
if marker in self.customEpochSettings.keys():
Expand Down
13 changes: 7 additions & 6 deletions pybci/ThreadClasses/FeatureProcessorThread.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import threading, queue, time
import threading
import queue
import time
from ..Utils.FeatureExtractor import GenericFeatureExtractor
from ..Utils.Logger import Logger
from ..Configuration.EpochSettings import GlobalEpochSettings
import copy

class FeatureProcessorThread(threading.Thread):
tempDeviceEpochLogger = []
Expand Down Expand Up @@ -43,9 +44,9 @@ def run(self):
features = self.featureExtractor.ProcessFeatures(dataFIFOs, sr, target) # allows custom epoch class to be passed
if (self.logger.level == Logger.TIMING):
end = time.time()
self.logger.log(Logger.TIMING, f" Feature Extraction time {end - start}")
self.logger.log(Logger.TIMING, " Feature Extraction time "+str(end - start))
if (end-start) >self.globalWindowSettings.windowLength:
self.logger.log(Logger.WARNING, f" Feature Extraction time > globalEpochSetting.windowLength, will create lag in classification output. Recommended to reduce channels, smapling rate, and features or reduce feature computational complexity.")
self.logger.log(Logger.WARNING, " Feature Extraction time > globalEpochSetting.windowLength, will create lag in classification output. Recommended to reduce channels, smapling rate, and features or reduce feature computational complexity.")
self.featureQueueTrain.put( [features, devCount, target, dict(self.epochCounts)] )
except queue.Empty:
pass
Expand All @@ -56,9 +57,9 @@ def run(self):
features = self.featureExtractor.ProcessFeatures(dataFIFOs, sr, None)
if (self.logger.level == Logger.TIMING):
end = time.time()
self.logger.log(Logger.TIMING, f" Feature Extraction time {end - start}")
self.logger.log(Logger.TIMING, " Feature Extraction time "+str(end - start))
if (end-start) >self.globalWindowSettings.windowLength:
self.logger.log(Logger.WARNING, f" Feature Extraction time > globalEpochSetting.windowLength, will create lag in classification output. Recommended to reduce channels, smapling rate, and features or reduce feature computational complexity.")
self.logger.log(Logger.WARNING, " Feature Extraction time > globalEpochSetting.windowLength, will create lag in classification output. Recommended to reduce channels, smapling rate, and features or reduce feature computational complexity.")
self.featureQueueTest.put([features, devCount])
except queue.Empty:
pass
2 changes: 1 addition & 1 deletion pybci/ThreadClasses/MarkerThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run(self):
while not self.closeEvent.is_set():
marker, timestamp = self.markerStreamInlet.pull_sample(timeout = 10)
if self.trainTestEvent.is_set(): # We're training!
if marker != None:
if marker is not None:
marker = marker[0]
for thread in self.dataThreads:
thread.ReceiveMarker(marker, timestamp)
Expand Down
7 changes: 2 additions & 5 deletions pybci/ThreadClasses/OptimisedDataReceiverThread.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import threading, time
from collections import deque
import itertools
import threading
import numpy as np
from bisect import bisect_left

class OptimisedDataReceiverThread(threading.Thread):
"""Responsible for receiving data from accepted LSL outlet, slices samples based on tmin+tmax basis,
Expand Down Expand Up @@ -125,7 +122,7 @@ def run(self):
# add levels of debug?

def ReceiveMarker(self, marker, timestamp): # timestamp will be used for non sample rate specific devices (pupil-labs gazedata)
if self.markerReceived == False: # only one marker at a time allow, other in windowed timeframe ignored
if self.markerReceived is False: # only one marker at a time allow, other in windowed timeframe ignored
self.currentMarker = marker
self.markerTimestamp = timestamp
if len(self.customEpochSettings.keys())>0: # custom marker received
Expand Down
13 changes: 6 additions & 7 deletions pybci/Utils/Classifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn import svm
import tensorflow
import torch

from sklearn.model_selection import train_test_split
Expand All @@ -16,20 +15,20 @@ class Classifier():

def __init__(self, clf = None, model = None, torchModel = None):
super().__init__()
if clf != None:
if clf is not None:
self.clf = clf
elif model != None:
elif model is not None:
self.model = model
elif torchModel != None:
elif torchModel is not None:
self.torchModel = torchModel
self.CheckClassifierLibrary()

def CheckClassifierLibrary(self):
if self.model != None: # maybe requires actual check for tensorflow model
if self.model is not None: # maybe requires actual check for tensorflow model
self.classifierLibrary = "tensor"
elif self.torchModel != None: # maybe requires actual check for sklearn clf
elif self.torchModel is not None: # maybe requires actual check for sklearn clf
self.classifierLibrary = "pyTorch"
elif self.clf != None: # maybe requires actual check for sklearn clf
elif self.clf is not None: # maybe requires actual check for sklearn clf
self.classifierLibrary = "sklearn"

def TrainModel(self, features, targets):
Expand Down
Loading

0 comments on commit 56865fc

Please sign in to comment.