Skip to content

Commit

Permalink
reverted unnecessary changes and made p300 experiment display correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
pellet committed Aug 22, 2023
1 parent af77f23 commit ef1e9b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
39 changes: 26 additions & 13 deletions eegnb/experiments/Experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

from eegnb import generate_save_fn


class BaseExperiment:

def __init__(self, exp_name, duration, eeg, save_fn, n_trials: int, iti: float, soa: float, jitter: float, use_vr=False):
def __init__(self, exp_name, duration, eeg, save_fn, n_trials: int, iti: float, soa: float, jitter: float,
use_vr=False):
""" Initializer for the Base Experiment Class
Args:
Expand Down Expand Up @@ -59,14 +61,14 @@ def load_stimulus(self):
raise NotImplementedError

@abstractmethod
def present_stimulus(self, current_trial: int):
def present_stimulus(self, idx : int):
"""
Method that presents the stimulus for the specific experiment, overwritten by the specific experiment
Displays the stimulus on the screen
Pushes EEG Sample if EEG is enabled
Throws error if not overwritten in the specific experiment
current_trial: Trial index for the current trial
idx : Trial index for the current trial
"""
raise NotImplementedError

Expand All @@ -81,7 +83,9 @@ def setup(self, instructions=True):
self.trials = DataFrame(dict(parameter=self.parameter, timestamp=np.zeros(self.n_trials)))

# Setting up Graphics
self.window = visual.Rift(monoscopic=True, headLocked=True) if self.use_vr else visual.Window([1600, 900], monitor="testMonitor", units="deg", fullscr=True)
self.window = (
visual.Rift(monoscopic=True, headLocked=True) if self.use_vr
else visual.Window([1600, 900], monitor="testMonitor", units="deg", fullscr=True))

# Loading the stimulus from the specific experiment, throws an error if not overwritten in the specific experiment
self.stim = self.load_stimulus()
Expand Down Expand Up @@ -120,11 +124,15 @@ def show_instructions(self):
while len(event.getKeys(keyList="space")) == 0:
# Displaying the instructions on the screen
text = visual.TextStim(win=self.window, text=self.instruction_text, color=[-1, -1, -1])
self.__draw(lambda: text.draw())
self.__draw(lambda: self.__draw_instructions(text))

# Enabling the cursor again
self.window.mouseVisible = True

def __draw_instructions(self, text):
text.draw()
self.window.flip()

def __draw(self, present_stimulus: Callable):
"""
Set the current eye position and projection for all given stimulus,
Expand All @@ -134,10 +142,8 @@ def __draw(self, present_stimulus: Callable):
tracking_state = self.window.getTrackingState()
self.window.calcEyePoses(tracking_state.headPose.thePose)
self.window.setDefaultView()
if present_stimulus:
present_stimulus()
self.window.flip()

present_stimulus()

def run(self, instructions=True):
""" Do the present operation for a bunch of experiments """

Expand All @@ -159,6 +165,9 @@ def iti_with_jitter():
start = time()
current_trial = current_trial_end = -1
current_trial_begin = None

# Current trial being rendered
rendering_trial = -1
while len(event.getKeys()) == 0 and (time() - start) < self.record_duration:

current_experiment_seconds = time() - start
Expand All @@ -170,11 +179,15 @@ def iti_with_jitter():

# Do not present stimulus after trial has ended(stimulus on arrival interval).
elif current_trial_begin < current_experiment_seconds:
# Some form of presenting the stimulus - sometimes order changed in lower files like ssvep
# Stimulus presentation overwritten by specific experiment
self.__draw(lambda: self.present_stimulus(current_trial))

# if current trial number changed get new choice of image.
if rendering_trial < current_trial:
# Some form of presenting the stimulus - sometimes order changed in lower files like ssvep
# Stimulus presentation overwritten by specific experiment
self.__draw(lambda: self.present_stimulus(current_trial, current_trial))
rendering_trial = current_trial
else:
self.__draw(lambda: None)
self.__draw(lambda: self.window.flip())

# Clearing the screen for the next trial
event.clearEvents()
Expand Down
50 changes: 22 additions & 28 deletions eegnb/experiments/visual_n170/n170.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@

class VisualN170(Experiment.BaseExperiment):

def __init__(self, duration=120, eeg: Optional[EEG]=None, save_fn=None,
n_trials = 2010, iti = 0.4, soa = 0.3, jitter = 0.2, use_vr=False):
def __init__(self, duration=120, eeg: Optional[EEG] = None, save_fn=None,
n_trials=2010, iti=0.4, soa=0.3, jitter=0.2, use_vr=False):

# Current image being rendered
self.image = None
# Current trial being rendered
self.rendering_trial = -1
# Set experiment name
# Set experiment name
exp_name = "Visual N170"
# Calling the super class constructor to initialize the experiment variables
super(VisualN170, self).__init__(exp_name, duration, eeg, save_fn, n_trials, iti, soa, jitter, use_vr)
Expand All @@ -42,25 +38,23 @@ def load_stimulus(self):

# Return the list of images as a stimulus object
return [self.houses, self.faces]

def present_stimulus(self, current_trial: int):

def present_stimulus(self, idx : int, trial):
# Get the label of the trial
label = self.trials["parameter"].iloc[current_trial]

# if current trial number changed get new choice of image.
if self.rendering_trial < current_trial:
# Get the image to be presented
self.image = choice(self.faces if label == 1 else self.houses)
self.rendering_trial = current_trial
# Pushing the sample to the EEG, and storing a timestamp of stimulus being displayed
if self.eeg:
timestamp = time()
if self.eeg.backend == "muselsl":
marker = [self.markernames[label]]
else:
marker = self.markernames[label]
self.eeg.push_sample(marker=marker, timestamp=timestamp)

# Redraw/draw the image based on user perspective.
self.image.draw()
label = self.trials["parameter"].iloc[idx]
# Get the image to be presented
image = choice(self.faces if label == 1 else self.houses)
# Draw the image
image.draw()

# Pushing the sample to the EEG
if self.eeg:
timestamp = time()
if self.eeg.backend == "muselsl":
marker = [self.markernames[label]]
else:
marker = self.markernames[label]
self.eeg.push_sample(marker=marker, timestamp=timestamp)

self.window.flip()

0 comments on commit ef1e9b7

Please sign in to comment.