class Annotator(db.Model): """Annotator Class""" __tablename__ = "annotator" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text) task_id = db.Column(db.Integer, db.ForeignKey('annotation_task.id'), nullable=False) token = db.Column(db.Text) results = db.relationship('Result', lazy='select', cascade='all', backref='annotator') entries = db.relationship('Entry', lazy='subquery', secondary='annotator_entries') def __init__(self): super(Annotator, self).__init__() def __repr__(self): """Display when printing a annotator task object""" return "<ID: {}, Name: {}}>".format(self.id, self.name) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Entry(db.Model): """Entry Class""" __tablename__ = "entry" id = db.Column(db.Integer, autoincrement=True, primary_key=True) patient_id = db.Column(db.Integer) task_id = db.Column(db.Integer, db.ForeignKey('annotation_task.id'), nullable=False) json = db.Column(db.Text) results = db.relationship('Result', lazy='select', cascade='all', backref='entry') annotators = db.relationship('Annotator', lazy='subquery', secondary='annotator_entries') def __init__(self): super(Entry, self).__init__() def __repr__(self): """Display when printing a result object""" return "<ID: {}, patient ID: {}, json: {}>".format( self.id, self.patient_id, self.json) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class AnnotationTask(db.Model): """Annotation Task Class""" __tablename__ = "annotation_task" id = db.Column(db.Integer, autoincrement=True, primary_key=True) crawler_job_id = db.Column(db.Text) creator_id = db.Column(db.Integer) name = db.Column(db.Text) anno_type = db.Column(db.Integer) description = db.Column(db.Text) scale_entries = db.relationship('ScaleEntry', lazy='select', cascade='all', backref='task') annotators = db.relationship('Annotator', lazy='select', cascade='all', backref='task') entries = db.relationship('Entry', lazy='select', cascade='all', backref='task') def __init__(self): super(AnnotationTask, self).__init__() def __repr__(self): """Display when printing a annotation task object""" return "<ID: {}, Name: {}, type: {}>".format(self.id, self.name, self.anno_type) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class FeatureFeatureSet(db.Model): __tablename__ = 'feature_feature_set' feature_id = db.Column(db.Integer, db.ForeignKey('feature.id'), primary_key=True) feature_set_id = db.Column(db.Integer, db.ForeignKey('feature_set.id'), primary_key=True) feature = db.relationship('Feature', backref=db.backref("feature_feature_set_link")) feature_set = db.relationship('FeatureSet', backref=db.backref("feature_set_feature_link"))
class UserEnvironmentAccess(db.Model): __tablename__ = 'user_environment_access' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) environment_id = db.Column(db.Integer, db.ForeignKey('environment.id'), primary_key=True) user = db.relationship('User', backref=db.backref("user_environment_link")) environment = db.relationship('Environment', backref=db.backref("environment_user_link"))
class UserDataRequest(db.Model): __tablename__ = 'user_data_request' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) datarequest_id = db.Column(db.Integer, db.ForeignKey('data_request.id'), primary_key=True) user = db.relationship('User', backref=db.backref("user_data_request_link")) data_request = db.relationship( 'DataRequest', backref=db.backref("user_data_request_link"))
class AnnotatorEntries(db.Model): __tablename__ = 'annotator_entries' annotator_id = db.Column(db.Integer, db.ForeignKey('annotator.id'), primary_key=True) entry_id = db.Column(db.Integer, db.ForeignKey('entry.id'), primary_key=True) annotator = db.relationship('Annotator', backref=db.backref("annotator_entry_link")) entry = db.relationship('Entry', backref=db.backref("entry_annotator_link"))
class Image(db.Model): """Image class""" __tablename__ = "image" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(LowerCaseText, unique=True, nullable=False) title = db.Column(db.Text) description = db.Column(db.Text, nullable=True) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) environments_based_on_this = db.relationship('Environment', lazy=True, backref='base_image') created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) def __init__(self): super(Image, self).__init__() def __repr__(self): """Display when printing a image object""" return "<ID: {}, description: {}>".format(self.id, self.desription) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class DataRequest(db.Model): """Data Request Class""" __tablename__ = "data_request" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text) request_id = db.Column(db.Text, nullable=False) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) users = db.relationship('User', lazy='subquery', secondary='user_data_request') def __init__(self): super(DataRequest, self).__init__() def __repr__(self): """Display when printing a Request object""" return "<ID: {}, request id: {}, creator id: {}, users: {}>".format( self.id, self.request_id, self.creator_id, self.users) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class User(db.Model): """User class""" __tablename__ = "user" id = db.Column(db.Integer, autoincrement=True, primary_key=True) first_name = db.Column(db.Text, nullable=True) last_name = db.Column(db.Text, nullable=True) username = db.Column(LowerCaseText, unique=True, index=True, nullable=False) email = db.Column(LowerCaseText, unique=True, index=True, nullable=False) password_hash = db.Column(db.Text, nullable=False) created_images = db.relationship('Image', lazy=True, backref='creator') created_environments = db.relationship('Environment', lazy=True, backref='creator') created_ml_models = db.relationship('MLModel', lazy=True, backref='creator') created_features = db.relationship('Feature', lazy=True, backref='creator') created_feature_sets = db.relationship('FeatureSet', lazy=True, backref='creator') # accessible_evironments = db.relationship('Environment', lazy='subquery', secondary='user_environment_access') created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) def __init__(self): super(User, self).__init__() def __repr__(self): """Display when printing a user object""" return "<ID: {}, Name: {} {}, username: {}, e-mail: {}>".format(self.id, self.first_name, self.last_name, self.username, self.email) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns} def hash_password(self, password): self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): return pwd_context.verify(password, self.password_hash)
class FeatureSet(db.Model): """Feature Set Class""" __tablename__ = "feature_set" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text) description = db.Column(db.Text) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) features = db.relationship('Feature', lazy='subquery', secondary='feature_feature_set') ml_models = db.relationship('MLModel', lazy='select', cascade='delete, delete-orphan', backref='feature_set') def __init__(self): super(FeatureSet, self).__init__() def __repr__(self): """Display when printing a feature set object""" return "<ID: {}, Name: {}, description: {}>".format( self.id, self.name, self.description) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class ScaleEntry(db.Model): """Scale Entry Class""" __tablename__ = "scale_entry" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text) code = db.Column(db.Text) task_id = db.Column(db.Integer, db.ForeignKey('annotation_task.id'), nullable=False) results = db.relationship('Result', lazy='select', cascade='all', backref='scale_entry') def __init__(self): super(ScaleEntry, self).__init__() def __repr__(self): """Display when printing a annotation task object""" return "<ID: {}, Name: {}, code: {}>".format(self.id, self.name, self.code) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Feature(db.Model): """Feature Class""" __tablename__ = "feature" id = db.Column(db.Integer, autoincrement=True, primary_key=True) resource = db.Column(db.Text, nullable=False) parameter_name = db.Column(db.Text, nullable=False) value = db.Column(db.Text, nullable=False) name = db.Column(db.Text) output_value_path = db.Column(db.Text, nullable=True) description = db.Column(db.Text) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) feature_sets = db.relationship('FeatureSet', lazy=True, secondary='feature_feature_set') def __init__(self): super(Feature, self).__init__() def __repr__(self): """Display when printing a feature object""" return "<ID: {}, Name: {}, description: {}>".format( self.id, self.name, self.description) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Environment(db.Model): """Environment Class""" __tablename__ = "environment" id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.Text, nullable=False) container_id = db.Column(db.Text, nullable=False) container_name = db.Column(db.Text, nullable=False) status = db.Column(LowerCaseText, nullable=False) jupyter_port = db.Column(db.Text, nullable=False) jupyter_token = db.Column(db.Text, nullable=False) jupyter_url = None description = db.Column(db.Text) creator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # authorized_users = db.relationship('User', lazy='subquery', secondary='user_environment_access') image_id = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=False) ml_models = db.relationship('MLModel', lazy='select', cascade='delete, delete-orphan', backref='environment') created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=datetime.datetime.now) def __init__(self): super(Environment, self).__init__() def __repr__(self): """Display when printing a environment object""" return "<ID: {}, Name: {}, description: {}>".format( self.id, self.name, self.description) def as_dict(self): """Convert object to dictionary""" return {c.name: getattr(self, c.name) for c in self.__table__.columns} def handle_jupyter_data(self): if is_admin_user() or g.user.id == self.creator_id: self.set_jupyter_url() else: self.hide_jupyter_data() def start_jupyter(self): # wait for container api to be up and running wait_for_it(self.container_name, 5000) # start jupyter notebook and get jupyter token resp = requests.post('http://' + self.container_name + ':5000/jupyter').json() self.jupyter_token = str(resp['jupyter_token']) self.status = Environment.Status.running.value def set_jupyter_url(self): # TODO: read host address from os host = 'localhost' self.jupyter_url = host + ':' + self.jupyter_port + '/?token=' + self.jupyter_token def hide_jupyter_data(self): self.jupyter_port = None self.jupyter_token = None self.jupyter_url = None def get_data_directory(self): return '/ketos/environments_data/' + self.container_name class Status(Enum): running = 'running' stopped = 'stopped'