Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Eye-tracking sentence reading: issues coordinating stimuli with ROIs

Hi there,

I am designing an experiment with Opensesame but my programming skills are very limited.

I would like to show the sentences, and at the same time define ROIs around each word.

I found useful code from another discussion: https://forum.cogsci.nl/discussion/4540/eye-tracking-sentence-reading-definition-of-areas-of-interest.

My problem is that I dont know how to adjust the rectangles to the size of the individual words, either the rectangle is too small or too big.

Here is a copy of the latest version I have of the project.

Thank you in advance for the help!

Ana

Comments

  • Hi @Ana_C ,

    This script indeed doesn't work properly anymore because the way that text is rendered has changed since this post (it changed from 3.2 to 3.3). To work around this, you need to draw one word at a time, rather than the entire string at once. I adjusted the original script (from the discussion you linked to) to do this, see below. You can adjust in turn so it fits into your own script.

    I am designing an experiment with Opensesame but my programming skills are very limited.

    Even when the presentation script is fixed, this will be a tricky dataset to analyze if you are not a proficient programmer. Therefore, before you start, carefully think all stres through, including how you are going to analyze this data once you've collected it!

    — Sebastiaan

    def draw_roi_text(c, text, x=0, y=0, show=False, **kwargs):
    
    
        """Draws text to a Canvas and additionally defines an ROI for each word and
        following space. The ROIs are named Canvas elements. The show keyword
        indicates whether the ROIs should be colored for visual debugging.
        """
    
    
        roi_x = x
        roi_y = y
        for i, word in enumerate(text.split()):
            # Words
            w, h = c.text_size(
                u'<span style="background:red;">%s</span>' % word,
                center=False,
                **kwargs
            )
            # Define ROI for word
            c['word%d' % i] = Rect(
                roi_x,
                roi_y,
                w,
                h,
                color='red' if show else var.background,
                fill=True
            )
            # Draw one word at a time
            c.text(word, x=roi_x, y=y, center=False, **kwargs)
            roi_x += w
            # Spaces
            w, h = c.text_size(
                u'<span style="background:red;">&nbsp;</span>',
                center=False,
                **kwargs
            )
            # Define ROI for word
            c['space%d' % i] = Rect(
                roi_x,
                roi_y,
                w,
                h,
                color='green' if show else var.background,
                fill=True
            )
            roi_x += w      
        # Don't draw the entire string at once
        # c.text(text, x=x, y=y, center=False, **kwargs)
    
    c = Canvas()
    draw_roi_text(c, 'This is a string', show=True)
    c.show()
    
Sign In or Register to comment.