def _create_workstation(users):
    w = Workstation.objects.create(
        title=settings.DEFAULT_WORKSTATION_SLUG,
        logo=create_uploaded_image(),
        public=True,
    )
    w.add_editor(user=users["workstation"])
def _create_challenge(*, creator, participant, archive, suffix, inputs,
                      outputs):
    c = Challenge.objects.create(
        short_name=f"algorithm-evaluation-{suffix}",
        creator=creator,
        hidden=False,
        logo=create_uploaded_image(),
    )
    c.add_participant(participant)

    p = c.phase_set.first()

    p.algorithm_inputs.set(inputs)
    p.algorithm_outputs.set(outputs)

    p.title = "Algorithm Evaluation"
    p.submission_kind = p.SubmissionKind.ALGORITHM
    p.archive = archive
    p.score_jsonpath = "score"
    p.save()

    m = Method(creator=creator, phase=p)

    with _uploaded_container_image() as container:
        m.image.save("algorithm_io.tar", container)
def _create_archive(*, creator, interfaces, suffix, items=5):
    a = Archive.objects.create(
        title=f"Algorithm Evaluation {suffix} Test Set",
        logo=create_uploaded_image(),
        workstation=Workstation.objects.get(
            slug=settings.DEFAULT_WORKSTATION_SLUG),
    )
    a.add_editor(creator)

    for n in range(items):
        ai = ArchiveItem.objects.create(archive=a)
        for interface in interfaces:
            v = ComponentInterfaceValue.objects.create(interface=interface)

            im = Image.objects.create(
                name=f"Test Image {n}",
                width=10,
                height=10,
            )
            im_file = ImageFile.objects.create(image=im)

            with _uploaded_image_file() as f:
                im_file.file.save(f"test_image_{n}.mha", f)
                im_file.save()

            v.image = im
            v.save()

            ai.values.add(v)

    return a
 def _try_create_challenge(self,
                           user,
                           short_name,
                           description="test project"):
     url = reverse("challenges:create")
     data = {
         "short_name": short_name,
         "description": description,
         "logo": create_uploaded_image(),
         "banner": create_uploaded_image(),
         "prefix": "form",
         "page_set-TOTAL_FORMS": "0",
         "page_set-INITIAL_FORMS": "0",
         "page_set-MAX_NUM_FORMS": "",
     }
     self._login(user)
     response = self.client.post(url, data)
     return response
def create_archive(name, user):
    a = Archive.objects.get_or_create(
        title=name,
        defaults={
            "logo": create_uploaded_image(),
            "workstation": Workstation.objects.first(),
        },
    )[0]
    a.add_editor(user)
    return a
def _create_algorithm(*, creator, inputs, outputs, suffix):
    algorithm = Algorithm.objects.create(
        title=f"Test Algorithm Evaluation {suffix}",
        logo=create_uploaded_image(),
        use_flexible_inputs=True,
    )
    algorithm.inputs.set(inputs)
    algorithm.outputs.set(outputs)
    algorithm.add_editor(creator)

    algorithm_image = AlgorithmImage(creator=creator, algorithm=algorithm)

    with _uploaded_container_image() as container:
        algorithm_image.image.save("algorithm_io.tar", container)
def _create_reader_studies(users):
    reader_study = ReaderStudy.objects.create(
        title="Reader Study",
        workstation=Workstation.objects.last(),
        logo=create_uploaded_image(),
        description="Test reader study",
    )
    reader_study.editors_group.user_set.add(users["readerstudy"])
    reader_study.readers_group.user_set.add(users["demo"])

    question = Question.objects.create(
        reader_study=reader_study,
        question_text="foo",
        answer_type=Question.AnswerType.SINGLE_LINE_TEXT,
    )

    answer = Answer.objects.create(creator=users["readerstudy"],
                                   question=question,
                                   answer="foo")
    answer.images.add(Image.objects.first())
    answer.save()
def _create_algorithm_demo(users):
    cases_image = Image(
        name="test_image.mha",
        modality=ImagingModality.objects.get(modality="MR"),
        width=128,
        height=128,
        color_space="RGB",
    )
    cases_image.save()

    algorithm = Algorithm.objects.create(title="Test Algorithm",
                                         logo=create_uploaded_image())
    algorithm.editors_group.user_set.add(users["algorithm"], users["demo"])
    algorithm.users_group.user_set.add(users["algorithmuser"])
    algorithm.result_template = (
        "{% for key, value in results.metrics.items() -%}"
        "{{ key }}  {{ value }}"
        "{% endfor %}")
    detection_interface = ComponentInterface(
        store_in_database=False,
        relative_path="detection_results.json",
        slug="detection-results",
        title="Detection Results",
        kind=ComponentInterface.Kind.ANY,
    )
    detection_interface.save()
    algorithm.outputs.add(detection_interface)
    algorithm_image = AlgorithmImage(creator=users["algorithm"],
                                     algorithm=algorithm)

    if os.path.isfile(settings.DEMO_ALGORITHM_IMAGE_PATH):
        with open(settings.DEMO_ALGORITHM_IMAGE_PATH, "rb") as f:
            container = File(f)
            algorithm_image.image.save("test_algorithm.tar", container)
            algorithm_image.image_sha256 = settings.DEMO_ALGORITHM_SHA256
    else:
        container = ContentFile(base64.b64decode(b""))
        algorithm_image.image.save("test_algorithm.tar", container)

    algorithm_image.save()

    results = [
        {
            "cancer_score": 0.5
        },
        {
            "cancer_score": 0.6
        },
        {
            "cancer_score": 0.7
        },
    ]

    detections = [
        {
            "detected points": [{
                "type": "Point",
                "start": [0, 1, 2],
                "end": [3, 4, 5]
            }]
        },
        {
            "detected points": [{
                "type": "Point",
                "start": [6, 7, 8],
                "end": [9, 10, 11]
            }]
        },
        {
            "detected points": [{
                "type": "Point",
                "start": [12, 13, 14],
                "end": [15, 16, 17]
            }]
        },
    ]
    for res, det in zip(results, detections):
        _create_job_result(users, algorithm_image, cases_image, res, det)