Ejemplo n.º 1
0
    def test_create_comparison_slice_session_elements_metadata(self):
        dataset = backend.get_dataset('dataset1')
        comparisons = [(ImageSlice('img1.nii.gz', 0, SliceType.SAGITTAL),
                        ImageSlice('img3', 1, SliceType.CORONAL)),
                       (ImageSlice('img2.nii', 0, SliceType.SAGITTAL),
                        ImageSlice('img3', 1, SliceType.AXIAL)),
                       (ImageSlice('img2.nii', 255, SliceType.CORONAL),
                        ImageSlice('img1.nii.gz', 100, SliceType.AXIAL))]
        sessions.create_comparison_slice_session(db.session, 'session1',
                                                 'test_prompt', dataset,
                                                 ['l1', 'l2', 'l3'],
                                                 comparisons)
        label_session = sessions.get_session_by_id(db.session, 1)
        session_elements = label_session.elements

        self.assertEqual(session_elements[0].image_1_name, 'img1.nii.gz')
        self.assertEqual(session_elements[0].image_2_name, 'img3')

        self.assertEqual(session_elements[1].slice_1_index, 0)
        self.assertEqual(session_elements[1].slice_2_index, 1)

        self.assertEqual(session_elements[2].slice_1_type,
                         SliceType.CORONAL.name)
        self.assertEqual(session_elements[2].slice_2_type,
                         SliceType.AXIAL.name)
Ejemplo n.º 2
0
    def test_create_comparison_slice_session_metadata(self):
        dataset = backend.get_dataset('dataset1')
        sessions.create_comparison_slice_session(db.session, 'session1',
                                                 'test_prompt', dataset,
                                                 ['l1', 'l2', 'l3'], [])
        label_session = sessions.get_session_by_id(db.session, 1)

        self.assertEqual(label_session.session_name, 'session1')
        self.assertEqual(label_session.session_type,
                         LabelSessionType.COMPARISON_SLICE.name)
        self.assertEqual(label_session.prompt, 'test_prompt')
        self.assertEqual(label_session.dataset, 'dataset1')
        self.assertEqual(label_session.label_values_str, 'l1,l2,l3')
Ejemplo n.º 3
0
def create_comparison_session(dataset_name: str):
    dataset = backend.get_dataset(dataset_name)
    if dataset is None:
        abort(400)

    current_sessions = sessions.get_sessions(db.session, dataset)
    label_session_count = len(current_sessions)

    images = backend.get_images(dataset)
    total_image_count = len(images)

    form = CreateComparisonSessionForm(meta={'csrf': False})

    comparison_sessions = sessions.get_sessions(db.session, dataset, LabelSessionType.COMPARISON_SLICE)
    for sess in comparison_sessions:
        form.comparisons.choices.append((str(sess.id), sess.session_name))

    form.image_count.validators = [
        ComparisonNumberRange(min=1, max=total_image_count,
                              message='Must be between %(min)s and %(max)s (the dataset size).')
    ]

    if form.validate_on_submit():
        if form.session_name.data in [se.session_name for se in current_sessions]:
            form.session_name.errors.append('Session name already in use.')
        elif form.comparisons.data == 'create' and form.min_slice_percent.data >= form.max_slice_percent.data:
            form.max_slice_percent.errors.append('Max must be greater than min.')
        else:
            slice_type = backend.SliceType[form.slice_type.data]
            if form.comparisons.data == 'create':
                slices = sampling.sample_slices(dataset, slice_type, form.image_count.data, form.slice_count.data,
                                                form.min_slice_percent.data, form.max_slice_percent.data)
                if form.comparison_count.data is None:
                    comparisons = sampling.all_comparisons(slices)
                else:
                    comparisons = sampling.sample_comparisons(slices, form.comparison_count.data,
                                                              form.max_comparisons_per_slice.data)
            else:
                from_session = sessions.get_session_by_id(db.session, int(form.comparisons.data))
                comparisons = sampling.get_comparisons_from_session(from_session)
            label_values = [v.strip() for v in form.label_values.data.split(',')]
            sessions.create_comparison_slice_session(db.session, form.session_name.data, form.prompt.data,
                                                     dataset, label_values, comparisons)
            return redirect(url_for('dataset_overview', dataset_name=dataset.name))

    return render_template('create_comparison_session.html',
                           dataset=dataset,
                           label_session_count=label_session_count,
                           total_image_count=total_image_count,
                           form=form)
Ejemplo n.º 4
0
    def test_get_ranking_contains_slices(self):
        dataset = backend.get_dataset('dataset1')
        comparisons = [
            (ImageSlice('img1.nii.gz', 0, SliceType.SAGITTAL), ImageSlice('img3', 1, SliceType.CORONAL)),
            (ImageSlice('img2.nii', 0, SliceType.SAGITTAL), ImageSlice('img3', 1, SliceType.AXIAL)),
            (ImageSlice('img2.nii', 255, SliceType.CORONAL), ImageSlice('img1.nii.gz', 100, SliceType.AXIAL))
        ]
        sessions.create_comparison_slice_session(db.session, 'session1', 'prompt', dataset,
                                                 ['l1', 'l2'], comparisons)
        label_session = sessions.get_session_by_id(db.session, 1)

        ranked_slices = ranking.rank_slices(label_session)
        check_slices = sampling.get_slices_from_session(label_session)

        self.assertEqual(set(ranked_slices), set(check_slices))
Ejemplo n.º 5
0
    def test_create_comparison_slice_session_elements_length(self):
        dataset = backend.get_dataset('dataset1')
        comparisons = [(ImageSlice('img1.nii.gz', 0, SliceType.SAGITTAL),
                        ImageSlice('img3', 1, SliceType.CORONAL)),
                       (ImageSlice('img2.nii', 0, SliceType.SAGITTAL),
                        ImageSlice('img3', 1, SliceType.AXIAL)),
                       (ImageSlice('img2.nii', 255, SliceType.CORONAL),
                        ImageSlice('img1.nii.gz', 100, SliceType.AXIAL))]
        sessions.create_comparison_slice_session(db.session, 'session1',
                                                 'test_prompt', dataset,
                                                 ['l1', 'l2', 'l3'],
                                                 comparisons)
        label_session = sessions.get_session_by_id(db.session, 1)
        session_elements = label_session.elements

        session_element_count = len(session_elements)
        self.assertEqual(session_element_count, 3)
Ejemplo n.º 6
0
    def test_get_comparisons_from_session(self):
        dataset = backend.get_dataset('dataset1')
        slices = [
            ImageSlice('img1.nii.gz', 0, SliceType.SAGITTAL),
            ImageSlice('img1.nii.gz', 100, SliceType.SAGITTAL),
            ImageSlice('img2.nii', 1, SliceType.CORONAL),
            ImageSlice('img3', 255, SliceType.AXIAL)
        ]
        comparisons = [(slices[0], slices[1]), (slices[2], slices[0]),
                       (slices[3], slices[1])]
        sessions.create_comparison_slice_session(db.session, 'session1',
                                                 'prompt', dataset,
                                                 ['l1', 'l2', 'l3'],
                                                 comparisons)
        label_session = sessions.get_session_by_id(db.session, 1)

        check_comparisons = sampling.get_comparisons_from_session(
            label_session)
        self.assertEqual(check_comparisons, comparisons)
Ejemplo n.º 7
0
    def test_get_slices_from_session_comparison_slice(self):
        dataset = backend.get_dataset('dataset1')
        slices = [
            ImageSlice('img1.nii.gz', 0, SliceType.SAGITTAL),
            ImageSlice('img1.nii.gz', 100, SliceType.SAGITTAL),
            ImageSlice('img2.nii', 1, SliceType.CORONAL),
            ImageSlice('img3', 255, SliceType.AXIAL)
        ]
        comparisons = [(slices[0], slices[1]), (slices[2], slices[0]),
                       (slices[3], slices[1])]
        sessions.create_comparison_slice_session(db.session, 'session1',
                                                 'prompt', dataset,
                                                 ['l1', 'l2', 'l3'],
                                                 comparisons)
        label_session = sessions.get_session_by_id(db.session, 1)
        check_slices = sampling.get_slices_from_session(label_session)

        # Note: Result from get_slices_by_session is sorted, so this relies on slices being defined in order above
        self.assertEqual(check_slices, slices)
Ejemplo n.º 8
0
    def test_get_sessions_by_type_length(self):
        dataset = backend.get_dataset('dataset1')

        sessions.create_categorical_image_session(db.session, 'session1',
                                                  'prompt', dataset,
                                                  ['l1', 'l2', 'l3'])
        sessions.create_categorical_image_session(db.session, 'session2',
                                                  'prompt', dataset,
                                                  ['l1', 'l2', 'l3'])

        sessions.create_categorical_slice_session(db.session, 'session3',
                                                  'prompt', dataset,
                                                  ['l1', 'l2', 'l3'], [])
        sessions.create_categorical_slice_session(db.session, 'session4',
                                                  'prompt', dataset,
                                                  ['l1', 'l2', 'l3'], [])

        sessions.create_comparison_slice_session(db.session, 'session5',
                                                 'prompt', dataset,
                                                 ['l1', 'l2'], [])

        sessions.create_categorical_image_session(db.session, 'session6',
                                                  'prompt', dataset,
                                                  ['l1', 'l2', 'l3'])
        sessions.create_categorical_image_session(db.session, 'session7',
                                                  'prompt', dataset,
                                                  ['l1', 'l2', 'l3'])

        categorical_image_sessions = sessions.get_sessions(
            db.session, dataset, LabelSessionType.CATEGORICAL_IMAGE)
        categorical_image_session_count = len(categorical_image_sessions)

        categorical_slice_sessions = sessions.get_sessions(
            db.session, dataset, LabelSessionType.CATEGORICAL_SLICE)
        categorical_slice_session_count = len(categorical_slice_sessions)

        comparison_slice_sessions = sessions.get_sessions(
            db.session, dataset, LabelSessionType.COMPARISON_SLICE)
        comparison_slice_session_count = len(comparison_slice_sessions)

        self.assertEqual(categorical_image_session_count, 4)
        self.assertEqual(categorical_slice_session_count, 2)
        self.assertEqual(comparison_slice_session_count, 1)
Ejemplo n.º 9
0
    def test_export_session_json_elements_metadata(self):
        dataset = backend.get_dataset('dataset1')
        comparisons = [(ImageSlice('img1.nii.gz', 0, SliceType.SAGITTAL),
                        ImageSlice('img3', 1, SliceType.CORONAL)),
                       (ImageSlice('img2.nii', 0, SliceType.SAGITTAL),
                        ImageSlice('img3', 1, SliceType.AXIAL)),
                       (ImageSlice('img2.nii', 255, SliceType.CORONAL),
                        ImageSlice('img1.nii.gz', 100, SliceType.AXIAL))]
        sessions.create_comparison_slice_session(db.session, 'session1',
                                                 'test_prompt', dataset,
                                                 ['l1', 'l2', 'l3'],
                                                 comparisons)
        label_session = sessions.get_session_by_id(db.session, 1)

        session_json = sessions.export_session_json(label_session)
        elements_json = session_json['elements']

        self.assertEqual(elements_json[0],
                         '0,img1.nii.gz,SAGITTAL,0,img3,CORONAL,1')
        self.assertEqual(elements_json[1],
                         '1,img2.nii,SAGITTAL,0,img3,AXIAL,1')
        self.assertEqual(elements_json[2],
                         '2,img2.nii,CORONAL,255,img1.nii.gz,AXIAL,100')