Howdy, Stranger!

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

Supported by

accessing information from the logfile

Dear forum,

is it possible to access the data in the logfile? (Data from a previous session)

I would like to run an experiment. In the beginning it should check whether there is a file in the logfile with a certain name (including ID and SessionNumber). Depeding on whether it is there or not, the session number will be altered.

In other words. The program shall check itself whether the participant has already performed lets say session 7, and therefore select 8 as the session number of the running experiment.

Any ideas on how to accomplish that?

Comments

  • Hi @DahmSF ,

    Yes, that's possible. To do that, you would need to write a simple script, like the one below, and add that to the Prepare phase of an inline_script at the start of the experiment. The script below checks whether a particular file, based on the subject number and session, already exists, and if so, increments the session number until the file doesn't exist yet. And then the file name is used to re-open the log.

    Hope that helps!

    Sebastiaan

    import os
    
    MAX_SESSION = 10
    
    for var.session_nr in range(1, MAX_SESSION + 1):
        # Get the full path to the log file
        log_path = os.path.abspath(
            'logfile-{}-{}.csv'.format(var.subject_nr, var.session_nr)
        )
        # If this log file doesn't exist yet, break the loop and use it
        if not os.path.exists(log_path):
            break
    else:
        # Executed if no break occurred
        raise Exception('All session have been completed!')
        
    # Re-open the logfile
    print(log_path)
    log.open(log_path)
    
  • This is great stuff! Too good to be true 😀

    I just changed the directory of where the logfiles are stored to the directory of my experiment file.

    log_path = os.path.join(var.experiment_path,"Logfile", 'ID{}_Session{}.csv'.format(var.subject_nr, var.session_nr)

    )

    Thanks

    Stephan

  • One more question on this:

    I always make a copy of the file at the end of the experiment. I used to take this code:

    shutil.copyfile(var.logfile,os.path.join(var.experiment_path,"Logfile",filename+".csv"))

    However, the var.logfile seems to take the old file which is empty as the data is stored in the new file (see above). Is there a way to make a copy of the new file. I am pretty sure I need to use log_path somehow. Only that this is only the path but not the file itself.

    Thanks for your help

    Stephan

  • Hi Stephan,

    Thanks for pointing this out. Of course, log.open() should also update var.logfile , so that's a minor bug. (To be fixed for 3.3.5, because 3.3.4 was already tagged just now.) For now, this is easily worked around though:

    var.logfile = log_path
    

    Cheers!

    Sebastiaan

Sign In or Register to comment.