[solved] Custom log file error
Hi,
Im working on a simple Stroop task with a Droid back-end and instead of the standard logger i want to use a custom log file with just a couple of variables. I use one inline_script at the start of the experiment that closes the log file and creates a new one with an updated participant number and begin by writing the header of the new file:
import codecs
import os
print log
# Close current log file
log.close()
# Define the first log
i = 0
print os.path.dirname(os.path.realpath('.'))
while os.path.exists('/sdcard/Data/Stroop/subject-%d.csv' % i):
i += 1
print i
# Open the new log file with up to date participant number
log = codecs.open('/sdcard/Data/Stroop/subject-%d.csv' % i, 'w', encoding='utf-8')
print log
# write header to log
log.write("word,color,congruency,rt/n")
Then after the response for each trial i put another inline_script to write the variables i'm interested in according to the new syntax documentation:
var_list = [var.word,var.color,var.congruency]
log.write_vars(var_list)
However, the script crashes after the first trial and the newly made log file remains empty. The debug text states that the 'file' object has no attribute 'write_vars'
Does anyone see what is going wrong?
Thanks in advance,
Laurent
Comments
Hi Laurent,
codecs.open()
returns a file object, which supports all the regular Python file operations. But you're treating it as though it's an OpenSesamelog
object (i.e. by callinglog.write_vars()
). The correct approach would be to either:file
object, as you're doing now, and use the standard operations, as described here:log
object, usinglog.open()
as described here:Does that make sense?
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
Thanks for the quick reply, it makes more sense now. What remains is a beginners question: How can i write the variables to the new log file? At the moment i create a list like in the previous post and i use log.write_vars([var_list]) to write the values to the file but this results in the following error: 'list' object has no attribute 'replace'
Am i missing something in the syntax here?
On a related note, in another experiment made with the previous version of OpenSesame i used a very similar customized log file; i create a new file object, write a header to it and gather the variables. Here i write the variables with
and it used to work perfectly. But with the new 3.0.1 version i get the folowing error: StreamReaderWriter instance has no call method
Could this be a result of the new version?
var_list
is already alist
, so you don't need to put brackets around it; that will just put thelist
in anotherlist
.This is essentially the same problem as before. You've replaced the a standard
log
object with aStreamReaderWriter
, which doesn't support calling the object directly--which happens here, becauseself.log
is an object, not a function.This may sound complicated, but as a general simple rule, OpenSesame's standard objects are a bit like Britney, you gotta leave them alone. So don't replace
log
,pool
,var
,items
,clock
, etc. If you want to create your own logfile through codecs, that's fine, but you should simply assign it to a different variable. For example, this is fine:Makes sense?
Check out SigmundAI.eu for our OpenSesame AI assistant!
Excellent explanation, it makes sense now
I've decided to stick with creating file objects as new log files and with some minor alterations both work fine now but i still don't understand why the logging stopped working in the first place.
Btw, i discovered the live variable inspector today, it makes tracking the errors a lot easier. Great new feature!