Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cortical-iv/be93307ffa870c4abef6cf63090d8b9f to your computer and use it in GitHub Desktop.
Save cortical-iv/be93307ffa870c4abef6cf63090d8b9f to your computer and use it in GitHub Desktop.
import numpy as np
from direct.showbase.ShowBase import ShowBase
from panda3d.core import Texture, CardMaker, TextureStage
from panda3d.core import WindowProperties
def rgb_texture(texture_size = 512, rgb = (0, 0, 0)):
x = np.linspace(-texture_size/2, texture_size/2, texture_size)
y = np.linspace(-texture_size/2, texture_size/2, texture_size)
X, Y = np.meshgrid(x, y)
rgb_texture = np.zeros((texture_size, texture_size, 3), dtype = np.uint8)
rgb_texture[..., 0] = rgb[0]
rgb_texture[..., 1] = rgb[1]
rgb_texture[..., 2] = rgb[2]
return rgb_texture
class RgbShow(ShowBase):
def __init__(self, texture_array):
super().__init__()
self.texture_array = texture_array
self.texture = Texture("stimulus")
self.texture.setup2dTexture(512, 512, Texture.T_unsigned_byte, Texture.F_rgb8)
self.texture.setRamImageAs(self.texture_array, "RGB")
self.texture_stage = TextureStage("rgb")
cm = CardMaker('card')
cm.setFrameFullscreenQuad()
self.card = self.aspect2d.attachNewNode(cm.generate())
self.card.setTexture(self.texture_stage, self.texture)
class SequenceShow(ShowBase):
def __init__(self, sequence_structure):
super().__init__()
self.stim_classes = experiment_structure['stim_classes']
self.stim_values = experiment_structure['stim_values']
self.stim_durations = experiment_structure['stim_durations']
self.stim_change_times = np.cumsum(self.stim_durations) #times to switch
self.current_stim_num = 0
self.active_stim = self.current_stim_class
self.bgcolor = (0.5, 0.5, 0.5, 1)
#Window properties
self.windowProps = WindowProperties()
self.windowProps.setSize(512, 512)
#Create scenegraph
cm = CardMaker('card')
cm.setFrameFullscreenQuad()
self.card = self.aspect2d.attachNewNode(cm.generate())
self.card.setScale(np.sqrt(8))
self.card.setColor(self.bgcolor) #make this an add mode
#Set initial texture
self.taskMgr.add(self.set_stim_task, "set_stimulus_class")
@property
def current_stim_ind(self):
"""
returns index, from stim list, of current stimulus
typical events = [3 1 3 0 3 3 1 3 2] so self.current_stim_ind[2] = 3
"""
return self.stimuli[self.current_stim_num]
@property
def current_stim_class(self):
"""
returns actual value of current stimulus
"""
return self.stim_classes[self.current_stim_ind]
@property
def next_change_time(self):
"""
returns time of next stimulus toggle
"""
return self.stim_change_times[self.current_stim_num]
def set_stim_task(self, task):
if task.time <= self.stim_change_times[-1]:
if task.time >= self.next_change_time:
#self.card.clearTexture(self.active_stim.texture_stage) #turn off stage
self.active_stim.destroy()
self.current_stim_num += 1
print(type(self.current_stim_class))
self.active_stim = self.current_stim_class
self.active_stim.run()
return task.cont
else:
return task.done
if __name__ == "__main__":
example = 'seq' #0: red, 1: green, 2: red/green alternating
red_tex = rgb_texture(rgb = (255, 0, 0))
green_tex = rgb_texture(rgb = (0, 255, 0))
if example == 'red':
red_screen = RgbShow(red_tex)
red_screen.run()
elif example == 'green':
green_screen = RgbShow(green_tex)
green_screen.run()
elif 'seq':
red_screen = RgbShow(red_tex)
green_screen = RgbShow(green_tex)
stim_classes = {0: red_screen, 1: green_screen}
stim_values = [0, 1, 0, 1, 0]
stim_durations = [1, 2, 3, 1, 2]
experiment_structure = {'stim_classes': stim_classes,
'stim_values': stim_values,
'stim_durations': stim_durations}
print("both not done yet")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment