In [2]:
#Homework 3
#Ethan Witkowski
In [6]:
#1

#Read in text file
f = open('hw3_data.txt', 'r')
full_data = f.readlines()
f.close()

#Declare first line as line1
line1 = full_data[0]

#Remove spacing elements for line1
line1_split = line1.strip().split('\t')
print(line1_split)
type(line1_split)

#Change line1_split to tuple
line1_split = tuple(line1_split)
print(line1_split)
type(line1_split)

#Assign variable names
subject, session, list_num, serialpos, word, recalled = line1_split

#Transfer to type int
sessions = int(session)
list_num = int(list_num)
serialpos = int(serialpos)
recalled = int(recalled)
['R1065J', '0', '1', '1', 'SEAT', '1']
('R1065J', '0', '1', '1', 'SEAT', '1')
In [7]:
#2

#Remove spacing elements, transfer to list
for i, observation in enumerate(full_data):
    full_data[i] = observation.strip().split('\t')
    
print(full_data[i])
['R1065J', '5', '25', '12', 'LEAF', '0']
In [8]:
#Initiate new list
data = []

for i in range(6):              
    data.append([])                   #Create new list for each variable

    for sub_list in full_data:
        data[i].append(sub_list[i])   #Append ith element in sub_list to correct variable

#Declare variable names
subj, sess, list_number, serial_position, word_pres, recall = data
len(subj)
Out[8]:
1800
In [9]:
from statistics import mean

#Change to integer
recall = list(map(int, recall))

#Compute mean/Proportion of recalled words
prec_overall = mean(recall)
print(prec_overall)
0.33555555555555555
In [10]:
#3

#Set unique session identifiers
unique_sess = list(set(sess))
print(unique_sess)

#Pre-allocate list
prec_by_sess = []

#Create new list for each session
for i,session in enumerate(unique_sess):
    prec_by_sess.append([])
     
#Append recall to prec_by_sess if same session
    for j,group in enumerate(sess):
        if group == session:
            prec_by_sess[i].append(recall[j])
            
print(prec_by_sess)

#Declare prec_by_sess as mean of each session
for i,sess_recall in enumerate(prec_by_sess):
    prec_by_sess[i] = mean(sess_recall)
    
print(prec_by_sess)
['3', '4', '0', '2', '5', '1']
[[1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1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1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0]]
[0.33666666666666667, 0.2866666666666667, 0.33, 0.33666666666666667, 0.36666666666666664, 0.3566666666666667]
In [8]:
#4

#Set unique serial position identifiers
unique_serial_position = list(set(serial_position))
print(unique_serial_position)

#Pre-allocate list
prec_by_serial_position = []

#Create new list for each serial position
for i,serial_pos in enumerate(unique_serial_position):
    prec_by_serial_position.append([])
     
#Append recall to prec_by_serial_position if same serial position
    for j,group in enumerate(serial_position):
        if group == serial_pos:
            prec_by_serial_position[i].append(recall[j])
            

#Declare prec_by_serial_position as mean of each serial position
for i,serial_recall in enumerate(prec_by_serial_position):
    prec_by_serial_position[i] = mean(serial_recall)
    
print(prec_by_serial_position)
['2', '7', '3', '8', '10', '6', '11', '9', '1', '5', '4', '12']
[0.36, 0.2866666666666667, 0.38666666666666666, 0.34, 0.4, 0.29333333333333333, 0.35333333333333333, 0.3466666666666667, 0.3933333333333333, 0.30666666666666664, 0.36666666666666664, 0.19333333333333333]
In [9]:
#Bonus

from psychopy import visual, core, event
import sys
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
In [10]:
#Test method function - timed presentation of words

def test_method(final_words):

    for i in final_words:
        stim = visual.TextStim(win,text=i.rstrip('\n'))
        stim.pos = [0,0]
        stim.draw()
        win.flip()
        core.wait(.25)
    
    win.close()
In [11]:
#Experimental method function - keypress presentation of words

def experimental_method(final_words):

    for i in final_words:
        stim = visual.TextStim(win,text=i.rstrip('\n'))
        stim.pos = [0,0]
        stim.draw()
        win.flip()
        pressedkey = event.waitKeys(keyList = ["j", "k", "q"])
        responses.append(pressedkey)

        if pressedkey[0] == "q":
                break
    
    win.close()
    return responses
    
In [15]:
#Access text file
f = open('wordpool.txt')
words = f.read().splitlines()
f.close()

#Ask user to input number of words 
user_q = input("Would you like to specify how many words will be shown to the subject? Please respond 'yes' or 'no': ")

if user_q == 'yes':
   #User input number of words
    num_words = input("Please specify the number of words shown to the subject: ")
    num_words = int(num_words)

    if num_words > len(words):
        print("Error: You selected too many words. " + "Please select between 1 and " + str(len(words)) + " words.")
        sys.exit()

    elif num_words < 1:
        print("Error: You did not select enough words. " + "Please select between 1 and " + str(len(words)) + " words.")
        sys.exit()
        
    else:
        final_words = words[:num_words]

elif user_q == 'no':
    print("Okay, we will show the subject " + str(len(words)) + " words")
    final_words = words
    
else:
    print("Error: We default to showing the subject " + str(len(words)) + " words")
    final_words = words

#Pre-allocate list of responses
responses = []

#Create window
win = visual.Window([400,400])

#Test or experimental question
method_q = input("Would you like to use the test or experimental method? Please respond 'test' or 'experimental': ")

if method_q == 'test':
    test_method(final_words)

elif method_q == 'experimental':
    experimental_method(final_words)
    
else:
    print("You did not specify a method")
    sys.exit()
Would you like to specify how many words will be shown to the subject? Please respond 'yes' or 'no': no
Okay, we will show the subject 1000 words
Would you like to use the test or experimental method? Please respond 'test' or 'experimental': test
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-15-f203c7418fe4> in <module>
     41 
     42 if method_q == 'test':
---> 43     test_method(final_words)
     44 
     45 elif method_q == 'experimental':

<ipython-input-10-9483bc5bb0be> in test_method(final_words)
      8         stim.draw()
      9         win.flip()
---> 10         core.wait(.25)
     11 
     12     win.close()

~\Anaconda3\envs\psypy3\lib\site-packages\psychopy\clock.py in wait(secs, hogCPUperiod)
    337             if (win.winType == "pyglet" and
    338                     hasattr(win.winHandle, "dispatch_events")):
--> 339                 win.winHandle.dispatch_events()  # pump events
    340 
    341 

~\Anaconda3\envs\psypy3\lib\site-packages\pyglet\window\win32\__init__.py in dispatch_events(self)
    618 
    619         msg = MSG()
--> 620         while _user32.PeekMessageW(byref(msg), 0, 0, 0, PM_REMOVE):
    621             _user32.TranslateMessage(byref(msg))
    622             _user32.DispatchMessageW(byref(msg))

KeyboardInterrupt: 
In [16]:
win.close()
1679.6304 	WARNING 	Monitor specification not found. Creating a temporary one...
In [ ]: