-
Notifications
You must be signed in to change notification settings - Fork 27
/
QA_db.py
135 lines (105 loc) · 4.98 KB
/
QA_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Text
import sqlalchemy
import logging
from QA_config import get_database_uri
jobs_logger = logging.getLogger('jobs')
_pool = []
db = SQLAlchemy()
# Create Flask-SQLALchemy models
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(Text, nullable=False, unique=True)
description = db.Column(db.Text, default="")
date = db.Column(db.DateTime)
train_ae_time = db.Column(db.DateTime)
make_patches_time = db.Column(db.DateTime)
iteration = db.Column(db.Integer, default=-1)
embed_iteration = db.Column(db.Integer, default=-1)
images = db.relationship('Image', backref='project', lazy=True)
jobs = db.relationship('Job', backref='project', lazy=True)
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
projId = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)
name = db.Column(db.Text)
path = db.Column(db.Text, unique=True)
height = db.Column(db.Integer)
width = db.Column(db.Integer)
ppixel = db.Column(db.Integer, default=0)
npixel = db.Column(db.Integer, default=0)
nobjects = db.Column(db.Integer, default=0)
date = db.Column(db.DateTime)
rois = db.relationship('Roi', backref='image', lazy=True)
make_patches_time = db.Column(db.DateTime)
superpixel_time = db.Column(db.DateTime)
superpixel_modelid = db.Column(db.Integer, default=-1)
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Roi(db.Model):
id = db.Column(db.Integer, primary_key=True)
imageId = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=False)
name = db.Column(db.Text)
path = db.Column(db.Text)
testingROI = db.Column(db.Integer, default=-1)
height = db.Column(db.Integer)
width = db.Column(db.Integer)
x = db.Column(db.Integer)
y = db.Column(db.Integer)
nobjects = db.Column(db.Integer, default=0)
date = db.Column(db.DateTime)
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Job(db.Model):
id = db.Column(db.Integer, primary_key=True)
projId = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False)
imageId = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=True)
cmd = db.Column(db.Text)
params = db.Column(db.Text)
status = db.Column(db.Text)
retval = db.Column(db.Text)
start_date = db.Column(db.DateTime, server_default=db.func.now())
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class JobidBase(db.Model):
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.Text)
procout = db.Column(db.Text)
# Remove all queued and running jobs from the database
def clear_stale_jobs():
jobs_deleted = Job.query.filter_by(status='QUEUE').delete()
jobs_deleted += Job.query.filter_by(status='RUNNING').delete()
return jobs_deleted
def set_job_status(job_id, status, retval = ""):
if job_id:
engine = sqlalchemy.create_engine(get_database_uri())
engine.connect().execute(f"update job set status= :status, retval = :retval where id={job_id}", status=status, retval = retval)
engine.dispose()
jobs_logger.info(f'Job {job_id} set to status "{status}".')
# Output the project id from the database for a given name:
def get_project_id(project_name):
return Project.query.filter_by(name=project_name).first().id
# Output the index of the latest trained ai
def get_latest_modelid(project_name):
# pull the last training iteration from the database
selected_proj = db.session.query(Project).filter_by(name=project_name).first()
iteration = int(selected_proj.iteration)
# count backwards until we find a trained model for the given index
# --- AJ: Lets comment this out for now to see if/when it breaks. with the improved callback, we should
# never be in this situation now and this code is more of a hack than a solution
# for model_id in range(iteration, -2, -1):
# model_path = f"./projects/{project_name}/models/{model_id}/best_model.pth"
# model_exists = os.path.exists(model_path)
# if (model_exists):
# break
# output the id
return iteration
################################################################################
def get_imagetable(project):
images = db.session.query(Image.id, Image.projId, Image.name, Image.path, Image.height, Image.width, Image.date,
Image.rois, Image.make_patches_time, Image.npixel, Image.ppixel, Image.nobjects,
db.func.count(Roi.id).label('ROIs'),
(db.func.count(Roi.id) - db.func.ifnull(db.func.sum(Roi.testingROI), 0))
.label('trainingROIs')). \
outerjoin(Roi, Roi.imageId == Image.id). \
filter(Image.projId == project.id).group_by(Image.id).all()
return images