reprostim.qr.psychopy

API to generate visual QR-codes and audio codes and embed them into PsychoPy scripts for fMRI experiments

Functions

get_iso_time(t)

Converts a timestamp into an ISO 8601 formatted string with local timezone.

get_times([t])

Get the current time and its ISO formatted string.

to_json(obj)

Convert an object to a JSON string.

Classes

EventType(value)

Enum for known event types for QR code.

QrCode(event, **kwargs)

Class to hold QR code data.

QrConfig([align, anchor, audio_codec, ...])

Class representing default QR and audio codes configuration and system properties.

QrStim(*args, **kwargs)

Class to represent/draw/play QR and audio code stimulus.

class reprostim.qr.psychopy.EventType(value)[source]

Enum for known event types for QR code.

MRI_TRIGGER_RECEIVED = 'trigger'

MRI trigger received event.

MRI_TRIGGER_WAITING = 'mri_trigger_waiting'

Waiting for MRI trigger event.

SERIES_END = 'series_end'

Series end event.

SERIES_START = 'series_begin'

Series start event.

SESSION_END = 'session_end'

Session end event.

SESSION_START = 'started'

Session start event.

class reprostim.qr.psychopy.QrCode(event: EventType, **kwargs: Any)[source]

Class to hold QR code data. Dict-based QR code record.

apply_keys(keys, keys_time=None)[source]

Apply keys and timestamp to the QR code data. :param keys: The keys to be applied to the QR code data. :param keys_time: Optional timestamp for the keys. If not provided, the current time will be used.

apply_times(t_prop: str, t_str_prop: str, t=None)[source]

Apply custom timestamp to the QR code data. :param t_prop: The key under which to store the timestamp. :param t_str_prop: The key under which to store the ISO formatted timestamp. :param t: Optional timestamp to be applied. If not provided, the current time will be used.

property event: EventType

Type of the event

property time: float

Time of the event, measured in seconds since the Epoch

property time_formatted: str

ISO 8601 time of the event

class reprostim.qr.psychopy.QrConfig(align: str = 'center', anchor: str = 'center', audio_codec: Any | None = None, audio_data_field: str = 'seq', audio_enabled: bool = False, audio_sample_rate: int = 44100, audio_volume: float = 0.8, back_color: str = 'white', border: int = 2, duration: float = 0.5, fill_color: str = 'black', padding: int = 10, retina_scale: float = 1.0, scale: float = 1.0)[source]

Class representing default QR and audio codes configuration and system properties.

align: str = 'center'

Alignment of the QR code, default is ‘center’ .

anchor: str = 'center'

Anchor position of the QR code, default is ‘center’ .

audio_codec: Any = None

Audio codec to use for audio code playback, default is AudioCodec.FSK, can be an AudioCodec enum when audio is enabled.

audio_data_field: str = 'seq'

Field in the QR code data to encode as audio data, default is ‘seq’ .

audio_enabled: bool = False

Flag to enable audio code playback, default is False .

audio_sample_rate: int = 44100

Audio sample rate for audio code playback, default is 44100 Hz .

audio_volume: float = 0.8

Audio playback volume. Should be a value between 0.0 and 1.0, where 1.0 is the maximum volume. Default is 0.8 .

back_color: str = 'white'

Background color of the QR code, default is ‘white’. Use ‘transparent’ for opaque background .

border: int = 2

Border size of the QR code, default is 2 .

duration: float = 0.5

Duration to display the QR code in seconds, default is 0.5 .

fill_color: str = 'black'

Fill color of the QR code, default is ‘black’ .

padding: int = 10

Window padding of QR code, default is 10.

pos = None

Position of the QR code center, default is (0.0, 0.0) .

retina_scale: float = 1.0

Retina scale factor for high-DPI displays, default is 1.0 .

scale: float = 1.0

Scale factor for the QR code size, default is 1.0 .

class reprostim.qr.psychopy.QrStim(*args: Any, **kwargs: Any)[source]

Class to represent/draw/play QR and audio code stimulus.

draw(win=None)[source]

Draw the QR code stimulus and play audio code if enabled. :param win: Optional PsychoPy window where the QR code will be displayed. If not provided, the window used during initialization will be used.

make_qr(data=None, **kwargs)[source]

Generate the QR code image from the QR code data. :param data: Optional data to encode in the QR code. If not provided, the QR code will be generated from the qr_code attribute. :param kwargs: Additional keyword arguments for the QRCode constructor. :return: The generated QR code image.

play_audio()[source]

Play the audio code if audio is enabled.

wait()[source]

Wait for the duration specified in the QR code configuration.

reprostim.qr.psychopy.get_iso_time(t)[source]

Converts a timestamp into an ISO 8601 formatted string with local timezone.

This function takes a timestamp in seconds and converts it into a datetime object with local timezone.

Parameters:

t (float) – The timestamp to be converted in seconds.

Returns:

The ISO 8601 formatted string with local timezone.

Return type:

str

reprostim.qr.psychopy.get_times(t=None)[source]

Get the current time and its ISO formatted string.

This function retrieves the current time in seconds since the epoch and formats it into an ISO 8601 string with local timezone.

Parameters:

t – Optional timestamp to be converted. If not provided, the current time will be used.

Returns:

A tuple containing the current time in seconds and its ISO formatted string.

Return type:

tuple[float, str]

reprostim.qr.psychopy.to_json(obj) str[source]

Convert an object to a JSON string.

This function checks if the provided object has a model_dump_json method (for Pydantic v2) and uses it to convert the object to a JSON string. If the method is not available, it falls back to using the standard json.dumps function.

Parameters:

obj (Any) – The object to be converted to a JSON string.

Returns:

The JSON string representation of the object.

Return type:

str