Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Custom log file error

edited November 2015 in OpenSesame

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

  • edited 12:49PM

    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 OpenSesame log object (i.e. by calling log.write_vars()). The correct approach would be to either:

    1. Create a file object, as you're doing now, and use the standard operations, as described here:
    2. Re-open the OpenSesame log object, using log.open() as described here:

    Does that make sense?

    Cheers,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 12:49PM

    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

    self.log('%s', % (self.get('var1'))) 
    

    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?

    • Laurent
  • edited 12:49PM

    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'

    var_list is already a list, so you don't need to put brackets around it; that will just put the list in another list.

    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 self.log('%s', % (self.get('var1'))) 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

    This is essentially the same problem as before. You've replaced the a standard log object with a StreamReaderWriter, which doesn't support calling the object directly--which happens here, because self.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:

    # Open the new log file with up to date participant number
    my_custom_log = codecs.open('/sdcard/Data/Stroop/subject-%d.csv' % i, 'w', encoding='utf-8')
    

    Makes sense?

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited 12:49PM

    This may sound complicated, but as a general simple rule, OpenSesame's standard objects are a bit like Britney, you gotta leave them alone.

    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!

Sign In or Register to comment.