Giter Site home page Giter Site logo

psychopy-docker's Introduction

PsychoPy running in a Docker container with Ubuntu

Instructions to create a docker container that runs PsychoPy on Ubuntu 21.04.

Please note that dockerfiles are work in progress. Tested so far only with an M1 MacBook Pro running macOS Monterey 12.3.1 and Docker 4.7.1. Dockerfile should also work on Intel computers.

1. Create docker image

# image with Ubuntu 21.04
# using Python venv with system-site-packages
# installs PsychoPy 2022.1.3
#
# GUI works but issues with PTB and ioHub
#
docker build -t psychopy -f Dockerfile/DockerfileSSP .

# image with Ubuntu 22.04
# PsychoPy 2022.1.2
#
# Unfortunately, PyQT5 fails to install.
# error: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel'
#
# docker build -t psychopy -f Dockerfile/Dockerfile .

Please note that it will take a while to create the docker image.

2. Install X11

macOS

Download and install latest XQuartz.

Enable Allow connections from network clients in X11 Preferences -> Security.

Add following line to file ~/.ssh/config.

XAuthLocation /opt/X11/bin/xauth

Enable indirect OpenGL rendering with X11 forwarding

defaults write org.macosforge.xquartz.X11 enable_iglx -bool true

Unfortunately, XQuartz only supports OpenGL 1.4.

3. Start PsychoPy in Docker container

Start PsychoPy in docker container with shared local folder and network.

In Terminal or iTerm app:

  1. Enable Xhost, which should start XQuartz on macOS

    xhost +
  2. Set DISPLAY variable to computer IP address

    Replace XXX.XXX.XXX.XXX with IP address of host computer. Find IP address of your computer with ifconfig.

    # https://stackoverflow.com/questions/8529181/which-terminal-command-to-get-just-ip-address-and-nothing-else
    ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2
    
    export DISPLAY=XXX.XXX.XXX.XXX:0
  3. Run docker container with shared folder and network

    docker run --rm -it \
        -v $(pwd):/usr/src/sharedfolder \
        -v /tmp/.X11-unix:/tmp/.X11-unix \
        -e PSYCH_XINITTHREADS=1 \
        -e XDG_RUNTIME_DIR=/tmp/ \
        -e RUNLEVEL=5 \
        --env="DISPLAY" \
        --net=host \
        --user psychopy \
        psychopy

    PsychoPy should now start.

Audio

Pass audio through to macOS

To get audio from the container passing through to macOS, you need to install pulseaudio.

brew install pulseaudio

# start pulseaudio daemon
pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon
#pulseaudio --start

# https://askubuntu.com/questions/14077/how-can-i-change-the-default-audio-device-from-command-line
# find audio output sources
pacmd list-sinks

# set default audio output on macOS using index
# pacmd set-default-sink <index>
pacmd set-default-sink 1

# defaults can also be changed in file `default.pa` 
# edit file to change pulseaudio settings
# pico $(brew --prefix pulseaudio)/etc/pulse/default.pa

Start container then with this command.

Note: replace XXX.XXX.XXX.XXX with ip address of your computer (host).

# set pulse server address
export PULSE_SERVER=XXX.XXX.XXX.XXX

docker run --rm -it \
    --privileged \
    -v $(pwd):/usr/src/sharedfolder \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -e PULSE_SERVER=$PULSE_SERVER \
    -e PULSE_COOKIE=/home/pulseaudio/.config/pulse/cookie \
    -v ~/.config/pulse/:/home/pulseaudio/.config/pulse/ \
    -e PSYCH_XINITTHREADS=1 \
    -e XDG_RUNTIME_DIR=/tmp/ \
    -e RUNLEVEL=2 \
    --env="DISPLAY" \
    --net=host \
    psychopy

Audio not yet working in PsychoPy but it works in other apps (e.g. Firefox). Check audio with Firefox running in the same Docker container (e.g. watch video on youtube).

docker run --rm -it \
    -v $(pwd):/usr/src/sharedfolder \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -e PULSE_SERVER=$PULSE_SERVER \
    -e PULSE_COOKIE=/home/pulseaudio/.config/pulse/cookie \
    -v ~/.config/pulse/:/home/pulseaudio/.config/pulse/ \
    -e PSYCH_XINITTHREADS=1 \
    -e XDG_RUNTIME_DIR=/tmp/ \
    -e RUNLEVEL=2 \    
    --env="DISPLAY" \
    --net=host \
    psychopy firefox

Issues

Errors:

PTB-CRITICAL: In call to PsychSetThreadPriority(): Failed to set new basePriority 2, tweakPriority 1, effective 1 [REALTIME] for thread (nil) provided!

PsychHID: KbQueueStart: Failed to switch to realtime priority [Operation not permitted].

RECORD extension not found. ioHub can not use python Xlib. Exiting....

Debug

  • Debug container if starting psychopy fails

    Start bash rather than psychopy.

    docker run --rm -it -v $(pwd):/usr/src/psychopy --env="DISPLAY" --net=host psychopy bash

    In container activate Python venv to start PsychoPy.

    source ~/venv/py3/bin/activate
    psychopy

psychopy-docker's People

Contributors

waltervanheuven avatar

Stargazers

 avatar

Watchers

 avatar

psychopy-docker's Issues

Had an issue, posting fix here for reference

I have newest ubuntu version jammy, 22.04 I think.
I had to add RUN sed -i -re 's/([a-z]{2}.)?archive.ubuntu.com|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
above the apt upgrade line in dockerfile otherwise apt upgrade resulted in error.
Hope it help someone :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.