def display_instructions(text="text instructions", text_end="Shoot to start the mission.", background=(13, 71, 161), display_trigger=False): n.newpage(background, auto_refresh=False) n.write("\n\n\n" + text, color="white", long_text=True) n.write(text_end, color="white", y=-9) if display_trigger is True: trigger.stop() n.refresh() n.response(allow=["DOWN", "RIGHT", "LEFT", "SPACE"])
def prime(side="RIGHT", duration=1000, conflict=False, testmode=False): display_background() display_ship() display_cue(side=side, conflict=conflict) n.refresh() time = datetime.datetime.now() response = np.nan RT = np.nan if testmode is False: while (datetime.datetime.now() - time).total_seconds() * 1000 < duration: current_response = n.response( time_max=duration - (datetime.datetime.now() - time).total_seconds() * 1000, get_RT=False) if (current_response != "Time_Max_Exceeded"): response = str(current_response) RT = datetime.datetime.now() if isinstance(RT, datetime.datetime): return ({ "Cue_Response": response, "Cue_Response_RT": -1 * (datetime.datetime.now() - RT).total_seconds() * 1000 }) else: return ({"Cue_Response": response, "Cue_Response_RT": RT})
def ITI(duration=1000, testmode=False, display_trigger=False): display_background() display_ship() if display_trigger is True: trigger.stop() n.refresh() time = datetime.datetime.now() response = np.nan RT = np.nan if testmode is False: while (datetime.datetime.now() - time).total_seconds() * 1000 < duration: current_response = n.response( time_max=duration - (datetime.datetime.now() - time).total_seconds() * 1000, get_RT=False) if (current_response != "Time_Max_Exceeded"): response = str(current_response) RT = datetime.datetime.now() if isinstance(RT, datetime.datetime): return ({ "Previous_Response": response, "Previous_RT": -1 * (datetime.datetime.now() - RT).total_seconds() * 1000 }) else: return ({"Previous_Response": response, "Previous_RT": RT})
def run_trials(cache, trials): prestim_interval = list(np.random.uniform(33.333333, 2000, len(trials)-1)) prestim_interval.insert(0, 2000) for order, trial in enumerate(trials): n.refresh() trial["Order"] = order+1 trial["Time_Trial_Onset"] = datetime.datetime.now() # Wait trial["Prestimulus_Interval"] = int(prestim_interval[order]) if testmode is False: trial["Prestimulus_Interval"] = n.time.wait(int(prestim_interval[order])) # Diplay stuff n.image(trial["Global_Color"] + "_" + trial["Global_Shape"], size=8, extension = ".png", cache = cache, path = "./Stimuli/", rotate=trial["Global_Angle"]) n.image(trial["Local_Color"] + "_" + trial["Local_Shape"], size=8, extension = ".png", cache = cache, path = "./Stimuli/", rotate=trial["Local_Angle"]) n.refresh() trial["Time_Stimulus_Onset"] = datetime.datetime.now() if testmode is False: answer, RT = n.response(time_max = 1750, allow=["DOWN", "RIGHT", "LEFT"]) if answer == "Time_Max_Exceeded": answer = "NA" else: answer = np.random.choice(["DOWN", "RIGHT", "LEFT", "NA"]) RT = np.random.uniform(100, 1750) trial["Response"] = answer trial["RT"] = RT n.newpage('grey', auto_refresh=False) return(trials)
n.instructions("Press LEFT when RED, DOWN when GREEN and RIGHT when BLUE.") n_trials = 10 for trial in range(n_trials): n.newpage("grey") # Neutral grey background n.write("+") # Fixation cross n.refresh() # Display it n.time.wait(250) # Wait 250 ms stim_color = np.random.choice(["raw_red", "raw_green", "raw_blue"]) # Choose a color stim = "XXXX" n.newpage("grey") # Neutral grey background n.write(stim, style="bold", color=stim_color, size=3) # Load the stimulus n.refresh() # Display it answer, RT = n.response() # Record response and response time # Append trial info to data["Stimulus"].append(stim) data["Stimulus_Color"].append(stim_color) data["Answer"].append(answer) data["RT"].append(RT) data["Condition"].append("Neutral") # Categorize the response if answer == "LEFT" and stim_color == "raw_red": data["Correct"].append(1) elif answer == "DOWN" and stim_color == "raw_green": data["Correct"].append(1) elif answer == "RIGHT" and stim_color == "raw_blue": data["Correct"].append(1)
"ISI":[], "RT":[], "Response":[]} for trial in range(5): # Iterate over the number of trials stimulus = random.choice(["green", "red"]) # Select a stimulus type ISI = random.randrange(start=500, stop=2000, step=500) # Select the inter-stimuli interval (ISI) n.newpage("grey") # Fill the screen n.write("+") # Fixation cross n.refresh() # Diplay it on screen n.time.wait(ISI) # Wait n.circle(size=2, fill_color=stimulus) # Display the stimulus (filled with the color selected above) n.refresh() # Diplay it on screen response, RT = n.response(time_max=1500) # Wait until 1.5s and collect the response and its time # Categorize the response if response == "SPACE" and stimulus == "green": response_type = "HIT" # Hit if response != "SPACE" and stimulus == "green": response_type = "MISS" # Miss if response == "SPACE" and stimulus == "red": response_type = "FA" # False Alarm if response != "SPACE" and stimulus == "red": response_type = "CR" # Correct Rejection # Store data by appending each item to its list data["Trial"].append(trial) data["Stimulus"].append(stimulus) data["ISI"].append(ISI)
"RT":[], "Response":[]} n_trials = 10 # Number of trials for trial in range(n_trials): # Iterate over the number of trials stimulus = random.choice(["green", "green", "green", "red"]) # Select a stimulus type ISI = random.randrange(start=250, stop=1250, step=250) # Select the inter-stimuli interval (ISI) n.newpage("grey") # Fill the screen n.write("+") # Fixation cross n.refresh() # Diplay it on screen n.time.wait(ISI) # Wait n.circle(size=2, fill_color=stimulus) # Display the stimulus (filled with the color selected above) n.refresh() # Diplay it on screen response, RT = n.response(time_max=1000) # Wait until 1 s and collect the response and its time # Categorize the response if response == "SPACE" and stimulus == "green": response_type = "HIT" # Hit if response != "SPACE" and stimulus == "green": response_type = "MISS" # Miss if response == "SPACE" and stimulus == "red": response_type = "FA" # False Alarm if response != "SPACE" and stimulus == "red": response_type = "CR" # Correct Rejection # Store data by appending each item to its list data["Trial"].append(trial) data["Stimulus"].append(stimulus) data["ISI"].append(ISI)
def response_inhibition(n_trials=200, min_SSRT=0, max_SSRT=300, frame=16.66667, staircase=False, testmode=False, display_trigger=False): def generate_data(n_trials, min_SSRT=0, max_SSRT=300, frame=16.66667, adaptive=False): data = { "Stimulus_Side": ["RIGHT"] * int(n_trials / 2) + ["LEFT"] * int(n_trials / 2), "ITI": list(generate_interval_frames(500, 1500, n_trials / 2)) * 2 } # SSRT ss = np.array( randomize_and_repeat([False, False, True], int(n_trials / 3)) + [False] * int(n_trials - int(n_trials / 3) * 3)) data["Stop_Signal"] = ss data["Stop_Signal_RT"] = np.array([np.nan] * int(n_trials)) if adaptive is False: ssrt = generate_interval_frames(min_SSRT, max_SSRT, int(sum(ss))) data["Stop_Signal_RT"][ss == True] = randomize_without_repetition( list(ssrt)) else: data["Stop_Signal_RT"][ss == True] = np.array( [-1] * len(data["Stop_Signal_RT"][ss == True])) data = pd.DataFrame.from_dict(data) data = data.sample(len(data)).reset_index(drop=True) data = data.to_dict(orient="index") return (data) # First if testmode is False: ITI(2000, testmode=testmode, display_trigger=display_trigger) display_enemy() if display_trigger is True: trigger.stop() n.refresh() n.time.wait(150) display_enemy(stop=True) if display_trigger is True: trigger.stop() n.refresh() n.response(allow=["RIGHT", "LEFT"], time_max=1500) n.time.wait(1500) # Instructions n.newpage((24, 4, 64), auto_refresh=False) n.write("Wait! What's that?!", color="white", y=5, size=1.2) if display_trigger is True: trigger.stop() n.refresh() n.time.wait(2000) display_instructions( """Bad news, rookie, it seems like the rebels have upgraded some of their ships!\n\nIf we do not manage to shoot as SOON as the ennemy appears, they'll have time to activate counter-measures that will return our bullets and damage our ship.""", text_end="Press SPACE to continue.", display_trigger=display_trigger) display_instructions( """Shoot the incoming ships as FAST as possible, before a RED CROSS appears.\n\nDo not shoot at the RED CROSS, or it will harm us too!""", display_trigger=display_trigger) # Generate data if staircase is True: staircase = nk.staircase(signal=generate_interval_frames( 0, max_SSRT, int(max_SSRT / frame)), treshold=0.5, burn=0) data = generate_data(int(n_trials / 2), min_SSRT, max_SSRT, frame) else: data = generate_data(int(n_trials), min_SSRT, max_SSRT, frame) # Run trials if staircase is False: for trial in range(0, n_trials): data[trial].update( ITI(data[trial]["ITI"], testmode=testmode, display_trigger=display_trigger)) data[trial].update( display_stimulus(side=data[trial]["Stimulus_Side"], stop=data[trial]["Stop_Signal_RT"], testmode=testmode, display_trigger=display_trigger)) data[trial]["Trial_Order"] = trial + 1 # # With staircase # else: # for trial in range(0, int(n_trials/2)): # data[trial].update(ITI(data[trial]["ITI"], testmode = testmode, display_trigger = display_trigger)) # data[trial].update(display_stimulus(side=data[trial]["Stimulus_Side"], stop=data[trial]["Stop_Signal_RT"], testmode = testmode, display_trigger = display_trigger)) # if staircase is True: # if data[trial]["Stop_Signal"] is True: # if data[trial]['RT'] >= data[trial]["Stop_Signal_RT"]: # if data[trial]["Response"] == "Time_Max_Exceeded": # staircase.add_response(response=0, value=data[trial]["Stop_Signal_RT"]) # else: # staircase.add_response(response=1, value=data[trial]["Stop_Signal_RT"]) # data[trial]["Trial_Order"] = trial + 1 # # data_staircase = generate_data(int(n_trials/2), min_SSRT, max_SSRT, adaptive=True) # for i in list(data_staircase.keys()): # Replace keys # data_staircase[i + int(n_trials/2)] = data_staircase.pop(i) # data.update(data_staircase) # for trial in range(int(n_trials/2), n_trials): # data[trial].update(ITI(data[trial]["ITI"], testmode = testmode, display_trigger = display_trigger)) # if data[trial]["Stop_Signal_RT"] == -1: # data[trial]["Stop_Signal_RT"] = staircase.predict_next_value() # data[trial].update(display_stimulus(side=data[trial]["Stimulus_Side"], stop=data[trial]["Stop_Signal_RT"], testmode = testmode, display_trigger = display_trigger)) # if data[trial]["Stop_Signal"] is True: # if data[trial]['RT'] >= data[trial]["Stop_Signal_RT"]: # if data[trial]["Response"] == "Time_Max_Exceeded": # staircase.add_response(response=0, value=data[trial]["Stop_Signal_RT"]) # else: # staircase.add_response(response=1, value=data[trial]["Stop_Signal_RT"]) # data[trial]["Trial_Order"] = trial + 1 data = pd.DataFrame.from_dict(data, orient="index") return (data)
def display_stimulus(side="RIGHT", always_right=False, allies=False, stop=np.nan, time_max=1500, testmode=False, display_trigger=False): if stop == 0: if allies is False: display_enemy(side=side, stop=True) elif allies is True: display_enemy(side=side, stop=True, allies=True) else: if allies is False: display_enemy(side=side) elif allies is True: display_enemy(side=side, allies=True) if display_trigger is True: trigger.start() n.refresh() time = datetime.datetime.now() if always_right is True: if testmode is False: response, RT = n.response(allow="DOWN", time_max=time_max) else: response, RT = "DOWN", np.random.normal(750, 250) display_fire(side=side) display_explosion(side=side) else: if np.isnan(stop) or stop == 0: if testmode is False: response, RT = n.response(allow=["LEFT", "RIGHT"], time_max=time_max) else: response, RT = np.random.choice(["LEFT", "RIGHT"]), np.random.normal( 750, 250) else: if testmode is True: response = np.random.choice(["LEFT", "RIGHT", np.nan]) if pd.isna(response): RT = np.nan else: RT = np.random.normal(750, 250) else: RT = 0 response, RT = n.response(allow=["LEFT", "RIGHT"], time_max=stop) if response not in ["LEFT", "RIGHT"]: display_enemy(side=side, stop=True) n.refresh() response, RT = n.response(allow=["LEFT", "RIGHT"], time_max=time_max - stop) RT += stop if response in ["LEFT", "RIGHT", "DOWN"]: display_fire(side=response) display_explosion(side=response) if testmode is False: if response in ["LEFT", "RIGHT", "DOWN"]: if display_trigger is True: trigger.stop() n.refresh() n.time.wait(200) return ({ "Response": response, "RT": RT, "Trial_Time_Onset": time, "Trial_Time_End": datetime.datetime.now() })