Skip to content

Experiment serializers

experiment.serializers#

get_finished_session_count(block, participant) #

Get the number of finished sessions for this block and participant

Parameters:

Name Type Description Default
block Block

Block instance

required
participant Participant

Participant instance

required

Returns:

Type Description
int

Number of finished sessions for this block and participant

Source code in experiment/serializers.py
def get_finished_session_count(block: Block, participant: Participant) -> int:
    """Get the number of finished sessions for this block and participant

    Args:
        block: Block instance
        participant: Participant instance

    Returns:
        Number of finished sessions for this block and participant
    """

    return Session.objects.filter(block=block, participant=participant, finished_at__isnull=False).count()

get_started_session_count(block, participant) #

Get the number of started sessions for this block and participant

Parameters:

Name Type Description Default
block Block

Block instance

required
participant Participant

Participant instance

required

Returns:

Type Description
int

Number of started sessions for this block and participant

Source code in experiment/serializers.py
def get_started_session_count(block: Block, participant: Participant) -> int:
    """Get the number of started sessions for this block and participant

    Args:
        block: Block instance
        participant: Participant instance

    Returns:
        Number of started sessions for this block and participant
    """

    count = Session.objects.filter(block=block, participant=participant).count()
    return count

get_total_score(blocks, participant) #

Calculate total score of all blocks on the dashboard

Parameters:

Name Type Description Default
blocks list

All blocks on the dashboard

required
participant Participant

The participant we want the total score from

required

Returns:

Type Description
int

Total score of given the blocks for this participant

Source code in experiment/serializers.py
def get_total_score(blocks: list, participant: Participant) -> int:
    """Calculate total score of all blocks on the dashboard

    Args:
        blocks: All blocks on the dashboard
        participant: The participant we want the total score from

    Returns:
        Total score of given the blocks for this participant
    """

    total_score = 0
    for block in blocks:
        sessions = Session.objects.filter(block=block, participant=participant)

        for session in sessions:
            total_score += session.final_score
    return total_score

get_upcoming_block(phase, participant, times_played) #

return next block with minimum finished sessions for this participant if all blocks have been played an equal number of times, return None

Parameters:

Name Type Description Default
phase Phase

Phase for which next block needs to be picked

required
participant Participant

Participant for which next block needs to be picked

required
Source code in experiment/serializers.py
def get_upcoming_block(phase: Phase, participant: Participant, times_played: int) -> dict:
    """return next block with minimum finished sessions for this participant
    if all blocks have been played an equal number of times, return None

    Args:
        phase: Phase for which next block needs to be picked
        participant: Participant for which next block needs to be picked
    """
    blocks = list(phase.blocks.all())

    shuffle(blocks)
    finished_session_counts = [get_finished_session_count(block, participant) for block in blocks]

    min_session_count = min(finished_session_counts)
    if not phase.dashboard:
        if times_played != min_session_count:
            return None
    next_block_index = finished_session_counts.index(min_session_count)
    return serialize_block(blocks[next_block_index])

serialize_actions(actions) #

Serialize an array of actions

Source code in experiment/serializers.py
def serialize_actions(actions):
    """Serialize an array of actions"""
    if isinstance(actions, list):
        return [a.action() for a in actions]
    return actions.action()

serialize_block(block_object, language='en') #

Serialize block

Parameters:

Name Type Description Default
block_object Block

Block instance

required
language str

Language code

'en'

Returns:

Type Description
dict

Block info for a participant

Source code in experiment/serializers.py
def serialize_block(block_object: Block, language: str = "en") -> dict:
    """Serialize block

    Args:
        block_object: Block instance
        language: Language code

    Returns:
        Block info for a participant
    """

    return {
        "slug": block_object.slug,
        "name": block_object.name,
        "description": block_object.description,
        "image": serialize_image(block_object.image) if block_object.image else None,
    }

serialize_experiment(experiment) #

Serialize experiment

Parameters:

Name Type Description Default
experiment Experiment

Experiment instance

required

Returns:

Type Description
dict

Basic info about an experiment

Source code in experiment/serializers.py
def serialize_experiment(experiment: Experiment) -> dict:
    """Serialize experiment

    Args:
        experiment: Experiment instance

    Returns:
        Basic info about an experiment
    """

    translated_content = get_experiment_translated_content(experiment)

    serialized = {
        "slug": experiment.slug,
        "name": translated_content.name,
        "description": formatter(translated_content.description, filter_name="markdown"),
    }

    if translated_content.consent:
        serialized["consent"] = Consent(translated_content.consent).action()
    elif experiment.get_fallback_content() and experiment.get_fallback_content().consent:
        serialized["consent"] = Consent(experiment.get_fallback_content().consent).action()

    if experiment.theme_config:
        serialized["theme"] = serialize_theme(experiment.theme_config)

    if translated_content.about_content:
        serialized["aboutContent"] = formatter(translated_content.about_content, filter_name="markdown")
        serialized["backButtonText"] = _("Back")

    if translated_content.disclaimer:
        serialized["disclaimer"] = formatter(translated_content.disclaimer, filter_name="markdown")

    if translated_content.privacy:
        serialized["privacy"] = formatter(translated_content.privacy, filter_name="markdown")

    if hasattr(experiment, "social_media_config") and experiment.social_media_config:
        serialized["socialMedia"] = serialize_social_media_config(experiment.social_media_config)

    return serialized

serialize_phase(phase, participant, times_played) #

Serialize phase

Parameters:

Name Type Description Default
phase Phase

Phase instance

required
participant Participant

Participant instance

required

Returns:

Type Description
dict

A dictionary of the dashboard (if applicable), the next block, and the total score of the phase

Source code in experiment/serializers.py
def serialize_phase(phase: Phase, participant: Participant, times_played: int) -> dict:
    """Serialize phase

    Args:
        phase: Phase instance
        participant: Participant instance

    Returns:
        A dictionary of the dashboard (if applicable), the next block, and the total score of the phase
    """
    blocks = list(phase.blocks.order_by("index").all())

    next_block = get_upcoming_block(phase, participant, times_played)
    if not next_block:
        return None

    total_score = get_total_score(blocks, participant)
    if phase.randomize:
        shuffle(blocks)

    return {
        "dashboard": [serialize_block(block, participant) for block in blocks] if phase.dashboard else [],
        "nextBlock": next_block,
        "totalScore": total_score,
    }