Calculating looking time duration output file

Hello PyschoPy community,

I have a simple task where I am recording gaze data with a tobii Fusion Eye tracker.
Each trial is composed of a Attention getter, a fixation cross and then a video.
The AOI is the video.
When we stop looking at the AOI, the next trial begins.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x): PsychoPy2021.2.3
Standard Standalone? (y/n) standalone

What are you trying to achieve?:
I would like to calculate the looking time duration at the video.
In the output file, I get the number of looks, then the start time(AOI.timesOn) of each look and the stop time (AOI.timesOff) of each look. To get the duration, I would like to calculate the stop time of the last look - the start time of the first look.
Does anyone know how to do that ?
I imagine I need to put a custom code, at the end of the routine.
Note also that I cannot calculate that by doing video_stop - video_started, I need to work only with looking times.

Many thanks in advance !

You could add some custom code to the end of the routine that has the roi in it:

# dwell_time is total amount of time looked within ROI
dwell_time = 0.0
for i, onTime in enumerate(roi.timesOn):
    dwell_time += roi.timesOff[i] - onTime
trials.addData('roi.dwellTime', dwell_time)

# look_period is the time between the first look start and the last look end.
look_period = 0.0
if roi.numLooks:
    look_period = roi.timesOff[-1] - roi.timesOn[0]
trials.addData('roi.lookPeriod', look_period)

This calculates roi.dwellTime as the amount of time spent within the ROI across all looks, and roi.lookPeriod as the last look end time - first look start time. Therefore roi.dwellTime <= roi.lookPeriod.

Thanks a lot Sol,

I actually found the second bit myself this morning but I hadn’t thought about the dwell time.
I am going to add it !

Many thanks!!

I’m also needing to calculate dwell and look times but I had data already collected to needed to do this on the output. Because of the formatting of the look times (comma separated times within brackets formatted as a string) it was a little tricky after the fact but I wrote a script that will do it. If anyone has a more efficient way of doing this please let me know.

data = data.fillna('')
data = data.groupby(["blocks.thisN","trial.thisN"]).max()
data = data.reset_index()


ROIs_on=['math_roi.timesOn', 'popup_roi.timesOn', 'pts_roi.timesOn', 'word_roi.timesOn', 'total_roi.timesOn']
ROIs_off=['math_roi.timesOff', 'popup_roi.timesOff', 'pts_roi.timesOff', 'word_roi.timesOff', 'total_roi.timesOff']
ROIs_dwell=['math_roi.dwell', 'popup_roi.dwell', 'pts_roi.dwell', 'word_roi.dwell', 'total_roi.dwell']

for i, roiOn in enumerate(ROIs_on):
    roiOff = ROIs_off[i]
    ondata = data[roiOn]
    offdata = data[roiOff]
    OnOffData = [ondata, offdata]
    onTimesArray = []
    offTimesArray = []
    outputs = [onTimesArray, offTimesArray]
    for j, lookdata in enumerate(OnOffData):
        for trial in lookdata:
            trial=trial.strip("[]")
            trial=trial.split(", ")
            times=[]
            for time in trial:
                try:
                    times.append(float(time))
                except Exception:
                    pass
            outputs[j].append(times)
    dwell_times = []
    for k in range(0, len(onTimesArray)):
        diffs = list()
        for on, off in zip (onTimesArray[k], offTimesArray[k]):
            diff = off - on
            diffs.append(diff)
        dwell_time = sum(diffs)
        dwell_times.append(dwell_time)
    data[roiOn] = onTimesArray
    data[roiOff] = offTimesArray
    data[ROIs_dwell[i]] = dwell_times

Hi,
Thank you for the information. This one has helped he a lot. I am not a Python coder and have been working with Builder. Is there a way to store pupil data (change in pupil size during simulus presentation) in the output file similar to the example you show here?
Appreciate the help!

To get pupil data you’ll need to save an hdf5 file (under the data tab of the settings). But to analyze this data you’ll need to use python or R.

1 Like