Пример #1
0
    def test_get_slices_from_session_invalid_type(self):
        dataset = backend.get_dataset('dataset1')
        sessions.create_categorical_image_session(db.session, 'session1', 'prompt', dataset, ['l1', 'l2', 'l3'])
        label_session = sessions.get_session_by_id(db.session, 1)

        with self.assertRaises(ValueError):
            sampling.get_slices_from_session(label_session)
Пример #2
0
def export_slices(session_id: int):
    with application.app_context():
        label_session = sessions.get_session_by_id(db.session, session_id)
        if label_session is None:
            print('Session with id {} not found'.format(session_id))
            return

        session_slices_dir_path = os.path.join(
            EXPORTED_SLICES_DIR_PATH, label_session.session_name + ' Slices')
        session_slices_dir_path = inc_dir_name(session_slices_dir_path)

        os.makedirs(session_slices_dir_path, exist_ok=True)

        slices = sampling.get_slices_from_session(label_session)
        dataset = backend.get_dataset(label_session.dataset)

        for sl in slices:
            d_img = backend.get_image(dataset, sl.image_name)
            sl_max = backend.get_image_info(d_img)[1]
            sl_img = backend.get_slice(d_img, sl.slice_index, sl.slice_type, 0,
                                       sl_max)

            save_name = '{}_{}_{}.png'.format(sl.image_name,
                                              sl.slice_type.name,
                                              sl.slice_index)
            save_path = os.path.join(session_slices_dir_path, save_name)

            sl_img.save(save_path)
            print('Saved {}'.format(save_path))
Пример #3
0
def create_categorical_slice_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)

    form = CreateCategoricalSliceSessionForm(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))

    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.')
        else:
            label_values = [v.strip() for v in form.label_values.data.split(',')]

            from_session = sessions.get_session_by_id(db.session, int(form.comparisons.data))
            slices = sampling.get_slices_from_session(from_session)

            sessions.create_categorical_slice_session(db.session, form.session_name.data, form.prompt.data,
                                                      dataset, label_values, slices)
            return redirect(url_for('dataset_overview', dataset_name=dataset.name))

    return render_template('create_categorical_slice_session.html',
                           dataset=dataset,
                           label_session_count=label_session_count,
                           form=form)
Пример #4
0
def get_thumbnails(
        label_session: LabelSession) -> Dict[backend.ImageSlice, ThumbData]:
    d_path = get_dataset_thumbnails_path(
        backend.get_dataset(label_session.dataset))
    slices = sampling.get_slices_from_session(label_session)

    thumbs_data = {}
    for sl in slices:
        path = os.path.join(d_path, get_thumbnail_name(sl))
        thumbs_data[sl] = ThumbData(path, os.path.exists(path))
    return thumbs_data
Пример #5
0
    def test_get_slices_from_session_categorical_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)
        ]
        sessions.create_categorical_slice_session(db.session, 'session1', 'prompt', dataset, ['l1', 'l2', 'l3'], slices)
        label_session = sessions.get_session_by_id(db.session, 1)

        check_slices = sampling.get_slices_from_session(label_session)
        self.assertEqual(check_slices, slices)
Пример #6
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))
Пример #7
0
def create_thumbnails(label_session: LabelSession):
    dataset = backend.get_dataset(label_session.dataset)
    slices = sampling.get_slices_from_session(label_session)

    session_thumbs_dir_path = get_session_thumbnails_dir_path(label_session)
    os.makedirs(session_thumbs_dir_path, exist_ok=True)

    for sl in slices:
        d_img = backend.get_image(dataset, sl.image_name)
        slice_thumb_path = os.path.join(session_thumbs_dir_path,
                                        backend.slice_name(sl) + '.jpg')
        backend.get_slice(d_img, sl.slice_index, sl.slice_type, 0,
                          None).save(slice_thumb_path)
    print('Created {} thumbnails for session {}'.format(
        len(slices), label_session.session_name))
Пример #8
0
def add_next_comparison(session: Session,
                        label_session: LabelSession) -> ComparisonAddResult:
    comparison_elements = [
        el for el in label_session.elements if el.image_2_name is not None
    ]
    if len(comparison_elements) > 0 and len(
            comparison_elements[-1].labels) == 0:
        return False, comparison_elements[
            -1], None  # There is already a pending comparison

    slices = sampling.get_slices_from_session(label_session)
    comparison_count = len(comparison_elements)

    new_comparison = None

    def compare(sl1: ImageSlice, sl2: ImageSlice):
        for comparison_el in comparison_elements:
            label = comparison_el.labels[-1].label_value
            comparison = sampling.get_comparison_from_element(comparison_el)
            if sl1 == comparison[0] and sl2 == comparison[1]:
                return {'First': 1, 'Second': -1, 'No Difference': 0}[label]
            elif sl1 == comparison[1] and sl2 == comparison[0]:
                return {'First': -1, 'Second': 1, 'No Difference': 0}[label]
        nonlocal new_comparison
        new_comparison = (sl1, sl2)
        raise ComparisonNotFound()

    try:
        slices_sorted = sorted(slices, key=functools.cmp_to_key(compare))
        return True, None, slices_sorted
    except ComparisonNotFound:
        assert new_comparison is not None
        new_comparison: Tuple[ImageSlice, ImageSlice]

        comparison_el = SessionElement(
            element_index=comparison_count,
            image_1_name=new_comparison[0].image_name,
            slice_1_index=new_comparison[0].slice_index,
            slice_1_type=new_comparison[0].slice_type.name,
            image_2_name=new_comparison[1].image_name,
            slice_2_index=new_comparison[1].slice_index,
            slice_2_type=new_comparison[1].slice_type.name)
        label_session.elements.append(comparison_el)
        session.commit()
        return False, comparison_el, None
Пример #9
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)
Пример #10
0
def rank_slices(
    label_session: LabelSession
) -> List[Tuple[ImageSlice, ComparisonRankResult]]:
    assert label_session.session_type == LabelSessionType.COMPARISON_SLICE.name

    rank_data = {
        sl: [0, 0, 0, 0, 0]
        for sl in sampling.get_slices_from_session(label_session)
    }

    for el, el_labels in labels.get_all_labels(label_session).items():
        if len(el_labels) == 0:
            continue
        comparison = sampling.get_comparison_from_element(el)
        latest_label = el_labels[-1].label_value

        data1 = rank_data[comparison[0]]
        data2 = rank_data[comparison[1]]

        # total_count
        data1[4] += 1
        data2[4] += 1

        if latest_label == 'First':
            data1[0] += 1  # score
            data2[0] -= 1  # score

            data1[1] += 1  # win_count
            data2[2] += 1  # loss_count
        elif latest_label == 'Second':
            data2[0] += 1  # score
            data1[0] -= 1  # score

            data2[1] += 1  # win_count
            data1[2] += 1  # loss_count
        else:
            data1[3] += 1  # draw_count
            data2[3] += 1  # draw_count

    return sorted([(sl, ComparisonRankResult(d[0], d[1], d[2], d[3], d[4]))
                   for sl, d in rank_data.items()],
                  key=lambda t: t[1].score,
                  reverse=True)
Пример #11
0
def create_sort_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 = CreateSortSessionForm(meta={'csrf': False})

    form.image_count.validators = [
        NumberRange(min=1, max=total_image_count, message='Must be between %(min)s and %(max)s (the dataset size).')
    ]
    for sess in sessions.get_sessions(db.session, dataset):
        t = sess.session_type
        if t in SLICE_SESSION_NAMES:
            form.slices_from.choices.append((str(sess.id), sess.session_name))

    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.min_slice_percent.data >= form.max_slice_percent.data:
            form.max_slice_percent.errors.append('Max must be greater than min.')
        else:
            if form.slices_from.data == 'create':
                slice_type = backend.SliceType[form.slice_type.data]
                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)
            else:
                from_session = sessions.get_session_by_id(db.session, int(form.slices_from.data))
                slices = sampling.get_slices_from_session(from_session)

            sessions.create_sort_slice_session(db.session, form.session_name.data, form.prompt.data, dataset, slices)
            return redirect(url_for('dataset_overview', dataset_name=dataset.name))
    return render_template('create_sort_session.html',
                           dataset=dataset,
                           label_session_count=label_session_count,
                           total_image_count=total_image_count,
                           form=form)
Пример #12
0
def create_thumbnails(label_session: LabelSession):
    dataset = backend.get_dataset(label_session.dataset)
    slices = sampling.get_slices_from_session(label_session)

    dataset_thumbs_path = get_dataset_thumbnails_path(dataset)
    os.makedirs(dataset_thumbs_path, exist_ok=True)

    created = 0
    for sl in slices:
        d_img = backend.get_image(dataset, sl.image_name)
        slice_thumb_path = os.path.join(dataset_thumbs_path,
                                        get_thumbnail_name(sl))
        if not os.path.exists(slice_thumb_path):
            img = backend.get_slice(d_img, sl.slice_index, sl.slice_type, 0,
                                    None, THUMB_MAX_PERCENTILE)
            img.save(slice_thumb_path)
            created += 1

    print('Created {} thumbnails for session {} (skipped {}, total {})'.format(
        created, label_session.session_name,
        len(slices) - created, len(slices)))
Пример #13
0
def get_thumbnail_names(label_session: LabelSession) -> List[str]:
    return [
        backend.slice_name(sl) + THUMB_EXTENSION
        for sl in sampling.get_slices_from_session(label_session)
    ]