Re: [psychopy-users] Change code to save data in excel-file instead of txt-file?

342 views
Skip to first unread message

Michael MacAskill

unread,
Feb 17, 2013, 11:31:21 PM2/17/13
to psychop...@googlegroups.com
Dear Nynne,

Find the line:
saveThisResponse(captured_string) #write to file

and change it to something like:

thisExp.addData("ResponseText", captured_string) # add to PsychoPy data file

Whoever wrote that code used their own system to save the data. Above, you are simply replacing that with a call to PsychoPy's built-in data handling routines instead. This will add a new column to the data output file, with a header label of "ResponseText" or whatever you prefer, and store the response in the same row along with all of the other info for that trial.

Regards,

Michael

On 15 Feb, 2013, at 23:24, <nynn...@gmail.com> wrote:

> Hi Psycho-Py users.
> Psycho-Py-version: 1.75.01
> Operating system: Windows
> I'm new to Python and PsychoPY, so please bear with me if my request is total nonsense.
> I have inserted a python-code into the code component in the Builder-version which generates a text-box where participants are allowed to type in their responses in text-format. This works fine.
> However, all the participants' responses are saved in the same file (named 'myResponses.txt'), and not in separate excel-files. I would prefer that their text-responses are saved in the same excel-file as the data from my looped trial-routines.
> Can anybody help? Below is the text-box code that I've put into the code component in Builder. Can anybody please tell me, where and what I should change and what I should change it to? (I have bolded the code-section, where I believe something should be changed)
> Thank you.
> Best, Nynne
>
> #!/usr/bin/env python
>
> # SUPPLIED WITH NO GUARANTEES!!
> from psychopy import visual, core, event
> #my lazy way of keeping time NB can be relatively inaccurate
> import time
>
> # set up a stimulus window
> myWin = visual.Window(size=(1366, 768), fullscr=True, screen=0, allowGUI=False, allowStencil=False,
> monitor='testMonitor', color=[0,0,0], colorSpace='rgb') #visual.Window((1366.0,768.0), allowGUI=False,winType='pygame',
> #monitor='testMonitor', units ='deg', fullscr=True, screen=0)
>
> #set up some fonts. If a list is provided, the first font found will be used.
> sans = ['Gill Sans MT', 'Arial','Helvetica','Verdana'] #use the first font found on this list
>
>
> #INITIALISE SOME STIMULI
> #the stimulus could be anything, I'm just using simple text
> The_stimulus = visual.TextStim(myWin,
> units='norm',height = 0.1,
> pos=(0, 0), text='',
> font=sans,
> alignHoriz = 'center',alignVert='center',
> color='Black')
>
> #acts as a cue for the participant to reponsd
> # shown at the top of the screen
> ResponseInstuction = visual.TextStim(myWin,
> units='norm',height = 0.1,
> pos=(0, 0.5), text='Angiv din alder. Tryk derefter enter',
> font=sans,
> alignHoriz = 'center',alignVert='center',
> color='Black')
>
> #will be used to show the text they are typing: will update every
> # time they type a letter
> CapturedResponseString = visual.TextStim(myWin,
> units='norm',height = 0.1,
> pos=(0, 0.0), text='',
> font=sans,
> alignHoriz = 'center',alignVert='center',
> color='Black')
>
> captured_string = '' #empty for now.. this is a string of zero length that
> # we will append our key presses to in sequence
>
> #a routine to save responses to file anytime we want to
> def saveThisResponse(captured_string):
> outfile = "./myResponses.txt"
> f = open(outfile, 'a')
> f.write(captured_string)
> f.write('; typed at %s' %time.asctime())
> f.write('\n')
> f.close()
>
> #a routine to update the string on the screen as the participant types
> def updateTheResponse(captured_string):
> CapturedResponseString.setText(captured_string)
> CapturedResponseString.draw()
> ResponseInstuction.draw()
> myWin.flip()
>
>
> #setup done, now start doing stuff
> subject_response_end = 0 # only changes when they hit return
>
> while subject_response_end == 0: # we don't want to keep going forever (if we did, we would write "while True:")
> #draw the stimulus .. this can be anything but here it is text
> The_stimulus.draw() # draw it
> myWin.flip() # show it
> time.sleep(1) #leave it on the screen for 1s
>
> #now instruct the participnat to respond
> ResponseInstuction.draw() # draw instruction
> myWin.flip() # show instruction
>
> # now we will keep tracking what's happening on the keyboard
> # until the participant hits the return key
> subject_response_finished = 0 # only changes when they hit return
>
> #check for Esc key / return key presses each frame
> while subject_response_finished == 0:
> for key in event.getKeys():
> #quit at any point
> if key in ['escape']:
> myWin.close()
> core.quit()
>
> #if the participant hits return, save the string so far out
> #and reset the string to zero length for the next trial
> elif key in ['return']:
> print 'participant typed %s' %captured_string #show in debug window
> saveThisResponse(captured_string) #write to file
> captured_string = '' #reset to zero length
> subject_response_finished = 1 #allows the next trial to start
> subject_response_end = 1 # allows the code-routine to end
>
> #allow the participant to do deletions too , using the
> # delete key, and show the change they made
> elif key in ['delete','backspace']:
> captured_string = captured_string[:-1] #delete last character
> updateTheResponse(captured_string)
> #handle spaces
> elif key in ['space']:
> captured_string = captured_string+' '
> updateTheResponse(captured_string)
> elif key in ['period']:
> captured_string = captured_string+'.'
> updateTheResponse(captured_string)
> elif key in ['comma']:
> captured_string = captured_string+','
> updateTheResponse(captured_string)
> elif key in ['æ']:
> captured_string = captured_string+'ae'
> updateTheResponse(captured_string)
> elif key in ['lshift','rshift']:
> pass #do nothing when some keys are pressed
> #etc ...
>
> #if any other key is pressed, add it to the string and
> # show the participant what they typed
> else:
> captured_string = captured_string+key
> #show it
> updateTheResponse(captured_string)
>
> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/a-VlcFOo0f4J.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
Michael R. MacAskill, PhD michael....@nzbri.org
Research Director,
New Zealand Brain Research Institute

66 Stewart St http://www.nzbri.org/macaskill
Christchurch 8011 Ph: +64 3 3786 072
NEW ZEALAND Fax: +64 3 3786 080










Radka Jersakova

unread,
Feb 25, 2013, 8:26:08 AM2/25/13
to psychop...@googlegroups.com
Hello Nynne,

I am using the same piece of code that you are using for recording text responses and this is the change that I have made to have a unique file for each participant that opens in excel. Before ppl start my experiment I assign them group number and participant number so that is what group and pNum stand for.

def saveThisResponse(list):
    outfile = "./" + str(group) + '_' + str(pNum) + "myResponses.csv"
    f = open(outfile, 'a') #open our results file in append mode so we don't overwrite anything
    for i in list:
        f.write(str(i) + ',')
    f.write('\n') # write a line ending
    f.close() #close and "save" the output file

To make sure that each file is saved under a unique name, simply make sure to give each participant a unique identifier so that for each participant, saveThisResponse will create a new file. If you want these responses to be added to the already existing file from the beginning of the study, what you need to find out is under what name are these saved and then simply change the outfile in the above piece of code to that name. Because the file is open as 'a', any extra data will be added to it and the program won't rewrite the existing information in that file. What you need to keep in mind is that you need a line of code that will be different for each participant so that each file is called something else (that is why I use group and participant numbers).

Does this make sense?

Radka

Radka Jersakova

unread,
Feb 25, 2013, 8:40:22 AM2/25/13
to psychop...@googlegroups.com
I should also point out that I first collect a list of responses that I then save together so you might not need to change that part of the code the way I have, you can simple call f.write(string_response)
Reply all
Reply to author
Forward
0 new messages