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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
163
164
165
166
167
168
169
170
171
172
173
174
175
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def get_upcoming_block(phase: Phase, participant: Participant, times_played: int):
    """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
16
17
18
19
20
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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")

    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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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,
    }

serialize_social_media_config(social_media_config, score=0)

Serialize social media config

Parameters:

Name Type Description Default
social_media_config SocialMediaConfig

SocialMediaConfig instance

required

Returns:

Type Description
dict

Basic social media info

Source code in experiment/serializers.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def serialize_social_media_config(
    social_media_config: SocialMediaConfig,
    score: Optional[float] = 0,
) -> dict:
    """Serialize social media config

    Args:
        social_media_config: SocialMediaConfig instance

    returns:
        Basic social media info
    """

    return {
        "tags": social_media_config.tags or ["amsterdammusiclab", "citizenscience"],
        "url": social_media_config.url,
        "content": social_media_config.get_content(score),
        "channels": social_media_config.channels or ["facebook", "twitter"],
    }