Variable sometimes not logged
in OpenSesame
I have this inline_script:
def my_form_validator():
"""Checks whether both the gender and age fields have been filled out"""
#also checks for the number of characters
return var.sex != u'no' and var.byear != u'' and var.bmonth != u'' and var.bday != u'' and len(str(var.byear))==4 and len(str(var.bmonth)) in [1,2] and len(str(var.bday)) in [1,2]
def filter_digits(ch):
"""Allows only digit characters as input"""
return ch in ['0','1','2','3','4','5','6','7','8','9','backspace']
# Define all widgets
button_ok = Button(text=u'Ok')
#label_gender= Label(u'Your gender')
checkbox_male = Checkbox(text=u'Maennlich', group=u'sex', var=u'sex')
checkbox_female = Checkbox(text=u'Weiblich', group=u'sex', var=u'sex')
label_age = Label(u'Bitte geben Sie Ihr Geburtsdatum ein')
label_year = Label(u'Jahr')
label_month = Label(u'Monat')
label_day = Label(u'Tag')
# Specify a key filter so that only digits are accepted as text input
input_year = TextInput(stub=u'JJJJ', var=u'byear', key_filter=filter_digits) #actually a selection thing would be nice
input_month = TextInput(stub=u'MM', var=u'bmonth', key_filter=filter_digits)
input_day = TextInput(stub=u'TT', var=u'bday', key_filter=filter_digits)
# Build the form. Specify a validator function to make sure that the form is
# completed.
my_form = Form( validator=my_form_validator, cols=3, rows=[3,1,1,1,1,1,1,1,1,3], spacing=10, margins=(100, 100, 100, 100), )
#my_form.set_widget(label_gender, (0, 0))
my_form.set_widget(checkbox_male, (1, 2))
my_form.set_widget(checkbox_female, (2, 2))
my_form.set_widget(label_age, (1, 3))
my_form.set_widget(label_day, (0, 4))
my_form.set_widget(label_month, (0, 5))
my_form.set_widget(label_year, (0, 6))
my_form.set_widget(input_day, (1, 4), colspan=2)
my_form.set_widget(input_month, (1, 5), colspan=2)
my_form.set_widget(input_year, (1, 6), colspan=2)
my_form.set_widget(button_ok, (1, 7))
my_form._exec()
# Calculate age from current time and Birtdate
import shutil,os,datetime
now = datetime.datetime.now()
current_year = now.strftime("%Y")
current_month = now.strftime("%m")
current_day = now.strftime("%d")
#current_time = now.strftime("%Y-%m-%d")
years = int(current_year) - var.byear
months = int(current_month) - var.bmonth
days = int(current_day) - var.bday
var.age= years + float(months)/12 + float(days)/365
var.Bday = str(var.byear)+'.'+str(var.bmonth)+'.'+str(var.bday)
var.Sex=str(var.sex)
print var.age, var.Bday
log.write_vars()
Mostly the variable sex is logged. But sometimes it is not. I have no clue about why it is sometimes not logged. And i cannot reproduce the behavior. It seems like random.
Could it have to do with garbage collection? Other ideas?
I tried full screen and quick run. In both it usually logs. But sometimes not.
Comments
Hi Stephan,
What do you mean with sometimes? Some trials in one session, or some session? So, if you hit the RUN button will it either log everything (usually) or nothing (sometimes), or will it log some trials but not others?
Eduard
Hi eduard,
it is just in some sessions. If i repeat the session the variable usually appears in the inspector (and the logfile).
The variable 'sex' is only coded once, so there is only one trial. It is logged at the same time as the birthday variables (which always appear in the inspector/output).
You see that i tried a workaround with
var.Sex=str(var.sex)
, which unfortunately does not really help.What i noticed is the following: Sex is always in the variable inspector. However, sometimes its values remain empty (and in those cases sex does not appear in the variable inspector).
What's the meaning of the 'u' in var=u'sex'? Could it have to do with that?
It is so strange, because if the variable is not filled the experiment should not go on (see my_form_validator), however it always does. So there must be a value for sex, but the variable gets lost on the way (somewhere)...
How often does that happen? I just tried it 10 times and it always worked. I used the expyriment backend and the quickrun button on a linux machine.
Hi eduard,
I just replicated the error on the second run (new day, same problem). Yesterday i also made like 10 runs, where it worked. I cannot say how often it occurs.
I am using Win7, expyriment backend. The error also occured on Mac.
Maybe it has to do with the rest of my experiment? Can I send you the whole experiment privately somewhere?
I shortened the script a little:
What i know for now:
-the form_validator is not the problem
-it is definitely the checkbox widget (if it occurs in one checkbox widget, the problem appears in another checkbox widget too)
-most variables appear at the start of the experiment in the inspector, however the checkbox variables appear on presentation of the widget (which takes some miliseconds to appear in the inspector). But in some cases (don't know why nor when), the variables do not appear. Even if they do not appear in the inspector, they are somewhere, because they do work in the form_validator --> the experiment goes on.
Update:
Actually, my workaround with the copy of the variable helped. Although not visible in the inspector, the values of the new variable Sex are logged in the logfile. However, the old variable sex is not in the logfile. Seems like a temporary variable, which surprises me, because in the form_validator i draw on it with var.sex and not with sex.
Maybe it has to do with the group definition?
Honestly, I have no idea. I tried it now close to 20 times and the variable is always present. Maybe it has to do with the Operating System?
Anyway, if this work around works for you, that that's good I guess. If you feel like it, It'd be cool if you could do some more testing, try to further hone in on the problem and eventually submit a bug report on github.
Thanks,
Eduard
Possibly, it had to do with the operating system. Actually, it was never present on a certain MacBook. And there it never appeared in the logfile. However, i want to be independent from operating systems (one of the strengths of OS!) using participants notebooks.
Now i solved it by changing the OpenSesameVar to a PythonVar, and then back again.