Howdy, Stranger!

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

Supported by

[solved] Resetting counters

edited June 2013 in OpenSesame

Hey all,

So I am trying to figure out how to reset a counter. I have a loop for blocks which contains a loop for trials. In order to loop through blocks and trials I trying to use count_variables as indices, in particular the count_blockloop and count_trialsequence in the inline_code.

So the block works just fine, but its the trial looping that breaks since the counter does not restart with each block and so it goes beyond the number of elements in a dictionary from which the trials are drawn. I tried doing self.experiment.set('counter_trialsequence', 0) in the block inline_code, but that has no effect.

So any other idea of how to restart the counter from within the block inline_code?

Thanks,

SillyNapalm

Comments

  • edited 1:21PM

    Hi SillyNapalm,

    The items maintain their own counter, which needs to be explicitly reset. Otherwise, as you have experienced, it will simply keep overwriting the count_[item_name] variable. The following should do the trick:

    # Uncomment for 0.25 and earlier
    # exp = self.experiment
    exp.items['item_to_reset'].count = 0

    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 1:21PM

    Thanks for the reply. I tried it, but it returns a KeyError. So how do I add it to the dictionary? Or maybe its something else...

    I would also be grateful if you could point me to where I can find documentation for the experiment dictionary methods as I fail to see it on the main website.

  • edited 1:21PM
    Thanks for the reply. I tried it, but it returns a KeyError. So how do I add it to the dictionary? Or maybe its something else...

    Did you use the correct item name? For example, if the sequence is called trial_sequence you would use the following statement:

    exp.items['trial_sequence'].count = 0
    I would also be grateful if you could point me to where I can find documentation for the experiment dictionary methods as I fail to see it on the main website.

    Unfortunately, there's no systematic documentation on this. The available functions depend on the item, and are not really intended to be accessed by the user. I sometimes access them nonetheless, like now, but that's kind of a hack for want of a better solution.

    Still, you could get a list of functions of attributes with a script like this:

    item = 'my_item_name'
    for i in dir(exp.items[item]):
        print '%s (%s)' % (i, type(eval('exp.items[item].%s' % i)))

    Cheers!

    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 1:21PM

    Yeah, I have the correct name. And I tried several combinations. Also, it does not matter where I put it in the experiment it always returns the KeyError.

    Any ideas? I could change the approach completely, but it would be nice for this to work since this is rather simple and fast.

  • edited 1:21PM

    Really? The exp.items dictionary is the place were all the items are stored. Are you sure you didn't accidentally modify exp.items? Or is there a typo (it's case sensitive)? You can check what's in the items dictionary with a script like this:

    for item in exp.items:
        print '%s (%s)' % (item, exp.items[item].__class__)

    This should give something like the following output, where the first part is the item name (= key in the dictionary).

    inline_script (libopensesame.inline_script.inline_script)
    experiment (libopensesame.sequence.sequence)
    getting_started (notepad.notepad)
    welcome (libopensesame.sketchpad.sketchpad)

    If it really doesn't work you could upload the entire script to something pastebin or paste it here, so I can take a look at it.

    Cheers!

    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 1:21PM

    I got it. The issue was that I was using 'count_var_name' instead of 'var_name' when applying count method. I suppose I was going based on the variable inspector.

    Anyhow, the issue is closed.

    Thanks,

    SillyNapalm

  • edited 1:21PM

    Can you reset the 'count' experiment variable itself? I would like my log file to only have one count column, which I will reset for each part of the experiment.

    Best regards,
    Jákup

  • Hi Jákup,

    What is the item you want to count? And when do you want to reset it?

    If, for instance, you want to count the number of trials and reset this counter at the beginning of every new block, I think the code Sebastiaan posted above should work.

    For example, if your trial sequence item is called trial_sequence, you would append an inline_script item to your block sequence item and place the following piece of code here:

    exp.items['trial_sequence'].count = 0

    By resetting this variable at the beginning of a new block, the variable count_trial_sequence (that you're probably familiar with) will automatically be reset to zero as well.

    Does this help? If this was not exactly what you were asking for, perhaps you could provide us with some more information about what output variables you would like to have in your log files.

    Best wishes,

    Lotje

  • edited 1:21PM

    Hello Lotje.
    Thank you very much for your help :) This will definitely work, as well. But isn't there a built in variable called simply 'count'? I was thinking, that instead of logging all the various instantiations of the count_* variables, that I could simply reset the built in counter, at each new portion of the program. Or is this variable perhaps the count_trial_sequence counter? :)

    Best regards,
    Jákup

  • edited December 2012

    Hi Jakub!

    I think I can answer that as well. The count_trial_sequence is the amount of times the sequence item called 'trial_sequence' (which you probably have) is run.

    Secondly, a built-in counter would be very hard to implement, since for every experiment you usually want to count different thing. What you could do, is make your own custom counter. Use an inline_script item to start the counter, use one every time you would want the counter to go up and use one for resetting:

    starting the counter:

    self.experiment.set("my_counter", 0)
    

    making the counter go up by one:

    self.experiment.set("my_counter", self.get("my_counter"+1))
    

    resetting the counter:

    self.experiment.set("my_counter", 0)
    
  • edited 1:21PM

    Hello again Edwin :)
    And thank you very much :) That is indeed a good idea, if I truly want to customize how I want the experiment counted.

    But there is a built in variable called 'count'. If you log it using the logger, it will count all the cycles throughout the experiment. If you can't see it in the logger item, you can just add this to the logger script:

     log "count"

    In my experiment, I could of course log, for example, "count_session1_squence" + "count_session2_sequence" + "count_questionnaire_sequence" separately. But then my log file will have a separate column for each of these counters, and I will have to manually edit them to a single one as "count". So, if it would be possible to reset control the count variable itself, that would be nice.

    exp.set('count')

    doesn't seem to work.

    Of course, this is just nit-picking, and I could settle for putting each item, I wish to be counted together in the same sequences, and then just count that sequence. But I'm curious if it is possible to use the count variable in this was. I'm doing the same for "response" - i.e., I only log that variable, and not any other separate response variables.

    All the best!
    Jákup

  • edited December 2012

    Hi Jákup,

    Just a quick note:

    Perhaps it's helpful to understand what the count variable actually is, because it's potentially a bit confusing: Every item has a variable that is called count, which simply counts how often the item has been called. This variable is specific to a particular item, and only accessible from within that item. This item variable is used to set a 'global' variable in the experiment, which is called count_[item name]. If you are interested, you can see in the source how this happens in an item's prepare phase: https://github.com/smathot/OpenSesame/blob/master/libopensesame/item.py#L76

    So if you do

    self.get('count')

    in an inline_script, you will get the counter for that inline_script item. Similarly, if you log count in a logger, you will log the counter for that logger. This may seem like a global counter, but it's not: It's simply the counter for that specific item. This is due to the fact that variables can exist at two levels, as explained (inadequately I must admit) here: Either as a 'global' experiment variable, accessible to all items, or as a 'local' item variable, only from within a specific item.

    There is no global counter, for the reason that Edwin mentioned: OpenSesame does not know what your experiment 'means' and as such does not know how to maintain an appropriate counter. Instead, it offers separate counters for all items, for you to interpret. Does that make sense? Generally speaking, it is count_trial_sequence that you want, but it depends on your experiment.

    Hope this clears things up a bit!

    Cheers!

    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 December 2012

    It certainly did, thanks for clarifying :)

    So when I log the response variable, in a similar way, it is not a global variable called "response", but a local one, that is replaced by the same variable in subsequent items?

    So, logging the logger's count simulates what I was looking for, correct? Is there a way to reset it?

    Best regards,

    Jákup

  • edited 1:21PM
    So when I log the response variable, in a similar way, it is not a global variable called "response", but a local one, that is replaced by the same variable in subsequent items?

    Well, the rule is a follows: When a logger self.get()s a variable, it first checks whether this variable exists at the item level. If not, it checks whether the variable exists at the experiment level. In the case of response, this variable generally only exists at the experiment level. But you will see that if, just to test, you add the line ...

    set response this_is_a_dummy_response

    ... to the logger script, the logger will pick this newly set item-level response variable (which is not what you want, of course, but just to illustrate). So it's a fallback mechanism, where item-level variables get precedence.

    So, logging the logger's count simulates what I was looking for, correct? Is there a way to reset it?

    Yes, see my first response in this thread ;)

    exp.items['item_to_reset'].count = 0

    The item to reset would in this case be the name of the logger item. This will reset the item-level count variable and indirectly the experiment-level count_[item name] variable.

    Cheers!

    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 1:21PM

    Oh... yes :) Silly me 8-|

    Thanks a bunch for taking the time to explain this in debt!

    Jákup

Sign In or Register to comment.