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)
def test_create_categorical_slice_session_metadata(self): dataset = backend.get_dataset('dataset1') sessions.create_categorical_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.CATEGORICAL_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')
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)
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)
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)
def test_create_categorical_slice_session_elements_metadata(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 self.assertEqual(session_elements[0].element_index, 0) self.assertEqual(session_elements[1].element_index, 1) self.assertEqual(session_elements[2].element_index, 2) self.assertEqual(session_elements[3].element_index, 3) self.assertEqual(session_elements[0].image_1_name, 'img1.nii.gz') self.assertEqual(session_elements[1].image_1_name, 'img1.nii.gz') self.assertEqual(session_elements[2].image_1_name, 'img2.nii') self.assertEqual(session_elements[3].image_1_name, 'img3') self.assertEqual(session_elements[0].slice_1_type, SliceType.SAGITTAL.name) self.assertEqual(session_elements[1].slice_1_type, SliceType.SAGITTAL.name) self.assertEqual(session_elements[2].slice_1_type, SliceType.CORONAL.name) self.assertEqual(session_elements[3].slice_1_type, SliceType.AXIAL.name) self.assertEqual(session_elements[0].slice_1_index, 0) self.assertEqual(session_elements[1].slice_1_index, 100) self.assertEqual(session_elements[2].slice_1_index, 1) self.assertEqual(session_elements[3].slice_1_index, 255)