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_get_sessions_length(self): dataset1 = backend.get_dataset('dataset1') dataset2 = backend.get_dataset('dataset2') sessions.create_categorical_image_session(db.session, 'session1', 'prompt', dataset1, ['l1', 'l2', 'l3']) sessions.create_categorical_image_session(db.session, 'session2', 'prompt', dataset1, ['l1', 'l2', 'l3']) sessions.create_categorical_image_session(db.session, 'session3', 'prompt', dataset1, ['l1', 'l2', 'l3']) sessions.create_categorical_image_session(db.session, 'session4', 'prompt', dataset2, ['l1', 'l2', 'l3']) dataset1_sessions = sessions.get_sessions(db.session, dataset1) dataset1_sessions_count = len(dataset1_sessions) dataset2_sessions = sessions.get_sessions(db.session, dataset2) dataset2_sessions_count = len(dataset2_sessions) self.assertEqual(dataset1_sessions_count, 3) self.assertEqual(dataset2_sessions_count, 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)
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 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)
def dataset_overview(dataset_name: str): dataset = backend.get_dataset(dataset_name) if dataset is None: abort(404) images = backend.get_images(dataset) label_sessions = sessions.get_sessions(db.session, dataset) sessions_by_type: Dict[LabelSessionType, List[LabelSession]] = {st: [] for st in LabelSessionType} for sess in label_sessions: sessions_by_type[LabelSessionType[sess.session_type]].append(sess) return render_template('dataset_overview.html', dataset=dataset, images=images, label_sessions=sessions_by_type)
def import_session(dataset_name: str): dataset = backend.get_dataset(dataset_name) if dataset is None: abort(400) form = ImportSessionForm(meta={'csrf': False}) if form.validate_on_submit(): sessions.import_session(db.session, dataset, form.session_name.data, form.session_file.data) return redirect(url_for('dataset_overview', dataset_name=dataset.name)) label_session_count = len(sessions.get_sessions(db.session, dataset)) return render_template('import_session.html', dataset=dataset, form=form, label_session_count=label_session_count)
def create_categorical_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 = CreateCategoricalSessionForm(meta={'csrf': False}) 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(',')] sessions.create_categorical_image_session(db.session, form.session_name.data, form.prompt.data, dataset, label_values) return redirect(url_for('dataset_overview', dataset_name=dataset.name)) return render_template('create_categorical_session.html', dataset=dataset, label_session_count=label_session_count, form=form)
def test_get_sessions_empty(self): dataset = backend.get_dataset('dataset1') label_sessions = sessions.get_sessions(db.session, dataset) label_session_count = len(label_sessions) self.assertEqual(label_session_count, 0)
def dataset_list(): datasets = [(d, backend.get_images(d), sessions.get_sessions(db.session, d)) for d in backend.get_datasets()] return render_template('dataset_list.html', datasets=datasets)