Skip to content

Instantly share code, notes, and snippets.

@papr
Created June 27, 2022 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papr/4dea04ac8a4f72fe205dc4f25c6ad0ea to your computer and use it in GitHub Desktop.
Save papr/4dea04ac8a4f72fe205dc4f25c6ad0ea to your computer and use it in GitHub Desktop.
Pupil Player plugin to export template questions and answers from Pupil Invisible recordings
import json
import logging
import pathlib
from plugin import Plugin
from pupil_recording import PupilRecording, RecordingInfo
logger = logging.getLogger(__name__)
def _process_template(recording: pathlib.Path, export_path: pathlib.Path) -> None:
template_questions = json.loads((recording / "template.json").read_text())
info = json.loads((recording / "info.invisible.json").read_text())
template_responses = info["template_data"]
assert (
template_questions["id"] == template_responses["id"]
), "Template data is inconsistent"
merged = {
"name": template_questions["name"],
"description": template_questions["description"],
"template_id": template_questions["id"],
"Q&A": [
{
"question": next(
item
for item in template_questions["items"]
if item["id"] == question_id
),
"responses": responses,
}
for question_id, responses in template_responses["data"].items()
],
}
export_path = export_path / "template.json"
logger.info(f"Exporting template data to '{export_path}'")
export_path.write_text(json.dumps(merged, indent=4))
class Export_Template_Data(Plugin):
def on_notify(self, notification):
if notification["subject"] == "should_export":
_process_template(
pathlib.Path(self.g_pool.rec_dir),
pathlib.Path(notification["export_dir"]),
)
@classmethod
def is_available_within_context(cls, g_pool) -> bool:
if g_pool.app != "player":
# Plugin not available if not running in Player
return False
recording = PupilRecording(rec_dir=g_pool.rec_dir)
meta_info = recording.meta_info
if (
meta_info.recording_software_name
!= RecordingInfo.RECORDING_SOFTWARE_NAME_PUPIL_INVISIBLE
):
# Plugin not available if recording is not from Pupil Invisible
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment