STIM files in SightLab allow you to easily manipulate an independent variable in a study. For example, you can adjust the size or position of an object per trial, and these conditions can be randomized or iterated through sequentially. Other potential conditions might be things such as a category of video to play, a specific environment that a participant will be in, etc.
SightLab has a built-in StimReader class which reads the contents of a STIM file in .csv format. To create a STIM file, you first define the variables you want to adjust and then declare what the value of that variable will be.
For example, to manipulate the size and position of an object (e.g., a basketball), you would do the following:
Create a STIM file with entries for 'object size' and 'object position (note that since python is sensitive to spaces, make sure to not have spaces between any values).
For certain situations you might want to use a config file to define the possible values for 'object size' and 'object position'. This is usually done in a config file, which is placed alongside your main script, but you can also add this directly in the code. The config file can also give the path to the STIM file. This may not be needed for all cases however.
Sample Optional Config File to define parameters for STIM file
3. In your main experiment script, use the StimReader to load the STIM file:
fromsightlab_utilsimportstim_reader# Initialize StimReader with the STIM fileSR=stim_reader.StimReader(STIM_FILE_LOCATION)SR.fillStimFileEntryList()
4. Define the list of entries
entries=SR.getStimFileEntryList()
In your experiment function, iterate or randomize through the conditions for each trial. Can also use this code to have number of trials automatically match number of lines in the STIM file:
entries=SR.getStimFileEntryList()sightlab.setTrialCount(len(entries))#If wanting to randomize the conditionsimportrandomrandom.shuffle(entries)# Iterate over the entriesforiinrange(len(entries)):yieldviztask.waitKeyDown(' ')yieldsightlab.startTrial()currentEntry=entries[i]
For each trial, adjust the object's properties based on the current entry:
By using STIM files and the StimReader, you can create more flexible and easily configurable experiments, adjusting different independent variables as needed.
Add STIM file defined conditions to the experiment summary and/or data files
if'object size'incurrentEntry:ballSizeString=currentEntry['object size']ballSize=target_object_size[ballSizeString]basketball.setScale(ballSize)CONDITION=ballSizeString# Set global CONDITIONif'object position'incurrentEntry:positionString=currentEntry['object position']positionTuple=target_object_position[positionString]basketball.setPosition(positionTuple)sightlab.setExperimentSummaryData('Object Size',CONDITION)sightlab.setExperimentSummaryData('Position',positionTuple)
STIM Files in Session Replay
Here's a quick sample of using the STIM file in the Session Replay using a few conditions
#First import a STIM file config file if using onefromSTIM_File_Configimport*#Import Stim File Readerfromsightlab_utilsimportstim_reader#Add these lines to locate your actual STIM file (i.e. what condition is being set for each trial)SR=stim_reader.StimReader(STIM_FILE_PATH)SR.fillStimFileEntryList()#Load variables stored in experiment summary file and change the variable per trialdeffind_experiment_summary():"""Locate the experiment_summary.csv file in the replay data folder."""files=os.listdir(replay.dataFolder)forfileinfiles:iffile.endswith("experiment_summary.csv"):returnos.path.join(replay.dataFolder,file)raiseFileNotFoundError("experiment_summary.csv not found in data folder.")# Load the experiment_summary.csv file. NOTE CHANGE "OBJECT SIZE" to your variabledefload_experiment_summary():summary_file=find_experiment_summary()entries=[]withopen(summary_file,'r')ascsvfile:reader=csv.DictReader(csvfile)forrowinreader:# Ignore empty rows or rows missing 'Object Size'ifrowandrow.get('Object Size'):entries.append(row)returnentriesentries=load_experiment_summary()defontrialchanged():#Get a handle to whatever object is being modifiedbasketball=replay.sceneObjects[GAZE_OBJECTS]["basketball"]trial_number=int(replay.currTrial)iftrial_numberand1<=trial_number<=len(entries):currentEntry=entries[trial_number-1]if'Object Size'incurrentEntry:# Set object sizeballSizeString=currentEntry['Object Size']ballSize=target_object_size.get(ballSizeString)ifballSizeandbasketball:basketball.setScale(ballSize)viz.callback(TRIAL_LOADED_EVENT,ontrialchanged)ontrialchanged()
STIM File Loader
If wanting to be able to save multiple STIM files you can also add a STIM file loader to choose which one to use
folder=STIM_FILE_FOLDERlist_of_files=os.listdir(folder)current_stim_file=(f'{folder}/{list_of_files[vizinput.choose("select a stim file",list_of_files)]}')SR=stim_reader.StimReader(current_stim_file)SR.fillStimFileEntryList()