Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Refactor Experiment to Block in backend (and partly in frontend) #1169

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backend/experiment/actions/consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def render_html_or_markdown(dry_text: str, render_format: str) -> str:
class Consent(BaseAction): # pylint: disable=too-few-public-methods
"""
Provide data for a view that ask consent for using the experiment data
- text: Uploaded file via experiment.consent (fileField)
- text: Uploaded file via block.consent (fileField)
- title: The title to be displayed
- confirm: The text on the confirm button
- deny: The text on the deny button
Expand All @@ -49,7 +49,7 @@ class Consent(BaseAction): # pylint: disable=too-few-public-methods
Relates to client component: Consent.js
"""

# default consent text, that can be used for multiple experiments
# default consent text, that can be used for multiple blocks
ID = 'CONSENT'

default_text = "Lorem ipsum dolor sit amet, nec te atqui scribentur. Diam \
Expand All @@ -62,11 +62,11 @@ class Consent(BaseAction): # pylint: disable=too-few-public-methods
contentiones, vix ex maiorum denique! Lorem ipsum dolor sit \
amet, nec te atqui scribentur. Diam molestie posidonium te sit, \
ea sea expetenda suscipiantur contentiones."

def __init__(self, text, title='Informed consent', confirm='I agree', deny='Stop', url=''):
# Determine which text to use
if text!='':
# Uploaded consent via file field: experiment.consent (High priority)
# Uploaded consent via file field: block.consent (High priority)
with text.open('r') as f:
dry_text = f.read()
render_format = get_render_format(text.url)
Expand Down
2 changes: 1 addition & 1 deletion backend/experiment/actions/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, session, title: str = None, score=None, score_message=None, c
self.session = session
self.title = title or _('Round {rounds_passed} / {total_rounds}').format(
rounds_passed=session.rounds_passed(),
total_rounds=self.session.experiment.rounds
total_rounds=self.session.block.rounds
)
self.score = score or session.last_score()
self.score_message = score_message or self.default_score_message
Expand Down
22 changes: 11 additions & 11 deletions backend/experiment/actions/tests/test_actions_consent.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
from django.test import TestCase
from django.core.files.uploadedfile import SimpleUploadedFile

from experiment.models import Experiment
from experiment.models import Block
from experiment.actions.consent import Consent


class ConsentTest(TestCase):

@classmethod
def setUpTestData(cls):
Experiment.objects.create(
def setUpTestData(cls):
Block.objects.create(
name='test_md',
slug='MARKDOWN',
consent=SimpleUploadedFile('consent.md', b'#test', content_type='text/html')
)
Experiment.objects.create(
Block.objects.create(
name='test_html',
slug='HTML',
consent=SimpleUploadedFile('consent.html', b'<h1>test</h1>', content_type='text/html')
)
Experiment.objects.create(
Block.objects.create(
name='test_template',
slug='TEMPLATE',
consent=SimpleUploadedFile('template.html', b'{% load i18n %}{% blocktranslate %}<p>test</p>{% endblocktranslate %}', content_type='text/html')
Expand All @@ -34,16 +34,16 @@ def test_html_rendering(self):
self.assertEqual(consent.text, '<h1>test</h1>')

def test_uploaded_markdown_rendering(self):
experiment = Experiment.objects.get(slug='MARKDOWN')
consent = Consent(experiment.consent)
block = Block.objects.get(slug='MARKDOWN')
consent = Consent(block.consent)
self.assertEqual(consent.text, '<h1>test</h1>')

def test_uploaded_html_rendering(self):
experiment = Experiment.objects.get(slug='HTML')
consent = Consent(experiment.consent)
block = Block.objects.get(slug='HTML')
consent = Consent(block.consent)
self.assertEqual(consent.text, '<h1>test</h1>')

def test_template_language_rendering(self):
experiment = Experiment.objects.get(slug='TEMPLATE')
consent = Consent(experiment.consent)
block = Block.objects.get(slug='TEMPLATE')
consent = Consent(block.consent)
self.assertEqual(consent.text, '<p>test</p>')
2 changes: 1 addition & 1 deletion backend/experiment/actions/tests/test_actions_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setUp(self):
self.mock_session.last_song.return_value = "Test Song"
self.mock_session.total_score.return_value = 50
self.mock_session.rounds_passed.return_value = 2
self.mock_session.experiment.rounds = 5
self.mock_session.block.rounds = 5

def test_initialization_full_parameters(self):
score = Score(
Expand Down
6 changes: 3 additions & 3 deletions backend/experiment/actions/tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from experiment.actions import Trial
from experiment.actions.wrappers import song_sync
from experiment.models import Experiment
from experiment.models import Block
from participant.models import Participant
from section.models import Playlist, Section
from session.models import Session
Expand All @@ -15,9 +15,9 @@ def setUp(self):
self.participant = Participant.objects.create()
self.section = Section.objects.create(
filename='some/audio/file.mp3', playlist=self.playlist)
self.experiment = Experiment.objects.create(name='TestExperiment')
self.block = Block.objects.create(name='TestBlock')
self.session = Session.objects.create(
experiment=self.experiment, participant=self.participant, playlist=self.playlist)
block=self.block, participant=self.participant, playlist=self.playlist)

def test_song_sync(self):
actions = song_sync(self.session, self.section, 'HookedTest')
Expand Down
6 changes: 3 additions & 3 deletions backend/experiment/actions/toontjehoger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ToontjeHoger(BaseAction): # pylint: disable=too-few-public-methods

ID = "TOONTJEHOGER"

def __init__(self, config, experiments=[]):
def __init__(self, config, blocks=[]):
"""
ToontjeHoger shows the ToontjeHoger homepage

Expand All @@ -21,7 +21,7 @@ def __init__(self, config, experiments=[]):
- main_button_url
- score_label
- supporters_intro
experiments: A list of ExperimentData objects
blocks: A list of ExperimentData objects
"""
self.config = config
self.experiments = [vars(i) for i in self.experiments]
self.blocks = [vars(i) for i in self.blocks]
2 changes: 1 addition & 1 deletion backend/experiment/actions/trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(

def action(self):
"""
Serialize data for experiment action
Serialize data for a block action

"""
# Create action
Expand Down
12 changes: 6 additions & 6 deletions backend/experiment/actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


def final_action_with_optional_button(session, final_text='', title=_('End'), button_text=_('Continue')):
""" given a session, a score message and an optional session dictionary from an experiment series,
return a Final.action, which has a button to continue to the next experiment if series is defined
""" given a session, a score message and an optional session dictionary from an experiment collection,
return a Final.action, which has a button to continue to the next block if series is defined
"""
collection_slug = session.load_json_data().get(COLLECTION_KEY)

Expand Down Expand Up @@ -49,7 +49,7 @@ def render_feedback_trivia(feedback, trivia):


def get_average_difference(session, num_turnpoints, initial_value):
"""
"""
return the average difference in milliseconds participants could hear
"""
last_turnpoints = get_last_n_turnpoints(session, num_turnpoints)
Expand All @@ -58,8 +58,8 @@ def get_average_difference(session, num_turnpoints, initial_value):
if last_result:
return float(last_result.section.song.name)
else:
# this cannot happen in DurationDiscrimination style experiments
# for future compatibility, still catch the condition that there may be no results
# this cannot happen in DurationDiscrimination style blocks
# for future compatibility, still catch the condition that there may be no results
return initial_value
return (sum([int(result.section.song.name) for result in last_turnpoints]) / last_turnpoints.count())

Expand All @@ -78,7 +78,7 @@ def get_average_difference_level_based(session, num_turnpoints, initial_value):
# no results right after the practice rounds
return initial_value
# Difference by level starts at initial value (which is level 1, so 20/(2^0)) and then halves for every next level
return sum([initial_value / (2 ** (int(result.section.song.name.split('_')[-1]) - 1)) for result in last_turnpoints]) / last_turnpoints.count()
return sum([initial_value / (2 ** (int(result.section.song.name.split('_')[-1]) - 1)) for result in last_turnpoints]) / last_turnpoints.count()


def get_fallback_result(session):
Expand Down
Loading
Loading