示例#1
0
class ProblemLabel(db.Model):
    __tablename__ = 'problem_label'

    id = db.Column(
        UUIDType(binary=False),
        server_default=db.func.uuid_generate_v4(),
        primary_key=True
    )

    problem_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey(Problem.id),
        nullable=False,
    )

    problem = db.relationship(Problem, backref='labels')

    label = db.Column(
        db.Unicode(255),
        nullable=False,
    )

    order_index = db.Column(
        db.Integer,
        nullable=False,
    )

    def __repr__(self):
        return '<Problemlabel label=%r>' % (self.label,)
示例#2
0
class UserProblem(db.Model):
    __tablename__ = 'user_problem'

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    problem_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey(Problem.id),
        nullable=False,
    )

    problem = db.relationship(Problem)

    user_id = db.Column(UUIDType(binary=False),
                        db.ForeignKey(User.id),
                        nullable=False,
                        index=True)

    user = db.relationship(User, backref='problems')

    def __repr__(self):
        return '<UserProblem problem=%r user=%r>' % (self.problem.label,
                                                     self.user.username)
示例#3
0
class LabelEvent(db.Model):
    __tablename__ = 'label_event'

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    data_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey(Dataset.id, ondelete='CASCADE'),
        nullable=False,
        index=True,
    )

    data = db.relationship(Dataset, backref='label_events')

    label_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey('problem_label.id', ondelete='CASCADE'),
        nullable=False,
        index=True,
    )

    label = db.relationship('ProblemLabel')

    label_matches = db.Column(db.Boolean(), nullable=True)

    created_at = db.Column(db.DateTime,
                           server_default=db.func.now(),
                           nullable=False)

    def __repr__(self):
        return '<LabelEvent label=%r>' % self.label
示例#4
0
class User(db.Model, UserMixin):
    __tablename__ = 'user'

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    username = db.Column(db.Unicode(255),
                         nullable=False,
                         unique=True,
                         index=True)

    password = db.Column(PasswordType(schemes=['pbkdf2_sha512']),
                         nullable=False)

    created_at = db.Column(db.DateTime,
                           server_default=db.func.now(),
                           nullable=False)

    is_superuser = db.Column(db.Boolean(),
                             nullable=False,
                             default=False,
                             server_default='FALSE')

    @hybrid_method
    def can_access_problem(self, problem):
        from annotator.models import UserProblem

        return self.is_superuser or db.session.query(UserProblem.id).filter(
            UserProblem.user_id == self.id, UserProblem.problem_id
            == problem.id).count() > 0

    @can_access_problem.expression
    def can_access_problem(cls, problem):
        from annotator.models import Problem, UserProblem

        return cls.is_superuser | Problem.id.in_(
            db.session.query(
                UserProblem.problem_id).filter(UserProblem.user_id == cls.id))

    def __repr__(self):
        return '<User username=%r>' % self.username
示例#5
0
class Problem(db.Model):
    __tablename__ = 'problem'
    query_class = ProblemQuery

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    name = db.Column(
        db.Unicode(255),
        nullable=False,
    )

    created_at = db.Column(db.DateTime,
                           server_default=db.func.now(),
                           nullable=False)

    classification_type = db.Column(db.Enum('binary',
                                            'multi-label',
                                            'multi-class',
                                            name='classification_type_enum'),
                                    default='binary',
                                    server_default='binary')

    @hybrid_property
    def dataset_count(self):
        from annotator.models import Dataset

        return object_session(self).query(Dataset).filter(
            Dataset.problem == self).count()

    @dataset_count.expression
    def dataset_count(cls):
        from annotator.models import Dataset

        return select([
            func.count(Dataset.id)
        ]).where(Dataset.problem_id == cls.id).label('dataset_count')

    def __repr__(self):
        return '<Problem name=%r>' % self.name
示例#6
0
class DatasetLabelProbability(db.Model):
    __tablename__ = 'dataset_label_probability'

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    data_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey(Dataset.id, ondelete='CASCADE'),
        nullable=False,
        index=True,
    )

    data = db.relationship(Dataset, backref='probabilities')

    label_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey('problem_label.id', ondelete='CASCADE'),
        nullable=False,
        index=True,
    )

    label = db.relationship('ProblemLabel')

    probability = db.Column(db.Float(), nullable=True)

    __table_args__ = (
        db.CheckConstraint((0 <= probability) & (probability <= 1),
                           name='chk_dataset_probability'),
        db.UniqueConstraint(
            'data_id',
            'label_id',
            name='uq_dataset_label_probability_data_id_label_id'),
    )

    def __repr__(self):
        return '<Problemlabel label=%r>' % (self.label, )
示例#7
0
class TrainingJob(db.Model):
    __tablename__ = 'training_job'

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    accuracy = db.Column(db.Float(), nullable=False)

    problem_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey(Problem.id, ondelete='CASCADE'),
        nullable=False,
        index=True,
    )

    problem = db.relationship(Problem, backref='training_jobs')

    created_at = db.Column(db.DateTime,
                           server_default=db.func.now(),
                           nullable=False)

    def __repr__(self):
        return '<TrainingJob problem=%r>' % self.problem.id
示例#8
0
class Dataset(db.Model):
    __tablename__ = 'dataset'

    id = db.Column(UUIDType(binary=False),
                   server_default=db.func.uuid_generate_v4(),
                   primary_key=True)

    entity_id = db.Column(db.Unicode(255), index=True)

    problem_id = db.Column(
        UUIDType(binary=False),
        db.ForeignKey(Problem.id, ondelete='CASCADE'),
        nullable=False,
        index=True,
    )

    problem = db.relationship(Problem, backref='dataset')

    table_name = db.Column(
        db.Unicode(255),
        nullable=False,
    )

    free_text = db.Column(db.Text(), nullable=False)

    meta = db.Column(JSONB)

    sort_value = db.Column(db.Float(), nullable=True, index=True)

    __table_args__ = (db.UniqueConstraint(
        'problem_id',
        'table_name',
        'entity_id',
        name='uq_dataset_problem_idtable_name_entity_id'), )

    def matching_label_events(self, matches=True):
        return [x for x in self.label_events if x.label_matches == matches]

    def __repr__(self):
        return '<Dataset table_name=%r>' % self.table_name