Pupil Labs import¶
Reading a Pupil Cloud (Neon) export and placing gaze, pupil and head motion on a video's frame clock; gazegrams as the gaze counterpart of motiongrams.
Read Pupil Labs Neon exports and put gaze, pupil and head motion on the video's clock.
Neon glasses record a scene camera, an eye camera, gaze at 200 Hz, pupil size, blinks, fixations, saccades and an inertial unit, and Pupil Cloud exports all of it as CSV files with one column of absolute UTC nanosecond timestamps. None of that is on the clock of any video the toolbox will analyse: the scene video starts a little after the recording (its first frame carries its own timestamp), a cut made for analysis starts later still, and the eye data run at their own rates. This module does the alignment once, so that gaze velocity, pupil diameter, blink rate and head rotation can sit in the same frame-indexed table as quantity of motion and be correlated, segmented and drawn with the same tools.
The clock is the important thing. Every Cloud export carries info.json with the
recording's start_time in nanoseconds; events.csv names moments on that clock
(recording.begin, and whatever the wearer's companion marked --- "Music begins"); and
the scene camera's frame times are in world_timestamps.csv. A video cut from the scene
recording is placed on the recording clock by naming the event it starts at, or by giving
its offset in seconds. Times in the exported frame table then count from the video's own
first frame, which is what every other MGT result does.
Two things the data do not say, and this module does not pretend they do:
- Gaze during a blink is the tracker's guess, so gaze position and velocity are set to missing for frames inside a blink, and pupil diameter is masked a few frames either side (the lids occlude the pupil before the detector reports a blink) and interpolated.
- The Cloud face detector reports faces in the scene image. On abstract painting it fired on the painting more often than on people (see the 2024 live-painting analysis this was written for), so face detections are read but not interpreted here; deciding what a face box means is the caller's job.
What is computed per frame: gaze position (scene pixels, and azimuth/elevation in
degrees), gaze angular velocity in degrees per second from consecutive 200 Hz samples on
the sphere, worn, fixation/saccade/blink flags with the export's ids, pupil diameter left,
right and mean, head gyroscope magnitude (deg/s), dynamic acceleration (|a| - 1 g, in g)
and roll/pitch/yaw. Per-second rates (fixations, saccades and blinks per second, mean
fixation duration, mean saccade amplitude, gaze dispersion) come from
:func:eyetracking_rates. A gazegram --- a histogram of gaze position per time bin,
drawn as an image with time across --- is the gaze counterpart of a motiongram, and lines
up with one when both are drawn on the same width.
Only pandas and numpy are needed; both are already hard dependencies.
EyeEvent
dataclass
¶
EyeEvent(kind, start, end, id=-1, features=dict())
One fixation, saccade or blink, in seconds on the video's clock.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
|
start |
float
|
Start, in seconds from the video's first frame. |
end |
float
|
End, in seconds. |
id |
int
|
The export's own id, so an event can be traced back to the CSV. |
features |
dict
|
What the export said about it: duration in ms, position for a fixation, amplitude and velocities for a saccade. |
PupilRecording
dataclass
¶
PupilRecording(folder, start_ns, duration_s, events, events_all, scene_size, gaze=None, eye_states=None, imu=None, fixations=None, saccades=None, blinks=None, world_timestamps=None, wearer='')
A Pupil Cloud export, read but not yet aligned to any video.
Attributes:
| Name | Type | Description |
|---|---|---|
folder |
Path
|
Where it was read from. |
start_ns |
int
|
Recording start on the absolute nanosecond clock, from info.json. |
duration_s |
float
|
Recording length in seconds, from info.json. |
events |
dict
|
Event name to seconds after |
events_all |
DataFrame
|
Every row of events.csv, with a |
scene_size |
tuple
|
Scene camera (width, height) in pixels. |
wearer |
str
|
The wearer's name from info.json, if any. |
offset_of ¶
offset_of(start)
Seconds after the recording start at which a video begins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
str | float
|
An event name from events.csv, or seconds. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The offset in seconds. |
Raises:
| Type | Description |
|---|---|
KeyError
|
when |
Source code in musicalgestures/_pupillabs.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
read_pupil_export ¶
read_pupil_export(folder)
Read a Pupil Cloud "Timeseries Data" export folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder
|
str | Path
|
The folder holding info.json, events.csv, gaze.csv and the other tables. Only info.json is required. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PupilRecording |
PupilRecording
|
The tables with seconds-after-start columns added. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
when the folder has no info.json, because without the start time nothing can be placed on a clock. |
Source code in musicalgestures/_pupillabs.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
pupil_to_frames ¶
pupil_to_frames(rec, fps, start=0.0, duration_s=None, blink_pad_frames=3)
Resample a recording onto the frames of a video that starts at start.
Each 200 Hz stream is binned to frames (median for positions and pupil size, mean for velocities and IMU magnitudes), event spans become per-frame flags, and pupil size is masked around blinks and interpolated. Frames with no sample keep NaN, which is the honest value for a gap: interpolating gaze across a dropped second would invent a fixation nobody made.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rec
|
PupilRecording
|
As returned by :func: |
required |
fps
|
float
|
The video's frame rate. |
required |
start
|
str | float
|
Where the video begins on the recording clock, as an event name from events.csv or as seconds after the recording start. Defaults to 0.0, the recording start. |
0.0
|
duration_s
|
float
|
Length of the video. Defaults to the rest of the recording. |
None
|
blink_pad_frames
|
int
|
Frames masked either side of a blink before the pupil series is interpolated. Defaults to 3. |
3
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per frame with |
DataFrame
|
frame), |
DataFrame
|
(deg/s), |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
source table is missing from the export are absent, not zero. |
Source code in musicalgestures/_pupillabs.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
eye_events ¶
eye_events(rec, kind, start=0.0, duration_s=None)
The fixations, saccades or blinks that fall inside a video, on its clock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rec
|
PupilRecording
|
The export. |
required |
kind
|
str
|
|
required |
start
|
str | float
|
Event name or seconds, as in :func: |
0.0
|
duration_s
|
float
|
Length of the video; defaults to the rest of the recording. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[EyeEvent]
|
:class: |
list[EyeEvent]
|
are kept, clipped to the video, because a blink that began a frame before the |
|
list[EyeEvent]
|
cut still happened in it. |
Source code in musicalgestures/_pupillabs.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
eyetracking_rates ¶
eyetracking_rates(frames, fps, bin_s=1.0)
Per-bin rates and means from a frame table: events per second, pupil, gaze spread.
Rates count event onsets (a fixation that spans two bins is counted once, where it began), which is what "fixations per second" means. Gaze dispersion is the standard deviation of the gaze position around the bin's mean, in scene pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames
|
DataFrame
|
From :func: |
required |
fps
|
float
|
The frame rate the table was made at. |
required |
bin_s
|
float
|
Bin length in seconds. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: Indexed by bin start time, with whichever of |
DataFrame
|
|
DataFrame
|
|
Source code in musicalgestures/_pupillabs.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
gazegram ¶
gazegram(frames, axis='y', size=None, bin_s=1.0, bins=120, normalise=True)
Histogram of gaze position per time bin, as an image with time across.
The gaze counterpart of a motiongram: where the motiongram's rows say where in the picture pixels changed, the gazegram's rows say where in the picture the wearer looked. Drawn at the same width the two line up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames
|
DataFrame
|
From :func: |
required |
axis
|
str
|
|
'y'
|
size
|
int
|
Scene extent along that axis in pixels; defaults to Neon's 1600 x 1200 scene camera. |
None
|
bin_s
|
float
|
Time bin in seconds. Defaults to 1.0. |
1.0
|
bins
|
int
|
Rows in the image. Defaults to 120. |
120
|
normalise
|
bool
|
Divide each column by its maximum, so a bin with few samples still shows where they were. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: |
ndarray
|
for |
Source code in musicalgestures/_pupillabs.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
mg_eyetracking ¶
mg_eyetracking(self, export_dir, start=0.0, blink_pad_frames=3, save_data=True, target_name=None, overwrite=True)
Align a Pupil Labs export to this video and keep the frame table on it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
export_dir
|
str | Path
|
The Pupil Cloud export folder. |
required |
start
|
str | float
|
Where this video begins on the recording clock: an event
name from events.csv ( |
0.0
|
blink_pad_frames
|
int
|
See :func: |
3
|
save_data
|
bool
|
Write the table as CSV beside the video. Defaults to True. |
True
|
target_name
|
str
|
Output path. Defaults to |
None
|
overwrite
|
bool
|
Overwrite or auto-increment. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: The frame table, also stored as |
DataFrame
|
recording as |
Source code in musicalgestures/_pupillabs.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |
mg_gazegrams ¶
mg_gazegrams(self, bins=120, bin_s=1.0, colormap='magma', target_name_x=None, target_name_y=None, overwrite=True)
Write the horizontal and vertical gazegrams of an aligned export as images.
Oriented like the motiongrams: the _ggy image has time across and vertical scene
position down; the _ggx image has time down and horizontal position across.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
int
|
Position bins. Defaults to 120. |
120
|
bin_s
|
float
|
Time bin in seconds. Defaults to 1.0. |
1.0
|
colormap
|
str
|
Matplotlib colormap. Defaults to |
'magma'
|
target_name_x
|
str
|
Path for the horizontal-position gazegram. |
None
|
target_name_y
|
str
|
Path for the vertical-position gazegram. |
None
|
overwrite
|
bool
|
Overwrite or auto-increment. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
MgList |
MgList
|
|
MgList
|
and |
Source code in musicalgestures/_pupillabs.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
mg_eyetracking_timeline ¶
mg_eyetracking_timeline(self, bin_s=1.0, dpi=120, title=None, target_name=None, overwrite=True)
Draw gaze velocity, event rates, pupil size and head rotation over the video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bin_s
|
float
|
Bin for the rates. Defaults to 1.0. |
1.0
|
dpi
|
int
|
Figure resolution. Defaults to 120. |
120
|
title
|
str
|
Figure title. Defaults to the video's name. |
None
|
target_name
|
str
|
Output path. Defaults to |
None
|
overwrite
|
bool
|
Overwrite or auto-increment. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
MgFigure |
MgFigure
|
With the per-bin table in |
MgFigure
|
|
Source code in musicalgestures/_pupillabs.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | |
head_turns ¶
head_turns(frames, fps, baseline_s=60.0, threshold_deg=25.0, min_duration_s=0.5)
Head yaw relative to its running median, and the spans where the head is turned away.
The IMU's yaw drifts and the wearer's "straight ahead" is wherever the work is, so an absolute heading means little. Relative to a slow running median it means "turned away from what she has been facing", which for a painter at an easel is the palette, the room or the musicians. The spans are not attributed --- the scene video says what was looked at.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames
|
DataFrame
|
From :func: |
required |
fps
|
float
|
Frame rate of the table. |
required |
baseline_s
|
float
|
Window of the running median. Defaults to 60 s. |
60.0
|
threshold_deg
|
float
|
Deviation counting as turned away. Defaults to 25°. |
25.0
|
min_duration_s
|
float
|
Shortest span kept. Defaults to 0.5 s. |
0.5
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per span with |
DataFrame
|
|
DataFrame
|
as |
Source code in musicalgestures/_pupillabs.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |