@author: Sol
"""
import string
import random
from psychopy import visual, core, event
from psychopy.iohub.util import NumPyRingBuffer
import pyglet.gl as gl

# Variables to control text string length etc.
text_length=160
chng_txt_each_flips=5
max_flip_count=60*10
display_resolution=1920,1080

# Circular buffers to store timing measures
stim1_txt_change_draw_times=NumPyRingBuffer(6000)
stim1_no_change_draw_times=NumPyRingBuffer(6000)
stim2_txt_change_draw_times=NumPyRingBuffer(6000)
stim2_no_change_draw_times=NumPyRingBuffer(6000)

# Some utility functions >>>
#
char_choices=string.ascii_uppercase+u"ùéèàç^ùèàç髼±£¢¤¬¦²³½¾°µ¯­±√∞≤≥±≠"

def getRandomString(slength):
    """
    Create a random text string of length 'slength' from the unichar values 
    in char_choices; then split the random text into 'words' of random length
    from [1,3,5,7,9].
    """
    s=u''.join(random.choice(char_choices) for i in range(slength))
Exemple #2
0
Values can be appended to the ring buffer and accessed using slice notation.

Any method of the numpy.array class can be called by a NumPyRingBuffer instance,
however numpy.array module funtions will not accept a NumPyRingBuffer as input.

@author: Sol
"""
from psychopy.iohub.util import NumPyRingBuffer

# Create a ring buffer with a maximum size of 10 elements. AS more than 10 elements
#   are added using append(x), each new element removed the oldeest element in
#   the buffer. The default data type is numpy.float32. To change the type to a
#   different numpy scalar value (for example numpy.uint, numpy ubyte, etc),
#   use the dtype parameter of NumPyRingBuffer.
ring_buffer = NumPyRingBuffer(10)

# Add 25 values to the ring buffer, between 1 and 25 inclusive.
# Print out info about the ring buffer and ring buffer contents state
#   after each element is added.
#
for i in xrange(1, 26):
    ring_buffer.append(i)
    print '-------'
    print 'Ring Buffer Stats:'
    print '\tWindow size: ', len(ring_buffer)
    print '\tMin Value: ', ring_buffer.min()
    print '\tMax Value: ', ring_buffer.max()
    print '\tMean Value: ', ring_buffer.mean()
    print '\tStandard Deviation: ', ring_buffer.std()
    print '\tFirst 3 Elements: ', ring_buffer[:3]
@author: Sol
"""
import string
import random
from psychopy import visual, core, event
from psychopy.iohub.util import NumPyRingBuffer
import pyglet.gl as gl

# Variables to control text string length etc.
text_length=160
chng_txt_each_flips=5
max_flip_count=60*10
display_resolution=1920,1080

# Circular buffers to store timing measures
stim1_txt_change_draw_times=NumPyRingBuffer(6000)
stim1_no_change_draw_times=NumPyRingBuffer(6000)
stim2_txt_change_draw_times=NumPyRingBuffer(6000)
stim2_no_change_draw_times=NumPyRingBuffer(6000)

# Some utility functions >>>
#
char_choices=string.ascii_uppercase+u"ùéèàç^ùèàç髼±£¢¤¬¦²³½¾°µ¯­±√∞≤≥±≠"

def getRandomString(slength):
    """
    Create a random text string of length 'slength' from the unichar values 
    in char_choices; then split the random text into 'words' of random length
    from [1,3,5,7,9].
    """
    s=u''.join(random.choice(char_choices) for i in range(slength))
from psychopy import visual, core, event
from psychopy.visual import textbox
from psychopy.iohub.util import NumPyRingBuffer
import pyglet.gl as gl
fm = textbox.getFontManager()
print(dir(fm))
print(fm.getFontFamilyNames())

# Variables to control text string length etc.
text_length=160
chng_txt_each_flips=5
max_flip_count=60*10
display_resolution=1920,1080

# Circular buffers to store timing measures
stim1_txt_change_draw_times=NumPyRingBuffer(6000)
stim1_no_change_draw_times=NumPyRingBuffer(6000)
stim2_txt_change_draw_times=NumPyRingBuffer(6000)
stim2_no_change_draw_times=NumPyRingBuffer(6000)

# Some utility functions >>>
#
char_choices=string.ascii_uppercase+u"ùéèàç^ùèàç髼±£¢¤¬¦²³½¾°µ¯­±√∞≤≥±≠"

def getRandomString(slength):
    """
    Create a random text string of length 'slength' from the unichar values 
    in char_choices; then split the random text into 'words' of random length
    from [1,3,5,7,9].
    """
    s=u''.join(random.choice(char_choices) for i in range(slength))
Values can be appended to the ring buffer and accessed using slice notation.

Any method of the numpy.array class can be called by a NumPyRingBuffer instance,
however numpy.array module funtions will not accept a NumPyRingBuffer as input.

@author: Sol
"""
from psychopy.iohub.util import NumPyRingBuffer

# Create a ring buffer with a maximum size of 10 elements. AS more than 10 elements
#   are added using append(x), each new element removed the oldeest element in
#   the buffer. The default data type is numpy.float32. To change the type to a
#   different numpy scalar value (for example numpy.uint, numpy ubyte, etc), 
#   use the dtype parameter of NumPyRingBuffer.   
ring_buffer=NumPyRingBuffer(10)

# Add 25 values to the ring buffer, between 1 and 25 inclusive.
# Print out info about the ring buffer and ring buffer contents state 
#   after each element is added.
#
for i in xrange(1,26):
    ring_buffer.append(i)
    print '-------'
    print 'Ring Buffer Stats:'
    print '\tWindow size: ',len(ring_buffer)
    print '\tMin Value: ',ring_buffer.min()
    print '\tMax Value: ',ring_buffer.max()
    print '\tMean Value: ',ring_buffer.mean()
    print '\tStandard Deviation: ',ring_buffer.std()
    print '\tFirst 3 Elements: ',ring_buffer[:3]
import pyglet.gl as gl

# Variables to control text string length etc.
text_length = 160
chng_txt_each_flips = 5
max_flip_count = 60 * 10
text_stim_types = [visual.TextBox, visual.TextBox2, visual.TextStim]
text_stim = []
stim_init_durations = {}
txt_change_draw_times = {}
no_change_draw_times = {}

for stype in text_stim_types:
    # Circular buffers to store timing measures
    cname = stype.__name__
    txt_change_draw_times[cname] = NumPyRingBuffer(max_flip_count)
    no_change_draw_times[cname] = NumPyRingBuffer(max_flip_count)

# Some utility functions >>>
#
char_choices = string.ascii_uppercase + "ùéèàç^ùèàç髼±£¢¤¬¦²³½¾°µ¯­±√∞≤≥±≠"


def getRandomString(slength):
    """
    Create a random text string of length 'slength' from the unichar values 
    in char_choices; then split the random text into 'words' of random length
    from [1,3,5,7,9].
    """
    s = u''.join(random.choice(char_choices) for i in range(slength))
    ns = u''
Exemple #7
0
    def processEyeTrackerData(self, dev_data):
        current_et_data = self.data_collection_state.get('eyetracker')
        if current_et_data is None:
            current_et_data = dev_data
            new_fields = {
                "eye_sample_type": [None, ''],
                "proportion_valid_samples": [None, ' %'],
                "time": [None, ' sec'],  # time of last sample received
                "rms_noise": [None, ' RMS'],
                "stdev_noise": [None, ' STDEV'],
            }
            current_et_data.update(new_fields)
            self.data_collection_state['eyetracker'] = current_et_data
            self.data_collection_state[
                'proportion_valid_samples'] = NumPyRingBuffer(int(
                    current_et_data["sampling_rate"][0]),
                                                              dtype=np.int8)
        else:
            current_et_data.update(dev_data)
        dcapp_config = self.data_collection_state['data_collection_config']
        new_samples = current_et_data.get('samples')[0]
        sampling_rate = float(current_et_data["sampling_rate"][0])

        if new_samples:
            latest_sample_event = new_samples[-1]
            current_et_data['time'][0] = latest_sample_event['time']
            tracking_eyes = dev_data.get('track_eyes')[0]
            eyename = 'BOTH'
            sample_type = current_et_data.get('eye_sample_type')[0]
            noise_win_size = dcapp_config.get('noise_calculation',
                                              {}).get("win_size", 0.2)
            noise_sample_count = int(sampling_rate * noise_win_size)

            if tracking_eyes == 'BINOCULAR_AVERAGED':
                sample_type = "Monocular"
                eyename = 'AVERAGED'
                if self.data_collection_state.get('right_eye') is None:
                    self.data_collection_state['right_eye'] = dict(
                        x=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        y=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        pupil=NumPyRingBuffer(noise_sample_count,
                                              dtype=np.float64))
                createAveragedEyeInfo(current_et_data)
            #----

            elif tracking_eyes == EyeTrackerConstants.LEFT_EYE:
                if self.data_collection_state.get('left_eye') is None:
                    createLeftEyeInfo(current_et_data)
                    self.data_collection_state['left_eye'] = dict(
                        x=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        y=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        pupil=NumPyRingBuffer(noise_sample_count,
                                              dtype=np.float64))
                eyename = 'LEFT'
                if sample_type is None:
                    sample_type = "Monocular"
            elif tracking_eyes <= EyeTrackerConstants.MONOCULAR:
                if self.data_collection_state.get('right_eye') is None:
                    self.data_collection_state['right_eye'] = dict(
                        x=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        y=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        pupil=NumPyRingBuffer(noise_sample_count,
                                              dtype=np.float64))
                    createRightEyeInfo(current_et_data)
                eyename = 'RIGHT'
                if sample_type is None:
                    sample_type = "Monocular"
            else:
                if self.data_collection_state.get('right_eye') is None:
                    createLeftEyeInfo(current_et_data)
                    createRightEyeInfo(current_et_data)
                    self.data_collection_state['left_eye'] = dict(
                        x=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        y=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        pupil=NumPyRingBuffer(noise_sample_count,
                                              dtype=np.float64))
                    self.data_collection_state['right_eye'] = dict(
                        x=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        y=NumPyRingBuffer(noise_sample_count,
                                          dtype=np.float64),
                        pupil=NumPyRingBuffer(noise_sample_count,
                                              dtype=np.float64))
                if sample_type is None:
                    sample_type = "Binocular"

            #----

            if sample_type is None:
                current_et_data['eye_sample_type'][0] = sample_type

            new_sample_count = len(new_samples)
            if tracking_eyes == 'BINOCULAR_AVERAGED':
                valid_samples = [v for v in new_samples if v['status'] != 22]
            else:
                valid_samples = [v for v in new_samples if v['status'] == 0]
            valid_sample_count = len(valid_samples)
            invalid_sample_count = new_sample_count - valid_sample_count

            #----

            s = None
            right_eye_buffers = None
            left_eye_buffers = None
            for s in valid_samples:
                if eyename == 'AVERAGED':
                    right_eye_buffers = self.data_collection_state['right_eye']
                    right_eye_buffers['x'].append(s['gaze_x'])
                    right_eye_buffers['y'].append(s['gaze_y'])
                    right_eye_buffers['pupil'].append(s['pupil_measure1'])
                elif eyename == 'BOTH':
                    rgx, rgy = s['right_gaze_x'], s['right_gaze_y']
                    lgx, lgy = s['left_gaze_x'], s['left_gaze_y']
                    right_eye_buffers = self.data_collection_state['right_eye']
                    right_eye_buffers['x'].append(rgx)
                    right_eye_buffers['y'].append(rgy)
                    right_eye_buffers['pupil'].append(
                        s['right_pupil_measure1'])
                    left_eye_buffers = self.data_collection_state['left_eye']
                    left_eye_buffers['x'].append(lgx)
                    left_eye_buffers['y'].append(lgx)
                    left_eye_buffers['pupil'].append(s['left_pupil_measure1'])
                elif eyename == 'RIGHT':
                    right_eye_buffers = self.data_collection_state['right_eye']
                    if sample_type == 'Binocular':
                        right_eye_buffers['x'].append(s['right_gaze_x'])
                        right_eye_buffers['y'].append(s['right_gaze_y'])
                        right_eye_buffers['pupil'].append(
                            s['right_pupil_measure1'])
                    else:
                        right_eye_buffers['x'].append(s['gaze_x'])
                        right_eye_buffers['y'].append(s['gaze_y'])
                        right_eye_buffers['pupil'].append(s['pupil_measure1'])
                else:  # LEFT
                    left_eye_buffers = self.data_collection_state['left_eye']
                    if sample_type == 'Binocular':
                        left_eye_buffers['x'].append(s['left_gaze_x'])
                        left_eye_buffers['y'].append(s['left_gaze_y'])
                        left_eye_buffers['pupil'].append(
                            s['left_pupil_measure1'])
                    else:
                        left_eye_buffers['x'].append(s['gaze_x'])
                        left_eye_buffers['y'].append(s['gaze_y'])
                        left_eye_buffers['pupil'].append(s['pupil_measure1'])

            def calcrms(x, axis=None):
                return float(np.sqrt(np.mean(np.power(x, 2.0),
                                             axis=axis))) / len(x)

            rms = None
            stdev = None

            if right_eye_buffers is not None and right_eye_buffers['x'].isFull(
            ):
                right_x = right_eye_buffers['x'].getElements()
                right_y = right_eye_buffers['y'].getElements()
                stdev = (right_x.std() + right_y.std()) / 2.0
                rms = (calcrms(right_x) + calcrms(right_y)) / 2.0

            if left_eye_buffers is not None and left_eye_buffers['x'].isFull():
                left_x = left_eye_buffers['x'].getElements()
                left_y = left_eye_buffers['y'].getElements()
                left_rms = (calcrms(left_x) + calcrms(left_y)) / 2.0
                left_stdev = (left_x.std() + left_y.std()) / 2.0
                if rms is not None:
                    rms = (rms + left_rms) / 2.0
                else:
                    rms = left_rms
                if stdev is not None:
                    stdev = (stdev + left_stdev) / 2.0
                else:
                    stdev = left_rms

            prop_vsamples = self.data_collection_state[
                'proportion_valid_samples']
            for i in range(valid_sample_count):
                prop_vsamples.append(1)
            for i in range(invalid_sample_count):
                prop_vsamples.append(0)

            prop_valid_samples = prop_vsamples.sum() / float(sampling_rate)

            current_et_data.get('samples')[0] = None
            if rms:
                current_et_data['rms_noise'][0] = rms
            if stdev:
                current_et_data['stdev_noise'][0] = stdev
            if prop_valid_samples:
                current_et_data['proportion_valid_samples'][0] = int(
                    prop_valid_samples * 100.0)

            if valid_samples:
                s = valid_samples[-1]
                status = 'TBC'

                if eyename == 'AVERAGED':
                    setAveragedEyeInfo(current_et_data, status, s)
                elif eyename == 'BOTH':
                    setLeftEyeInfo(current_et_data, status, s)
                    setRightEyeInfo(current_et_data, status, s)
                elif eyename == 'LEFT':
                    setLeftEyeInfo(current_et_data, status, s)
                else:
                    setRightEyeInfo(current_et_data, status, s)

            assert self.data_collection_state['eyetracker'] == current_et_data