Getting input to psychopy from a Python script which performs animal tracking

Hi, I’m relatively new to Psychopy. I am comfortable with coding in Python and have written my own tracking software to track a single animal while performing tasks. I would like to “feed” tracking coordinates into Psychopy from my own script, so that I can design closed-loop experiments using Psychopy’s excellent visual stimulation characteristics. Has anyone done this before. From an in depth search I was unable to find out how to communicate with Psychopy from another independent script. I am using the standalone version of Psychopy, as the “pip” installation just won’t work on my machine. Thanks for any feedback!
Kindest regards. Mauro

It depends what you mean by “communicate with psychopy”. Running two scripts in parallel and having them communicate would be tricky. Is there any reason you can’t simply run your own Python script in PsychoPy and add stimulus presentation to it directly? If you need additional libraries that aren’t included in PsychoPy you can add them to the standalone install: Adding new python module to standalone psychopy

There’s also the option of making your script into a library and adding it to your standalone PsychoPy installation, which you could then import into a Python script, but that’s just a way of implementing what I describe above. It basically comes down to whether you can run your code in PsychoPy’s environment.

Thanks for your feedback Jonathan! I had thought of this type of solution. The problem is that, for a start, my script uses opencv-contrib-python (for the KCF tracking module) and not opencv-python, so really I can’t make it work inside Psychopy’s environment… On the other hand (working on MacOSX 11.7.2) I am unable to pip install Python 3.8, consequently I am also unable to pip install Psychopy, which leaves me with the impossibility of importing Psychopy functionality into my own Python script… On a side note: I did succeed in pip installing Python 3.8.16, but then Psychopy won’t pip install into that environment…That’s essentially why I was looking for a way of passing data between my script and Psychopy.

OK. I have found a way around the problem. On MacOSX 11.7 it is possible to install Python versions from 3.8.4+. So I installed Python version 3.8.4 using pyenv. Then I activated that version of Python using pyenv (I have many versions of Python installed on my machine for my other projects). I then used 'python -m venv .env" to create a virtual environment into which install Psychopy using pip. This worked. (It didn’t work for me with python version 3.8.16.) So now I can finally integrate the Psychopy functionality I need into my own scripts, because I have access to the Psychopy libraries. I’ll have a go at substituting the opencv-python (standard library) with opencv-contrib-python (because this library has the tracking algorithm I need, while standard opencv-python does not. I’ll keep you updated if this works (it should).

Update on that: substituted opencv-contrib-python and it works just fine. I’ll have to renounce on using the Tk interface (for a minimal GUI), because the version of Tk shipped with python 3.8.4 does not seem to be compatible with my version of MacOSX…

Further update: got closed-loop working like a breeze with the Psychopy dots in a dot-kinetogram changing direction of movement according to the orientation of my fly being tracked live. What I really wanted!

2 Likes