Skip to content

Commit

Permalink
task code and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
evgluk committed Sep 17, 2021
1 parent 0dc06e7 commit b299e3d
Show file tree
Hide file tree
Showing 24 changed files with 5,006 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/task/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[email *[email protected]* for further explanations]
## Necessary setups before delay discounting or risk experiment

**Computer setup:**

1. Python 2.7 needs to be installed in your computer
2. Download and install PsychoPy 1.83.04
3. Download and install any MySQL user interface
4. git clone repos/ folder (with helpers/ folder and code/ folder in it)
'helpers' is custom module needed to access dtb
5. Setup working path
- In repos/code/functions.py, find setpath() function
- sys.path.append('directory where you git cloned repos folder')
- sys.path.append('directory where your /Python/2.7/site-packages is')
6. Setup MySQL database
6.1. Setup all tables(and column names) needed in our code
-sessions: sessid/subjid/sessiondate/starttime/treatment/hostip/settingsid/startts
-sessions_end: sessid/subjid/sessiondate/endtime/treatment/hostip/num_trials/total_profit/settingsid/sessionpay/trialpay/payrew/paydelay/endts/moneyscarcity/timescarcity/hurry/questionnaire/sessionpay_w/trialpay_w/payrew_w/paydelay_w
-settings: settingid/expgroupid/data/description
-subjinfo: subjid/netid/firstname/fullname
-trials: trialid/sessid/trialtime/trialnum/trialdata/stage/rewmag/delay/probability/choice/points/smag/sdelay/short_delay/long_delay
6.2. Set database user name
- In repos/code/functions, find dbconnect() function
- Input your database user name in sql.select_user(dbc,'your user name')
6.3. Setup a subjects pool in your database
- In 'subjinfo' table, input information of all subjects
- There needs to be an ID(netid) for each subject to input at the beginning of each program that will lead a unique subjid saved in the 'subjinfo' table
6.4. Create two necessary settings
- You can input whatever fits your need in the n/a's
- In 'settings' table, create two rows of necessary settings for the experiment:
| settingsid | expgroupid | data | description |
|-----------:|------------|----------------------------------------------------------------------------------------------------------------------------------|-------------|
| n/a | n/a | {"delay": [ 3 , 7, 14, 30, 64], "refresherNum": 10, "blockTrial": 2, "passThreshold": 2, "rewdelpair": 10, "ddiscounter": 1.2} | delay |
| n/a | n/a | {"probability": [ 0.25, 0.5, 0.75], "rewmag": [ 2, 4, 6, 6, 6, 12, 12, 12, 18, 18, 18, 50], "surebet": 4, "refresherNum": 10, "blockTrial": 2, "passThreshold": 2, "rewdelpair": 10, "ddiscounter": 1.2} | risk |

**Subject data saving preparation**

1. Once the subject gave his consent, add his data to subjinfo table.
2. He will be given a subjid (manual entry in the dtb).
3. Participant will be instructed to insert his netID (linked to subjid).
4. If netID is not in the database, PsychoPy will display an error message.

**Run the following code:**

1. For [odd subjid] delay d (DS: days and seconds) -> run verbal_combined_odd_Chn.py
2. For [even subjid] delay d (DS: days and seconds) -> run verbal_combined_even_Chn.py
3. For [odd subjid] delay d (DW: days and weeks) -> run verbal_dw_odd_Chn.py
4. For [even subjid] delay d (DW: days and weeks) -> run verbal_dw_even_Chn.py
5. For risk task -> run risk_combined_Chn.py

**Auxiliary files catalog**

1. functions.py: file which contains all functions needed for other scripts
2. SQL_call.py: all database related commands(read and write) needed for other scripts
3. Short_Verbal_Chn.py: short-delay session (imported by verbal_combined_odd/even_Chn.py)
4. Long_Verbal_Chn.py: long-delay session (imported by verbal_combined_odd/even_Chn.py)
5. Days_Verbal_Chn.py: day-delay experiment (imported by verbal_dw_odd/even_Chn.py)
6. Weeks_Verbal_Chn.py: week-delay experiment (imported by verbal_dw_odd/even_CHn.py)
7. Risk_Chn.py: risk experiment (imported by risk_combined_Chn.py)
5. doodle.py: defines class Doodle which creates circle/text/sound objects and modifies their properties (important file that decreases memory use)
6. newPoints.py: defines class NewPoins which creates coin objects and modifies their properties (imported by all files that need to draw coins)
7. setup.py: defines the window that displays stimuli and other setups (imported by all files for experiments)
8. variables.py: defines class Variables which defines all the variables needed (imported by all files for experiments)
9. coin_echo5.wav: the sound stimuli when getting a coin in the short delay task

75 changes: 75 additions & 0 deletions src/task/delay/Days_Verbal_Chn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# jfkdjakf
# Verbal tasks with longer delays (in days)
#--------------import stuff----------------------------------------
import os, datetime, copy, random
import newPoints,setup,variables,doodle
from psychopy import event
import functions as fc
import SQL_call as sql
#------------make objects----------------------
# these objects won't be made if the file is called from another file
myPoints = newPoints.NewPoints()
var = variables.Variables()
doo = doodle.Doodle()
dbc = fc.dbconnect()
mouse = event.Mouse(setup.mywin)
#--------------------------Main Code--------------------------------
def DaysVerbal_Chn(var,doo,myPoints,dbc,mouse,getName=True,sub_id=None,pre_points=0,gettrialn=0,pre_trials=0):
var.trialCounter = pre_trials
var.rewardGot = gettrialn
var.points = pre_points # if it's the second long verbal session in the experiment, points are accumulated from the first long verbal session
var.longVerbal = True # specify long verbal for specific functions
fc.setpath() # set up directory
fN = os.path.basename(__file__) # get expName of this file
expName, extN = os.path.splitext(fN) # get expName and extN
stageName = expName[:-11] # get stageName
expDate = datetime.datetime.now().strftime("%Y-%m-%dT%H_%M_%S")# get expDate
sessid,p_num,p_net,setiddtb,host_ip, var,dbc = fc.exp_setup(var,doo,dbc,getName,sub_id,expName,expDate) # database setup and data start recording
var.pay = fc.paymentSelection(var.verbalTotalTrial_dw)+gettrialn # select one trial(number) to actually pay the subject
setupList = fc.verbalSetup(var.verbalTotalTrial_dw,var.mag,var.shortmag) # setup the reward of blocks for the whole experiment
shuffledSetupList = copy.copy(setupList)# shuffle the reward list
random.shuffle(shuffledSetupList)
fc.stage_instruction_Chn(stageName,var,doo) # show subjects stage name in Chinese (day delays)
for i in range(len(shuffledSetupList)): # apply a reward mag for each block
var.blockRewMag = shuffledSetupList[i][0] # assign reward mag for this block
var.blockName = i+1 # assign blockName
var.rewmag = var.blockRewMag # assign reward mag
fc.block_instruction_Chn(var,doo) # show block instruction to the subject
while var.blockTrialCounter < shuffledSetupList[i][1]: # run pre-determined number of trials in each block
#if not var.sameInitPos: # get a new setup if no violation made in the last trial
var = fc.new_trial_setup(var)
var,doo,myPoints = fc.draw_init_Chn(var,doo,myPoints) # start the first state
while var.state != 'none': # go through all connected states in one trial
#mouse.clickReset()
var,doo,myPoints = fc.funcDic_Chn[var.state](var,doo,myPoints)
if fc.again_or_next(var) == 'again': # if any violations made
var.trialCounter+=1 # plus 1 in trialCounter
var.sameInitPos = True # next trial will use the same init position
var.reward_clicks=[] # reset reward_clicks(records the clicks during reward) list
var.delay_clicks=[] # reset delay_clicks(records the clicks during delay waiting time) list
again = True
else: # if no violation made
var.rewardGot += 1 # plus 1 in rewardGot
var.trialCounter+=1 # plus 1 in trialCounter (total trial num)
var.blockTrialCounter += 1 # plus 1 in blockTrialCounter (trial num in current block)
var.pairNum+=1 # proceed to next pair
var.reward_clicks=[]# reset reward_clicks record list
var.delay_clicks=[] # reset delay_clicks(records the clicks during delay waiting time) list
again = False
if var.rewardGot == var.pay and again == False: # assign pay_delay and pay_num if the current trial is the one picked to be actually paid
if var.choice!='b':
var.pay_delay = var.delaymag
else:
var.pay_delay = var.shortdelay
var.pay_num = var.rewmag
fc.dataRecord_longVerbal(var,dbc,sessid) # record data for this trial
fc.resetVar(var,again)
var.blockTrialCounter = 0 # reset blockTrialCounter
var.dataFile.close() # close the data file
var.longVerbal = False # reset longVerbal boolean to be False
endt = datetime.datetime.now() # record end time
sql.w_after_Verbal(dbc,var,sessid, p_num, expDate, endt, expName, host_ip, setiddtb) # save data of this session in database
return p_net, var.pay, var.pay_delay, var.pay_num, var.trialCounter,var.points,

if __name__ == "__main__":
DaysVerbal_Chn(var,doo,myPoints,dbc,mouse) # run main function
75 changes: 75 additions & 0 deletions src/task/delay/Long_Verbal_Chn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# jfkdjakf
# Verbal tasks with longer delays (in days) Chinese Version
#--------------import stuff----------------------------------------
import os, datetime, copy, random
import newPoints,setup,variables,doodle
from psychopy import event
import functions as fc
import SQL_call as sql
#------------make objects----------------------
# these objects won't be made if the file is called from another file
myPoints = newPoints.NewPoints()
var = variables.Variables()
doo = doodle.Doodle()
dbc = fc.dbconnect()
mouse = event.Mouse(setup.mywin)
#--------------------------Main Code--------------------------------
def LongVerbal(var,doo,myPoints,dbc,mouse,getName=True,sub_id=None,pre_points=0,gettrialn=0,pre_trials=0):
var.trialCounter = pre_trials
var.rewardGot = gettrialn
var.points = pre_points # if it's the second long verbal session in the experiment, points are accumulated from the first long verbal session
var.longVerbal = True # specify long verbal for specific functions
fc.setpath() # set up directory
fN = os.path.basename(__file__) # get expName of this file
expName, extN = os.path.splitext(fN) # get expName and extN
stageName = expName[:-11] # get stageName
expDate = datetime.datetime.now().strftime("%Y-%m-%dT%H_%M_%S")# get expDate
sessid,p_num,p_net,setiddtb,host_ip, var,dbc = fc.exp_setup(var,doo,dbc,getName,sub_id,expName,expDate) # database setup and data start recording
var.pay = fc.paymentSelection(var.verbalTotalTrial_Chn)+gettrialn # select one trial(number) to actually pay the subject
setupList = fc.verbalSetup(var.verbalTotalTrial_Chn,var.mag,var.shortmag) # setup the reward of blocks for the whole experiment
shuffledSetupList = copy.copy(setupList)# shuffle the reward list
random.shuffle(shuffledSetupList)
fc.stage_instruction_Chn(stageName,var,doo) # show subjects stage name (long delay)
for i in range(len(shuffledSetupList)): # apply a reward mag for each block
var.blockRewMag = shuffledSetupList[i][0] # assign reward mag for this block
var.blockName = i+1 # assign blockName
var.rewmag = var.blockRewMag # assign reward mag
fc.block_instruction_Chn(var,doo) # show block instruction to the subject
while var.blockTrialCounter < shuffledSetupList[i][1]: # run pre-determined number of trials in each block
#if not var.sameInitPos: # get a new setup if no violation made in the last trial
var = fc.new_trial_setup(var)
var,doo,myPoints = fc.draw_init_Chn(var,doo,myPoints) # start the first state
while var.state != 'none': # go through all connected states in one trial
#mouse.clickReset()
var,doo,myPoints = fc.funcDic_Chn[var.state](var,doo,myPoints)
if fc.again_or_next(var) == 'again': # if any violations made
var.trialCounter+=1 # plus 1 in trialCounter
var.sameInitPos = True # next trial will use the same init position
var.reward_clicks=[] # reset reward_clicks(records the clicks during reward) list
var.delay_clicks=[] # reset delay_clicks(records the clicks during delay waiting time) list
again = True
else: # if no violation made
var.rewardGot += 1 # plus 1 in rewardGot
var.trialCounter+=1 # plus 1 in trialCounter (total trial num)
var.blockTrialCounter += 1 # plus 1 in blockTrialCounter (trial num in current block)
var.pairNum+=1 # proceed to next pair
var.reward_clicks=[]# reset reward_clicks record list
var.delay_clicks=[] # reset delay_clicks(records the clicks during delay waiting time) list
again = False
if var.rewardGot == var.pay and again == False: # assign pay_delay and pay_num if the current trial is the one picked to be actually paid
if var.choice!='b':
var.pay_delay = var.delaymag
else:
var.pay_delay = var.shortdelay
var.pay_num = var.rewmag
fc.dataRecord_longVerbal(var,dbc,sessid) # record data for this trial
fc.resetVar(var,again)
var.blockTrialCounter = 0 # reset blockTrialCounter
var.dataFile.close() # close the data file
var.longVerbal = False # reset longVerbal boolean to be False
endt = datetime.datetime.now() # record end time
sql.w_after_Verbal(dbc,var,sessid, p_num, expDate, endt, expName, host_ip, setiddtb) # save data of this session in database
return p_net, var.pay, var.pay_delay, var.pay_num, var.trialCounter,var.points,

if __name__ == "__main__":
LongVerbal(var,doo,myPoints,dbc,mouse) # run main function
Loading

0 comments on commit b299e3d

Please sign in to comment.