Skip to content

Commit f491afc

Browse files
authored
Merge pull request #4 from postgresml/small-fixes
Small fixes
2 parents fab7311 + 28545c9 commit f491afc

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

pgml/pgml/model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def find_by_name(cls, name: str):
7979
1,
8080
)
8181
if len(result) == 0:
82-
return None
82+
raise PgMLException(f"Project '{name}' does not exist.")
8383

8484
project = Project()
8585
project.__dict__ = dict(result[0])
@@ -206,14 +206,13 @@ def data(self):
206206
"""
207207
)
208208

209-
print(data)
210209
# Sanity check the data
211210
if len(data) == 0:
212-
PgMLException(
211+
raise PgMLException(
213212
f"Relation `{self.relation_name}` contains no rows. Did you pass the correct `relation_name`?"
214213
)
215214
if self.y_column_name not in data[0]:
216-
PgMLException(
215+
raise PgMLException(
217216
f"Column `{self.y_column_name}` not found. Did you pass the correct `y_column_name`?"
218217
)
219218

@@ -429,6 +428,10 @@ def train(
429428
algorithms = ["linear", "random_forest"]
430429
elif objective == "classification":
431430
algorithms = ["random_forest"]
431+
else:
432+
raise PgMLException(
433+
f"Unknown objective '{objective}', available options are: regression, classification"
434+
)
432435

433436
for algorithm_name in algorithms:
434437
model = Model.create(project, snapshot, algorithm_name)

sql/install.sql

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CREATE EXTENSION IF NOT EXISTS plpython3u;
66
---
77
--- Create schema for models.
88
---
9-
DROP SCHEMA pgml CASCADE;
9+
-- DROP SCHEMA pgml CASCADE;
1010
CREATE SCHEMA IF NOT EXISTS pgml;
1111

1212
CREATE OR REPLACE FUNCTION pgml.auto_updated_at(tbl regclass)
@@ -40,17 +40,17 @@ END;
4040
$$
4141
LANGUAGE plpgsql;
4242

43-
CREATE TABLE pgml.projects(
43+
CREATE TABLE IF NOT EXISTS pgml.projects(
4444
id BIGSERIAL PRIMARY KEY,
4545
name TEXT NOT NULL,
4646
objective TEXT NOT NULL,
4747
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT clock_timestamp(),
4848
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT clock_timestamp()
4949
);
5050
SELECT pgml.auto_updated_at('pgml.projects');
51-
CREATE UNIQUE INDEX projects_name_idx ON pgml.projects(name);
51+
CREATE UNIQUE INDEX IF NOT EXISTS projects_name_idx ON pgml.projects(name);
5252

53-
CREATE TABLE pgml.snapshots(
53+
CREATE TABLE IF NOT EXISTS pgml.snapshots(
5454
id BIGSERIAL PRIMARY KEY,
5555
relation_name TEXT NOT NULL,
5656
y_column_name TEXT NOT NULL,
@@ -62,7 +62,7 @@ CREATE TABLE pgml.snapshots(
6262
);
6363
SELECT pgml.auto_updated_at('pgml.snapshots');
6464

65-
CREATE TABLE pgml.models(
65+
CREATE TABLE IF NOT EXISTS pgml.models(
6666
id BIGSERIAL PRIMARY KEY,
6767
project_id BIGINT NOT NULL,
6868
snapshot_id BIGINT NOT NULL,
@@ -76,17 +76,17 @@ CREATE TABLE pgml.models(
7676
CONSTRAINT project_id_fk FOREIGN KEY(project_id) REFERENCES pgml.projects(id),
7777
CONSTRAINT snapshot_id_fk FOREIGN KEY(snapshot_id) REFERENCES pgml.snapshots(id)
7878
);
79-
CREATE INDEX models_project_id_created_at_idx ON pgml.models(project_id, created_at);
79+
CREATE INDEX IF NOT EXISTS models_project_id_created_at_idx ON pgml.models(project_id, created_at);
8080
SELECT pgml.auto_updated_at('pgml.models');
8181

82-
CREATE TABLE pgml.deployments(
82+
CREATE TABLE IF NOT EXISTS pgml.deployments(
8383
project_id BIGINT NOT NULL,
8484
model_id BIGINT NOT NULL,
8585
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT clock_timestamp(),
8686
CONSTRAINT project_id_fk FOREIGN KEY(project_id) REFERENCES pgml.projects(id),
8787
CONSTRAINT model_id_fk FOREIGN KEY(model_id) REFERENCES pgml.models(id)
8888
);
89-
CREATE INDEX deployments_project_id_created_at_idx ON pgml.deployments(project_id, created_at);
89+
CREATE INDEX IF NOT EXISTS deployments_project_id_created_at_idx ON pgml.deployments(project_id, created_at);
9090
SELECT pgml.auto_updated_at('pgml.deployments');
9191

9292

@@ -103,12 +103,15 @@ $$ LANGUAGE plpython3u;
103103
---
104104
--- Regression
105105
---
106+
DROP FUNCTION IF EXISTS pgml.train(project_name TEXT, objective TEXT, relation_name TEXT, y_column_name TEXT);
106107
CREATE OR REPLACE FUNCTION pgml.train(project_name TEXT, objective TEXT, relation_name TEXT, y_column_name TEXT)
107-
RETURNS VOID
108+
RETURNS TABLE(project_name TEXT, objective TEXT, status TEXT)
108109
AS $$
109110
from pgml.model import train
110111

111112
train(project_name, objective, relation_name, y_column_name)
113+
114+
return [(project_name, objective, "deployed")]
112115
$$ LANGUAGE plpython3u;
113116

114117
---
@@ -121,3 +124,26 @@ AS $$
121124

122125
return Project.find_by_name(project_name).deployed_model.predict([features,])[0]
123126
$$ LANGUAGE plpython3u;
127+
128+
---
129+
--- Quick status check on the system.
130+
---
131+
DROP VIEW IF EXISTS pgml.overview;
132+
CREATE VIEW pgml.overview AS
133+
SELECT
134+
p.name,
135+
d.created_at AS deployed_at,
136+
p.objective,
137+
m.algorithm_name,
138+
m.mean_squared_error,
139+
m.r2_score,
140+
s.relation_name,
141+
s.y_column_name,
142+
s.test_sampling,
143+
s.test_size
144+
FROM pgml.projects p
145+
INNER JOIN pgml.models m ON p.id = m.project_id
146+
INNER JOIN pgml.deployments d ON d.project_id = p.id
147+
AND d.model_id = m.id
148+
INNER JOIN pgml.snapshots s ON s.id = m.snapshot_id
149+
ORDER BY d.created_at DESC;

0 commit comments

Comments
 (0)