SceneNamespace#

class pypillometry.eyedata.head_mounted.SceneNamespace(data=None)[source]#

Bases: Namespace[HeadMountedEyeData]

Methods for the scene video and the motion sensors of a head-mounted eye tracker, accessed as d.scene (e.g., d.scene.luminance()) on a HeadMountedEyeData object.

The transforming methods store their result as channels in d.aux and return the data object (see Namespace).

Methods

distance

Calculate the viewing distance from the 3D gaze point.

frame

The frame of the scene video that was shown at a time of the recording.

head_movement

Calculate the speed of the head rotation from the gyroscope.

luminance

Calculate the luminance of the scene video over time.

render_video

Write the scene video with the gaze position drawn on it.

Details

distance(window=500, inplace=None)[source]#

Calculate the viewing distance from the 3D gaze point.

The eye tracker calculates the point in space at which the lines of sight of the two eyes meet (gaze3d_x, gaze3d_y, gaze3d_z in d.aux, in mm from the scene camera). Its distance tells how far away the person looks, which matters for the pupil size because the pupil constricts when looking at near objects (near response). The distance of single samples is very noisy for far objects (the lines of sight are almost parallel), so it is smoothed with a running median of the vergence (1/distance).

The result is stored as "gaze_distance" (in mm) in d.aux; NaN where there is no 3D gaze point.

Parameters:
  • window (float, str or Quantity, optional) – width of the running median, e.g. 500 (ms), "1 sec"; default 500 ms. Use 0 for the distance of the single samples.

  • inplace (bool or None) – If True, make change in-place and return the object. If False, make and return copy before making changes. If None, use the object-level setting.

Return type:

HeadMountedEyeData

Returns:

the object with the new channel in aux (self or a copy, depending on inplace)

Raises:

ValueError – if the object has no 3D gaze point

Examples

>>> import pypillometry as pp
>>> d = pp.HeadMountedEyeData.from_tobii_prolab("rec.xlsx")
>>> d = d.scene.distance(window="1 sec")
frame(time, units='ms')[source]#

The frame of the scene video that was shown at a time of the recording.

Requires the optional package av.

Parameters:
  • time (float, str or Quantity) – time on the clock of the data (as in d.tx), e.g. 12000, "12 sec" or 12 * u.sec

  • units (str) – units of time if it is a plain number; default “ms”

Return type:

ndarray

Returns:

the frame as an RGB image (height x width x 3, uint8)

Raises:

ValueError – if the object has no scene video or the video has no frame at time

Examples

>>> import pypillometry as pp
>>> d = pp.HeadMountedEyeData.from_tobii_prolab("rec.xlsx")
>>> img = d.scene.frame("12 sec")
head_movement(inplace=None)[source]#

Calculate the speed of the head rotation from the gyroscope.

The result (the length of the vector gyro_x, gyro_y, gyro_z, in degrees per second) is stored as "head_speed" in d.aux. It can be used as a covariate or to find periods in which the head moved.

Parameters:

inplace (bool or None) – If True, make change in-place and return the object. If False, make and return copy before making changes. If None, use the object-level setting.

Return type:

HeadMountedEyeData

Returns:

the object with the new channel in aux (self or a copy, depending on inplace)

Raises:

ValueError – if the object has no gyroscope data

Examples

>>> import pypillometry as pp
>>> d = pp.HeadMountedEyeData.from_tobii_prolab("rec.xlsx")
>>> d = d.scene.head_movement()
luminance(radius=100, gamma=2.2, threads=2, inplace=None)[source]#

Calculate the luminance of the scene video over time.

Light is the strongest driver of pupil size, and outside the lab it changes with every movement of the head and the eyes. This method reads all frames of the scene video and stores two channels in d.aux that can be used as covariates of the pupil size:

  • "scene_luminance": mean luminance of the whole frame

  • "gaze_luminance": mean luminance in a circle around the gaze position (both_x, both_y); NaN where the gaze position is not usable or outside the frame

Each sample gets the value of the frame that was shown at its time (NaN outside the video). The values are between 0 (black) and 1 (white). They are relative: the scene camera adapts its exposure to the light, so that the same value can stand for different amounts of light at different times.

Requires the optional package av. All frames are decoded, which takes about a tenth of the duration of the video.

Parameters:
  • radius (float) – radius of the circle around the gaze position in pixels of the scene video; default 100 (about 5 degrees for Tobii Pro Glasses 3)

  • gamma (float) – the pixel values (0 to 1) are raised to this power before averaging, which undoes the gamma encoding of the video (approximately, values proportional to the light); default 2.2. Use 1 for the pixel values as they are.

  • threads (int) – number of processor cores used for reading the video. Default 2, so that the computer stays usable; more is faster, 0 uses all cores.

  • inplace (bool or None) – If True, make change in-place and return the object. If False, make and return copy before making changes. If None, use the object-level setting.

Return type:

HeadMountedEyeData

Returns:

the object with the new channels in aux (self or a copy, depending on inplace)

Raises:

ValueError – if the object has no scene video

Examples

>>> import pypillometry as pp
>>> d = pp.HeadMountedEyeData.from_tobii_prolab("rec.xlsx")
>>> d = d.scene.luminance()
>>> d.aux["gaze_luminance"]
render_video(fname, plot_range=(-inf, inf), trail=500, radius=30, color='red', pupil=True, pupil_alpha=0.3, audio=True, fps=None, threads=2, units='sec')[source]#

Write the scene video with the gaze position drawn on it.

Each frame gets a ring at the gaze position (both_x, both_y; the mean of the usable samples during the frame) and a fading line through the positions before it. Frames without a usable gaze position get no ring. A strip at the bottom of the frame shows the pupil size of each eye (usable samples) from 5 seconds before to 5 seconds after the frame; the white line in its middle is the time of the frame. The scale of the strip (the numbers at its right) is the same in all frames and leaves out the most extreme 1% of the values at each end. The video is useful for checking the data and the timing of the video relative to it (scene_video_offset).

Requires the optional package av. All frames are decoded and encoded again (H.264); a progress bar counts the frames. With the default settings, this takes about as long as the part of the video that is written. For a fast preview, write fewer frames (fps). An existing file fname is overwritten.

Parameters:
  • fname (str) – name of the video file to write (e.g., "gaze.mp4")

  • plot_range (tuple) – The time range to write, on the clock of the data (plain numbers are in units, or with units, e.g. (3, 5) * u.min). Default is (-np.inf, +np.inf), i.e. all data. Only the part of the video for which the object has samples is written.

  • trail (float, str or Quantity, optional) – duration of the line behind the gaze position, e.g. 500 (ms) or "1 sec"; default 500 ms. Use 0 for no line.

  • radius (float) – radius of the ring in pixels of the scene video; default 30

  • color (color, optional) – color of the ring and the line (any matplotlib color); default “red”

  • pupil (bool) – if True (default), draw the strip with the pupil size (if the object has pupil data); it covers the lower fifth of the frame

  • pupil_alpha (float) – opacity of the dark background of the strip, from 0 (no background, the traces are drawn on the scene) to 1 (black); default 0.3

  • audio (bool) – if True (default), copy the sound of the scene video (if any)

  • fps (Optional[float]) – frames per second of the written video. Default: as the scene video. With a lower value, only every n-th frame is written (e.g., every fifth frame for fps=5 and a scene video with 25 frames per second), which is faster by about this factor.

  • threads (int) – number of processor cores used for reading and for writing the video. Default 2, so that the computer stays usable; more is faster, 0 uses all cores.

  • units (str) – The units of plain numbers in plot_range (“ms”, “sec”, “min”, “h”). Default “sec”.

Return type:

str

Returns:

fname

Raises:

ValueError – if the object has no scene video or no gaze position, plot_range does not overlap with the data and the video, or fps is not positive

Examples

>>> import pypillometry as pp
>>> d = pp.HeadMountedEyeData.from_tobii_prolab("rec.xlsx")
>>> d.scene.render_video("gaze.mp4", (10, 30))
>>> d.scene.render_video("preview.mp4", fps=5, threads=4)