Howdy, Stranger!

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

Supported by

Access variable's history

Hi,

Although it is a similar question as the one I've previously asked, I opened an new discussion because it may be interesting for others too.

I found the following page on accessing previous responses: https://osdoc.cogsci.nl/3.1/manual/python/responses/. However, I wanted to know whether there is a way to access another variable's history in a similar fashion.
Imagine that you have a binary variable that can either have the values 0 or 1. How could I access the history of this variable via an inline script?

I tried to define a variable and access it in a similar fashion as described in the link above:
example = var.binaryvariable[:10]
However, this only led to the following error message: 'int' object has no attribute 'getitem'.

Thank you so much in advance!
Best,
Christian

Comments

  • Hi Christian,

    That's an interesting question. The var object has no memory (unlike the responses object). You could give it memory by overriding the way it sets variables, and by adding a function to retrieve a variable's history. Adding the following script to the Prepare phase at the very start of the experiment should do this.

    Note: This hacks into the OpenSesame internals and may break in future versions. And test your experiment to make sure that there are no unwanted side-effects (I don't think there are, but still).

    def my_var_setattr(key, val=None):
    
        if key not in var._history:
            var._history[key] = []
        var._history[key].append(val)
        var.__vars__[key] = val
    
    
    def my_var_history(key):
    
        return var._history.get(key, [])
    
    
    object.__setattr__(var, '_history', {})
    object.__setattr__(var, '__setattr__', my_var_setattr)
    object.__setattr__(var, 'history' , my_var_history)
    

    Once you've done this, you get a list of all values that a variable every had with the var.history() function:

    print(var.history('binary_variable'))
    

    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

  • Hi Sebastiaan,

    Thank you very much! Meanwhile I thought of a similar workaround, created a list and added the wanted variable at the end of each trial to the list:

     if self.get('my_variable') == 1:
        my_list.append(1)
    else: my_list.append(0)
    

    Then I could just access the last few trials via:

     my_list[-10:]

    But thank you again, I for sure have to try your approach!

    Best,
    Christian

Sign In or Register to comment.