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)
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)
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])
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)