Tobii eyetracker and Psychopy

413 views
Skip to first unread message

Horea Christian

unread,
Mar 1, 2013, 10:34:19 PM3/1/13
to psychop...@googlegroups.com
Hi, I think this is rather a python comprehension problem than a psychopy one, but I do think it's topic-relevant.

I decided to use this script I found on the internet http://pastebin.com/zbaXq3g9 . I managed to get it to work, but I want to keep all the controller functions in a separate file - because otherwise my main experiment file will become very big. I tried to split the files like so:

Main experiment file: 

Tobii controller file:


Now, for some reason my import won't work - I get complaints about win being undefined - though I _do_ define it before I call the functions from the "letobii" file. My error reads:


Many thanks for your help,

Denis A. Engemann

unread,
Mar 2, 2013, 1:31:10 AM3/2/13
to psychop...@googlegroups.com
Hi chris,
you do define win in you script, but if you import the tobi module win must already be defined in a scope the tobii class can reach at that point. It will first search in the innermost scope, that is the calibration method and then in the global scope of the module.  No win is defined there. I think btw. this is most-likely a typo and should say self.win rather that win. (line 67).

HTH,
Denis


Many thanks for your help,

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/uY9VDY9dfPUJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Horea Christian

unread,
Mar 4, 2013, 10:44:28 AM3/4/13
to psychop...@googlegroups.com
line 67? In both files line 67 is commented. Should I import tobii mid-script then? or how would it be best to fix this?

Cheers,

Denis-Alexander Engemann

unread,
Mar 4, 2013, 11:28:19 AM3/4/13
to psychop...@googlegroups.com
Hi Chris,

sorry, it seems something went wrong with the Github diaplay on my mobile device.

I marked the line (in fact 109) here:

To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/MgCwe2z0L7wJ.

Horea Christian

unread,
Mar 4, 2013, 9:44:10 PM3/4/13
to psychop...@googlegroups.com
So,

 self.calresultmsg = psychopy.visual.TextStim(self.win,pos=(0,-self.win[1]/4))

instead of:

 self.calresultmsg = psychopy.visual.TextStim(self.win,pos=(0,-win.size[1]/4))

Also, would moving 

from letobii import TobiiController


to line 96 make it stop complaining about how win is not defined?

Denis-Alexander Engemann

unread,
Mar 5, 2013, 2:16:10 AM3/5/13
to psychop...@googlegroups.com
On Tue, Mar 5, 2013 at 3:44 AM, Horea Christian <horea....@gmail.com> wrote:
So,

 self.calresultmsg = psychopy.visual.TextStim(self.win,pos=(0,-self.win[1]/4))

instead of:

 self.calresultmsg = psychopy.visual.TextStim(self.win,pos=(0,-win.size[1]/4))


yes. 

(some background:

Python at this stages just checks, whether all names required for the definitions are available, it won't evaluate the code inside the class besides some formal syntactic integrity and name space checks. The code inside the class will be evaluated as you explicitly construct an instance of TobiiController at line 97 in your script where you pass 'win' to the class constructor and then call controller.doCalibration in you script which knows about win because 'win' then is a member of the 'controller' object (your instance of TobiiController). 
The reason it breaks now is that Python can't complete the class definition as the class code refers to a name (not an object) that does not exist in the method, class and module namespace. This does not mean that the win object has to exist already. Just for your understanding, the problem would also disappear if you added a win argument to TobiiController.doCalibration (which you should not do as it does not make sense from a design point of view).


)
 
Also, would moving 

from letobii import TobiiController


no. It should not matter where you put the import statement. The requirement is that the code in your module has to be correct. If the import will succeed it does not matter from where in a file you invoke the import as long as Python can find the file.

To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/ixQ0gXfy6lcJ.

Horea Christian

unread,
Mar 5, 2013, 10:19:58 AM3/5/13
to psychop...@googlegroups.com
So how can I make the code in my module correct? I don't want to define win in the module, because I'll want to use that module with other scripts where I may have another win. Also, why does the code work in this set-up? (where the module code also precedes the definition of win)

Denis-Alexander Engemann

unread,
Mar 5, 2013, 10:33:13 AM3/5/13
to psychop...@googlegroups.com
On Tue, Mar 5, 2013 at 4:19 PM, Horea Christian <horea....@gmail.com> wrote:
So how can I make the code in my module correct? I don't want to define win in the module, because I'll want to use that module with other scripts where I may have another win. Also, why does the code work in this set-up? (where the module code also precedes the definition of win)

On Tuesday, March 5, 2013 7:16:10 AM UTC, megaze wrote:


On Tue, Mar 5, 2013 at 3:44 AM, Horea Christian <horea....@gmail.com> wrote:
So,

 self.calresultmsg = psychopy.visual.TextStim(self.win,pos=(0,-self.win[1]/4))

instead of:

 self.calresultmsg = psychopy.visual.TextStim(self.win,pos=(0,-win.size[1]/4))


yes. 


Didn't you try to do this? It should be sufficient to resolve your trouble.
 
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/2kjR0VkEepkJ.

Jeremy Gray

unread,
Mar 5, 2013, 10:40:07 AM3/5/13
to psychop...@googlegroups.com
> So how can I make the code in my module correct? I don't want to define win
> in the module, because I'll want to use that module with other scripts where
> I may have another win.

just change (almost) all references to "win" to be "self.win" instead,
e.g., "-win.size[1]" --> "-self.win.size[1]". don't change the ones
in __init__() though, they are correct.

> Also, why does the code work in this set-up? (where the module code also precedes the definition of win)

Yes, as you said, this is a python question rather than PsychoPy
question. If you are unsure of the difference between a class and an
object of that class, this is a good time to do some homework :-) The
class definition specifies that a window needs to be provided at the
time that a new TobiiController object (i.e., an object, an instance
of the class) is created. The window does not need to be provided
before the blue-prints for the object (i.e., the class) is specified.

--Jeremy

Jeremy Gray

unread,
Mar 5, 2013, 10:51:37 AM3/5/13
to psychop...@googlegroups.com
ps. So this means that you will also need to change "win.flip()" to
"self.win.flip()" -- there are a lot of those. Python name spaces and
scope rules are another useful thing to know about.

Chris Cox

unread,
Mar 5, 2013, 11:23:30 AM3/5/13
to psychop...@googlegroups.com
As a demonstration of scope, try:
  1. = 1
  2. class aClass:
  3.     def __init__(self):
  4.         self.A = 2
  5.  
  6.     def aMethod(self):
  7.         A = 3
  8.         return A
  9.  
  10. anInstance = aClass()
  11. print anInstance.aMethod()
  12. print anInstance.A
  13. print A

   Calling the method, and defining A within the method, does not effect the instance property, nor does it effect the global variable.  Now, comment out line 7 and run again.  You should see that the method doesn't just fail... it looks for A locally, doesn't find it, and then looks globally.  It does not look within the instance properties.

FWIW,
Chris


--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

Chris Cox

unread,
Mar 5, 2013, 11:25:28 AM3/5/13
to psychop...@googlegroups.com
Wow, used effect wrong twice... ***cringe*** Sorry.

Horea Christian

unread,
Mar 11, 2013, 2:55:41 PM3/11/13
to psychop...@googlegroups.com
Hey there, I'm having another problem with using a premade file as a module. this time it doesn't seem related to namespaces, but rather to how it can/cannot import gtk

I have this code here ( https://github.com/TheChymera/E2att/tree/c4c4b5b95a98865f0e21f74bfedcb7b18f3324fe ), where I call lebrowser.py from experiments.py which I call from start.py. In any case, it fails with this error message ( http://paste2.org/p/3117469 ). However, just running this script http://pastebin.com/DgMnXjYB works perfectly. Incidentally it is identical to the lebrowser.py script which fails on my other set-up. Could you help me out with this? Basically import gtk suddenly stops working :-/ 

Chris Cox

unread,
Mar 11, 2013, 4:44:19 PM3/11/13
to psychop...@googlegroups.com
Hi Horea,

I don't really have an explanation, but I can demonstrate what might be going wrong. I don't want to send attachments to the list, but save each bit of this code to separate files as indicated and run start.py.  You'll see that A is imported into B, and within B I can access aClass... but when I import B into start, it actually treats A as a sub-module of B.  You'll see what I mean.  I don't know how to resolve your specific problem, but this might give some insight as to where things are breaking down.

HTH,
Chris

  1. # Save as start.py
  2. import B
  3. = B.A.aClass() # A.aClass() will fail!
  4. = B.bClass()
  5.  
  6. print "Called from start.py"
  7. print a.name
  8. print b.name
  9.  
  10.  
  11. # Save as B.py
  12. import A
  13. class bClass:
  14.     def __init__(self):
  15.         self.name = "I'm an instance of class B."
  16.  
  17. = A.aClass()
  18. print "Called from B.py:"
  19. print a.name
  20.  
  21.  
  22. # Save as A.py
  23. class aClass:
  24.     def __init__(self):
  25.         self.name = "I'm an instance of class A."


To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/BU3VXkPEZycJ.
Reply all
Reply to author
Forward
0 new messages