Example #1
0
def get_comparisons_from_session(
        label_session: LabelSession) -> List[Tuple[ImageSlice, ImageSlice]]:
    comparisons = []
    for el in label_session.elements:
        comparisons.append((ImageSlice(el.image_1_name, el.slice_1_index,
                                       SliceType[el.slice_1_type]),
                            ImageSlice(el.image_2_name, el.slice_2_index,
                                       SliceType[el.slice_2_type])))
    return comparisons
Example #2
0
    def test_sample_comparisons_max_not_enough(self):
        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)
        ]

        with self.assertRaises(AssertionError):
            comparisons = sampling.sample_comparisons(slices, 100, 2)
Example #3
0
    def test_sample_comparisons_self_compare(self):
        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 = sampling.sample_comparisons(slices, 3, None)

        for co in comparisons:
            self.assertNotEqual(co[0], co[1])
Example #4
0
    def test_sample_comparisons_length(self):
        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 = sampling.sample_comparisons(slices, 3, None)

        comparison_count = len(comparisons)
        self.assertEqual(comparison_count, 3)
Example #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)
Example #6
0
    def test_create_categorical_slice_session_elements_length(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',
                                                  'test_prompt', dataset,
                                                  ['l1', 'l2', 'l3'], slices)
        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, 4)
Example #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)
Example #8
0
def get_slices_from_session(label_session: LabelSession) -> List[ImageSlice]:
    if label_session.session_type == LabelSessionType.CATEGORICAL_SLICE.name:
        slices = [ImageSlice(el.image_1_name, el.slice_1_index, SliceType[el.slice_1_type])
                  for el in label_session.elements]

    elif label_session.session_type == LabelSessionType.COMPARISON_SLICE.name:
        comparisons = get_comparisons_from_session(label_session)
        slices = [co[0] for co in comparisons] + [co[1] for co in comparisons]
        slices = set(slices)
        slices = sorted(slices, key=lambda sl: sl.image_name + sl.slice_type.name + str(sl.slice_index))

    elif label_session.session_type == LabelSessionType.SORT_SLICE.name:
        slices = [ImageSlice(el.image_1_name, el.slice_1_index, SliceType[el.slice_1_type])
                  for el in label_session.elements if not el.is_comparison()]

    else:
        raise ValueError('Invalid session type {}'.format(label_session.session_type))

    return slices
Example #9
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)
Example #10
0
def sample_slices(dataset: Dataset, slice_type: SliceType, image_count: int, slice_count: int,
                  min_slice_percent: int, max_slice_percent: int) -> List[ImageSlice]:
    images = random.sample(backend.get_images(dataset), image_count)
    slices: List[ImageSlice] = []

    for i in range(slice_count):
        im: DataImage = random.choice(images)
        im_slice_max = get_volume_width(im.path, slice_type)

        slice_min = int(im_slice_max * (min_slice_percent / 100))
        slice_max = int(im_slice_max * (max_slice_percent / 100))

        if slice_min == slice_max:
            sl = slice_max
        else:
            sl = random.randrange(slice_min, slice_max)
        slices.append(ImageSlice(im.name, sl, slice_type))

    slices = list(set(slices))  # Remove duplicates
    return slices
Example #11
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))
Example #12
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')
Example #13
0
def get_comparison_from_element(el: SessionElement) -> Tuple[ImageSlice, ImageSlice]:
    return (ImageSlice(el.image_1_name, el.slice_1_index, SliceType[el.slice_1_type]),
            ImageSlice(el.image_2_name, el.slice_2_index, SliceType[el.slice_2_type]))