Skip to content

Instantly share code, notes, and snippets.

@MSchnei
Created June 18, 2018 11:59
Show Gist options
  • Save MSchnei/bd282b1dbce85431ee61bbd955574279 to your computer and use it in GitHub Desktop.
Save MSchnei/bd282b1dbce85431ee61bbd955574279 to your computer and use it in GitHub Desktop.
Measure luminance levels of psychopy presentation
# -*- coding: utf-8 -*-
"""
Psychopy script to present and measure luminance levels (with a light meter).
Press buttons 1 and 2 to move from darker to brighter, or vice verser.
"""
from psychopy import visual, monitors, core, event
import numpy as np
# %%
"""GENERAL PARAMETERS"""
# set number of linear steps from black to white
steps = 17
# space linearly
colorArray = np.linspace(-1.0, 1.0, num=steps)
# repeat and reshape array so that it confirms to rgb triplet where r=g=b
colorArray = np.tile(colorArray, 3).reshape(3, -1).T
# %%
"""MONITOR"""
# set monitor information:
distanceMon = 99
widthMon = 30
PixW = 1920.0 # 1920.0
PixH = 1200.0 # 1080.0
moni = monitors.Monitor('testMonitor', width=widthMon, distance=distanceMon)
moni.setSizePix([PixW, PixH])
# set screen (make 'fullscr = True' for fullscreen)
mywin = visual.Window(
size=(PixW, PixH),
screen=0,
winType='pyglet', # winType : None, ‘pyglet’, ‘pygame’
allowGUI=False,
allowStencil=False,
fullscr=True, # for psychoph lab: fullscr = True
monitor=moni,
color = [0, 0, 0],
colorSpace= 'rgb',
units='pix',
blendMode='avg'
)
# %%
"""STIMULUS"""
# Squares
testStim1 = visual.GratingStim(
win=mywin,
tex=None,
units='pix',
size=(PixW, PixH),
colorSpace='rgb'
)
# Text
RGBText = visual.TextStim(
win=mywin,
color='green',
height=30,
units='pix',
opacity=1,
pos=(0, -PixH/2+50)
)
# %%
"""TIME"""
# give the system time to settle
core.wait(0.5)
# %%
"""RENDER_LOOP"""
trigCount = 0
textCount = 0
presentation = True
while presentation:
# set colour
testStim1.setColor(colorArray[trigCount])
testStim1.draw()
# update text text
StepText = 'Step %s out of %s' % (trigCount+1, steps)
RGBText.setText('RGB: ' + str(colorArray[trigCount]) + '\n' + StepText)
RGBText.draw()
mywin.flip()
# handle key presses each frame
for keys in event.getKeys():
# if 2 was pressed: increase RGB value
if keys[0] in ['2']:
if trigCount < steps-1:
trigCount = trigCount+1
# if 1 was pressed: decrease RGB value
if keys[0] in ['1']:
if trigCount > 0:
trigCount = trigCount-1
# if 4 was pressed: toggle RGB value shown/hidden
if keys[0] in ['4']:
textCount += 1
if textCount % 2 == 0:
RGBText.opacity = 0
RGBText.draw()
elif textCount % 2 == 1:
RGBText.opacity = 1
RGBText.draw()
if keys[0] in ['escape', 'q']:
presentation = False
mywin.close()
core.quit()
mywin.close()
core.quit()
@ingo-m
Copy link

ingo-m commented Jun 29, 2018

Luminance measurement data can be plotted & modelled with this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment