Skip to content

Commit

Permalink
Add reader studies
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsmkn committed May 15, 2024
1 parent 5faf8e6 commit 1eb0958
Showing 1 changed file with 149 additions and 19 deletions.
168 changes: 149 additions & 19 deletions scripts/pentest_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
import secrets
import string
from contextlib import contextmanager
Expand All @@ -23,9 +24,18 @@
ComponentInterface,
ComponentInterfaceValue,
)
from grandchallenge.direct_messages.models import Conversation, DirectMessage
from grandchallenge.evaluation.models import Phase
from grandchallenge.evaluation.utils import SubmissionKindChoices
from grandchallenge.invoices.models import Invoice
from grandchallenge.pages.models import Page
from grandchallenge.reader_studies.models import (
Answer,
DisplaySet,
Question,
QuestionWidgetKindChoices,
ReaderStudy,
)
from grandchallenge.verifications.models import Verification
from grandchallenge.workstations.models import Workstation

Expand All @@ -34,6 +44,7 @@
def run():
create_challenges()
create_archives()
create_reader_studies()


def create_challenges():
Expand All @@ -50,23 +61,48 @@ def create_archives():
create_archive(archive_num=archive_count + 1 + ii)


def create_reader_studies():
reader_study_count = ReaderStudy.objects.count()

for ii in range(2):
create_reader_study(reader_study_num=reader_study_count + 1 + ii)


def create_challenge(*, challenge_num):
admin = _create_user(f"challenge-{challenge_num}", "admin")
participant = _create_user(f"challenge-{challenge_num}", "participant")
participant_1 = _create_user(f"challenge-{challenge_num}", "participant-1")
participant_2 = _create_user(f"challenge-{challenge_num}", "participant-2")

archive = _create_phase_archive(
creator=admin,
interfaces=_get_inputs(),
title=f"Challenge {challenge_num}, Phase 1 Test Set",
)
create_direct_messages([(admin, participant_1), (admin, participant_2)])

c = Challenge.objects.create(
short_name=f"challenge-{challenge_num}",
creator=admin,
hidden=False,
logo=create_image_with_text(text=f"Challenge {challenge_num}, Logo"),
)
c.add_participant(participant)

c.add_participant(participant_1)
c.add_participant(participant_2)

Page.objects.create(
challenge=c,
display_title="all",
permission_level="ALL",
html="Should be visible by all",
)
Page.objects.create(
challenge=c,
display_title="reg",
permission_level="REG",
html="Should only be visible by this challenges participants",
)
Page.objects.create(
challenge=c,
display_title="adm",
permission_level="ADM",
html="Should only be visible by this challenges admins",
)

Invoice.objects.create(
challenge=c,
Expand All @@ -76,20 +112,30 @@ def create_challenge(*, challenge_num):
payment_status=Invoice.PaymentStatusChoices.PAID,
)

p = Phase.objects.create(
challenge=c,
title=f"Challenge {challenge_num} Phase 1",
algorithm_time_limit=300,
)
for ii, give_algorithm_editors_job_view_permissions in enumerate(
(True, False)
):
phase_num = ii + 1

archive = _create_phase_archive(
creator=admin,
interfaces=_get_inputs(),
title=f"Challenge {challenge_num}, Phase {phase_num} Test Set",
)

p.algorithm_inputs.set(_get_inputs())
p.algorithm_outputs.set(_get_outputs())
p = Phase.objects.create(
challenge=c,
title=f"Challenge {challenge_num} Phase {phase_num}",
algorithm_time_limit=300,
submission_kind=SubmissionKindChoices.ALGORITHM,
archive=archive,
score_jsonpath="score",
submissions_limit_per_user_per_period=10,
give_algorithm_editors_job_view_permissions=give_algorithm_editors_job_view_permissions,
)

p.submission_kind = SubmissionKindChoices.ALGORITHM
p.archive = archive
p.score_jsonpath = "score"
p.submissions_limit_per_user_per_period = 10
p.save()
p.algorithm_inputs.set(_get_inputs())
p.algorithm_outputs.set(_get_outputs())


def _get_inputs():
Expand Down Expand Up @@ -164,6 +210,70 @@ def add_archive_item(*, archive, interfaces):
ai.values.add(v)


def create_reader_study(*, reader_study_num):
editor = _create_user(f"reader-study-{reader_study_num}", "editor")
reader_1 = _create_user(f"reader-study-{reader_study_num}", "reader-1")
reader_2 = _create_user(f"reader-study-{reader_study_num}", "reader-2")

create_direct_messages([(editor, reader_1), (editor, reader_2)])

title = f"Reader Study {reader_study_num}"

interface = ComponentInterface.objects.get(slug="generic-medical-image")

reader_study = ReaderStudy.objects.create(
title=title,
workstation=Workstation.objects.last(),
logo=create_image_with_text(text=f"{title}, Logo"),
view_content={"main": [interface.slug]},
)

reader_study.add_editor(editor)
reader_study.add_reader(reader_1)
reader_study.add_reader(reader_2)

question = Question.objects.create(
reader_study=reader_study,
question_text=f"{title} question",
answer_type=Question.AnswerType.TEXT,
widget=QuestionWidgetKindChoices.TEXT_INPUT,
)

display_set = DisplaySet.objects.create(
reader_study=reader_study,
)

v = ComponentInterfaceValue.objects.create(interface=interface)

name = f"{reader_study.title}, Image 1, {interface.title}"

im = Image.objects.create(name=name, width=10, height=10)
im_file = ImageFile.objects.create(image=im)

with _uploaded_image_file(text=name) as f:
im_file.file.save(f"{im_file.pk}.mha", f)
im_file.save()

v.image = im
v.save()

display_set.values.add(v)

Answer.objects.create(
creator=reader_1,
question=question,
answer="Reader 1 answer",
display_set=display_set,
)

Answer.objects.create(
creator=reader_2,
question=question,
answer="Reader 2 answer",
display_set=display_set,
)


@contextmanager
def _uploaded_image_file(*, text):
jpeg_bytes = create_image_with_text(text=text)
Expand Down Expand Up @@ -271,3 +381,23 @@ def _create_user(first_name, last_name):
user.user_profile.save()

return user


def create_direct_messages(users):
fake = Faker()

for combination in users:
conversation = Conversation.objects.create()
conversation.participants.set(combination)

unread = random.choice([True, False])

for _ in range(5):
sender = random.choice(combination)
message = DirectMessage.objects.create(
conversation=conversation,
sender=sender,
message=fake.text(max_nb_chars=160),
)
if unread:
message.unread_by.set({*combination} - {sender})

0 comments on commit 1eb0958

Please sign in to comment.